/
TestSetupUtil.java
145 lines (125 loc) · 5.46 KB
/
TestSetupUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package org.jumpmind.symmetric.test;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.jumpmind.db.io.DatabaseIO;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.IDdlBuilder;
import org.jumpmind.db.sql.SqlScript;
import org.jumpmind.properties.EnvironmentSpecificProperties;
import org.jumpmind.symmetric.ClientSymmetricEngine;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.TestConstants;
import org.jumpmind.symmetric.TestUtils;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.db.ISymmetricDialect;
public class TestSetupUtil {
static private ISymmetricEngine engine;
public static ISymmetricEngine prepareForServiceTests() {
if (engine == null) {
removeEmbededdedDatabases();
EnvironmentSpecificProperties properties = new EnvironmentSpecificProperties(new URL[] {getResource("/test-db.properties"), getResource("/symmetric-test.properties")},
"test.root", new String[] { "root" });
properties.setProperty(ParameterConstants.AUTO_CONFIGURE_REG_SVR_SQL_SCRIPT,
"/test-services-setup.sql");
engine = new ClientSymmetricEngine(properties);
dropAndCreateDatabaseTables(properties.getProperty("test.root"), engine);
engine.start();
}
return engine;
}
protected static void dropAndCreateDatabaseTables(String databaseType, ISymmetricEngine engine) {
try {
ISymmetricDialect dialect = engine.getSymmetricDialect();
IDatabasePlatform platform = dialect.getPlatform();
dialect.cleanupTriggers();
String fileName = TestConstants.TEST_DROP_SEQ_SCRIPT + databaseType + "-pre.sql";
URL url = getResource(fileName);
if (url != null) {
new SqlScript(url, dialect.getPlatform().getSqlTemplate(), false).execute(true);
}
Database testDb = getTestDatabase();
IDdlBuilder builder = platform.getDdlBuilder();
String sql = builder.dropTables(testDb);
new SqlScript(sql, dialect.getPlatform().getSqlTemplate(), false).execute(true);
new SqlScript(getResource(TestConstants.TEST_DROP_ALL_SCRIPT),
platform.getSqlTemplate(), false).execute(true);
fileName = TestConstants.TEST_DROP_SEQ_SCRIPT + databaseType + ".sql";
url = getResource(fileName);
if (url != null) {
new SqlScript(url, dialect.getPlatform().getSqlTemplate(), false).execute(true);
}
dialect.purge();
platform.createDatabase(testDb, false, true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected static Database getTestDatabase() throws IOException {
return new DatabaseIO().read(new InputStreamReader(getResource("/test-schema.xml")
.openStream()));
}
protected static boolean isConnectionValid(Properties properties) throws Exception {
try {
Class.forName(properties.getProperty("db.driver"));
Connection c = DriverManager.getConnection(properties.getProperty("db.url"),
properties.getProperty("db.user"), properties.getProperty("db.password"));
c.close();
return true;
} catch (Exception ex) {
TestUtils.getLog().error(
"Could not connect to the test database using the url: "
+ properties.getProperty("db.url") + " and classpath: "
+ System.getProperty("java.class.path"), ex);
return false;
}
}
protected static void removeEmbededdedDatabases() {
File derby = new File("target/derby");
if (derby.exists()) {
try {
TestUtils.getLog().info("Removing derby database files.");
FileUtils.deleteDirectory(derby);
} catch (IOException e) {
TestUtils.getLog().error(e);
}
}
File h2 = new File("target/h2");
if (h2.exists()) {
try {
TestUtils.getLog().info("Removing h2 database files");
FileUtils.deleteDirectory(h2);
} catch (IOException e) {
TestUtils.getLog().error(e);
}
}
File hsqldb = new File("target/hsqldb");
if (hsqldb.exists()) {
try {
TestUtils.getLog().info("Removing hsqldb database files");
FileUtils.deleteDirectory(hsqldb);
} catch (IOException e) {
TestUtils.getLog().error(e);
}
}
File sqlitedb = new File("target/sqlite");
if (sqlitedb.exists() && FileUtils.listFiles(sqlitedb, null, true).size() > 0) {
try {
TestUtils.getLog().info("Removing sqlite database files");
FileUtils.deleteDirectory(sqlitedb);
} catch (IOException e) {
TestUtils.getLog().error(e);
}
}
sqlitedb.mkdirs();
}
protected static URL getResource(String resource) {
return TestSetupUtil.class.getResource(resource);
}
}