You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sg...@apache.org on 2010/09/14 22:23:08 UTC

svn commit: r997075 - /commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/util/StringUtilTest.java

Author: sgoeschl
Date: Tue Sep 14 20:23:08 2010
New Revision: 997075

URL: http://svn.apache.org/viewvc?rev=997075&view=rev
Log:
Initial import

Added:
    commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/util/StringUtilTest.java
      - copied, changed from r996648, commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/util/MapUtilTest.java

Copied: commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/util/StringUtilTest.java (from r996648, commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/util/MapUtilTest.java)
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/util/StringUtilTest.java?p2=commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/util/StringUtilTest.java&p1=commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/util/MapUtilTest.java&r1=996648&r2=997075&rev=997075&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/util/MapUtilTest.java (original)
+++ commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/util/StringUtilTest.java Tue Sep 14 20:23:08 2010
@@ -18,57 +18,70 @@
 
 package org.apache.commons.exec.util;
 
+import junit.framework.TestCase;
 import org.apache.commons.exec.environment.EnvironmentUtils;
 
 import java.util.HashMap;
 import java.util.Map;
 
-import junit.framework.TestCase;
-
-public class MapUtilTest extends TestCase
+public class StringUtilTest extends TestCase
 {
     /**
-     * Test copying of map
+     * Test no string substitution
      */
-    public void testCopyMap() throws Exception {
+    public void testNoStringSubstitution() throws Exception
+    {
+        Map vars = new HashMap();
+        vars.put("foo", "FOO");
+        vars.put("bar", "BAR");
 
-        HashMap procEnvironment = new HashMap();
-        procEnvironment.put("JAVA_HOME", "/usr/opt/java");
+        assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a FOO & BAR test", vars, true).toString());
+    }
 
-        Map result = MapUtils.copy(procEnvironment);
-        assertTrue(result.size() == 1);
-        assertTrue(procEnvironment.size() == 1);
-        assertEquals("/usr/opt/java", result.get("JAVA_HOME"));
-
-        result.remove("JAVA_HOME");
-        assertTrue(result.size() == 0);
-        assertTrue(procEnvironment.size() == 1);
+    /**
+     * Test a default string substitution, e.g. all placeholders
+     * are expanded.
+     */
+    public void testDefaultStringSubstitution() throws Exception 
+    {
+        Map vars = new HashMap();
+        vars.put("foo", "FOO");
+        vars.put("bar", "BAR");
+
+        assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, true).toString());
+        assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, false).toString());
     }
 
     /**
-     * Test merging of maps
+     * Test an incomplete string substitution where not all placeholders
+     * are expanded.
      */
-    public void testMergeMap() throws Exception {
+    public void testIncompleteSubstitution() throws Exception {
 
-        Map procEnvironment = EnvironmentUtils.getProcEnvironment();
-        HashMap applicationEnvironment = new HashMap();
+        Map vars = new HashMap();
+        vars.put("foo", "FOO");
 
-        applicationEnvironment.put("appMainClass", "foo.bar.Main");
-        Map result = MapUtils.merge(procEnvironment, applicationEnvironment);
-        assertTrue((procEnvironment.size() + applicationEnvironment.size()) == result.size());
-        assertEquals("foo.bar.Main", result.get("appMainClass"));
+        assertEquals("This is a FOO & ${bar} test",  StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, true).toString());
+
+        try
+        {
+            StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, false).toString();
+            fail();
+        }
+        catch(RuntimeException e)
+        {
+            // nothing to do
+        }
     }
 
     /**
-     * Test prefixing of map
+     * Test a erroneous template.
      */
-    public void testPrefixMap() throws Exception {
-
-        HashMap procEnvironment = new HashMap();
-        procEnvironment.put("JAVA_HOME", "/usr/opt/java");
+    public void testErroneousTemplate() throws Exception
+    {
+        Map vars = new HashMap();
+        vars.put("foo", "FOO");
 
-        Map result = MapUtils.prefix(procEnvironment, "env");
-        assertTrue(procEnvironment.size() == result.size());
-        assertEquals("/usr/opt/java", result.get("env.JAVA_HOME"));
+        assertEquals("This is a FOO & ${}} test",  StringUtils.stringSubstitution("This is a ${foo} & ${}} test", vars, true).toString());
     }
 }
\ No newline at end of file