oodt-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattm...@apache.org
Subject svn commit: r1441830 - in /oodt/trunk: CHANGES.txt commons/src/main/java/org/apache/oodt/commons/exec/EnvUtilities.java commons/src/test/org/apache/oodt/commons/exec/TestEnvUtilities.java
Date Sat, 02 Feb 2013 23:13:56 GMT
Author: mattmann
Date: Sat Feb  2 23:13:56 2013
New Revision: 1441830

URL: http://svn.apache.org/viewvc?rev=1441830&view=rev
Log:
- fix for OODT-553 Update org.apache.oodt.commons.exec.EnvUtilities to Use System.getEnvironment
contributed by Michael Starch

Modified:
    oodt/trunk/CHANGES.txt
    oodt/trunk/commons/src/main/java/org/apache/oodt/commons/exec/EnvUtilities.java
    oodt/trunk/commons/src/test/org/apache/oodt/commons/exec/TestEnvUtilities.java

Modified: oodt/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/oodt/trunk/CHANGES.txt?rev=1441830&r1=1441829&r2=1441830&view=diff
==============================================================================
--- oodt/trunk/CHANGES.txt (original)
+++ oodt/trunk/CHANGES.txt Sat Feb  2 23:13:56 2013
@@ -3,6 +3,9 @@ Apache OODT Change Log
 Release 0.6 - Current Development
 --------------------------------------------
 
+* OODT-553 Update org.apache.oodt.commons.exec.EnvUtilities to Use System.getEnvironment

+  (Michael Starch via mattmann)
+
 * OODT-551 Insert primary key in metadata table for database-based File Manager, to always
return metadata values in proper order
            (luca)
 

Modified: oodt/trunk/commons/src/main/java/org/apache/oodt/commons/exec/EnvUtilities.java
URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/main/java/org/apache/oodt/commons/exec/EnvUtilities.java?rev=1441830&r1=1441829&r2=1441830&view=diff
==============================================================================
--- oodt/trunk/commons/src/main/java/org/apache/oodt/commons/exec/EnvUtilities.java (original)
+++ oodt/trunk/commons/src/main/java/org/apache/oodt/commons/exec/EnvUtilities.java Sat Feb
 2 23:13:56 2013
@@ -24,6 +24,7 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.Map;
 import java.util.Properties;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -31,6 +32,7 @@ import java.util.logging.Logger;
 /**
  * @author mattmann
  * @author bfoster
+ * @author mstarch
  * @version $Revision$
  * 
  * <p>
@@ -54,37 +56,19 @@ public final class EnvUtilities {
      * @return The environment variable value, as a String.
      */
     public static String getEnv(String envVarName) {
-        Properties props = getEnv();
-
-        return (props != null) ? props.getProperty(envVarName) : null;
+        return System.getenv(envVarName);
     }
 
     /**
      * This method does exactly the same thing as {@link #getEnvUsingWaitFor()},
-     * except it uses {@link ExecHelper} rather than traditional Java
-     * {@link Process} creation ( and
-     * 
-     * @link {@link Process#waitFor()}) and termination.
-     * 
+     * except it uses System.getenv()
      * 
      * @return The user's current environment, in a {@link Properties} object.
      */
     public static Properties getEnv() {
-        String commandLine = "env";
-
-        Properties envProps = null;
-        ExecHelper exec;
-        try {
-            exec = ExecHelper.exec(new String[] { commandLine });
-            envProps = new Properties();
-            envProps.load(preProcessInputStream(new ByteArrayInputStream(exec
-                    .getOutput().getBytes())));
-        } catch (Exception e) {
-            e.printStackTrace();
-            LOG.log(Level.WARNING, "Error executing env command: Message: "
-                    + e.getMessage());
-        }
-
+        Properties envProps = new Properties();
+        for (Map.Entry<String, String> entry : System.getenv().entrySet())
+            envProps.setProperty(entry.getKey(), entry.getValue());
         return envProps;
     }
 

Modified: oodt/trunk/commons/src/test/org/apache/oodt/commons/exec/TestEnvUtilities.java
URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/org/apache/oodt/commons/exec/TestEnvUtilities.java?rev=1441830&r1=1441829&r2=1441830&view=diff
==============================================================================
--- oodt/trunk/commons/src/test/org/apache/oodt/commons/exec/TestEnvUtilities.java (original)
+++ oodt/trunk/commons/src/test/org/apache/oodt/commons/exec/TestEnvUtilities.java Sat Feb
 2 23:13:56 2013
@@ -20,12 +20,17 @@ package org.apache.oodt.commons.exec;
 //JDK imports
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.util.Properties;
+
+//Apache Commons
+import org.apache.commons.lang.SystemUtils;
 
 //Junit imports
 import junit.framework.TestCase;
 
 /**
  * @author mattmann
+ * @author mstarch
  * @version $Revision$
  * 
  * <p>
@@ -62,5 +67,61 @@ public class TestEnvUtilities extends Te
         assertEquals(translatedEnvStr, expectedVarStr);
 
     }
-
+    /**
+     * Tests two environment variables that should exist in any build 
+     * environment. USER, HOME
+     * By calling (EnvUtilities.getEnv(String))
+     */
+    public void testSetEnvironmentVar() {
+        //Test if an only if HOME and USER is defined (Assumed to be true on unix)
+        if (SystemUtils.IS_OS_UNIX) {
+            //Makes the assumption that System.properties() is correct.
+            String userHomeTruth = System.getProperty("user.home");
+            String userNameTruth = System.getProperty("user.name");
+            //Test values
+            String userHomeTest = EnvUtilities.getEnv("HOME");
+            String userNameTest = EnvUtilities.getEnv("USER");
+            //Check all three tests
+            assertEquals(userHomeTruth,userHomeTest);
+            assertEquals(userNameTruth,userNameTest);
+        } 
+    }
+    /**
+     * Tests two environment variables that should exist in any build 
+     * environment. USER, HOME
+     * By getting the environment (EnvUtilities.getEnv()) and reading from this.
+     */
+    public void testGetEnvironment() {
+        //Test if an only if HOME and USER is defined (Assumed to be true on unix)
+        if (SystemUtils.IS_OS_UNIX) {
+            //Makes the assumption that System.properties() is correct.
+            String userHomeTruth = System.getProperty("user.home");
+            String userNameTruth = System.getProperty("user.name");
+            Properties env = EnvUtilities.getEnv();
+            //Test values
+            String userHomeTest = env.getProperty("HOME");
+            String userNameTest = env.getProperty("USER");
+            //Check all three tests
+            assertEquals(userHomeTruth,userHomeTest);
+            assertEquals(userNameTruth,userNameTest);
+        } 
+    }
+    /**
+     * Tests for consistency between the two methods for getting environment variables
+     * in EnvUtilities calling getEnv(String) and calling getEnv().getProperty(String).
+     */
+    public void testGetEnvironmentConsistency() {
+        //Test if an only if HOME and USER is defined (Assumed to be true on unix)
+        if (SystemUtils.IS_OS_UNIX) {
+            Properties env = EnvUtilities.getEnv();
+            //Test values
+            String userHomeTest1 = env.getProperty("HOME");
+            String userNameTest1 = env.getProperty("USER");
+            String userHomeTest2 = EnvUtilities.getEnv("HOME");
+            String userNameTest2 = EnvUtilities.getEnv("USER");
+            //Check all three tests
+            assertEquals(userHomeTest1,userHomeTest2);
+            assertEquals(userNameTest1,userNameTest2);
+        }
+    }
 }



Mime
View raw message