001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /**
020: * Package to test JOrphanUtils methods
021: */package org.apache.jorphan.util;
022:
023: import junit.framework.TestCase;
024:
025: public class TestJorphanUtils extends TestCase {
026:
027: public TestJorphanUtils() {
028: super ();
029: }
030:
031: public TestJorphanUtils(String arg0) {
032: super (arg0);
033: }
034:
035: public void testReplace1() {
036: assertEquals("xyzdef", JOrphanUtils.replaceFirst("abcdef",
037: "abc", "xyz"));
038: }
039:
040: public void testReplace2() {
041: assertEquals("axyzdef", JOrphanUtils.replaceFirst("abcdef",
042: "bc", "xyz"));
043: }
044:
045: public void testReplace3() {
046: assertEquals("abcxyz", JOrphanUtils.replaceFirst("abcdef",
047: "def", "xyz"));
048: }
049:
050: public void testReplace4() {
051: assertEquals("abcdef", JOrphanUtils.replaceFirst("abcdef",
052: "bce", "xyz"));
053: }
054:
055: public void testReplace5() {
056: assertEquals("abcdef", JOrphanUtils.replaceFirst("abcdef",
057: "alt=\"\" ", ""));
058: }
059:
060: public void testReplace6() {
061: assertEquals("abcdef", JOrphanUtils.replaceFirst("abcdef",
062: "alt=\"\" ", ""));
063: }
064:
065: public void testReplace7() {
066: assertEquals("alt=\"\"", JOrphanUtils.replaceFirst("alt=\"\"",
067: "alt=\"\" ", ""));
068: }
069:
070: public void testReplace8() {
071: assertEquals("img src=xyz ", JOrphanUtils.replaceFirst(
072: "img src=xyz alt=\"\" ", "alt=\"\" ", ""));
073: }
074:
075: // Tests for split(String,String,boolean)
076: public void testSplit1() {
077: String in = "a,bc,,"; // Test ignore trailing split characters
078: String out[] = JOrphanUtils.split(in, ",", true);// Ignore adjacent delimiters
079: assertEquals(2, out.length);
080: assertEquals("a", out[0]);
081: assertEquals("bc", out[1]);
082: out = JOrphanUtils.split(in, ",", false);
083: assertEquals("Should detect the trailing split chars; ", 4,
084: out.length);
085: assertEquals("a", out[0]);
086: assertEquals("bc", out[1]);
087: assertEquals("", out[2]);
088: assertEquals("", out[3]);
089: }
090:
091: public void testSplit2() {
092: String in = ",,a,bc"; // Test leading split characters
093: String out[] = JOrphanUtils.split(in, ",", true);
094: assertEquals(2, out.length);
095: assertEquals("a", out[0]);
096: assertEquals("bc", out[1]);
097: out = JOrphanUtils.split(in, ",", false);
098: assertEquals("Should detect the leading split chars; ", 4,
099: out.length);
100: assertEquals("", out[0]);
101: assertEquals("", out[1]);
102: assertEquals("a", out[2]);
103: assertEquals("bc", out[3]);
104: }
105:
106: public void testSplit3() {
107: String in = "a,bc,,"; // Test ignore trailing split characters
108: String out[] = JOrphanUtils.split(in, ",", true);// Ignore adjacent delimiters
109: assertEquals(2, out.length);
110: assertEquals("a", out[0]);
111: assertEquals("bc", out[1]);
112: out = JOrphanUtils.split(in, ",", false);
113: assertEquals("Should detect the trailing split chars; ", 4,
114: out.length);
115: assertEquals("a", out[0]);
116: assertEquals("bc", out[1]);
117: assertEquals("", out[2]);
118: assertEquals("", out[3]);
119: }
120:
121: public void testSplit4() {
122: String in = " , ,a ,bc"; // Test leading split characters
123: String out[] = JOrphanUtils.split(in, " ,", true);
124: assertEquals(2, out.length);
125: assertEquals("a", out[0]);
126: assertEquals("bc", out[1]);
127: out = JOrphanUtils.split(in, " ,", false);
128: assertEquals("Should detect the leading split chars; ", 4,
129: out.length);
130: assertEquals("", out[0]);
131: assertEquals("", out[1]);
132: assertEquals("a", out[2]);
133: assertEquals("bc", out[3]);
134: }
135:
136: public void testTruncate() throws Exception {
137: String in = "a;,b;,;,;,d;,e;,;,f";
138: String[] out = JOrphanUtils.split(in, ";,", true);
139: assertEquals(5, out.length);
140: assertEquals("a", out[0]);
141: assertEquals("b", out[1]);
142: assertEquals("d", out[2]);
143: assertEquals("e", out[3]);
144: assertEquals("f", out[4]);
145: out = JOrphanUtils.split(in, ";,", false);
146: assertEquals(8, out.length);
147: assertEquals("a", out[0]);
148: assertEquals("b", out[1]);
149: assertEquals("", out[2]);
150: assertEquals("", out[3]);
151: assertEquals("d", out[4]);
152: assertEquals("e", out[5]);
153: assertEquals("", out[6]);
154: assertEquals("f", out[7]);
155:
156: }
157:
158: public void testSplit5() throws Exception {
159: String in = "a;;b;;;;;;d;;e;;;;f";
160: String[] out = JOrphanUtils.split(in, ";;", true);
161: assertEquals(5, out.length);
162: assertEquals("a", out[0]);
163: assertEquals("b", out[1]);
164: assertEquals("d", out[2]);
165: assertEquals("e", out[3]);
166: assertEquals("f", out[4]);
167: out = JOrphanUtils.split(in, ";;", false);
168: assertEquals(8, out.length);
169: assertEquals("a", out[0]);
170: assertEquals("b", out[1]);
171: assertEquals("", out[2]);
172: assertEquals("", out[3]);
173: assertEquals("d", out[4]);
174: assertEquals("e", out[5]);
175: assertEquals("", out[6]);
176: assertEquals("f", out[7]);
177:
178: }
179:
180: // Tests for split(String,String,String)
181: public void testSplitSSS1() {
182: String in = "a,bc,,"; // Test non-empty parameters
183: String out[] = JOrphanUtils.split(in, ",", "?");
184: assertEquals(4, out.length);
185: assertEquals("a", out[0]);
186: assertEquals("bc", out[1]);
187: assertEquals("?", out[2]);
188: assertEquals("?", out[3]);
189: }
190:
191: public void testSplitSSS2() {
192: String in = "a,bc,,"; // Empty default
193: String out[] = JOrphanUtils.split(in, ",", "");
194: assertEquals(4, out.length);
195: assertEquals("a", out[0]);
196: assertEquals("bc", out[1]);
197: assertEquals("", out[2]);
198: assertEquals("", out[3]);
199: }
200:
201: public void testSplitSSS3() {
202: String in = "a,bc,,"; // Empty delimiter
203: String out[] = JOrphanUtils.split(in, "", "?");
204: assertEquals(1, out.length);
205: assertEquals(in, out[0]);
206: }
207:
208: public void testSplitSSS4() {
209: String in = "a,b;c,,"; // Multiple delimiters
210: String out[];
211: out = JOrphanUtils.split(in, ",;", "?");
212: assertEquals(5, out.length);
213: assertEquals("a", out[0]);
214: assertEquals("b", out[1]);
215: assertEquals("c", out[2]);
216: assertEquals("?", out[3]);
217: assertEquals("?", out[4]);
218: out = JOrphanUtils.split(in, ",;", "");
219: assertEquals(5, out.length);
220: assertEquals("a", out[0]);
221: assertEquals("b", out[1]);
222: assertEquals("c", out[2]);
223: assertEquals("", out[3]);
224: assertEquals("", out[4]);
225: }
226:
227: public void testSplitSSS5() {
228: String in = "a,bc,,"; // Delimiter same as splitter
229: String out[] = JOrphanUtils.split(in, ",", ",");
230: assertEquals(4, out.length);
231: assertEquals("a", out[0]);
232: assertEquals("bc", out[1]);
233: assertEquals(",", out[2]);
234: assertEquals(",", out[3]);
235: }
236:
237: public void testSplitSSSNulls() {
238: String in = "a,bc,,";
239: String out[];
240: try {
241: out = JOrphanUtils.split(null, ",", "?");
242: assertEquals(0, out.length);
243: fail("Expecting NullPointerException");
244: } catch (NullPointerException ignored) {
245: //Ignored
246: }
247: try {
248: out = JOrphanUtils.split(in, null, "?");
249: assertEquals(0, out.length);
250: fail("Expecting NullPointerException");
251: } catch (NullPointerException ignored) {
252: //Ignored
253: }
254: }
255:
256: public void testSplitSSSNull() {
257: String out[];
258: out = JOrphanUtils.split("a,bc,,", ",", null);
259: assertEquals(2, out.length);
260: assertEquals("a", out[0]);
261: assertEquals("bc", out[1]);
262:
263: out = JOrphanUtils.split("a,;bc,;,", ",;", null);
264: assertEquals(2, out.length);
265: assertEquals("a", out[0]);
266: assertEquals("bc", out[1]);
267: }
268:
269: public void testSplitSSSNone() {
270: String out[];
271: out = JOrphanUtils.split("", ",", "x");
272: assertEquals(0, out.length);
273:
274: out = JOrphanUtils.split("a,;bc,;,", "", "x");
275: assertEquals(1, out.length);
276: assertEquals("a,;bc,;,", out[0]);
277: }
278:
279: public void testreplaceAllChars() {
280: assertEquals(JOrphanUtils.replaceAllChars("", ' ', "+"), "");
281: String in, out;
282: in = "source";
283: assertEquals(JOrphanUtils.replaceAllChars(in, ' ', "+"), in);
284: out = "so+rce";
285: assertEquals(JOrphanUtils.replaceAllChars(in, 'u', "+"), out);
286: in = "A B C ";
287: out = "A+B++C+";
288: assertEquals(JOrphanUtils.replaceAllChars(in, ' ', "+"), out);
289: }
290:
291: public void testTrim() {
292: assertEquals("", JOrphanUtils.trim("", " ;"));
293: assertEquals("", JOrphanUtils.trim(" ", " ;"));
294: assertEquals("", JOrphanUtils.trim("; ", " ;"));
295: assertEquals("", JOrphanUtils.trim(";;", " ;"));
296: assertEquals("", JOrphanUtils.trim(" ", " ;"));
297: assertEquals("abc", JOrphanUtils.trim("abc ;", " ;"));
298: }
299: }
|