001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.util.test;
006:
007: import com.sun.portal.rewriter.RewriterModule;
008: import com.sun.portal.rewriter.test.util.BasicTestCase;
009: import com.sun.portal.rewriter.util.StringHelper;
010: import junit.framework.TestSuite;
011: import junitx.util.PrivateAccessor;
012:
013: import java.util.Arrays;
014:
015: public class TestStringHelper extends BasicTestCase {
016: public TestStringHelper(String aName) {
017: super (aName);
018: }//constuctor
019:
020: public void testSearchAndReplace() throws Exception {
021: String[][] lData = { { "abczyz", "abc", null, "zyz", },
022:
023: { "abczyz", null, "abc", "abczyz", },
024:
025: { null, null, "abc", null, },
026:
027: { null, "abc", "abc", null, },
028:
029: { "a&bcdef", //source
030: "&", //search
031: "&", // replace
032: "a&bcdef" // result
033: },
034:
035: { "a&bcdef", //source
036: "&e;", //search
037: "&", // replace
038: "a&bcdef" // result
039: },
040:
041: { "a&<bcdef", //source
042: "<", //search
043: "&", // replace
044: "a&&bcdef" // result
045: },
046:
047: { "a&>bcdef", //source
048: ">", //search
049: ">", // replace
050: "a&>bcdef" // result
051: },
052:
053: { "a&"bcdef", //source
054: """, //search
055: "'", // replace
056: "a&'bcdef" // result
057: },
058:
059: { "a&bcdef", //source
060: "&", //search
061: "&", // replace
062: "a&bcdef" // result
063: },
064:
065: { "abcdef", //source
066: "b", //search
067: "x", // replace
068: "axcdef" // result
069: }, //0
070:
071: { "abcdef", //source
072: "b", //search
073: "b", // replace
074: "abcdef" // result
075: }, //1
076:
077: { "axxyyzzbcdef", //source
078: "xy", //search
079: "y", // replace
080: "axyyzzbcdef" // result
081: }, //2
082:
083: { "a\\\\*bcdef", //source
084: "\\*", //search
085: "*", // replace
086: "a\\*bcdef" // result
087: }, //3
088: };
089:
090: String result;
091: for (int i = 0; i < lData.length; i++) {
092: result = StringHelper.searchAndReplace(lData[i][0],
093: lData[i][1], lData[i][2]);
094: assertEquals("Failed at i=" + i, lData[i][3], result);
095:
096: result = StringHelper.searchAndReplace(lData[i][0],
097: new String[][] { { lData[i][1], lData[i][2] } });
098: assertEquals("Failed at i=" + i, lData[i][3], result);
099: }
100: }//testSearchAndReplace()
101:
102: public void testSearchAndReplaceVarient() throws Exception {
103: Object[][] lData = {
104: {
105: "abc>&abc",
106: new String[][] { { ">", ">" },
107: { "&", "&" } },
108: "abc&gt;&abc", },
109:
110: {
111: "abc>&abc",
112: new String[][] { { "&", "&" },
113: { ">", ">" } }, //order changed w.r.t to above one
114: "abc>&abc", },
115:
116: { "abczyz", new String[][] { { "abc", "" } }, "zyz", },
117:
118: { "abczyz", new String[][] { { "abc", null } }, "zyz", },
119:
120: { "abczyz", new String[][] { { "abc", "abc" } },
121: "abczyz", },
122:
123: { null, new String[][] { { "abc", "abc" } }, null, },
124:
125: { "abczyz", null, "abczyz", },
126:
127: { null, null, null, }, };
128:
129: for (int i = 0; i < lData.length; i++) {
130: String bResult = StringHelper.searchAndReplace(
131: (String) lData[i][0], (String[][]) lData[i][1]);
132: assertEquals("Failed at i=" + i, lData[i][2], bResult);
133: }
134: }// testSearchAndReplaceVarient()
135:
136: public void testTokenize() throws Throwable {
137: Object[][] lData = {
138: { "", //source
139: ":", //token list
140: new String[0], //tokenized list with empty strings
141: new String[0], //without empty strings
142: }, //0
143:
144: { ":::", ":", new String[] { "", "", "", "" },
145: new String[0], }, //1
146:
147: {
148: "jini://raja.sun. com ;mahesh.sun.com;jini://srinu.sil.co.in",
149: ";",
150: new String[] { "jini://raja.sun. com ",
151: "mahesh.sun.com",
152: "jini://srinu.sil.co.in" },
153: new String[] { "jini://raja.sun. com ",
154: "mahesh.sun.com",
155: "jini://srinu.sil.co.in" }, }, //2
156:
157: { "test", " ", new String[] { "test" },
158: new String[] { "test" }, }, //3
159:
160: { " test", " ", new String[] { "", "test" },
161: new String[] { "test" }, }, //4
162:
163: { " test", " ", new String[] { "", "", "test" },
164: new String[] { "test" }, }, //5
165:
166: { " test ", " ",
167: new String[] { "", "", "test", "", "" },
168: new String[] { "test" }, }, //6
169:
170: { "test abc xyz", " ",
171: new String[] { "test", "abc", "xyz" },
172: new String[] { "test", "abc", "xyz" }, }, //7
173:
174: { " test abc xyz", " ",
175: new String[] { "", "test", "abc", "xyz" },
176: new String[] { "test", "abc", "xyz" }, }, //8
177:
178: { ",y,,,,", ",",
179: new String[] { "", "y", "", "", "", "" },
180: new String[] { "y" }, }, //9
181:
182: { ",y", ",", new String[] { "", "y" },
183: new String[] { "y" }, }, //10
184:
185: {
186: " ,y", //space
187: ",", new String[] { " ", "y" },
188: new String[] { " ", "y" }, }, //11
189:
190: { ",y,,", ",", new String[] { "", "y", "", "" },
191: new String[] { "y" }, }, //12
192:
193: { ",,,,y,,", ",",
194: new String[] { "", "", "", "", "y", "", "" },
195: new String[] { "y" }, }, //13
196:
197: { ",y,", ",", new String[] { "", "y", "" },
198: new String[] { "y" }, }, //14
199: };
200:
201: for (int i = 0; i < lData.length; i++) {
202:
203: {
204: String[] b = (String[]) PrivateAccessor.invoke(
205: StringHelper.class, "tokenizeNonPerfWay",
206: new Class[] { String.class, String.class,
207: boolean.class }, new Object[] {
208: lData[i][0], lData[i][1],
209: new Boolean(true) });
210: assertEquals("non per tokenize() Failed at i=" + i,
211: Arrays.asList((String[]) lData[i][2]), Arrays
212: .asList(b));
213: }
214:
215: assertEquals("tokenize() with empty included, Failed at i="
216: + i, Arrays.asList((String[]) lData[i][2]), Arrays
217: .asList(StringHelper.tokenize(lData[i][0]
218: .toString(), lData[i][1].toString(), true)));
219:
220: {
221: String[] c = (String[]) PrivateAccessor.invoke(
222: StringHelper.class, "tokenizeNonPerfWay",
223: new Class[] { String.class, String.class,
224: boolean.class }, new Object[] {
225: lData[i][0], lData[i][1],
226: new Boolean(false) });
227:
228: assertEquals(
229: "non perf eee tokenize() Failed at i=" + i,
230: Arrays.asList((String[]) lData[i][3]), Arrays
231: .asList(c));
232: }
233:
234: assertEquals("tokenize() without empty, Failed at i=" + i,
235: Arrays.asList((String[]) lData[i][3]), Arrays
236: .asList(StringHelper
237: .tokenize(lData[i][0].toString(),
238: lData[i][1].toString())));
239: }
240: }//testTokenize()
241:
242: public void testTrimQuotes() {
243: String[][] lData = { { "ABCdEd\"\\' \\", "ABCdEd\"\\' \\" },
244: { "ABCdEd\"\\' \\'", "ABCdEd" }, { null, null },
245: { "", "" }, { " ", "" }, { "''''", "" },
246: { "\"\" abc\"'", "abc" }, { "''''abc", "abc" },
247: { "abcd", "abcd" }, { "ABCdEd", "ABCdEd" }, };
248:
249: for (int i = 0; i < lData.length; i++) {
250: assertEquals(lData[i][1], StringHelper
251: .trimQuotes(lData[i][0]));
252: }
253: }//testTrimQuotes()
254:
255: public void testEscapedTokenInTokenize() {
256: Object[][] lData = { { "\\\\*abc;", //Escaped Pattern
257: new String[] { "\\\\*abc" } //tokenized
258: },
259:
260: { "\\\\*abc\\;", //Escaped Pattern
261: new String[] { "\\\\*abc;" } //tokenized
262: }, };
263:
264: for (int i = 0; i < lData.length; i++) {
265: String[] lExpectation = (String[]) lData[i][1];
266:
267: String[] lResult = StringHelper.tokenize(lData[i][0]
268: .toString(), ";");
269: assertEquals("Expected no of substrings does not match i="
270: + i, lExpectation.length, lResult.length);
271:
272: for (int j = 0; j < lExpectation.length; j++) {
273: assertEquals("Failed at pattern=" + i + ",token=," + j,
274: lExpectation[j], lResult[j]);
275: }
276: }
277: }//testEscapedTokenInTokenize()
278:
279: public static void main(String[] args) {
280: RewriterModule.initFile();
281: //BasicTestCase.run( TestStringHelper.class );
282: TestSuite testSuite = new TestSuite();
283: testSuite.addTest(new TestStringHelper(
284: "testEscapedTokenInTokenize"));
285: BasicTestCase.run(testSuite);
286: }//main()
287:
288: }//class TestStringHelper
|