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.io.Reader;
0021: import java.io.Writer;
0022: import java.util.Arrays;
0023:
0024: import junit.framework.Test;
0025: import junit.framework.TestCase;
0026: import junit.framework.TestSuite;
0027: import junit.textui.TestRunner;
0028:
0029: import org.apache.commons.lang.ArrayUtils;
0030:
0031: /**
0032: * Unit tests for {@link org.apache.commons.lang.text.StrBuilder}.
0033: *
0034: * @author Michael Heuer
0035: * @version $Id: StrBuilderTest.java 469696 2006-10-31 23:00:54Z bayard $
0036: */
0037: public class StrBuilderTest extends TestCase {
0038:
0039: /**
0040: * Main method.
0041: *
0042: * @param args command line arguments, ignored
0043: */
0044: public static void main(String[] args) {
0045: TestRunner.run(suite());
0046: }
0047:
0048: /**
0049: * Return a new test suite containing this test case.
0050: *
0051: * @return a new test suite containing this test case
0052: */
0053: public static Test suite() {
0054: TestSuite suite = new TestSuite(StrBuilderTest.class);
0055: suite.setName("StrBuilder Tests");
0056: return suite;
0057: }
0058:
0059: /**
0060: * Create a new test case with the specified name.
0061: *
0062: * @param name
0063: * name
0064: */
0065: public StrBuilderTest(String name) {
0066: super (name);
0067: }
0068:
0069: //-----------------------------------------------------------------------
0070: public void testConstructors() {
0071: StrBuilder sb0 = new StrBuilder();
0072: assertEquals(32, sb0.capacity());
0073: assertEquals(0, sb0.length());
0074: assertEquals(0, sb0.size());
0075:
0076: StrBuilder sb1 = new StrBuilder(32);
0077: assertEquals(32, sb1.capacity());
0078: assertEquals(0, sb1.length());
0079: assertEquals(0, sb1.size());
0080:
0081: StrBuilder sb2 = new StrBuilder(0);
0082: assertEquals(32, sb2.capacity());
0083: assertEquals(0, sb2.length());
0084: assertEquals(0, sb2.size());
0085:
0086: StrBuilder sb3 = new StrBuilder(-1);
0087: assertEquals(32, sb3.capacity());
0088: assertEquals(0, sb3.length());
0089: assertEquals(0, sb3.size());
0090:
0091: StrBuilder sb4 = new StrBuilder(1);
0092: assertEquals(1, sb4.capacity());
0093: assertEquals(0, sb4.length());
0094: assertEquals(0, sb4.size());
0095:
0096: StrBuilder sb5 = new StrBuilder((String) null);
0097: assertEquals(32, sb5.capacity());
0098: assertEquals(0, sb5.length());
0099: assertEquals(0, sb5.size());
0100:
0101: StrBuilder sb6 = new StrBuilder("");
0102: assertEquals(32, sb6.capacity());
0103: assertEquals(0, sb6.length());
0104: assertEquals(0, sb6.size());
0105:
0106: StrBuilder sb7 = new StrBuilder("foo");
0107: assertEquals(35, sb7.capacity());
0108: assertEquals(3, sb7.length());
0109: assertEquals(3, sb7.size());
0110: }
0111:
0112: //-----------------------------------------------------------------------
0113: public void testChaining() {
0114: StrBuilder sb = new StrBuilder();
0115: assertSame(sb, sb.setNewLineText(null));
0116: assertSame(sb, sb.setNullText(null));
0117: assertSame(sb, sb.setLength(1));
0118: assertSame(sb, sb.setCharAt(0, 'a'));
0119: assertSame(sb, sb.ensureCapacity(0));
0120: assertSame(sb, sb.minimizeCapacity());
0121: assertSame(sb, sb.clear());
0122: assertSame(sb, sb.reverse());
0123: assertSame(sb, sb.trim());
0124: }
0125:
0126: //-----------------------------------------------------------------------
0127: public void testGetSetNewLineText() {
0128: StrBuilder sb = new StrBuilder();
0129: assertEquals(null, sb.getNewLineText());
0130:
0131: sb.setNewLineText("#");
0132: assertEquals("#", sb.getNewLineText());
0133:
0134: sb.setNewLineText("");
0135: assertEquals("", sb.getNewLineText());
0136:
0137: sb.setNewLineText((String) null);
0138: assertEquals(null, sb.getNewLineText());
0139: }
0140:
0141: //-----------------------------------------------------------------------
0142: public void testGetSetNullText() {
0143: StrBuilder sb = new StrBuilder();
0144: assertEquals(null, sb.getNullText());
0145:
0146: sb.setNullText("null");
0147: assertEquals("null", sb.getNullText());
0148:
0149: sb.setNullText("");
0150: assertEquals(null, sb.getNullText());
0151:
0152: sb.setNullText("NULL");
0153: assertEquals("NULL", sb.getNullText());
0154:
0155: sb.setNullText((String) null);
0156: assertEquals(null, sb.getNullText());
0157: }
0158:
0159: //-----------------------------------------------------------------------
0160: public void testCapacityAndLength() {
0161: StrBuilder sb = new StrBuilder();
0162: assertEquals(32, sb.capacity());
0163: assertEquals(0, sb.length());
0164: assertEquals(0, sb.size());
0165: assertTrue(sb.isEmpty());
0166:
0167: sb.minimizeCapacity();
0168: assertEquals(0, sb.capacity());
0169: assertEquals(0, sb.length());
0170: assertEquals(0, sb.size());
0171: assertTrue(sb.isEmpty());
0172:
0173: sb.ensureCapacity(32);
0174: assertTrue(sb.capacity() >= 32);
0175: assertEquals(0, sb.length());
0176: assertEquals(0, sb.size());
0177: assertTrue(sb.isEmpty());
0178:
0179: sb.append("foo");
0180: assertTrue(sb.capacity() >= 32);
0181: assertEquals(3, sb.length());
0182: assertEquals(3, sb.size());
0183: assertTrue(sb.isEmpty() == false);
0184:
0185: sb.clear();
0186: assertTrue(sb.capacity() >= 32);
0187: assertEquals(0, sb.length());
0188: assertEquals(0, sb.size());
0189: assertTrue(sb.isEmpty());
0190:
0191: sb.append("123456789012345678901234567890123");
0192: assertTrue(sb.capacity() > 32);
0193: assertEquals(33, sb.length());
0194: assertEquals(33, sb.size());
0195: assertTrue(sb.isEmpty() == false);
0196:
0197: sb.ensureCapacity(16);
0198: assertTrue(sb.capacity() > 16);
0199: assertEquals(33, sb.length());
0200: assertEquals(33, sb.size());
0201: assertTrue(sb.isEmpty() == false);
0202:
0203: sb.minimizeCapacity();
0204: assertEquals(33, sb.capacity());
0205: assertEquals(33, sb.length());
0206: assertEquals(33, sb.size());
0207: assertTrue(sb.isEmpty() == false);
0208:
0209: try {
0210: sb.setLength(-1);
0211: fail("setLength(-1) expected StringIndexOutOfBoundsException");
0212: } catch (IndexOutOfBoundsException e) {
0213: // expected
0214: }
0215:
0216: sb.setLength(33);
0217: assertEquals(33, sb.capacity());
0218: assertEquals(33, sb.length());
0219: assertEquals(33, sb.size());
0220: assertTrue(sb.isEmpty() == false);
0221:
0222: sb.setLength(16);
0223: assertTrue(sb.capacity() >= 16);
0224: assertEquals(16, sb.length());
0225: assertEquals(16, sb.size());
0226: assertEquals("1234567890123456", sb.toString());
0227: assertTrue(sb.isEmpty() == false);
0228:
0229: sb.setLength(32);
0230: assertTrue(sb.capacity() >= 32);
0231: assertEquals(32, sb.length());
0232: assertEquals(32, sb.size());
0233: assertEquals(
0234: "1234567890123456\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", sb
0235: .toString());
0236: assertTrue(sb.isEmpty() == false);
0237:
0238: sb.setLength(0);
0239: assertTrue(sb.capacity() >= 32);
0240: assertEquals(0, sb.length());
0241: assertEquals(0, sb.size());
0242: assertTrue(sb.isEmpty());
0243: }
0244:
0245: //-----------------------------------------------------------------------
0246: public void testLength() {
0247: StrBuilder sb = new StrBuilder();
0248: assertEquals(0, sb.length());
0249:
0250: sb.append("Hello");
0251: assertEquals(5, sb.length());
0252: }
0253:
0254: public void testSetLength() {
0255: StrBuilder sb = new StrBuilder();
0256: sb.append("Hello");
0257: sb.setLength(2); // shorten
0258: assertEquals("He", sb.toString());
0259: sb.setLength(2); // no change
0260: assertEquals("He", sb.toString());
0261: sb.setLength(3); // lengthen
0262: assertEquals("He\0", sb.toString());
0263:
0264: try {
0265: sb.setLength(-1);
0266: fail("setLength(-1) expected StringIndexOutOfBoundsException");
0267: } catch (IndexOutOfBoundsException e) {
0268: // expected
0269: }
0270: }
0271:
0272: //-----------------------------------------------------------------------
0273: public void testCapacity() {
0274: StrBuilder sb = new StrBuilder();
0275: assertEquals(sb.buffer.length, sb.capacity());
0276:
0277: sb.append("HelloWorldHelloWorldHelloWorldHelloWorld");
0278: assertEquals(sb.buffer.length, sb.capacity());
0279: }
0280:
0281: public void testEnsureCapacity() {
0282: StrBuilder sb = new StrBuilder();
0283: sb.ensureCapacity(2);
0284: assertEquals(true, sb.capacity() >= 2);
0285:
0286: sb.ensureCapacity(-1);
0287: assertEquals(true, sb.capacity() >= 0);
0288:
0289: sb.append("HelloWorld");
0290: sb.ensureCapacity(40);
0291: assertEquals(true, sb.capacity() >= 40);
0292: }
0293:
0294: public void testMinimizeCapacity() {
0295: StrBuilder sb = new StrBuilder();
0296: sb.minimizeCapacity();
0297: assertEquals(0, sb.capacity());
0298:
0299: sb.append("HelloWorld");
0300: sb.minimizeCapacity();
0301: assertEquals(10, sb.capacity());
0302: }
0303:
0304: //-----------------------------------------------------------------------
0305: public void testSize() {
0306: StrBuilder sb = new StrBuilder();
0307: assertEquals(0, sb.size());
0308:
0309: sb.append("Hello");
0310: assertEquals(5, sb.size());
0311: }
0312:
0313: public void testIsEmpty() {
0314: StrBuilder sb = new StrBuilder();
0315: assertEquals(true, sb.isEmpty());
0316:
0317: sb.append("Hello");
0318: assertEquals(false, sb.isEmpty());
0319:
0320: sb.clear();
0321: assertEquals(true, sb.isEmpty());
0322: }
0323:
0324: public void testClear() {
0325: StrBuilder sb = new StrBuilder();
0326: sb.append("Hello");
0327: sb.clear();
0328: assertEquals(0, sb.length());
0329: assertEquals(true, sb.buffer.length >= 5);
0330: }
0331:
0332: //-----------------------------------------------------------------------
0333: public void testCharAt() {
0334: StrBuilder sb = new StrBuilder();
0335: try {
0336: sb.charAt(0);
0337: fail("charAt(0) expected IndexOutOfBoundsException");
0338: } catch (IndexOutOfBoundsException e) {
0339: // expected
0340: }
0341: try {
0342: sb.charAt(-1);
0343: fail("charAt(-1) expected IndexOutOfBoundsException");
0344: } catch (IndexOutOfBoundsException e) {
0345: // expected
0346: }
0347: sb.append("foo");
0348: assertEquals('f', sb.charAt(0));
0349: assertEquals('o', sb.charAt(1));
0350: assertEquals('o', sb.charAt(2));
0351: try {
0352: sb.charAt(-1);
0353: fail("charAt(-1) expected IndexOutOfBoundsException");
0354: } catch (IndexOutOfBoundsException e) {
0355: // expected
0356: }
0357: try {
0358: sb.charAt(3);
0359: fail("charAt(3) expected IndexOutOfBoundsException");
0360: } catch (IndexOutOfBoundsException e) {
0361: // expected
0362: }
0363: }
0364:
0365: //-----------------------------------------------------------------------
0366: public void testSetCharAt() {
0367: StrBuilder sb = new StrBuilder();
0368: try {
0369: sb.setCharAt(0, 'f');
0370: fail("setCharAt(0,) expected IndexOutOfBoundsException");
0371: } catch (IndexOutOfBoundsException e) {
0372: // expected
0373: }
0374: try {
0375: sb.setCharAt(-1, 'f');
0376: fail("setCharAt(-1,) expected IndexOutOfBoundsException");
0377: } catch (IndexOutOfBoundsException e) {
0378: // expected
0379: }
0380: sb.append("foo");
0381: sb.setCharAt(0, 'b');
0382: sb.setCharAt(1, 'a');
0383: sb.setCharAt(2, 'r');
0384: try {
0385: sb.setCharAt(3, '!');
0386: fail("setCharAt(3,) expected IndexOutOfBoundsException");
0387: } catch (IndexOutOfBoundsException e) {
0388: // expected
0389: }
0390: assertEquals("bar", sb.toString());
0391: }
0392:
0393: //-----------------------------------------------------------------------
0394: public void testDeleteCharAt() {
0395: StrBuilder sb = new StrBuilder("abc");
0396: sb.deleteCharAt(0);
0397: assertEquals("bc", sb.toString());
0398:
0399: try {
0400: sb.deleteCharAt(1000);
0401: fail("Expected IndexOutOfBoundsException");
0402: } catch (IndexOutOfBoundsException e) {
0403: }
0404: }
0405:
0406: //-----------------------------------------------------------------------
0407: public void testToCharArray() {
0408: StrBuilder sb = new StrBuilder();
0409: assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, sb.toCharArray());
0410:
0411: char[] a = sb.toCharArray();
0412: assertNotNull("toCharArray() result is null", a);
0413: assertEquals("toCharArray() result is too large", 0, a.length);
0414:
0415: sb.append("junit");
0416: a = sb.toCharArray();
0417: assertEquals("toCharArray() result incorrect length", 5,
0418: a.length);
0419: assertTrue("toCharArray() result does not match", Arrays
0420: .equals("junit".toCharArray(), a));
0421: }
0422:
0423: public void testToCharArrayIntInt() {
0424: StrBuilder sb = new StrBuilder();
0425: assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, sb.toCharArray(0, 0));
0426:
0427: sb.append("junit");
0428: char[] a = sb.toCharArray(0, 20); // too large test
0429: assertEquals("toCharArray(int,int) result incorrect length", 5,
0430: a.length);
0431: assertTrue("toCharArray(int,int) result does not match", Arrays
0432: .equals("junit".toCharArray(), a));
0433:
0434: a = sb.toCharArray(0, 4);
0435: assertEquals("toCharArray(int,int) result incorrect length", 4,
0436: a.length);
0437: assertTrue("toCharArray(int,int) result does not match", Arrays
0438: .equals("juni".toCharArray(), a));
0439:
0440: a = sb.toCharArray(0, 4);
0441: assertEquals("toCharArray(int,int) result incorrect length", 4,
0442: a.length);
0443: assertTrue("toCharArray(int,int) result does not match", Arrays
0444: .equals("juni".toCharArray(), a));
0445:
0446: a = sb.toCharArray(0, 1);
0447: assertNotNull("toCharArray(int,int) result is null", a);
0448:
0449: try {
0450: sb.toCharArray(-1, 5);
0451: fail("no string index out of bound on -1");
0452: } catch (IndexOutOfBoundsException e) {
0453: }
0454:
0455: try {
0456: sb.toCharArray(6, 5);
0457: fail("no string index out of bound on -1");
0458: } catch (IndexOutOfBoundsException e) {
0459: }
0460: }
0461:
0462: public void testGetChars() {
0463: StrBuilder sb = new StrBuilder();
0464:
0465: char[] input = new char[10];
0466: char[] a = sb.getChars(input);
0467: assertSame(input, a);
0468: assertTrue(Arrays.equals(new char[10], a));
0469:
0470: sb.append("junit");
0471: a = sb.getChars(input);
0472: assertSame(input, a);
0473: assertTrue(Arrays.equals(new char[] { 'j', 'u', 'n', 'i', 't',
0474: 0, 0, 0, 0, 0 }, a));
0475:
0476: a = sb.getChars(null);
0477: assertNotSame(input, a);
0478: assertEquals(5, a.length);
0479: assertTrue(Arrays.equals("junit".toCharArray(), a));
0480:
0481: input = new char[5];
0482: a = sb.getChars(input);
0483: assertSame(input, a);
0484:
0485: input = new char[4];
0486: a = sb.getChars(input);
0487: assertNotSame(input, a);
0488: }
0489:
0490: public void testGetCharsIntIntCharArrayInt() {
0491: StrBuilder sb = new StrBuilder();
0492:
0493: sb.append("junit");
0494: char[] a = new char[5];
0495: sb.getChars(0, 5, a, 0);
0496: assertTrue(Arrays.equals(
0497: new char[] { 'j', 'u', 'n', 'i', 't' }, a));
0498:
0499: a = new char[5];
0500: sb.getChars(0, 2, a, 3);
0501: assertTrue(Arrays.equals(new char[] { 0, 0, 0, 'j', 'u' }, a));
0502:
0503: try {
0504: sb.getChars(-1, 0, a, 0);
0505: fail("no exception");
0506: } catch (IndexOutOfBoundsException e) {
0507: }
0508:
0509: try {
0510: sb.getChars(0, -1, a, 0);
0511: fail("no exception");
0512: } catch (IndexOutOfBoundsException e) {
0513: }
0514:
0515: try {
0516: sb.getChars(0, 20, a, 0);
0517: fail("no exception");
0518: } catch (IndexOutOfBoundsException e) {
0519: }
0520:
0521: try {
0522: sb.getChars(4, 2, a, 0);
0523: fail("no exception");
0524: } catch (IndexOutOfBoundsException e) {
0525: }
0526: }
0527:
0528: //-----------------------------------------------------------------------
0529: public void testDeleteIntInt() {
0530: StrBuilder sb = new StrBuilder("abc");
0531: sb.delete(0, 1);
0532: assertEquals("bc", sb.toString());
0533: sb.delete(1, 2);
0534: assertEquals("b", sb.toString());
0535: sb.delete(0, 1);
0536: assertEquals("", sb.toString());
0537: sb.delete(0, 1000);
0538: assertEquals("", sb.toString());
0539:
0540: try {
0541: sb.delete(1, 2);
0542: fail("Expected IndexOutOfBoundsException");
0543: } catch (IndexOutOfBoundsException e) {
0544: }
0545: try {
0546: sb.delete(-1, 1);
0547: fail("Expected IndexOutOfBoundsException");
0548: } catch (IndexOutOfBoundsException e) {
0549: }
0550:
0551: sb = new StrBuilder("anything");
0552: try {
0553: sb.delete(2, 1);
0554: fail("Expected IndexOutOfBoundsException");
0555: } catch (IndexOutOfBoundsException e) {
0556: }
0557: }
0558:
0559: //-----------------------------------------------------------------------
0560: public void testDeleteAll_char() {
0561: StrBuilder sb = new StrBuilder("abcbccba");
0562: sb.deleteAll('X');
0563: assertEquals("abcbccba", sb.toString());
0564: sb.deleteAll('a');
0565: assertEquals("bcbccb", sb.toString());
0566: sb.deleteAll('c');
0567: assertEquals("bbb", sb.toString());
0568: sb.deleteAll('b');
0569: assertEquals("", sb.toString());
0570:
0571: sb = new StrBuilder("");
0572: sb.deleteAll('b');
0573: assertEquals("", sb.toString());
0574: }
0575:
0576: public void testDeleteFirst_char() {
0577: StrBuilder sb = new StrBuilder("abcba");
0578: sb.deleteFirst('X');
0579: assertEquals("abcba", sb.toString());
0580: sb.deleteFirst('a');
0581: assertEquals("bcba", sb.toString());
0582: sb.deleteFirst('c');
0583: assertEquals("bba", sb.toString());
0584: sb.deleteFirst('b');
0585: assertEquals("ba", sb.toString());
0586:
0587: sb = new StrBuilder("");
0588: sb.deleteFirst('b');
0589: assertEquals("", sb.toString());
0590: }
0591:
0592: // -----------------------------------------------------------------------
0593: public void testDeleteAll_String() {
0594: StrBuilder sb = new StrBuilder("abcbccba");
0595: sb.deleteAll((String) null);
0596: assertEquals("abcbccba", sb.toString());
0597: sb.deleteAll("");
0598: assertEquals("abcbccba", sb.toString());
0599:
0600: sb.deleteAll("X");
0601: assertEquals("abcbccba", sb.toString());
0602: sb.deleteAll("a");
0603: assertEquals("bcbccb", sb.toString());
0604: sb.deleteAll("c");
0605: assertEquals("bbb", sb.toString());
0606: sb.deleteAll("b");
0607: assertEquals("", sb.toString());
0608:
0609: sb = new StrBuilder("abcbccba");
0610: sb.deleteAll("bc");
0611: assertEquals("acba", sb.toString());
0612:
0613: sb = new StrBuilder("");
0614: sb.deleteAll("bc");
0615: assertEquals("", sb.toString());
0616: }
0617:
0618: public void testDeleteFirst_String() {
0619: StrBuilder sb = new StrBuilder("abcbccba");
0620: sb.deleteFirst((String) null);
0621: assertEquals("abcbccba", sb.toString());
0622: sb.deleteFirst("");
0623: assertEquals("abcbccba", sb.toString());
0624:
0625: sb.deleteFirst("X");
0626: assertEquals("abcbccba", sb.toString());
0627: sb.deleteFirst("a");
0628: assertEquals("bcbccba", sb.toString());
0629: sb.deleteFirst("c");
0630: assertEquals("bbccba", sb.toString());
0631: sb.deleteFirst("b");
0632: assertEquals("bccba", sb.toString());
0633:
0634: sb = new StrBuilder("abcbccba");
0635: sb.deleteFirst("bc");
0636: assertEquals("abccba", sb.toString());
0637:
0638: sb = new StrBuilder("");
0639: sb.deleteFirst("bc");
0640: assertEquals("", sb.toString());
0641: }
0642:
0643: // -----------------------------------------------------------------------
0644: public void testDeleteAll_StrMatcher() {
0645: StrBuilder sb = new StrBuilder("A0xA1A2yA3");
0646: sb.deleteAll((StrMatcher) null);
0647: assertEquals("A0xA1A2yA3", sb.toString());
0648: sb.deleteAll(A_NUMBER_MATCHER);
0649: assertEquals("xy", sb.toString());
0650:
0651: sb = new StrBuilder("Ax1");
0652: sb.deleteAll(A_NUMBER_MATCHER);
0653: assertEquals("Ax1", sb.toString());
0654:
0655: sb = new StrBuilder("");
0656: sb.deleteAll(A_NUMBER_MATCHER);
0657: assertEquals("", sb.toString());
0658: }
0659:
0660: public void testDeleteFirst_StrMatcher() {
0661: StrBuilder sb = new StrBuilder("A0xA1A2yA3");
0662: sb.deleteFirst((StrMatcher) null);
0663: assertEquals("A0xA1A2yA3", sb.toString());
0664: sb.deleteFirst(A_NUMBER_MATCHER);
0665: assertEquals("xA1A2yA3", sb.toString());
0666:
0667: sb = new StrBuilder("Ax1");
0668: sb.deleteFirst(A_NUMBER_MATCHER);
0669: assertEquals("Ax1", sb.toString());
0670:
0671: sb = new StrBuilder("");
0672: sb.deleteFirst(A_NUMBER_MATCHER);
0673: assertEquals("", sb.toString());
0674: }
0675:
0676: // -----------------------------------------------------------------------
0677: public void testReplace_int_int_String() {
0678: StrBuilder sb = new StrBuilder("abc");
0679: sb.replace(0, 1, "d");
0680: assertEquals("dbc", sb.toString());
0681: sb.replace(0, 1, "aaa");
0682: assertEquals("aaabc", sb.toString());
0683: sb.replace(0, 3, "");
0684: assertEquals("bc", sb.toString());
0685: sb.replace(1, 2, (String) null);
0686: assertEquals("b", sb.toString());
0687: sb.replace(1, 1000, "text");
0688: assertEquals("btext", sb.toString());
0689: sb.replace(0, 1000, "text");
0690: assertEquals("text", sb.toString());
0691:
0692: sb = new StrBuilder("atext");
0693: sb.replace(1, 1, "ny");
0694: assertEquals("anytext", sb.toString());
0695: try {
0696: sb.replace(2, 1, "anything");
0697: fail("Expected IndexOutOfBoundsException");
0698: } catch (IndexOutOfBoundsException e) {
0699: }
0700:
0701: sb = new StrBuilder();
0702: try {
0703: sb.replace(1, 2, "anything");
0704: fail("Expected IndexOutOfBoundsException");
0705: } catch (IndexOutOfBoundsException e) {
0706: }
0707: try {
0708: sb.replace(-1, 1, "anything");
0709: fail("Expected IndexOutOfBoundsException");
0710: } catch (IndexOutOfBoundsException e) {
0711: }
0712: }
0713:
0714: //-----------------------------------------------------------------------
0715: public void testReplaceAll_char_char() {
0716: StrBuilder sb = new StrBuilder("abcbccba");
0717: sb.replaceAll('x', 'y');
0718: assertEquals("abcbccba", sb.toString());
0719: sb.replaceAll('a', 'd');
0720: assertEquals("dbcbccbd", sb.toString());
0721: sb.replaceAll('b', 'e');
0722: assertEquals("dececced", sb.toString());
0723: sb.replaceAll('c', 'f');
0724: assertEquals("defeffed", sb.toString());
0725: sb.replaceAll('d', 'd');
0726: assertEquals("defeffed", sb.toString());
0727: }
0728:
0729: //-----------------------------------------------------------------------
0730: public void testReplaceFirst_char_char() {
0731: StrBuilder sb = new StrBuilder("abcbccba");
0732: sb.replaceFirst('x', 'y');
0733: assertEquals("abcbccba", sb.toString());
0734: sb.replaceFirst('a', 'd');
0735: assertEquals("dbcbccba", sb.toString());
0736: sb.replaceFirst('b', 'e');
0737: assertEquals("decbccba", sb.toString());
0738: sb.replaceFirst('c', 'f');
0739: assertEquals("defbccba", sb.toString());
0740: sb.replaceFirst('d', 'd');
0741: assertEquals("defbccba", sb.toString());
0742: }
0743:
0744: //-----------------------------------------------------------------------
0745: public void testReplaceAll_String_String() {
0746: StrBuilder sb = new StrBuilder("abcbccba");
0747: sb.replaceAll((String) null, null);
0748: assertEquals("abcbccba", sb.toString());
0749: sb.replaceAll((String) null, "anything");
0750: assertEquals("abcbccba", sb.toString());
0751: sb.replaceAll("", null);
0752: assertEquals("abcbccba", sb.toString());
0753: sb.replaceAll("", "anything");
0754: assertEquals("abcbccba", sb.toString());
0755:
0756: sb.replaceAll("x", "y");
0757: assertEquals("abcbccba", sb.toString());
0758: sb.replaceAll("a", "d");
0759: assertEquals("dbcbccbd", sb.toString());
0760: sb.replaceAll("d", null);
0761: assertEquals("bcbccb", sb.toString());
0762: sb.replaceAll("cb", "-");
0763: assertEquals("b-c-", sb.toString());
0764:
0765: sb = new StrBuilder("abcba");
0766: sb.replaceAll("b", "xbx");
0767: assertEquals("axbxcxbxa", sb.toString());
0768:
0769: sb = new StrBuilder("bb");
0770: sb.replaceAll("b", "xbx");
0771: assertEquals("xbxxbx", sb.toString());
0772: }
0773:
0774: public void testReplaceFirst_String_String() {
0775: StrBuilder sb = new StrBuilder("abcbccba");
0776: sb.replaceFirst((String) null, null);
0777: assertEquals("abcbccba", sb.toString());
0778: sb.replaceFirst((String) null, "anything");
0779: assertEquals("abcbccba", sb.toString());
0780: sb.replaceFirst("", null);
0781: assertEquals("abcbccba", sb.toString());
0782: sb.replaceFirst("", "anything");
0783: assertEquals("abcbccba", sb.toString());
0784:
0785: sb.replaceFirst("x", "y");
0786: assertEquals("abcbccba", sb.toString());
0787: sb.replaceFirst("a", "d");
0788: assertEquals("dbcbccba", sb.toString());
0789: sb.replaceFirst("d", null);
0790: assertEquals("bcbccba", sb.toString());
0791: sb.replaceFirst("cb", "-");
0792: assertEquals("b-ccba", sb.toString());
0793:
0794: sb = new StrBuilder("abcba");
0795: sb.replaceFirst("b", "xbx");
0796: assertEquals("axbxcba", sb.toString());
0797:
0798: sb = new StrBuilder("bb");
0799: sb.replaceFirst("b", "xbx");
0800: assertEquals("xbxb", sb.toString());
0801: }
0802:
0803: //-----------------------------------------------------------------------
0804: public void testReplaceAll_StrMatcher_String() {
0805: StrBuilder sb = new StrBuilder("abcbccba");
0806: sb.replaceAll((StrMatcher) null, null);
0807: assertEquals("abcbccba", sb.toString());
0808: sb.replaceAll((StrMatcher) null, "anything");
0809: assertEquals("abcbccba", sb.toString());
0810: sb.replaceAll(StrMatcher.noneMatcher(), null);
0811: assertEquals("abcbccba", sb.toString());
0812: sb.replaceAll(StrMatcher.noneMatcher(), "anything");
0813: assertEquals("abcbccba", sb.toString());
0814:
0815: sb.replaceAll(StrMatcher.charMatcher('x'), "y");
0816: assertEquals("abcbccba", sb.toString());
0817: sb.replaceAll(StrMatcher.charMatcher('a'), "d");
0818: assertEquals("dbcbccbd", sb.toString());
0819: sb.replaceAll(StrMatcher.charMatcher('d'), null);
0820: assertEquals("bcbccb", sb.toString());
0821: sb.replaceAll(StrMatcher.stringMatcher("cb"), "-");
0822: assertEquals("b-c-", sb.toString());
0823:
0824: sb = new StrBuilder("abcba");
0825: sb.replaceAll(StrMatcher.charMatcher('b'), "xbx");
0826: assertEquals("axbxcxbxa", sb.toString());
0827:
0828: sb = new StrBuilder("bb");
0829: sb.replaceAll(StrMatcher.charMatcher('b'), "xbx");
0830: assertEquals("xbxxbx", sb.toString());
0831:
0832: sb = new StrBuilder("A1-A2A3-A4");
0833: sb.replaceAll(A_NUMBER_MATCHER, "***");
0834: assertEquals("***-******-***", sb.toString());
0835: }
0836:
0837: public void testReplaceFirst_StrMatcher_String() {
0838: StrBuilder sb = new StrBuilder("abcbccba");
0839: sb.replaceFirst((StrMatcher) null, null);
0840: assertEquals("abcbccba", sb.toString());
0841: sb.replaceFirst((StrMatcher) null, "anything");
0842: assertEquals("abcbccba", sb.toString());
0843: sb.replaceFirst(StrMatcher.noneMatcher(), null);
0844: assertEquals("abcbccba", sb.toString());
0845: sb.replaceFirst(StrMatcher.noneMatcher(), "anything");
0846: assertEquals("abcbccba", sb.toString());
0847:
0848: sb.replaceFirst(StrMatcher.charMatcher('x'), "y");
0849: assertEquals("abcbccba", sb.toString());
0850: sb.replaceFirst(StrMatcher.charMatcher('a'), "d");
0851: assertEquals("dbcbccba", sb.toString());
0852: sb.replaceFirst(StrMatcher.charMatcher('d'), null);
0853: assertEquals("bcbccba", sb.toString());
0854: sb.replaceFirst(StrMatcher.stringMatcher("cb"), "-");
0855: assertEquals("b-ccba", sb.toString());
0856:
0857: sb = new StrBuilder("abcba");
0858: sb.replaceFirst(StrMatcher.charMatcher('b'), "xbx");
0859: assertEquals("axbxcba", sb.toString());
0860:
0861: sb = new StrBuilder("bb");
0862: sb.replaceFirst(StrMatcher.charMatcher('b'), "xbx");
0863: assertEquals("xbxb", sb.toString());
0864:
0865: sb = new StrBuilder("A1-A2A3-A4");
0866: sb.replaceFirst(A_NUMBER_MATCHER, "***");
0867: assertEquals("***-A2A3-A4", sb.toString());
0868: }
0869:
0870: //-----------------------------------------------------------------------
0871: public void testReplace_StrMatcher_String_int_int_int_VaryMatcher() {
0872: StrBuilder sb = new StrBuilder("abcbccba");
0873: sb.replace((StrMatcher) null, "x", 0, sb.length(), -1);
0874: assertEquals("abcbccba", sb.toString());
0875:
0876: sb
0877: .replace(StrMatcher.charMatcher('a'), "x", 0, sb
0878: .length(), -1);
0879: assertEquals("xbcbccbx", sb.toString());
0880:
0881: sb.replace(StrMatcher.stringMatcher("cb"), "x", 0, sb.length(),
0882: -1);
0883: assertEquals("xbxcxx", sb.toString());
0884:
0885: sb = new StrBuilder("A1-A2A3-A4");
0886: sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1);
0887: assertEquals("***-******-***", sb.toString());
0888:
0889: sb = new StrBuilder();
0890: sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1);
0891: assertEquals("", sb.toString());
0892: }
0893:
0894: public void testReplace_StrMatcher_String_int_int_int_VaryReplace() {
0895: StrBuilder sb = new StrBuilder("abcbccba");
0896: sb.replace(StrMatcher.stringMatcher("cb"), "cb", 0,
0897: sb.length(), -1);
0898: assertEquals("abcbccba", sb.toString());
0899:
0900: sb = new StrBuilder("abcbccba");
0901: sb.replace(StrMatcher.stringMatcher("cb"), "-", 0, sb.length(),
0902: -1);
0903: assertEquals("ab-c-a", sb.toString());
0904:
0905: sb = new StrBuilder("abcbccba");
0906: sb.replace(StrMatcher.stringMatcher("cb"), "+++", 0, sb
0907: .length(), -1);
0908: assertEquals("ab+++c+++a", sb.toString());
0909:
0910: sb = new StrBuilder("abcbccba");
0911: sb.replace(StrMatcher.stringMatcher("cb"), "", 0, sb.length(),
0912: -1);
0913: assertEquals("abca", sb.toString());
0914:
0915: sb = new StrBuilder("abcbccba");
0916: sb.replace(StrMatcher.stringMatcher("cb"), null, 0,
0917: sb.length(), -1);
0918: assertEquals("abca", sb.toString());
0919: }
0920:
0921: public void testReplace_StrMatcher_String_int_int_int_VaryStartIndex() {
0922: StrBuilder sb = new StrBuilder("aaxaaaayaa");
0923: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, sb.length(),
0924: -1);
0925: assertEquals("-x--y-", sb.toString());
0926:
0927: sb = new StrBuilder("aaxaaaayaa");
0928: sb.replace(StrMatcher.stringMatcher("aa"), "-", 1, sb.length(),
0929: -1);
0930: assertEquals("aax--y-", sb.toString());
0931:
0932: sb = new StrBuilder("aaxaaaayaa");
0933: sb.replace(StrMatcher.stringMatcher("aa"), "-", 2, sb.length(),
0934: -1);
0935: assertEquals("aax--y-", sb.toString());
0936:
0937: sb = new StrBuilder("aaxaaaayaa");
0938: sb.replace(StrMatcher.stringMatcher("aa"), "-", 3, sb.length(),
0939: -1);
0940: assertEquals("aax--y-", sb.toString());
0941:
0942: sb = new StrBuilder("aaxaaaayaa");
0943: sb.replace(StrMatcher.stringMatcher("aa"), "-", 4, sb.length(),
0944: -1);
0945: assertEquals("aaxa-ay-", sb.toString());
0946:
0947: sb = new StrBuilder("aaxaaaayaa");
0948: sb.replace(StrMatcher.stringMatcher("aa"), "-", 5, sb.length(),
0949: -1);
0950: assertEquals("aaxaa-y-", sb.toString());
0951:
0952: sb = new StrBuilder("aaxaaaayaa");
0953: sb.replace(StrMatcher.stringMatcher("aa"), "-", 6, sb.length(),
0954: -1);
0955: assertEquals("aaxaaaay-", sb.toString());
0956:
0957: sb = new StrBuilder("aaxaaaayaa");
0958: sb.replace(StrMatcher.stringMatcher("aa"), "-", 7, sb.length(),
0959: -1);
0960: assertEquals("aaxaaaay-", sb.toString());
0961:
0962: sb = new StrBuilder("aaxaaaayaa");
0963: sb.replace(StrMatcher.stringMatcher("aa"), "-", 8, sb.length(),
0964: -1);
0965: assertEquals("aaxaaaay-", sb.toString());
0966:
0967: sb = new StrBuilder("aaxaaaayaa");
0968: sb.replace(StrMatcher.stringMatcher("aa"), "-", 9, sb.length(),
0969: -1);
0970: assertEquals("aaxaaaayaa", sb.toString());
0971:
0972: sb = new StrBuilder("aaxaaaayaa");
0973: sb.replace(StrMatcher.stringMatcher("aa"), "-", 10,
0974: sb.length(), -1);
0975: assertEquals("aaxaaaayaa", sb.toString());
0976:
0977: sb = new StrBuilder("aaxaaaayaa");
0978: try {
0979: sb.replace(StrMatcher.stringMatcher("aa"), "-", 11, sb
0980: .length(), -1);
0981: fail();
0982: } catch (IndexOutOfBoundsException ex) {
0983: }
0984: assertEquals("aaxaaaayaa", sb.toString());
0985:
0986: sb = new StrBuilder("aaxaaaayaa");
0987: try {
0988: sb.replace(StrMatcher.stringMatcher("aa"), "-", -1, sb
0989: .length(), -1);
0990: fail();
0991: } catch (IndexOutOfBoundsException ex) {
0992: }
0993: assertEquals("aaxaaaayaa", sb.toString());
0994: }
0995:
0996: public void testReplace_StrMatcher_String_int_int_int_VaryEndIndex() {
0997: StrBuilder sb = new StrBuilder("aaxaaaayaa");
0998: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 0, -1);
0999: assertEquals("aaxaaaayaa", sb.toString());
1000:
1001: sb = new StrBuilder("aaxaaaayaa");
1002: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 2, -1);
1003: assertEquals("-xaaaayaa", sb.toString());
1004:
1005: sb = new StrBuilder("aaxaaaayaa");
1006: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 3, -1);
1007: assertEquals("-xaaaayaa", sb.toString());
1008:
1009: sb = new StrBuilder("aaxaaaayaa");
1010: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 4, -1);
1011: assertEquals("-xaaaayaa", sb.toString());
1012:
1013: sb = new StrBuilder("aaxaaaayaa");
1014: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 5, -1);
1015: assertEquals("-x-aayaa", sb.toString());
1016:
1017: sb = new StrBuilder("aaxaaaayaa");
1018: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 6, -1);
1019: assertEquals("-x-aayaa", sb.toString());
1020:
1021: sb = new StrBuilder("aaxaaaayaa");
1022: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 7, -1);
1023: assertEquals("-x--yaa", sb.toString());
1024:
1025: sb = new StrBuilder("aaxaaaayaa");
1026: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 8, -1);
1027: assertEquals("-x--yaa", sb.toString());
1028:
1029: sb = new StrBuilder("aaxaaaayaa");
1030: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 9, -1);
1031: assertEquals("-x--yaa", sb.toString());
1032:
1033: sb = new StrBuilder("aaxaaaayaa");
1034: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, -1);
1035: assertEquals("-x--y-", sb.toString());
1036:
1037: sb = new StrBuilder("aaxaaaayaa");
1038: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 1000, -1);
1039: assertEquals("-x--y-", sb.toString());
1040:
1041: sb = new StrBuilder("aaxaaaayaa");
1042: try {
1043: sb.replace(StrMatcher.stringMatcher("aa"), "-", 2, 1, -1);
1044: fail();
1045: } catch (IndexOutOfBoundsException ex) {
1046: }
1047: assertEquals("aaxaaaayaa", sb.toString());
1048: }
1049:
1050: public void testReplace_StrMatcher_String_int_int_int_VaryCount() {
1051: StrBuilder sb = new StrBuilder("aaxaaaayaa");
1052: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, -1);
1053: assertEquals("-x--y-", sb.toString());
1054:
1055: sb = new StrBuilder("aaxaaaayaa");
1056: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 0);
1057: assertEquals("aaxaaaayaa", sb.toString());
1058:
1059: sb = new StrBuilder("aaxaaaayaa");
1060: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 1);
1061: assertEquals("-xaaaayaa", sb.toString());
1062:
1063: sb = new StrBuilder("aaxaaaayaa");
1064: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 2);
1065: assertEquals("-x-aayaa", sb.toString());
1066:
1067: sb = new StrBuilder("aaxaaaayaa");
1068: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 3);
1069: assertEquals("-x--yaa", sb.toString());
1070:
1071: sb = new StrBuilder("aaxaaaayaa");
1072: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 4);
1073: assertEquals("-x--y-", sb.toString());
1074:
1075: sb = new StrBuilder("aaxaaaayaa");
1076: sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 5);
1077: assertEquals("-x--y-", sb.toString());
1078: }
1079:
1080: //-----------------------------------------------------------------------
1081: public void testReverse() {
1082: StrBuilder sb = new StrBuilder();
1083: assertEquals("", sb.reverse().toString());
1084:
1085: sb.clear().append(true);
1086: assertEquals("eurt", sb.reverse().toString());
1087: assertEquals("true", sb.reverse().toString());
1088: }
1089:
1090: //-----------------------------------------------------------------------
1091: public void testTrim() {
1092: StrBuilder sb = new StrBuilder();
1093: assertEquals("", sb.reverse().toString());
1094:
1095: sb.clear().append(" \u0000 ");
1096: assertEquals("", sb.trim().toString());
1097:
1098: sb.clear().append(" \u0000 a b c");
1099: assertEquals("a b c", sb.trim().toString());
1100:
1101: sb.clear().append("a b c \u0000 ");
1102: assertEquals("a b c", sb.trim().toString());
1103:
1104: sb.clear().append(" \u0000 a b c \u0000 ");
1105: assertEquals("a b c", sb.trim().toString());
1106:
1107: sb.clear().append("a b c");
1108: assertEquals("a b c", sb.trim().toString());
1109: }
1110:
1111: //-----------------------------------------------------------------------
1112: public void testStartsWith() {
1113: StrBuilder sb = new StrBuilder();
1114: assertFalse(sb.startsWith("a"));
1115: assertFalse(sb.startsWith(null));
1116: assertTrue(sb.startsWith(""));
1117: sb.append("abc");
1118: assertTrue(sb.startsWith("a"));
1119: assertTrue(sb.startsWith("ab"));
1120: assertTrue(sb.startsWith("abc"));
1121: assertFalse(sb.startsWith("cba"));
1122: }
1123:
1124: public void testEndsWith() {
1125: StrBuilder sb = new StrBuilder();
1126: assertFalse(sb.endsWith("a"));
1127: assertFalse(sb.endsWith("c"));
1128: assertTrue(sb.endsWith(""));
1129: assertFalse(sb.endsWith(null));
1130: sb.append("abc");
1131: assertTrue(sb.endsWith("c"));
1132: assertTrue(sb.endsWith("bc"));
1133: assertTrue(sb.endsWith("abc"));
1134: assertFalse(sb.endsWith("cba"));
1135: assertFalse(sb.endsWith("abcd"));
1136: assertFalse(sb.endsWith(" abc"));
1137: assertFalse(sb.endsWith("abc "));
1138: }
1139:
1140: //-----------------------------------------------------------------------
1141: public void testSubstringInt() {
1142: StrBuilder sb = new StrBuilder("hello goodbye");
1143: assertEquals("goodbye", sb.substring(6));
1144: assertEquals("hello goodbye".substring(6), sb.substring(6));
1145: assertEquals("hello goodbye", sb.substring(0));
1146: assertEquals("hello goodbye".substring(0), sb.substring(0));
1147: try {
1148: sb.substring(-1);
1149: fail();
1150: } catch (IndexOutOfBoundsException e) {
1151: }
1152:
1153: try {
1154: sb.substring(15);
1155: fail();
1156: } catch (IndexOutOfBoundsException e) {
1157: }
1158:
1159: }
1160:
1161: public void testSubstringIntInt() {
1162: StrBuilder sb = new StrBuilder("hello goodbye");
1163: assertEquals("hello", sb.substring(0, 5));
1164: assertEquals("hello goodbye".substring(0, 6), sb
1165: .substring(0, 6));
1166:
1167: assertEquals("goodbye", sb.substring(6, 13));
1168: assertEquals("hello goodbye".substring(6, 13), sb.substring(6,
1169: 13));
1170:
1171: assertEquals("goodbye", sb.substring(6, 20));
1172:
1173: try {
1174: sb.substring(-1, 5);
1175: fail();
1176: } catch (IndexOutOfBoundsException e) {
1177: }
1178:
1179: try {
1180: sb.substring(15, 20);
1181: fail();
1182: } catch (IndexOutOfBoundsException e) {
1183: }
1184: }
1185:
1186: // -----------------------------------------------------------------------
1187: public void testMidString() {
1188: StrBuilder sb = new StrBuilder("hello goodbye hello");
1189: assertEquals("goodbye", sb.midString(6, 7));
1190: assertEquals("hello", sb.midString(0, 5));
1191: assertEquals("hello", sb.midString(-5, 5));
1192: assertEquals("", sb.midString(0, -1));
1193: assertEquals("", sb.midString(20, 2));
1194: assertEquals("hello", sb.midString(14, 22));
1195: }
1196:
1197: public void testRightString() {
1198: StrBuilder sb = new StrBuilder("left right");
1199: assertEquals("right", sb.rightString(5));
1200: assertEquals("", sb.rightString(0));
1201: assertEquals("", sb.rightString(-5));
1202: assertEquals("left right", sb.rightString(15));
1203: }
1204:
1205: public void testLeftString() {
1206: StrBuilder sb = new StrBuilder("left right");
1207: assertEquals("left", sb.leftString(4));
1208: assertEquals("", sb.leftString(0));
1209: assertEquals("", sb.leftString(-5));
1210: assertEquals("left right", sb.leftString(15));
1211: }
1212:
1213: // -----------------------------------------------------------------------
1214: public void testContains_char() {
1215: StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
1216: assertEquals(true, sb.contains('a'));
1217: assertEquals(true, sb.contains('o'));
1218: assertEquals(true, sb.contains('z'));
1219: assertEquals(false, sb.contains('1'));
1220: }
1221:
1222: public void testContains_String() {
1223: StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
1224: assertEquals(true, sb.contains("a"));
1225: assertEquals(true, sb.contains("pq"));
1226: assertEquals(true, sb.contains("z"));
1227: assertEquals(false, sb.contains("zyx"));
1228: assertEquals(false, sb.contains((String) null));
1229: }
1230:
1231: public void testContains_StrMatcher() {
1232: StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
1233: assertEquals(true, sb.contains(StrMatcher.charMatcher('a')));
1234: assertEquals(true, sb.contains(StrMatcher.stringMatcher("pq")));
1235: assertEquals(true, sb.contains(StrMatcher.charMatcher('z')));
1236: assertEquals(false, sb.contains(StrMatcher.stringMatcher("zy")));
1237: assertEquals(false, sb.contains((StrMatcher) null));
1238:
1239: sb = new StrBuilder();
1240: assertEquals(false, sb.contains(A_NUMBER_MATCHER));
1241: sb.append("B A1 C");
1242: assertEquals(true, sb.contains(A_NUMBER_MATCHER));
1243: }
1244:
1245: // -----------------------------------------------------------------------
1246: public void testIndexOf_char() {
1247: StrBuilder sb = new StrBuilder("abab");
1248: assertEquals(0, sb.indexOf('a'));
1249:
1250: // should work like String#indexOf
1251: assertEquals("abab".indexOf('a'), sb.indexOf('a'));
1252:
1253: assertEquals(1, sb.indexOf('b'));
1254: assertEquals("abab".indexOf('b'), sb.indexOf('b'));
1255:
1256: assertEquals(-1, sb.indexOf('z'));
1257: }
1258:
1259: public void testIndexOf_char_int() {
1260: StrBuilder sb = new StrBuilder("abab");
1261: assertEquals(0, sb.indexOf('a', -1));
1262: assertEquals(0, sb.indexOf('a', 0));
1263: assertEquals(2, sb.indexOf('a', 1));
1264: assertEquals(-1, sb.indexOf('a', 4));
1265: assertEquals(-1, sb.indexOf('a', 5));
1266:
1267: // should work like String#indexOf
1268: assertEquals("abab".indexOf('a', 1), sb.indexOf('a', 1));
1269:
1270: assertEquals(3, sb.indexOf('b', 2));
1271: assertEquals("abab".indexOf('b', 2), sb.indexOf('b', 2));
1272:
1273: assertEquals(-1, sb.indexOf('z', 2));
1274:
1275: sb = new StrBuilder("xyzabc");
1276: assertEquals(2, sb.indexOf('z', 0));
1277: assertEquals(-1, sb.indexOf('z', 3));
1278: }
1279:
1280: public void testLastIndexOf_char() {
1281: StrBuilder sb = new StrBuilder("abab");
1282:
1283: assertEquals(2, sb.lastIndexOf('a'));
1284: //should work like String#lastIndexOf
1285: assertEquals("abab".lastIndexOf('a'), sb.lastIndexOf('a'));
1286:
1287: assertEquals(3, sb.lastIndexOf('b'));
1288: assertEquals("abab".lastIndexOf('b'), sb.lastIndexOf('b'));
1289:
1290: assertEquals(-1, sb.lastIndexOf('z'));
1291: }
1292:
1293: public void testLastIndexOf_char_int() {
1294: StrBuilder sb = new StrBuilder("abab");
1295: assertEquals(-1, sb.lastIndexOf('a', -1));
1296: assertEquals(0, sb.lastIndexOf('a', 0));
1297: assertEquals(0, sb.lastIndexOf('a', 1));
1298:
1299: // should work like String#lastIndexOf
1300: assertEquals("abab".lastIndexOf('a', 1), sb.lastIndexOf('a', 1));
1301:
1302: assertEquals(1, sb.lastIndexOf('b', 2));
1303: assertEquals("abab".lastIndexOf('b', 2), sb.lastIndexOf('b', 2));
1304:
1305: assertEquals(-1, sb.lastIndexOf('z', 2));
1306:
1307: sb = new StrBuilder("xyzabc");
1308: assertEquals(2, sb.lastIndexOf('z', sb.length()));
1309: assertEquals(-1, sb.lastIndexOf('z', 1));
1310: }
1311:
1312: // -----------------------------------------------------------------------
1313: public void testIndexOf_String() {
1314: StrBuilder sb = new StrBuilder("abab");
1315:
1316: assertEquals(0, sb.indexOf("a"));
1317: //should work like String#indexOf
1318: assertEquals("abab".indexOf("a"), sb.indexOf("a"));
1319:
1320: assertEquals(0, sb.indexOf("ab"));
1321: //should work like String#indexOf
1322: assertEquals("abab".indexOf("ab"), sb.indexOf("ab"));
1323:
1324: assertEquals(1, sb.indexOf("b"));
1325: assertEquals("abab".indexOf("b"), sb.indexOf("b"));
1326:
1327: assertEquals(1, sb.indexOf("ba"));
1328: assertEquals("abab".indexOf("ba"), sb.indexOf("ba"));
1329:
1330: assertEquals(-1, sb.indexOf("z"));
1331:
1332: assertEquals(-1, sb.indexOf((String) null));
1333: }
1334:
1335: public void testIndexOf_String_int() {
1336: StrBuilder sb = new StrBuilder("abab");
1337: assertEquals(0, sb.indexOf("a", -1));
1338: assertEquals(0, sb.indexOf("a", 0));
1339: assertEquals(2, sb.indexOf("a", 1));
1340: assertEquals(2, sb.indexOf("a", 2));
1341: assertEquals(-1, sb.indexOf("a", 3));
1342: assertEquals(-1, sb.indexOf("a", 4));
1343: assertEquals(-1, sb.indexOf("a", 5));
1344:
1345: assertEquals(-1, sb.indexOf("abcdef", 0));
1346: assertEquals(0, sb.indexOf("", 0));
1347: assertEquals(1, sb.indexOf("", 1));
1348:
1349: //should work like String#indexOf
1350: assertEquals("abab".indexOf("a", 1), sb.indexOf("a", 1));
1351:
1352: assertEquals(2, sb.indexOf("ab", 1));
1353: //should work like String#indexOf
1354: assertEquals("abab".indexOf("ab", 1), sb.indexOf("ab", 1));
1355:
1356: assertEquals(3, sb.indexOf("b", 2));
1357: assertEquals("abab".indexOf("b", 2), sb.indexOf("b", 2));
1358:
1359: assertEquals(1, sb.indexOf("ba", 1));
1360: assertEquals("abab".indexOf("ba", 2), sb.indexOf("ba", 2));
1361:
1362: assertEquals(-1, sb.indexOf("z", 2));
1363:
1364: sb = new StrBuilder("xyzabc");
1365: assertEquals(2, sb.indexOf("za", 0));
1366: assertEquals(-1, sb.indexOf("za", 3));
1367:
1368: assertEquals(-1, sb.indexOf((String) null, 2));
1369: }
1370:
1371: public void testLastIndexOf_String() {
1372: StrBuilder sb = new StrBuilder("abab");
1373:
1374: assertEquals(2, sb.lastIndexOf("a"));
1375: //should work like String#lastIndexOf
1376: assertEquals("abab".lastIndexOf("a"), sb.lastIndexOf("a"));
1377:
1378: assertEquals(2, sb.lastIndexOf("ab"));
1379: //should work like String#lastIndexOf
1380: assertEquals("abab".lastIndexOf("ab"), sb.lastIndexOf("ab"));
1381:
1382: assertEquals(3, sb.lastIndexOf("b"));
1383: assertEquals("abab".lastIndexOf("b"), sb.lastIndexOf("b"));
1384:
1385: assertEquals(1, sb.lastIndexOf("ba"));
1386: assertEquals("abab".lastIndexOf("ba"), sb.lastIndexOf("ba"));
1387:
1388: assertEquals(-1, sb.lastIndexOf("z"));
1389:
1390: assertEquals(-1, sb.lastIndexOf((String) null));
1391: }
1392:
1393: public void testLastIndexOf_String_int() {
1394: StrBuilder sb = new StrBuilder("abab");
1395: assertEquals(-1, sb.lastIndexOf("a", -1));
1396: assertEquals(0, sb.lastIndexOf("a", 0));
1397: assertEquals(0, sb.lastIndexOf("a", 1));
1398: assertEquals(2, sb.lastIndexOf("a", 2));
1399: assertEquals(2, sb.lastIndexOf("a", 3));
1400: assertEquals(2, sb.lastIndexOf("a", 4));
1401: assertEquals(2, sb.lastIndexOf("a", 5));
1402:
1403: assertEquals(-1, sb.lastIndexOf("abcdef", 3));
1404: assertEquals("abab".lastIndexOf("", 3), sb.lastIndexOf("", 3));
1405: assertEquals("abab".lastIndexOf("", 1), sb.lastIndexOf("", 1));
1406:
1407: //should work like String#lastIndexOf
1408: assertEquals("abab".lastIndexOf("a", 1), sb.lastIndexOf("a", 1));
1409:
1410: assertEquals(0, sb.lastIndexOf("ab", 1));
1411: //should work like String#lastIndexOf
1412: assertEquals("abab".lastIndexOf("ab", 1), sb.lastIndexOf("ab",
1413: 1));
1414:
1415: assertEquals(1, sb.lastIndexOf("b", 2));
1416: assertEquals("abab".lastIndexOf("b", 2), sb.lastIndexOf("b", 2));
1417:
1418: assertEquals(1, sb.lastIndexOf("ba", 2));
1419: assertEquals("abab".lastIndexOf("ba", 2), sb.lastIndexOf("ba",
1420: 2));
1421:
1422: assertEquals(-1, sb.lastIndexOf("z", 2));
1423:
1424: sb = new StrBuilder("xyzabc");
1425: assertEquals(2, sb.lastIndexOf("za", sb.length()));
1426: assertEquals(-1, sb.lastIndexOf("za", 1));
1427:
1428: assertEquals(-1, sb.lastIndexOf((String) null, 2));
1429: }
1430:
1431: // -----------------------------------------------------------------------
1432: public void testIndexOf_StrMatcher() {
1433: StrBuilder sb = new StrBuilder();
1434: assertEquals(-1, sb.indexOf((StrMatcher) null));
1435: assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a')));
1436:
1437: sb.append("ab bd");
1438: assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a')));
1439: assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b')));
1440: assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher()));
1441: assertEquals(4, sb.indexOf(StrMatcher.charMatcher('d')));
1442: assertEquals(-1, sb.indexOf(StrMatcher.noneMatcher()));
1443: assertEquals(-1, sb.indexOf((StrMatcher) null));
1444:
1445: sb.append(" A1 junction");
1446: assertEquals(6, sb.indexOf(A_NUMBER_MATCHER));
1447: }
1448:
1449: public void testIndexOf_StrMatcher_int() {
1450: StrBuilder sb = new StrBuilder();
1451: assertEquals(-1, sb.indexOf((StrMatcher) null, 2));
1452: assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 2));
1453: assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 0));
1454:
1455: sb.append("ab bd");
1456: assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'), -2));
1457: assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'), 0));
1458: assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 2));
1459: assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 20));
1460:
1461: assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), -1));
1462: assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), 0));
1463: assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), 1));
1464: assertEquals(3, sb.indexOf(StrMatcher.charMatcher('b'), 2));
1465: assertEquals(3, sb.indexOf(StrMatcher.charMatcher('b'), 3));
1466: assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 4));
1467: assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 5));
1468: assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 6));
1469:
1470: assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), -2));
1471: assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), 0));
1472: assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), 2));
1473: assertEquals(-1, sb.indexOf(StrMatcher.spaceMatcher(), 4));
1474: assertEquals(-1, sb.indexOf(StrMatcher.spaceMatcher(), 20));
1475:
1476: assertEquals(-1, sb.indexOf(StrMatcher.noneMatcher(), 0));
1477: assertEquals(-1, sb.indexOf((StrMatcher) null, 0));
1478:
1479: sb.append(" A1 junction with A2");
1480: assertEquals(6, sb.indexOf(A_NUMBER_MATCHER, 5));
1481: assertEquals(6, sb.indexOf(A_NUMBER_MATCHER, 6));
1482: assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 7));
1483: assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 22));
1484: assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 23));
1485: assertEquals(-1, sb.indexOf(A_NUMBER_MATCHER, 24));
1486: }
1487:
1488: public void testLastIndexOf_StrMatcher() {
1489: StrBuilder sb = new StrBuilder();
1490: assertEquals(-1, sb.lastIndexOf((StrMatcher) null));
1491: assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a')));
1492:
1493: sb.append("ab bd");
1494: assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a')));
1495: assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b')));
1496: assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher()));
1497: assertEquals(4, sb.lastIndexOf(StrMatcher.charMatcher('d')));
1498: assertEquals(-1, sb.lastIndexOf(StrMatcher.noneMatcher()));
1499: assertEquals(-1, sb.lastIndexOf((StrMatcher) null));
1500:
1501: sb.append(" A1 junction");
1502: assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER));
1503: }
1504:
1505: public void testLastIndexOf_StrMatcher_int() {
1506: StrBuilder sb = new StrBuilder();
1507: assertEquals(-1, sb.lastIndexOf((StrMatcher) null, 2));
1508: assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), 2));
1509: assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), 0));
1510: assertEquals(-1, sb
1511: .lastIndexOf(StrMatcher.charMatcher('a'), -1));
1512:
1513: sb.append("ab bd");
1514: assertEquals(-1, sb
1515: .lastIndexOf(StrMatcher.charMatcher('a'), -2));
1516: assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 0));
1517: assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 2));
1518: assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 20));
1519:
1520: assertEquals(-1, sb
1521: .lastIndexOf(StrMatcher.charMatcher('b'), -1));
1522: assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 0));
1523: assertEquals(1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 1));
1524: assertEquals(1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 2));
1525: assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 3));
1526: assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 4));
1527: assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 5));
1528: assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 6));
1529:
1530: assertEquals(-1, sb.lastIndexOf(StrMatcher.spaceMatcher(), -2));
1531: assertEquals(-1, sb.lastIndexOf(StrMatcher.spaceMatcher(), 0));
1532: assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 2));
1533: assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 4));
1534: assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 20));
1535:
1536: assertEquals(-1, sb.lastIndexOf(StrMatcher.noneMatcher(), 0));
1537: assertEquals(-1, sb.lastIndexOf((StrMatcher) null, 0));
1538:
1539: sb.append(" A1 junction with A2");
1540: assertEquals(-1, sb.lastIndexOf(A_NUMBER_MATCHER, 5));
1541: assertEquals(-1, sb.lastIndexOf(A_NUMBER_MATCHER, 6)); // A matches, 1 is outside bounds
1542: assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 7));
1543: assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 22));
1544: assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 23)); // A matches, 2 is outside bounds
1545: assertEquals(23, sb.lastIndexOf(A_NUMBER_MATCHER, 24));
1546: }
1547:
1548: static final StrMatcher A_NUMBER_MATCHER = new StrMatcher() {
1549: public int isMatch(char[] buffer, int pos, int bufferStart,
1550: int bufferEnd) {
1551: if (buffer[pos] == 'A') {
1552: pos++;
1553: if (pos < bufferEnd && buffer[pos] >= '0'
1554: && buffer[pos] <= '9') {
1555: return 2;
1556: }
1557: }
1558: return 0;
1559: }
1560: };
1561:
1562: //-----------------------------------------------------------------------
1563: public void testAsTokenizer() throws Exception {
1564: // from Javadoc
1565: StrBuilder b = new StrBuilder();
1566: b.append("a b ");
1567: StrTokenizer t = b.asTokenizer();
1568:
1569: String[] tokens1 = t.getTokenArray();
1570: assertEquals(2, tokens1.length);
1571: assertEquals("a", tokens1[0]);
1572: assertEquals("b", tokens1[1]);
1573: assertEquals(2, t.size());
1574:
1575: b.append("c d ");
1576: String[] tokens2 = t.getTokenArray();
1577: assertEquals(2, tokens2.length);
1578: assertEquals("a", tokens2[0]);
1579: assertEquals("b", tokens2[1]);
1580: assertEquals(2, t.size());
1581: assertEquals("a", t.next());
1582: assertEquals("b", t.next());
1583:
1584: t.reset();
1585: String[] tokens3 = t.getTokenArray();
1586: assertEquals(4, tokens3.length);
1587: assertEquals("a", tokens3[0]);
1588: assertEquals("b", tokens3[1]);
1589: assertEquals("c", tokens3[2]);
1590: assertEquals("d", tokens3[3]);
1591: assertEquals(4, t.size());
1592: assertEquals("a", t.next());
1593: assertEquals("b", t.next());
1594: assertEquals("c", t.next());
1595: assertEquals("d", t.next());
1596:
1597: assertEquals("a b c d ", t.getContent());
1598: }
1599:
1600: // -----------------------------------------------------------------------
1601: public void testAsReader() throws Exception {
1602: StrBuilder sb = new StrBuilder("some text");
1603: Reader reader = sb.asReader();
1604: assertEquals(true, reader.ready());
1605: char[] buf = new char[40];
1606: assertEquals(9, reader.read(buf));
1607: assertEquals("some text", new String(buf, 0, 9));
1608:
1609: assertEquals(-1, reader.read());
1610: assertEquals(false, reader.ready());
1611: assertEquals(0, reader.skip(2));
1612: assertEquals(0, reader.skip(-1));
1613:
1614: assertEquals(true, reader.markSupported());
1615: reader = sb.asReader();
1616: assertEquals('s', reader.read());
1617: reader.mark(-1);
1618: char[] array = new char[3];
1619: assertEquals(3, reader.read(array, 0, 3));
1620: assertEquals('o', array[0]);
1621: assertEquals('m', array[1]);
1622: assertEquals('e', array[2]);
1623: reader.reset();
1624: assertEquals(1, reader.read(array, 1, 1));
1625: assertEquals('o', array[0]);
1626: assertEquals('o', array[1]);
1627: assertEquals('e', array[2]);
1628: assertEquals(2, reader.skip(2));
1629: assertEquals(' ', reader.read());
1630:
1631: assertEquals(true, reader.ready());
1632: reader.close();
1633: assertEquals(true, reader.ready());
1634:
1635: reader = sb.asReader();
1636: array = new char[3];
1637: try {
1638: reader.read(array, -1, 0);
1639: fail();
1640: } catch (IndexOutOfBoundsException ex) {
1641: }
1642: try {
1643: reader.read(array, 0, -1);
1644: fail();
1645: } catch (IndexOutOfBoundsException ex) {
1646: }
1647: try {
1648: reader.read(array, 100, 1);
1649: fail();
1650: } catch (IndexOutOfBoundsException ex) {
1651: }
1652: try {
1653: reader.read(array, 0, 100);
1654: fail();
1655: } catch (IndexOutOfBoundsException ex) {
1656: }
1657: try {
1658: reader.read(array, Integer.MAX_VALUE, Integer.MAX_VALUE);
1659: fail();
1660: } catch (IndexOutOfBoundsException ex) {
1661: }
1662:
1663: assertEquals(0, reader.read(array, 0, 0));
1664: assertEquals(0, array[0]);
1665: assertEquals(0, array[1]);
1666: assertEquals(0, array[2]);
1667:
1668: reader.skip(9);
1669: assertEquals(-1, reader.read(array, 0, 1));
1670:
1671: reader.reset();
1672: array = new char[30];
1673: assertEquals(9, reader.read(array, 0, 30));
1674: }
1675:
1676: //-----------------------------------------------------------------------
1677: public void testAsWriter() throws Exception {
1678: StrBuilder sb = new StrBuilder("base");
1679: Writer writer = sb.asWriter();
1680:
1681: writer.write('l');
1682: assertEquals("basel", sb.toString());
1683:
1684: writer.write(new char[] { 'i', 'n' });
1685: assertEquals("baselin", sb.toString());
1686:
1687: writer.write(new char[] { 'n', 'e', 'r' }, 1, 2);
1688: assertEquals("baseliner", sb.toString());
1689:
1690: writer.write(" rout");
1691: assertEquals("baseliner rout", sb.toString());
1692:
1693: writer.write("ping that server", 1, 3);
1694: assertEquals("baseliner routing", sb.toString());
1695:
1696: writer.flush(); // no effect
1697: assertEquals("baseliner routing", sb.toString());
1698:
1699: writer.close(); // no effect
1700: assertEquals("baseliner routing", sb.toString());
1701:
1702: writer.write(" hi"); // works after close
1703: assertEquals("baseliner routing hi", sb.toString());
1704:
1705: sb.setLength(4); // mix and match
1706: writer.write('d');
1707: assertEquals("based", sb.toString());
1708: }
1709:
1710: //-----------------------------------------------------------------------
1711: public void testEqualsIgnoreCase() {
1712: StrBuilder sb1 = new StrBuilder();
1713: StrBuilder sb2 = new StrBuilder();
1714: assertEquals(true, sb1.equalsIgnoreCase(sb1));
1715: assertEquals(true, sb1.equalsIgnoreCase(sb2));
1716: assertEquals(true, sb2.equalsIgnoreCase(sb2));
1717:
1718: sb1.append("abc");
1719: assertEquals(false, sb1.equalsIgnoreCase(sb2));
1720:
1721: sb2.append("ABC");
1722: assertEquals(true, sb1.equalsIgnoreCase(sb2));
1723:
1724: sb2.clear().append("abc");
1725: assertEquals(true, sb1.equalsIgnoreCase(sb2));
1726: assertEquals(true, sb1.equalsIgnoreCase(sb1));
1727: assertEquals(true, sb2.equalsIgnoreCase(sb2));
1728:
1729: sb2.clear().append("aBc");
1730: assertEquals(true, sb1.equalsIgnoreCase(sb2));
1731: }
1732:
1733: //-----------------------------------------------------------------------
1734: public void testEquals() {
1735: StrBuilder sb1 = new StrBuilder();
1736: StrBuilder sb2 = new StrBuilder();
1737: assertEquals(true, sb1.equals(sb2));
1738: assertEquals(true, sb1.equals(sb1));
1739: assertEquals(true, sb2.equals(sb2));
1740: assertEquals(true, sb1.equals((Object) sb2));
1741:
1742: sb1.append("abc");
1743: assertEquals(false, sb1.equals(sb2));
1744: assertEquals(false, sb1.equals((Object) sb2));
1745:
1746: sb2.append("ABC");
1747: assertEquals(false, sb1.equals(sb2));
1748: assertEquals(false, sb1.equals((Object) sb2));
1749:
1750: sb2.clear().append("abc");
1751: assertEquals(true, sb1.equals(sb2));
1752: assertEquals(true, sb1.equals((Object) sb2));
1753:
1754: assertEquals(false, sb1.equals(new Integer(1)));
1755: assertEquals(false, sb1.equals("abc"));
1756: }
1757:
1758: //-----------------------------------------------------------------------
1759: public void testHashCode() {
1760: StrBuilder sb = new StrBuilder();
1761: int hc1a = sb.hashCode();
1762: int hc1b = sb.hashCode();
1763: assertEquals(0, hc1a);
1764: assertEquals(hc1a, hc1b);
1765:
1766: sb.append("abc");
1767: int hc2a = sb.hashCode();
1768: int hc2b = sb.hashCode();
1769: assertEquals(true, hc2a != 0);
1770: assertEquals(hc2a, hc2b);
1771: }
1772:
1773: //-----------------------------------------------------------------------
1774: public void testToString() {
1775: StrBuilder sb = new StrBuilder("abc");
1776: assertEquals("abc", sb.toString());
1777: }
1778:
1779: //-----------------------------------------------------------------------
1780: public void testToStringBuffer() {
1781: StrBuilder sb = new StrBuilder();
1782: assertEquals(new StringBuffer().toString(), sb.toStringBuffer()
1783: .toString());
1784:
1785: sb.append("junit");
1786: assertEquals(new StringBuffer("junit").toString(), sb
1787: .toStringBuffer().toString());
1788: }
1789:
1790: //-----------------------------------------------------------------------
1791: public void testLang294() {
1792: StrBuilder sb = new StrBuilder(
1793: "\n%BLAH%\nDo more stuff\neven more stuff\n%BLAH%\n");
1794: sb.deleteAll("\n%BLAH%");
1795: assertEquals("\nDo more stuff\neven more stuff\n", sb
1796: .toString());
1797: }
1798:
1799: public void testIndexOfLang294() {
1800: StrBuilder sb = new StrBuilder("onetwothree");
1801: sb.deleteFirst("three");
1802: assertEquals(-1, sb.indexOf("three"));
1803: }
1804:
1805: //-----------------------------------------------------------------------
1806: public void testLang295() {
1807: StrBuilder sb = new StrBuilder("onetwothree");
1808: sb.deleteFirst("three");
1809: assertFalse(
1810: "The contains(char) method is looking beyond the end of the string",
1811: sb.contains('h'));
1812: assertEquals(
1813: "The indexOf(char) method is looking beyond the end of the string",
1814: -1, sb.indexOf('h'));
1815: }
1816:
1817: }
|