/**
 *    Copyright 2010-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.migration.system_property;

import static org.junit.Assert.*;

import java.io.File;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.migration.Migrator;
import org.apache.ibatis.migration.utils.TestUtil;
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.ExpectedSystemExit;
import org.junit.contrib.java.lang.system.SystemOutRule;

public class SystemPropertyTest {

  @Rule
  public final ExpectedSystemExit exit = ExpectedSystemExit.none();

  @Rule
  public final SystemOutRule out = new SystemOutRule().enableLog();

  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 {
    exit.expectSystemExit();
    exit.checkAssertionAfterwards(new Assertion() {
      public void checkAssertion() {
        assertEquals("", out.getLog());
      }
    });
    out.clearLog();

    // Set system properties
    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");

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

    String output = out.getLog();
    assertTrue(output.contains("SUCCESS"));
    assertTrue(output.contains("username: Pocahontas"));
    assertTrue(output.contains("var1: Variable 1"));
    assertTrue(output.contains("var2: ${var2}"));
    assertTrue(output.contains("var3: Variable 3"));
    assertTrue(output.contains("var4: Variable 4"));
    assertTrue(output.contains("var5: Variable 5"));
    assertTrue(output.contains("Var5: Var5 in properties file"));

    out.clearLog();
    System.exit(0);
  }
}

@@ -889,16 +889,65 @@
 stUtil;%0A
+import org.junit.After;%0Aimport org.junit.Before;%0A
 import o
@@ -1110,33 +1110,164 @@
 system.E
-xpectedSystemExit
+nvironmentVariables;%0Aimport org.junit.contrib.java.lang.system.ExpectedSystemExit;%0Aimport org.junit.contrib.java.lang.system.RestoreSystemProperties
 ;%0Aimport
@@ -1507,16 +1507,196 @@
 Log();%0A%0A
+  @Rule%0A  public final RestoreSystemProperties restoreSysProps = new RestoreSystemProperties();%0A%0A  @Rule%0A  public final EnvironmentVariables envVars = new EnvironmentVariables();%0A%0A
   privat
@@ -1879,20 +1879,22 @@
   %7D%0A%0A  @
-Test
+Before
 %0A  publi
@@ -1904,47 +1904,24 @@
 oid 
-testSystemProperties() throws Exception
+beforeEachTest()
  %7B%0A 
@@ -2117,37 +2117,556 @@
 ();%0A
-%0A    // Set system properties
+  %7D%0A%0A  @After%0A  public void afterEachTest() %7B%0A    out.clearLog();%0A    System.exit(0);%0A  %7D%0A%0A  @Test%0A  public void testEnvironmentVariables() throws Exception %7B%0A    envVars.set(%22MIGRATIONS_DRIVER%22, %22org.hsqldb.jdbcDriver%22);%0A    envVars.set(%22username%22, %22Pocahontas%22);%0A    envVars.set(%22var1%22, %22Variable 1%22);%0A    envVars.set(%22MIGRATIONS_VAR3%22, %22Variable 3%22);%0A    envVars.set(%22migrations_var4%22, %22Variable 4%22);%0A    envVars.set(%22MIGRATIONS_VAR5%22, %22Variable 5%22);%0A%0A    assertEnvironment();%0A  %7D%0A%0A  @Test%0A  public void testSystemProperties() throws Exception %7B
 %0A   
@@ -2995,24 +2995,244 @@
 riable 5%22);%0A
+    // Set duplicate env vars to assert priority%0A    envVars.set(%22MIGRATIONS_DRIVER%22, %22bogus_driver%22);%0A    envVars.set(%22MIGRATIONS_VAR3%22, %22bogus_var3%22);%0A%0A    assertEnvironment();%0A  %7D%0A%0A  private void assertEnvironment() %7B
 %0A    Migrato
@@ -3784,41 +3784,83 @@
     
-out.clearLog();%0A    System.exit(0
+Migrator.main(TestUtil.args(%22--path=%22 + dir.getAbsolutePath(), %22down%22, %221%22)
 );%0A 

Add a test for environment variables.