This is an automated email from the ASF dual-hosted git repository.

yasserzamani pushed a commit to branch struts-2-5-x
in repository https://gitbox.apache.org/repos/asf/struts.git

commit fe630db5f299ba4037be29e3ce12e933d7042b2f
Author: Sebastian Peters <sebastian.pet...@gmail.com>
AuthorDate: Sun Nov 18 00:50:48 2018 +0330

    Refactor environment dependant tests
    
    * use org.apache.commons.lang3.SystemUtils for env detection
    * use assertThat for more concise assertions
    * move setup code to setUp
    * some formatting
    
    (cherry picked from commit 8d341df)
---
 .../config/providers/EnvsValueSubstitutorTest.java | 76 ++++++++--------------
 ...lConfigurationProviderEnvsSubstitutionTest.java | 36 +++-------
 2 files changed, 38 insertions(+), 74 deletions(-)

diff --git 
a/core/src/test/java/com/opensymphony/xwork2/config/providers/EnvsValueSubstitutorTest.java
 
b/core/src/test/java/com/opensymphony/xwork2/config/providers/EnvsValueSubstitutorTest.java
index 4c2ab15..a360c63 100644
--- 
a/core/src/test/java/com/opensymphony/xwork2/config/providers/EnvsValueSubstitutorTest.java
+++ 
b/core/src/test/java/com/opensymphony/xwork2/config/providers/EnvsValueSubstitutorTest.java
@@ -18,70 +18,50 @@
  */
 package com.opensymphony.xwork2.config.providers;
 
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+import org.apache.commons.lang3.SystemUtils;
 import org.apache.struts2.StrutsInternalTestCase;
 
 public class EnvsValueSubstitutorTest extends StrutsInternalTestCase {
 
-    private boolean osIsWindows = false;  // Assume Linux/Unix environment by 
default
+    private ValueSubstitutor substitutor;
 
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-
-        final String os = System.getProperty("os.name");
-        if (os != null && os.startsWith("Windows")) {
-            osIsWindows = true;   // Determined that the OS is Windows (must 
use different environment variables)
-        }
-        else {
-            osIsWindows = false;  // Assume Linux/Unix environment by default
-        }
+        substitutor = new EnvsValueSubstitutor();
     }
 
-    public void testSimpleValue() throws Exception {
-
-        String expected;
-        String actual;
-        final ValueSubstitutor substitutor = new EnvsValueSubstitutor();
-
-        if (osIsWindows) {
-            // given
-            expected = System.getenv("USERNAME");
-
-            // when
-            actual = substitutor.substitute("${env.USERNAME}");
+    public void testEnvSimpleValue() {
+        if (SystemUtils.IS_OS_WINDOWS) {
+            assertThat(substitutor.substitute("${env.USERNAME}"), 
is(System.getenv("USERNAME")));
+        } else {
+            assertThat(substitutor.substitute("${env.USER}"), 
is(System.getenv("USER")));
         }
-        else {
-            // given
-            expected = System.getenv("USER");
-
-            // when
-            actual = substitutor.substitute("${env.USER}");
-        }
-
-        // then
-        assertEquals(expected, actual);
     }
 
-    public void testDefaultValue() throws Exception {
-        // given
-        String expected = "defaultValue";
-        ValueSubstitutor substitutor = new EnvsValueSubstitutor();
+    public void testEnvSimpleDefaultValue() {
+        final String defaultValue = "defaultValue";
+        assertThat(substitutor.substitute("${env.UNKNOWN:" + defaultValue + 
"}"), is(defaultValue));
+    }
 
-        // when
-        String actual = substitutor.substitute("${env.UNKNOWN:" + expected + 
"}");
+    public void testSystemSimpleValue() {
+        final String key = "sysPropKey";
+        final String value = "sysPropValue";
+        System.setProperty(key, value);
 
-        // then
-        assertEquals(expected, actual);
+        assertThat(substitutor.substitute("${" + key + "}"), is(value));
     }
 
-    public void testNoSubstitution() throws Exception {
-        // given
-        ValueSubstitutor substitutor = new EnvsValueSubstitutor();
-
-        // when
-        String actual = substitutor.substitute("val1");
+    public void testSystemSimpleDefaultValue() {
+        final String defaultValue = "defaultValue";
+        assertThat(substitutor.substitute("${UNKNOWN:" + defaultValue + "}"), 
is(defaultValue));
+    }
 
-        // then
-        assertEquals("val1", actual);
+    public void testNoSubstitution() {
+        final String value = "val1";
+        assertThat(substitutor.substitute(value), is(value));
     }
-}
\ No newline at end of file
+}
diff --git 
a/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderEnvsSubstitutionTest.java
 
b/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderEnvsSubstitutionTest.java
index eb76000..7fb20f0 100644
--- 
a/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderEnvsSubstitutionTest.java
+++ 
b/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderEnvsSubstitutionTest.java
@@ -18,55 +18,39 @@
  */
 package com.opensymphony.xwork2.config.providers;
 
-import com.opensymphony.xwork2.config.ConfigurationException;
-import com.opensymphony.xwork2.config.ConfigurationProvider;
+import org.apache.commons.lang3.SystemUtils;
 import org.apache.struts2.StrutsConstants;
 
 public class XmlConfigurationProviderEnvsSubstitutionTest extends 
ConfigurationTestBase {
 
-    private boolean osIsWindows = false;  // Assume Linux/Unix environment by 
default
-
     @Override
     protected void setUp() throws Exception {
         super.setUp();
 
-        final String os = System.getProperty("os.name");
-        if (os != null && os.startsWith("Windows")) {
-            osIsWindows = true;   // Determined that the OS is Windows (must 
use different environment variables)
-        }
-        else {
-            osIsWindows = false;  // Assume Linux/Unix environment by default
-        }
-    }
-
-    public void testSubstitution() throws ConfigurationException {
-        final String filename = 
"com/opensymphony/xwork2/config/providers/xwork-test-envs-substitution.xml";
-        ConfigurationProvider provider = buildConfigurationProvider(filename);
-
-        configurationManager.addContainerProvider(provider);
+        configurationManager.addContainerProvider(buildConfigurationProvider(
+                
"com/opensymphony/xwork2/config/providers/xwork-test-envs-substitution.xml"));
         configurationManager.reload();
         configuration = configurationManager.getConfiguration();
         container = configuration.getContainer();
+    }
 
-        String foo = container.getInstance(String.class, "foo");
-        assertEquals("bar", foo);
+    public void testSubstitution() {
+        assertEquals("bar", container.getInstance(String.class, "foo"));
 
         String user;
-        if (osIsWindows) {
+        if (SystemUtils.IS_OS_WINDOWS) {
             user = container.getInstance(String.class, "username");
             assertEquals(System.getenv("USERNAME"), user);
-        }
-        else {
+        } else {
             user = container.getInstance(String.class, "user");
             assertEquals(System.getenv("USER"), user);
         }
 
         String home;
-        if (osIsWindows) {
+        if (SystemUtils.IS_OS_WINDOWS) {
             home = container.getInstance(String.class, "homedrive.homepath");
             assertEquals("Current HOMEDRIVE.HOMEPATH = " + 
System.getenv("HOMEDRIVE") + System.getenv("HOMEPATH"), home);
-        }
-        else {
+        } else {
             home = container.getInstance(String.class, "home");
             assertEquals("Current HOME = " + System.getenv("HOME"), home);
         }

Reply via email to