KickJava   Java API By Example, From Geeks To Geeks.

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


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 import junit.textui.TestRunner;
25
26 /**
27  * Unit tests {@link org.apache.commons.lang.CharSetUtils}.
28  *
29  * @author <a HREF="mailto:bayard@generationjava.com">Henri Yandell</a>
30  * @author <a HREF="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
31  * @author Stephen Colebourne
32  * @author Gary D. Gregory
33  * @version $Id: CharSetUtilsTest.java 161244 2005-04-14 06:16:36Z ggregory $
34  */

35 public class CharSetUtilsTest extends TestCase {
36     
37     public CharSetUtilsTest(String JavaDoc name) {
38         super(name);
39     }
40
41     public static void main(String JavaDoc[] args) {
42         TestRunner.run(suite());
43     }
44
45     public static Test suite() {
46         TestSuite suite = new TestSuite(CharSetUtilsTest.class);
47         suite.setName("CharSetUtils Tests");
48         return suite;
49     }
50
51     protected void setUp() throws Exception JavaDoc {
52         super.setUp();
53     }
54
55     protected void tearDown() throws Exception JavaDoc {
56         super.tearDown();
57     }
58
59     //-----------------------------------------------------------------------
60
public void testConstructor() {
61         assertNotNull(new CharSetUtils());
62         Constructor JavaDoc[] cons = CharSetUtils.class.getDeclaredConstructors();
63         assertEquals(1, cons.length);
64         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
65         assertEquals(true, Modifier.isPublic(CharSetUtils.class.getModifiers()));
66         assertEquals(false, Modifier.isFinal(CharSetUtils.class.getModifiers()));
67     }
68     
69     //-----------------------------------------------------------------------
70
public void testEvaluateSet_Stringarray() {
71         assertEquals(null, CharSetUtils.evaluateSet((String JavaDoc[]) null));
72         assertEquals("[]", CharSetUtils.evaluateSet(new String JavaDoc[0]).toString());
73         assertEquals("[]", CharSetUtils.evaluateSet(new String JavaDoc[] {null}).toString());
74         assertEquals("[a-e]", CharSetUtils.evaluateSet(new String JavaDoc[] {"a-e"}).toString());
75     }
76     
77     //-----------------------------------------------------------------------
78
public void testSqueeze_StringString() {
79         assertEquals(null, CharSetUtils.squeeze(null, (String JavaDoc) null));
80         assertEquals(null, CharSetUtils.squeeze(null, ""));
81         
82         assertEquals("", CharSetUtils.squeeze("", (String JavaDoc) null));
83         assertEquals("", CharSetUtils.squeeze("", ""));
84         assertEquals("", CharSetUtils.squeeze("", "a-e"));
85         
86         assertEquals("hello", CharSetUtils.squeeze("hello", (String JavaDoc) null));
87         assertEquals("hello", CharSetUtils.squeeze("hello", ""));
88         assertEquals("hello", CharSetUtils.squeeze("hello", "a-e"));
89         assertEquals("helo", CharSetUtils.squeeze("hello", "l-p"));
90         assertEquals("heloo", CharSetUtils.squeeze("helloo", "l"));
91         assertEquals("hello", CharSetUtils.squeeze("helloo", "^l"));
92     }
93     
94     public void testSqueeze_StringStringarray() {
95         assertEquals(null, CharSetUtils.squeeze(null, (String JavaDoc[]) null));
96         assertEquals(null, CharSetUtils.squeeze(null, new String JavaDoc[0]));
97         assertEquals(null, CharSetUtils.squeeze(null, new String JavaDoc[] {null}));
98         assertEquals(null, CharSetUtils.squeeze(null, new String JavaDoc[] {"el"}));
99         
100         assertEquals("", CharSetUtils.squeeze("", (String JavaDoc[]) null));
101         assertEquals("", CharSetUtils.squeeze("", new String JavaDoc[0]));
102         assertEquals("", CharSetUtils.squeeze("", new String JavaDoc[] {null}));
103         assertEquals("", CharSetUtils.squeeze("", new String JavaDoc[] {"a-e"}));
104         
105         assertEquals("hello", CharSetUtils.squeeze("hello", (String JavaDoc[]) null));
106         assertEquals("hello", CharSetUtils.squeeze("hello", new String JavaDoc[0]));
107         assertEquals("hello", CharSetUtils.squeeze("hello", new String JavaDoc[] {null}));
108         assertEquals("hello", CharSetUtils.squeeze("hello", new String JavaDoc[] {"a-e"}));
109         
110         assertEquals("helo", CharSetUtils.squeeze("hello", new String JavaDoc[] { "el" }));
111         assertEquals("hello", CharSetUtils.squeeze("hello", new String JavaDoc[] { "e" }));
112         assertEquals("fofof", CharSetUtils.squeeze("fooffooff", new String JavaDoc[] { "of" }));
113         assertEquals("fof", CharSetUtils.squeeze("fooooff", new String JavaDoc[] { "fo" }));
114     }
115
116     //-----------------------------------------------------------------------
117
public void testCount_StringString() {
118         assertEquals(0, CharSetUtils.count(null, (String JavaDoc) null));
119         assertEquals(0, CharSetUtils.count(null, ""));
120         
121         assertEquals(0, CharSetUtils.count("", (String JavaDoc) null));
122         assertEquals(0, CharSetUtils.count("", ""));
123         assertEquals(0, CharSetUtils.count("", "a-e"));
124         
125         assertEquals(0, CharSetUtils.count("hello", (String JavaDoc) null));
126         assertEquals(0, CharSetUtils.count("hello", ""));
127         assertEquals(1, CharSetUtils.count("hello", "a-e"));
128         assertEquals(3, CharSetUtils.count("hello", "l-p"));
129     }
130     
131     public void testCount_StringStringarray() {
132         assertEquals(0, CharSetUtils.count(null, (String JavaDoc[]) null));
133         assertEquals(0, CharSetUtils.count(null, new String JavaDoc[0]));
134         assertEquals(0, CharSetUtils.count(null, new String JavaDoc[] {null}));
135         assertEquals(0, CharSetUtils.count(null, new String JavaDoc[] {"a-e"}));
136         
137         assertEquals(0, CharSetUtils.count("", (String JavaDoc[]) null));
138         assertEquals(0, CharSetUtils.count("", new String JavaDoc[0]));
139         assertEquals(0, CharSetUtils.count("", new String JavaDoc[] {null}));
140         assertEquals(0, CharSetUtils.count("", new String JavaDoc[] {"a-e"}));
141         
142         assertEquals(0, CharSetUtils.count("hello", (String JavaDoc[]) null));
143         assertEquals(0, CharSetUtils.count("hello", new String JavaDoc[0]));
144         assertEquals(0, CharSetUtils.count("hello", new String JavaDoc[] {null}));
145         assertEquals(1, CharSetUtils.count("hello", new String JavaDoc[] {"a-e"}));
146         
147         assertEquals(3, CharSetUtils.count("hello", new String JavaDoc[] { "el" }));
148         assertEquals(0, CharSetUtils.count("hello", new String JavaDoc[] { "x" }));
149         assertEquals(2, CharSetUtils.count("hello", new String JavaDoc[] { "e-i" }));
150         assertEquals(5, CharSetUtils.count("hello", new String JavaDoc[] { "a-z" }));
151         assertEquals(0, CharSetUtils.count("hello", new String JavaDoc[] { "" }));
152     }
153
154     //-----------------------------------------------------------------------
155
public void testKeep_StringString() {
156         assertEquals(null, CharSetUtils.keep(null, (String JavaDoc) null));
157         assertEquals(null, CharSetUtils.keep(null, ""));
158         
159         assertEquals("", CharSetUtils.keep("", (String JavaDoc) null));
160         assertEquals("", CharSetUtils.keep("", ""));
161         assertEquals("", CharSetUtils.keep("", "a-e"));
162         
163         assertEquals("", CharSetUtils.keep("hello", (String JavaDoc) null));
164         assertEquals("", CharSetUtils.keep("hello", ""));
165         assertEquals("", CharSetUtils.keep("hello", "xyz"));
166         assertEquals("hello", CharSetUtils.keep("hello", "a-z"));
167         assertEquals("hello", CharSetUtils.keep("hello", "oleh"));
168         assertEquals("ell", CharSetUtils.keep("hello", "el"));
169     }
170     
171     public void testKeep_StringStringarray() {
172         assertEquals(null, CharSetUtils.keep(null, (String JavaDoc[]) null));
173         assertEquals(null, CharSetUtils.keep(null, new String JavaDoc[0]));
174         assertEquals(null, CharSetUtils.keep(null, new String JavaDoc[] {null}));
175         assertEquals(null, CharSetUtils.keep(null, new String JavaDoc[] {"a-e"}));
176         
177         assertEquals("", CharSetUtils.keep("", (String JavaDoc[]) null));
178         assertEquals("", CharSetUtils.keep("", new String JavaDoc[0]));
179         assertEquals("", CharSetUtils.keep("", new String JavaDoc[] {null}));
180         assertEquals("", CharSetUtils.keep("", new String JavaDoc[] {"a-e"}));
181         
182         assertEquals("", CharSetUtils.keep("hello", (String JavaDoc[]) null));
183         assertEquals("", CharSetUtils.keep("hello", new String JavaDoc[0]));
184         assertEquals("", CharSetUtils.keep("hello", new String JavaDoc[] {null}));
185         assertEquals("e", CharSetUtils.keep("hello", new String JavaDoc[] {"a-e"}));
186         
187         assertEquals("e", CharSetUtils.keep("hello", new String JavaDoc[] { "a-e" }));
188         assertEquals("ell", CharSetUtils.keep("hello", new String JavaDoc[] { "el" }));
189         assertEquals("hello", CharSetUtils.keep("hello", new String JavaDoc[] { "elho" }));
190         assertEquals("hello", CharSetUtils.keep("hello", new String JavaDoc[] { "a-z" }));
191         assertEquals("----", CharSetUtils.keep("----", new String JavaDoc[] { "-" }));
192         assertEquals("ll", CharSetUtils.keep("hello", new String JavaDoc[] { "l" }));
193     }
194
195     //-----------------------------------------------------------------------
196
public void testDelete_StringString() {
197         assertEquals(null, CharSetUtils.delete(null, (String JavaDoc) null));
198         assertEquals(null, CharSetUtils.delete(null, ""));
199         
200         assertEquals("", CharSetUtils.delete("", (String JavaDoc) null));
201         assertEquals("", CharSetUtils.delete("", ""));
202         assertEquals("", CharSetUtils.delete("", "a-e"));
203         
204         assertEquals("hello", CharSetUtils.delete("hello", (String JavaDoc) null));
205         assertEquals("hello", CharSetUtils.delete("hello", ""));
206         assertEquals("hllo", CharSetUtils.delete("hello", "a-e"));
207         assertEquals("he", CharSetUtils.delete("hello", "l-p"));
208         assertEquals("hello", CharSetUtils.delete("hello", "z"));
209     }
210     
211     public void testDelete_StringStringarray() {
212         assertEquals(null, CharSetUtils.delete(null, (String JavaDoc[]) null));
213         assertEquals(null, CharSetUtils.delete(null, new String JavaDoc[0]));
214         assertEquals(null, CharSetUtils.delete(null, new String JavaDoc[] {null}));
215         assertEquals(null, CharSetUtils.delete(null, new String JavaDoc[] {"el"}));
216         
217         assertEquals("", CharSetUtils.delete("", (String JavaDoc[]) null));
218         assertEquals("", CharSetUtils.delete("", new String JavaDoc[0]));
219         assertEquals("", CharSetUtils.delete("", new String JavaDoc[] {null}));
220         assertEquals("", CharSetUtils.delete("", new String JavaDoc[] {"a-e"}));
221         
222         assertEquals("hello", CharSetUtils.delete("hello", (String JavaDoc[]) null));
223         assertEquals("hello", CharSetUtils.delete("hello", new String JavaDoc[0]));
224         assertEquals("hello", CharSetUtils.delete("hello", new String JavaDoc[] {null}));
225         assertEquals("hello", CharSetUtils.delete("hello", new String JavaDoc[] {"xyz"}));
226
227         assertEquals("ho", CharSetUtils.delete("hello", new String JavaDoc[] { "el" }));
228         assertEquals("", CharSetUtils.delete("hello", new String JavaDoc[] { "elho" }));
229         assertEquals("hello", CharSetUtils.delete("hello", new String JavaDoc[] { "" }));
230         assertEquals("hello", CharSetUtils.delete("hello", ""));
231         assertEquals("", CharSetUtils.delete("hello", new String JavaDoc[] { "a-z" }));
232         assertEquals("", CharSetUtils.delete("----", new String JavaDoc[] { "-" }));
233         assertEquals("heo", CharSetUtils.delete("hello", new String JavaDoc[] { "l" }));
234     }
235     
236     
237     public void testTranslate() {
238         assertEquals(null, CharSetUtils.translate(null, null, null));
239         assertEquals("", CharSetUtils.translate("", "a", "b"));
240         assertEquals("jelly", CharSetUtils.translate("hello", "ho", "jy"));
241         assertEquals("jellj", CharSetUtils.translate("hello", "ho", "j"));
242         assertEquals("jelly", CharSetUtils.translate("hello", "ho", "jyx"));
243         assertEquals("\rhello\r", CharSetUtils.translate("\nhello\n", "\n", "\r"));
244         assertEquals("hello", CharSetUtils.translate("hello", "", "x"));
245         assertEquals("hello", CharSetUtils.translate("hello", "", ""));
246         assertEquals("hello", CharSetUtils.translate("hello", "", ""));
247         // From http://issues.apache.org/bugzilla/show_bug.cgi?id=25454
248
assertEquals("q651.506bera", CharSetUtils.translate("d216.102oren", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789",
249                 "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM567891234"));
250     }
251
252     public void testTranslateNullPointerException() {
253         try {
254             CharSetUtils.translate("hello", null, null);
255             fail("Expecting NullPointerException");
256         } catch (NullPointerException JavaDoc ex) {
257         }
258         try {
259             CharSetUtils.translate("hello", "h", null);
260             fail("Expecting NullPointerException");
261         } catch (NullPointerException JavaDoc ex) {
262         }
263         try {
264             CharSetUtils.translate("hello", null, "a");
265             fail("Expecting NullPointerException");
266         } catch (NullPointerException JavaDoc ex) {
267         }
268         try {
269             CharSetUtils.translate("hello", "h", "");
270             fail("Expecting ArrayIndexOutOfBoundsException");
271         } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
272         }
273     }
274          
275     
276 }
277
Popular Tags