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: package org.apache.commons.lang;
018:
019: import java.lang.reflect.Constructor;
020: import java.lang.reflect.Modifier;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: /**
027: * Unit tests for WordUtils class.
028: *
029: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
030: * @author Stephen Colebourne
031: * @version $Id: WordUtilsTest.java 471626 2006-11-06 04:02:09Z bayard $
032: */
033: public class WordUtilsTest extends TestCase {
034:
035: public WordUtilsTest(String name) {
036: super (name);
037: }
038:
039: public static Test suite() {
040: TestSuite suite = new TestSuite(WordUtilsTest.class);
041: suite.setName("WordUtilsTests");
042: return suite;
043: }
044:
045: //-----------------------------------------------------------------------
046: public void testConstructor() {
047: assertNotNull(new WordUtils());
048: Constructor[] cons = WordUtils.class.getDeclaredConstructors();
049: assertEquals(1, cons.length);
050: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
051: assertEquals(true, Modifier.isPublic(WordUtils.class
052: .getModifiers()));
053: assertEquals(false, Modifier.isFinal(WordUtils.class
054: .getModifiers()));
055: }
056:
057: //-----------------------------------------------------------------------
058: public void testWrap_StringInt() {
059: assertEquals(null, WordUtils.wrap(null, 20));
060: assertEquals(null, WordUtils.wrap(null, -1));
061:
062: assertEquals("", WordUtils.wrap("", 20));
063: assertEquals("", WordUtils.wrap("", -1));
064:
065: // normal
066: String systemNewLine = System.getProperty("line.separator");
067: String input = "Here is one line of text that is going to be wrapped after 20 columns.";
068: String expected = "Here is one line of" + systemNewLine
069: + "text that is going" + systemNewLine
070: + "to be wrapped after" + systemNewLine + "20 columns.";
071: assertEquals(expected, WordUtils.wrap(input, 20));
072:
073: // long word at end
074: input = "Click here to jump to the jakarta website - http://jakarta.apache.org";
075: expected = "Click here to jump" + systemNewLine
076: + "to the jakarta" + systemNewLine + "website -"
077: + systemNewLine + "http://jakarta.apache.org";
078: assertEquals(expected, WordUtils.wrap(input, 20));
079:
080: // long word in middle
081: input = "Click here, http://jakarta.apache.org, to jump to the jakarta website";
082: expected = "Click here," + systemNewLine
083: + "http://jakarta.apache.org," + systemNewLine
084: + "to jump to the" + systemNewLine + "jakarta website";
085: assertEquals(expected, WordUtils.wrap(input, 20));
086: }
087:
088: public void testWrap_StringIntStringBoolean() {
089: assertEquals(null, WordUtils.wrap(null, 20, "\n", false));
090: assertEquals(null, WordUtils.wrap(null, 20, "\n", true));
091: assertEquals(null, WordUtils.wrap(null, 20, null, true));
092: assertEquals(null, WordUtils.wrap(null, 20, null, false));
093: assertEquals(null, WordUtils.wrap(null, -1, null, true));
094: assertEquals(null, WordUtils.wrap(null, -1, null, false));
095:
096: assertEquals("", WordUtils.wrap("", 20, "\n", false));
097: assertEquals("", WordUtils.wrap("", 20, "\n", true));
098: assertEquals("", WordUtils.wrap("", 20, null, false));
099: assertEquals("", WordUtils.wrap("", 20, null, true));
100: assertEquals("", WordUtils.wrap("", -1, null, false));
101: assertEquals("", WordUtils.wrap("", -1, null, true));
102:
103: // normal
104: String input = "Here is one line of text that is going to be wrapped after 20 columns.";
105: String expected = "Here is one line of\ntext that is going\nto be wrapped after\n20 columns.";
106: assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
107: assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
108:
109: // unusual newline char
110: input = "Here is one line of text that is going to be wrapped after 20 columns.";
111: expected = "Here is one line of<br />text that is going<br />to be wrapped after<br />20 columns.";
112: assertEquals(expected, WordUtils.wrap(input, 20, "<br />",
113: false));
114: assertEquals(expected, WordUtils
115: .wrap(input, 20, "<br />", true));
116:
117: // short line length
118: input = "Here is one line";
119: expected = "Here\nis one\nline";
120: assertEquals(expected, WordUtils.wrap(input, 6, "\n", false));
121: expected = "Here\nis\none\nline";
122: assertEquals(expected, WordUtils.wrap(input, 2, "\n", false));
123: assertEquals(expected, WordUtils.wrap(input, -1, "\n", false));
124:
125: // system newline char
126: String systemNewLine = System.getProperty("line.separator");
127: input = "Here is one line of text that is going to be wrapped after 20 columns.";
128: expected = "Here is one line of" + systemNewLine
129: + "text that is going" + systemNewLine
130: + "to be wrapped after" + systemNewLine + "20 columns.";
131: assertEquals(expected, WordUtils.wrap(input, 20, null, false));
132: assertEquals(expected, WordUtils.wrap(input, 20, null, true));
133:
134: // with extra spaces
135: input = " Here: is one line of text that is going to be wrapped after 20 columns.";
136: expected = "Here: is one line\nof text that is \ngoing to be \nwrapped after 20 \ncolumns.";
137: assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
138: assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
139:
140: // with tab
141: input = "Here is\tone line of text that is going to be wrapped after 20 columns.";
142: expected = "Here is\tone line of\ntext that is going\nto be wrapped after\n20 columns.";
143: assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
144: assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
145:
146: // with tab at wrapColumn
147: input = "Here is one line of\ttext that is going to be wrapped after 20 columns.";
148: expected = "Here is one line\nof\ttext that is\ngoing to be wrapped\nafter 20 columns.";
149: assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
150: assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
151:
152: // difference because of long word
153: input = "Click here to jump to the jakarta website - http://jakarta.apache.org";
154: expected = "Click here to jump\nto the jakarta\nwebsite -\nhttp://jakarta.apache.org";
155: assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
156: expected = "Click here to jump\nto the jakarta\nwebsite -\nhttp://jakarta.apach\ne.org";
157: assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
158:
159: // difference because of long word in middle
160: input = "Click here, http://jakarta.apache.org, to jump to the jakarta website";
161: expected = "Click here,\nhttp://jakarta.apache.org,\nto jump to the\njakarta website";
162: assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
163: expected = "Click here,\nhttp://jakarta.apach\ne.org, to jump to\nthe jakarta website";
164: assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
165: // System.err.println(expected);
166: // System.err.println(WordUtils.wrap(input, 20, "\n", false));
167: }
168:
169: //-----------------------------------------------------------------------
170: public void testCapitalize_String() {
171: assertEquals(null, WordUtils.capitalize(null));
172: assertEquals("", WordUtils.capitalize(""));
173: assertEquals(" ", WordUtils.capitalize(" "));
174:
175: assertEquals("I", WordUtils.capitalize("I"));
176: assertEquals("I", WordUtils.capitalize("i"));
177: assertEquals("I Am Here 123", WordUtils
178: .capitalize("i am here 123"));
179: assertEquals("I Am Here 123", WordUtils
180: .capitalize("I Am Here 123"));
181: assertEquals("I Am HERE 123", WordUtils
182: .capitalize("i am HERE 123"));
183: assertEquals("I AM HERE 123", WordUtils
184: .capitalize("I AM HERE 123"));
185: }
186:
187: public void testCapitalizeWithDelimiters_String() {
188: assertEquals(null, WordUtils.capitalize(null, null));
189: assertEquals("", WordUtils.capitalize("", new char[0]));
190: assertEquals(" ", WordUtils.capitalize(" ", new char[0]));
191:
192: char[] chars = new char[] { '-', '+', ' ', '@' };
193: assertEquals("I", WordUtils.capitalize("I", chars));
194: assertEquals("I", WordUtils.capitalize("i", chars));
195: assertEquals("I-Am Here+123", WordUtils.capitalize(
196: "i-am here+123", chars));
197: assertEquals("I Am+Here-123", WordUtils.capitalize(
198: "I Am+Here-123", chars));
199: assertEquals("I+Am-HERE 123", WordUtils.capitalize(
200: "i+am-HERE 123", chars));
201: assertEquals("I-AM HERE+123", WordUtils.capitalize(
202: "I-AM HERE+123", chars));
203: chars = new char[] { '.' };
204: assertEquals("I aM.Fine", WordUtils.capitalize("i aM.fine",
205: chars));
206: assertEquals("I Am.fine", WordUtils.capitalize("i am.fine",
207: null));
208: }
209:
210: public void testCapitalizeFully_String() {
211: assertEquals(null, WordUtils.capitalizeFully(null));
212: assertEquals("", WordUtils.capitalizeFully(""));
213: assertEquals(" ", WordUtils.capitalizeFully(" "));
214:
215: assertEquals("I", WordUtils.capitalizeFully("I"));
216: assertEquals("I", WordUtils.capitalizeFully("i"));
217: assertEquals("I Am Here 123", WordUtils
218: .capitalizeFully("i am here 123"));
219: assertEquals("I Am Here 123", WordUtils
220: .capitalizeFully("I Am Here 123"));
221: assertEquals("I Am Here 123", WordUtils
222: .capitalizeFully("i am HERE 123"));
223: assertEquals("I Am Here 123", WordUtils
224: .capitalizeFully("I AM HERE 123"));
225: }
226:
227: public void testCapitalizeFullyWithDelimiters_String() {
228: assertEquals(null, WordUtils.capitalizeFully(null, null));
229: assertEquals("", WordUtils.capitalizeFully("", new char[0]));
230: assertEquals(" ", WordUtils.capitalizeFully(" ", new char[0]));
231:
232: char[] chars = new char[] { '-', '+', ' ', '@' };
233: assertEquals("I", WordUtils.capitalizeFully("I", chars));
234: assertEquals("I", WordUtils.capitalizeFully("i", chars));
235: assertEquals("I-Am Here+123", WordUtils.capitalizeFully(
236: "i-am here+123", chars));
237: assertEquals("I Am+Here-123", WordUtils.capitalizeFully(
238: "I Am+Here-123", chars));
239: assertEquals("I+Am-Here 123", WordUtils.capitalizeFully(
240: "i+am-HERE 123", chars));
241: assertEquals("I-Am Here+123", WordUtils.capitalizeFully(
242: "I-AM HERE+123", chars));
243: chars = new char[] { '.' };
244: assertEquals("I am.Fine", WordUtils.capitalizeFully(
245: "i aM.fine", chars));
246: assertEquals("I Am.fine", WordUtils.capitalizeFully(
247: "i am.fine", null));
248: }
249:
250: public void testUncapitalize_String() {
251: assertEquals(null, WordUtils.uncapitalize(null));
252: assertEquals("", WordUtils.uncapitalize(""));
253: assertEquals(" ", WordUtils.uncapitalize(" "));
254:
255: assertEquals("i", WordUtils.uncapitalize("I"));
256: assertEquals("i", WordUtils.uncapitalize("i"));
257: assertEquals("i am here 123", WordUtils
258: .uncapitalize("i am here 123"));
259: assertEquals("i am here 123", WordUtils
260: .uncapitalize("I Am Here 123"));
261: assertEquals("i am hERE 123", WordUtils
262: .uncapitalize("i am HERE 123"));
263: assertEquals("i aM hERE 123", WordUtils
264: .uncapitalize("I AM HERE 123"));
265: }
266:
267: public void testUncapitalizeWithDelimiters_String() {
268: assertEquals(null, WordUtils.uncapitalize(null, null));
269: assertEquals("", WordUtils.uncapitalize("", new char[0]));
270: assertEquals(" ", WordUtils.uncapitalize(" ", new char[0]));
271:
272: char[] chars = new char[] { '-', '+', ' ', '@' };
273: assertEquals("i", WordUtils.uncapitalize("I", chars));
274: assertEquals("i", WordUtils.uncapitalize("i", chars));
275: assertEquals("i am-here+123", WordUtils.uncapitalize(
276: "i am-here+123", chars));
277: assertEquals("i+am here-123", WordUtils.uncapitalize(
278: "I+Am Here-123", chars));
279: assertEquals("i-am+hERE 123", WordUtils.uncapitalize(
280: "i-am+HERE 123", chars));
281: assertEquals("i aM-hERE+123", WordUtils.uncapitalize(
282: "I AM-HERE+123", chars));
283: chars = new char[] { '.' };
284: assertEquals("i AM.fINE", WordUtils.uncapitalize("I AM.FINE",
285: chars));
286: assertEquals("i aM.FINE", WordUtils.uncapitalize("I AM.FINE",
287: null));
288: }
289:
290: //-----------------------------------------------------------------------
291: public void testInitials_String() {
292: assertEquals(null, WordUtils.initials(null));
293: assertEquals("", WordUtils.initials(""));
294: assertEquals("", WordUtils.initials(" "));
295:
296: assertEquals("I", WordUtils.initials("I"));
297: assertEquals("i", WordUtils.initials("i"));
298: assertEquals("BJL", WordUtils.initials("Ben John Lee"));
299: assertEquals("BJ", WordUtils.initials("Ben J.Lee"));
300: assertEquals("BJ.L", WordUtils.initials(" Ben John . Lee"));
301: assertEquals("iah1", WordUtils.initials("i am here 123"));
302: }
303:
304: // -----------------------------------------------------------------------
305: public void testInitials_String_charArray() {
306: char[] array = null;
307: assertEquals(null, WordUtils.initials(null, array));
308: assertEquals("", WordUtils.initials("", array));
309: assertEquals("", WordUtils.initials(" ", array));
310: assertEquals("I", WordUtils.initials("I", array));
311: assertEquals("i", WordUtils.initials("i", array));
312: assertEquals("S", WordUtils.initials("SJC", array));
313: assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
314: assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
315: assertEquals("BJ.L", WordUtils.initials(" Ben John . Lee",
316: array));
317: assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
318: assertEquals("iah1", WordUtils.initials("i am here 123", array));
319:
320: array = new char[0];
321: assertEquals(null, WordUtils.initials(null, array));
322: assertEquals("", WordUtils.initials("", array));
323: assertEquals("", WordUtils.initials(" ", array));
324: assertEquals("", WordUtils.initials("I", array));
325: assertEquals("", WordUtils.initials("i", array));
326: assertEquals("", WordUtils.initials("SJC", array));
327: assertEquals("", WordUtils.initials("Ben John Lee", array));
328: assertEquals("", WordUtils.initials("Ben J.Lee", array));
329: assertEquals("", WordUtils
330: .initials(" Ben John . Lee", array));
331: assertEquals("", WordUtils.initials("Kay O'Murphy", array));
332: assertEquals("", WordUtils.initials("i am here 123", array));
333:
334: array = " ".toCharArray();
335: assertEquals(null, WordUtils.initials(null, array));
336: assertEquals("", WordUtils.initials("", array));
337: assertEquals("", WordUtils.initials(" ", array));
338: assertEquals("I", WordUtils.initials("I", array));
339: assertEquals("i", WordUtils.initials("i", array));
340: assertEquals("S", WordUtils.initials("SJC", array));
341: assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
342: assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
343: assertEquals("BJ.L", WordUtils.initials(" Ben John . Lee",
344: array));
345: assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
346: assertEquals("iah1", WordUtils.initials("i am here 123", array));
347:
348: array = " .".toCharArray();
349: assertEquals(null, WordUtils.initials(null, array));
350: assertEquals("", WordUtils.initials("", array));
351: assertEquals("", WordUtils.initials(" ", array));
352: assertEquals("I", WordUtils.initials("I", array));
353: assertEquals("i", WordUtils.initials("i", array));
354: assertEquals("S", WordUtils.initials("SJC", array));
355: assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
356: assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
357: assertEquals("BJL", WordUtils.initials(" Ben John . Lee",
358: array));
359: assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
360: assertEquals("iah1", WordUtils.initials("i am here 123", array));
361:
362: array = " .'".toCharArray();
363: assertEquals(null, WordUtils.initials(null, array));
364: assertEquals("", WordUtils.initials("", array));
365: assertEquals("", WordUtils.initials(" ", array));
366: assertEquals("I", WordUtils.initials("I", array));
367: assertEquals("i", WordUtils.initials("i", array));
368: assertEquals("S", WordUtils.initials("SJC", array));
369: assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
370: assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
371: assertEquals("BJL", WordUtils.initials(" Ben John . Lee",
372: array));
373: assertEquals("KOM", WordUtils.initials("Kay O'Murphy", array));
374: assertEquals("iah1", WordUtils.initials("i am here 123", array));
375:
376: array = "SIJo1".toCharArray();
377: assertEquals(null, WordUtils.initials(null, array));
378: assertEquals("", WordUtils.initials("", array));
379: assertEquals(" ", WordUtils.initials(" ", array));
380: assertEquals("", WordUtils.initials("I", array));
381: assertEquals("i", WordUtils.initials("i", array));
382: assertEquals("C", WordUtils.initials("SJC", array));
383: assertEquals("Bh", WordUtils.initials("Ben John Lee", array));
384: assertEquals("B.", WordUtils.initials("Ben J.Lee", array));
385: assertEquals(" h", WordUtils.initials(" Ben John . Lee",
386: array));
387: assertEquals("K", WordUtils.initials("Kay O'Murphy", array));
388: assertEquals("i2", WordUtils.initials("i am here 123", array));
389: }
390:
391: // -----------------------------------------------------------------------
392: public void testSwapCase_String() {
393: assertEquals(null, WordUtils.swapCase(null));
394: assertEquals("", WordUtils.swapCase(""));
395: assertEquals(" ", WordUtils.swapCase(" "));
396:
397: assertEquals("i", WordUtils.swapCase("I"));
398: assertEquals("I", WordUtils.swapCase("i"));
399: assertEquals("I AM HERE 123", WordUtils
400: .swapCase("i am here 123"));
401: assertEquals("i aM hERE 123", WordUtils
402: .swapCase("I Am Here 123"));
403: assertEquals("I AM here 123", WordUtils
404: .swapCase("i am HERE 123"));
405: assertEquals("i am here 123", WordUtils
406: .swapCase("I AM HERE 123"));
407:
408: String test = "This String contains a TitleCase character: \u01C8";
409: String expect = "tHIS sTRING CONTAINS A tITLEcASE CHARACTER: \u01C9";
410: assertEquals(expect, WordUtils.swapCase(test));
411: }
412:
413: }
|