avoid ClassCastException
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src-test / com / quantum / util / StringUtilTest.java
1 /* Created on Jan 19, 2004 */
2 package com.quantum.util;
3
4 import java.lang.reflect.Method;
5
6 import junit.framework.TestCase;
7
8 /**
9  * @author holmesbc
10  */
11 public class StringUtilTest extends TestCase {
12         public void testSubstituteString() throws Exception {
13                 
14                 String sample = "This is a test of ${variable} substitution.";
15                 String replaceAll = replaceAll(sample, "\\$\\{variable\\}", "parameter");
16                 
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"));
20         }
21
22         public void testSubstituteStringAtEnd() throws Exception {
23                 
24                 String sample = "Substitute ${variable}";
25                 String replaceAll = replaceAll(sample, "\\$\\{variable\\}", "parameter");
26                 
27                 System.out.println(StringUtil.substituteString(sample, "${variable}", "parameter"));            
28                 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "${variable}", "parameter"));
29         }
30
31         public void testSubstituteStringAtBeginning() throws Exception  {
32                 
33                 String sample = "${variable} substitution";
34                 String replaceAll = replaceAll(sample, "\\$\\{variable\\}", "parameter");
35                 
36                 System.out.println(StringUtil.substituteString(sample, "${variable}", "parameter"));            
37                 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "${variable}", "parameter"));
38         }
39
40         public void testSubstituteStringMultipleTokens() throws Exception  {
41                 
42                 String sample = "${variable}${variable}";
43                 String replaceAll = replaceAll(sample, "\\$\\{variable\\}", "parameter");
44                 
45                 System.out.println(StringUtil.substituteString(sample, "${variable}", "parameter"));            
46                 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "${variable}", "parameter"));
47         }
48
49         public void testSubstituteStringZeroLength() throws Exception  {
50                 
51                 String sample = "This is a test of ${variable} substitution.";
52                 String replaceAll = replaceAll(sample, "\\$\\{variable\\}", "");
53                 
54                 System.out.println(StringUtil.substituteString(sample, "${variable}", ""));             
55                 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "${variable}", ""));
56         }
57
58         public void testSubstituteStringSpecialChars() throws Exception  {
59                 
60                 String sample = "This is a test of \r substitution.";
61                 String replaceAll = replaceAll(sample, "\r", "");
62                 
63                 System.out.println(StringUtil.substituteString(sample, "\r", ""));              
64                 assertEquals("substitute String", replaceAll, StringUtil.substituteString(sample, "\r", ""));
65         }
66         
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 } );
70         }
71 }