<commit_before>package com.amplitude.api;

import static org.junit.Assert.fail;

import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;

import java.lang.InterruptedException;
import static java.util.concurrent.TimeUnit.SECONDS;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowLooper;
import org.robolectric.Shadows;

import android.content.Context;
import android.util.Pair;

public class BaseTest {

    protected AmplitudeClient amplitude;
    protected Context context;
    protected MockWebServer server;

    public void setUp() throws Exception {
        setUp(true);
    }

    /**
     * Handle common test setup for default cases. Specific cases can
     * override the defaults by providing an amplitude object before
     * calling this method or passing false for withServer.
     */
    public void setUp(boolean withServer) throws Exception {
        ShadowApplication.getInstance().setPackageName("com.amplitude.test");
        context = ShadowApplication.getInstance().getApplicationContext();

        // Clear the database helper for each test. Better to have isolation.
        // See https://github.com/robolectric/robolectric/issues/569
        // and https://github.com/robolectric/robolectric/issues/1622
        DatabaseHelper.instance = null;

        if (withServer) {
            server = new MockWebServer();
            server.play();
        }

        if (amplitude == null) {
            amplitude = new AmplitudeClient();
            // this sometimes deadlocks with lock contention by logThread and httpThread for
            // a ShadowWrangler instance and the ShadowLooper class
            // Might be a sign of a bug, or just Robolectric's bug.
            amplitude.initialize(context, "1cc2c1978ebab0f6451112a8f5df4f4e");
        }

        if (server != null) {
            amplitude.url = server.getUrl("/").toString();            
        }
    }

    public void tearDown() throws Exception {
        if (amplitude != null) {
            amplitude.logThread.getLooper().quit();
            amplitude.httpThread.getLooper().quit();
            amplitude = null;
        }

        if (server != null) {
            server.shutdown();
        }
    }

    public RecordedRequest runRequest() {
        server.enqueue(new MockResponse().setBody("success"));
        ShadowLooper httplooper = Shadows.shadowOf(amplitude.httpThread.getLooper());
        httplooper.runToEndOfTasks();

        try {
            return server.takeRequest(1, SECONDS);
        } catch (InterruptedException e) {
            return null;
        }
    }

    public RecordedRequest sendEvent(AmplitudeClient amplitude, String name, JSONObject props) {
        Shadows.shadowOf(amplitude.logThread.getLooper()).runToEndOfTasks();
        amplitude.logEvent(name, props);
        Shadows.shadowOf(amplitude.logThread.getLooper()).runToEndOfTasks();
        Shadows.shadowOf(amplitude.logThread.getLooper()).runToEndOfTasks();
<commit_msg>fixed an issue where db state was not being reset between tests<commit_after>package com.amplitude.api;

import static org.junit.Assert.fail;

import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;

import java.lang.InterruptedException;
import static java.util.concurrent.TimeUnit.SECONDS;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowLooper;
import org.robolectric.Shadows;

import android.content.Context;
import android.util.Pair;

public class BaseTest {

    protected AmplitudeClient amplitude;
    protected Context context;
    protected MockWebServer server;

    public void setUp() throws Exception {
        setUp(true);
    }

    /**
     * Handle common test setup for default cases. Specific cases can
     * override the defaults by providing an amplitude object before
     * calling this method or passing false for withServer.
     */
    public void setUp(boolean withServer) throws Exception {
        ShadowApplication.getInstance().setPackageName("com.amplitude.test");
        context = ShadowApplication.getInstance().getApplicationContext();

        // Clear the database helper for each test. Better to have isolation.
        // See https://github.com/robolectric/robolectric/issues/569
        // and https://github.com/robolectric/robolectric/issues/1622
        DatabaseHelper.instance = null;

        if (withServer) {
            server = new MockWebServer();
            server.play();
        }

        if (amplitude == null) {
            amplitude = new AmplitudeClient();
            // this sometimes deadlocks with lock contention by logThread and httpThread for
            // a ShadowWrangler instance and the ShadowLooper class
            // Might be a sign of a bug, or just Robolectric's bug.
            amplitude.initialize(context, "1cc2c1978ebab0f6451112a8f5df4f4e");
        }

        if (server != null) {
            amplitude.url = server.getUrl("/").toString();            
        }
    }

    public void tearDown() throws Exception {
        if (amplitude != null) {
            amplitude.logThread.getLooper().quit();
            amplitude.httpThread.getLooper().quit();
            amplitude = null;
        }

        if (server != null) {
            server.shutdown();
        }

        DatabaseHelper.instance = null;
    }

    public RecordedRequest runRequest() {
        server.enqueue(new MockResponse().setBody("success"));
        ShadowLooper httplooper = Shadows.shadowOf(amplitude.httpThread.getLooper());
        httplooper.runToEndOfTasks();

        try {
            return server.takeRequest(1, SECONDS);
        } catch (InterruptedException e) {
            return null;
        }
    }

    public RecordedRequest sendEvent(AmplitudeClient amplitude, String name, JSONObject props) {
        Shadows.shadowOf(amplitude.logThread.getLooper()).runToEndOfTasks();
        amplitude.logEvent(name, props);
        Shadows.shadowOf(amplitude.logThread.getLooper()).runToEndOfTasks();
        Shadows.shadowOf(amplitude.logThread.getLooper()).runToEndOfTasks();

{'fraction_non_alphanumeric': 0.09514263685427911, 'fraction_numerical': 0.008018504240555127, 'mean_word_length': 2.786339754816112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '2960834', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1740, 'n_words': 409}