Author: sgoeschl
Date: Thu Nov 27 14:55:53 2008
New Revision: 721311
URL: http://svn.apache.org/viewvc?rev=721311&view=rev
Log:
[EXEC-30] Make environment variables respect casing rules of platforms
Modified:
commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java
commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/environment/EnvironmentUtil.java
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java
Modified:
commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java
URL:
http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java?rev=721311&r1=721310&r2=721311&view=diff
==============================================================================
---
commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java
(original)
+++
commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java
Thu Nov 27 14:55:53 2008
@@ -24,15 +24,16 @@
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
+import java.util.TreeMap;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.OS;
import org.apache.commons.exec.PumpStreamHandler;
-import org.apache.commons.exec.util.MapUtils;
/**
* Helper class to determine the environment variable
@@ -64,8 +65,9 @@
// create a copy of the map just in case that
// anyone is going to modifiy it, e.g. removing
// or setting an evironment variable
-
- return MapUtils.copy(procEnvironment);
+ Map copy = createEnvironmentMap();
+ copy.putAll(procEnvironment);
+ return copy;
}
/**
@@ -79,7 +81,8 @@
try {
Method getenvs = System.class.getMethod( "getenv",
(java.lang.Class[]) null );
Map env = (Map) getenvs.invoke( null, (java.lang.Object[])
null );
- procEnvironment = new HashMap( env );
+ procEnvironment = createEnvironmentMap();
+ procEnvironment.putAll(env);
} catch ( NoSuchMethodException e ) {
// ok, just not on JDK 1.5
} catch ( IllegalAccessException e ) {
@@ -90,7 +93,7 @@
}
if(procEnvironment == null) {
- procEnvironment = new HashMap();
+ procEnvironment = createEnvironmentMap();
BufferedReader in = runProcEnvCommand();
String var = null;
@@ -212,4 +215,27 @@
}
return bos.toString();
}
+
+ /**
+ * Creates a map that obeys the casing rules of the current platform for
key
+ * lookup. E.g. on a Windows platform, the map keys will be
+ * case-insensitive.
+ *
+ * @return The map for storage of environment variables, never
+ * <code>null</code>.
+ */
+ private Map createEnvironmentMap() {
+ if (OS.isFamilyWindows()) {
+ return new TreeMap(new Comparator() {
+ public int compare(Object arg0, Object arg1) {
+ String key0 = (String) arg0;
+ String key1 = (String) arg1;
+ return key0.compareToIgnoreCase(key1);
+ }
+ });
+ } else {
+ return new HashMap();
+ }
+ }
+
}
Modified:
commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/environment/EnvironmentUtil.java
URL:
http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/environment/EnvironmentUtil.java?rev=721311&r1=721310&r2=721311&view=diff
==============================================================================
---
commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/environment/EnvironmentUtil.java
(original)
+++
commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/environment/EnvironmentUtil.java
Thu Nov 27 14:55:53 2008
@@ -69,13 +69,15 @@
}
/**
- * Find the list of environment variables for this process.
+ * Find the list of environment variables for this process. The returned
map preserves
+ * the casing of a variable's name on all platforms but obeys the casing
rules of the
+ * current platform during lookup, e.g. key names will be case-insensitive
on Windows
+ * platforms.
*
- * @return a vector containing the environment variables the vector
elements
- * are strings formatted like variable = value
+ * @return a map containing the environment variables, may be empty but
never <code>null</code>
* @throws IOException the operation failed
*/
- public static synchronized Map getProcEnvironment() throws IOException {
+ public static Map getProcEnvironment() throws IOException {
return procEnvironment.getProcEnvironment();
}
Modified:
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java?rev=721311&r1=721310&r2=721311&view=diff
==============================================================================
---
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java
(original)
+++
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java
Thu Nov 27 14:55:53 2008
@@ -20,10 +20,13 @@
import java.io.IOException;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Locale;
import java.util.Map;
import junit.framework.TestCase;
+import org.apache.commons.exec.OS;
import org.apache.commons.exec.TestUtil;
public class EnvironmentUtilTest extends TestCase {
@@ -34,22 +37,22 @@
Map env = new HashMap();
TestUtil.assertEquals(new String[0], EnvironmentUtil.toStrings(env),
false);
-
+
env.put("foo2", "bar2");
env.put("foo", "bar");
String[] envStrings = EnvironmentUtil.toStrings(env);
String[] expected = new String[]{"foo=bar", "foo2=bar2"};
-
-
+
+
TestUtil.assertEquals(expected, envStrings, false);
}
/**
* Test to access the environment variables of the current
* process. Please note that this test does not run on
- * java-gjc
+ * java-gjc.
*/
public void testGetProcEnvironment() throws IOException {
Map procEnvironment = EnvironmentUtil.getProcEnvironment();
@@ -63,4 +66,32 @@
System.out.println(envArgs[i]);
}
}
+
+ /**
+ * On Windows platforms test that accessing environment variables
+ * can be done in a case-insensitive way, e.g. "PATH", "Path" and
+ * "path" would reference the same environment variable.
+ */
+ public void testGetProcEnvironmentCaseInsensitiveLookup() throws
IOException {
+ // run tests only on windows platforms
+ if (!OS.isFamilyWindows()) {
+ return;
+ }
+
+ // ensure that we have the same value for upper and lowercase keys
+ Map procEnvironment = EnvironmentUtil.getProcEnvironment();
+ for (Iterator it = procEnvironment.keySet().iterator(); it.hasNext();)
{
+ String variable = (String) it.next();
+ String value = (String) procEnvironment.get(variable);
+ assertEquals(value,
procEnvironment.get(variable.toLowerCase(Locale.ENGLISH)));
+ assertEquals(value,
procEnvironment.get(variable.toUpperCase(Locale.ENGLISH)));
+ }
+
+ // add an environment variable and check access
+ EnvironmentUtil.addVariableToEnvironment( procEnvironment, "foo=bar" );
+ assertEquals("bar", procEnvironment.get("FOO"));
+ assertEquals("bar", procEnvironment.get("Foo"));
+ assertEquals("bar", procEnvironment.get("foo"));
+ }
+
}