KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
35
36 /**
37  * @author Taras Puchko
38  */

39 public class _StringBufferTestCase extends TestCase {
40
41     public void testConvertConstructorArguments() throws Exception JavaDoc {
42         CharSequence JavaDoc sequence = "abc";
43         assertEquals("abc", new StringBuffer JavaDoc(sequence).toString());
44         sequence = null;
45         try {
46             new StringBuilder JavaDoc(sequence);
47             fail();
48         } catch (NullPointerException JavaDoc e) {
49             //ok
50
}
51     }
52
53     public void testAppend() throws Exception JavaDoc {
54         CharSequence JavaDoc sequence = "xyz";
55         assertEquals("abcxyz", new StringBuffer JavaDoc("abc").append(sequence).toString());
56         assertEquals("abcy", new StringBuilder JavaDoc("abc").append(sequence, 1, 2).toString());
57     }
58
59     public void testAppendCodePoint() throws Exception JavaDoc {
60         assertEquals("abc", new StringBuffer JavaDoc("a").appendCodePoint('b').append('c').toString());
61         assertEquals("a\uD834\uDD1Eb", new StringBuffer JavaDoc("a").appendCodePoint(0x1D11E).append('b').toString());
62         try {
63             new StringBuffer JavaDoc().appendCodePoint(-1);
64             fail();
65         } catch (IllegalArgumentException JavaDoc e) {
66             //ok
67
}
68     }
69
70     public void testCodePointAt() throws Exception JavaDoc {
71         assertEquals('b', new StringBuffer JavaDoc("ab").codePointAt(1));
72         assertEquals(0x1D11E, new StringBuffer JavaDoc("b\uD834\uDD1Ec").codePointAt(1));
73         assertEquals(0xD834, new StringBuffer JavaDoc("b\uD834").codePointAt(1));
74         try {
75             new StringBuffer JavaDoc("abc").codePointAt(-1);
76             fail();
77         } catch (IndexOutOfBoundsException JavaDoc e) {
78             //ok
79
}
80         try {
81             new StringBuffer JavaDoc("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', new StringBuffer JavaDoc("ab").codePointBefore(1));
90         assertEquals(0x1D11E, new StringBuffer JavaDoc("b\uD834\uDD1Ec").codePointBefore(3));
91         assertEquals(0xDD1E, new StringBuffer JavaDoc("\uDD1E").codePointBefore(1));
92         try {
93             new StringBuffer JavaDoc("abc").codePointBefore(0);
94             fail();
95         } catch (IndexOutOfBoundsException JavaDoc e) {
96             //ok
97
}
98         try {
99             new StringBuffer JavaDoc("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, new StringBuffer JavaDoc("abcd").codePointCount(1, 3));
108         assertEquals(3, new StringBuffer JavaDoc("b\uD834\uDD1Ec").codePointCount(0, 4));
109         assertEquals(2, new StringBuffer JavaDoc("b\uD834\uDD1Ec").codePointCount(0, 2));
110         assertEquals(2, new StringBuffer JavaDoc("b\uD834").codePointCount(0, 2));
111         try {
112             new StringBuffer JavaDoc("abc").codePointCount(-1, 1);
113             fail();
114         } catch (IndexOutOfBoundsException JavaDoc e) {
115             //ok
116
}
117         try {
118             new StringBuffer JavaDoc("abc").codePointCount(1, 0);
119             fail();
120         } catch (IndexOutOfBoundsException JavaDoc e) {
121             //ok
122
}
123         try {
124             new StringBuffer JavaDoc("abc").codePointCount(5, 5);
125             fail();
126         } catch (IndexOutOfBoundsException JavaDoc e) {
127             //ok
128
}
129         try {
130             new StringBuffer JavaDoc("abc").codePointCount(1, 5);
131             fail();
132         } catch (IndexOutOfBoundsException JavaDoc e) {
133             //ok
134
}
135     }
136
137     public void testInsert() throws Exception JavaDoc {
138         CharSequence JavaDoc sequence = "xyz";
139         assertEquals("axyzbc", new StringBuffer JavaDoc("abc").insert(1, sequence).toString());
140         assertEquals("aybc", new StringBuilder JavaDoc("abc").insert(1, sequence, 1, 2).toString());
141     }
142
143     public void testOffsetByCodePoints() throws Exception JavaDoc {
144         assertEquals(3, new StringBuffer JavaDoc("abc").offsetByCodePoints(1, 2));
145         assertEquals(1, new StringBuffer JavaDoc("abc").offsetByCodePoints(1, 0));
146         assertEquals(3, new StringBuffer JavaDoc("b\uD834\uDD1Ec").offsetByCodePoints(0, 2));
147         try {
148             new StringBuffer JavaDoc("abc").offsetByCodePoints(-1, 1);
149             fail();
150         } catch (IndexOutOfBoundsException JavaDoc e) {
151             //ok
152
}
153         try {
154             new StringBuffer JavaDoc("abc").offsetByCodePoints(10, 0);
155             fail();
156         } catch (IndexOutOfBoundsException JavaDoc e) {
157             //ok
158
}
159         try {
160             new StringBuffer JavaDoc("abc").offsetByCodePoints(0, 5);
161             fail();
162         } catch (IndexOutOfBoundsException JavaDoc e) {
163             //ok
164
}
165         try {
166             new StringBuffer JavaDoc("abc").offsetByCodePoints(2, -5);
167             fail();
168         } catch (IndexOutOfBoundsException JavaDoc e) {
169             //ok
170
}
171     }
172
173 }
Popular Tags