From 8fd9643abb605ef6c98699171f1e66561113c7e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20M=C3=A4kinen?= Date: Thu, 27 Jun 2019 16:05:12 +0300 Subject: [PATCH] Initial support for common JUnit DB --- .../fi/nls/oskari/mybatis/MyBatisHelper.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/service-mybatis/src/main/java/fi/nls/oskari/mybatis/MyBatisHelper.java b/service-mybatis/src/main/java/fi/nls/oskari/mybatis/MyBatisHelper.java index 223e8821f4..78c2d4e4aa 100755 --- a/service-mybatis/src/main/java/fi/nls/oskari/mybatis/MyBatisHelper.java +++ b/service-mybatis/src/main/java/fi/nls/oskari/mybatis/MyBatisHelper.java @@ -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 { @@ -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"); } @@ -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);