You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by gg...@apache.org on 2005/07/15 00:24:20 UTC
svn commit: r219119 - in /jakarta/commons/proper/lang/trunk/src:
java/org/apache/commons/lang/text/VariableFormatter.java
test/org/apache/commons/lang/text/VariableFormatterTest.java
Author: ggregory
Date: Thu Jul 14 15:24:17 2005
New Revision: 219119
URL: http://svn.apache.org/viewcvs?rev=219119&view=rev
Log:
- VariableFormatter.java now gets 98.7% clover coverage.
- MapVariableFormatter.java now gets 100% clover coverage.
- Javadocs.
- Constructor clean ups.
Modified:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/VariableFormatter.java
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/VariableFormatterTest.java
Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/VariableFormatter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/VariableFormatter.java?rev=219119&r1=219118&r2=219119&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/VariableFormatter.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/VariableFormatter.java Thu Jul 14 15:24:17 2005
@@ -233,26 +233,34 @@
/** Stores the variable prefix. */
private String variablePrefix;
+ /**
+ * Variable resolution is delegated to an implementor of VariableResolver.
+ */
private VariableResolver variableResolver;
/** Stores the variable suffix. */
private String variableSuffix;
/**
- * Creates a new instance of <code>VariableFormat</code> and initializes it. Uses defaults for variable prefix and
- * suffix and the escaping character.
+ * Creates a new instance and initializes it. Uses defaults for variable prefix and suffix and the escaping
+ * character.
*
* @param valueMap
* the map with the variables' values
- * @throws IllegalArgumentException
- * if the map is undefined
*/
public VariableFormatter(Map valueMap) {
this(valueMap, DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE);
}
/**
- * Creates a new instance of <code>VariableFormat</code> and initializes it. Uses a default escaping character.
+ * Creates a new instance with defaults for variable prefix and suffix and the escaping character.
+ */
+ public VariableFormatter() {
+ this((VariableResolver) null, DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE);
+ }
+
+ /**
+ * Creates a new instance and initializes it. Uses a default escaping character.
*
* @param valueMap
* the map with the variables' values
@@ -260,15 +268,13 @@
* the prefix for variables
* @param suffix
* the suffix for variables
- * @throws IllegalArgumentException
- * if the map is undefined
*/
public VariableFormatter(Map valueMap, String prefix, String suffix) {
this(valueMap, prefix, suffix, DEFAULT_ESCAPE);
}
/**
- * Creates a new instance of <code>VariableFormat</code> and initializes it.
+ * Creates a new instance and initializes it.
*
* @param valueMap
* the map with the variables' values
@@ -278,11 +284,25 @@
* the suffix for variables
* @param escape
* the escape character
- * @throws IllegalArgumentException
- * if the map is undefined
*/
public VariableFormatter(Map valueMap, String prefix, String suffix, char escape) {
- this.setVariableResolver(new MapVariableResolver(valueMap));
+ this(new MapVariableResolver(valueMap), prefix, suffix, escape);
+ }
+
+ /**
+ * Creates a new instance and initializes it.
+ *
+ * @param variableResolver
+ * the variable resolver
+ * @param prefix
+ * the prefix for variables
+ * @param suffix
+ * the suffix for variables
+ * @param escape
+ * the escape character
+ */
+ public VariableFormatter(VariableResolver variableResolver, String prefix, String suffix, char escape) {
+ this.setVariableResolver(variableResolver);
this.setVariablePrefix(prefix);
this.setVariableSuffix(suffix);
this.setEscapeCharacter(escape);
@@ -447,6 +467,11 @@
return this.variablePrefix;
}
+ /**
+ * Gets the VariableResolver
+ *
+ * @return the VariableResolver
+ */
public VariableResolver getVariableResolver() {
return this.variableResolver;
}
@@ -531,6 +556,12 @@
this.variablePrefix = variablePrefix;
}
+ /**
+ * Sets the VariableResolver
+ *
+ * @param variableResolver
+ * the VariableResolver
+ */
public void setVariableResolver(VariableResolver variableResolver) {
this.variableResolver = variableResolver;
}
Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/VariableFormatterTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/VariableFormatterTest.java?rev=219119&r1=219118&r2=219119&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/VariableFormatterTest.java (original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/VariableFormatterTest.java Thu Jul 14 15:24:17 2005
@@ -41,7 +41,7 @@
}
MapVariableResolver getMapVariableResolver() {
- return (MapVariableResolver)this.getFormat().getVariableResolver();
+ return (MapVariableResolver) this.getFormat().getVariableResolver();
}
private Map getValueMap() {
@@ -119,14 +119,14 @@
assertEquals(">>", this.getFormat().getVariableSuffix());
assertEquals('\\', this.getFormat().getEscapeCharacter());
-// new VariableFormatter(null) should be OK IMO
-// Gary Gregory - July 14 2005
-// try {
-// format = new VariableFormatter(null);
-// fail("Could create format object with null map!");
-// } catch (IllegalArgumentException iex) {
-// // ok
-// }
+ // new VariableFormatter(null) should be OK IMO
+ // Gary Gregory - July 14 2005
+ // try {
+ // format = new VariableFormatter(null);
+ // fail("Could create format object with null map!");
+ // } catch (IllegalArgumentException iex) {
+ // // ok
+ // }
try {
format = new VariableFormatter(values, "${", null);
@@ -158,13 +158,29 @@
* Tests invoking the static convenience methods.
*/
public void testNonInstanceMethods() {
- assertEquals("The quick brown fox jumps over the lazy dog.", VariableFormatter.replace(values, REPLACE_TEMPLATE));
+ assertEquals("The quick brown fox jumps over the lazy dog.", VariableFormatter
+ .replace(values, REPLACE_TEMPLATE));
values.put("animal", "cow");
values.put("target", "moon");
assertEquals("The cow jumps over the moon.", VariableFormatter.replace(values, "&", ";",
"The &animal; jumps over the ⌖."));
}
+ public void testNoResolver() throws Exception {
+ this.testNoResolver(new VariableFormatter());
+ this.testNoResolver(new VariableFormatter(null));
+ }
+
+ void testNoResolver(VariableFormatter formatter) throws Exception {
+ formatter.setVariableResolver(null);
+ this.validateNoReplace(formatter);
+ }
+
+ public void testNullMap() throws Exception {
+ VariableFormatter formatter = new VariableFormatter(null);
+ validateNoReplace(formatter);
+ }
+
/**
* Tests recursive replacements.
*/
@@ -226,5 +242,11 @@
assertEquals(buf.toString(), VariableFormatter.replaceSystemProperties("Hi ${user.name}, you are "
+ "working with ${os.name}, your home "
+ "directory is ${user.home}."));
+ }
+
+ void validateNoReplace(VariableFormatter formatter) {
+ String srcString = "Hello ${user.name}";
+ String destString = formatter.replace(srcString);
+ assertEquals(srcString, destString);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org