0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.commons.lang;
0018:
0019: import java.lang.reflect.Constructor;
0020: import java.lang.reflect.Modifier;
0021: import java.util.Arrays;
0022: import java.util.Iterator;
0023: import java.util.Collection;
0024: import java.util.Collections;
0025:
0026: import junit.framework.Test;
0027: import junit.framework.TestCase;
0028: import junit.framework.TestSuite;
0029: import junit.textui.TestRunner;
0030:
0031: /**
0032: * Unit tests {@link org.apache.commons.lang.StringUtils}.
0033: *
0034: * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
0035: * @author Stephen Colebourne
0036: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
0037: * @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
0038: * @author Holger Krauth
0039: * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
0040: * @author Phil Steitz
0041: * @author Gary D. Gregory
0042: * @author Al Chou
0043: * @version $Id: StringUtilsTest.java 471626 2006-11-06 04:02:09Z bayard $
0044: */
0045: public class StringUtilsTest extends TestCase {
0046:
0047: static final String WHITESPACE;
0048: static final String NON_WHITESPACE;
0049: static final String TRIMMABLE;
0050: static final String NON_TRIMMABLE;
0051: static {
0052: String ws = "";
0053: String nws = "";
0054: String tr = "";
0055: String ntr = "";
0056: for (int i = 0; i < Character.MAX_VALUE; i++) {
0057: if (Character.isWhitespace((char) i)) {
0058: ws += String.valueOf((char) i);
0059: if (i > 32) {
0060: ntr += String.valueOf((char) i);
0061: }
0062: } else if (i < 40) {
0063: nws += String.valueOf((char) i);
0064: }
0065: }
0066: for (int i = 0; i <= 32; i++) {
0067: tr += String.valueOf((char) i);
0068: }
0069: WHITESPACE = ws;
0070: NON_WHITESPACE = nws;
0071: TRIMMABLE = tr;
0072: NON_TRIMMABLE = ntr;
0073: }
0074:
0075: private static final String[] ARRAY_LIST = { "foo", "bar", "baz" };
0076: private static final String[] EMPTY_ARRAY_LIST = {};
0077: private static final String[] NULL_ARRAY_LIST = { null };
0078: private static final String[] MIXED_ARRAY_LIST = { null, "", "foo" };
0079: private static final Object[] MIXED_TYPE_LIST = {
0080: new String("foo"), new Long(2) };
0081:
0082: private static final String SEPARATOR = ",";
0083: private static final char SEPARATOR_CHAR = ';';
0084:
0085: private static final String TEXT_LIST = "foo,bar,baz";
0086: private static final String TEXT_LIST_CHAR = "foo;bar;baz";
0087: private static final String TEXT_LIST_NOSEP = "foobarbaz";
0088:
0089: private static final String FOO_UNCAP = "foo";
0090: private static final String FOO_CAP = "Foo";
0091:
0092: private static final String SENTENCE_UNCAP = "foo bar baz";
0093: private static final String SENTENCE_CAP = "Foo Bar Baz";
0094:
0095: public StringUtilsTest(String name) {
0096: super (name);
0097: }
0098:
0099: public static void main(String[] args) {
0100: TestRunner.run(suite());
0101: }
0102:
0103: public static Test suite() {
0104: TestSuite suite = new TestSuite(StringUtilsTest.class);
0105: suite.setName("StringUtilsTest Tests");
0106: return suite;
0107: }
0108:
0109: protected void setUp() throws Exception {
0110: super .setUp();
0111: }
0112:
0113: protected void tearDown() throws Exception {
0114: super .tearDown();
0115: }
0116:
0117: //-----------------------------------------------------------------------
0118: public void testConstructor() {
0119: assertNotNull(new StringUtils());
0120: Constructor[] cons = StringUtils.class
0121: .getDeclaredConstructors();
0122: assertEquals(1, cons.length);
0123: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
0124: assertEquals(true, Modifier.isPublic(StringUtils.class
0125: .getModifiers()));
0126: assertEquals(false, Modifier.isFinal(StringUtils.class
0127: .getModifiers()));
0128: }
0129:
0130: //-----------------------------------------------------------------------
0131: public void testCaseFunctions() {
0132: assertEquals(null, StringUtils.upperCase(null));
0133: assertEquals(null, StringUtils.lowerCase(null));
0134: assertEquals(null, StringUtils.capitalize(null));
0135: assertEquals(null, StringUtils.uncapitalise(null));
0136: assertEquals(null, StringUtils.uncapitalize(null));
0137:
0138: assertEquals("capitalise(String) failed", FOO_CAP, StringUtils
0139: .capitalise(FOO_UNCAP));
0140: assertEquals("capitalise(empty-string) failed", "", StringUtils
0141: .capitalise(""));
0142: assertEquals("capitalise(single-char-string) failed", "X",
0143: StringUtils.capitalise("x"));
0144: assertEquals("capitalize(String) failed", FOO_CAP, StringUtils
0145: .capitalize(FOO_UNCAP));
0146: assertEquals("capitalize(empty-string) failed", "", StringUtils
0147: .capitalize(""));
0148: assertEquals("capitalize(single-char-string) failed", "X",
0149: StringUtils.capitalize("x"));
0150: assertEquals("uncapitalise(String) failed", FOO_UNCAP,
0151: StringUtils.uncapitalise(FOO_CAP));
0152: assertEquals("uncapitalise(empty-string) failed", "",
0153: StringUtils.uncapitalise(""));
0154: assertEquals("uncapitalise(single-char-string) failed", "x",
0155: StringUtils.uncapitalise("X"));
0156: assertEquals("uncapitalize(String) failed", FOO_UNCAP,
0157: StringUtils.uncapitalize(FOO_CAP));
0158: assertEquals("uncapitalize(empty-string) failed", "",
0159: StringUtils.uncapitalize(""));
0160: assertEquals("uncapitalize(single-char-string) failed", "x",
0161: StringUtils.uncapitalize("X"));
0162:
0163: // reflection type of tests: Sentences.
0164: assertEquals("uncapitalise(capitalise(String)) failed",
0165: SENTENCE_UNCAP, StringUtils.uncapitalise(StringUtils
0166: .capitalise(SENTENCE_UNCAP)));
0167: assertEquals("capitalise(uncapitalise(String)) failed",
0168: SENTENCE_CAP, StringUtils.capitalise(StringUtils
0169: .uncapitalise(SENTENCE_CAP)));
0170: assertEquals("uncapitalize(capitalize(String)) failed",
0171: SENTENCE_UNCAP, StringUtils.uncapitalize(StringUtils
0172: .capitalize(SENTENCE_UNCAP)));
0173: assertEquals("capitalize(uncapitalize(String)) failed",
0174: SENTENCE_CAP, StringUtils.capitalize(StringUtils
0175: .uncapitalize(SENTENCE_CAP)));
0176:
0177: // reflection type of tests: One word.
0178: assertEquals("uncapitalise(capitalise(String)) failed",
0179: FOO_UNCAP, StringUtils.uncapitalise(StringUtils
0180: .capitalise(FOO_UNCAP)));
0181: assertEquals("capitalise(uncapitalise(String)) failed",
0182: FOO_CAP, StringUtils.capitalise(StringUtils
0183: .uncapitalise(FOO_CAP)));
0184: assertEquals("uncapitalize(capitalize(String)) failed",
0185: FOO_UNCAP, StringUtils.uncapitalize(StringUtils
0186: .capitalize(FOO_UNCAP)));
0187: assertEquals("capitalize(uncapitalize(String)) failed",
0188: FOO_CAP, StringUtils.capitalize(StringUtils
0189: .uncapitalize(FOO_CAP)));
0190:
0191: assertEquals("upperCase(String) failed", "FOO TEST THING",
0192: StringUtils.upperCase("fOo test THING"));
0193: assertEquals("upperCase(empty-string) failed", "", StringUtils
0194: .upperCase(""));
0195: assertEquals("lowerCase(String) failed", "foo test thing",
0196: StringUtils.lowerCase("fOo test THING"));
0197: assertEquals("lowerCase(empty-string) failed", "", StringUtils
0198: .lowerCase(""));
0199:
0200: }
0201:
0202: public void testSwapCase_String() {
0203: assertEquals(null, StringUtils.swapCase(null));
0204: assertEquals("", StringUtils.swapCase(""));
0205: assertEquals(" ", StringUtils.swapCase(" "));
0206:
0207: assertEquals("i", WordUtils.swapCase("I"));
0208: assertEquals("I", WordUtils.swapCase("i"));
0209: assertEquals("I AM HERE 123", StringUtils
0210: .swapCase("i am here 123"));
0211: assertEquals("i aM hERE 123", StringUtils
0212: .swapCase("I Am Here 123"));
0213: assertEquals("I AM here 123", StringUtils
0214: .swapCase("i am HERE 123"));
0215: assertEquals("i am here 123", StringUtils
0216: .swapCase("I AM HERE 123"));
0217:
0218: String test = "This String contains a TitleCase character: \u01C8";
0219: String expect = "tHIS sTRING CONTAINS A tITLEcASE CHARACTER: \u01C9";
0220: assertEquals(expect, WordUtils.swapCase(test));
0221: }
0222:
0223: //-----------------------------------------------------------------------
0224: public void testJoin_Objectarray() {
0225: assertEquals(null, StringUtils.join(null));
0226: assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST));
0227: assertEquals("", StringUtils.join(NULL_ARRAY_LIST));
0228: assertEquals("abc", StringUtils.join(new String[] { "a", "b",
0229: "c" }));
0230: assertEquals("a", StringUtils
0231: .join(new String[] { null, "a", "" }));
0232: assertEquals("foo", StringUtils.join(MIXED_ARRAY_LIST));
0233: assertEquals("foo2", StringUtils.join(MIXED_TYPE_LIST));
0234: }
0235:
0236: public void testJoin_ArrayChar() {
0237: assertEquals(null, StringUtils.join((Object[]) null, ','));
0238: assertEquals(TEXT_LIST_CHAR, StringUtils.join(ARRAY_LIST,
0239: SEPARATOR_CHAR));
0240: assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST,
0241: SEPARATOR_CHAR));
0242: assertEquals(";;foo", StringUtils.join(MIXED_ARRAY_LIST,
0243: SEPARATOR_CHAR));
0244: assertEquals("foo;2", StringUtils.join(MIXED_TYPE_LIST,
0245: SEPARATOR_CHAR));
0246:
0247: assertEquals("/", StringUtils.join(MIXED_ARRAY_LIST, '/', 0,
0248: MIXED_ARRAY_LIST.length - 1));
0249: assertEquals("foo", StringUtils
0250: .join(MIXED_TYPE_LIST, '/', 0, 1));
0251: assertEquals("foo/2", StringUtils.join(MIXED_TYPE_LIST, '/', 0,
0252: 2));
0253: assertEquals("2", StringUtils.join(MIXED_TYPE_LIST, '/', 1, 2));
0254: assertEquals("", StringUtils.join(MIXED_TYPE_LIST, '/', 2, 1));
0255: }
0256:
0257: public void testJoin_ArrayString() {
0258: assertEquals(null, StringUtils.join((Object[]) null, null));
0259: assertEquals(TEXT_LIST_NOSEP, StringUtils
0260: .join(ARRAY_LIST, null));
0261: assertEquals(TEXT_LIST_NOSEP, StringUtils.join(ARRAY_LIST, ""));
0262:
0263: assertEquals("", StringUtils.join(NULL_ARRAY_LIST, null));
0264:
0265: assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, null));
0266: assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, ""));
0267: assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR));
0268:
0269: assertEquals(TEXT_LIST, StringUtils.join(ARRAY_LIST, SEPARATOR));
0270: assertEquals(",,foo", StringUtils.join(MIXED_ARRAY_LIST,
0271: SEPARATOR));
0272: assertEquals("foo,2", StringUtils.join(MIXED_TYPE_LIST,
0273: SEPARATOR));
0274:
0275: assertEquals("/", StringUtils.join(MIXED_ARRAY_LIST, "/", 0,
0276: MIXED_ARRAY_LIST.length - 1));
0277: assertEquals("", StringUtils.join(MIXED_ARRAY_LIST, "", 0,
0278: MIXED_ARRAY_LIST.length - 1));
0279: assertEquals("foo", StringUtils
0280: .join(MIXED_TYPE_LIST, "/", 0, 1));
0281: assertEquals("foo/2", StringUtils.join(MIXED_TYPE_LIST, "/", 0,
0282: 2));
0283: assertEquals("2", StringUtils.join(MIXED_TYPE_LIST, "/", 1, 2));
0284: assertEquals("", StringUtils.join(MIXED_TYPE_LIST, "/", 2, 1));
0285: }
0286:
0287: public void testJoin_IteratorChar() {
0288: assertEquals(null, StringUtils.join((Iterator) null, ','));
0289: assertEquals(TEXT_LIST_CHAR, StringUtils.join(Arrays.asList(
0290: ARRAY_LIST).iterator(), SEPARATOR_CHAR));
0291: assertEquals("", StringUtils.join(Arrays
0292: .asList(NULL_ARRAY_LIST).iterator(), SEPARATOR_CHAR));
0293: assertEquals("", StringUtils.join(Arrays.asList(
0294: EMPTY_ARRAY_LIST).iterator(), SEPARATOR_CHAR));
0295: assertEquals("foo", StringUtils.join(Collections.singleton(
0296: "foo").iterator(), 'x'));
0297: }
0298:
0299: public void testJoin_IteratorString() {
0300: assertEquals(null, StringUtils.join((Iterator) null, null));
0301: assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays.asList(
0302: ARRAY_LIST).iterator(), null));
0303: assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays.asList(
0304: ARRAY_LIST).iterator(), ""));
0305: assertEquals("foo", StringUtils.join(Collections.singleton(
0306: "foo").iterator(), "x"));
0307: assertEquals("foo", StringUtils.join(Collections.singleton(
0308: "foo").iterator(), null));
0309:
0310: assertEquals("", StringUtils.join(Arrays
0311: .asList(NULL_ARRAY_LIST).iterator(), null));
0312:
0313: assertEquals("", StringUtils.join(Arrays.asList(
0314: EMPTY_ARRAY_LIST).iterator(), null));
0315: assertEquals("", StringUtils.join(Arrays.asList(
0316: EMPTY_ARRAY_LIST).iterator(), ""));
0317: assertEquals("", StringUtils.join(Arrays.asList(
0318: EMPTY_ARRAY_LIST).iterator(), SEPARATOR));
0319:
0320: assertEquals(TEXT_LIST, StringUtils.join(Arrays.asList(
0321: ARRAY_LIST).iterator(), SEPARATOR));
0322: }
0323:
0324: public void testJoin_CollectionChar() {
0325: assertEquals(null, StringUtils.join((Collection) null, ','));
0326: assertEquals(TEXT_LIST_CHAR, StringUtils.join(Arrays
0327: .asList(ARRAY_LIST), SEPARATOR_CHAR));
0328: assertEquals("", StringUtils.join(Arrays
0329: .asList(NULL_ARRAY_LIST), SEPARATOR_CHAR));
0330: assertEquals("", StringUtils.join(Arrays
0331: .asList(EMPTY_ARRAY_LIST), SEPARATOR_CHAR));
0332: assertEquals("foo", StringUtils.join(Collections
0333: .singleton("foo"), 'x'));
0334: }
0335:
0336: public void testJoin_CollectionString() {
0337: assertEquals(null, StringUtils.join((Collection) null, null));
0338: assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays
0339: .asList(ARRAY_LIST), null));
0340: assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays
0341: .asList(ARRAY_LIST), ""));
0342: assertEquals("foo", StringUtils.join(Collections
0343: .singleton("foo"), "x"));
0344: assertEquals("foo", StringUtils.join(Collections
0345: .singleton("foo"), null));
0346:
0347: assertEquals("", StringUtils.join(Arrays
0348: .asList(NULL_ARRAY_LIST), null));
0349:
0350: assertEquals("", StringUtils.join(Arrays
0351: .asList(EMPTY_ARRAY_LIST), null));
0352: assertEquals("", StringUtils.join(Arrays
0353: .asList(EMPTY_ARRAY_LIST), ""));
0354: assertEquals("", StringUtils.join(Arrays
0355: .asList(EMPTY_ARRAY_LIST), SEPARATOR));
0356:
0357: assertEquals(TEXT_LIST, StringUtils.join(Arrays
0358: .asList(ARRAY_LIST), SEPARATOR));
0359: }
0360:
0361: public void testConcatenate_Objectarray() {
0362: assertEquals(null, StringUtils.concatenate(null));
0363: assertEquals("", StringUtils.concatenate(EMPTY_ARRAY_LIST));
0364: assertEquals("", StringUtils.concatenate(NULL_ARRAY_LIST));
0365: assertEquals("foo", StringUtils.concatenate(MIXED_ARRAY_LIST));
0366: assertEquals("foo2", StringUtils.concatenate(MIXED_TYPE_LIST));
0367: }
0368:
0369: public void testSplit_String() {
0370: assertEquals(null, StringUtils.split(null));
0371: assertEquals(0, StringUtils.split("").length);
0372:
0373: String str = "a b .c";
0374: String[] res = StringUtils.split(str);
0375: assertEquals(3, res.length);
0376: assertEquals("a", res[0]);
0377: assertEquals("b", res[1]);
0378: assertEquals(".c", res[2]);
0379:
0380: str = " a ";
0381: res = StringUtils.split(str);
0382: assertEquals(1, res.length);
0383: assertEquals("a", res[0]);
0384:
0385: str = "a" + WHITESPACE + "b" + NON_WHITESPACE + "c";
0386: res = StringUtils.split(str);
0387: assertEquals(2, res.length);
0388: assertEquals("a", res[0]);
0389: assertEquals("b" + NON_WHITESPACE + "c", res[1]);
0390: }
0391:
0392: public void testSplit_StringChar() {
0393: assertEquals(null, StringUtils.split(null, '.'));
0394: assertEquals(0, StringUtils.split("", '.').length);
0395:
0396: String str = "a.b.. c";
0397: String[] res = StringUtils.split(str, '.');
0398: assertEquals(3, res.length);
0399: assertEquals("a", res[0]);
0400: assertEquals("b", res[1]);
0401: assertEquals(" c", res[2]);
0402:
0403: str = ".a.";
0404: res = StringUtils.split(str, '.');
0405: assertEquals(1, res.length);
0406: assertEquals("a", res[0]);
0407:
0408: str = "a b c";
0409: res = StringUtils.split(str, ' ');
0410: assertEquals(3, res.length);
0411: assertEquals("a", res[0]);
0412: assertEquals("b", res[1]);
0413: assertEquals("c", res[2]);
0414: }
0415:
0416: public void testSplit_StringString_StringStringInt() {
0417: assertEquals(null, StringUtils.split(null, "."));
0418: assertEquals(null, StringUtils.split(null, ".", 3));
0419:
0420: assertEquals(0, StringUtils.split("", ".").length);
0421: assertEquals(0, StringUtils.split("", ".", 3).length);
0422:
0423: innerTestSplit('.', ".", ' ');
0424: innerTestSplit('.', ".", ',');
0425: innerTestSplit('.', ".,", 'x');
0426: for (int i = 0; i < WHITESPACE.length(); i++) {
0427: for (int j = 0; j < NON_WHITESPACE.length(); j++) {
0428: innerTestSplit(WHITESPACE.charAt(i), null,
0429: NON_WHITESPACE.charAt(j));
0430: innerTestSplit(WHITESPACE.charAt(i), String
0431: .valueOf(WHITESPACE.charAt(i)), NON_WHITESPACE
0432: .charAt(j));
0433: }
0434: }
0435:
0436: String[] results = null;
0437: String[] expectedResults = { "ab", "de fg" };
0438: results = StringUtils.split("ab de fg", null, 2);
0439: assertEquals(expectedResults.length, results.length);
0440: for (int i = 0; i < expectedResults.length; i++) {
0441: assertEquals(expectedResults[i], results[i]);
0442: }
0443:
0444: String[] expectedResults2 = { "ab", "cd:ef" };
0445: results = StringUtils.split("ab:cd:ef", ":", 2);
0446: assertEquals(expectedResults2.length, results.length);
0447: for (int i = 0; i < expectedResults2.length; i++) {
0448: assertEquals(expectedResults2[i], results[i]);
0449: }
0450: }
0451:
0452: private void innerTestSplit(char separator, String sepStr,
0453: char noMatch) {
0454: String msg = "Failed on separator hex("
0455: + Integer.toHexString(separator) + "), noMatch hex("
0456: + Integer.toHexString(noMatch) + "), sepStr(" + sepStr
0457: + ")";
0458:
0459: final String str = "a" + separator + "b" + separator
0460: + separator + noMatch + "c";
0461: String[] res;
0462: // (str, sepStr)
0463: res = StringUtils.split(str, sepStr);
0464: assertEquals(msg, 3, res.length);
0465: assertEquals(msg, "a", res[0]);
0466: assertEquals(msg, "b", res[1]);
0467: assertEquals(msg, noMatch + "c", res[2]);
0468:
0469: final String str2 = separator + "a" + separator;
0470: res = StringUtils.split(str2, sepStr);
0471: assertEquals(msg, 1, res.length);
0472: assertEquals(msg, "a", res[0]);
0473:
0474: res = StringUtils.split(str, sepStr, -1);
0475: assertEquals(msg, 3, res.length);
0476: assertEquals(msg, "a", res[0]);
0477: assertEquals(msg, "b", res[1]);
0478: assertEquals(msg, noMatch + "c", res[2]);
0479:
0480: res = StringUtils.split(str, sepStr, 0);
0481: assertEquals(msg, 3, res.length);
0482: assertEquals(msg, "a", res[0]);
0483: assertEquals(msg, "b", res[1]);
0484: assertEquals(msg, noMatch + "c", res[2]);
0485:
0486: res = StringUtils.split(str, sepStr, 1);
0487: assertEquals(msg, 1, res.length);
0488: assertEquals(msg, str, res[0]);
0489:
0490: res = StringUtils.split(str, sepStr, 2);
0491: assertEquals(msg, 2, res.length);
0492: assertEquals(msg, "a", res[0]);
0493: assertEquals(msg, str.substring(2), res[1]);
0494: }
0495:
0496: public void testSplitByWholeString_StringStringBoolean() {
0497: assertEquals(null, StringUtils.splitByWholeSeparator(null, "."));
0498:
0499: assertEquals(0,
0500: StringUtils.splitByWholeSeparator("", ".").length);
0501:
0502: String stringToSplitOnNulls = "ab de fg";
0503: String[] splitOnNullExpectedResults = { "ab", "de", "fg" };
0504:
0505: String[] splitOnNullResults = StringUtils
0506: .splitByWholeSeparator("ab de fg", null);
0507: assertEquals(splitOnNullExpectedResults.length,
0508: splitOnNullResults.length);
0509: for (int i = 0; i < splitOnNullExpectedResults.length; i += 1) {
0510: assertEquals(splitOnNullExpectedResults[i],
0511: splitOnNullResults[i]);
0512: }
0513:
0514: String stringToSplitOnCharactersAndString = "abstemiouslyaeiouyabstemiously";
0515:
0516: String[] splitOnStringExpectedResults = { "abstemiously",
0517: "abstemiously" };
0518: String[] splitOnStringResults = StringUtils
0519: .splitByWholeSeparator(
0520: stringToSplitOnCharactersAndString, "aeiouy");
0521: assertEquals(splitOnStringExpectedResults.length,
0522: splitOnStringResults.length);
0523: for (int i = 0; i < splitOnStringExpectedResults.length; i += 1) {
0524: assertEquals(splitOnStringExpectedResults[i],
0525: splitOnStringResults[i]);
0526: }
0527:
0528: String[] splitWithMultipleSeparatorExpectedResults = { "ab",
0529: "cd", "ef" };
0530: String[] splitWithMultipleSeparator = StringUtils
0531: .splitByWholeSeparator("ab:cd::ef", ":");
0532: assertEquals(splitWithMultipleSeparatorExpectedResults.length,
0533: splitWithMultipleSeparator.length);
0534: for (int i = 0; i < splitWithMultipleSeparatorExpectedResults.length; i++) {
0535: assertEquals(splitWithMultipleSeparatorExpectedResults[i],
0536: splitWithMultipleSeparator[i]);
0537: }
0538: }
0539:
0540: public void testSplitByWholeString_StringStringBooleanInt() {
0541: assertEquals(null, StringUtils.splitByWholeSeparator(null, ".",
0542: 3));
0543:
0544: assertEquals(0,
0545: StringUtils.splitByWholeSeparator("", ".", 3).length);
0546:
0547: String stringToSplitOnNulls = "ab de fg";
0548: String[] splitOnNullExpectedResults = { "ab", "de fg" };
0549: //String[] splitOnNullExpectedResults = { "ab", "de" } ;
0550:
0551: String[] splitOnNullResults = StringUtils
0552: .splitByWholeSeparator(stringToSplitOnNulls, null, 2);
0553: assertEquals(splitOnNullExpectedResults.length,
0554: splitOnNullResults.length);
0555: for (int i = 0; i < splitOnNullExpectedResults.length; i += 1) {
0556: assertEquals(splitOnNullExpectedResults[i],
0557: splitOnNullResults[i]);
0558: }
0559:
0560: String stringToSplitOnCharactersAndString = "abstemiouslyaeiouyabstemiouslyaeiouyabstemiously";
0561:
0562: String[] splitOnStringExpectedResults = { "abstemiously",
0563: "abstemiouslyaeiouyabstemiously" };
0564: //String[] splitOnStringExpectedResults = { "abstemiously", "abstemiously" } ;
0565: String[] splitOnStringResults = StringUtils
0566: .splitByWholeSeparator(
0567: stringToSplitOnCharactersAndString, "aeiouy", 2);
0568: assertEquals(splitOnStringExpectedResults.length,
0569: splitOnStringResults.length);
0570: for (int i = 0; i < splitOnStringExpectedResults.length; i++) {
0571: assertEquals(splitOnStringExpectedResults[i],
0572: splitOnStringResults[i]);
0573: }
0574: }
0575:
0576: public void testSplitPreserveAllTokens_String() {
0577: assertEquals(null, StringUtils.splitPreserveAllTokens(null));
0578: assertEquals(0, StringUtils.splitPreserveAllTokens("").length);
0579:
0580: String str = "abc def";
0581: String[] res = StringUtils.splitPreserveAllTokens(str);
0582: assertEquals(2, res.length);
0583: assertEquals("abc", res[0]);
0584: assertEquals("def", res[1]);
0585:
0586: str = "abc def";
0587: res = StringUtils.splitPreserveAllTokens(str);
0588: assertEquals(3, res.length);
0589: assertEquals("abc", res[0]);
0590: assertEquals("", res[1]);
0591: assertEquals("def", res[2]);
0592:
0593: str = " abc ";
0594: res = StringUtils.splitPreserveAllTokens(str);
0595: assertEquals(3, res.length);
0596: assertEquals("", res[0]);
0597: assertEquals("abc", res[1]);
0598: assertEquals("", res[2]);
0599:
0600: str = "a b .c";
0601: res = StringUtils.splitPreserveAllTokens(str);
0602: assertEquals(3, res.length);
0603: assertEquals("a", res[0]);
0604: assertEquals("b", res[1]);
0605: assertEquals(".c", res[2]);
0606:
0607: str = " a b .c";
0608: res = StringUtils.splitPreserveAllTokens(str);
0609: assertEquals(4, res.length);
0610: assertEquals("", res[0]);
0611: assertEquals("a", res[1]);
0612: assertEquals("b", res[2]);
0613: assertEquals(".c", res[3]);
0614:
0615: str = "a b .c";
0616: res = StringUtils.splitPreserveAllTokens(str);
0617: assertEquals(5, res.length);
0618: assertEquals("a", res[0]);
0619: assertEquals("", res[1]);
0620: assertEquals("b", res[2]);
0621: assertEquals("", res[3]);
0622: assertEquals(".c", res[4]);
0623:
0624: str = " a ";
0625: res = StringUtils.splitPreserveAllTokens(str);
0626: assertEquals(4, res.length);
0627: assertEquals("", res[0]);
0628: assertEquals("a", res[1]);
0629: assertEquals("", res[2]);
0630: assertEquals("", res[3]);
0631:
0632: str = " a b";
0633: res = StringUtils.splitPreserveAllTokens(str);
0634: assertEquals(4, res.length);
0635: assertEquals("", res[0]);
0636: assertEquals("a", res[1]);
0637: assertEquals("", res[2]);
0638: assertEquals("b", res[3]);
0639:
0640: str = "a" + WHITESPACE + "b" + NON_WHITESPACE + "c";
0641: res = StringUtils.splitPreserveAllTokens(str);
0642: assertEquals(WHITESPACE.length() + 1, res.length);
0643: assertEquals("a", res[0]);
0644: for (int i = 1; i < WHITESPACE.length() - 1; i++) {
0645: assertEquals("", res[i]);
0646: }
0647: assertEquals("b" + NON_WHITESPACE + "c", res[WHITESPACE
0648: .length()]);
0649: }
0650:
0651: public void testSplitPreserveAllTokens_StringChar() {
0652: assertEquals(null, StringUtils
0653: .splitPreserveAllTokens(null, '.'));
0654: assertEquals(0,
0655: StringUtils.splitPreserveAllTokens("", '.').length);
0656:
0657: String str = "a.b. c";
0658: String[] res = StringUtils.splitPreserveAllTokens(str, '.');
0659: assertEquals(3, res.length);
0660: assertEquals("a", res[0]);
0661: assertEquals("b", res[1]);
0662: assertEquals(" c", res[2]);
0663:
0664: str = "a.b.. c";
0665: res = StringUtils.splitPreserveAllTokens(str, '.');
0666: assertEquals(4, res.length);
0667: assertEquals("a", res[0]);
0668: assertEquals("b", res[1]);
0669: assertEquals("", res[2]);
0670: assertEquals(" c", res[3]);
0671:
0672: str = ".a.";
0673: res = StringUtils.splitPreserveAllTokens(str, '.');
0674: assertEquals(3, res.length);
0675: assertEquals("", res[0]);
0676: assertEquals("a", res[1]);
0677: assertEquals("", res[2]);
0678:
0679: str = ".a..";
0680: res = StringUtils.splitPreserveAllTokens(str, '.');
0681: assertEquals(4, res.length);
0682: assertEquals("", res[0]);
0683: assertEquals("a", res[1]);
0684: assertEquals("", res[2]);
0685: assertEquals("", res[3]);
0686:
0687: str = "..a.";
0688: res = StringUtils.splitPreserveAllTokens(str, '.');
0689: assertEquals(4, res.length);
0690: assertEquals("", res[0]);
0691: assertEquals("", res[1]);
0692: assertEquals("a", res[2]);
0693: assertEquals("", res[3]);
0694:
0695: str = "..a";
0696: res = StringUtils.splitPreserveAllTokens(str, '.');
0697: assertEquals(3, res.length);
0698: assertEquals("", res[0]);
0699: assertEquals("", res[1]);
0700: assertEquals("a", res[2]);
0701:
0702: str = "a b c";
0703: res = StringUtils.splitPreserveAllTokens(str, ' ');
0704: assertEquals(3, res.length);
0705: assertEquals("a", res[0]);
0706: assertEquals("b", res[1]);
0707: assertEquals("c", res[2]);
0708:
0709: str = "a b c";
0710: res = StringUtils.splitPreserveAllTokens(str, ' ');
0711: assertEquals(5, res.length);
0712: assertEquals("a", res[0]);
0713: assertEquals("", res[1]);
0714: assertEquals("b", res[2]);
0715: assertEquals("", res[3]);
0716: assertEquals("c", res[4]);
0717:
0718: str = " a b c";
0719: res = StringUtils.splitPreserveAllTokens(str, ' ');
0720: assertEquals(4, res.length);
0721: assertEquals("", res[0]);
0722: assertEquals("a", res[1]);
0723: assertEquals("b", res[2]);
0724: assertEquals("c", res[3]);
0725:
0726: str = " a b c";
0727: res = StringUtils.splitPreserveAllTokens(str, ' ');
0728: assertEquals(5, res.length);
0729: assertEquals("", res[0]);
0730: assertEquals("", res[1]);
0731: assertEquals("a", res[2]);
0732: assertEquals("b", res[3]);
0733: assertEquals("c", res[4]);
0734:
0735: str = "a b c ";
0736: res = StringUtils.splitPreserveAllTokens(str, ' ');
0737: assertEquals(4, res.length);
0738: assertEquals("a", res[0]);
0739: assertEquals("b", res[1]);
0740: assertEquals("c", res[2]);
0741: assertEquals("", res[3]);
0742:
0743: str = "a b c ";
0744: res = StringUtils.splitPreserveAllTokens(str, ' ');
0745: assertEquals(5, res.length);
0746: assertEquals("a", res[0]);
0747: assertEquals("b", res[1]);
0748: assertEquals("c", res[2]);
0749: assertEquals("", res[3]);
0750: assertEquals("", res[3]);
0751:
0752: // Match example in javadoc
0753: {
0754: String[] results = null;
0755: String[] expectedResults = { "a", "", "b", "c" };
0756: results = StringUtils.splitPreserveAllTokens("a..b.c", '.');
0757: assertEquals(expectedResults.length, results.length);
0758: for (int i = 0; i < expectedResults.length; i++) {
0759: assertEquals(expectedResults[i], results[i]);
0760: }
0761: }
0762: }
0763:
0764: public void testSplitPreserveAllTokens_StringString_StringStringInt() {
0765: assertEquals(null, StringUtils
0766: .splitPreserveAllTokens(null, "."));
0767: assertEquals(null, StringUtils.splitPreserveAllTokens(null,
0768: ".", 3));
0769:
0770: assertEquals(0,
0771: StringUtils.splitPreserveAllTokens("", ".").length);
0772: assertEquals(0,
0773: StringUtils.splitPreserveAllTokens("", ".", 3).length);
0774:
0775: innerTestSplitPreserveAllTokens('.', ".", ' ');
0776: innerTestSplitPreserveAllTokens('.', ".", ',');
0777: innerTestSplitPreserveAllTokens('.', ".,", 'x');
0778: for (int i = 0; i < WHITESPACE.length(); i++) {
0779: for (int j = 0; j < NON_WHITESPACE.length(); j++) {
0780: innerTestSplitPreserveAllTokens(WHITESPACE.charAt(i),
0781: null, NON_WHITESPACE.charAt(j));
0782: innerTestSplitPreserveAllTokens(WHITESPACE.charAt(i),
0783: String.valueOf(WHITESPACE.charAt(i)),
0784: NON_WHITESPACE.charAt(j));
0785: }
0786: }
0787:
0788: {
0789: String[] results = null;
0790: String[] expectedResults = { "ab", "de fg" };
0791: results = StringUtils.splitPreserveAllTokens("ab de fg",
0792: null, 2);
0793: assertEquals(expectedResults.length, results.length);
0794: for (int i = 0; i < expectedResults.length; i++) {
0795: assertEquals(expectedResults[i], results[i]);
0796: }
0797: }
0798:
0799: {
0800: String[] results = null;
0801: String[] expectedResults = { "ab", " de fg" };
0802: results = StringUtils.splitPreserveAllTokens("ab de fg",
0803: null, 2);
0804: System.out.println("");
0805: assertEquals(expectedResults.length, results.length);
0806: for (int i = 0; i < expectedResults.length; i++) {
0807: assertEquals(expectedResults[i], results[i]);
0808: }
0809: }
0810:
0811: {
0812: String[] results = null;
0813: String[] expectedResults = { "ab", "::de:fg" };
0814: results = StringUtils.splitPreserveAllTokens("ab:::de:fg",
0815: ":", 2);
0816: assertEquals(expectedResults.length, results.length);
0817: for (int i = 0; i < expectedResults.length; i++) {
0818: assertEquals(expectedResults[i], results[i]);
0819: }
0820: }
0821:
0822: {
0823: String[] results = null;
0824: String[] expectedResults = { "ab", "", " de fg" };
0825: results = StringUtils.splitPreserveAllTokens("ab de fg",
0826: null, 3);
0827: assertEquals(expectedResults.length, results.length);
0828: for (int i = 0; i < expectedResults.length; i++) {
0829: assertEquals(expectedResults[i], results[i]);
0830: }
0831: }
0832:
0833: {
0834: String[] results = null;
0835: String[] expectedResults = { "ab", "", "", "de fg" };
0836: results = StringUtils.splitPreserveAllTokens("ab de fg",
0837: null, 4);
0838: assertEquals(expectedResults.length, results.length);
0839: for (int i = 0; i < expectedResults.length; i++) {
0840: assertEquals(expectedResults[i], results[i]);
0841: }
0842: }
0843:
0844: {
0845: String[] expectedResults = { "ab", "cd:ef" };
0846: String[] results = null;
0847: results = StringUtils.splitPreserveAllTokens("ab:cd:ef",
0848: ":", 2);
0849: assertEquals(expectedResults.length, results.length);
0850: for (int i = 0; i < expectedResults.length; i++) {
0851: assertEquals(expectedResults[i], results[i]);
0852: }
0853: }
0854:
0855: {
0856: String[] results = null;
0857: String[] expectedResults = { "ab", ":cd:ef" };
0858: results = StringUtils.splitPreserveAllTokens("ab::cd:ef",
0859: ":", 2);
0860: assertEquals(expectedResults.length, results.length);
0861: for (int i = 0; i < expectedResults.length; i++) {
0862: assertEquals(expectedResults[i], results[i]);
0863: }
0864: }
0865:
0866: {
0867: String[] results = null;
0868: String[] expectedResults = { "ab", "", ":cd:ef" };
0869: results = StringUtils.splitPreserveAllTokens("ab:::cd:ef",
0870: ":", 3);
0871: assertEquals(expectedResults.length, results.length);
0872: for (int i = 0; i < expectedResults.length; i++) {
0873: assertEquals(expectedResults[i], results[i]);
0874: }
0875: }
0876:
0877: {
0878: String[] results = null;
0879: String[] expectedResults = { "ab", "", "", "cd:ef" };
0880: results = StringUtils.splitPreserveAllTokens("ab:::cd:ef",
0881: ":", 4);
0882: assertEquals(expectedResults.length, results.length);
0883: for (int i = 0; i < expectedResults.length; i++) {
0884: assertEquals(expectedResults[i], results[i]);
0885: }
0886: }
0887:
0888: {
0889: String[] results = null;
0890: String[] expectedResults = { "", "ab", "", "", "cd:ef" };
0891: results = StringUtils.splitPreserveAllTokens(":ab:::cd:ef",
0892: ":", 5);
0893: assertEquals(expectedResults.length, results.length);
0894: for (int i = 0; i < expectedResults.length; i++) {
0895: assertEquals(expectedResults[i], results[i]);
0896: }
0897: }
0898:
0899: {
0900: String[] results = null;
0901: String[] expectedResults = { "", "", "ab", "", "", "cd:ef" };
0902: results = StringUtils.splitPreserveAllTokens(
0903: "::ab:::cd:ef", ":", 6);
0904: assertEquals(expectedResults.length, results.length);
0905: for (int i = 0; i < expectedResults.length; i++) {
0906: assertEquals(expectedResults[i], results[i]);
0907: }
0908: }
0909:
0910: }
0911:
0912: private void innerTestSplitPreserveAllTokens(char separator,
0913: String sepStr, char noMatch) {
0914: String msg = "Failed on separator hex("
0915: + Integer.toHexString(separator) + "), noMatch hex("
0916: + Integer.toHexString(noMatch) + "), sepStr(" + sepStr
0917: + ")";
0918:
0919: final String str = "a" + separator + "b" + separator
0920: + separator + noMatch + "c";
0921: String[] res;
0922: // (str, sepStr)
0923: res = StringUtils.splitPreserveAllTokens(str, sepStr);
0924: assertEquals(msg, 4, res.length);
0925: assertEquals(msg, "a", res[0]);
0926: assertEquals(msg, "b", res[1]);
0927: assertEquals(msg, "", res[2]);
0928: assertEquals(msg, noMatch + "c", res[3]);
0929:
0930: final String str2 = separator + "a" + separator;
0931: res = StringUtils.splitPreserveAllTokens(str2, sepStr);
0932: assertEquals(msg, 3, res.length);
0933: assertEquals(msg, "", res[0]);
0934: assertEquals(msg, "a", res[1]);
0935: assertEquals(msg, "", res[2]);
0936:
0937: res = StringUtils.splitPreserveAllTokens(str, sepStr, -1);
0938: assertEquals(msg, 4, res.length);
0939: assertEquals(msg, "a", res[0]);
0940: assertEquals(msg, "b", res[1]);
0941: assertEquals(msg, "", res[2]);
0942: assertEquals(msg, noMatch + "c", res[3]);
0943:
0944: res = StringUtils.splitPreserveAllTokens(str, sepStr, 0);
0945: assertEquals(msg, 4, res.length);
0946: assertEquals(msg, "a", res[0]);
0947: assertEquals(msg, "b", res[1]);
0948: assertEquals(msg, "", res[2]);
0949: assertEquals(msg, noMatch + "c", res[3]);
0950:
0951: res = StringUtils.splitPreserveAllTokens(str, sepStr, 1);
0952: assertEquals(msg, 1, res.length);
0953: assertEquals(msg, str, res[0]);
0954:
0955: res = StringUtils.splitPreserveAllTokens(str, sepStr, 2);
0956: assertEquals(msg, 2, res.length);
0957: assertEquals(msg, "a", res[0]);
0958: assertEquals(msg, str.substring(2), res[1]);
0959: }
0960:
0961: public void testDeleteSpace_String() {
0962: assertEquals(null, StringUtils.deleteSpaces(null));
0963: assertEquals("", StringUtils.deleteSpaces(""));
0964: assertEquals("", StringUtils.deleteSpaces(" \t\t\n\n "));
0965: assertEquals("test", StringUtils
0966: .deleteSpaces("t \t\ne\rs\n\n \tt"));
0967: }
0968:
0969: public void testDeleteWhitespace_String() {
0970: assertEquals(null, StringUtils.deleteWhitespace(null));
0971: assertEquals("", StringUtils.deleteWhitespace(""));
0972: assertEquals("", StringUtils
0973: .deleteWhitespace(" \u000C \t\t\u001F\n\n \u000B "));
0974: assertEquals("", StringUtils
0975: .deleteWhitespace(StringUtilsTest.WHITESPACE));
0976: assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils
0977: .deleteWhitespace(StringUtilsTest.NON_WHITESPACE));
0978: // Note: u-2007 and u-000A both cause problems in the source code
0979: // it should ignore 2007 but delete 000A
0980: assertEquals("\u00A0\u202F", StringUtils
0981: .deleteWhitespace(" \u00A0 \t\t\n\n \u202F "));
0982: assertEquals("\u00A0\u202F", StringUtils
0983: .deleteWhitespace("\u00A0\u202F"));
0984: assertEquals("test", StringUtils
0985: .deleteWhitespace("\u000Bt \t\n\u0009e\rs\n\n \tt"));
0986: }
0987:
0988: public void testReplace_StringStringString() {
0989: assertEquals(null, StringUtils.replace(null, null, null));
0990: assertEquals(null, StringUtils.replace(null, null, "any"));
0991: assertEquals(null, StringUtils.replace(null, "any", null));
0992: assertEquals(null, StringUtils.replace(null, "any", "any"));
0993:
0994: assertEquals("", StringUtils.replace("", null, null));
0995: assertEquals("", StringUtils.replace("", null, "any"));
0996: assertEquals("", StringUtils.replace("", "any", null));
0997: assertEquals("", StringUtils.replace("", "any", "any"));
0998:
0999: assertEquals("FOO", StringUtils.replace("FOO", "", "any"));
1000: assertEquals("FOO", StringUtils.replace("FOO", null, "any"));
1001: assertEquals("FOO", StringUtils.replace("FOO", "F", null));
1002: assertEquals("FOO", StringUtils.replace("FOO", null, null));
1003:
1004: assertEquals("", StringUtils.replace("foofoofoo", "foo", ""));
1005: assertEquals("barbarbar", StringUtils.replace("foofoofoo",
1006: "foo", "bar"));
1007: assertEquals("farfarfar", StringUtils.replace("foofoofoo",
1008: "oo", "ar"));
1009: }
1010:
1011: public void testReplace_StringStringStringInt() {
1012: assertEquals(null, StringUtils.replace(null, null, null, 2));
1013: assertEquals(null, StringUtils.replace(null, null, "any", 2));
1014: assertEquals(null, StringUtils.replace(null, "any", null, 2));
1015: assertEquals(null, StringUtils.replace(null, "any", "any", 2));
1016:
1017: assertEquals("", StringUtils.replace("", null, null, 2));
1018: assertEquals("", StringUtils.replace("", null, "any", 2));
1019: assertEquals("", StringUtils.replace("", "any", null, 2));
1020: assertEquals("", StringUtils.replace("", "any", "any", 2));
1021:
1022: String str = new String(new char[] { 'o', 'o', 'f', 'o', 'o' });
1023: assertSame(str, StringUtils.replace(str, "x", "", -1));
1024:
1025: assertEquals("f", StringUtils.replace("oofoo", "o", "", -1));
1026: assertEquals("oofoo", StringUtils.replace("oofoo", "o", "", 0));
1027: assertEquals("ofoo", StringUtils.replace("oofoo", "o", "", 1));
1028: assertEquals("foo", StringUtils.replace("oofoo", "o", "", 2));
1029: assertEquals("fo", StringUtils.replace("oofoo", "o", "", 3));
1030: assertEquals("f", StringUtils.replace("oofoo", "o", "", 4));
1031:
1032: assertEquals("f", StringUtils.replace("oofoo", "o", "", -5));
1033: assertEquals("f", StringUtils.replace("oofoo", "o", "", 1000));
1034: }
1035:
1036: public void testReplaceOnce_StringStringString() {
1037: assertEquals(null, StringUtils.replaceOnce(null, null, null));
1038: assertEquals(null, StringUtils.replaceOnce(null, null, "any"));
1039: assertEquals(null, StringUtils.replaceOnce(null, "any", null));
1040: assertEquals(null, StringUtils.replaceOnce(null, "any", "any"));
1041:
1042: assertEquals("", StringUtils.replaceOnce("", null, null));
1043: assertEquals("", StringUtils.replaceOnce("", null, "any"));
1044: assertEquals("", StringUtils.replaceOnce("", "any", null));
1045: assertEquals("", StringUtils.replaceOnce("", "any", "any"));
1046:
1047: assertEquals("FOO", StringUtils.replaceOnce("FOO", "", "any"));
1048: assertEquals("FOO", StringUtils.replaceOnce("FOO", null, "any"));
1049: assertEquals("FOO", StringUtils.replaceOnce("FOO", "F", null));
1050: assertEquals("FOO", StringUtils.replaceOnce("FOO", null, null));
1051:
1052: assertEquals("foofoo", StringUtils.replaceOnce("foofoofoo",
1053: "foo", ""));
1054: }
1055:
1056: public void testReplaceChars_StringCharChar() {
1057: assertEquals(null, StringUtils.replaceChars(null, 'b', 'z'));
1058: assertEquals("", StringUtils.replaceChars("", 'b', 'z'));
1059: assertEquals("azcza", StringUtils.replaceChars("abcba", 'b',
1060: 'z'));
1061: assertEquals("abcba", StringUtils.replaceChars("abcba", 'x',
1062: 'z'));
1063: }
1064:
1065: public void testReplaceChars_StringStringString() {
1066: assertEquals(null, StringUtils.replaceChars(null, null, null));
1067: assertEquals(null, StringUtils.replaceChars(null, "", null));
1068: assertEquals(null, StringUtils.replaceChars(null, "a", null));
1069: assertEquals(null, StringUtils.replaceChars(null, null, ""));
1070: assertEquals(null, StringUtils.replaceChars(null, null, "x"));
1071:
1072: assertEquals("", StringUtils.replaceChars("", null, null));
1073: assertEquals("", StringUtils.replaceChars("", "", null));
1074: assertEquals("", StringUtils.replaceChars("", "a", null));
1075: assertEquals("", StringUtils.replaceChars("", null, ""));
1076: assertEquals("", StringUtils.replaceChars("", null, "x"));
1077:
1078: assertEquals("abc", StringUtils.replaceChars("abc", null, null));
1079: assertEquals("abc", StringUtils.replaceChars("abc", null, ""));
1080: assertEquals("abc", StringUtils.replaceChars("abc", null, "x"));
1081:
1082: assertEquals("abc", StringUtils.replaceChars("abc", "", null));
1083: assertEquals("abc", StringUtils.replaceChars("abc", "", ""));
1084: assertEquals("abc", StringUtils.replaceChars("abc", "", "x"));
1085:
1086: assertEquals("ac", StringUtils.replaceChars("abc", "b", null));
1087: assertEquals("ac", StringUtils.replaceChars("abc", "b", ""));
1088: assertEquals("axc", StringUtils.replaceChars("abc", "b", "x"));
1089:
1090: assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc",
1091: "yz"));
1092: assertEquals("ayya", StringUtils.replaceChars("abcba", "bc",
1093: "y"));
1094: assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc",
1095: "yzx"));
1096:
1097: assertEquals("abcba", StringUtils.replaceChars("abcba", "z",
1098: "w"));
1099: assertSame("abcba", StringUtils.replaceChars("abcba", "z", "w"));
1100:
1101: // Javadoc examples:
1102: assertEquals("jelly", StringUtils.replaceChars("hello", "ho",
1103: "jy"));
1104: assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc",
1105: "yz"));
1106: assertEquals("ayya", StringUtils.replaceChars("abcba", "bc",
1107: "y"));
1108: assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc",
1109: "yzx"));
1110:
1111: // From http://issues.apache.org/bugzilla/show_bug.cgi?id=25454
1112: assertEquals("bcc", StringUtils.replaceChars("abc", "ab", "bc"));
1113: assertEquals(
1114: "q651.506bera",
1115: StringUtils
1116: .replaceChars(
1117: "d216.102oren",
1118: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789",
1119: "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM567891234"));
1120: }
1121:
1122: public void testOverlayString_StringStringIntInt() {
1123: assertEquals("overlayString(String, String, int, int) failed",
1124: "foo foor baz", StringUtils.overlayString(
1125: SENTENCE_UNCAP, FOO_UNCAP, 4, 6));
1126: assertEquals("abef", StringUtils.overlayString("abcdef", "", 2,
1127: 4));
1128: assertEquals("abzzzzef", StringUtils.overlayString("abcdef",
1129: "zzzz", 2, 4));
1130: assertEquals("abcdzzzzcdef", StringUtils.overlayString(
1131: "abcdef", "zzzz", 4, 2));
1132: try {
1133: StringUtils.overlayString(null, "zzzz", 2, 4);
1134: fail();
1135: } catch (NullPointerException ex) {
1136: }
1137: try {
1138: StringUtils.overlayString("abcdef", null, 2, 4);
1139: fail();
1140: } catch (NullPointerException ex) {
1141: }
1142: try {
1143: StringUtils.overlayString("abcdef", "zzzz", -1, 4);
1144: fail();
1145: } catch (IndexOutOfBoundsException ex) {
1146: }
1147: try {
1148: StringUtils.overlayString("abcdef", "zzzz", 2, 8);
1149: fail();
1150: } catch (IndexOutOfBoundsException ex) {
1151: }
1152: }
1153:
1154: public void testOverlay_StringStringIntInt() {
1155: assertEquals(null, StringUtils.overlay(null, null, 2, 4));
1156: assertEquals(null, StringUtils.overlay(null, null, -2, -4));
1157:
1158: assertEquals("", StringUtils.overlay("", null, 0, 0));
1159: assertEquals("", StringUtils.overlay("", "", 0, 0));
1160: assertEquals("zzzz", StringUtils.overlay("", "zzzz", 0, 0));
1161: assertEquals("zzzz", StringUtils.overlay("", "zzzz", 2, 4));
1162: assertEquals("zzzz", StringUtils.overlay("", "zzzz", -2, -4));
1163:
1164: assertEquals("abef", StringUtils.overlay("abcdef", null, 2, 4));
1165: assertEquals("abef", StringUtils.overlay("abcdef", null, 4, 2));
1166: assertEquals("abef", StringUtils.overlay("abcdef", "", 2, 4));
1167: assertEquals("abef", StringUtils.overlay("abcdef", "", 4, 2));
1168: assertEquals("abzzzzef", StringUtils.overlay("abcdef", "zzzz",
1169: 2, 4));
1170: assertEquals("abzzzzef", StringUtils.overlay("abcdef", "zzzz",
1171: 4, 2));
1172:
1173: assertEquals("zzzzef", StringUtils.overlay("abcdef", "zzzz",
1174: -1, 4));
1175: assertEquals("zzzzef", StringUtils.overlay("abcdef", "zzzz", 4,
1176: -1));
1177: assertEquals("zzzzabcdef", StringUtils.overlay("abcdef",
1178: "zzzz", -2, -1));
1179: assertEquals("zzzzabcdef", StringUtils.overlay("abcdef",
1180: "zzzz", -1, -2));
1181: assertEquals("abcdzzzz", StringUtils.overlay("abcdef", "zzzz",
1182: 4, 10));
1183: assertEquals("abcdzzzz", StringUtils.overlay("abcdef", "zzzz",
1184: 10, 4));
1185: assertEquals("abcdefzzzz", StringUtils.overlay("abcdef",
1186: "zzzz", 8, 10));
1187: assertEquals("abcdefzzzz", StringUtils.overlay("abcdef",
1188: "zzzz", 10, 8));
1189: }
1190:
1191: public void testRepeat_StringInt() {
1192: assertEquals(null, StringUtils.repeat(null, 2));
1193: assertEquals("", StringUtils.repeat("ab", 0));
1194: assertEquals("", StringUtils.repeat("", 3));
1195: assertEquals("aaa", StringUtils.repeat("a", 3));
1196: assertEquals("ababab", StringUtils.repeat("ab", 3));
1197: assertEquals("abcabcabc", StringUtils.repeat("abc", 3));
1198: String str = StringUtils.repeat("a", 10000); // bigger than pad limit
1199: assertEquals(10000, str.length());
1200: assertEquals(true, StringUtils.containsOnly(str,
1201: new char[] { 'a' }));
1202: }
1203:
1204: public void testDeprecatedChompFunctions() {
1205: assertEquals("chompLast(String) failed", FOO_UNCAP, StringUtils
1206: .chompLast(FOO_UNCAP + "\n"));
1207:
1208: assertEquals("chompLast(\"\") failed", "", StringUtils
1209: .chompLast(""));
1210: assertEquals("chompLast(\"test\", \"test\") failed", "test",
1211: StringUtils.chompLast("test", "tst"));
1212:
1213: assertEquals("getChomp(String, String) failed", "\n"
1214: + FOO_UNCAP, StringUtils.getChomp(FOO_UNCAP + "\n"
1215: + FOO_UNCAP, "\n"));
1216: assertEquals("getChomp(String, String) failed", FOO_CAP,
1217: StringUtils.getChomp(FOO_CAP + FOO_CAP, FOO_CAP));
1218: assertEquals("getChomp(String, String) failed", "", StringUtils
1219: .getChomp(FOO_UNCAP, FOO_CAP));
1220:
1221: assertEquals("prechomp(String, String) failed", FOO_UNCAP,
1222: StringUtils
1223: .prechomp(FOO_UNCAP + "\n" + FOO_UNCAP, "\n"));
1224: assertEquals("prechomp(String, String) failed", FOO_UNCAP,
1225: StringUtils.prechomp(FOO_UNCAP, FOO_CAP));
1226:
1227: assertEquals("getPrechomp(String, String) failed", FOO_UNCAP
1228: + "\n", StringUtils.getPrechomp(FOO_UNCAP + "\n"
1229: + FOO_UNCAP, "\n"));
1230: assertEquals("getPrechomp(String, String) failed", "",
1231: StringUtils.getPrechomp(FOO_CAP, FOO_UNCAP));
1232:
1233: assertEquals("chopNewline(String, String) failed", FOO_UNCAP,
1234: StringUtils.chopNewline(FOO_UNCAP + "\r\n"));
1235: }
1236:
1237: public void testChop() {
1238:
1239: String[][] chopCases = { { FOO_UNCAP + "\r\n", FOO_UNCAP },
1240: { FOO_UNCAP + "\n", FOO_UNCAP },
1241: { FOO_UNCAP + "\r", FOO_UNCAP },
1242: { FOO_UNCAP + " \r", FOO_UNCAP + " " },
1243: { "foo", "fo" }, { "foo\nfoo", "foo\nfo" },
1244: { "\n", "" }, { "\r", "" }, { "\r\n", "" },
1245: { null, null }, { "", "" }, { "a", "" }, };
1246: for (int i = 0; i < chopCases.length; i++) {
1247: String original = chopCases[i][0];
1248: String expectedResult = chopCases[i][1];
1249: assertEquals("chop(String) failed", expectedResult,
1250: StringUtils.chop(original));
1251: }
1252: }
1253:
1254: public void testChomp() {
1255:
1256: String[][] chompCases = { { FOO_UNCAP + "\r\n", FOO_UNCAP },
1257: { FOO_UNCAP + "\n", FOO_UNCAP },
1258: { FOO_UNCAP + "\r", FOO_UNCAP },
1259: { FOO_UNCAP + " \r", FOO_UNCAP + " " },
1260: { FOO_UNCAP, FOO_UNCAP },
1261: { FOO_UNCAP + "\n\n", FOO_UNCAP + "\n" },
1262: { FOO_UNCAP + "\r\n\r\n", FOO_UNCAP + "\r\n" },
1263: { "foo\nfoo", "foo\nfoo" },
1264: { "foo\n\rfoo", "foo\n\rfoo" }, { "\n", "" },
1265: { "\r", "" }, { "a", "a" }, { "\r\n", "" }, { "", "" },
1266: { null, null },
1267: { FOO_UNCAP + "\n\r", FOO_UNCAP + "\n" } };
1268: for (int i = 0; i < chompCases.length; i++) {
1269: String original = chompCases[i][0];
1270: String expectedResult = chompCases[i][1];
1271: assertEquals("chomp(String) failed", expectedResult,
1272: StringUtils.chomp(original));
1273: }
1274:
1275: assertEquals("chomp(String, String) failed", "foo", StringUtils
1276: .chomp("foobar", "bar"));
1277: assertEquals("chomp(String, String) failed", "foobar",
1278: StringUtils.chomp("foobar", "baz"));
1279: assertEquals("chomp(String, String) failed", "foo", StringUtils
1280: .chomp("foo", "foooo"));
1281: assertEquals("chomp(String, String) failed", "foobar",
1282: StringUtils.chomp("foobar", ""));
1283: assertEquals("chomp(String, String) failed", "foobar",
1284: StringUtils.chomp("foobar", null));
1285: assertEquals("chomp(String, String) failed", "", StringUtils
1286: .chomp("", "foo"));
1287: assertEquals("chomp(String, String) failed", "", StringUtils
1288: .chomp("", null));
1289: assertEquals("chomp(String, String) failed", "", StringUtils
1290: .chomp("", ""));
1291: assertEquals("chomp(String, String) failed", null, StringUtils
1292: .chomp(null, "foo"));
1293: assertEquals("chomp(String, String) failed", null, StringUtils
1294: .chomp(null, null));
1295: assertEquals("chomp(String, String) failed", null, StringUtils
1296: .chomp(null, ""));
1297: assertEquals("chomp(String, String) failed", "", StringUtils
1298: .chomp("foo", "foo"));
1299: assertEquals("chomp(String, String) failed", " ", StringUtils
1300: .chomp(" foo", "foo"));
1301: assertEquals("chomp(String, String) failed", "foo ",
1302: StringUtils.chomp("foo ", "foo"));
1303: }
1304:
1305: public void testChopNewLine() {
1306:
1307: String[][] newLineCases = {
1308: { FOO_UNCAP + "\r\n", FOO_UNCAP },
1309: { FOO_UNCAP + "\n", FOO_UNCAP },
1310: { FOO_UNCAP + "\r", FOO_UNCAP + "\r" },
1311: { FOO_UNCAP, FOO_UNCAP },
1312: { FOO_UNCAP + "\n" + FOO_UNCAP,
1313: FOO_UNCAP + "\n" + FOO_UNCAP },
1314: { FOO_UNCAP + "\n\n", FOO_UNCAP + "\n" }, { "\n", "" },
1315: { "", "" }, { "\r\n", "" } };
1316:
1317: for (int i = 0; i < newLineCases.length; i++) {
1318: String original = newLineCases[i][0];
1319: String expectedResult = newLineCases[i][1];
1320: assertEquals("chopNewline(String) failed", expectedResult,
1321: StringUtils.chopNewline(original));
1322: }
1323: }
1324:
1325: //-----------------------------------------------------------------------
1326: public void testRightPad_StringInt() {
1327: assertEquals(null, StringUtils.rightPad(null, 5));
1328: assertEquals(" ", StringUtils.rightPad("", 5));
1329: assertEquals("abc ", StringUtils.rightPad("abc", 5));
1330: assertEquals("abc", StringUtils.rightPad("abc", 2));
1331: assertEquals("abc", StringUtils.rightPad("abc", -1));
1332: }
1333:
1334: public void testRightPad_StringIntChar() {
1335: assertEquals(null, StringUtils.rightPad(null, 5, ' '));
1336: assertEquals(" ", StringUtils.rightPad("", 5, ' '));
1337: assertEquals("abc ", StringUtils.rightPad("abc", 5, ' '));
1338: assertEquals("abc", StringUtils.rightPad("abc", 2, ' '));
1339: assertEquals("abc", StringUtils.rightPad("abc", -1, ' '));
1340: assertEquals("abcxx", StringUtils.rightPad("abc", 5, 'x'));
1341: String str = StringUtils.rightPad("aaa", 10000, 'a'); // bigger than pad length
1342: assertEquals(10000, str.length());
1343: assertEquals(true, StringUtils.containsOnly(str,
1344: new char[] { 'a' }));
1345: }
1346:
1347: public void testRightPad_StringIntString() {
1348: assertEquals(null, StringUtils.rightPad(null, 5, "-+"));
1349: assertEquals(" ", StringUtils.rightPad("", 5, " "));
1350: assertEquals(null, StringUtils.rightPad(null, 8, null));
1351: assertEquals("abc-+-+", StringUtils.rightPad("abc", 7, "-+"));
1352: assertEquals("abc-+~", StringUtils.rightPad("abc", 6, "-+~"));
1353: assertEquals("abc-+", StringUtils.rightPad("abc", 5, "-+~"));
1354: assertEquals("abc", StringUtils.rightPad("abc", 2, " "));
1355: assertEquals("abc", StringUtils.rightPad("abc", -1, " "));
1356: assertEquals("abc ", StringUtils.rightPad("abc", 5, null));
1357: assertEquals("abc ", StringUtils.rightPad("abc", 5, ""));
1358: }
1359:
1360: //-----------------------------------------------------------------------
1361: public void testLeftPad_StringInt() {
1362: assertEquals(null, StringUtils.leftPad(null, 5));
1363: assertEquals(" ", StringUtils.leftPad("", 5));
1364: assertEquals(" abc", StringUtils.leftPad("abc", 5));
1365: assertEquals("abc", StringUtils.leftPad("abc", 2));
1366: }
1367:
1368: public void testLeftPad_StringIntChar() {
1369: assertEquals(null, StringUtils.leftPad(null, 5, ' '));
1370: assertEquals(" ", StringUtils.leftPad("", 5, ' '));
1371: assertEquals(" abc", StringUtils.leftPad("abc", 5, ' '));
1372: assertEquals("xxabc", StringUtils.leftPad("abc", 5, 'x'));
1373: assertEquals("\uffff\uffffabc", StringUtils.leftPad("abc", 5,
1374: '\uffff'));
1375: assertEquals("abc", StringUtils.leftPad("abc", 2, ' '));
1376: String str = StringUtils.leftPad("aaa", 10000, 'a'); // bigger than pad length
1377: assertEquals(10000, str.length());
1378: assertEquals(true, StringUtils.containsOnly(str,
1379: new char[] { 'a' }));
1380: }
1381:
1382: public void testLeftPad_StringIntString() {
1383: assertEquals(null, StringUtils.leftPad(null, 5, "-+"));
1384: assertEquals(null, StringUtils.leftPad(null, 5, null));
1385: assertEquals(" ", StringUtils.leftPad("", 5, " "));
1386: assertEquals("-+-+abc", StringUtils.leftPad("abc", 7, "-+"));
1387: assertEquals("-+~abc", StringUtils.leftPad("abc", 6, "-+~"));
1388: assertEquals("-+abc", StringUtils.leftPad("abc", 5, "-+~"));
1389: assertEquals("abc", StringUtils.leftPad("abc", 2, " "));
1390: assertEquals("abc", StringUtils.leftPad("abc", -1, " "));
1391: assertEquals(" abc", StringUtils.leftPad("abc", 5, null));
1392: assertEquals(" abc", StringUtils.leftPad("abc", 5, ""));
1393: }
1394:
1395: //-----------------------------------------------------------------------
1396: public void testCenter_StringInt() {
1397: assertEquals(null, StringUtils.center(null, -1));
1398: assertEquals(null, StringUtils.center(null, 4));
1399: assertEquals(" ", StringUtils.center("", 4));
1400: assertEquals("ab", StringUtils.center("ab", 0));
1401: assertEquals("ab", StringUtils.center("ab", -1));
1402: assertEquals("ab", StringUtils.center("ab", 1));
1403: assertEquals(" ", StringUtils.center("", 4));
1404: assertEquals(" ab ", StringUtils.center("ab", 4));
1405: assertEquals("abcd", StringUtils.center("abcd", 2));
1406: assertEquals(" a ", StringUtils.center("a", 4));
1407: assertEquals(" a ", StringUtils.center("a", 5));
1408: }
1409:
1410: public void testCenter_StringIntChar() {
1411: assertEquals(null, StringUtils.center(null, -1, ' '));
1412: assertEquals(null, StringUtils.center(null, 4, ' '));
1413: assertEquals(" ", StringUtils.center("", 4, ' '));
1414: assertEquals("ab", StringUtils.center("ab", 0, ' '));
1415: assertEquals("ab", StringUtils.center("ab", -1, ' '));
1416: assertEquals("ab", StringUtils.center("ab", 1, ' '));
1417: assertEquals(" ", StringUtils.center("", 4, ' '));
1418: assertEquals(" ab ", StringUtils.center("ab", 4, ' '));
1419: assertEquals("abcd", StringUtils.center("abcd", 2, ' '));
1420: assertEquals(" a ", StringUtils.center("a", 4, ' '));
1421: assertEquals(" a ", StringUtils.center("a", 5, ' '));
1422: assertEquals("xxaxx", StringUtils.center("a", 5, 'x'));
1423: }
1424:
1425: public void testCenter_StringIntString() {
1426: assertEquals(null, StringUtils.center(null, 4, null));
1427: assertEquals(null, StringUtils.center(null, -1, " "));
1428: assertEquals(null, StringUtils.center(null, 4, " "));
1429: assertEquals(" ", StringUtils.center("", 4, " "));
1430: assertEquals("ab", StringUtils.center("ab", 0, " "));
1431: assertEquals("ab", StringUtils.center("ab", -1, " "));
1432: assertEquals("ab", StringUtils.center("ab", 1, " "));
1433: assertEquals(" ", StringUtils.center("", 4, " "));
1434: assertEquals(" ab ", StringUtils.center("ab", 4, " "));
1435: assertEquals("abcd", StringUtils.center("abcd", 2, " "));
1436: assertEquals(" a ", StringUtils.center("a", 4, " "));
1437: assertEquals("yayz", StringUtils.center("a", 4, "yz"));
1438: assertEquals("yzyayzy", StringUtils.center("a", 7, "yz"));
1439: assertEquals(" abc ", StringUtils.center("abc", 7, null));
1440: assertEquals(" abc ", StringUtils.center("abc", 7, ""));
1441: }
1442:
1443: //-----------------------------------------------------------------------
1444: public void testReverse_String() {
1445: assertEquals(null, StringUtils.reverse(null));
1446: assertEquals("", StringUtils.reverse(""));
1447: assertEquals("sdrawkcab", StringUtils.reverse("backwards"));
1448: }
1449:
1450: public void testReverseDelimited_StringChar() {
1451: assertEquals(null, StringUtils.reverseDelimited(null, '.'));
1452: assertEquals("", StringUtils.reverseDelimited("", '.'));
1453: assertEquals("c.b.a", StringUtils
1454: .reverseDelimited("a.b.c", '.'));
1455: assertEquals("a b c", StringUtils
1456: .reverseDelimited("a b c", '.'));
1457: assertEquals("", StringUtils.reverseDelimited("", '.'));
1458: }
1459:
1460: public void testReverseDelimitedString_StringString() {
1461: assertEquals(null, StringUtils.reverseDelimitedString(null,
1462: null));
1463: assertEquals("", StringUtils.reverseDelimitedString("", null));
1464: assertEquals("", StringUtils.reverseDelimitedString("", "."));
1465: assertEquals("a.b.c", StringUtils.reverseDelimitedString(
1466: "a.b.c", null));
1467: assertEquals("c b a", StringUtils.reverseDelimitedString(
1468: "a b c", null));
1469: assertEquals("c.b.a", StringUtils.reverseDelimitedString(
1470: "a.b.c", "."));
1471: }
1472:
1473: //-----------------------------------------------------------------------
1474: public void testDefault_String() {
1475: assertEquals("", StringUtils.defaultString(null));
1476: assertEquals("", StringUtils.defaultString(""));
1477: assertEquals("abc", StringUtils.defaultString("abc"));
1478: }
1479:
1480: public void testDefault_StringString() {
1481: assertEquals("NULL", StringUtils.defaultString(null, "NULL"));
1482: assertEquals("", StringUtils.defaultString("", "NULL"));
1483: assertEquals("abc", StringUtils.defaultString("abc", "NULL"));
1484: }
1485:
1486: public void testDefaultIfEmpty_StringString() {
1487: assertEquals("NULL", StringUtils.defaultIfEmpty(null, "NULL"));
1488: assertEquals("NULL", StringUtils.defaultIfEmpty("", "NULL"));
1489: assertEquals("abc", StringUtils.defaultIfEmpty("abc", "NULL"));
1490: }
1491:
1492: //-----------------------------------------------------------------------
1493: public void testEscapeFunctions_String() {
1494: assertEquals("", StringUtils.escape(""));
1495: assertEquals("abc", StringUtils.escape("abc"));
1496: assertEquals("\\t", StringUtils.escape("\t"));
1497: assertEquals("\\\\", StringUtils.escape("\\"));
1498: assertEquals("\\\\\\b\\t\\r", StringUtils.escape("\\\b\t\r"));
1499: assertEquals("\\u1234", StringUtils.escape("\u1234"));
1500: assertEquals("\\u0234", StringUtils.escape("\u0234"));
1501: assertEquals("\\u00FD", StringUtils.escape("\u00fd"));
1502: }
1503:
1504: //-----------------------------------------------------------------------
1505: public void testAbbreviate_StringInt() {
1506: assertEquals(null, StringUtils.abbreviate(null, 10));
1507: assertEquals("", StringUtils.abbreviate("", 10));
1508: assertEquals("short", StringUtils.abbreviate("short", 10));
1509: assertEquals(
1510: "Now is ...",
1511: StringUtils
1512: .abbreviate(
1513: "Now is the time for all good men to come to the aid of their party.",
1514: 10));
1515:
1516: String raspberry = "raspberry peach";
1517: assertEquals("raspberry p...", StringUtils.abbreviate(
1518: raspberry, 14));
1519: assertEquals("raspberry peach", StringUtils.abbreviate(
1520: "raspberry peach", 15));
1521: assertEquals("raspberry peach", StringUtils.abbreviate(
1522: "raspberry peach", 16));
1523: assertEquals("abc...", StringUtils.abbreviate("abcdefg", 6));
1524: assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 7));
1525: assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 8));
1526: assertEquals("a...", StringUtils.abbreviate("abcdefg", 4));
1527: assertEquals("", StringUtils.abbreviate("", 4));
1528:
1529: try {
1530: String res = StringUtils.abbreviate("abc", 3);
1531: fail("StringUtils.abbreviate expecting IllegalArgumentException");
1532: } catch (IllegalArgumentException ex) {
1533: // empty
1534: }
1535: }
1536:
1537: public void testAbbreviate_StringIntInt() {
1538: assertEquals(null, StringUtils.abbreviate(null, 10, 12));
1539: assertEquals("", StringUtils.abbreviate("", 0, 10));
1540: assertEquals("", StringUtils.abbreviate("", 2, 10));
1541:
1542: try {
1543: String res = StringUtils.abbreviate("abcdefghij", 0, 3);
1544: fail("StringUtils.abbreviate expecting IllegalArgumentException");
1545: } catch (IllegalArgumentException ex) {
1546: // empty
1547: }
1548: try {
1549: String res = StringUtils.abbreviate("abcdefghij", 5, 6);
1550: fail("StringUtils.abbreviate expecting IllegalArgumentException");
1551: } catch (IllegalArgumentException ex) {
1552: // empty
1553: }
1554:
1555: String raspberry = "raspberry peach";
1556: assertEquals("raspberry peach", StringUtils.abbreviate(
1557: raspberry, 11, 15));
1558:
1559: assertEquals(null, StringUtils.abbreviate(null, 7, 14));
1560: assertAbbreviateWithOffset("abcdefg...", -1, 10);
1561: assertAbbreviateWithOffset("abcdefg...", 0, 10);
1562: assertAbbreviateWithOffset("abcdefg...", 1, 10);
1563: assertAbbreviateWithOffset("abcdefg...", 2, 10);
1564: assertAbbreviateWithOffset("abcdefg...", 3, 10);
1565: assertAbbreviateWithOffset("abcdefg...", 4, 10);
1566: assertAbbreviateWithOffset("...fghi...", 5, 10);
1567: assertAbbreviateWithOffset("...ghij...", 6, 10);
1568: assertAbbreviateWithOffset("...hijk...", 7, 10);
1569: assertAbbreviateWithOffset("...ijklmno", 8, 10);
1570: assertAbbreviateWithOffset("...ijklmno", 9, 10);
1571: assertAbbreviateWithOffset("...ijklmno", 10, 10);
1572: assertAbbreviateWithOffset("...ijklmno", 10, 10);
1573: assertAbbreviateWithOffset("...ijklmno", 11, 10);
1574: assertAbbreviateWithOffset("...ijklmno", 12, 10);
1575: assertAbbreviateWithOffset("...ijklmno", 13, 10);
1576: assertAbbreviateWithOffset("...ijklmno", 14, 10);
1577: assertAbbreviateWithOffset("...ijklmno", 15, 10);
1578: assertAbbreviateWithOffset("...ijklmno", 16, 10);
1579: assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
1580: }
1581:
1582: private void assertAbbreviateWithOffset(String expected,
1583: int offset, int maxWidth) {
1584: String abcdefghijklmno = "abcdefghijklmno";
1585: String message = "abbreviate(String,int,int) failed";
1586: String actual = StringUtils.abbreviate(abcdefghijklmno, offset,
1587: maxWidth);
1588: if (offset >= 0 && offset < abcdefghijklmno.length()) {
1589: assertTrue(message + " -- should contain offset character",
1590: actual.indexOf((char) ('a' + offset)) != -1);
1591: }
1592: assertTrue(message + " -- should not be greater than maxWidth",
1593: actual.length() <= maxWidth);
1594: assertEquals(message, expected, actual);
1595: }
1596:
1597: //-----------------------------------------------------------------------
1598: public void testDifference_StringString() {
1599: assertEquals(null, StringUtils.difference(null, null));
1600: assertEquals("", StringUtils.difference("", ""));
1601: assertEquals("abc", StringUtils.difference("", "abc"));
1602: assertEquals("", StringUtils.difference("abc", ""));
1603: assertEquals("i am a robot", StringUtils.difference(null,
1604: "i am a robot"));
1605: assertEquals("i am a machine", StringUtils.difference(
1606: "i am a machine", null));
1607: assertEquals("robot", StringUtils.difference("i am a machine",
1608: "i am a robot"));
1609: assertEquals("", StringUtils.difference("abc", "abc"));
1610: assertEquals("you are a robot", StringUtils.difference(
1611: "i am a robot", "you are a robot"));
1612: }
1613:
1614: public void testDifferenceAt_StringString() {
1615: assertEquals(-1, StringUtils.indexOfDifference(null, null));
1616: assertEquals(0, StringUtils.indexOfDifference(null,
1617: "i am a robot"));
1618: assertEquals(-1, StringUtils.indexOfDifference("", ""));
1619: assertEquals(0, StringUtils.indexOfDifference("", "abc"));
1620: assertEquals(0, StringUtils.indexOfDifference("abc", ""));
1621: assertEquals(0, StringUtils.indexOfDifference("i am a machine",
1622: null));
1623: assertEquals(7, StringUtils.indexOfDifference("i am a machine",
1624: "i am a robot"));
1625: assertEquals(-1, StringUtils.indexOfDifference("foo", "foo"));
1626: assertEquals(0, StringUtils.indexOfDifference("i am a robot",
1627: "you are a robot"));
1628: //System.out.println("indexOfDiff: " + StringUtils.indexOfDifference("i am a robot", "not machine"));
1629: }
1630:
1631: //-----------------------------------------------------------------------
1632: public void testGetLevenshteinDistance_StringString() {
1633: assertEquals(0, StringUtils.getLevenshteinDistance("", ""));
1634: assertEquals(1, StringUtils.getLevenshteinDistance("", "a"));
1635: assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp",
1636: ""));
1637: assertEquals(1, StringUtils.getLevenshteinDistance("frog",
1638: "fog"));
1639: assertEquals(3, StringUtils
1640: .getLevenshteinDistance("fly", "ant"));
1641: assertEquals(7, StringUtils.getLevenshteinDistance("elephant",
1642: "hippo"));
1643: assertEquals(7, StringUtils.getLevenshteinDistance("hippo",
1644: "elephant"));
1645: assertEquals(8, StringUtils.getLevenshteinDistance("hippo",
1646: "zzzzzzzz"));
1647: assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz",
1648: "hippo"));
1649: assertEquals(1, StringUtils.getLevenshteinDistance("hello",
1650: "hallo"));
1651: try {
1652: int d = StringUtils.getLevenshteinDistance("a", null);
1653: fail("expecting IllegalArgumentException");
1654: } catch (IllegalArgumentException ex) {
1655: // empty
1656: }
1657: try {
1658: int d = StringUtils.getLevenshteinDistance(null, "a");
1659: fail("expecting IllegalArgumentException");
1660: } catch (IllegalArgumentException ex) {
1661: // empty
1662: }
1663: }
1664:
1665: /**
1666: * A sanity check for {@link StringUtils.EMPTY}.
1667: */
1668: public void testEMPTY() {
1669: assertNotNull(StringUtils.EMPTY);
1670: assertEquals("", StringUtils.EMPTY);
1671: }
1672:
1673: public void testRemoveStart() {
1674: // StringUtils.removeStart("", *) = ""
1675: assertNull(StringUtils.removeStart(null, null));
1676: assertNull(StringUtils.removeStart(null, ""));
1677: assertNull(StringUtils.removeStart(null, "a"));
1678:
1679: // StringUtils.removeStart(*, null) = *
1680: assertEquals(StringUtils.removeStart("", null), "");
1681: assertEquals(StringUtils.removeStart("", ""), "");
1682: assertEquals(StringUtils.removeStart("", "a"), "");
1683:
1684: // All others:
1685: assertEquals(StringUtils.removeStart("www.domain.com", "www."),
1686: "domain.com");
1687: assertEquals(StringUtils.removeStart("domain.com", "www."),
1688: "domain.com");
1689: assertEquals(StringUtils.removeStart("domain.com", ""),
1690: "domain.com");
1691: assertEquals(StringUtils.removeStart("domain.com", null),
1692: "domain.com");
1693: }
1694:
1695: public void testRemoveEnd() {
1696: // StringUtils.removeEnd("", *) = ""
1697: assertNull(StringUtils.removeEnd(null, null));
1698: assertNull(StringUtils.removeEnd(null, ""));
1699: assertNull(StringUtils.removeEnd(null, "a"));
1700:
1701: // StringUtils.removeEnd(*, null) = *
1702: assertEquals(StringUtils.removeEnd("", null), "");
1703: assertEquals(StringUtils.removeEnd("", ""), "");
1704: assertEquals(StringUtils.removeEnd("", "a"), "");
1705:
1706: // All others:
1707: assertEquals(StringUtils.removeEnd("www.domain.com.", ".com"),
1708: "www.domain.com.");
1709: assertEquals(StringUtils.removeEnd("www.domain.com", ".com"),
1710: "www.domain");
1711: assertEquals(StringUtils.removeEnd("www.domain", ".com"),
1712: "www.domain");
1713: assertEquals(StringUtils.removeEnd("domain.com", ""),
1714: "domain.com");
1715: assertEquals(StringUtils.removeEnd("domain.com", null),
1716: "domain.com");
1717: }
1718:
1719: public void testRemove_String() {
1720: // StringUtils.remove(null, *) = null
1721: assertEquals(null, StringUtils.remove(null, null));
1722: assertEquals(null, StringUtils.remove(null, ""));
1723: assertEquals(null, StringUtils.remove(null, "a"));
1724:
1725: // StringUtils.remove("", *) = ""
1726: assertEquals("", StringUtils.remove("", null));
1727: assertEquals("", StringUtils.remove("", ""));
1728: assertEquals("", StringUtils.remove("", "a"));
1729:
1730: // StringUtils.remove(*, null) = *
1731: assertEquals(null, StringUtils.remove(null, null));
1732: assertEquals("", StringUtils.remove("", null));
1733: assertEquals("a", StringUtils.remove("a", null));
1734:
1735: // StringUtils.remove(*, "") = *
1736: assertEquals(null, StringUtils.remove(null, ""));
1737: assertEquals("", StringUtils.remove("", ""));
1738: assertEquals("a", StringUtils.remove("a", ""));
1739:
1740: // StringUtils.remove("queued", "ue") = "qd"
1741: assertEquals("qd", StringUtils.remove("queued", "ue"));
1742:
1743: // StringUtils.remove("queued", "zz") = "queued"
1744: assertEquals("queued", StringUtils.remove("queued", "zz"));
1745: }
1746:
1747: public void testRemove_char() {
1748: // StringUtils.remove(null, *) = null
1749: assertEquals(null, StringUtils.remove(null, 'a'));
1750: assertEquals(null, StringUtils.remove(null, 'a'));
1751: assertEquals(null, StringUtils.remove(null, 'a'));
1752:
1753: // StringUtils.remove("", *) = ""
1754: assertEquals("", StringUtils.remove("", 'a'));
1755: assertEquals("", StringUtils.remove("", 'a'));
1756: assertEquals("", StringUtils.remove("", 'a'));
1757:
1758: // StringUtils.remove("queued", 'u') = "qeed"
1759: assertEquals("qeed", StringUtils.remove("queued", 'u'));
1760:
1761: // StringUtils.remove("queued", 'z') = "queued"
1762: assertEquals("queued", StringUtils.remove("queued", 'z'));
1763: }
1764:
1765: }
|