001: package jimm.datavision.test;
002:
003: import jimm.util.StringUtils;
004: import jimm.util.Replacer;
005: import java.util.ArrayList;
006: import java.util.List;
007: import junit.framework.TestCase;
008: import junit.framework.TestSuite;
009: import junit.framework.Test;
010:
011: public class StringUtilsTest extends TestCase {
012:
013: public static Test suite() {
014: return new TestSuite(StringUtilsTest.class);
015: }
016:
017: public StringUtilsTest(String name) {
018: super (name);
019: }
020:
021: public void testEscapeXML() {
022: assertEquals("", StringUtils.escapeXML(null));
023: assertEquals("", StringUtils.escapeXML(""));
024: assertEquals("abc&def", StringUtils.escapeXML("abc&def"));
025: assertEquals("abc"def'", StringUtils
026: .escapeXML("abc\"def'"));
027: assertEquals("<>", StringUtils.escapeXML("<>"));
028: }
029:
030: public void testUnescapeXML() {
031: assertEquals("", StringUtils.unescapeXML(null));
032: assertEquals("", StringUtils.unescapeXML(""));
033: assertEquals("abc&def", StringUtils.unescapeXML("abc&def"));
034: assertEquals("abc\"def'", StringUtils
035: .unescapeXML("abc"def'"));
036: assertEquals("<>", StringUtils.unescapeXML("<>"));
037: assertEquals("abc def", StringUtils.unescapeXML("abcdef"));
038: }
039:
040: public void testNewlines() {
041: assertEquals("", StringUtils.newlinesToXHTMLBreaks(null));
042: assertEquals("", StringUtils.newlinesToXHTMLBreaks(""));
043: assertEquals("\n<br />", StringUtils
044: .newlinesToXHTMLBreaks("\n"));
045: assertEquals("\n<br />", StringUtils
046: .newlinesToXHTMLBreaks("\r"));
047: assertEquals("\n<br />", StringUtils
048: .newlinesToXHTMLBreaks("\r\n"));
049: assertEquals("hello\n<br />world", StringUtils
050: .newlinesToXHTMLBreaks("hello\nworld"));
051: assertEquals("hello\n<br />world", StringUtils
052: .newlinesToXHTMLBreaks("hello\rworld"));
053: assertEquals("hello\n<br />world", StringUtils
054: .newlinesToXHTMLBreaks("hello\r\nworld"));
055: }
056:
057: public void testSplit() {
058: ArrayList typicalAnswer = new ArrayList();
059: typicalAnswer.add("a");
060: typicalAnswer.add("b");
061: typicalAnswer.add("c");
062:
063: ArrayList origStringAnswer = new ArrayList();
064: origStringAnswer.add("abc");
065:
066: // Special cases
067: assertNull(StringUtils.split(null, null));
068: assertEquals(origStringAnswer, StringUtils.split("abc", null));
069: assertEquals(origStringAnswer, StringUtils.split("abc", "x"));
070:
071: assertEquals(typicalAnswer, StringUtils.split("a.b.c", "."));
072: assertEquals(typicalAnswer, StringUtils.split("a**&*b**&*c",
073: "**&*"));
074:
075: ArrayList answer = new ArrayList(typicalAnswer);
076: answer.add(0, "");
077: assertEquals(answer, StringUtils.split(".a.b.c", "."));
078: assertEquals(answer, StringUtils.split("**&*a**&*b**&*c",
079: "**&*"));
080:
081: answer = new ArrayList(typicalAnswer);
082: answer.add(1, "");
083: assertEquals(answer, StringUtils.split("a..b.c", "."));
084: assertEquals(answer, StringUtils.split("a**&***&*b**&*c",
085: "**&*"));
086:
087: answer = new ArrayList(typicalAnswer);
088: answer.add("");
089: assertEquals(answer, StringUtils.split("a.b.c.", "."));
090: assertEquals(answer, StringUtils.split("a**&*b**&*c**&*",
091: "**&*"));
092:
093: answer = new ArrayList(typicalAnswer);
094: answer.add(1, "");
095: answer.add("");
096: assertEquals(answer, StringUtils.split("a..b.c.", "."));
097: assertEquals(answer, StringUtils.split("a**&***&*b**&*c**&*",
098: "**&*"));
099: }
100:
101: public void testJoin() {
102: assertEquals("", StringUtils.join(null, null));
103: assertEquals("", StringUtils.join(null, ","));
104:
105: ArrayList list = new ArrayList();
106: assertEquals("", StringUtils.join(list, null));
107: assertEquals("", StringUtils.join(list, ","));
108:
109: list.add("a");
110: assertEquals("a", StringUtils.join(list, null));
111: assertEquals("a", StringUtils.join(list, ","));
112:
113: list.add("xyzzy");
114: assertEquals("axyzzy", StringUtils.join(list, null));
115: assertEquals("a,xyzzy", StringUtils.join(list, ","));
116: assertEquals("a-*-xyzzy", StringUtils.join(list, "-*-"));
117: }
118:
119: public void testReplace() {
120: Replacer r = new Replacer() {
121: public Object replace(String s) {
122: return "x";
123: }
124: };
125:
126: assertNull(StringUtils.replaceDelimited("", "", null, null));
127: assertNull(StringUtils.replaceDelimited(null, null, null, null));
128: assertEquals("abcde", StringUtils.replaceDelimited(null, null,
129: r, "abcde"));
130: assertEquals("abcde", StringUtils.replaceDelimited("", "", r,
131: "abcde"));
132: assertEquals("", StringUtils.replaceDelimited("{%", "}", r, ""));
133:
134: assertEquals("abxde", StringUtils.replaceDelimited("{%", "}",
135: r, "ab{%c}de"));
136: assertEquals("xabxdex", StringUtils.replaceDelimited("{%", "}",
137: r, "{%}ab{%c}de{% }"));
138: assertEquals("abcd{%e", StringUtils.replaceDelimited("{%", "}",
139: r, "abcd{%e"));
140: assertEquals(
141: "x.nil? ? nil : x / 100.0",
142: StringUtils
143: .replaceDelimited("{", "}", r,
144: "{jobs.hourly rate}.nil? ? nil : {jobs.hourly rate} / 100.0"));
145: }
146:
147: public void testReplaceNotAfter() {
148: Replacer r = new Replacer() {
149: public Object replace(String s) {
150: return "x";
151: }
152: };
153:
154: assertNull(StringUtils
155: .replaceDelimited("#", "", "", null, null));
156: assertNull(StringUtils.replaceDelimited("#", "#", "#", null,
157: null));
158: assertNull(StringUtils.replaceDelimited("#", null, null, null,
159: null));
160: assertEquals("abcde", StringUtils.replaceDelimited("#", null,
161: null, r, "abcde"));
162: assertEquals("abcde", StringUtils.replaceDelimited("#", "", "",
163: r, "abcde"));
164: assertEquals("", StringUtils.replaceDelimited("#", "{%", "}",
165: r, ""));
166:
167: assertEquals("abxde", StringUtils.replaceDelimited("#", "{%",
168: "}", r, "ab{%c}de"));
169: assertEquals("xabxdex", StringUtils.replaceDelimited("#", "{%",
170: "}", r, "{%}ab{%c}de{% }"));
171: assertEquals("abcd{%e", StringUtils.replaceDelimited("#", "{%",
172: "}", r, "abcd{%e"));
173:
174: assertEquals("ab#{c}de", StringUtils.replaceDelimited("#", "{",
175: "}", r, "ab#{c}de"));
176: assertEquals("ab##{c}de", StringUtils.replaceDelimited("#",
177: "{", "}", r, "ab##{c}de"));
178: assertEquals("ab##{x}dex", StringUtils.replaceDelimited("#",
179: "{", "}", r, "ab##{{c}}de{z}"));
180: assertEquals("ab#{x}de", StringUtils.replaceDelimited("#", "{",
181: "}", r, "ab#{{c}}de"));
182: assertEquals("ab#{x}de", StringUtils.replaceDelimited("#",
183: "{%", "}", r, "ab#{{%c}}de"));
184: }
185:
186: public void testAvoidInfiniteLoop() {
187: final int[] count = new int[1];
188: count[0] = 0;
189: Replacer r = new Replacer() {
190: public Object replace(String s) {
191: if (++count[0] == 2)
192: fail("caught in an infinite loop");
193: return "{" + s + "}";
194: }
195: };
196: assertEquals("a{}b", StringUtils.replaceDelimited(null, "{",
197: "}", r, "a{}b"));
198: }
199:
200: public void testLineSplit() {
201: ArrayList answer = new ArrayList();
202: answer.add("");
203:
204: List split = StringUtils.splitIntoLines("");
205: assertEquals(answer.size(), split.size());
206: assertEquals(answer, split);
207:
208: // The three flavors of line endings
209: String[] endings = { "\n", "\r", "\r\n" };
210:
211: // We want to see an array with one empty string (the text before the
212: // newline). There is no string after the newline.
213: for (int i = 0; i < endings.length; ++i) {
214: split = StringUtils.splitIntoLines(endings[i]);
215: assertEquals(answer.size(), split.size());
216: assertEquals(answer, split);
217: }
218:
219: answer.add("x");
220: for (int i = 0; i < endings.length; ++i) {
221: split = StringUtils.splitIntoLines(endings[i] + "x");
222: assertEquals(answer.size(), split.size());
223: assertEquals(answer, split);
224: }
225:
226: answer = new ArrayList();
227: answer.add("line one");
228: answer.add("part deux");
229: answer.add("three's a crowd, eh?");
230: answer.add("");
231:
232: split = StringUtils
233: .splitIntoLines("line one\npart deux\nthree's a crowd, eh?"
234: + "\n\n");
235: assertEquals(answer.size(), split.size());
236: assertEquals(answer, split);
237:
238: // Test two newlines in a row
239: answer = new ArrayList();
240: answer.add("two newlines after this line");
241: answer.add("");
242: answer.add("part deux");
243: answer.add("three's a crowd, eh?");
244: answer.add("");
245:
246: split = StringUtils
247: .splitIntoLines("two newlines after this line\n\npart deux\nthree's a crowd, eh?"
248: + "\n\n");
249: assertEquals(answer.size(), split.size());
250: assertEquals(answer, split);
251:
252: split = StringUtils
253: .splitIntoLines("two newlines after this line\r\n\r\npart deux\r\nthree's a crowd, eh?"
254: + "\n\n");
255: assertEquals(answer.size(), split.size());
256: assertEquals(answer, split);
257:
258: split = StringUtils
259: .splitIntoLines("two newlines after this line\r\rpart deux\rthree's a crowd, eh?"
260: + "\r\r");
261: assertEquals(answer.size(), split.size());
262: assertEquals(answer, split);
263: }
264:
265: public void testSplitUp() {
266: StringBuffer buf = new StringBuffer();
267: StringUtils.splitUp(buf, null);
268: assertEquals("", buf.toString());
269:
270: buf = new StringBuffer();
271: StringUtils.splitUp(buf, "abcde abcde abcde abcde", 12);
272: assertEquals("abcde abcde\nabcde abcde", buf.toString());
273:
274: buf = new StringBuffer();
275: StringUtils.splitUp(buf, "abcdeabcdeabcdeabcde", 12);
276: assertEquals("abcdeabcdeabcdeabcde", buf.toString());
277:
278: buf = new StringBuffer();
279: StringUtils.splitUp(buf, " abcde", 12);
280: assertEquals("abcde", buf.toString());
281:
282: buf = new StringBuffer();
283: StringUtils.splitUp(buf,
284: " abcde abcde abcde abcde ", 12);
285: assertEquals("abcde\nabcde\nabcde\nabcde", buf.toString());
286: }
287:
288: public static void main(String[] args) {
289: junit.textui.TestRunner.run(suite());
290: System.exit(0);
291: }
292:
293: }
|