| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.config; |
| 2 | |
| margaretha | 0c47c65 | 2017-04-19 18:44:40 +0200 | [diff] [blame] | 3 | import static org.junit.Assert.assertNotEquals; |
| 4 | import static org.junit.Assert.assertNotNull; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 5 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 6 | import java.io.File; |
| 7 | import java.io.IOException; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 8 | import java.sql.ResultSet; |
| 9 | import java.sql.SQLException; |
| margaretha | 0c47c65 | 2017-04-19 18:44:40 +0200 | [diff] [blame] | 10 | import java.util.Arrays; |
| 11 | import java.util.HashMap; |
| 12 | import java.util.HashSet; |
| margaretha | 0c47c65 | 2017-04-19 18:44:40 +0200 | [diff] [blame] | 13 | import java.util.Map; |
| 14 | import java.util.Set; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 15 | |
| margaretha | 0c47c65 | 2017-04-19 18:44:40 +0200 | [diff] [blame] | 16 | import org.apache.commons.dbcp2.BasicDataSource; |
| 17 | import org.flywaydb.core.Flyway; |
| 18 | import org.joda.time.DateTime; |
| 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | import org.springframework.context.ApplicationContext; |
| margaretha | 0c47c65 | 2017-04-19 18:44:40 +0200 | [diff] [blame] | 22 | import org.springframework.jdbc.core.RowCallbackHandler; |
| 23 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
| 24 | import org.springframework.jdbc.datasource.SingleConnectionDataSource; |
| 25 | |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 26 | import de.ids_mannheim.korap.authentication.KustvaktAuthenticationManager; |
| margaretha | 0c47c65 | 2017-04-19 18:44:40 +0200 | [diff] [blame] | 27 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | 0c47c65 | 2017-04-19 18:44:40 +0200 | [diff] [blame] | 28 | import de.ids_mannheim.korap.handlers.JDBCClient; |
| margaretha | 69e8adc | 2018-03-15 15:14:22 +0100 | [diff] [blame] | 29 | import de.ids_mannheim.korap.interfaces.EntityHandlerIface; |
| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 30 | import de.ids_mannheim.korap.interfaces.KustvaktBaseDaoInterface; |
| margaretha | 0c47c65 | 2017-04-19 18:44:40 +0200 | [diff] [blame] | 31 | import de.ids_mannheim.korap.interfaces.db.PersistenceClient; |
| margaretha | 0c47c65 | 2017-04-19 18:44:40 +0200 | [diff] [blame] | 32 | import de.ids_mannheim.korap.user.User; |
| 33 | import de.ids_mannheim.korap.utils.TimeUtils; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 34 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 35 | /** |
| 36 | * creates a test user that can be used to access protected functions |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 37 | * |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 38 | * @author hanl |
| 39 | * @date 16/10/2015 |
| 40 | */ |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 41 | public class TestHelper { |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 42 | |
| Michael Hanl | 08aa772 | 2016-06-03 13:34:37 +0200 | [diff] [blame] | 43 | private static Logger jlog = LoggerFactory.getLogger(TestHelper.class); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 44 | private static final Map<String, Object> data = new HashMap<>(); |
| 45 | static { |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 46 | data.put(Attributes.ID, 3); // 2); |
| 47 | data.put(Attributes.USERNAME, "testUser1"); // bodmer funktioniert noch nicht |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 48 | data.put(Attributes.PASSWORD, "testPass2015"); |
| 49 | data.put(Attributes.FIRSTNAME, "test"); |
| 50 | data.put(Attributes.LASTNAME, "user"); |
| 51 | data.put(Attributes.EMAIL, "test@ids-mannheim.de"); |
| 52 | data.put(Attributes.ADDRESS, "Mannheim"); |
| margaretha | 4fa4b06 | 2019-01-28 19:43:30 +0100 | [diff] [blame] | 53 | data.put(Attributes.DEFAULT_FOUNDRY_LEMMA, "test_l"); |
| 54 | data.put(Attributes.DEFAULT_FOUNDRY_POS, "test_p"); |
| 55 | data.put(Attributes.DEFAULT_FOUNDRY_CONSTITUENT, "test_const"); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 56 | } |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 57 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 58 | private ContextHolder beansHolder; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 59 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 60 | public static TestHelper newInstance (ApplicationContext ctx) |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 61 | throws Exception { |
| 62 | TestHelper b = new TestHelper(); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 63 | b.beansHolder = new ContextHolder(ctx) {}; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 64 | return b; |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 67 | |
| 68 | public <T> T getBean (Class<T> type) { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 69 | return this.beansHolder.getBean(type); |
| 70 | } |
| 71 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 72 | |
| 73 | public ContextHolder getContext () { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 74 | return this.beansHolder; |
| 75 | } |
| 76 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 77 | |
| 78 | public <T> T getBean (String name) { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 79 | return (T) this.beansHolder.getBean(name); |
| 80 | } |
| 81 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 82 | |
| margaretha | e7badb3 | 2017-06-22 15:16:00 +0200 | [diff] [blame] | 83 | public TestHelper setupAccount () throws KustvaktException { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 84 | KustvaktBaseDaoInterface dao = getBean(ContextHolder.KUSTVAKT_USERDB); |
| 85 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 86 | KustvaktAuthenticationManager manager = getBean(ContextHolder.KUSTVAKT_AUTHENTICATION_MANAGER); |
| margaretha | e7badb3 | 2017-06-22 15:16:00 +0200 | [diff] [blame] | 87 | // manager.createUserAccount(KustvaktConfiguration.KUSTVAKT_USER, false); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 88 | try { |
| margaretha | 65b6714 | 2017-05-29 16:23:16 +0200 | [diff] [blame] | 89 | User user = getUser(); |
| Michael Hanl | 08aa772 | 2016-06-03 13:34:37 +0200 | [diff] [blame] | 90 | jlog.debug("found user, skipping setup ..."); |
| margaretha | 65b6714 | 2017-05-29 16:23:16 +0200 | [diff] [blame] | 91 | if (!user.getUsername().equals(data.get(Attributes.USERNAME))){ |
| 92 | return this; |
| 93 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 94 | } |
| 95 | catch (RuntimeException e) { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 96 | // do nothing and continue |
| 97 | } |
| 98 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 99 | Map m = getUserCredentials(); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 100 | assertNotNull("userdatabase handler must not be null", dao); |
| 101 | |
| margaretha | 4842673 | 2019-01-21 16:58:28 +0100 | [diff] [blame] | 102 | // try { |
| 103 | // manager.createUserAccount(m, false); |
| 104 | // } |
| 105 | // catch (KustvaktException e) { |
| 106 | // throw new RuntimeException(e); |
| 107 | // /*// do nothing |
| 108 | // jlog.error("Error: {}", e.string()); |
| 109 | // assertNotNull("Test user could not be set up", null);*/ |
| 110 | // } |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 111 | assertNotEquals(0, dao.size()); |
| 112 | return this; |
| 113 | } |
| 114 | |
| margaretha | 69e8adc | 2018-03-15 15:14:22 +0100 | [diff] [blame] | 115 | // EM |
| 116 | @Deprecated |
| 117 | // public TestHelper setupSimpleAccount (String username, String password) { |
| 118 | // KustvaktBaseDaoInterface dao = getBean(ContextHolder.KUSTVAKT_USERDB); |
| 119 | // EntityHandlerIface edao = (EntityHandlerIface) dao; |
| 120 | // try { |
| 121 | // edao.getAccount(username); |
| 122 | // } |
| 123 | // catch (EmptyResultException e) { |
| 124 | // // do nothing |
| 125 | // } |
| 126 | // catch (KustvaktException ex) { |
| 127 | // assertNull("Test user could not be set up", true); |
| 128 | // } |
| 129 | // |
| 130 | // Map m = new HashMap<>(); |
| 131 | // m.put(Attributes.USERNAME, username); |
| 132 | // |
| 133 | // try { |
| 134 | // String hash = ((EncryptionIface) getBean(ContextHolder.KUSTVAKT_ENCRYPTION)) |
| 135 | // .secureHash(password); |
| 136 | // m.put(Attributes.PASSWORD, hash); |
| 137 | // } |
| 138 | // catch (NoSuchAlgorithmException | UnsupportedEncodingException |
| 139 | // | KustvaktException e) { |
| 140 | // // do nohting |
| 141 | // assertNotNull("Exception thrown", null); |
| 142 | // } |
| 143 | // assertNotNull("userdatabase handler must not be null", dao); |
| 144 | // |
| 145 | // try { |
| 146 | // |
| 147 | // int i = edao.createAccount(User.UserFactory.toKorAPUser(m)); |
| 148 | // assert BeansFactory.getKustvaktContext().getUserDBHandler() |
| 149 | // .getAccount((String) data.get(Attributes.USERNAME)) != null; |
| 150 | // assertEquals(1, i); |
| 151 | // } |
| 152 | // catch (KustvaktException e) { |
| 153 | // // do nothing |
| 154 | // assertNull("Test user could not be set up", true); |
| 155 | // } |
| 156 | // return this; |
| 157 | // } |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 158 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 159 | |
| 160 | public User getUser () { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 161 | try { |
| 162 | return ((EntityHandlerIface) getBean(ContextHolder.KUSTVAKT_USERDB)) |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 163 | .getAccount((String) data.get(Attributes.USERNAME)); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 164 | } |
| 165 | catch (KustvaktException e) { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 166 | // do nothing |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 167 | } |
| 168 | throw new RuntimeException("User could not be retrieved!"); |
| 169 | } |
| 170 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 171 | |
| 172 | public TestHelper dropUser (String ... usernames) throws KustvaktException { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 173 | if (usernames == null || usernames.length == 0) { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 174 | KustvaktBaseDaoInterface dao = getBean(ContextHolder.KUSTVAKT_USERDB); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 175 | dao.truncate(); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 176 | } |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 177 | for (String name : Arrays.asList(usernames)) { |
| 178 | if (remove(name)) |
| 179 | break; |
| 180 | } |
| 181 | return this; |
| Michael Hanl | 8335675 | 2016-02-06 14:33:50 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 184 | |
| 185 | private boolean remove (String username) throws KustvaktException { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 186 | EntityHandlerIface dao = getBean(ContextHolder.KUSTVAKT_USERDB); |
| 187 | User us = dao.getAccount(username); |
| 188 | dao.deleteAccount(us.getId()); |
| 189 | return true; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 192 | |
| 193 | public TestHelper truncateAll () { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 194 | String sql = "SELECT Concat('TRUNCATE TABLE ', TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES"; |
| 195 | final Set<String> queries = new HashSet<>(); |
| 196 | PersistenceClient cl = getBean(ContextHolder.KUSTVAKT_POLICIES); |
| 197 | NamedParameterJdbcTemplate source = (NamedParameterJdbcTemplate) cl |
| 198 | .getSource(); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 199 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 200 | source.query(sql, new RowCallbackHandler() { |
| 201 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 202 | public void processRow (ResultSet rs) throws SQLException { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 203 | queries.add(rs.getString(1)); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 204 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 205 | } |
| 206 | }); |
| margaretha | 0a45be1 | 2018-07-12 15:06:30 +0200 | [diff] [blame] | 207 | // System.out.println(queries); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 208 | for (String query : queries) |
| 209 | source.update(query, new HashMap<String, Object>()); |
| 210 | return this; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 213 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 214 | public static Map<String, Object> getUserCredentials () { |
| 215 | return new HashMap<>(data); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 218 | public void close () { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 219 | BeansFactory.closeApplication(); |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 222 | |
| 223 | private TestHelper () { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 224 | |
| 225 | } |
| 226 | |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 227 | // EM: use Spring XML config |
| 228 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 229 | private static PersistenceClient mysql_db () throws IOException { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 230 | BasicDataSource dataSource = new BasicDataSource(); |
| 231 | dataSource.setDriverClassName("com.mysql.jdbc.Driver"); |
| 232 | dataSource.setUrl("jdbc:mysql://localhost:3306/kustvakt_test"); |
| 233 | dataSource.setUsername("mhanl"); |
| 234 | dataSource.setPassword("password"); |
| 235 | JDBCClient client = new JDBCClient(dataSource); |
| 236 | client.setDatabase("mariadb"); |
| 237 | |
| 238 | Flyway fl = new Flyway(); |
| 239 | fl.setDataSource(dataSource); |
| 240 | fl.setLocations("db.mysql"); |
| 241 | fl.migrate(); |
| 242 | |
| 243 | return client; |
| 244 | } |
| 245 | |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 246 | // EM: use Spring XML config |
| 247 | @Deprecated |
| margaretha | f68daa6 | 2017-09-21 02:11:24 +0200 | [diff] [blame] | 248 | protected static PersistenceClient sqlite_db (boolean memory) |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 249 | throws InterruptedException { |
| 250 | SingleConnectionDataSource dataSource = new SingleConnectionDataSource(); |
| 251 | dataSource.setDriverClassName("org.sqlite.JDBC"); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 252 | DateTime t = TimeUtils.getNow(); |
| Michael Hanl | 08aa772 | 2016-06-03 13:34:37 +0200 | [diff] [blame] | 253 | //String name = testclass != null ? testclass.getSimpleName() + "_" : ""; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 254 | |
| 255 | if (memory) |
| 256 | dataSource.setUrl("jdbc:sqlite::memory:"); |
| 257 | else { |
| 258 | File tmp = new File("tmp"); |
| 259 | if (!tmp.exists()) |
| 260 | tmp.mkdirs(); |
| Michael Hanl | 08aa772 | 2016-06-03 13:34:37 +0200 | [diff] [blame] | 261 | dataSource.setUrl("jdbc:sqlite:tmp/sqlite_" + t.getMillis() |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 262 | + ".sqlite"); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 263 | } |
| 264 | dataSource.setSuppressClose(true); |
| 265 | |
| 266 | Flyway fl = new Flyway(); |
| 267 | fl.setDataSource(dataSource); |
| 268 | fl.setLocations("db.sqlite"); |
| 269 | fl.migrate(); |
| 270 | |
| 271 | JDBCClient client = new JDBCClient(dataSource); |
| 272 | client.setDatabase("sqlite"); |
| 273 | return client; |
| 274 | } |
| 275 | |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 276 | // EM: use Spring XML config |
| 277 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 278 | public static PersistenceClient sqlite_db_norm (boolean memory) { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 279 | BasicDataSource dataSource = new BasicDataSource(); |
| 280 | dataSource.setDriverClassName("org.sqlite.JDBC"); |
| 281 | dataSource.setMaxTotal(1); |
| 282 | dataSource.setInitialSize(1); |
| 283 | dataSource.setMaxIdle(1); |
| 284 | dataSource.addConnectionProperty("lazy-init", "true"); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 285 | DateTime t = TimeUtils.getNow(); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 286 | if (memory) |
| 287 | dataSource.setUrl("jdbc:sqlite::memory:"); |
| 288 | else { |
| 289 | File tmp = new File("tmp"); |
| 290 | if (!tmp.exists()) |
| 291 | tmp.mkdirs(); |
| 292 | dataSource.setUrl("jdbc:sqlite:tmp/sqlite_" + t.toString()); |
| 293 | } |
| 294 | |
| 295 | Flyway fl = new Flyway(); |
| 296 | fl.setDataSource(dataSource); |
| 297 | fl.setLocations("db.sqlite"); |
| 298 | fl.migrate(); |
| 299 | |
| 300 | JDBCClient client = new JDBCClient(dataSource); |
| 301 | client.setDatabase("sqlite"); |
| 302 | return client; |
| 303 | } |
| 304 | |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 305 | // EM: use Spring XML config |
| 306 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 307 | public static PersistenceClient h2_emb () throws SQLException { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 308 | BasicDataSource dataSource = new BasicDataSource(); |
| 309 | dataSource.setUrl("jdbc:h2:mem:"); |
| 310 | dataSource.getConnection().nativeSQL("SET MODE MySQL;"); |
| 311 | dataSource.getConnection().commit(); |
| 312 | dataSource.setUsername("sa"); |
| 313 | dataSource.setPassword(""); |
| 314 | dataSource.setDriverClassName("org.h2.Driver"); |
| 315 | |
| 316 | Flyway fl = new Flyway(); |
| 317 | fl.setDataSource(dataSource); |
| 318 | fl.setLocations("db.mysql"); |
| 319 | fl.migrate(); |
| 320 | JDBCClient client = new JDBCClient(dataSource); |
| 321 | client.setDatabase("h2"); |
| 322 | return client; |
| 323 | } |
| 324 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 325 | } |