Skip to content

Commit ac708a7

Browse files
harawatah3adache
authored andcommitted
Add a test for environment variables.
1 parent 5848e54 commit ac708a7

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

src/test/java/org/apache/ibatis/migration/system_property/SystemPropertyTest.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
import org.apache.ibatis.io.Resources;
2323
import org.apache.ibatis.migration.Migrator;
2424
import org.apache.ibatis.migration.utils.TestUtil;
25+
import org.junit.After;
26+
import org.junit.Before;
2527
import org.junit.BeforeClass;
2628
import org.junit.Rule;
2729
import org.junit.Test;
2830
import org.junit.contrib.java.lang.system.Assertion;
31+
import org.junit.contrib.java.lang.system.EnvironmentVariables;
2932
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
33+
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
3034
import org.junit.contrib.java.lang.system.SystemOutRule;
3135

3236
public class SystemPropertyTest {
@@ -37,31 +41,64 @@ public class SystemPropertyTest {
3741
@Rule
3842
public final SystemOutRule out = new SystemOutRule().enableLog();
3943

44+
@Rule
45+
public final RestoreSystemProperties restoreSysProps = new RestoreSystemProperties();
46+
47+
@Rule
48+
public final EnvironmentVariables envVars = new EnvironmentVariables();
49+
4050
private static File dir;
4151

4252
@BeforeClass
4353
public static void init() throws Exception {
4454
dir = Resources.getResourceAsFile("org/apache/ibatis/migration/system_property/testdir");
4555
}
4656

47-
@Test
48-
public void testSystemProperties() throws Exception {
57+
@Before
58+
public void beforeEachTest() {
4959
exit.expectSystemExit();
5060
exit.checkAssertionAfterwards(new Assertion() {
5161
public void checkAssertion() {
5262
assertEquals("", out.getLog());
5363
}
5464
});
5565
out.clearLog();
66+
}
67+
68+
@After
69+
public void afterEachTest() {
70+
out.clearLog();
71+
System.exit(0);
72+
}
5673

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 {
5888
System.setProperty("MIGRATIONS_DRIVER", "org.hsqldb.jdbcDriver");
5989
System.setProperty("username", "Pocahontas");
6090
System.setProperty("var1", "Variable 1");
6191
System.setProperty("MIGRATIONS_VAR3", "Variable 3");
6292
System.setProperty("migrations_var4", "Variable 4");
6393
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+
}
64100

101+
private void assertEnvironment() {
65102
Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "up", "1", "--trace"));
66103

67104
String output = out.getLog();
@@ -74,7 +111,6 @@ public void checkAssertion() {
74111
assertTrue(output.contains("var5: Variable 5"));
75112
assertTrue(output.contains("Var5: Var5 in properties file"));
76113

77-
out.clearLog();
78-
System.exit(0);
114+
Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "down", "1"));
79115
}
80116
}

0 commit comments

Comments
 (0)