001: package soot.util;
002:
003: import soot.util.StringTools;
004: import junit.framework.Test;
005: import junit.framework.TestCase;
006: import junit.framework.TestSuite;
007:
008: /**
009: * JUnit test suite for the StringTools.replaceAll() method.
010: */
011: public class StringTools_replaceAll_Test extends TestCase {
012:
013: public StringTools_replaceAll_Test(String name) {
014: super (name);
015: }
016:
017: private static void runAndCompare(String orig, String toBeReplaced,
018: String replacement, String expected) {
019: String result = StringTools.replaceAll(orig, toBeReplaced,
020: replacement);
021: assertEquals(expected, result);
022: }
023:
024: public void testNoOccurrences() {
025: runAndCompare(
026: "This string contains no occurrences of the pattern.",
027: "absent", "present",
028: "This string contains no occurrences of the pattern.");
029: }
030:
031: public void testOccurrenceAtBeginning() {
032: runAndCompare(
033: "The pattern is at the beginning of this string.",
034: "The pattern", "A replacement",
035: "A replacement is at the beginning of this string.");
036: }
037:
038: public void testOccurrenceAtEnd() {
039: runAndCompare("This string ends with the pattern.",
040: "the pattern.", "a replacement.",
041: "This string ends with a replacement.");
042: }
043:
044: public void testReplaceWholeOriginal() {
045: runAndCompare("The whole shebang.", "The whole shebang.",
046: "Everything in its entirety.",
047: "Everything in its entirety.");
048: }
049:
050: public void testReplaceEverythingInPieces() {
051: runAndCompare("old?old?old?old?old?old?old?", "old?", "new!",
052: "new!new!new!new!new!new!new!");
053: }
054:
055: public void testMultipleOccurrencesSameLength() {
056: runAndCompare("abcOLDabcOLDabOLDOLDa", "OLD", "NEW",
057: "abcNEWabcNEWabNEWNEWa");
058: }
059:
060: public void testMultipleOccurrencesShorter() {
061: runAndCompare("abcOLDabcOLDabOLDOLDa", "OLD", "NE",
062: "abcNEabcNEabNENEa");
063: }
064:
065: public void testMultipleOccurrencesLonger() {
066: runAndCompare("abcOLDabcOLDabOLDOLDa", "OLD", "_REFURBISHED_",
067: "abc_REFURBISHED_abc_REFURBISHED_ab_REFURBISHED__REFURBISHED_a");
068: }
069:
070: public void testBlankReplacement() {
071: runAndCompare("abcOLDabcOLDabOLDOLDa", "OLD", "", "abcabcaba");
072: }
073:
074: public void testBlankPattern() {
075: runAndCompare("abcOLDabcOLDabOLDOLDa", "", "new",
076: "abcOLDabcOLDabOLDOLDa");
077: }
078:
079: public void testPatternLongerThanOrig() {
080: runAndCompare("This is a string", "This is a string pattern",
081: "This is a string replacement", "This is a string");
082: }
083:
084: public void testSingleLetterPatternSingleLetterReplacement() {
085: runAndCompare(
086: "in this string is a phrase in in which to replace the 'i's, including the last i",
087: "i",
088: "1",
089: "1n th1s str1ng 1s a phrase 1n 1n wh1ch to replace the '1's, 1nclud1ng the last 1");
090: }
091:
092: public void testSingleLetterPatternZeroLetterReplacement() {
093: runAndCompare(
094: "in this string is a phrase in in which to replace the 'i's, including the last i",
095: "i", "",
096: "n ths strng s a phrase n n whch to replace the ''s, ncludng the last ");
097: }
098:
099: public void testSingleLetterPatternMultiLetterReplacement() {
100: runAndCompare(
101: "in this string is a phrase in in which to replace the 'i's, including the last i",
102: "i",
103: "III",
104: "IIIn thIIIs strIIIng IIIs a phrase IIIn IIIn whIIIch to replace the 'III's, IIIncludIIIng the last III");
105: }
106:
107: public void testPatternIncludedInsideReplacement() {
108: // This checks that including the pattern to be replaced within
109: // the replacing text does not cause some sort of infinite loop.
110: runAndCompare("Some old text to be folded.", "old", "embold",
111: "Some embold text to be fembolded.");
112: }
113:
114: public void testBlankOriginal() {
115: runAndCompare("", "old", "new", "");
116: }
117:
118: public static Test reflectionSuite() {
119: TestSuite suite = new TestSuite(
120: StringTools_replaceAll_Test.class);
121: return suite;
122: }
123:
124: public static void main(String[] arg) {
125: junit.textui.TestRunner.run(reflectionSuite());
126: }
127: }
|