<commit_before>package com.github.arteam.dropwizard.testing.jdbi;

import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * Date: 1/22/15
 * Time: 8:55 PM
 *
 * @author <NAME>
 */
public class DBIRunner extends BlockJUnit4ClassRunner {

    private static SchemaMigration schemaMigration = new SchemaMigration();

    private DBIContext dbiContext;

    public DBIRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected Object createTest() throws Exception {
        Object test = super.createTest();
        injectTestObjects(test);

        return test;
    }

    private void injectTestObjects(Object test) throws IllegalAccessException {
        Field[] fields = test.getClass().getDeclaredFields();
        if (fields == null) {
            return;
        }
        for (Field field : test.getClass().getDeclaredFields()) {
            Annotation[] annotations = field.getAnnotations();
            if (annotations == null) {
                continue;
            }
            for (Annotation annotation : annotations) {
                if (annotation.annotationType() == DBIHandle.class) {
                    if (!field.getGenericType().equals(Handle.class)) {
                        throw new IllegalArgumentException("Unable inject a DBI handle to a " +
                                "field with type " + field.getGenericType());
                    }
                    if (Modifier.isStatic(field.getModifiers())) {
                        throw new IllegalArgumentException("Unable inject a DBI Handle to a static field");
                    }
                    field.setAccessible(true);
                    field.set(test, dbiContext.getHandle());
                }
                if (annotation.annotationType() == DBIInstance.class) {
                    if (!field.getGenericType().equals(DBI.class)) {
                        throw new IllegalArgumentException("Unable inject a DBI instance to " +
                                "a field with type " + field.getGenericType());
                    }
                    if (Modifier.isStatic(field.getModifiers())) {
                        throw new IllegalArgumentException("Unable inject a DBI instance to a static field");
                    }
                    field.setAccessible(true);
                    field.set(test, dbiContext.getDbi());
                }
            }
        }
    }

    @Override
    protected Statement classBlock(RunNotifier notifier) {
        final Class<?> testClass = getTestClass().getJavaClass();
        final Statement statement = super.classBlock(notifier);
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                // TODO Create database, DBI and handle, migrate populate schema
                dbiContext = new DBIContext();
                schemaMigration.migrate(dbiContext.getHandle());
                try {
                    statement.evaluate();
                } finally {
                    dbiContext.close();
                }
            }
        };
    }

    @Override
    protected Statement methodBlock(final FrameworkMethod method) {
        final Statement statement = super.methodBlock(method);
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                // TODO Load data from method annotations
                statement.evaluate();
                // TODO Cleanup database
            }
        };
    }
}
<commit_msg>Compare annotation classes by equals not by reference<commit_after>package com.github.arteam.dropwizard.testing.jdbi;

import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * Date: 1/22/15
 * Time: 8:55 PM
 *
 * @author <NAME>
 */
public class DBIRunner extends BlockJUnit4ClassRunner {

    private static SchemaMigration schemaMigration = new SchemaMigration();

    private DBIContext dbiContext;

    public DBIRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected Object createTest() throws Exception {
        Object test = super.createTest();
        injectTestObjects(test);

        return test;
    }

    private void injectTestObjects(Object test) throws IllegalAccessException {
        Field[] fields = test.getClass().getDeclaredFields();
        if (fields == null) {
            return;
        }
        for (Field field : test.getClass().getDeclaredFields()) {
            Annotation[] annotations = field.getAnnotations();
            if (annotations == null) {
                continue;
            }
            for (Annotation annotation : annotations) {
                if (annotation.annotationType().equals(DBIHandle.class)) {
                    if (!field.getGenericType().equals(Handle.class)) {
                        throw new IllegalArgumentException("Unable inject a DBI handle to a " +
                                "field with type " + field.getGenericType());
                    }
                    if (Modifier.isStatic(field.getModifiers())) {
                        throw new IllegalArgumentException("Unable inject a DBI Handle to a static field");
                    }
                    field.setAccessible(true);
                    field.set(test, dbiContext.getHandle());
                }
                if (annotation.annotationType().equals(DBIInstance.class)) {
                    if (!field.getGenericType().equals(DBI.class)) {
                        throw new IllegalArgumentException("Unable inject a DBI instance to " +
                                "a field with type " + field.getGenericType());
                    }
                    if (Modifier.isStatic(field.getModifiers())) {
                        throw new IllegalArgumentException("Unable inject a DBI instance to a static field");
                    }
                    field.setAccessible(true);
                    field.set(test, dbiContext.getDbi());
                }
            }
        }
    }

    @Override
    protected Statement classBlock(RunNotifier notifier) {
        final Class<?> testClass = getTestClass().getJavaClass();
        final Statement statement = super.classBlock(notifier);
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                // TODO Create database, DBI and handle, migrate populate schema
                dbiContext = new DBIContext();
                schemaMigration.migrate(dbiContext.getHandle());
                try {
                    statement.evaluate();
                } finally {
                    dbiContext.close();
                }
            }
        };
    }

    @Override
    protected Statement methodBlock(final FrameworkMethod method) {
        final Statement statement = super.methodBlock(method);
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                // TODO Load data from method annotations
                statement.evaluate();
                // TODO Cleanup database
            }
        };
    }
}

{'fraction_non_alphanumeric': 0.0917384693360365, 'fraction_numerical': 0.0030410542321338066, 'mean_word_length': 1.81490727532097, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2498360', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1849, 'n_words': 455}