Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for common JUnit DB #412

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

public class MyBatisHelper {

Expand All @@ -15,6 +18,10 @@ public static SqlSessionFactory initMyBatis(DataSource ds, Class<?>... mappers)
}

public static Configuration getConfig(DataSource ds, Class<?>... mappers) {
if (ds == null) {
// TODO: anything better to detect that we are in test env?
ds = getTestDS();
}
if (ds == null) {
throw new NullPointerException("Tried initializing MyBatis without a datasource");
}
Expand All @@ -26,6 +33,18 @@ public static Configuration getConfig(DataSource ds, Class<?>... mappers) {
return configuration;
}

private static DataSource getTestDS() {
try {
// try to dig up TestHelper that is only available while testing to get a mem-based datasource
Class helper = Class.forName("fi.nls.test.util.TestHelper");
Method m = helper.getMethod("createMemDBforUnitTest", List.class);
// TODO: call with DB initializing scripts to actually use this
return (DataSource) m.invoke(null, (Object) Collections.emptyList());
} catch (Exception e) {
throw new RuntimeException("Not testing");
}
}

public static void addAliases(Configuration config, Class<?>... aliases) {
for (Class<?> alias : aliases) {
config.getTypeAliasRegistry().registerAlias(alias);
Expand Down