Skip to content

Commit

Permalink
Add a test for environment variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata authored and h3adache committed Aug 20, 2019
1 parent 5848e54 commit ac708a7
Showing 1 changed file with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.migration.Migrator;
import org.apache.ibatis.migration.utils.TestUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.Assertion;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.contrib.java.lang.system.SystemOutRule;

public class SystemPropertyTest {
Expand All @@ -37,31 +41,64 @@ public class SystemPropertyTest {
@Rule
public final SystemOutRule out = new SystemOutRule().enableLog();

@Rule
public final RestoreSystemProperties restoreSysProps = new RestoreSystemProperties();

@Rule
public final EnvironmentVariables envVars = new EnvironmentVariables();

private static File dir;

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

@Test
public void testSystemProperties() throws Exception {
@Before
public void beforeEachTest() {
exit.expectSystemExit();
exit.checkAssertionAfterwards(new Assertion() {
public void checkAssertion() {
assertEquals("", out.getLog());
}
});
out.clearLog();
}

@After
public void afterEachTest() {
out.clearLog();
System.exit(0);
}

// Set system properties
@Test
public void testEnvironmentVariables() throws Exception {
envVars.set("MIGRATIONS_DRIVER", "org.hsqldb.jdbcDriver");
envVars.set("username", "Pocahontas");
envVars.set("var1", "Variable 1");
envVars.set("MIGRATIONS_VAR3", "Variable 3");
envVars.set("migrations_var4", "Variable 4");
envVars.set("MIGRATIONS_VAR5", "Variable 5");

assertEnvironment();
}

@Test
public void testSystemProperties() throws Exception {
System.setProperty("MIGRATIONS_DRIVER", "org.hsqldb.jdbcDriver");
System.setProperty("username", "Pocahontas");
System.setProperty("var1", "Variable 1");
System.setProperty("MIGRATIONS_VAR3", "Variable 3");
System.setProperty("migrations_var4", "Variable 4");
System.setProperty("MIGRATIONS_VAR5", "Variable 5");
// Set duplicate env vars to assert priority
envVars.set("MIGRATIONS_DRIVER", "bogus_driver");
envVars.set("MIGRATIONS_VAR3", "bogus_var3");

assertEnvironment();
}

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

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

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

0 comments on commit ac708a7

Please sign in to comment.