001: package com.mockrunner.test.util;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007: import java.util.TreeMap;
008:
009: import junit.framework.TestCase;
010:
011: import com.mockrunner.util.common.StringUtil;
012:
013: public class StringUtilTest extends TestCase {
014: public void testReplaceAll() {
015: try {
016: StringUtil.replaceAll(null, "test", "test");
017: fail();
018: } catch (IllegalArgumentException exc) {
019: //should throw exception
020: }
021: try {
022: StringUtil.replaceAll("test", null, "test");
023: fail();
024: } catch (IllegalArgumentException exc) {
025: //should throw exception
026: }
027: try {
028: StringUtil.replaceAll("test", "test", null);
029: fail();
030: } catch (IllegalArgumentException exc) {
031: //should throw exception
032: }
033: try {
034: StringUtil.replaceAll("test", "", "test");
035: fail();
036: } catch (IllegalArgumentException exc) {
037: //should throw exception
038: }
039: assertEquals("test", StringUtil.replaceAll("test", "test",
040: "test"));
041: assertEquals("", StringUtil.replaceAll("", "test", "test"));
042: assertEquals("abcd", StringUtil.replaceAll("abcd", "test",
043: "test"));
044: assertEquals("test", StringUtil.replaceAll("abcd", "abcd",
045: "test"));
046: assertEquals("test", StringUtil.replaceAll("test", "testabc",
047: "1"));
048: assertEquals("tfst", StringUtil.replaceAll("test", "e", "f"));
049: assertEquals("tft", StringUtil.replaceAll("test", "es", "f"));
050: assertEquals("", StringUtil.replaceAll("test", "test", ""));
051: assertEquals("hello world", StringUtil.replaceAll("test",
052: "test", "hello world"));
053: assertEquals("hello worldhello world", StringUtil.replaceAll(
054: "testtest", "test", "hello world"));
055: assertEquals("ab123abkkjuuababab", StringUtil.replaceAll(
056: "a123akkjuuaaa", "a", "ab"));
057: assertEquals("aaaa", StringUtil
058: .replaceAll("aaaaaaa", "aa", "a"));
059: assertEquals("this is a test", StringUtil.replaceAll(
060: "this is a xxxx", "xxxx", "test"));
061: assertEquals("aaaaa", StringUtil.replaceAll("aaaaa", "a", "a"));
062: assertEquals("bbbbb", StringUtil.replaceAll("aaaaa", "a", "b"));
063: assertEquals("bbbb", StringUtil.replaceAll("aabbaa", "aa", "b"));
064: assertEquals("Hella warld", StringUtil.replaceAll(
065: "Hello world", "o", "a"));
066: assertEquals("Hello world", StringUtil.replaceAll("a", "a",
067: "Hello world"));
068: assertEquals("Hello world ", StringUtil.replaceAll(" ", " ",
069: "Hello world"));
070: assertEquals("aabc", StringUtil.replaceAll("abcdabc", "abcd",
071: "a"));
072: }
073:
074: public void testCompare() {
075: assertEquals(-1, StringUtil.compare("", ""));
076: assertEquals(-1, StringUtil.compare("", "abc"));
077: assertEquals(-1, StringUtil.compare("123", ""));
078: assertEquals(-1, StringUtil.compare("123", "abc"));
079: assertEquals(-1, StringUtil.compare("ASD", "asd"));
080: assertEquals(0, StringUtil.compare("a", "a"));
081: assertEquals(0, StringUtil.compare("a", "ab"));
082: assertEquals(0, StringUtil.compare("123", "1bc"));
083: assertEquals(2, StringUtil.compare("123", "123"));
084: assertEquals(2, StringUtil.compare("123", "1234"));
085: assertEquals(2, StringUtil.compare("1234", "123"));
086: assertEquals(11, StringUtil.compare(" Hello World",
087: " Hello World "));
088: }
089:
090: public void testLowerCase() {
091: assertEquals("aBC", StringUtil.lowerCase("ABC", 0));
092: assertEquals("ABc", StringUtil.lowerCase("ABC", 2));
093: assertEquals("abc", StringUtil.lowerCase("abc", 1));
094: try {
095: StringUtil.lowerCase("abc", 3);
096: fail();
097: } catch (IndexOutOfBoundsException exc) {
098: //should throw exception
099: }
100: }
101:
102: public void testLowerCaseWithEndIndex() {
103: assertEquals("abC", StringUtil.lowerCase("ABC", 0, 2));
104: assertEquals("AbC", StringUtil.lowerCase("ABC", 1, 2));
105: assertEquals("abc", StringUtil.lowerCase("abc", 0, 1));
106: assertEquals("aBC", StringUtil.lowerCase("ABC", 0, 0));
107: assertEquals("aBC", StringUtil.lowerCase("ABC", 0, -1));
108: assertEquals("abc", StringUtil.lowerCase("ABC", 0, 3));
109: try {
110: StringUtil.lowerCase("abc", 0, 4);
111: fail();
112: } catch (IndexOutOfBoundsException exc) {
113: //should throw exception
114: }
115: }
116:
117: public void testSplit() {
118: String test = "This;;is;a;; ;test; String;;";
119: String[] tokens = StringUtil.split(test, ";", true);
120: assertTrue(tokens.length == 9);
121: assertEquals("This", tokens[0]);
122: assertEquals(null, tokens[1]);
123: assertEquals("is", tokens[2]);
124: assertEquals("a", tokens[3]);
125: assertEquals(null, tokens[4]);
126: assertEquals(null, tokens[5]);
127: assertEquals("test", tokens[6]);
128: assertEquals("String", tokens[7]);
129: assertEquals(null, tokens[8]);
130: tokens = StringUtil.split(test, "test", false);
131: assertTrue(tokens.length == 2);
132: assertEquals("This;;is;a;; ;", tokens[0]);
133: assertEquals("; String;;", tokens[1]);
134: }
135:
136: public void testAppendObjectsAsString() {
137: List list = new ArrayList();
138: list.add("Test");
139: list.add("Test");
140: list.add(new Integer(1000));
141: list.add(null);
142: list.add(new Long(3));
143: StringBuffer buffer = new StringBuffer();
144: StringUtil.appendObjectsAsString(buffer, list);
145: assertEquals("Test\nTest\n1000\nnull\n3\n", buffer.toString());
146: }
147:
148: public void testCountMatches() {
149: assertEquals(0, StringUtil.countMatches(null, null));
150: assertEquals(0, StringUtil.countMatches(null, ""));
151: assertEquals(0, StringUtil.countMatches("", null));
152: assertEquals(0, StringUtil.countMatches("", ""));
153: assertEquals(1, StringUtil.countMatches("a", "a"));
154: assertEquals(3, StringUtil.countMatches("aaa", "a"));
155: assertEquals(2, StringUtil.countMatches("abcabcab", "abc"));
156: assertEquals(2, StringUtil.countMatches("abcababc", "abc"));
157: assertEquals(3, StringUtil.countMatches("abcababc", "a"));
158: assertEquals(1, StringUtil.countMatches("Hello World", "ld"));
159: assertEquals(3, StringUtil.countMatches(" Hello World ", " "));
160: assertEquals(3, StringUtil.countMatches("112211111", "11"));
161: assertEquals(1, StringUtil.countMatches("aaaaa", "aaa"));
162: assertEquals(1, StringUtil.countMatches("bbb", "bb"));
163: assertEquals(0, StringUtil.countMatches("bbb", "bbbb"));
164: }
165:
166: public void testMatchesExact() {
167: assertTrue(StringUtil.matchesExact("", "", true));
168: assertTrue(StringUtil.matchesExact("", "", false));
169: assertTrue(StringUtil.matchesExact("abc", "abc", true));
170: assertTrue(StringUtil.matchesExact("aBc", "abc", false));
171: assertFalse(StringUtil.matchesExact("aBc", "abc", true));
172: assertFalse(StringUtil.matchesExact("", "abc", true));
173: assertFalse(StringUtil.matchesExact("abc", "abcd", true));
174: assertFalse(StringUtil.matchesExact("abcd", "abc", false));
175: }
176:
177: public void testMatchesContains() {
178: assertTrue(StringUtil.matchesContains("", "", true));
179: assertTrue(StringUtil.matchesContains("", "", false));
180: assertTrue(StringUtil.matchesContains("abc", "abc", true));
181: assertTrue(StringUtil.matchesContains("abcd", "abc", true));
182: assertTrue(StringUtil.matchesContains("aBcDDD", "abc", false));
183: assertTrue(StringUtil.matchesContains("aBcDDD", "", true));
184: assertFalse(StringUtil.matchesContains("aBc", "abc", true));
185: assertFalse(StringUtil.matchesContains("", "abc", true));
186: assertFalse(StringUtil.matchesContains("abc", "abcd", true));
187: }
188:
189: public void testMatchesPerl5() {
190: assertTrue(StringUtil.matchesPerl5("abc", "abc", true));
191: assertTrue(StringUtil.matchesPerl5("abc", "a[abc]c", true));
192: assertTrue(StringUtil.matchesPerl5("aBc", "a[abc]c", false));
193: assertTrue(StringUtil.matchesPerl5("abc", ".*", true));
194: assertFalse(StringUtil.matchesPerl5("aBc", "abc", true));
195: assertFalse(StringUtil.matchesPerl5("aBc", "a[abc]c", true));
196: assertFalse(StringUtil.matchesPerl5("a[abc]c", "abc", true));
197: }
198:
199: public void testFieldToString() {
200: assertEquals("test: class java.lang.String", StringUtil
201: .fieldToString("test", String.class));
202: assertEquals("test: 3", StringUtil.fieldToString("test",
203: new Integer(3)));
204: assertEquals("test: empty", StringUtil.fieldToString("test",
205: new ArrayList()));
206: assertEquals("test: empty", StringUtil.fieldToString("test",
207: new Object[0]));
208: List testList = new ArrayList();
209: testList.add(new Integer(5));
210: testList.add("abc");
211: testList.add(this .getClass());
212: assertEquals(
213: "test 0: 5\ntest 1: abc\ntest 2: class com.mockrunner.test.util.StringUtilTest",
214: StringUtil.fieldToString("test", testList));
215: assertEquals(
216: "test 0: 5\ntest 1: abc\ntest 2: class com.mockrunner.test.util.StringUtilTest",
217: StringUtil.fieldToString("test", testList.toArray()));
218: Map testMap = new TreeMap();
219: testMap.put("5", "xyz");
220: testMap.put("abc", "xyz");
221: testMap.put("123", null);
222: assertEquals("test 123: null\ntest 5: xyz\ntest abc: xyz",
223: StringUtil.fieldToString("test", testMap));
224: assertEquals("test: empty", StringUtil.fieldToString("test",
225: new HashMap()));
226: }
227: }
|