harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lian...@apache.org
Subject svn commit: r480871 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/lang/ProcessBuilder.java main/java/org/apache/harmony/luni/platform/Environment.java test/java/org/apache/harmony/luni/tests/java/lang/SystemTest.java
Date Thu, 30 Nov 2006 08:19:58 GMT
Author: liangyx
Date: Thu Nov 30 00:19:55 2006
New Revision: 480871

URL: http://svn.apache.org/viewvc?view=rev&rev=480871
Log:
Apply patch for HARMONY-2356 ([classlib][luni]The Map from System.getEnv should throw NullPointerException
while get(null) and is not modifiable.)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/ProcessBuilder.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/Environment.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SystemTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/ProcessBuilder.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/ProcessBuilder.java?view=diff&rev=480871&r1=480870&r2=480871
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/ProcessBuilder.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/ProcessBuilder.java
Thu Nov 30 00:19:55 2006
@@ -19,7 +19,6 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.Arrays;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -66,7 +65,7 @@
             throw new NullPointerException();
         }
         this.command = command;
-        this.environment = new ProcessHashMap<String, String>(System.getenv());
+        this.environment = new org.apache.harmony.luni.platform.Environment.EnvironmentMap(System.getenv());
     }
 
     /**
@@ -215,30 +214,6 @@
         Process process = Runtime.getRuntime().exec(cmdArray, envArray, directory);
         // TODO implement support for redirectErrorStream
         return process;
-    }
-    
-    private static class ProcessHashMap<K, V> extends HashMap<K, V> {
-
-        public ProcessHashMap(Map<K, V> env) {
-            super(env);
-        }
-        @Override
-        public V put(K key, V value) {
-            if(key == null || value == null){
-                throw new NullPointerException();
-            }
-            return super.put(key, value);
-        }
-        @Override
-        public V get(Object key) {
-            if(key == null){
-                throw new NullPointerException();
-            }
-            if(!(key instanceof String)){
-                throw new ClassCastException(key.getClass().getName());
-            }
-            return super.get(key);
-        }
     }
 }
 

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/Environment.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/Environment.java?view=diff&rev=480871&r1=480870&r2=480871
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/Environment.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/Environment.java
Thu Nov 30 00:19:55 2006
@@ -16,6 +16,7 @@
 
 package org.apache.harmony.luni.platform;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -24,49 +25,98 @@
  */
 public class Environment {
 
-	private static Map<String, String> envMap = null;
+    private static Map<String, String> envMap = null;
 
-	/**
-	 * Returns a Map of the current environment variables, containing key and
-	 * value pairs.
-	 * 
-	 * @return a Map containing the environment variables and their values
-	 1*/
-	public static Map<String, String> getenv() {
-		if (null == envMap) {
-			envMap = new HashMap<String, String>();
-			byte[] bytes = getEnvBytes();
-			if (bytes == null) {
-				throw new Error("Failed to get environment variables.");
-			}
-			String[] envStrings = new String(bytes).split("\0");
-			for (int i = 0; i < envStrings.length; i++) {
-				int separator = envStrings[i].indexOf("=");
-				envMap.put(envStrings[i].substring(0, separator), envStrings[i]
-						.substring(separator + 1));
-			}
-		}
-		return envMap;
-	}
-
-	/**
-	 * Returns a String containing the value of the specified name environment
-	 * variable
-	 * 
-	 * @param name -
-	 *            the environment variable to get the value of
-	 * 
-	 * @return the value of the environment variable specified
-	 */
-	public static String getenv(String name) {
-		byte[] env = getEnvByName(name.getBytes());
-		if (null == env) {
-			return null;
-		}
-		return new String(env);
-	}
+    /**
+     * Returns a Map of the current environment variables, containing key and
+     * value pairs.
+     * 
+     * @return a Map containing the environment variables and their values
+     */
+    public static Map<String, String> getenv() {
+        if (null == envMap) {
+            HashMap<String, String> newEnvMap = new EnvironmentMap();
+            byte[] bytes = getEnvBytes();
+            if (bytes == null) {
+                throw new Error("Failed to get environment variables.");
+            }
+            String[] envStrings = new String(bytes).split("\0");
+            for (int i = 0; i < envStrings.length; i++) {
+                int separator = envStrings[i].indexOf("=");
+                newEnvMap.put(envStrings[i].substring(0, separator),
+                        envStrings[i].substring(separator + 1));
+            }
+            envMap = Collections.unmodifiableMap(newEnvMap);
+        }
+        return envMap;
+    }
+
+    /**
+     * Returns a String containing the value of the specified name environment
+     * variable
+     * 
+     * @param name -
+     *            the environment variable to get the value of
+     * 
+     * @return the value of the environment variable specified
+     */
+    public static String getenv(String name) {
+        byte[] env = getEnvByName(name.getBytes());
+        if (null == env) {
+            return null;
+        }
+        return new String(env);
+    }
+
+    public static class EnvironmentMap extends HashMap<String, String> {
+
+        private static final long serialVersionUID = 1L;
+
+        public EnvironmentMap() {
+            super();
+        }
+        
+        public EnvironmentMap(Map<String, String> map) {
+            super(map);
+        }
+
+        public boolean containsKey(Object key) {
+            checkParam(key);
+            return super.containsKey(key);
+        }
+
+        public boolean containsValue(Object value) {
+            checkParam(value);
+            return super.containsValue(value);
+        }
+
+        public String get(Object key) {
+            checkParam(key);
+            return super.get(key);
+        }
+
+        public String put(String key, String value) {
+            checkParam(key);
+            checkParam(value);
+            return super.put(key, value);
+        }
+
+        public String remove(Object key) {
+            checkParam(key);
+            return super.remove(key);
+        }
+
+        private void checkParam(Object o) {
+            if (null == o) {
+                throw new NullPointerException();
+            }
+            if (!(o instanceof String)) {
+                throw new ClassCastException();
+            }
+        }
+    }
 
-	private static native byte[] getEnvBytes();
+    private static native byte[] getEnvBytes();
 
-	private static native byte[] getEnvByName(byte[] name);
+    private static native byte[] getEnvByName(byte[] name);
 }

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SystemTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SystemTest.java?view=diff&rev=480871&r1=480870&r2=480871
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SystemTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SystemTest.java
Thu Nov 30 00:19:55 2006
@@ -21,6 +21,7 @@
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.io.PrintStream;
+import java.util.Map;
 import java.util.Properties;
 
 public class SystemTest extends junit.framework.TestCase {
@@ -299,6 +300,88 @@
 			System.setProperties(orgProps);
 		}
 	}
+    
+    //Regression Test for Harmony-2356
+    public void testEnvUnmodifiable() {
+        Map map = System.getenv();
+        try {
+            map.containsKey(null);
+            fail("Should throw NullPointerExcepiton.");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            map.containsKey(new Integer(10));
+            fail("Should throw ClassCastException.");
+        } catch (ClassCastException e) {
+            // expected
+        }
+
+        try {
+            map.containsValue(null);
+            fail("Should throw NullPointerExcepiton.");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            map.containsValue(new Integer(10));
+            fail("Should throw ClassCastException.");
+        } catch (ClassCastException e) {
+            // expected
+        }
+
+        try {
+            map.get(null);
+            fail("Should throw NullPointerExcepiton.");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            map.get(new Integer(10));
+            fail("Should throw ClassCastException.");
+        } catch (ClassCastException e) {
+            // expected
+        }
+
+        try {
+            map.put(null, "AAA");
+            fail("Should throw UnsupportedOperationExcepiton.");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+
+        try {
+            map.put("AAA", new Integer(10));
+            fail("Should throw UnsupportedOperationException.");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+
+        try {
+            map.put("AAA", "BBB");
+            fail("Should throw UnsupportedOperationException.");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+
+        try {
+            map.clear();
+            fail("Should throw UnsupportedOperationException.");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+
+        try {
+            map.remove(null);
+            fail("Should throw UnsupportedOperationException.");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+
+    }
 
 	@Override
     protected void setUp() {



Mime
View raw message