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 junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: /**
025: * Unit tests {@link org.apache.commons.lang.StringUtils} - Trim/Empty methods
026: *
027: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
028: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
029: * @version $Id: StringUtilsTrimEmptyTest.java 437554 2006-08-28 06:21:41Z bayard $
030: */
031: public class StringUtilsTrimEmptyTest extends TestCase {
032: private static final String FOO = "foo";
033:
034: public StringUtilsTrimEmptyTest(String name) {
035: super (name);
036: }
037:
038: public static void main(String[] args) {
039: TestRunner.run(suite());
040: }
041:
042: public static Test suite() {
043: TestSuite suite = new TestSuite(StringUtilsTrimEmptyTest.class);
044: suite.setName("StringUtilsTrimEmpty Tests");
045: return suite;
046: }
047:
048: protected void setUp() throws Exception {
049: super .setUp();
050: }
051:
052: protected void tearDown() throws Exception {
053: super .tearDown();
054: }
055:
056: //-----------------------------------------------------------------------
057: public void testIsEmpty() {
058: assertEquals(true, StringUtils.isEmpty(null));
059: assertEquals(true, StringUtils.isEmpty(""));
060: assertEquals(false, StringUtils.isEmpty(" "));
061: assertEquals(false, StringUtils.isEmpty("foo"));
062: assertEquals(false, StringUtils.isEmpty(" foo "));
063: }
064:
065: public void testIsNotEmpty() {
066: assertEquals(false, StringUtils.isNotEmpty(null));
067: assertEquals(false, StringUtils.isNotEmpty(""));
068: assertEquals(true, StringUtils.isNotEmpty(" "));
069: assertEquals(true, StringUtils.isNotEmpty("foo"));
070: assertEquals(true, StringUtils.isNotEmpty(" foo "));
071: }
072:
073: public void testIsBlank() {
074: assertEquals(true, StringUtils.isBlank(null));
075: assertEquals(true, StringUtils.isBlank(""));
076: assertEquals(true, StringUtils
077: .isBlank(StringUtilsTest.WHITESPACE));
078: assertEquals(false, StringUtils.isBlank("foo"));
079: assertEquals(false, StringUtils.isBlank(" foo "));
080: }
081:
082: public void testIsNotBlank() {
083: assertEquals(false, StringUtils.isNotBlank(null));
084: assertEquals(false, StringUtils.isNotBlank(""));
085: assertEquals(false, StringUtils
086: .isNotBlank(StringUtilsTest.WHITESPACE));
087: assertEquals(true, StringUtils.isNotBlank("foo"));
088: assertEquals(true, StringUtils.isNotBlank(" foo "));
089: }
090:
091: //-----------------------------------------------------------------------
092: public void testClean() {
093: assertEquals(FOO, StringUtils.clean(FOO + " "));
094: assertEquals(FOO, StringUtils.clean(" " + FOO + " "));
095: assertEquals(FOO, StringUtils.clean(" " + FOO));
096: assertEquals(FOO, StringUtils.clean(FOO + ""));
097: assertEquals("", StringUtils.clean(" \t\r\n\b "));
098: assertEquals("", StringUtils.clean(""));
099: assertEquals("", StringUtils.clean(null));
100: }
101:
102: public void testTrim() {
103: assertEquals(FOO, StringUtils.trim(FOO + " "));
104: assertEquals(FOO, StringUtils.trim(" " + FOO + " "));
105: assertEquals(FOO, StringUtils.trim(" " + FOO));
106: assertEquals(FOO, StringUtils.trim(FOO + ""));
107: assertEquals("", StringUtils.trim(" \t\r\n\b "));
108: assertEquals("", StringUtils.trim(StringUtilsTest.TRIMMABLE));
109: assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils
110: .trim(StringUtilsTest.NON_TRIMMABLE));
111: assertEquals("", StringUtils.trim(""));
112: assertEquals(null, StringUtils.trim(null));
113: }
114:
115: public void testTrimToNull() {
116: assertEquals(FOO, StringUtils.trimToNull(FOO + " "));
117: assertEquals(FOO, StringUtils.trimToNull(" " + FOO + " "));
118: assertEquals(FOO, StringUtils.trimToNull(" " + FOO));
119: assertEquals(FOO, StringUtils.trimToNull(FOO + ""));
120: assertEquals(null, StringUtils.trimToNull(" \t\r\n\b "));
121: assertEquals(null, StringUtils
122: .trimToNull(StringUtilsTest.TRIMMABLE));
123: assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils
124: .trimToNull(StringUtilsTest.NON_TRIMMABLE));
125: assertEquals(null, StringUtils.trimToNull(""));
126: assertEquals(null, StringUtils.trimToNull(null));
127: }
128:
129: public void testTrimToEmpty() {
130: assertEquals(FOO, StringUtils.trimToEmpty(FOO + " "));
131: assertEquals(FOO, StringUtils.trimToEmpty(" " + FOO + " "));
132: assertEquals(FOO, StringUtils.trimToEmpty(" " + FOO));
133: assertEquals(FOO, StringUtils.trimToEmpty(FOO + ""));
134: assertEquals("", StringUtils.trimToEmpty(" \t\r\n\b "));
135: assertEquals("", StringUtils
136: .trimToEmpty(StringUtilsTest.TRIMMABLE));
137: assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils
138: .trimToEmpty(StringUtilsTest.NON_TRIMMABLE));
139: assertEquals("", StringUtils.trimToEmpty(""));
140: assertEquals("", StringUtils.trimToEmpty(null));
141: }
142:
143: //-----------------------------------------------------------------------
144: public void testStrip_String() {
145: assertEquals(null, StringUtils.strip(null));
146: assertEquals("", StringUtils.strip(""));
147: assertEquals("", StringUtils.strip(" "));
148: assertEquals("abc", StringUtils.strip(" abc "));
149: assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils
150: .strip(StringUtilsTest.WHITESPACE
151: + StringUtilsTest.NON_WHITESPACE
152: + StringUtilsTest.WHITESPACE));
153: }
154:
155: public void testStripToNull_String() {
156: assertEquals(null, StringUtils.stripToNull(null));
157: assertEquals(null, StringUtils.stripToNull(""));
158: assertEquals(null, StringUtils.stripToNull(" "));
159: assertEquals(null, StringUtils
160: .stripToNull(StringUtilsTest.WHITESPACE));
161: assertEquals("ab c", StringUtils.stripToNull(" ab c "));
162: assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils
163: .stripToNull(StringUtilsTest.WHITESPACE
164: + StringUtilsTest.NON_WHITESPACE
165: + StringUtilsTest.WHITESPACE));
166: }
167:
168: public void testStripToEmpty_String() {
169: assertEquals("", StringUtils.stripToEmpty(null));
170: assertEquals("", StringUtils.stripToEmpty(""));
171: assertEquals("", StringUtils.stripToEmpty(" "));
172: assertEquals("", StringUtils
173: .stripToEmpty(StringUtilsTest.WHITESPACE));
174: assertEquals("ab c", StringUtils.stripToEmpty(" ab c "));
175: assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils
176: .stripToEmpty(StringUtilsTest.WHITESPACE
177: + StringUtilsTest.NON_WHITESPACE
178: + StringUtilsTest.WHITESPACE));
179: }
180:
181: public void testStrip_StringString() {
182: // null strip
183: assertEquals(null, StringUtils.strip(null, null));
184: assertEquals("", StringUtils.strip("", null));
185: assertEquals("", StringUtils.strip(" ", null));
186: assertEquals("abc", StringUtils.strip(" abc ", null));
187: assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils.strip(
188: StringUtilsTest.WHITESPACE
189: + StringUtilsTest.NON_WHITESPACE
190: + StringUtilsTest.WHITESPACE, null));
191:
192: // "" strip
193: assertEquals(null, StringUtils.strip(null, ""));
194: assertEquals("", StringUtils.strip("", ""));
195: assertEquals(" ", StringUtils.strip(" ", ""));
196: assertEquals(" abc ", StringUtils.strip(" abc ", ""));
197: assertEquals(StringUtilsTest.WHITESPACE, StringUtils.strip(
198: StringUtilsTest.WHITESPACE, ""));
199:
200: // " " strip
201: assertEquals(null, StringUtils.strip(null, " "));
202: assertEquals("", StringUtils.strip("", " "));
203: assertEquals("", StringUtils.strip(" ", " "));
204: assertEquals("abc", StringUtils.strip(" abc ", " "));
205:
206: // "ab" strip
207: assertEquals(null, StringUtils.strip(null, "ab"));
208: assertEquals("", StringUtils.strip("", "ab"));
209: assertEquals(" ", StringUtils.strip(" ", "ab"));
210: assertEquals(" abc ", StringUtils.strip(" abc ", "ab"));
211: assertEquals("c", StringUtils.strip("abcabab", "ab"));
212: assertEquals(StringUtilsTest.WHITESPACE, StringUtils.strip(
213: StringUtilsTest.WHITESPACE, ""));
214: }
215:
216: public void testStripStart_StringString() {
217: // null stripStart
218: assertEquals(null, StringUtils.stripStart(null, null));
219: assertEquals("", StringUtils.stripStart("", null));
220: assertEquals("", StringUtils.stripStart(" ", null));
221: assertEquals("abc ", StringUtils.stripStart(" abc ", null));
222: assertEquals(StringUtilsTest.NON_WHITESPACE
223: + StringUtilsTest.WHITESPACE, StringUtils.stripStart(
224: StringUtilsTest.WHITESPACE
225: + StringUtilsTest.NON_WHITESPACE
226: + StringUtilsTest.WHITESPACE, null));
227:
228: // "" stripStart
229: assertEquals(null, StringUtils.stripStart(null, ""));
230: assertEquals("", StringUtils.stripStart("", ""));
231: assertEquals(" ", StringUtils.stripStart(" ", ""));
232: assertEquals(" abc ", StringUtils.stripStart(" abc ", ""));
233: assertEquals(StringUtilsTest.WHITESPACE, StringUtils
234: .stripStart(StringUtilsTest.WHITESPACE, ""));
235:
236: // " " stripStart
237: assertEquals(null, StringUtils.stripStart(null, " "));
238: assertEquals("", StringUtils.stripStart("", " "));
239: assertEquals("", StringUtils.stripStart(" ", " "));
240: assertEquals("abc ", StringUtils.stripStart(" abc ", " "));
241:
242: // "ab" stripStart
243: assertEquals(null, StringUtils.stripStart(null, "ab"));
244: assertEquals("", StringUtils.stripStart("", "ab"));
245: assertEquals(" ", StringUtils.stripStart(" ",
246: "ab"));
247: assertEquals(" abc ", StringUtils.stripStart(" abc ", "ab"));
248: assertEquals("cabab", StringUtils.stripStart("abcabab", "ab"));
249: assertEquals(StringUtilsTest.WHITESPACE, StringUtils
250: .stripStart(StringUtilsTest.WHITESPACE, ""));
251: }
252:
253: public void testStripEnd_StringString() {
254: // null stripEnd
255: assertEquals(null, StringUtils.stripEnd(null, null));
256: assertEquals("", StringUtils.stripEnd("", null));
257: assertEquals("", StringUtils.stripEnd(" ", null));
258: assertEquals(" abc", StringUtils.stripEnd(" abc ", null));
259: assertEquals(StringUtilsTest.WHITESPACE
260: + StringUtilsTest.NON_WHITESPACE, StringUtils.stripEnd(
261: StringUtilsTest.WHITESPACE
262: + StringUtilsTest.NON_WHITESPACE
263: + StringUtilsTest.WHITESPACE, null));
264:
265: // "" stripEnd
266: assertEquals(null, StringUtils.stripEnd(null, ""));
267: assertEquals("", StringUtils.stripEnd("", ""));
268: assertEquals(" ", StringUtils.stripEnd(" ", ""));
269: assertEquals(" abc ", StringUtils.stripEnd(" abc ", ""));
270: assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripEnd(
271: StringUtilsTest.WHITESPACE, ""));
272:
273: // " " stripEnd
274: assertEquals(null, StringUtils.stripEnd(null, " "));
275: assertEquals("", StringUtils.stripEnd("", " "));
276: assertEquals("", StringUtils.stripEnd(" ", " "));
277: assertEquals(" abc", StringUtils.stripEnd(" abc ", " "));
278:
279: // "ab" stripEnd
280: assertEquals(null, StringUtils.stripEnd(null, "ab"));
281: assertEquals("", StringUtils.stripEnd("", "ab"));
282: assertEquals(" ", StringUtils.stripEnd(" ", "ab"));
283: assertEquals(" abc ", StringUtils.stripEnd(" abc ", "ab"));
284: assertEquals("abc", StringUtils.stripEnd("abcabab", "ab"));
285: assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripEnd(
286: StringUtilsTest.WHITESPACE, ""));
287: }
288:
289: public void testStripAll() {
290: // test stripAll method, merely an array version of the above strip
291: String[] empty = new String[0];
292: String[] fooSpace = new String[] { " " + FOO + " ",
293: " " + FOO, FOO + " " };
294: String[] fooDots = new String[] { ".." + FOO + "..",
295: ".." + FOO, FOO + ".." };
296: String[] foo = new String[] { FOO, FOO, FOO };
297:
298: assertEquals(null, StringUtils.stripAll(null));
299: assertArrayEquals(empty, StringUtils.stripAll(empty));
300: assertArrayEquals(foo, StringUtils.stripAll(fooSpace));
301:
302: assertEquals(null, StringUtils.stripAll(null, null));
303: assertArrayEquals(foo, StringUtils.stripAll(fooSpace, null));
304: assertArrayEquals(foo, StringUtils.stripAll(fooDots, "."));
305: }
306:
307: private void assertArrayEquals(Object[] o1, Object[] o2) {
308: if (o1 == null) {
309: assertEquals(o1, o2);
310: return;
311: }
312: assertEquals("Length not equal. ", o1.length, o2.length);
313: int sz = o1.length;
314: for (int i = 0; i < sz; i++) {
315: if (o1[i] instanceof Object[]) {
316: // do an assert equals on type....
317: assertArrayEquals((Object[]) o1[i], (Object[]) o2[i]);
318: } else {
319: assertEquals(o1[i], o2[i]);
320: }
321: }
322: }
323:
324: }
|