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
This commit is contained in:
Sebastian Peters
2018-11-17 22:20:48 +01:00
parent 56d86a485b
commit 8d341df2db
2 changed files with 36 additions and 99 deletions
@@ -18,97 +18,50 @@
*/ */
package com.opensymphony.xwork2.config.providers; 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; import org.apache.struts2.StrutsInternalTestCase;
public class EnvsValueSubstitutorTest extends StrutsInternalTestCase { public class EnvsValueSubstitutorTest extends StrutsInternalTestCase {
private boolean osIsWindows = false; // Assume Linux/Unix environment by default private ValueSubstitutor substitutor;
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
substitutor = new EnvsValueSubstitutor();
}
final String os = System.getProperty("os.name"); public void testEnvSimpleValue() {
if (os != null && os.startsWith("Windows")) { if (SystemUtils.IS_OS_WINDOWS) {
osIsWindows = true; // Determined that the OS is Windows (must use different environment variables) assertThat(substitutor.substitute("${env.USERNAME}"), is(System.getenv("USERNAME")));
} } else {
else { assertThat(substitutor.substitute("${env.USER}"), is(System.getenv("USER")));
osIsWindows = false; // Assume Linux/Unix environment by default
} }
} }
public void testEnvSimpleValue() throws Exception { public void testEnvSimpleDefaultValue() {
final String defaultValue = "defaultValue";
String expected; assertThat(substitutor.substitute("${env.UNKNOWN:" + defaultValue + "}"), is(defaultValue));
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() throws Exception { public void testSystemSimpleValue() {
// given final String key = "sysPropKey";
String expected = "defaultValue"; final String value = "sysPropValue";
ValueSubstitutor substitutor = new EnvsValueSubstitutor(); System.setProperty(key, value);
// when assertThat(substitutor.substitute("${" + key + "}"), is(value));
String actual = substitutor.substitute("${env.UNKNOWN:" + expected + "}");
// then
assertEquals(expected, actual);
} }
public void testSystemSimpleValue() throws Exception { public void testSystemSimpleDefaultValue() {
// given final String defaultValue = "defaultValue";
String key = "sysPropKey"; assertThat(substitutor.substitute("${UNKNOWN:" + defaultValue + "}"), is(defaultValue));
String expected = "sysPropValue";
System.setProperty(key, expected);
ValueSubstitutor substitutor = new EnvsValueSubstitutor();
// when
String actual = substitutor.substitute("${" + key + "}");
// then
assertEquals(expected, actual);
} }
public void testSystemSimpleDefaultValue() throws Exception { public void testNoSubstitution() {
// given final String value = "val1";
String expected = "defaultValue"; assertThat(substitutor.substitute(value), is(value));
ValueSubstitutor substitutor = new EnvsValueSubstitutor();
// when
String actual = substitutor.substitute("${UNKNOWN:" + expected + "}");
// then
assertEquals(expected, actual);
}
public void testNoSubstitution() throws Exception {
// given
ValueSubstitutor substitutor = new EnvsValueSubstitutor();
// when
String actual = substitutor.substitute("val1");
// then
assertEquals("val1", actual);
} }
} }
@@ -18,55 +18,39 @@
*/ */
package com.opensymphony.xwork2.config.providers; package com.opensymphony.xwork2.config.providers;
import com.opensymphony.xwork2.config.ConfigurationException; import org.apache.commons.lang3.SystemUtils;
import com.opensymphony.xwork2.config.ConfigurationProvider;
import org.apache.struts2.StrutsConstants; import org.apache.struts2.StrutsConstants;
public class XmlConfigurationProviderEnvsSubstitutionTest extends ConfigurationTestBase { public class XmlConfigurationProviderEnvsSubstitutionTest extends ConfigurationTestBase {
private boolean osIsWindows = false; // Assume Linux/Unix environment by default
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
final String os = System.getProperty("os.name"); configurationManager.addContainerProvider(buildConfigurationProvider(
if (os != null && os.startsWith("Windows")) { "com/opensymphony/xwork2/config/providers/xwork-test-envs-substitution.xml"));
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.reload(); configurationManager.reload();
configuration = configurationManager.getConfiguration(); configuration = configurationManager.getConfiguration();
container = configuration.getContainer(); container = configuration.getContainer();
}
String foo = container.getInstance(String.class, "foo"); public void testSubstitution() {
assertEquals("bar", foo); assertEquals("bar", container.getInstance(String.class, "foo"));
String user; String user;
if (osIsWindows) { if (SystemUtils.IS_OS_WINDOWS) {
user = container.getInstance(String.class, "username"); user = container.getInstance(String.class, "username");
assertEquals(System.getenv("USERNAME"), user); assertEquals(System.getenv("USERNAME"), user);
} } else {
else {
user = container.getInstance(String.class, "user"); user = container.getInstance(String.class, "user");
assertEquals(System.getenv("USER"), user); assertEquals(System.getenv("USER"), user);
} }
String home; String home;
if (osIsWindows) { if (SystemUtils.IS_OS_WINDOWS) {
home = container.getInstance(String.class, "homedrive.homepath"); home = container.getInstance(String.class, "homedrive.homepath");
assertEquals("Current HOMEDRIVE.HOMEPATH = " + System.getenv("HOMEDRIVE") + System.getenv("HOMEPATH"), home); assertEquals("Current HOMEDRIVE.HOMEPATH = " + System.getenv("HOMEDRIVE") + System.getenv("HOMEPATH"), home);
} } else {
else {
home = container.getInstance(String.class, "home"); home = container.getInstance(String.class, "home");
assertEquals("Current HOME = " + System.getenv("HOME"), home); assertEquals("Current HOME = " + System.getenv("HOME"), home);
} }