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)
This commit is contained in:
Sebastian Peters
2018-11-18 00:50:48 +03:30
committed by Yasser Zamani
parent b75c0ba5e1
commit fe630db5f2
2 changed files with 39 additions and 75 deletions
@@ -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));
}
}
public void testNoSubstitution() {
final String value = "val1";
assertThat(substitutor.substitute(value), is(value));
}
}
@@ -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);
}