22
22
import org .apache .ibatis .io .Resources ;
23
23
import org .apache .ibatis .migration .Migrator ;
24
24
import org .apache .ibatis .migration .utils .TestUtil ;
25
+ import org .junit .After ;
26
+ import org .junit .Before ;
25
27
import org .junit .BeforeClass ;
26
28
import org .junit .Rule ;
27
29
import org .junit .Test ;
28
30
import org .junit .contrib .java .lang .system .Assertion ;
31
+ import org .junit .contrib .java .lang .system .EnvironmentVariables ;
29
32
import org .junit .contrib .java .lang .system .ExpectedSystemExit ;
33
+ import org .junit .contrib .java .lang .system .RestoreSystemProperties ;
30
34
import org .junit .contrib .java .lang .system .SystemOutRule ;
31
35
32
36
public class SystemPropertyTest {
@@ -37,31 +41,64 @@ public class SystemPropertyTest {
37
41
@ Rule
38
42
public final SystemOutRule out = new SystemOutRule ().enableLog ();
39
43
44
+ @ Rule
45
+ public final RestoreSystemProperties restoreSysProps = new RestoreSystemProperties ();
46
+
47
+ @ Rule
48
+ public final EnvironmentVariables envVars = new EnvironmentVariables ();
49
+
40
50
private static File dir ;
41
51
42
52
@ BeforeClass
43
53
public static void init () throws Exception {
44
54
dir = Resources .getResourceAsFile ("org/apache/ibatis/migration/system_property/testdir" );
45
55
}
46
56
47
- @ Test
48
- public void testSystemProperties () throws Exception {
57
+ @ Before
58
+ public void beforeEachTest () {
49
59
exit .expectSystemExit ();
50
60
exit .checkAssertionAfterwards (new Assertion () {
51
61
public void checkAssertion () {
52
62
assertEquals ("" , out .getLog ());
53
63
}
54
64
});
55
65
out .clearLog ();
66
+ }
67
+
68
+ @ After
69
+ public void afterEachTest () {
70
+ out .clearLog ();
71
+ System .exit (0 );
72
+ }
56
73
57
- // Set system properties
74
+ @ Test
75
+ public void testEnvironmentVariables () throws Exception {
76
+ envVars .set ("MIGRATIONS_DRIVER" , "org.hsqldb.jdbcDriver" );
77
+ envVars .set ("username" , "Pocahontas" );
78
+ envVars .set ("var1" , "Variable 1" );
79
+ envVars .set ("MIGRATIONS_VAR3" , "Variable 3" );
80
+ envVars .set ("migrations_var4" , "Variable 4" );
81
+ envVars .set ("MIGRATIONS_VAR5" , "Variable 5" );
82
+
83
+ assertEnvironment ();
84
+ }
85
+
86
+ @ Test
87
+ public void testSystemProperties () throws Exception {
58
88
System .setProperty ("MIGRATIONS_DRIVER" , "org.hsqldb.jdbcDriver" );
59
89
System .setProperty ("username" , "Pocahontas" );
60
90
System .setProperty ("var1" , "Variable 1" );
61
91
System .setProperty ("MIGRATIONS_VAR3" , "Variable 3" );
62
92
System .setProperty ("migrations_var4" , "Variable 4" );
63
93
System .setProperty ("MIGRATIONS_VAR5" , "Variable 5" );
94
+ // Set duplicate env vars to assert priority
95
+ envVars .set ("MIGRATIONS_DRIVER" , "bogus_driver" );
96
+ envVars .set ("MIGRATIONS_VAR3" , "bogus_var3" );
97
+
98
+ assertEnvironment ();
99
+ }
64
100
101
+ private void assertEnvironment () {
65
102
Migrator .main (TestUtil .args ("--path=" + dir .getAbsolutePath (), "up" , "1" , "--trace" ));
66
103
67
104
String output = out .getLog ();
@@ -74,7 +111,6 @@ public void checkAssertion() {
74
111
assertTrue (output .contains ("var5: Variable 5" ));
75
112
assertTrue (output .contains ("Var5: Var5 in properties file" ));
76
113
77
- out .clearLog ();
78
- System .exit (0 );
114
+ Migrator .main (TestUtil .args ("--path=" + dir .getAbsolutePath (), "down" , "1" ));
79
115
}
80
116
}
0 commit comments