/
TestSetupUtil.java
125 lines (106 loc) · 4.77 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
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 clientDbDir = new File("target/clientdbs");
if (clientDbDir.exists()) {
try {
TestUtils.getLog().info("Removing client database files.");
FileUtils.deleteDirectory(clientDbDir);
} catch (IOException e) {
TestUtils.getLog().error(e);
}
}
File rootDbDir = new File("target/rootdbs");
if (rootDbDir.exists()) {
try {
TestUtils.getLog().info("Removing root database files");
FileUtils.deleteDirectory(rootDbDir);
} catch (IOException e) {
TestUtils.getLog().error(e);
}
}
}
protected static URL getResource(String resource) {
return TestSetupUtil.class.getResource(resource);
}
}