Return-Path: X-Original-To: apmail-oodt-commits-archive@www.apache.org Delivered-To: apmail-oodt-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 36D57ED92 for ; Sat, 2 Feb 2013 23:14:17 +0000 (UTC) Received: (qmail 63012 invoked by uid 500); 2 Feb 2013 23:14:17 -0000 Delivered-To: apmail-oodt-commits-archive@oodt.apache.org Received: (qmail 62979 invoked by uid 500); 2 Feb 2013 23:14:17 -0000 Mailing-List: contact commits-help@oodt.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@oodt.apache.org Delivered-To: mailing list commits@oodt.apache.org Received: (qmail 62972 invoked by uid 99); 2 Feb 2013 23:14:17 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 02 Feb 2013 23:14:17 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 02 Feb 2013 23:14:15 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id C77EB2388980; Sat, 2 Feb 2013 23:13:56 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@oodt.apache.org From: mattmann@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130202231356.C77EB2388980@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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$ * *

@@ -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 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$ * *

@@ -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); + } + } }