KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > StringTools_replaceAll_Test


1 package soot.util;
2
3 import soot.util.StringTools;
4 import java.util.*;
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8
9
10 /**
11  * JUnit test suite for the StringTools.replaceAll() method.
12  */

13 public class StringTools_replaceAll_Test extends TestCase {
14
15     public StringTools_replaceAll_Test(String JavaDoc name) {
16     super(name);
17     }
18
19     private static boolean useJJ = false;
20     private static void runAndCompare(String JavaDoc orig, String JavaDoc toBeReplaced,
21                    String JavaDoc replacement, String JavaDoc expected) {
22     String JavaDoc result = StringTools.replaceAll(orig, toBeReplaced, replacement);
23     assertEquals(expected, result);
24     }
25
26     public void testNoOccurrences() {
27     runAndCompare("This string contains no occurrences of the pattern.",
28               "absent", "present",
29               "This string contains no occurrences of the pattern.");
30     }
31
32     public void testOccurrenceAtBeginning() {
33     runAndCompare("The pattern is at the beginning of this string.",
34               "The pattern", "A replacement",
35               "A replacement is at the beginning of this string.");
36     }
37
38     public void testOccurrenceAtEnd() {
39     runAndCompare("This string ends with the pattern.",
40               "the pattern.", "a replacement.",
41               "This string ends with a replacement.");
42     }
43
44     public void testReplaceWholeOriginal() {
45     runAndCompare("The whole shebang.",
46               "The whole shebang.", "Everything in its entirety.",
47               "Everything in its entirety.");
48     }
49
50     public void testReplaceEverythingInPieces() {
51     runAndCompare("old?old?old?old?old?old?old?",
52               "old?", "new!",
53               "new!new!new!new!new!new!new!");
54     }
55
56     public void testMultipleOccurrencesSameLength() {
57     runAndCompare("abcOLDabcOLDabOLDOLDa",
58               "OLD", "NEW",
59               "abcNEWabcNEWabNEWNEWa");
60     }
61
62     public void testMultipleOccurrencesShorter() {
63     runAndCompare("abcOLDabcOLDabOLDOLDa",
64               "OLD", "NE",
65               "abcNEabcNEabNENEa");
66     }
67
68     public void testMultipleOccurrencesLonger() {
69     runAndCompare("abcOLDabcOLDabOLDOLDa",
70               "OLD", "_REFURBISHED_",
71               "abc_REFURBISHED_abc_REFURBISHED_ab_REFURBISHED__REFURBISHED_a");
72     }
73
74     public void testBlankReplacement() {
75     runAndCompare("abcOLDabcOLDabOLDOLDa",
76               "OLD", "",
77               "abcabcaba");
78     }
79
80     public void testBlankPattern() {
81     runAndCompare("abcOLDabcOLDabOLDOLDa",
82               "", "new",
83               "abcOLDabcOLDabOLDOLDa");
84     }
85
86     public void testPatternLongerThanOrig() {
87     runAndCompare("This is a string",
88               "This is a string pattern", "This is a string replacement",
89               "This is a string");
90     }
91
92     public void testSingleLetterPatternSingleLetterReplacement() {
93     runAndCompare("in this string is a phrase in in which to replace the 'i's, including the last i",
94               "i", "1",
95               "1n th1s str1ng 1s a phrase 1n 1n wh1ch to replace the '1's, 1nclud1ng the last 1");
96     }
97
98     public void testSingleLetterPatternZeroLetterReplacement() {
99     runAndCompare("in this string is a phrase in in which to replace the 'i's, including the last i",
100               "i", "",
101               "n ths strng s a phrase n n whch to replace the ''s, ncludng the last ");
102     }
103
104     public void testSingleLetterPatternMultiLetterReplacement() {
105     runAndCompare("in this string is a phrase in in which to replace the 'i's, including the last i",
106               "i", "III",
107               "IIIn thIIIs strIIIng IIIs a phrase IIIn IIIn whIIIch to replace the 'III's, IIIncludIIIng the last III");
108     }
109
110     public void testPatternIncludedInsideReplacement() {
111     // This checks that including the pattern to be replaced within
112
// the replacing text does not cause some sort of infinite loop.
113
runAndCompare("Some old text to be folded.",
114               "old", "embold",
115               "Some embold text to be fembolded.");
116     }
117
118     public void testBlankOriginal() {
119     runAndCompare("", "old", "new", "");
120     }
121
122     public static Test reflectionSuite() {
123     TestSuite suite = new TestSuite(StringTools_replaceAll_Test.class);
124     return suite;
125     }
126
127     public static void main(String JavaDoc[] arg) {
128     if (arg.length > 0 && arg[0].equals("useJJ"))
129         useJJ = true;
130     else
131         useJJ = false;
132         junit.textui.TestRunner.run(reflectionSuite());
133     }
134 }
135
Popular Tags