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 java.util.Locale;
034: import junit.framework.TestCase;
035:
036: /**
037: * @author Taras Puchko
038: */
039: public class _StringTestCase extends TestCase {
040:
041: public void testConvertConstructorArguments() throws Exception {
042: assertEquals("b\uD834\uDD1Ec", new String(new int[] { 'a', 'b',
043: 0x1D11E, 'c', 'd' }, 1, 3));
044: try {
045: new String(new int[] { -1, 'b' }, 0, 1);
046: fail();
047: } catch (IllegalArgumentException e) {
048: //ok
049: }
050: try {
051: new String(new int[] { 'a', 'b' }, -1, 1);
052: fail();
053: } catch (IndexOutOfBoundsException e) {
054: //ok
055: }
056: try {
057: new String(new int[] { 'a', 'b' }, 1, -1);
058: fail();
059: } catch (IndexOutOfBoundsException e) {
060: //ok
061: }
062: try {
063: new String(new int[] { 'a', 'b' }, 1, 2);
064: fail();
065: } catch (IndexOutOfBoundsException e) {
066: //ok
067: }
068: }
069:
070: public void testCodePointAt() throws Exception {
071: assertEquals('b', "ab".codePointAt(1));
072: assertEquals(0x1D11E, "b\uD834\uDD1Ec".codePointAt(1));
073: assertEquals(0xD834, "b\uD834".codePointAt(1));
074: try {
075: "abc".codePointAt(-1);
076: fail();
077: } catch (IndexOutOfBoundsException e) {
078: //ok
079: }
080: try {
081: "abc".codePointAt(3);
082: fail();
083: } catch (IndexOutOfBoundsException e) {
084: //ok
085: }
086: }
087:
088: public void testCodePointBefore() throws Exception {
089: assertEquals('a', "ab".codePointBefore(1));
090: assertEquals(0x1D11E, "b\uD834\uDD1Ec".codePointBefore(3));
091: assertEquals(0xDD1E, "\uDD1E".codePointBefore(1));
092: try {
093: "abc".codePointBefore(0);
094: fail();
095: } catch (IndexOutOfBoundsException e) {
096: //ok
097: }
098: try {
099: "abc".codePointBefore(4);
100: fail();
101: } catch (IndexOutOfBoundsException e) {
102: //ok
103: }
104: }
105:
106: public void testCodePointCount() throws Exception {
107: assertEquals(2, "abcd".codePointCount(1, 3));
108: assertEquals(3, "b\uD834\uDD1Ec".codePointCount(0, 4));
109: assertEquals(2, "b\uD834\uDD1Ec".codePointCount(0, 2));
110: assertEquals(2, "b\uD834".codePointCount(0, 2));
111: try {
112: "abc".codePointCount(-1, 1);
113: fail();
114: } catch (IndexOutOfBoundsException e) {
115: //ok
116: }
117: try {
118: "abc".codePointCount(1, 0);
119: fail();
120: } catch (IndexOutOfBoundsException e) {
121: //ok
122: }
123: try {
124: "abc".codePointCount(5, 5);
125: fail();
126: } catch (IndexOutOfBoundsException e) {
127: //ok
128: }
129: try {
130: "abc".codePointCount(1, 5);
131: fail();
132: } catch (IndexOutOfBoundsException e) {
133: //ok
134: }
135: }
136:
137: public void testContains() throws Exception {
138: assertTrue("abcd".contains("bc"));
139: assertFalse("abcd".contains("xy"));
140: }
141:
142: public void testContentEquals() throws Exception {
143: assertTrue("abcd".contentEquals("abcd"));
144: assertFalse("abcd".contentEquals("xy"));
145: }
146:
147: public void testFormat() throws Exception {
148: assertEquals("1234", String.format("%d", 1234));
149: assertEquals("1.234", String.format(Locale.GERMAN, "%,d", 1234));
150: }
151:
152: public void testOffsetByCodePoints() throws Exception {
153: assertEquals(3, "abc".offsetByCodePoints(1, 2));
154: assertEquals(1, "abc".offsetByCodePoints(1, 0));
155: assertEquals(3, "b\uD834\uDD1Ec".offsetByCodePoints(0, 2));
156: try {
157: "abc".offsetByCodePoints(-1, 1);
158: fail();
159: } catch (IndexOutOfBoundsException e) {
160: //ok
161: }
162: try {
163: "abc".offsetByCodePoints(10, 0);
164: fail();
165: } catch (IndexOutOfBoundsException e) {
166: //ok
167: }
168: try {
169: "abc".offsetByCodePoints(0, 5);
170: fail();
171: } catch (IndexOutOfBoundsException e) {
172: //ok
173: }
174: try {
175: "abc".offsetByCodePoints(2, -5);
176: fail();
177: } catch (IndexOutOfBoundsException e) {
178: //ok
179: }
180: }
181:
182: public void testReplace() throws Exception {
183: assertEquals("axydxy", "abcdbc".replace("bc", "xy"));
184: assertEquals("abc", "abc*".replace("*", ""));
185: assertEquals("XaXbXcX", "abc".replace("", "X"));
186: assertEquals("ac", "abc".replace("b", ""));
187: assertEquals("abc", "abc".replace("", ""));
188: }
189:
190: }
|