001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.lang;
032:
033: import junit.framework.TestCase;
034:
035: /**
036: * @author Taras Puchko
037: */
038: public class _StringBufferTestCase extends TestCase {
039:
040: public void testConvertConstructorArguments() throws Exception {
041: CharSequence sequence = "abc";
042: assertEquals("abc", new StringBuffer(sequence).toString());
043: sequence = null;
044: try {
045: new StringBuilder(sequence);
046: fail();
047: } catch (NullPointerException e) {
048: //ok
049: }
050: }
051:
052: public void testAppend() throws Exception {
053: CharSequence sequence = "xyz";
054: assertEquals("abcxyz", new StringBuffer("abc").append(sequence)
055: .toString());
056: assertEquals("abcy", new StringBuilder("abc").append(sequence,
057: 1, 2).toString());
058: }
059:
060: public void testAppendCodePoint() throws Exception {
061: assertEquals("abc", new StringBuffer("a").appendCodePoint('b')
062: .append('c').toString());
063: assertEquals("a\uD834\uDD1Eb", new StringBuffer("a")
064: .appendCodePoint(0x1D11E).append('b').toString());
065: try {
066: new StringBuffer().appendCodePoint(-1);
067: fail();
068: } catch (IllegalArgumentException e) {
069: //ok
070: }
071: }
072:
073: public void testCodePointAt() throws Exception {
074: assertEquals('b', new StringBuffer("ab").codePointAt(1));
075: assertEquals(0x1D11E, new StringBuffer("b\uD834\uDD1Ec")
076: .codePointAt(1));
077: assertEquals(0xD834, new StringBuffer("b\uD834").codePointAt(1));
078: try {
079: new StringBuffer("abc").codePointAt(-1);
080: fail();
081: } catch (IndexOutOfBoundsException e) {
082: //ok
083: }
084: try {
085: new StringBuffer("abc").codePointAt(3);
086: fail();
087: } catch (IndexOutOfBoundsException e) {
088: //ok
089: }
090: }
091:
092: public void testCodePointBefore() throws Exception {
093: assertEquals('a', new StringBuffer("ab").codePointBefore(1));
094: assertEquals(0x1D11E, new StringBuffer("b\uD834\uDD1Ec")
095: .codePointBefore(3));
096: assertEquals(0xDD1E, new StringBuffer("\uDD1E")
097: .codePointBefore(1));
098: try {
099: new StringBuffer("abc").codePointBefore(0);
100: fail();
101: } catch (IndexOutOfBoundsException e) {
102: //ok
103: }
104: try {
105: new StringBuffer("abc").codePointBefore(4);
106: fail();
107: } catch (IndexOutOfBoundsException e) {
108: //ok
109: }
110: }
111:
112: public void testCodePointCount() throws Exception {
113: assertEquals(2, new StringBuffer("abcd").codePointCount(1, 3));
114: assertEquals(3, new StringBuffer("b\uD834\uDD1Ec")
115: .codePointCount(0, 4));
116: assertEquals(2, new StringBuffer("b\uD834\uDD1Ec")
117: .codePointCount(0, 2));
118: assertEquals(2, new StringBuffer("b\uD834")
119: .codePointCount(0, 2));
120: try {
121: new StringBuffer("abc").codePointCount(-1, 1);
122: fail();
123: } catch (IndexOutOfBoundsException e) {
124: //ok
125: }
126: try {
127: new StringBuffer("abc").codePointCount(1, 0);
128: fail();
129: } catch (IndexOutOfBoundsException e) {
130: //ok
131: }
132: try {
133: new StringBuffer("abc").codePointCount(5, 5);
134: fail();
135: } catch (IndexOutOfBoundsException e) {
136: //ok
137: }
138: try {
139: new StringBuffer("abc").codePointCount(1, 5);
140: fail();
141: } catch (IndexOutOfBoundsException e) {
142: //ok
143: }
144: }
145:
146: public void testInsert() throws Exception {
147: CharSequence sequence = "xyz";
148: assertEquals("axyzbc", new StringBuffer("abc").insert(1,
149: sequence).toString());
150: assertEquals("aybc", new StringBuilder("abc").insert(1,
151: sequence, 1, 2).toString());
152: }
153:
154: public void testOffsetByCodePoints() throws Exception {
155: assertEquals(3, new StringBuffer("abc")
156: .offsetByCodePoints(1, 2));
157: assertEquals(1, new StringBuffer("abc")
158: .offsetByCodePoints(1, 0));
159: assertEquals(3, new StringBuffer("b\uD834\uDD1Ec")
160: .offsetByCodePoints(0, 2));
161: try {
162: new StringBuffer("abc").offsetByCodePoints(-1, 1);
163: fail();
164: } catch (IndexOutOfBoundsException e) {
165: //ok
166: }
167: try {
168: new StringBuffer("abc").offsetByCodePoints(10, 0);
169: fail();
170: } catch (IndexOutOfBoundsException e) {
171: //ok
172: }
173: try {
174: new StringBuffer("abc").offsetByCodePoints(0, 5);
175: fail();
176: } catch (IndexOutOfBoundsException e) {
177: //ok
178: }
179: try {
180: new StringBuffer("abc").offsetByCodePoints(2, -5);
181: fail();
182: } catch (IndexOutOfBoundsException e) {
183: //ok
184: }
185: }
186:
187: public void testTrimToSize() throws Exception {
188: StringBuffer buffer = new StringBuffer(100);
189: buffer.append("abc");
190: buffer.trimToSize();
191: }
192:
193: }
|