1 /* Created on Jan 19, 2004 */
2 package com.quantum.util;
4 import java.lang.reflect.Method;
6 import junit.framework.TestCase;
11 public class StringUtilTest extends TestCase {
12 public void testSubstituteString() throws Exception {
14 String sample = "This is a test of ${variable} substitution.";
15 String replaceAll = replaceAll(sample, "\\$\\{variable\\}", "parameter");
17 assertEquals("replaceAll", "This is a test of parameter substitution.", replaceAll);
18 System.out.println(StringUtil.substituteString(sample, "${variable}", "parameter"));
19 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "${variable}", "parameter"));
22 public void testSubstituteStringAtEnd() throws Exception {
24 String sample = "Substitute ${variable}";
25 String replaceAll = replaceAll(sample, "\\$\\{variable\\}", "parameter");
27 System.out.println(StringUtil.substituteString(sample, "${variable}", "parameter"));
28 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "${variable}", "parameter"));
31 public void testSubstituteStringAtBeginning() throws Exception {
33 String sample = "${variable} substitution";
34 String replaceAll = replaceAll(sample, "\\$\\{variable\\}", "parameter");
36 System.out.println(StringUtil.substituteString(sample, "${variable}", "parameter"));
37 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "${variable}", "parameter"));
40 public void testSubstituteStringMultipleTokens() throws Exception {
42 String sample = "${variable}${variable}";
43 String replaceAll = replaceAll(sample, "\\$\\{variable\\}", "parameter");
45 System.out.println(StringUtil.substituteString(sample, "${variable}", "parameter"));
46 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "${variable}", "parameter"));
49 public void testSubstituteStringZeroLength() throws Exception {
51 String sample = "This is a test of ${variable} substitution.";
52 String replaceAll = replaceAll(sample, "\\$\\{variable\\}", "");
54 System.out.println(StringUtil.substituteString(sample, "${variable}", ""));
55 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "${variable}", ""));
58 public void testSubstituteStringSpecialChars() throws Exception {
60 String sample = "This is a test of \r substitution.";
61 String replaceAll = replaceAll(sample, "\r", "");
63 System.out.println(StringUtil.substituteString(sample, "\r", ""));
64 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "\r", ""));
67 private String replaceAll(String original, String regex, String newValue) throws Exception {
68 Method method = String.class.getMethod("replaceAll", new Class[] { String.class, String.class } );
69 return (String) method.invoke(original, new Object[] { regex, newValue } );