KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > WordUtilsTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 /**
26  * Unit tests for WordUtils class.
27  *
28  * @author <a HREF="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
29  * @author Henri Yandell
30  * @author Stephen Colebourne
31  * @version $Id: WordUtilsTest.java 161244 2005-04-14 06:16:36Z ggregory $
32  */

33 public class WordUtilsTest extends TestCase {
34
35     public WordUtilsTest(String JavaDoc name) {
36         super(name);
37     }
38
39     public static Test suite() {
40         TestSuite suite = new TestSuite(WordUtilsTest.class);
41         suite.setName("WordUtilsTests");
42         return suite;
43     }
44
45     //-----------------------------------------------------------------------
46
public void testConstructor() {
47         assertNotNull(new WordUtils());
48         Constructor JavaDoc[] cons = WordUtils.class.getDeclaredConstructors();
49         assertEquals(1, cons.length);
50         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
51         assertEquals(true, Modifier.isPublic(WordUtils.class.getModifiers()));
52         assertEquals(false, Modifier.isFinal(WordUtils.class.getModifiers()));
53     }
54     
55     //-----------------------------------------------------------------------
56
public void testWrap_StringInt() {
57         assertEquals(null, WordUtils.wrap(null, 20));
58         assertEquals(null, WordUtils.wrap(null, -1));
59         
60         assertEquals("", WordUtils.wrap("", 20));
61         assertEquals("", WordUtils.wrap("", -1));
62         
63         // normal
64
String JavaDoc systemNewLine = System.getProperty("line.separator");
65         String JavaDoc input = "Here is one line of text that is going to be wrapped after 20 columns.";
66         String JavaDoc expected = "Here is one line of" + systemNewLine + "text that is going"
67             + systemNewLine + "to be wrapped after" + systemNewLine + "20 columns.";
68         assertEquals(expected, WordUtils.wrap(input, 20));
69         
70         // long word at end
71
input = "Click here to jump to the jakarta website - http://jakarta.apache.org";
72         expected = "Click here to jump" + systemNewLine + "to the jakarta" + systemNewLine
73             + "website -" + systemNewLine + "http://jakarta.apache.org";
74         assertEquals(expected, WordUtils.wrap(input, 20));
75         
76         // long word in middle
77
input = "Click here, http://jakarta.apache.org, to jump to the jakarta website";
78         expected = "Click here," + systemNewLine + "http://jakarta.apache.org," + systemNewLine
79             + "to jump to the" + systemNewLine + "jakarta website";
80         assertEquals(expected, WordUtils.wrap(input, 20));
81     }
82     
83     public void testWrap_StringIntStringBoolean() {
84         assertEquals(null, WordUtils.wrap(null, 20, "\n", false));
85         assertEquals(null, WordUtils.wrap(null, 20, "\n", true));
86         assertEquals(null, WordUtils.wrap(null, 20, null, true));
87         assertEquals(null, WordUtils.wrap(null, 20, null, false));
88         assertEquals(null, WordUtils.wrap(null, -1, null, true));
89         assertEquals(null, WordUtils.wrap(null, -1, null, false));
90         
91         assertEquals("", WordUtils.wrap("", 20, "\n", false));
92         assertEquals("", WordUtils.wrap("", 20, "\n", true));
93         assertEquals("", WordUtils.wrap("", 20, null, false));
94         assertEquals("", WordUtils.wrap("", 20, null, true));
95         assertEquals("", WordUtils.wrap("", -1, null, false));
96         assertEquals("", WordUtils.wrap("", -1, null, true));
97         
98         // normal
99
String JavaDoc input = "Here is one line of text that is going to be wrapped after 20 columns.";
100         String JavaDoc expected = "Here is one line of\ntext that is going\nto be wrapped after\n20 columns.";
101         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
102         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
103
104         // unusual newline char
105
input = "Here is one line of text that is going to be wrapped after 20 columns.";
106         expected = "Here is one line of<br />text that is going<br />to be wrapped after<br />20 columns.";
107         assertEquals(expected, WordUtils.wrap(input, 20, "<br />", false));
108         assertEquals(expected, WordUtils.wrap(input, 20, "<br />", true));
109
110         // short line length
111
input = "Here is one line";
112         expected = "Here\nis one\nline";
113         assertEquals(expected, WordUtils.wrap(input, 6, "\n", false));
114         expected = "Here\nis\none\nline";
115         assertEquals(expected, WordUtils.wrap(input, 2, "\n", false));
116         assertEquals(expected, WordUtils.wrap(input, -1, "\n", false));
117
118         // system newline char
119
String JavaDoc systemNewLine = System.getProperty("line.separator");
120         input = "Here is one line of text that is going to be wrapped after 20 columns.";
121         expected = "Here is one line of" + systemNewLine + "text that is going" + systemNewLine
122             + "to be wrapped after" + systemNewLine + "20 columns.";
123         assertEquals(expected, WordUtils.wrap(input, 20, null, false));
124         assertEquals(expected, WordUtils.wrap(input, 20, null, true));
125
126         // with extra spaces
127
input = " Here: is one line of text that is going to be wrapped after 20 columns.";
128         expected = "Here: is one line\nof text that is \ngoing to be \nwrapped after 20 \ncolumns.";
129         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
130         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
131         
132         // with tab
133
input = "Here is\tone line of text that is going to be wrapped after 20 columns.";
134         expected = "Here is\tone line of\ntext that is going\nto be wrapped after\n20 columns.";
135         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
136         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
137         
138         // with tab at wrapColumn
139
input = "Here is one line of\ttext that is going to be wrapped after 20 columns.";
140         expected = "Here is one line\nof\ttext that is\ngoing to be wrapped\nafter 20 columns.";
141         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
142         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
143         
144         // difference because of long word
145
input = "Click here to jump to the jakarta website - http://jakarta.apache.org";
146         expected = "Click here to jump\nto the jakarta\nwebsite -\nhttp://jakarta.apache.org";
147         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
148         expected = "Click here to jump\nto the jakarta\nwebsite -\nhttp://jakarta.apach\ne.org";
149         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
150         
151         // difference because of long word in middle
152
input = "Click here, http://jakarta.apache.org, to jump to the jakarta website";
153         expected = "Click here,\nhttp://jakarta.apache.org,\nto jump to the\njakarta website";
154         assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
155         expected = "Click here,\nhttp://jakarta.apach\ne.org, to jump to\nthe jakarta website";
156         assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));
157 // System.err.println(expected);
158
// System.err.println(WordUtils.wrap(input, 20, "\n", false));
159
}
160     
161     //-----------------------------------------------------------------------
162
public void testCapitalize_String() {
163         assertEquals(null, WordUtils.capitalize(null));
164         assertEquals("", WordUtils.capitalize(""));
165         assertEquals(" ", WordUtils.capitalize(" "));
166         
167         assertEquals("I", WordUtils.capitalize("I") );
168         assertEquals("I", WordUtils.capitalize("i") );
169         assertEquals("I Am Here 123", WordUtils.capitalize("i am here 123") );
170         assertEquals("I Am Here 123", WordUtils.capitalize("I Am Here 123") );
171         assertEquals("I Am HERE 123", WordUtils.capitalize("i am HERE 123") );
172         assertEquals("I AM HERE 123", WordUtils.capitalize("I AM HERE 123") );
173     }
174     
175     public void testCapitalizeWithDelimiters_String() {
176         assertEquals(null, WordUtils.capitalize(null, null));
177         assertEquals("", WordUtils.capitalize("", new char[0]));
178         assertEquals(" ", WordUtils.capitalize(" ", new char[0]));
179         
180         char[] chars = new char[] { '-', '+', ' ', '@' };
181         assertEquals("I", WordUtils.capitalize("I", chars) );
182         assertEquals("I", WordUtils.capitalize("i", chars) );
183         assertEquals("I-Am Here+123", WordUtils.capitalize("i-am here+123", chars) );
184         assertEquals("I Am+Here-123", WordUtils.capitalize("I Am+Here-123", chars) );
185         assertEquals("I+Am-HERE 123", WordUtils.capitalize("i+am-HERE 123", chars) );
186         assertEquals("I-AM HERE+123", WordUtils.capitalize("I-AM HERE+123", chars) );
187         chars = new char[] {'.'};
188         assertEquals("I aM.Fine", WordUtils.capitalize("i aM.fine", chars) );
189         assertEquals("I Am.fine", WordUtils.capitalize("i am.fine", null) );
190     }
191
192     public void testCapitalizeFully_String() {
193         assertEquals(null, WordUtils.capitalizeFully(null));
194         assertEquals("", WordUtils.capitalizeFully(""));
195         assertEquals(" ", WordUtils.capitalizeFully(" "));
196         
197         assertEquals("I", WordUtils.capitalizeFully("I") );
198         assertEquals("I", WordUtils.capitalizeFully("i") );
199         assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am here 123") );
200         assertEquals("I Am Here 123", WordUtils.capitalizeFully("I Am Here 123") );
201         assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am HERE 123") );
202         assertEquals("I Am Here 123", WordUtils.capitalizeFully("I AM HERE 123") );
203     }
204     
205     public void testCapitalizeFullyWithDelimiters_String() {
206         assertEquals(null, WordUtils.capitalizeFully(null, null));
207         assertEquals("", WordUtils.capitalizeFully("", new char[0]));
208         assertEquals(" ", WordUtils.capitalizeFully(" ", new char[0]));
209         
210         char[] chars = new char[] { '-', '+', ' ', '@' };
211         assertEquals("I", WordUtils.capitalizeFully("I", chars) );
212         assertEquals("I", WordUtils.capitalizeFully("i", chars) );
213         assertEquals("I-Am Here+123", WordUtils.capitalizeFully("i-am here+123", chars) );
214         assertEquals("I Am+Here-123", WordUtils.capitalizeFully("I Am+Here-123", chars) );
215         assertEquals("I+Am-Here 123", WordUtils.capitalizeFully("i+am-HERE 123", chars) );
216         assertEquals("I-Am Here+123", WordUtils.capitalizeFully("I-AM HERE+123", chars) );
217         chars = new char[] {'.'};
218         assertEquals("I am.Fine", WordUtils.capitalizeFully("i aM.fine", chars) );
219         assertEquals("I Am.fine", WordUtils.capitalizeFully("i am.fine", null) );
220     }
221
222     public void testUncapitalize_String() {
223         assertEquals(null, WordUtils.uncapitalize(null));
224         assertEquals("", WordUtils.uncapitalize(""));
225         assertEquals(" ", WordUtils.uncapitalize(" "));
226         
227         assertEquals("i", WordUtils.uncapitalize("I") );
228         assertEquals("i", WordUtils.uncapitalize("i") );
229         assertEquals("i am here 123", WordUtils.uncapitalize("i am here 123") );
230         assertEquals("i am here 123", WordUtils.uncapitalize("I Am Here 123") );
231         assertEquals("i am hERE 123", WordUtils.uncapitalize("i am HERE 123") );
232         assertEquals("i aM hERE 123", WordUtils.uncapitalize("I AM HERE 123") );
233     }
234     
235     public void testUncapitalizeWithDelimiters_String() {
236         assertEquals(null, WordUtils.uncapitalize(null, null));
237         assertEquals("", WordUtils.uncapitalize("", new char[0]));
238         assertEquals(" ", WordUtils.uncapitalize(" ", new char[0]));
239         
240         char[] chars = new char[] { '-', '+', ' ', '@' };
241         assertEquals("i", WordUtils.uncapitalize("I", chars) );
242         assertEquals("i", WordUtils.uncapitalize("i", chars) );
243         assertEquals("i am-here+123", WordUtils.uncapitalize("i am-here+123", chars) );
244         assertEquals("i+am here-123", WordUtils.uncapitalize("I+Am Here-123", chars) );
245         assertEquals("i-am+hERE 123", WordUtils.uncapitalize("i-am+HERE 123", chars) );
246         assertEquals("i aM-hERE+123", WordUtils.uncapitalize("I AM-HERE+123", chars) );
247         chars = new char[] {'.'};
248         assertEquals("i AM.fINE", WordUtils.uncapitalize("I AM.FINE", chars) );
249         assertEquals("i aM.FINE", WordUtils.uncapitalize("I AM.FINE", null) );
250     }
251     
252     public void testSwapCase_String() {
253         assertEquals(null, WordUtils.swapCase(null));
254         assertEquals("", WordUtils.swapCase(""));
255         assertEquals(" ", WordUtils.swapCase(" "));
256         
257         assertEquals("i", WordUtils.swapCase("I") );
258         assertEquals("I", WordUtils.swapCase("i") );
259         assertEquals("I AM HERE 123", WordUtils.swapCase("i am here 123") );
260         assertEquals("i aM hERE 123", WordUtils.swapCase("I Am Here 123") );
261         assertEquals("I AM here 123", WordUtils.swapCase("i am HERE 123") );
262         assertEquals("i am here 123", WordUtils.swapCase("I AM HERE 123") );
263     }
264
265 }
266
Popular Tags