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:
0018: package org.apache.commons.lang.text;
0019:
0020: import java.util.Arrays;
0021: import java.util.Collection;
0022: import java.util.Collections;
0023: import java.util.Iterator;
0024:
0025: import junit.framework.Test;
0026: import junit.framework.TestCase;
0027: import junit.framework.TestSuite;
0028: import junit.textui.TestRunner;
0029:
0030: import org.apache.commons.lang.SystemUtils;
0031:
0032: /**
0033: * Unit tests for {@link org.apache.commons.lang.text.StrBuilder}.
0034: *
0035: * @version $Id: StrBuilderAppendInsertTest.java 492369 2007-01-04 00:35:45Z scolebourne $
0036: */
0037: public class StrBuilderAppendInsertTest extends TestCase {
0038:
0039: /** The system line separator. */
0040: private static final String SEP = SystemUtils.LINE_SEPARATOR;
0041:
0042: /** Test subclass of Object, with a toString method. */
0043: private static Object FOO = new Object() {
0044: public String toString() {
0045: return "foo";
0046: }
0047: };
0048:
0049: /**
0050: * Main method.
0051: *
0052: * @param args command line arguments, ignored
0053: */
0054: public static void main(String[] args) {
0055: TestRunner.run(suite());
0056: }
0057:
0058: /**
0059: * Return a new test suite containing this test case.
0060: *
0061: * @return a new test suite containing this test case
0062: */
0063: public static Test suite() {
0064: TestSuite suite = new TestSuite(
0065: StrBuilderAppendInsertTest.class);
0066: suite.setName("StrBuilder Tests");
0067: return suite;
0068: }
0069:
0070: /**
0071: * Create a new test case with the specified name.
0072: *
0073: * @param name the name
0074: */
0075: public StrBuilderAppendInsertTest(String name) {
0076: super (name);
0077: }
0078:
0079: //-----------------------------------------------------------------------
0080: public void testAppendNewLine() {
0081: StrBuilder sb = new StrBuilder("---");
0082: sb.appendNewLine().append("+++");
0083: assertEquals("---" + SEP + "+++", sb.toString());
0084:
0085: sb = new StrBuilder("---");
0086: sb.setNewLineText("#").appendNewLine().setNewLineText(null)
0087: .appendNewLine();
0088: assertEquals("---#" + SEP, sb.toString());
0089: }
0090:
0091: //-----------------------------------------------------------------------
0092: public void testAppendWithNullText() {
0093: StrBuilder sb = new StrBuilder();
0094: sb.setNullText("NULL");
0095: assertEquals("", sb.toString());
0096:
0097: sb.appendNull();
0098: assertEquals("NULL", sb.toString());
0099:
0100: sb.append((Object) null);
0101: assertEquals("NULLNULL", sb.toString());
0102:
0103: sb.append(FOO);
0104: assertEquals("NULLNULLfoo", sb.toString());
0105:
0106: sb.append((String) null);
0107: assertEquals("NULLNULLfooNULL", sb.toString());
0108:
0109: sb.append("");
0110: assertEquals("NULLNULLfooNULL", sb.toString());
0111:
0112: sb.append("bar");
0113: assertEquals("NULLNULLfooNULLbar", sb.toString());
0114:
0115: sb.append((StringBuffer) null);
0116: assertEquals("NULLNULLfooNULLbarNULL", sb.toString());
0117:
0118: sb.append(new StringBuffer("baz"));
0119: assertEquals("NULLNULLfooNULLbarNULLbaz", sb.toString());
0120: }
0121:
0122: //-----------------------------------------------------------------------
0123: public void testAppend_Object() {
0124: StrBuilder sb = new StrBuilder();
0125: sb.appendNull();
0126: assertEquals("", sb.toString());
0127:
0128: sb.append((Object) null);
0129: assertEquals("", sb.toString());
0130:
0131: sb.append(FOO);
0132: assertEquals("foo", sb.toString());
0133:
0134: sb.append((StringBuffer) null);
0135: assertEquals("foo", sb.toString());
0136:
0137: sb.append(new StringBuffer("baz"));
0138: assertEquals("foobaz", sb.toString());
0139:
0140: sb.append(new StrBuilder("yes"));
0141: assertEquals("foobazyes", sb.toString());
0142: }
0143:
0144: //-----------------------------------------------------------------------
0145: public void testAppend_String() {
0146: StrBuilder sb = new StrBuilder();
0147: sb.setNullText("NULL").append((String) null);
0148: assertEquals("NULL", sb.toString());
0149:
0150: sb = new StrBuilder();
0151: sb.append("foo");
0152: assertEquals("foo", sb.toString());
0153:
0154: sb.append("");
0155: assertEquals("foo", sb.toString());
0156:
0157: sb.append("bar");
0158: assertEquals("foobar", sb.toString());
0159: }
0160:
0161: //-----------------------------------------------------------------------
0162: public void testAppend_String_int_int() {
0163: StrBuilder sb = new StrBuilder();
0164: sb.setNullText("NULL").append((String) null, 0, 1);
0165: assertEquals("NULL", sb.toString());
0166:
0167: sb = new StrBuilder();
0168: sb.append("foo", 0, 3);
0169: assertEquals("foo", sb.toString());
0170:
0171: try {
0172: sb.append("bar", -1, 1);
0173: fail("append(char[], -1,) expected IndexOutOfBoundsException");
0174: } catch (IndexOutOfBoundsException e) {
0175: // expected
0176: }
0177:
0178: try {
0179: sb.append("bar", 3, 1);
0180: fail("append(char[], 3,) expected IndexOutOfBoundsException");
0181: } catch (IndexOutOfBoundsException e) {
0182: // expected
0183: }
0184:
0185: try {
0186: sb.append("bar", 1, -1);
0187: fail("append(char[],, -1) expected IndexOutOfBoundsException");
0188: } catch (IndexOutOfBoundsException e) {
0189: // expected
0190: }
0191:
0192: try {
0193: sb.append("bar", 1, 3);
0194: fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
0195: } catch (IndexOutOfBoundsException e) {
0196: // expected
0197: }
0198:
0199: try {
0200: sb.append("bar", -1, 3);
0201: fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
0202: } catch (IndexOutOfBoundsException e) {
0203: // expected
0204: }
0205:
0206: try {
0207: sb.append("bar", 4, 0);
0208: fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
0209: } catch (IndexOutOfBoundsException e) {
0210: // expected
0211: }
0212:
0213: sb.append("bar", 3, 0);
0214: assertEquals("foo", sb.toString());
0215:
0216: sb.append("abcbardef", 3, 3);
0217: assertEquals("foobar", sb.toString());
0218: }
0219:
0220: //-----------------------------------------------------------------------
0221: public void testAppend_StringBuffer() {
0222: StrBuilder sb = new StrBuilder();
0223: sb.setNullText("NULL").append((StringBuffer) null);
0224: assertEquals("NULL", sb.toString());
0225:
0226: sb = new StrBuilder();
0227: sb.append(new StringBuffer("foo"));
0228: assertEquals("foo", sb.toString());
0229:
0230: sb.append(new StringBuffer(""));
0231: assertEquals("foo", sb.toString());
0232:
0233: sb.append(new StringBuffer("bar"));
0234: assertEquals("foobar", sb.toString());
0235: }
0236:
0237: //-----------------------------------------------------------------------
0238: public void testAppend_StringBuffer_int_int() {
0239: StrBuilder sb = new StrBuilder();
0240: sb.setNullText("NULL").append((StringBuffer) null, 0, 1);
0241: assertEquals("NULL", sb.toString());
0242:
0243: sb = new StrBuilder();
0244: sb.append(new StringBuffer("foo"), 0, 3);
0245: assertEquals("foo", sb.toString());
0246:
0247: try {
0248: sb.append(new StringBuffer("bar"), -1, 1);
0249: fail("append(char[], -1,) expected IndexOutOfBoundsException");
0250: } catch (IndexOutOfBoundsException e) {
0251: // expected
0252: }
0253:
0254: try {
0255: sb.append(new StringBuffer("bar"), 3, 1);
0256: fail("append(char[], 3,) expected IndexOutOfBoundsException");
0257: } catch (IndexOutOfBoundsException e) {
0258: // expected
0259: }
0260:
0261: try {
0262: sb.append(new StringBuffer("bar"), 1, -1);
0263: fail("append(char[],, -1) expected IndexOutOfBoundsException");
0264: } catch (IndexOutOfBoundsException e) {
0265: // expected
0266: }
0267:
0268: try {
0269: sb.append(new StringBuffer("bar"), 1, 3);
0270: fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
0271: } catch (IndexOutOfBoundsException e) {
0272: // expected
0273: }
0274:
0275: try {
0276: sb.append(new StringBuffer("bar"), -1, 3);
0277: fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
0278: } catch (IndexOutOfBoundsException e) {
0279: // expected
0280: }
0281:
0282: try {
0283: sb.append(new StringBuffer("bar"), 4, 0);
0284: fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
0285: } catch (IndexOutOfBoundsException e) {
0286: // expected
0287: }
0288:
0289: sb.append(new StringBuffer("bar"), 3, 0);
0290: assertEquals("foo", sb.toString());
0291:
0292: sb.append(new StringBuffer("abcbardef"), 3, 3);
0293: assertEquals("foobar", sb.toString());
0294: }
0295:
0296: //-----------------------------------------------------------------------
0297: public void testAppend_StrBuilder() {
0298: StrBuilder sb = new StrBuilder();
0299: sb.setNullText("NULL").append((StrBuilder) null);
0300: assertEquals("NULL", sb.toString());
0301:
0302: sb = new StrBuilder();
0303: sb.append(new StrBuilder("foo"));
0304: assertEquals("foo", sb.toString());
0305:
0306: sb.append(new StrBuilder(""));
0307: assertEquals("foo", sb.toString());
0308:
0309: sb.append(new StrBuilder("bar"));
0310: assertEquals("foobar", sb.toString());
0311: }
0312:
0313: //-----------------------------------------------------------------------
0314: public void testAppend_StrBuilder_int_int() {
0315: StrBuilder sb = new StrBuilder();
0316: sb.setNullText("NULL").append((StrBuilder) null, 0, 1);
0317: assertEquals("NULL", sb.toString());
0318:
0319: sb = new StrBuilder();
0320: sb.append(new StrBuilder("foo"), 0, 3);
0321: assertEquals("foo", sb.toString());
0322:
0323: try {
0324: sb.append(new StrBuilder("bar"), -1, 1);
0325: fail("append(char[], -1,) expected IndexOutOfBoundsException");
0326: } catch (IndexOutOfBoundsException e) {
0327: // expected
0328: }
0329:
0330: try {
0331: sb.append(new StrBuilder("bar"), 3, 1);
0332: fail("append(char[], 3,) expected IndexOutOfBoundsException");
0333: } catch (IndexOutOfBoundsException e) {
0334: // expected
0335: }
0336:
0337: try {
0338: sb.append(new StrBuilder("bar"), 1, -1);
0339: fail("append(char[],, -1) expected IndexOutOfBoundsException");
0340: } catch (IndexOutOfBoundsException e) {
0341: // expected
0342: }
0343:
0344: try {
0345: sb.append(new StrBuilder("bar"), 1, 3);
0346: fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
0347: } catch (IndexOutOfBoundsException e) {
0348: // expected
0349: }
0350:
0351: try {
0352: sb.append(new StrBuilder("bar"), -1, 3);
0353: fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
0354: } catch (IndexOutOfBoundsException e) {
0355: // expected
0356: }
0357:
0358: try {
0359: sb.append(new StrBuilder("bar"), 4, 0);
0360: fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
0361: } catch (IndexOutOfBoundsException e) {
0362: // expected
0363: }
0364:
0365: sb.append(new StrBuilder("bar"), 3, 0);
0366: assertEquals("foo", sb.toString());
0367:
0368: sb.append(new StrBuilder("abcbardef"), 3, 3);
0369: assertEquals("foobar", sb.toString());
0370: }
0371:
0372: //-----------------------------------------------------------------------
0373: public void testAppend_CharArray() {
0374: StrBuilder sb = new StrBuilder();
0375: sb.setNullText("NULL").append((char[]) null);
0376: assertEquals("NULL", sb.toString());
0377:
0378: sb = new StrBuilder();
0379: sb.append(new char[0]);
0380: assertEquals("", sb.toString());
0381:
0382: sb.append(new char[] { 'f', 'o', 'o' });
0383: assertEquals("foo", sb.toString());
0384: }
0385:
0386: //-----------------------------------------------------------------------
0387: public void testAppend_CharArray_int_int() {
0388: StrBuilder sb = new StrBuilder();
0389: sb.setNullText("NULL").append((char[]) null, 0, 1);
0390: assertEquals("NULL", sb.toString());
0391:
0392: sb = new StrBuilder();
0393: sb.append(new char[] { 'f', 'o', 'o' }, 0, 3);
0394: assertEquals("foo", sb.toString());
0395:
0396: try {
0397: sb.append(new char[] { 'b', 'a', 'r' }, -1, 1);
0398: fail("append(char[], -1,) expected IndexOutOfBoundsException");
0399: } catch (IndexOutOfBoundsException e) {
0400: // expected
0401: }
0402:
0403: try {
0404: sb.append(new char[] { 'b', 'a', 'r' }, 3, 1);
0405: fail("append(char[], 3,) expected IndexOutOfBoundsException");
0406: } catch (IndexOutOfBoundsException e) {
0407: // expected
0408: }
0409:
0410: try {
0411: sb.append(new char[] { 'b', 'a', 'r' }, 1, -1);
0412: fail("append(char[],, -1) expected IndexOutOfBoundsException");
0413: } catch (IndexOutOfBoundsException e) {
0414: // expected
0415: }
0416:
0417: try {
0418: sb.append(new char[] { 'b', 'a', 'r' }, 1, 3);
0419: fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
0420: } catch (IndexOutOfBoundsException e) {
0421: // expected
0422: }
0423:
0424: try {
0425: sb.append(new char[] { 'b', 'a', 'r' }, -1, 3);
0426: fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
0427: } catch (IndexOutOfBoundsException e) {
0428: // expected
0429: }
0430:
0431: try {
0432: sb.append(new char[] { 'b', 'a', 'r' }, 4, 0);
0433: fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
0434: } catch (IndexOutOfBoundsException e) {
0435: // expected
0436: }
0437:
0438: sb.append(new char[] { 'b', 'a', 'r' }, 3, 0);
0439: assertEquals("foo", sb.toString());
0440:
0441: sb.append(new char[] { 'a', 'b', 'c', 'b', 'a', 'r', 'd', 'e',
0442: 'f' }, 3, 3);
0443: assertEquals("foobar", sb.toString());
0444: }
0445:
0446: //-----------------------------------------------------------------------
0447: public void testAppend_Boolean() {
0448: StrBuilder sb = new StrBuilder();
0449: sb.append(true);
0450: assertEquals("true", sb.toString());
0451:
0452: sb.append(false);
0453: assertEquals("truefalse", sb.toString());
0454:
0455: sb.append('!');
0456: assertEquals("truefalse!", sb.toString());
0457: }
0458:
0459: //-----------------------------------------------------------------------
0460: public void testAppend_PrimitiveNumber() {
0461: StrBuilder sb = new StrBuilder();
0462: sb.append(0);
0463: assertEquals("0", sb.toString());
0464:
0465: sb.append(1L);
0466: assertEquals("01", sb.toString());
0467:
0468: sb.append(2.3f);
0469: assertEquals("012.3", sb.toString());
0470:
0471: sb.append(4.5d);
0472: assertEquals("012.34.5", sb.toString());
0473: }
0474:
0475: //-----------------------------------------------------------------------
0476: public void testAppendln_Object() {
0477: StrBuilder sb = new StrBuilder();
0478: sb.appendln((Object) null);
0479: assertEquals("" + SEP, sb.toString());
0480:
0481: sb.appendln((Object) FOO);
0482: assertEquals(SEP + "foo" + SEP, sb.toString());
0483:
0484: sb.appendln(new Integer(6));
0485: assertEquals(SEP + "foo" + SEP + "6" + SEP, sb.toString());
0486: }
0487:
0488: //-----------------------------------------------------------------------
0489: public void testAppendln_String() {
0490: final int[] count = new int[2];
0491: StrBuilder sb = new StrBuilder() {
0492: public StrBuilder append(String str) {
0493: count[0]++;
0494: return super .append(str);
0495: }
0496:
0497: public StrBuilder appendNewLine() {
0498: count[1]++;
0499: return super .appendNewLine();
0500: }
0501: };
0502: sb.appendln("foo");
0503: assertEquals("foo" + SEP, sb.toString());
0504: assertEquals(2, count[0]); // appendNewLine() calls append(String)
0505: assertEquals(1, count[1]);
0506: }
0507:
0508: //-----------------------------------------------------------------------
0509: public void testAppendln_String_int_int() {
0510: final int[] count = new int[2];
0511: StrBuilder sb = new StrBuilder() {
0512: public StrBuilder append(String str, int startIndex,
0513: int length) {
0514: count[0]++;
0515: return super .append(str, startIndex, length);
0516: }
0517:
0518: public StrBuilder appendNewLine() {
0519: count[1]++;
0520: return super .appendNewLine();
0521: }
0522: };
0523: sb.appendln("foo", 0, 3);
0524: assertEquals("foo" + SEP, sb.toString());
0525: assertEquals(1, count[0]);
0526: assertEquals(1, count[1]);
0527: }
0528:
0529: //-----------------------------------------------------------------------
0530: public void testAppendln_StringBuffer() {
0531: final int[] count = new int[2];
0532: StrBuilder sb = new StrBuilder() {
0533: public StrBuilder append(StringBuffer str) {
0534: count[0]++;
0535: return super .append(str);
0536: }
0537:
0538: public StrBuilder appendNewLine() {
0539: count[1]++;
0540: return super .appendNewLine();
0541: }
0542: };
0543: sb.appendln(new StringBuffer("foo"));
0544: assertEquals("foo" + SEP, sb.toString());
0545: assertEquals(1, count[0]);
0546: assertEquals(1, count[1]);
0547: }
0548:
0549: //-----------------------------------------------------------------------
0550: public void testAppendln_StringBuffer_int_int() {
0551: final int[] count = new int[2];
0552: StrBuilder sb = new StrBuilder() {
0553: public StrBuilder append(StringBuffer str, int startIndex,
0554: int length) {
0555: count[0]++;
0556: return super .append(str, startIndex, length);
0557: }
0558:
0559: public StrBuilder appendNewLine() {
0560: count[1]++;
0561: return super .appendNewLine();
0562: }
0563: };
0564: sb.appendln(new StringBuffer("foo"), 0, 3);
0565: assertEquals("foo" + SEP, sb.toString());
0566: assertEquals(1, count[0]);
0567: assertEquals(1, count[1]);
0568: }
0569:
0570: //-----------------------------------------------------------------------
0571: public void testAppendln_StrBuilder() {
0572: final int[] count = new int[2];
0573: StrBuilder sb = new StrBuilder() {
0574: public StrBuilder append(StrBuilder str) {
0575: count[0]++;
0576: return super .append(str);
0577: }
0578:
0579: public StrBuilder appendNewLine() {
0580: count[1]++;
0581: return super .appendNewLine();
0582: }
0583: };
0584: sb.appendln(new StrBuilder("foo"));
0585: assertEquals("foo" + SEP, sb.toString());
0586: assertEquals(1, count[0]);
0587: assertEquals(1, count[1]);
0588: }
0589:
0590: //-----------------------------------------------------------------------
0591: public void testAppendln_StrBuilder_int_int() {
0592: final int[] count = new int[2];
0593: StrBuilder sb = new StrBuilder() {
0594: public StrBuilder append(StrBuilder str, int startIndex,
0595: int length) {
0596: count[0]++;
0597: return super .append(str, startIndex, length);
0598: }
0599:
0600: public StrBuilder appendNewLine() {
0601: count[1]++;
0602: return super .appendNewLine();
0603: }
0604: };
0605: sb.appendln(new StrBuilder("foo"), 0, 3);
0606: assertEquals("foo" + SEP, sb.toString());
0607: assertEquals(1, count[0]);
0608: assertEquals(1, count[1]);
0609: }
0610:
0611: //-----------------------------------------------------------------------
0612: public void testAppendln_CharArray() {
0613: final int[] count = new int[2];
0614: StrBuilder sb = new StrBuilder() {
0615: public StrBuilder append(char[] str) {
0616: count[0]++;
0617: return super .append(str);
0618: }
0619:
0620: public StrBuilder appendNewLine() {
0621: count[1]++;
0622: return super .appendNewLine();
0623: }
0624: };
0625: sb.appendln("foo".toCharArray());
0626: assertEquals("foo" + SEP, sb.toString());
0627: assertEquals(1, count[0]);
0628: assertEquals(1, count[1]);
0629: }
0630:
0631: //-----------------------------------------------------------------------
0632: public void testAppendln_CharArray_int_int() {
0633: final int[] count = new int[2];
0634: StrBuilder sb = new StrBuilder() {
0635: public StrBuilder append(char[] str, int startIndex,
0636: int length) {
0637: count[0]++;
0638: return super .append(str, startIndex, length);
0639: }
0640:
0641: public StrBuilder appendNewLine() {
0642: count[1]++;
0643: return super .appendNewLine();
0644: }
0645: };
0646: sb.appendln("foo".toCharArray(), 0, 3);
0647: assertEquals("foo" + SEP, sb.toString());
0648: assertEquals(1, count[0]);
0649: assertEquals(1, count[1]);
0650: }
0651:
0652: //-----------------------------------------------------------------------
0653: public void testAppendln_Boolean() {
0654: StrBuilder sb = new StrBuilder();
0655: sb.appendln(true);
0656: assertEquals("true" + SEP, sb.toString());
0657:
0658: sb.clear();
0659: sb.appendln(false);
0660: assertEquals("false" + SEP, sb.toString());
0661: }
0662:
0663: //-----------------------------------------------------------------------
0664: public void testAppendln_PrimitiveNumber() {
0665: StrBuilder sb = new StrBuilder();
0666: sb.appendln(0);
0667: assertEquals("0" + SEP, sb.toString());
0668:
0669: sb.clear();
0670: sb.appendln(1L);
0671: assertEquals("1" + SEP, sb.toString());
0672:
0673: sb.clear();
0674: sb.appendln(2.3f);
0675: assertEquals("2.3" + SEP, sb.toString());
0676:
0677: sb.clear();
0678: sb.appendln(4.5d);
0679: assertEquals("4.5" + SEP, sb.toString());
0680: }
0681:
0682: //-----------------------------------------------------------------------
0683: public void testAppendPadding() {
0684: StrBuilder sb = new StrBuilder();
0685: sb.append("foo");
0686: assertEquals("foo", sb.toString());
0687:
0688: sb.appendPadding(-1, '-');
0689: assertEquals("foo", sb.toString());
0690:
0691: sb.appendPadding(0, '-');
0692: assertEquals("foo", sb.toString());
0693:
0694: sb.appendPadding(1, '-');
0695: assertEquals("foo-", sb.toString());
0696:
0697: sb.appendPadding(16, '-');
0698: assertEquals(20, sb.length());
0699: // 12345678901234567890
0700: assertEquals("foo-----------------", sb.toString());
0701: }
0702:
0703: //-----------------------------------------------------------------------
0704: public void testAppendFixedWidthPadLeft() {
0705: StrBuilder sb = new StrBuilder();
0706: sb.appendFixedWidthPadLeft("foo", -1, '-');
0707: assertEquals("", sb.toString());
0708:
0709: sb.clear();
0710: sb.appendFixedWidthPadLeft("foo", 0, '-');
0711: assertEquals("", sb.toString());
0712:
0713: sb.clear();
0714: sb.appendFixedWidthPadLeft("foo", 1, '-');
0715: assertEquals("o", sb.toString());
0716:
0717: sb.clear();
0718: sb.appendFixedWidthPadLeft("foo", 2, '-');
0719: assertEquals("oo", sb.toString());
0720:
0721: sb.clear();
0722: sb.appendFixedWidthPadLeft("foo", 3, '-');
0723: assertEquals("foo", sb.toString());
0724:
0725: sb.clear();
0726: sb.appendFixedWidthPadLeft("foo", 4, '-');
0727: assertEquals("-foo", sb.toString());
0728:
0729: sb.clear();
0730: sb.appendFixedWidthPadLeft("foo", 10, '-');
0731: assertEquals(10, sb.length());
0732: // 1234567890
0733: assertEquals("-------foo", sb.toString());
0734:
0735: sb.clear();
0736: sb.setNullText("null");
0737: sb.appendFixedWidthPadLeft(null, 5, '-');
0738: assertEquals("-null", sb.toString());
0739: }
0740:
0741: //-----------------------------------------------------------------------
0742: public void testAppendFixedWidthPadLeft_int() {
0743: StrBuilder sb = new StrBuilder();
0744: sb.appendFixedWidthPadLeft(123, -1, '-');
0745: assertEquals("", sb.toString());
0746:
0747: sb.clear();
0748: sb.appendFixedWidthPadLeft(123, 0, '-');
0749: assertEquals("", sb.toString());
0750:
0751: sb.clear();
0752: sb.appendFixedWidthPadLeft(123, 1, '-');
0753: assertEquals("3", sb.toString());
0754:
0755: sb.clear();
0756: sb.appendFixedWidthPadLeft(123, 2, '-');
0757: assertEquals("23", sb.toString());
0758:
0759: sb.clear();
0760: sb.appendFixedWidthPadLeft(123, 3, '-');
0761: assertEquals("123", sb.toString());
0762:
0763: sb.clear();
0764: sb.appendFixedWidthPadLeft(123, 4, '-');
0765: assertEquals("-123", sb.toString());
0766:
0767: sb.clear();
0768: sb.appendFixedWidthPadLeft(123, 10, '-');
0769: assertEquals(10, sb.length());
0770: // 1234567890
0771: assertEquals("-------123", sb.toString());
0772: }
0773:
0774: //-----------------------------------------------------------------------
0775: public void testAppendFixedWidthPadRight() {
0776: StrBuilder sb = new StrBuilder();
0777: sb.appendFixedWidthPadRight("foo", -1, '-');
0778: assertEquals("", sb.toString());
0779:
0780: sb.clear();
0781: sb.appendFixedWidthPadRight("foo", 0, '-');
0782: assertEquals("", sb.toString());
0783:
0784: sb.clear();
0785: sb.appendFixedWidthPadRight("foo", 1, '-');
0786: assertEquals("f", sb.toString());
0787:
0788: sb.clear();
0789: sb.appendFixedWidthPadRight("foo", 2, '-');
0790: assertEquals("fo", sb.toString());
0791:
0792: sb.clear();
0793: sb.appendFixedWidthPadRight("foo", 3, '-');
0794: assertEquals("foo", sb.toString());
0795:
0796: sb.clear();
0797: sb.appendFixedWidthPadRight("foo", 4, '-');
0798: assertEquals("foo-", sb.toString());
0799:
0800: sb.clear();
0801: sb.appendFixedWidthPadRight("foo", 10, '-');
0802: assertEquals(10, sb.length());
0803: // 1234567890
0804: assertEquals("foo-------", sb.toString());
0805:
0806: sb.clear();
0807: sb.setNullText("null");
0808: sb.appendFixedWidthPadRight(null, 5, '-');
0809: assertEquals("null-", sb.toString());
0810: }
0811:
0812: // See: http://issues.apache.org/jira/browse/LANG-299
0813: public void testLang299() {
0814: StrBuilder sb = new StrBuilder(1);
0815: sb.appendFixedWidthPadRight("foo", 1, '-');
0816: assertEquals("f", sb.toString());
0817: }
0818:
0819: //-----------------------------------------------------------------------
0820: public void testAppendFixedWidthPadRight_int() {
0821: StrBuilder sb = new StrBuilder();
0822: sb.appendFixedWidthPadRight(123, -1, '-');
0823: assertEquals("", sb.toString());
0824:
0825: sb.clear();
0826: sb.appendFixedWidthPadRight(123, 0, '-');
0827: assertEquals("", sb.toString());
0828:
0829: sb.clear();
0830: sb.appendFixedWidthPadRight(123, 1, '-');
0831: assertEquals("1", sb.toString());
0832:
0833: sb.clear();
0834: sb.appendFixedWidthPadRight(123, 2, '-');
0835: assertEquals("12", sb.toString());
0836:
0837: sb.clear();
0838: sb.appendFixedWidthPadRight(123, 3, '-');
0839: assertEquals("123", sb.toString());
0840:
0841: sb.clear();
0842: sb.appendFixedWidthPadRight(123, 4, '-');
0843: assertEquals("123-", sb.toString());
0844:
0845: sb.clear();
0846: sb.appendFixedWidthPadRight(123, 10, '-');
0847: assertEquals(10, sb.length());
0848: // 1234567890
0849: assertEquals("123-------", sb.toString());
0850: }
0851:
0852: //-----------------------------------------------------------------------
0853: public void testAppendAll_Array() {
0854: StrBuilder sb = new StrBuilder();
0855: sb.appendAll((Object[]) null);
0856: assertEquals("", sb.toString());
0857:
0858: sb.clear();
0859: sb.appendAll(new Object[0]);
0860: assertEquals("", sb.toString());
0861:
0862: sb.clear();
0863: sb.appendAll(new Object[] { "foo", "bar", "baz" });
0864: assertEquals("foobarbaz", sb.toString());
0865: }
0866:
0867: //-----------------------------------------------------------------------
0868: public void testAppendAll_Collection() {
0869: StrBuilder sb = new StrBuilder();
0870: sb.appendAll((Collection) null);
0871: assertEquals("", sb.toString());
0872:
0873: sb.clear();
0874: sb.appendAll(Collections.EMPTY_LIST);
0875: assertEquals("", sb.toString());
0876:
0877: sb.clear();
0878: sb.appendAll(Arrays
0879: .asList(new Object[] { "foo", "bar", "baz" }));
0880: assertEquals("foobarbaz", sb.toString());
0881: }
0882:
0883: //-----------------------------------------------------------------------
0884: public void testAppendAll_Iterator() {
0885: StrBuilder sb = new StrBuilder();
0886: sb.appendAll((Iterator) null);
0887: assertEquals("", sb.toString());
0888:
0889: sb.clear();
0890: sb.appendAll(Collections.EMPTY_LIST.iterator());
0891: assertEquals("", sb.toString());
0892:
0893: sb.clear();
0894: sb.appendAll(Arrays
0895: .asList(new Object[] { "foo", "bar", "baz" })
0896: .iterator());
0897: assertEquals("foobarbaz", sb.toString());
0898: }
0899:
0900: //-----------------------------------------------------------------------
0901: public void testAppendWithSeparators_Array() {
0902: StrBuilder sb = new StrBuilder();
0903: sb.appendWithSeparators((Object[]) null, ",");
0904: assertEquals("", sb.toString());
0905:
0906: sb.clear();
0907: sb.appendWithSeparators(new Object[0], ",");
0908: assertEquals("", sb.toString());
0909:
0910: sb.clear();
0911: sb.appendWithSeparators(new Object[] { "foo", "bar", "baz" },
0912: ",");
0913: assertEquals("foo,bar,baz", sb.toString());
0914:
0915: sb.clear();
0916: sb.appendWithSeparators(new Object[] { "foo", "bar", "baz" },
0917: null);
0918: assertEquals("foobarbaz", sb.toString());
0919:
0920: sb.clear();
0921: sb.appendWithSeparators(new Object[] { "foo", null, "baz" },
0922: ",");
0923: assertEquals("foo,,baz", sb.toString());
0924: }
0925:
0926: //-----------------------------------------------------------------------
0927: public void testAppendWithSeparators_Collection() {
0928: StrBuilder sb = new StrBuilder();
0929: sb.appendWithSeparators((Collection) null, ",");
0930: assertEquals("", sb.toString());
0931:
0932: sb.clear();
0933: sb.appendWithSeparators(Collections.EMPTY_LIST, ",");
0934: assertEquals("", sb.toString());
0935:
0936: sb.clear();
0937: sb.appendWithSeparators(Arrays.asList(new Object[] { "foo",
0938: "bar", "baz" }), ",");
0939: assertEquals("foo,bar,baz", sb.toString());
0940:
0941: sb.clear();
0942: sb.appendWithSeparators(Arrays.asList(new Object[] { "foo",
0943: "bar", "baz" }), null);
0944: assertEquals("foobarbaz", sb.toString());
0945:
0946: sb.clear();
0947: sb.appendWithSeparators(Arrays.asList(new Object[] { "foo",
0948: null, "baz" }), ",");
0949: assertEquals("foo,,baz", sb.toString());
0950: }
0951:
0952: //-----------------------------------------------------------------------
0953: public void testAppendWithSeparators_Iterator() {
0954: StrBuilder sb = new StrBuilder();
0955: sb.appendWithSeparators((Iterator) null, ",");
0956: assertEquals("", sb.toString());
0957:
0958: sb.clear();
0959: sb.appendWithSeparators(Collections.EMPTY_LIST.iterator(), ",");
0960: assertEquals("", sb.toString());
0961:
0962: sb.clear();
0963: sb.appendWithSeparators(Arrays.asList(
0964: new Object[] { "foo", "bar", "baz" }).iterator(), ",");
0965: assertEquals("foo,bar,baz", sb.toString());
0966:
0967: sb.clear();
0968: sb.appendWithSeparators(Arrays.asList(
0969: new Object[] { "foo", "bar", "baz" }).iterator(), null);
0970: assertEquals("foobarbaz", sb.toString());
0971:
0972: sb.clear();
0973: sb.appendWithSeparators(Arrays.asList(
0974: new Object[] { "foo", null, "baz" }).iterator(), ",");
0975: assertEquals("foo,,baz", sb.toString());
0976: }
0977:
0978: //-----------------------------------------------------------------------
0979: public void testAppendWithSeparatorsWithNullText() {
0980: StrBuilder sb = new StrBuilder();
0981: sb.setNullText("null");
0982: sb.appendWithSeparators(new Object[] { "foo", null, "baz" },
0983: ",");
0984: assertEquals("foo,null,baz", sb.toString());
0985:
0986: sb.clear();
0987: sb.appendWithSeparators(Arrays.asList(new Object[] { "foo",
0988: null, "baz" }), ",");
0989: assertEquals("foo,null,baz", sb.toString());
0990: }
0991:
0992: //-----------------------------------------------------------------------
0993: public void testAppendSeparator_String() {
0994: StrBuilder sb = new StrBuilder();
0995: sb.appendSeparator(","); // no effect
0996: assertEquals("", sb.toString());
0997: sb.append("foo");
0998: assertEquals("foo", sb.toString());
0999: sb.appendSeparator(",");
1000: assertEquals("foo,", sb.toString());
1001: }
1002:
1003: //-----------------------------------------------------------------------
1004: public void testAppendSeparator_char() {
1005: StrBuilder sb = new StrBuilder();
1006: sb.appendSeparator(','); // no effect
1007: assertEquals("", sb.toString());
1008: sb.append("foo");
1009: assertEquals("foo", sb.toString());
1010: sb.appendSeparator(',');
1011: assertEquals("foo,", sb.toString());
1012: }
1013:
1014: //-----------------------------------------------------------------------
1015: public void testAppendSeparator_String_int() {
1016: StrBuilder sb = new StrBuilder();
1017: sb.appendSeparator(",", 0); // no effect
1018: assertEquals("", sb.toString());
1019: sb.append("foo");
1020: assertEquals("foo", sb.toString());
1021: sb.appendSeparator(",", 1);
1022: assertEquals("foo,", sb.toString());
1023:
1024: sb.appendSeparator(",", -1); // no effect
1025: assertEquals("foo,", sb.toString());
1026: }
1027:
1028: //-----------------------------------------------------------------------
1029: public void testAppendSeparator_char_int() {
1030: StrBuilder sb = new StrBuilder();
1031: sb.appendSeparator(',', 0); // no effect
1032: assertEquals("", sb.toString());
1033: sb.append("foo");
1034: assertEquals("foo", sb.toString());
1035: sb.appendSeparator(',', 1);
1036: assertEquals("foo,", sb.toString());
1037:
1038: sb.appendSeparator(',', -1); // no effect
1039: assertEquals("foo,", sb.toString());
1040: }
1041:
1042: //-----------------------------------------------------------------------
1043: public void testInsert() {
1044:
1045: StrBuilder sb = new StrBuilder();
1046: sb.append("barbaz");
1047: assertEquals("barbaz", sb.toString());
1048:
1049: try {
1050: sb.insert(-1, FOO);
1051: fail("insert(-1, Object) expected StringIndexOutOfBoundsException");
1052: } catch (IndexOutOfBoundsException e) {
1053: // expected
1054: }
1055:
1056: try {
1057: sb.insert(7, FOO);
1058: fail("insert(7, Object) expected StringIndexOutOfBoundsException");
1059: } catch (IndexOutOfBoundsException e) {
1060: // expected
1061: }
1062:
1063: sb.insert(0, (Object) null);
1064: assertEquals("barbaz", sb.toString());
1065:
1066: sb.insert(0, FOO);
1067: assertEquals("foobarbaz", sb.toString());
1068:
1069: sb.clear();
1070: sb.append("barbaz");
1071: assertEquals("barbaz", sb.toString());
1072:
1073: try {
1074: sb.insert(-1, "foo");
1075: fail("insert(-1, String) expected StringIndexOutOfBoundsException");
1076: } catch (IndexOutOfBoundsException e) {
1077: // expected
1078: }
1079:
1080: try {
1081: sb.insert(7, "foo");
1082: fail("insert(7, String) expected StringIndexOutOfBoundsException");
1083: } catch (IndexOutOfBoundsException e) {
1084: // expected
1085: }
1086:
1087: sb.insert(0, (String) null);
1088: assertEquals("barbaz", sb.toString());
1089:
1090: sb.insert(0, "foo");
1091: assertEquals("foobarbaz", sb.toString());
1092:
1093: sb.clear();
1094: sb.append("barbaz");
1095: assertEquals("barbaz", sb.toString());
1096:
1097: try {
1098: sb.insert(-1, new char[] { 'f', 'o', 'o' });
1099: fail("insert(-1, char[]) expected StringIndexOutOfBoundsException");
1100: } catch (IndexOutOfBoundsException e) {
1101: // expected
1102: }
1103:
1104: try {
1105: sb.insert(7, new char[] { 'f', 'o', 'o' });
1106: fail("insert(7, char[]) expected StringIndexOutOfBoundsException");
1107: } catch (IndexOutOfBoundsException e) {
1108: // expected
1109: }
1110:
1111: sb.insert(0, (char[]) null);
1112: assertEquals("barbaz", sb.toString());
1113:
1114: sb.insert(0, new char[0]);
1115: assertEquals("barbaz", sb.toString());
1116:
1117: sb.insert(0, new char[] { 'f', 'o', 'o' });
1118: assertEquals("foobarbaz", sb.toString());
1119:
1120: sb.clear();
1121: sb.append("barbaz");
1122: assertEquals("barbaz", sb.toString());
1123:
1124: try {
1125: sb.insert(-1, new char[] { 'a', 'b', 'c', 'f', 'o', 'o',
1126: 'd', 'e', 'f' }, 3, 3);
1127: fail("insert(-1, char[], 3, 3) expected StringIndexOutOfBoundsException");
1128: } catch (IndexOutOfBoundsException e) {
1129: // expected
1130: }
1131:
1132: try {
1133: sb.insert(7, new char[] { 'a', 'b', 'c', 'f', 'o', 'o',
1134: 'd', 'e', 'f' }, 3, 3);
1135: fail("insert(7, char[], 3, 3) expected StringIndexOutOfBoundsException");
1136: } catch (IndexOutOfBoundsException e) {
1137: // expected
1138: }
1139:
1140: sb.insert(0, (char[]) null, 0, 0);
1141: assertEquals("barbaz", sb.toString());
1142:
1143: sb.insert(0, new char[0], 0, 0);
1144: assertEquals("barbaz", sb.toString());
1145:
1146: try {
1147: sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o',
1148: 'd', 'e', 'f' }, -1, 3);
1149: fail("insert(0, char[], -1, 3) expected StringIndexOutOfBoundsException");
1150: } catch (IndexOutOfBoundsException e) {
1151: // expected
1152: }
1153:
1154: try {
1155: sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o',
1156: 'd', 'e', 'f' }, 10, 3);
1157: fail("insert(0, char[], 10, 3) expected StringIndexOutOfBoundsException");
1158: } catch (IndexOutOfBoundsException e) {
1159: // expected
1160: }
1161:
1162: try {
1163: sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o',
1164: 'd', 'e', 'f' }, 0, -1);
1165: fail("insert(0, char[], 0, -1) expected StringIndexOutOfBoundsException");
1166: } catch (IndexOutOfBoundsException e) {
1167: // expected
1168: }
1169:
1170: try {
1171: sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o',
1172: 'd', 'e', 'f' }, 0, 10);
1173: fail("insert(0, char[], 0, 10) expected StringIndexOutOfBoundsException");
1174: } catch (IndexOutOfBoundsException e) {
1175: // expected
1176: }
1177:
1178: sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o', 'd',
1179: 'e', 'f' }, 0, 0);
1180: assertEquals("barbaz", sb.toString());
1181:
1182: sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o', 'd',
1183: 'e', 'f' }, 3, 3);
1184: assertEquals("foobarbaz", sb.toString());
1185:
1186: sb.clear();
1187: sb.append("barbaz");
1188: assertEquals("barbaz", sb.toString());
1189:
1190: try {
1191: sb.insert(-1, true);
1192: fail("insert(-1, boolean) expected StringIndexOutOfBoundsException");
1193: } catch (IndexOutOfBoundsException e) {
1194: // expected
1195: }
1196:
1197: try {
1198: sb.insert(7, true);
1199: fail("insert(7, boolean) expected StringIndexOutOfBoundsException");
1200: } catch (IndexOutOfBoundsException e) {
1201: // expected
1202: }
1203:
1204: sb.insert(0, true);
1205: assertEquals("truebarbaz", sb.toString());
1206:
1207: sb.insert(0, false);
1208: assertEquals("falsetruebarbaz", sb.toString());
1209:
1210: sb.clear();
1211: sb.append("barbaz");
1212: assertEquals("barbaz", sb.toString());
1213:
1214: try {
1215: sb.insert(-1, '!');
1216: fail("insert(-1, char) expected StringIndexOutOfBoundsException");
1217: } catch (IndexOutOfBoundsException e) {
1218: // expected
1219: }
1220:
1221: try {
1222: sb.insert(7, '!');
1223: fail("insert(7, char) expected StringIndexOutOfBoundsException");
1224: } catch (IndexOutOfBoundsException e) {
1225: // expected
1226: }
1227:
1228: sb.insert(0, '!');
1229: assertEquals("!barbaz", sb.toString());
1230:
1231: sb.clear();
1232: sb.append("barbaz");
1233: assertEquals("barbaz", sb.toString());
1234:
1235: try {
1236: sb.insert(-1, 0);
1237: fail("insert(-1, int) expected StringIndexOutOfBoundsException");
1238: } catch (IndexOutOfBoundsException e) {
1239: // expected
1240: }
1241:
1242: try {
1243: sb.insert(7, 0);
1244: fail("insert(7, int) expected StringIndexOutOfBoundsException");
1245: } catch (IndexOutOfBoundsException e) {
1246: // expected
1247: }
1248:
1249: sb.insert(0, '0');
1250: assertEquals("0barbaz", sb.toString());
1251:
1252: sb.clear();
1253: sb.append("barbaz");
1254: assertEquals("barbaz", sb.toString());
1255:
1256: try {
1257: sb.insert(-1, 1L);
1258: fail("insert(-1, long) expected StringIndexOutOfBoundsException");
1259: } catch (IndexOutOfBoundsException e) {
1260: // expected
1261: }
1262:
1263: try {
1264: sb.insert(7, 1L);
1265: fail("insert(7, long) expected StringIndexOutOfBoundsException");
1266: } catch (IndexOutOfBoundsException e) {
1267: // expected
1268: }
1269:
1270: sb.insert(0, 1L);
1271: assertEquals("1barbaz", sb.toString());
1272:
1273: sb.clear();
1274: sb.append("barbaz");
1275: assertEquals("barbaz", sb.toString());
1276:
1277: try {
1278: sb.insert(-1, 2.3F);
1279: fail("insert(-1, float) expected StringIndexOutOfBoundsException");
1280: } catch (IndexOutOfBoundsException e) {
1281: // expected
1282: }
1283:
1284: try {
1285: sb.insert(7, 2.3F);
1286: fail("insert(7, float) expected StringIndexOutOfBoundsException");
1287: } catch (IndexOutOfBoundsException e) {
1288: // expected
1289: }
1290:
1291: sb.insert(0, 2.3F);
1292: assertEquals("2.3barbaz", sb.toString());
1293:
1294: sb.clear();
1295: sb.append("barbaz");
1296: assertEquals("barbaz", sb.toString());
1297:
1298: try {
1299: sb.insert(-1, 4.5D);
1300: fail("insert(-1, double) expected StringIndexOutOfBoundsException");
1301: } catch (IndexOutOfBoundsException e) {
1302: // expected
1303: }
1304:
1305: try {
1306: sb.insert(7, 4.5D);
1307: fail("insert(7, double) expected StringIndexOutOfBoundsException");
1308: } catch (IndexOutOfBoundsException e) {
1309: // expected
1310: }
1311:
1312: sb.insert(0, 4.5D);
1313: assertEquals("4.5barbaz", sb.toString());
1314: }
1315:
1316: //-----------------------------------------------------------------------
1317: public void testInsertWithNullText() {
1318: StrBuilder sb = new StrBuilder();
1319: sb.setNullText("null");
1320: sb.append("barbaz");
1321: assertEquals("barbaz", sb.toString());
1322:
1323: try {
1324: sb.insert(-1, FOO);
1325: fail("insert(-1, Object) expected StringIndexOutOfBoundsException");
1326: } catch (IndexOutOfBoundsException e) {
1327: // expected
1328: }
1329:
1330: try {
1331: sb.insert(7, FOO);
1332: fail("insert(7, Object) expected StringIndexOutOfBoundsException");
1333: } catch (IndexOutOfBoundsException e) {
1334: // expected
1335: }
1336:
1337: sb.insert(0, (Object) null);
1338: assertEquals("nullbarbaz", sb.toString());
1339:
1340: sb.insert(0, FOO);
1341: assertEquals("foonullbarbaz", sb.toString());
1342:
1343: sb.clear();
1344: sb.append("barbaz");
1345: assertEquals("barbaz", sb.toString());
1346:
1347: try {
1348: sb.insert(-1, "foo");
1349: fail("insert(-1, String) expected StringIndexOutOfBoundsException");
1350: } catch (IndexOutOfBoundsException e) {
1351: // expected
1352: }
1353:
1354: try {
1355: sb.insert(7, "foo");
1356: fail("insert(7, String) expected StringIndexOutOfBoundsException");
1357: } catch (IndexOutOfBoundsException e) {
1358: // expected
1359: }
1360:
1361: sb.insert(0, (String) null);
1362: assertEquals("nullbarbaz", sb.toString());
1363:
1364: sb.insert(0, "foo");
1365: assertEquals("foonullbarbaz", sb.toString());
1366:
1367: sb.insert(0, (char[]) null);
1368: assertEquals("nullfoonullbarbaz", sb.toString());
1369:
1370: sb.insert(0, (char[]) null, 0, 0);
1371: assertEquals("nullnullfoonullbarbaz", sb.toString());
1372: }
1373:
1374: }
|