From fe630db5f299ba4037be29e3ce12e933d7042b2f Mon Sep 17 00:00:00 2001 From: Sebastian Peters Date: Sun, 18 Nov 2018 00:50:48 +0330 Subject: [PATCH] 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) --- .../providers/EnvsValueSubstitutorTest.java | 78 +++++++------------ ...igurationProviderEnvsSubstitutionTest.java | 36 +++------ 2 files changed, 39 insertions(+), 75 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 4c2ab1519..a360c63ea 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(); + substitutor = new EnvsValueSubstitutor(); + } - 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 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"))); } } - 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}"); - } - else { - // given - expected = System.getenv("USER"); - - // when - actual = substitutor.substitute("${env.USER}"); - } - - // then - assertEquals(expected, actual); + public void testEnvSimpleDefaultValue() { + final String defaultValue = "defaultValue"; + assertThat(substitutor.substitute("${env.UNKNOWN:" + defaultValue + "}"), is(defaultValue)); } - public void testDefaultValue() throws Exception { - // given - String expected = "defaultValue"; - ValueSubstitutor substitutor = new EnvsValueSubstitutor(); + public void testSystemSimpleValue() { + final String key = "sysPropKey"; + final String value = "sysPropValue"; + System.setProperty(key, value); - // when - String actual = substitutor.substitute("${env.UNKNOWN:" + expected + "}"); - - // 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"); - - // then - assertEquals("val1", actual); + public void testSystemSimpleDefaultValue() { + final String defaultValue = "defaultValue"; + assertThat(substitutor.substitute("${UNKNOWN:" + defaultValue + "}"), is(defaultValue)); } -} \ No newline at end of file + + public void testNoSubstitution() { + final String value = "val1"; + assertThat(substitutor.substitute(value), is(value)); + } +} 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 eb7600013..7fb20f0de 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); }