<commit_before>package org.robolectric.shadows;

import static android.database.sqlite.SQLiteDatabase.OPEN_READWRITE;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.CancellationSignal;
import android.os.OperationCanceledException;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.res.FileFsFile;
import org.robolectric.util.TempDirectory;

@RunWith(RobolectricTestRunner.class)
public class SQLiteDatabaseTest {
    private SQLiteDatabase database;
    private List<SQLiteDatabase> openDatabases = new ArrayList<>();
    private static final String ANY_VALID_SQL = "SELECT 1";
    private File databasePath;

    @Before
    public void setUp() throws Exception {
        databasePath = RuntimeEnvironment.application.getDatabasePath("database.db");
        databasePath.getParentFile().mkdirs();

        database = openOrCreateDatabase(databasePath);
        database.execSQL("CREATE TABLE table_name (\n" +
                "  id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
                "  first_column VARCHAR(255),\n" +
                "  second_column BINARY,\n" +
                "  name VARCHAR(255),\n" +
                "  big_int INTEGER\n" +
                ");");

        database.execSQL("CREATE TABLE rawtable (\n" +
                "  id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
                "  first_column VARCHAR(255),\n" +
                "  second_column BINARY,\n" +
                "  name VARCHAR(255),\n" +
                "  big_int INTEGER\n" +
                ");");

        database.execSQL("CREATE TABLE exectable (\n" +
                "  id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
                "  first_column VARCHAR(255),\n" +
                "  second_column BINARY,\n" +
                "  name VARCHAR(255),\n" +
                "  big_int INTEGER\n" +
                ");");

        database.execSQL("CREATE TABLE blob_table (\n" +
                "  id INTEGER PRIMARY KEY,\n" +
                "  blob_col BLOB\n" +
                ");");

        String stringColumnValue = "column_value";
        byte[] byteColumnValue = new byte[]{1, 2, 3};

        ContentValues values = new ContentValues();

        values.put("first_column", stringColumnValue);
        values.put("second_column", byteColumnValue);

        database.insert("rawtable", null, values);
        ////////////////////////////////////////////////
        String stringColumnValue2 = "column_value2";
        byte[] byteColumnValue2 = new byte[]{4, 5, 6};
        ContentValues values2 = new ContentValues();

        values2.put("first_column", stringColumnValue2);
        values2.put("second_column", byteColumnValue2);

        database.insert("rawtable", null, values2);
    }

    @After
    public void tearDown() throws Exception {
        for (SQLiteDatabase openDatabase : openDatabases) {
            openDatabase.close();
        }
    }

    @Test
    public void testInsertAndQuery() throws Exception {
        String stringColumnValue = "column_value";
        byte[] byteColumnValue = new byte[]{1, 2, 3};

        ContentValues values = new ContentValues();

        values.put("first_column", stringColumnValue);
        values.put("second_column", byteColumnValue);

        database.insert("table_name", null, values);

        Cursor cursor = database.query("table_name", new String[]{"second_column", "first_column"}, null, null, null, null, null);

        assertThat(cursor.moveToFirst()).isTrue();
<commit_msg>Cache setup database in SQLiteDatabaseTest.<commit_after>package org.robolectric.shadows;

import static android.database.sqlite.SQLiteDatabase.OPEN_READWRITE;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.CancellationSignal;
import android.os.OperationCanceledException;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.res.FileFsFile;
import org.robolectric.util.TempDirectory;

@RunWith(RobolectricTestRunner.class)
public class SQLiteDatabaseTest {
    private static File cachedDbFile = null;

    private SQLiteDatabase database;
    private List<SQLiteDatabase> openDatabases = new ArrayList<>();
    private static final String ANY_VALID_SQL = "SELECT 1";
    private File databasePath;

    @Before
    public void setUp() throws Exception {
        databasePath = RuntimeEnvironment.application.getDatabasePath("database.db");
        databasePath.getParentFile().mkdirs();

        database = setupDatabase(databasePath);
    }

    /**
     * Return a setup database; from a cached file if available.
     */
    private SQLiteDatabase setupDatabase(File databasePath) throws IOException {
        if (cachedDbFile != null) {
            Files.copy(cachedDbFile, databasePath);
            return openOrCreateDatabase(databasePath);
        }

        // cache empty. Setup the database

        SQLiteDatabase database = openOrCreateDatabase(databasePath);

        database.execSQL("CREATE TABLE table_name (\n" +
            "  id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
            "  first_column VARCHAR(255),\n" +
            "  second_column BINARY,\n" +
            "  name VARCHAR(255),\n" +
            "  big_int INTEGER\n" +
            ");");

        database.execSQL("CREATE TABLE rawtable (\n" +
            "  id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
            "  first_column VARCHAR(255),\n" +
            "  second_column BINARY,\n" +
            "  name VARCHAR(255),\n" +
            "  big_int INTEGER\n" +
            ");");

        database.execSQL("CREATE TABLE exectable (\n" +
            "  id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
            "  first_column VARCHAR(255),\n" +
            "  second_column BINARY,\n" +
            "  name VARCHAR(255),\n" +
            "  big_int INTEGER\n" +
            ");");

        database.execSQL("CREATE TABLE blob_table (\n" +
            "  id INTEGER PRIMARY KEY,\n" +
            "  blob_col BLOB\n" +
            ");");

        String stringColumnValue = "column_value";
        byte[] byteColumnValue = new byte[]{1, 2, 3};

        ContentValues values = new ContentValues();

        values.put("first_column", stringColumnValue);
        values.put("second_column", byteColumnValue);

        database.insert("rawtable", null, values);
        ////////////////////////////////////////////////
        String stringColumnValue2 = "column_value2";
        byte[] byteColumnValue2 = new byte[]{4, 5, 6};
        ContentValues values2 = new ContentValues();

        values2.put("first_column", stringColumnValue2);
        values2.put("second_column", byteColumnValue2);

        database.insert("rawtable", null, values2);

        // now cached the result so it can be used by subsequent tests
        cachedDbFile = File.createTempFile("SQLiteDatabaseTest", "dbCache.db");
        cachedDbFile.deleteOnExit();
        Files.copy(databasePath, cachedDbFile);

        return database;
    }

    @After
    public void tearDown() throws Exception {
        for (SQLiteDatabase openDatabase : openDatabases) {
            openDatabase.close();
        }
    }

    @Test
    public void testInsertAndQuery() throws Exception {
        String stringColumnValue = "column_value";
        byte[] byteColumnValue = new byte[]{1, 2, 3};

        ContentValues values = new ContentValues();

        values.put("first_column", stringColumnValue);
        values.put("second_column", byteColumnValue);

        database.insert("table_name", null, values);

        Cursor cursor = database.query("table_name", new String[]{"second_column", "first_column"}, null, null, null, null, null);

        assertThat(cursor.moveToFirst()).isTrue();

{'fraction_non_alphanumeric': 0.11947099355412313, 'fraction_numerical': 0.008446321404756613, 'mean_word_length': 2.7109278350515464, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6204628', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2428, 'n_words': 544}