1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package libcore.java.lang;
19
20import junit.framework.TestCase;
21
22public class OldStringBufferTest extends TestCase {
23
24    StringBuffer testBuffer = new StringBuffer("This is a test buffer");
25
26    public void test_deleteCharAtI() {
27        try {
28            testBuffer.deleteCharAt(testBuffer.length() + 1);
29            fail("StringIndexOutOfBoundsException was not thrown.");
30        } catch(StringIndexOutOfBoundsException sioobe) {
31            //expected
32        }
33
34        try {
35            testBuffer.deleteCharAt(-1);
36            fail("StringIndexOutOfBoundsException was not thrown.");
37        } catch(StringIndexOutOfBoundsException sioobe) {
38            //expected
39        }
40    }
41
42    public void test_ensureCapacityI() {
43        StringBuffer sb = new StringBuffer(10);
44        sb.ensureCapacity(-2);
45        assertEquals("Failed to increase capacity.", 10, sb.capacity());
46    }
47
48    public void test_getCharsII$CI() {
49        StringBuffer buf2 = new StringBuffer("");
50        try {
51            buf2.getChars(-1, 0, new char[5], 2);
52            fail("IndexOutOfBoundsException is not thrown.");
53        } catch (IndexOutOfBoundsException e) {
54            //expected
55        }
56
57        try {
58            buf2.getChars(0, -1, new char[5], 2);
59            fail("IndexOutOfBoundsException is not thrown.");
60        } catch (IndexOutOfBoundsException e) {
61            //expected
62        }
63
64        try {
65            buf2.getChars(0, -1, new char[5], 2);
66            fail("IndexOutOfBoundsException is not thrown.");
67        } catch (IndexOutOfBoundsException e) {
68            //expected
69        }
70
71        try {
72            buf2.getChars(2, 1, new char[5], 2);
73            fail("IndexOutOfBoundsException is not thrown.");
74        } catch (IndexOutOfBoundsException e) {
75            //expected
76        }
77
78        try {
79            buf2.getChars(0, 6, new char[5], 2);
80            fail("IndexOutOfBoundsException is not thrown.");
81        } catch (IndexOutOfBoundsException e) {
82            //expected
83        }
84
85        try {
86            buf2.getChars(0, 6, new char[10], 5);
87            fail("IndexOutOfBoundsException is not thrown.");
88        } catch (IndexOutOfBoundsException e) {
89            //expected
90        }
91    }
92
93    public void test_insertID() {
94        try {
95            testBuffer.insert(-1, Double.MAX_VALUE);
96            fail("StringIndexOutOfBoundsException is not thrown.");
97        } catch(StringIndexOutOfBoundsException sioobe) {
98            //expected
99        }
100
101        try {
102            testBuffer.insert(testBuffer.length() + 1, Double.MAX_VALUE);
103            fail("StringIndexOutOfBoundsException is not thrown.");
104        } catch(StringIndexOutOfBoundsException sioobe) {
105            //expected
106        }
107    }
108
109    public void test_insertIF() {
110        try {
111            testBuffer.insert(-1, Float.MAX_VALUE);
112            fail("StringIndexOutOfBoundsException is not thrown.");
113        } catch(StringIndexOutOfBoundsException sioobe) {
114            //expected
115        }
116
117        try {
118            testBuffer.insert(testBuffer.length() + 1, Float.MAX_VALUE);
119            fail("StringIndexOutOfBoundsException is not thrown.");
120        } catch(StringIndexOutOfBoundsException sioobe) {
121            //expected
122        }
123    }
124
125    public void test_insertII() {
126        try {
127            testBuffer.insert(-1, Integer.MAX_VALUE);
128            fail("StringIndexOutOfBoundsException is not thrown.");
129        } catch(StringIndexOutOfBoundsException sioobe) {
130            //expected
131        }
132
133        try {
134            testBuffer.insert(testBuffer.length() + 1, Integer.MAX_VALUE);
135            fail("StringIndexOutOfBoundsException is not thrown.");
136        } catch(StringIndexOutOfBoundsException sioobe) {
137            //expected
138        }
139    }
140
141    public void test_insertIJ() {
142        try {
143            testBuffer.insert(-1, Long.MAX_VALUE);
144            fail("StringIndexOutOfBoundsException is not thrown.");
145        } catch(StringIndexOutOfBoundsException sioobe) {
146            //expected
147        }
148
149        try {
150            testBuffer.insert(testBuffer.length() + 1, Long.MAX_VALUE);
151            fail("StringIndexOutOfBoundsException is not thrown.");
152        } catch(StringIndexOutOfBoundsException sioobe) {
153            //expected
154        }
155    }
156
157    public void test_insertILjava_lang_Object() {
158        Object obj1 = new Object();
159        try {
160            testBuffer.insert(-1, obj1);
161            fail("StringIndexOutOfBoundsException is not thrown.");
162        } catch(StringIndexOutOfBoundsException sioobe) {
163            //expected
164        }
165
166        try {
167            testBuffer.insert(testBuffer.length() + 1, obj1);
168            fail("StringIndexOutOfBoundsException is not thrown.");
169        } catch(StringIndexOutOfBoundsException sioobe) {
170            //expected
171        }
172    }
173
174    public void test_insertILjava_lang_String() {
175        try {
176            testBuffer.insert(-1, "");
177            fail("StringIndexOutOfBoundsException is not thrown.");
178        } catch(StringIndexOutOfBoundsException sioobe) {
179            //expected
180        }
181
182        try {
183            testBuffer.insert(testBuffer.length() + 1, "");
184            fail("StringIndexOutOfBoundsException is not thrown.");
185        } catch(StringIndexOutOfBoundsException sioobe) {
186            //expected
187        }
188    }
189
190    public void test_insertIZ() {
191        try {
192            testBuffer.insert(testBuffer.length() + 1, true);
193            fail("StringIndexOutOfBoundsException is not thrown.");
194        } catch(StringIndexOutOfBoundsException sioobe) {
195            //expected
196        }
197
198        try {
199            testBuffer.insert(-1, true);
200            fail("StringIndexOutOfBoundsException is not thrown.");
201        } catch(StringIndexOutOfBoundsException sioobe) {
202            //expected
203        }
204    }
205
206    public void test_replaceIILjava_lang_String() {
207        try {
208            testBuffer.replace(-1, 0, "text");
209            fail("StringIndexOutOfBoundsException is not thrown.");
210        } catch(StringIndexOutOfBoundsException sioobe) {
211            //expected
212        }
213
214        try {
215            testBuffer.replace(0, -1, "text");
216            fail("StringIndexOutOfBoundsException is not thrown.");
217        } catch(StringIndexOutOfBoundsException sioobe) {
218            //expected
219        }
220
221        try {
222            testBuffer.replace(2, 1, "text");
223            fail("StringIndexOutOfBoundsException is not thrown.");
224        } catch(StringIndexOutOfBoundsException sioobe) {
225            //expected
226        }
227
228        try {
229            testBuffer.replace(testBuffer.length() + 1, testBuffer.length() + 1,
230                    "text");
231            fail("StringIndexOutOfBoundsException is not thrown.");
232        } catch(StringIndexOutOfBoundsException sioobe) {
233            //expected
234        }
235    }
236
237    public void test_setCharAtIC() {
238        StringBuffer s = new StringBuffer("HelloWorld");
239        try {
240            s.setCharAt(-1, 'Z');
241            fail("IndexOutOfBoundsException is not thrown.");
242        } catch(IndexOutOfBoundsException ioobe) {
243            //expected
244        }
245        try {
246            s.setCharAt(s.length() + 1, 'Z');
247            fail("IndexOutOfBoundsException is not thrown.");
248        } catch(IndexOutOfBoundsException ioobe) {
249            //expected
250        }
251    }
252
253    public void test_substringI() {
254        try {
255            testBuffer.substring(testBuffer.length() + 1);
256            fail("StringIndexOutOfBoundsException is not thrown.");
257        } catch(StringIndexOutOfBoundsException oobe) {
258            //expected
259        }
260
261        try {
262            testBuffer.substring(-1);
263            fail("StringIndexOutOfBoundsException is not thrown.");
264        } catch(StringIndexOutOfBoundsException oobe) {
265            //expected
266        }
267    }
268
269    public void test_substringII() {
270        try {
271            testBuffer.substring(-1, testBuffer.length());
272            fail("StringIndexOutOfBoundsException is not thrown.");
273        } catch(StringIndexOutOfBoundsException oobe) {
274            //expected
275        }
276
277        try {
278            testBuffer.substring(0, -1);
279            fail("StringIndexOutOfBoundsException is not thrown.");
280        } catch(StringIndexOutOfBoundsException oobe) {
281            //expected
282        }
283
284        try {
285            testBuffer.substring(2, 1);
286            fail("StringIndexOutOfBoundsException is not thrown.");
287        } catch(StringIndexOutOfBoundsException oobe) {
288            //expected
289        }
290    }
291
292    public void test_subSequence() {
293        assertEquals("Incorrect substring returned", " is",
294                testBuffer.subSequence(4, 7));
295        assertEquals("Incorrect substring returned", "test buffer",
296                testBuffer.subSequence(10, 21));
297        assertEquals("not identical", "This is a test buffer",
298                testBuffer.subSequence(0, testBuffer.length()));
299
300        try {
301            testBuffer.subSequence(0, Integer.MAX_VALUE);
302            fail("IndexOutOfBoundsException was not thrown.");
303        } catch (IndexOutOfBoundsException ioobe) {
304            //expected
305        }
306
307        try {
308            testBuffer.subSequence(Integer.MAX_VALUE, testBuffer.length());
309            fail("IndexOutOfBoundsException was not thrown.");
310        } catch (IndexOutOfBoundsException ioobe) {
311            //expected
312        }
313
314        try {
315            testBuffer.subSequence(-1, testBuffer.length());
316            fail("IndexOutOfBoundsException was not thrown.");
317        } catch (IndexOutOfBoundsException ioobe) {
318            //expected
319        }
320    }
321}
322