-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
TestContext.java
95 lines (79 loc) · 4.01 KB
/
TestContext.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
package liquibase.test;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.core.SQLiteDatabase;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.ResourceAccessor;
import liquibase.database.core.MockDatabase;
import java.io.File;
import java.net.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Controls the database connections for running tests.
* For times we aren't wanting to run the database-hitting tests, set the "test.databases" system property
* to be a comma-separated list of the databses we want to test against. The string is checked against the database
* url.
*/
public class TestContext {
private static TestContext instance = new TestContext();
private Set<Database> allDatabases;
private ResourceAccessor resourceAccessor;
public static TestContext getInstance() {
return instance;
}
public Set<Database> getAllDatabases() {
if (allDatabases == null) {
allDatabases = new HashSet<Database>();
allDatabases.addAll(DatabaseFactory.getInstance().getImplementedDatabases());
List<Database> toRemove = new ArrayList<Database>();
for (Database database : allDatabases) {
if ((database instanceof SQLiteDatabase) //todo: re-enable sqlite testing
|| (database instanceof MockDatabase)) {
toRemove.add(database);
}
database.setCanCacheLiquibaseTableInfo(false);
}
allDatabases.removeAll(toRemove);
}
return allDatabases;
}
public File findCoreJvmProjectRoot() throws URISyntaxException {
return new File(findCoreProjectRoot().getParentFile(), "liquibase-core");
}
public File findIntegrationTestProjectRoot() throws URISyntaxException {
return new File(findCoreProjectRoot().getParentFile(), "liquibase-integration-tests");
}
public File findCoreProjectRoot() throws URISyntaxException {
URI uri = new URI(this.getClass().getClassLoader().getResource("liquibase/test/TestContext.class").toExternalForm());
if(!uri.isOpaque()) {
File thisClassFile = new File(uri);
return thisClassFile.getParentFile().getParentFile().getParentFile().getParentFile().getParentFile();
}
uri = new URI(this.getClass().getClassLoader().getResource("liquibase/integration/commandline/Main.class").toExternalForm());
if(!uri.isOpaque()) {
File thisClassFile = new File(uri);
return new File(thisClassFile.getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParentFile(), "liquibase-core");
}
uri = new URI(this.getClass().getClassLoader().getResource("liquibase/dbtest/AbstractIntegrationTest.class").toExternalForm());
if(!uri.isOpaque()) {
File thisClassFile = new File(uri);
return new File(thisClassFile.getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParentFile(), "liquibase-core");
}
throw new IllegalStateException("Cannot find liquibase-core project root");
}
public ResourceAccessor getTestResourceAccessor() throws URISyntaxException, MalformedURLException {
if (resourceAccessor == null) {
resourceAccessor = new ClassLoaderResourceAccessor(new URLClassLoader(new URL[]{
new File(TestContext.getInstance().findCoreJvmProjectRoot(), "/target/classes").toURI().toURL(),
new File(TestContext.getInstance().findCoreJvmProjectRoot(), "/target/test-classes").toURI()
.toURL(),
new File(TestContext.getInstance().findCoreProjectRoot(), "/target/classes").toURI().toURL(),
new File(TestContext.getInstance().findCoreProjectRoot(), "/target/test-classes").toURI().toURL()
}));
}
return resourceAccessor;
}
}