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: import junit.textui.TestRunner;
026:
027: /**
028: * Unit tests {@link org.apache.commons.lang.CharSetUtils}.
029: *
030: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
031: * @author Stephen Colebourne
032: * @author Gary D. Gregory
033: * @version $Id: CharSetUtilsTest.java 471626 2006-11-06 04:02:09Z bayard $
034: */
035: public class CharSetUtilsTest extends TestCase {
036:
037: public CharSetUtilsTest(String name) {
038: super (name);
039: }
040:
041: public static void main(String[] args) {
042: TestRunner.run(suite());
043: }
044:
045: public static Test suite() {
046: TestSuite suite = new TestSuite(CharSetUtilsTest.class);
047: suite.setName("CharSetUtils Tests");
048: return suite;
049: }
050:
051: protected void setUp() throws Exception {
052: super .setUp();
053: }
054:
055: protected void tearDown() throws Exception {
056: super .tearDown();
057: }
058:
059: //-----------------------------------------------------------------------
060: public void testConstructor() {
061: assertNotNull(new CharSetUtils());
062: Constructor[] cons = CharSetUtils.class
063: .getDeclaredConstructors();
064: assertEquals(1, cons.length);
065: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
066: assertEquals(true, Modifier.isPublic(CharSetUtils.class
067: .getModifiers()));
068: assertEquals(false, Modifier.isFinal(CharSetUtils.class
069: .getModifiers()));
070: }
071:
072: //-----------------------------------------------------------------------
073: public void testEvaluateSet_Stringarray() {
074: assertEquals(null, CharSetUtils.evaluateSet((String[]) null));
075: assertEquals("[]", CharSetUtils.evaluateSet(new String[0])
076: .toString());
077: assertEquals("[]", CharSetUtils.evaluateSet(
078: new String[] { null }).toString());
079: assertEquals("[a-e]", CharSetUtils.evaluateSet(
080: new String[] { "a-e" }).toString());
081: }
082:
083: //-----------------------------------------------------------------------
084: public void testSqueeze_StringString() {
085: assertEquals(null, CharSetUtils.squeeze(null, (String) null));
086: assertEquals(null, CharSetUtils.squeeze(null, ""));
087:
088: assertEquals("", CharSetUtils.squeeze("", (String) null));
089: assertEquals("", CharSetUtils.squeeze("", ""));
090: assertEquals("", CharSetUtils.squeeze("", "a-e"));
091:
092: assertEquals("hello", CharSetUtils.squeeze("hello",
093: (String) null));
094: assertEquals("hello", CharSetUtils.squeeze("hello", ""));
095: assertEquals("hello", CharSetUtils.squeeze("hello", "a-e"));
096: assertEquals("helo", CharSetUtils.squeeze("hello", "l-p"));
097: assertEquals("heloo", CharSetUtils.squeeze("helloo", "l"));
098: assertEquals("hello", CharSetUtils.squeeze("helloo", "^l"));
099: }
100:
101: public void testSqueeze_StringStringarray() {
102: assertEquals(null, CharSetUtils.squeeze(null, (String[]) null));
103: assertEquals(null, CharSetUtils.squeeze(null, new String[0]));
104: assertEquals(null, CharSetUtils.squeeze(null,
105: new String[] { null }));
106: assertEquals(null, CharSetUtils.squeeze(null,
107: new String[] { "el" }));
108:
109: assertEquals("", CharSetUtils.squeeze("", (String[]) null));
110: assertEquals("", CharSetUtils.squeeze("", new String[0]));
111: assertEquals("", CharSetUtils
112: .squeeze("", new String[] { null }));
113: assertEquals("", CharSetUtils.squeeze("",
114: new String[] { "a-e" }));
115:
116: assertEquals("hello", CharSetUtils.squeeze("hello",
117: (String[]) null));
118: assertEquals("hello", CharSetUtils.squeeze("hello",
119: new String[0]));
120: assertEquals("hello", CharSetUtils.squeeze("hello",
121: new String[] { null }));
122: assertEquals("hello", CharSetUtils.squeeze("hello",
123: new String[] { "a-e" }));
124:
125: assertEquals("helo", CharSetUtils.squeeze("hello",
126: new String[] { "el" }));
127: assertEquals("hello", CharSetUtils.squeeze("hello",
128: new String[] { "e" }));
129: assertEquals("fofof", CharSetUtils.squeeze("fooffooff",
130: new String[] { "of" }));
131: assertEquals("fof", CharSetUtils.squeeze("fooooff",
132: new String[] { "fo" }));
133: }
134:
135: //-----------------------------------------------------------------------
136: public void testCount_StringString() {
137: assertEquals(0, CharSetUtils.count(null, (String) null));
138: assertEquals(0, CharSetUtils.count(null, ""));
139:
140: assertEquals(0, CharSetUtils.count("", (String) null));
141: assertEquals(0, CharSetUtils.count("", ""));
142: assertEquals(0, CharSetUtils.count("", "a-e"));
143:
144: assertEquals(0, CharSetUtils.count("hello", (String) null));
145: assertEquals(0, CharSetUtils.count("hello", ""));
146: assertEquals(1, CharSetUtils.count("hello", "a-e"));
147: assertEquals(3, CharSetUtils.count("hello", "l-p"));
148: }
149:
150: public void testCount_StringStringarray() {
151: assertEquals(0, CharSetUtils.count(null, (String[]) null));
152: assertEquals(0, CharSetUtils.count(null, new String[0]));
153: assertEquals(0, CharSetUtils.count(null, new String[] { null }));
154: assertEquals(0, CharSetUtils
155: .count(null, new String[] { "a-e" }));
156:
157: assertEquals(0, CharSetUtils.count("", (String[]) null));
158: assertEquals(0, CharSetUtils.count("", new String[0]));
159: assertEquals(0, CharSetUtils.count("", new String[] { null }));
160: assertEquals(0, CharSetUtils.count("", new String[] { "a-e" }));
161:
162: assertEquals(0, CharSetUtils.count("hello", (String[]) null));
163: assertEquals(0, CharSetUtils.count("hello", new String[0]));
164: assertEquals(0, CharSetUtils.count("hello",
165: new String[] { null }));
166: assertEquals(1, CharSetUtils.count("hello",
167: new String[] { "a-e" }));
168:
169: assertEquals(3, CharSetUtils.count("hello",
170: new String[] { "el" }));
171: assertEquals(0, CharSetUtils.count("hello",
172: new String[] { "x" }));
173: assertEquals(2, CharSetUtils.count("hello",
174: new String[] { "e-i" }));
175: assertEquals(5, CharSetUtils.count("hello",
176: new String[] { "a-z" }));
177: assertEquals(0, CharSetUtils
178: .count("hello", new String[] { "" }));
179: }
180:
181: //-----------------------------------------------------------------------
182: public void testKeep_StringString() {
183: assertEquals(null, CharSetUtils.keep(null, (String) null));
184: assertEquals(null, CharSetUtils.keep(null, ""));
185:
186: assertEquals("", CharSetUtils.keep("", (String) null));
187: assertEquals("", CharSetUtils.keep("", ""));
188: assertEquals("", CharSetUtils.keep("", "a-e"));
189:
190: assertEquals("", CharSetUtils.keep("hello", (String) null));
191: assertEquals("", CharSetUtils.keep("hello", ""));
192: assertEquals("", CharSetUtils.keep("hello", "xyz"));
193: assertEquals("hello", CharSetUtils.keep("hello", "a-z"));
194: assertEquals("hello", CharSetUtils.keep("hello", "oleh"));
195: assertEquals("ell", CharSetUtils.keep("hello", "el"));
196: }
197:
198: public void testKeep_StringStringarray() {
199: assertEquals(null, CharSetUtils.keep(null, (String[]) null));
200: assertEquals(null, CharSetUtils.keep(null, new String[0]));
201: assertEquals(null, CharSetUtils.keep(null,
202: new String[] { null }));
203: assertEquals(null, CharSetUtils.keep(null,
204: new String[] { "a-e" }));
205:
206: assertEquals("", CharSetUtils.keep("", (String[]) null));
207: assertEquals("", CharSetUtils.keep("", new String[0]));
208: assertEquals("", CharSetUtils.keep("", new String[] { null }));
209: assertEquals("", CharSetUtils.keep("", new String[] { "a-e" }));
210:
211: assertEquals("", CharSetUtils.keep("hello", (String[]) null));
212: assertEquals("", CharSetUtils.keep("hello", new String[0]));
213: assertEquals("", CharSetUtils.keep("hello",
214: new String[] { null }));
215: assertEquals("e", CharSetUtils.keep("hello",
216: new String[] { "a-e" }));
217:
218: assertEquals("e", CharSetUtils.keep("hello",
219: new String[] { "a-e" }));
220: assertEquals("ell", CharSetUtils.keep("hello",
221: new String[] { "el" }));
222: assertEquals("hello", CharSetUtils.keep("hello",
223: new String[] { "elho" }));
224: assertEquals("hello", CharSetUtils.keep("hello",
225: new String[] { "a-z" }));
226: assertEquals("----", CharSetUtils.keep("----",
227: new String[] { "-" }));
228: assertEquals("ll", CharSetUtils.keep("hello",
229: new String[] { "l" }));
230: }
231:
232: //-----------------------------------------------------------------------
233: public void testDelete_StringString() {
234: assertEquals(null, CharSetUtils.delete(null, (String) null));
235: assertEquals(null, CharSetUtils.delete(null, ""));
236:
237: assertEquals("", CharSetUtils.delete("", (String) null));
238: assertEquals("", CharSetUtils.delete("", ""));
239: assertEquals("", CharSetUtils.delete("", "a-e"));
240:
241: assertEquals("hello", CharSetUtils.delete("hello",
242: (String) null));
243: assertEquals("hello", CharSetUtils.delete("hello", ""));
244: assertEquals("hllo", CharSetUtils.delete("hello", "a-e"));
245: assertEquals("he", CharSetUtils.delete("hello", "l-p"));
246: assertEquals("hello", CharSetUtils.delete("hello", "z"));
247: }
248:
249: public void testDelete_StringStringarray() {
250: assertEquals(null, CharSetUtils.delete(null, (String[]) null));
251: assertEquals(null, CharSetUtils.delete(null, new String[0]));
252: assertEquals(null, CharSetUtils.delete(null,
253: new String[] { null }));
254: assertEquals(null, CharSetUtils.delete(null,
255: new String[] { "el" }));
256:
257: assertEquals("", CharSetUtils.delete("", (String[]) null));
258: assertEquals("", CharSetUtils.delete("", new String[0]));
259: assertEquals("", CharSetUtils.delete("", new String[] { null }));
260: assertEquals("", CharSetUtils
261: .delete("", new String[] { "a-e" }));
262:
263: assertEquals("hello", CharSetUtils.delete("hello",
264: (String[]) null));
265: assertEquals("hello", CharSetUtils.delete("hello",
266: new String[0]));
267: assertEquals("hello", CharSetUtils.delete("hello",
268: new String[] { null }));
269: assertEquals("hello", CharSetUtils.delete("hello",
270: new String[] { "xyz" }));
271:
272: assertEquals("ho", CharSetUtils.delete("hello",
273: new String[] { "el" }));
274: assertEquals("", CharSetUtils.delete("hello",
275: new String[] { "elho" }));
276: assertEquals("hello", CharSetUtils.delete("hello",
277: new String[] { "" }));
278: assertEquals("hello", CharSetUtils.delete("hello", ""));
279: assertEquals("", CharSetUtils.delete("hello",
280: new String[] { "a-z" }));
281: assertEquals("", CharSetUtils.delete("----",
282: new String[] { "-" }));
283: assertEquals("heo", CharSetUtils.delete("hello",
284: new String[] { "l" }));
285: }
286:
287: public void testTranslate() {
288: assertEquals(null, CharSetUtils.translate(null, null, null));
289: assertEquals("", CharSetUtils.translate("", "a", "b"));
290: assertEquals("jelly", CharSetUtils.translate("hello", "ho",
291: "jy"));
292: assertEquals("jellj", CharSetUtils
293: .translate("hello", "ho", "j"));
294: assertEquals("jelly", CharSetUtils.translate("hello", "ho",
295: "jyx"));
296: assertEquals("\rhello\r", CharSetUtils.translate("\nhello\n",
297: "\n", "\r"));
298: assertEquals("hello", CharSetUtils.translate("hello", "", "x"));
299: assertEquals("hello", CharSetUtils.translate("hello", "", ""));
300: assertEquals("hello", CharSetUtils.translate("hello", "", ""));
301: // From http://issues.apache.org/bugzilla/show_bug.cgi?id=25454
302: assertEquals(
303: "q651.506bera",
304: CharSetUtils
305: .translate(
306: "d216.102oren",
307: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789",
308: "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM567891234"));
309: }
310:
311: public void testTranslateNullPointerException() {
312: try {
313: CharSetUtils.translate("hello", null, null);
314: fail("Expecting NullPointerException");
315: } catch (NullPointerException ex) {
316: }
317: try {
318: CharSetUtils.translate("hello", "h", null);
319: fail("Expecting NullPointerException");
320: } catch (NullPointerException ex) {
321: }
322: try {
323: CharSetUtils.translate("hello", null, "a");
324: fail("Expecting NullPointerException");
325: } catch (NullPointerException ex) {
326: }
327: try {
328: CharSetUtils.translate("hello", "h", "");
329: fail("Expecting ArrayIndexOutOfBoundsException");
330: } catch (ArrayIndexOutOfBoundsException ex) {
331: }
332: }
333:
334: }
|