KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > lang > _StringTestCase


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.lang;
33
34 import java.util.Locale JavaDoc;
35 import junit.framework.TestCase;
36
37 /**
38  * @author Taras Puchko
39  */

40 public class _StringTestCase extends TestCase {
41
42     public void testConvertConstructorArguments() throws Exception JavaDoc {
43         assertEquals("b\uD834\uDD1Ec", new String JavaDoc(new int[]{'a', 'b', 0x1D11E, 'c', 'd'}, 1, 3));
44         try {
45             new String JavaDoc(new int[]{-1, 'b'}, 0, 1);
46             fail();
47         } catch (IllegalArgumentException JavaDoc e) {
48             //ok
49
}
50         try {
51             new String JavaDoc(new int[]{'a', 'b'}, -1, 1);
52             fail();
53         } catch (IndexOutOfBoundsException JavaDoc e) {
54             //ok
55
}
56         try {
57             new String JavaDoc(new int[]{'a', 'b'}, 1, -1);
58             fail();
59         } catch (IndexOutOfBoundsException JavaDoc e) {
60             //ok
61
}
62         try {
63             new String JavaDoc(new int[]{'a', 'b'}, 1, 2);
64             fail();
65         } catch (IndexOutOfBoundsException JavaDoc e) {
66             //ok
67
}
68     }
69
70     public void testCodePointAt() throws Exception JavaDoc {
71         assertEquals('b', "ab".codePointAt(1));
72         assertEquals(0x1D11E, "b\uD834\uDD1Ec".codePointAt(1));
73         assertEquals(0xD834, "b\uD834".codePointAt(1));
74         try {
75             "abc".codePointAt(-1);
76             fail();
77         } catch (IndexOutOfBoundsException JavaDoc e) {
78             //ok
79
}
80         try {
81             "abc".codePointAt(3);
82             fail();
83         } catch (IndexOutOfBoundsException JavaDoc e) {
84             //ok
85
}
86     }
87
88     public void testCodePointBefore() throws Exception JavaDoc {
89         assertEquals('a', "ab".codePointBefore(1));
90         assertEquals(0x1D11E, "b\uD834\uDD1Ec".codePointBefore(3));
91         assertEquals(0xDD1E, "\uDD1E".codePointBefore(1));
92         try {
93             "abc".codePointBefore(0);
94             fail();
95         } catch (IndexOutOfBoundsException JavaDoc e) {
96             //ok
97
}
98         try {
99             "abc".codePointBefore(4);
100             fail();
101         } catch (IndexOutOfBoundsException JavaDoc e) {
102             //ok
103
}
104     }
105
106     public void testCodePointCount() throws Exception JavaDoc {
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 JavaDoc e) {
115             //ok
116
}
117         try {
118             "abc".codePointCount(1, 0);
119             fail();
120         } catch (IndexOutOfBoundsException JavaDoc e) {
121             //ok
122
}
123         try {
124             "abc".codePointCount(5, 5);
125             fail();
126         } catch (IndexOutOfBoundsException JavaDoc e) {
127             //ok
128
}
129         try {
130             "abc".codePointCount(1, 5);
131             fail();
132         } catch (IndexOutOfBoundsException JavaDoc e) {
133             //ok
134
}
135     }
136
137     public void testContains() throws Exception JavaDoc {
138         assertTrue("abcd".contains("bc"));
139         assertFalse("abcd".contains("xy"));
140     }
141
142     public void testContentEquals() throws Exception JavaDoc {
143         assertTrue("abcd".contentEquals("abcd"));
144         assertFalse("abcd".contentEquals("xy"));
145     }
146
147     public void testFormat() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc e) {
160             //ok
161
}
162         try {
163             "abc".offsetByCodePoints(10, 0);
164             fail();
165         } catch (IndexOutOfBoundsException JavaDoc e) {
166             //ok
167
}
168         try {
169             "abc".offsetByCodePoints(0, 5);
170             fail();
171         } catch (IndexOutOfBoundsException JavaDoc e) {
172             //ok
173
}
174         try {
175             "abc".offsetByCodePoints(2, -5);
176             fail();
177         } catch (IndexOutOfBoundsException JavaDoc e) {
178             //ok
179
}
180     }
181
182     public void testReplace() throws Exception JavaDoc {
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 }
Popular Tags