<commit_before>package io.bootique.jdbc.test.junit;

import io.bootique.BQRuntime;
import io.bootique.jdbc.DataSourceFactory;
import io.bootique.jdbc.test.DatabaseChannel;
import io.bootique.jdbc.test.DefaultDatabaseChannel;
import io.bootique.jdbc.test.Table;
import io.bootique.test.BQTestRuntime;
import org.junit.rules.ExternalResource;

/**
 * An object that sets up a database for the unit tests and shuts it down after the tests
 * are executed. <p>
 * Instances should be annotated within the unit tests with {@link org.junit.Rule} or
 * {@link org.junit.ClassRule}. E.g.:
 * <p>
 * <pre>
 * public class MyTest {
 *
 * 	&#64;Rule
 * 	public TestDatabase db = new TestDatabase(new DerbyDatabase("target/mydb"));
 * }
 * </pre>
 *
 * @since 0.12
 */
public class TestDatabase extends ExternalResource {

    private String dataSourceName;
    private DatabaseChannel channel;

    public TestDatabase(String dataSourceName) {
        this.dataSourceName = dataSourceName;
    }

    @Override
    protected void after() {
        if (channel != null) {
            channel.close();
            channel = null;
        }
    }

    public DatabaseChannel getChannel(BQTestRuntime runtime) {
        return getChannel(runtime.getRuntime());
    }

    public DatabaseChannel getChannel(BQRuntime runtime) {

        // reusing the DataSource from the Bootique runtime. Alternatively starting our own DataSource is probably a
        // bad idea, especially with embedded databases...

        if (this.channel == null) {
            DataSourceFactory dataSourceFactory = runtime.getInstance(DataSourceFactory.class);
            this.channel = new DefaultDatabaseChannel(dataSourceFactory.forName(dataSourceName));
        }

        return this.channel;
    }

    public Table.Builder newTable(BQRuntime runtime, String name) {
        return getChannel(runtime).newTable(name);
    }
}
<commit_msg>JDBC test utils inspired by Cayenne unit tests #12<commit_after>package io.bootique.jdbc.test.junit;

import io.bootique.BQRuntime;
import io.bootique.jdbc.DataSourceFactory;
import io.bootique.jdbc.test.DatabaseChannel;
import io.bootique.jdbc.test.DefaultDatabaseChannel;
import io.bootique.jdbc.test.Table;
import io.bootique.test.BQTestRuntime;
import org.junit.rules.ExternalResource;

/**
 * An object that sets up a database for the unit tests and shuts it down after the tests
 * are executed. <p>
 * Instances should be annotated within the unit tests with {@link org.junit.Rule} or
 * {@link org.junit.ClassRule}. E.g.:
 * <p>
 * <pre>
 * public class MyTest {
 *
 * 	&#64;Rule
 * 	public TestDatabase db = new TestDatabase(new DerbyDatabase("target/mydb"));
 * }
 * </pre>
 *
 * @since 0.12
 */
public class TestDatabase extends ExternalResource {

    private String dataSourceName;
    private DatabaseChannel channel;

    public TestDatabase(String dataSourceName) {
        this.dataSourceName = dataSourceName;
    }

    @Override
    protected void after() {
        if (channel != null) {
            channel.close();
            channel = null;
        }
    }

    public DatabaseChannel getChannel(BQTestRuntime runtime) {
        return getChannel(runtime.getRuntime());
    }

    public DatabaseChannel getChannel(BQRuntime runtime) {

        // reusing the DataSource from the Bootique runtime. Alternatively starting our own DataSource is probably a
        // bad idea, especially with embedded databases...

        if (this.channel == null) {
            DataSourceFactory dataSourceFactory = runtime.getInstance(DataSourceFactory.class);
            this.channel = new DefaultDatabaseChannel(dataSourceFactory.forName(dataSourceName));
        }

        return this.channel;
    }

    public Table.Builder newTable(BQRuntime runtime, String name) {
        return getChannel(runtime).newTable(name);
    }

    public Table.Builder newTable(BQTestRuntime runtime, String name) {
        return getChannel(runtime).newTable(name);
    }

}

{'fraction_non_alphanumeric': 0.09522613065326634, 'fraction_numerical': 0.003015075376884422, 'mean_word_length': 3.212698412698413, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5566198', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1074, 'n_words': 302}