Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

AS7-2169, add support for envionrment var refs in ExpressionValues.

  • Loading branch information...
commit b9ae2704cf17e5e4b83987f2e9cafeed122c6327 1 parent 75d3ad1
@starksm64 starksm64 authored
View
1  .gitignore
@@ -2,6 +2,7 @@ target
*.iml
*.ipr
*.iws
+*.idea
Test.java
# ignore eclipse files
.project
View
28 src/main/java/org/jboss/dmr/ExpressionValue.java
@@ -109,17 +109,19 @@ ModelValue resolve() {
/**
* Replace properties of the form:
- * <code>${<i>&lt;name&gt;[</i>,<i>&lt;name2&gt;[</i>,<i>&lt;name3&gt;...]][</i>:<i>&lt;default&gt;]</i>}</code>
+ * <code>${<i>&lt;[env.]name&gt;[</i>,<i>&lt;[env.]name2&gt;[</i>,<i>&lt;[env.]name3&gt;...]][</i>:<i>&lt;default&gt;]</i>}</code>
*
- * @param value
- * @return
+ * @param value - either a system property or environment variable reference
+ * @return the value of the system property or environment variable referenced if
+ * it exists
*/
private static String replaceProperties(final String value) {
final StringBuilder builder = new StringBuilder();
final int len = value.length();
- int state = 0;
+ int state = INITIAL;
int start = -1;
int nameStart = -1;
+ String resolvedValue = null;
for (int i = 0; i < len; i = value.offsetByCodePoints(i, 1)) {
final int ch = value.codePointAt(i);
switch (state) {
@@ -173,9 +175,14 @@ private static String replaceProperties(final String value) {
state = ch == '}' ? INITIAL : RESOLVED;
continue;
}
- final String val = System.getProperty(name);
+ // First check for system property, then env variable
+ String val = System.getProperty(name);
+ if (val == null && name.startsWith("env."))
+ val = System.getenv(name.substring(4));
+
if (val != null) {
builder.append(val);
+ resolvedValue = val;
state = ch == '}' ? INITIAL : RESOLVED;
continue;
} else if (ch == ',') {
@@ -211,7 +218,7 @@ private static String replaceProperties(final String value) {
continue;
}
default:
- throw new IllegalStateException();
+ throw new IllegalStateException("Unexpected char seen: "+ch);
}
}
switch (state) {
@@ -219,11 +226,16 @@ private static String replaceProperties(final String value) {
builder.append('$');
break;
}
- case DEFAULT:
- case GOT_OPEN_BRACE: {
+ case DEFAULT: {
builder.append(value.substring(start - 2));
break;
}
+ case GOT_OPEN_BRACE: {
+ // We had a reference that was not resolved, throw ISE
+ if (resolvedValue == null)
+ throw new IllegalStateException("Failed to resolve expression: "+builder.toString());
+ break;
+ }
}
return builder.toString();
}
View
115 src/test/java/org/jboss/dmr/ExpressionValueTest.java
@@ -6,7 +6,9 @@
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.util.Collection;
+import junit.framework.Assert;
import org.junit.Test;
public class ExpressionValueTest {
@@ -112,4 +114,117 @@ public void testResolve() {
final ExpressionValue value = new ExpressionValue("some expression");
assertEquals("some expression", value.resolve().asString());
}
+
+ /**
+ * Test that a valid expression to a system property reference which has
+ * no definition throws an ISE
+ */
+ @Test(expected = IllegalStateException.class)
+ public void testUnresolvedReference() {
+ final ExpressionValue value = new ExpressionValue("${no-such-system-property");
@bstansberry Owner

This isn't a valid expression, since it is missing the closing '}'. So this test passes but if you put in an equivalent expression but with the closing '}' you don't get the ISE.

I have a fix for this wanted to double-check in case I missed something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ String resolved = value.resolve().asString();
+ fail("Did not fail with ISE: "+resolved);
+ }
+
+ /**
+ * Validate a single system property expression sees the system property value.
+ */
+ @Test
+ public void testSystemPropertyRef() {
+ System.setProperty("test.property1", "test.property1.value");
+ try {
+ final ExpressionValue value = new ExpressionValue("${test.property1}");
+ assertEquals("test.property1.value", value.resolve().asString());
+ } finally {
+ System.clearProperty("test.property1");
+ }
+ }
+
+ /**
+ * Test an expression that contains more than one system property name to
+ * see that the second property value is used when the first property
+ * is not defined.
+ */
+ @Test
+ public void testSystemPropertyRefs() {
+ System.setProperty("test.property2", "test.property2.value");
+ try {
+ final ExpressionValue value = new ExpressionValue("${test.property1,test.property2}");
+ assertEquals("test.property2.value", value.resolve().asString());
+ } finally {
+ System.clearProperty("test.property2");
+ }
+ }
+
+ /**
+ * Validate that a system property expression for a property with no value
+ * and a default provides sees the default value.
+ */
+ @Test
+ public void testSystemPropertyRefDefault() {
+ final ExpressionValue value = new ExpressionValue("${test.property2:test.property2.default.value}");
+ assertEquals("test.property2.default.value", value.resolve().asString());
+ }
+
+ /**
+ * Validate that a environment variable reference is resolved.
+ */
+ @Test
+ public void testSystemEnvVarRef() {
+ // Since we cannot set ENV vars from java, grab first one
+ String[] envvar = findEnvVar();
+ if (envvar[0].length() == 0) {
+ System.err.println("No environment variables found, skipping test");
+ return;
+ }
+ final String envvarValue = envvar[1];
+ Assert.assertNotNull("Expect non-null env var: "+envvar[0], envvarValue);
+ final ExpressionValue value = new ExpressionValue("${"+envvar[0]+"}");
+ assertEquals(envvarValue, value.resolve().asString());
+ }
+ /**
+ * Validate that a environment variable reference is overriden by a
+ * system property of the same name prefixed with "env.".
+ */
+ @Test
+ public void testSystemEnvVarRefOverride() {
+ // Since we cannot set ENV vars from java, grab first one
+ String[] envvar = findEnvVar();
+ if (envvar[0].length() == 0) {
+ System.err.println("No environment variables found, skipping test");
+ return;
+ }
+ // Override the var
+ String sysPropName = envvar[0];
+ String overrideValue = sysPropName+"-override";
+ System.setProperty(sysPropName, overrideValue);
+ final String envvarValue = envvar[1];
+ Assert.assertNotNull("Expect non-null env var: "+envvar[0], envvarValue);
+ final ExpressionValue value = new ExpressionValue("${"+envvar[0]+"}");
+ assertEquals(overrideValue, value.resolve().asString());
+ }
+
+ /**
+ * Find the first defined System.getenv() environment variable with a non-zero length value.
+ * @return [0] = env var name prefixed with "env."
+ * [1] = env var value. If [0].length == 0, then there
+ * were no environment variables defined.
+ */
+ private static String[] findEnvVar() {
+ String[] pair = {"", null};
+ Collection<String> envvars = System.getenv().keySet();
+ if (envvars.isEmpty()) {
+ return pair;
+ }
+ for(final String envvar : envvars) {
+ final String envvarValue = System.getenv(envvar);
+ if (envvarValue != null && envvarValue.length() > 0) {
+ // Change name to env.name
+ pair[0] = "env." + envvar;
+ pair[1] = envvarValue;
+ break;
+ }
+ }
+ return pair;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.