<commit_before>package com.xtremelabs.robolectric.shadows;

import android.app.Activity;
import android.content.Context;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;

import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(WithTestDefaultsRunner.class)
public class ContextTest {
    private Context context;

    @Before
    public void setUp() throws Exception {
        context = new Activity();
        deleteDir(context.getFilesDir());
        deleteDir(context.getCacheDir());

        File[] files = context.getFilesDir().listFiles();
        assertNotNull(files);
        assertThat(files.length, is(0));

        File[] cachedFiles = context.getFilesDir().listFiles();
        assertNotNull(cachedFiles);
        assertThat(cachedFiles.length, is(0));
    }

    @After
    public void after() {
    	deleteDir(context.getFilesDir());
    	deleteDir(context.getCacheDir());
    	deleteDir(context.getExternalCacheDir());
    	deleteDir(context.getExternalFilesDir(null));
    }

    public void deleteDir(File path) {
		if (path.isDirectory()) {
			File[] files = path.listFiles();
            assertNotNull(files);
			for (File f : files) {
				deleteDir(f);
			}
		}
		path.delete();
	}
    
    @Test
    public void shouldGetApplicationDataDirectory() throws IOException {
        File dataDir = new File(context.getFilesDir(), "data");
        assertThat(dataDir.mkdir(), is(true));

        dataDir = context.getDir("data", Context.MODE_PRIVATE);
        assertThat(dataDir, not(nullValue()));
        assertThat(dataDir.exists(), is(true));
    }


    @Test
    public void shouldCreateIfDoesNotExistAndGetApplicationDataDirectory() {
        File dataDir = new File(ShadowContext.FILES_DIR, "data");<commit_msg>For consistency, instantiate a new file in the same way as in shouldCreateIfDoesNotExistAndGetApplicationDataDirectory() test<commit_after>package com.xtremelabs.robolectric.shadows;

import android.app.Activity;
import android.content.Context;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;

import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(WithTestDefaultsRunner.class)
public class ContextTest {
    private Context context;

    @Before
    public void setUp() throws Exception {
        context = new Activity();
        deleteDir(context.getFilesDir());
        deleteDir(context.getCacheDir());

        File[] files = context.getFilesDir().listFiles();
        assertNotNull(files);
        assertThat(files.length, is(0));

        File[] cachedFiles = context.getFilesDir().listFiles();
        assertNotNull(cachedFiles);
        assertThat(cachedFiles.length, is(0));
    }

    @After
    public void after() {
    	deleteDir(context.getFilesDir());
    	deleteDir(context.getCacheDir());
    	deleteDir(context.getExternalCacheDir());
    	deleteDir(context.getExternalFilesDir(null));
    }

    public void deleteDir(File path) {
		if (path.isDirectory()) {
			File[] files = path.listFiles();
            assertNotNull(files);
			for (File f : files) {
				deleteDir(f);
			}
		}
		path.delete();
	}
    
    @Test
    public void shouldGetApplicationDataDirectory() throws IOException {
        File dataDir = new File(ShadowContext.FILES_DIR, "data");
        assertThat(dataDir.mkdir(), is(true));

        dataDir = context.getDir("data", Context.MODE_PRIVATE);
        assertThat(dataDir, not(nullValue()));
        assertThat(dataDir.exists(), is(true));
    }


    @Test
    public void shouldCreateIfDoesNotExistAndGetApplicationDataDirectory() {
        File dataDir = new File(ShadowContext.FILES_DIR, "data");
{'fraction_non_alphanumeric': 0.1160633484162896, 'fraction_numerical': 0.0009049773755656109, 'mean_word_length': 3.826419213973799, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7321015', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1272, 'n_words': 258}