commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r990671 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/text/StrSubstitutor.java test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java
Date Sun, 29 Aug 2010 22:53:43 GMT
Author: bayard
Date: Sun Aug 29 22:53:42 2010
New Revision: 990671

URL: http://svn.apache.org/viewvc?rev=990671&view=rev
Log:
Adding Ulrich + Tomas' patch to LANG-596 adding a replace(String, Properties) variant to StrSubstitutor

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/StrSubstitutor.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/StrSubstitutor.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/StrSubstitutor.java?rev=990671&r1=990670&r2=990671&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/StrSubstitutor.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/StrSubstitutor.java
Sun Aug 29 22:53:42 2010
@@ -17,8 +17,11 @@
 package org.apache.commons.lang3.text;
 
 import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 
 /**
  * Substitutes variables within a string by values.
@@ -151,6 +154,30 @@ public class StrSubstitutor {
     }
 
     /**
+     * Replaces all the occurrences of variables in the given source object with their matching
+     * values from the properties.
+     * 
+     * @param source the source text containing the variables to substitute, null returns
null
+     * @param properties the properties with values, may be null
+     * @return the result of the replace operation
+     */
+    public static String replace(Object source, Properties valueProperties)
+    {
+        if (valueProperties == null) {
+            return source.toString();
+        }
+        Map<String,String> valueMap = new HashMap<String,String>();
+        Enumeration<?> propNames = valueProperties.propertyNames();
+        while (propNames.hasMoreElements())
+        {
+            String propName = (String)propNames.nextElement();
+            String propValue = valueProperties.getProperty(propName);
+            valueMap.put(propName, propValue);
+        }
+        return StrSubstitutor.replace(source, valueMap);
+    }
+    
+    /**
      * Replaces all the occurrences of variables in the given source object with
      * their matching values from the system properties.
      *

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java?rev=990671&r1=990670&r2=990671&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java
Sun Aug 29 22:53:42 2010
@@ -19,6 +19,7 @@ package org.apache.commons.lang3.text;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Properties;
 
 import junit.framework.TestCase;
 
@@ -411,6 +412,19 @@ public class StrSubstitutorTest extends 
             + "working with ${os.name}, your home "
             + "directory is ${user.home}."));
     }
+    
+    /**
+     * Test the replace of a properties object
+     */
+    public void testSubstitutetDefaultProperties(){
+    	String org = "${doesnotwork}";
+        System.setProperty("doesnotwork", "It work's!");
+
+        // create a new Properties object with the System.getProperties as default
+        Properties props = new Properties(System.getProperties());
+
+        assertEquals("It work's!",StrSubstitutor.replace(org, props));
+    }
 
     //-----------------------------------------------------------------------
     private void doTestReplace(String expectedResult, String replaceTemplate, boolean substring)
{



Mime
View raw message