-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
CDILiquibaseTest.java
87 lines (75 loc) · 2.77 KB
/
CDILiquibaseTest.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
package liquibase.integration.cdi;
import liquibase.Scope;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.exception.LiquibaseException;
import liquibase.logging.core.BufferedLogService;
import liquibase.logging.core.CompositeLogService;
import liquibase.logging.core.CompositeLogger;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.logging.Level;
import static org.junit.Assert.*;
/**
* Unit tests for CDILiquibaseTest
*
* @author Aaron Walker (http://github.com/aaronwalker)
*/
public class CDILiquibaseTest {
@Before
@After
public void clearProperty() {
System.clearProperty("liquibase.shouldRun");
System.clearProperty("liquibase.config.shouldRun");
}
private void validateRunningState(boolean shouldBeRunning) {
WeldContainer weld = new Weld().initialize();
CDILiquibase cdiLiquibase = weld.instance().select(CDILiquibase.class).get();
assertNotNull(cdiLiquibase);
assertEquals(shouldBeRunning, cdiLiquibase.isInitialized());
assertEquals(shouldBeRunning, cdiLiquibase.isUpdateSuccessful());
}
@Test
public void shouldntRunWhenShouldRunIsFalse() {
System.setProperty("liquibase.shouldRun", "false");
validateRunningState(false);
}
@Test
public void shouldRunWhenShouldRunIsTrue() {
System.setProperty("liquibase.shouldRun", "true");
validateRunningState(true);
}
@Test
public void shouldntRunWhenConfigShouldRunIsFalse() {
System.setProperty("liquibase.config.shouldRun", "false");
validateRunningState(false);
}
@Test
public void shouldRunWhenConfigShouldRunIsTrue() {
System.setProperty("liquibase.config.shouldRun", "true");
validateRunningState(true);
}
@Test
public void onStartupExceptionsAreCorrectlyHandled() throws Exception {
System.setProperty("liquibase.config.shouldRun", "true");
final CDILiquibase cdi = new CDILiquibase() {
@Override
protected void performUpdate() {
throw new IllegalArgumentException("Tested Exception");
}
};
cdi.config = new CDILiquibaseConfig();
BufferedLogService bufferLog = new BufferedLogService();
Scope.child(Scope.Attr.logService.name(), bufferLog, () -> {
try {
cdi.onStartup();
fail("Did not throw exception");
} catch (IllegalArgumentException e) {
assert e.getMessage().equals("Tested Exception");
assert bufferLog.getLogAsString(Level.SEVERE).contains("SEVERE Tested Exception");
}
});
}
}