001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.lang;
019:
020: public class StringBuffer2Test extends junit.framework.TestCase {
021:
022: StringBuffer testBuffer;
023:
024: /**
025: * @tests java.lang.StringBuffer#StringBuffer()
026: */
027: public void test_Constructor() {
028: // Test for method java.lang.StringBuffer()
029: new StringBuffer();
030: assertTrue("Invalid buffer created", true);
031: }
032:
033: /**
034: * @tests java.lang.StringBuffer#StringBuffer(int)
035: */
036: public void test_ConstructorI() {
037: // Test for method java.lang.StringBuffer(int)
038: StringBuffer sb = new StringBuffer(8);
039: assertEquals("Newly constructed buffer is of incorrect length",
040: 0, sb.length());
041: }
042:
043: /**
044: * @tests java.lang.StringBuffer#StringBuffer(java.lang.String)
045: */
046: public void test_ConstructorLjava_lang_String() {
047: // Test for method java.lang.StringBuffer(java.lang.String)
048:
049: StringBuffer sb = new StringBuffer("HelloWorld");
050:
051: assertTrue("Invalid buffer created", sb.length() == 10
052: && (sb.toString().equals("HelloWorld")));
053:
054: boolean pass = false;
055: try {
056: new StringBuffer(null);
057: } catch (NullPointerException e) {
058: pass = true;
059: }
060: assertTrue("Should throw NullPointerException", pass);
061: }
062:
063: /**
064: * @tests java.lang.StringBuffer#append(char[])
065: */
066: public void test_append$C() {
067: // Test for method java.lang.StringBuffer
068: // java.lang.StringBuffer.append(char [])
069: char buf[] = new char[4];
070: "char".getChars(0, 4, buf, 0);
071: testBuffer.append(buf);
072: assertEquals("Append of char[] failed",
073: "This is a test bufferchar", testBuffer.toString());
074: }
075:
076: /**
077: * @tests java.lang.StringBuffer#append(char[], int, int)
078: */
079: public void test_append$CII() {
080: // Test for method java.lang.StringBuffer
081: // java.lang.StringBuffer.append(char [], int, int)
082: StringBuffer sb = new StringBuffer();
083: char[] buf1 = { 'H', 'e', 'l', 'l', 'o' };
084: char[] buf2 = { 'W', 'o', 'r', 'l', 'd' };
085: sb.append(buf1, 0, buf1.length);
086: assertEquals("Buffer is invalid length after append", 5, sb
087: .length());
088: sb.append(buf2, 0, buf2.length);
089: assertEquals("Buffer is invalid length after append", 10, sb
090: .length());
091: assertTrue("Buffer contains invalid chars", (sb.toString()
092: .equals("HelloWorld")));
093: }
094:
095: /**
096: * @tests java.lang.StringBuffer#append(char)
097: */
098: public void test_appendC() {
099: // Test for method java.lang.StringBuffer
100: // java.lang.StringBuffer.append(char)
101: StringBuffer sb = new StringBuffer();
102: char buf1 = 'H';
103: char buf2 = 'W';
104: sb.append(buf1);
105: assertEquals("Buffer is invalid length after append", 1, sb
106: .length());
107: sb.append(buf2);
108: assertEquals("Buffer is invalid length after append", 2, sb
109: .length());
110: assertTrue("Buffer contains invalid chars", (sb.toString()
111: .equals("HW")));
112: }
113:
114: /**
115: * @tests java.lang.StringBuffer#append(double)
116: */
117: public void test_appendD() {
118: // Test for method java.lang.StringBuffer
119: // java.lang.StringBuffer.append(double)
120: StringBuffer sb = new StringBuffer();
121: sb.append(Double.MAX_VALUE);
122: assertEquals("Buffer is invalid length after append", 22, sb
123: .length());
124: assertEquals("Buffer contains invalid characters",
125: "1.7976931348623157E308", sb.toString());
126: }
127:
128: /**
129: * @tests java.lang.StringBuffer#append(float)
130: */
131: public void test_appendF() {
132: // Test for method java.lang.StringBuffer
133: // java.lang.StringBuffer.append(float)
134: StringBuffer sb = new StringBuffer();
135: final float floatNum = 900.87654F;
136: sb.append(floatNum);
137: assertTrue("Buffer is invalid length after append: "
138: + sb.length(), sb.length() == String.valueOf(floatNum)
139: .length());
140: assertTrue("Buffer contains invalid characters", sb.toString()
141: .equals(String.valueOf(floatNum)));
142: }
143:
144: /**
145: * @tests java.lang.StringBuffer#append(int)
146: */
147: public void test_appendI() {
148: // Test for method java.lang.StringBuffer
149: // java.lang.StringBuffer.append(int)
150: StringBuffer sb = new StringBuffer();
151: sb.append(9000);
152: assertEquals("Buffer is invalid length after append", 4, sb
153: .length());
154: sb.append(1000);
155: assertEquals("Buffer is invalid length after append", 8, sb
156: .length());
157: assertEquals("Buffer contains invalid characters", "90001000",
158: sb.toString());
159: }
160:
161: /**
162: * @tests java.lang.StringBuffer#append(long)
163: */
164: public void test_appendJ() {
165: // Test for method java.lang.StringBuffer
166: // java.lang.StringBuffer.append(long)
167:
168: StringBuffer sb = new StringBuffer();
169: long t = 927654321098L;
170: sb.append(t);
171: assertEquals("Buffer is of invlaid length", 12, sb.length());
172: assertEquals("Buffer contains invalid characters",
173: "927654321098", sb.toString());
174: }
175:
176: /**
177: * @tests java.lang.StringBuffer#append(java.lang.Object)
178: */
179: public void test_appendLjava_lang_Object() {
180: // Test for method java.lang.StringBuffer
181: // java.lang.StringBuffer.append(java.lang.Object)
182: StringBuffer sb = new StringBuffer();
183: Object obj1 = new Object();
184: Object obj2 = new Object();
185: sb.append(obj1);
186: sb.append(obj2);
187: assertTrue("Buffer contains invalid characters", sb.toString()
188: .equals(obj1.toString() + obj2.toString()));
189: }
190:
191: /**
192: * @tests java.lang.StringBuffer#append(java.lang.String)
193: */
194: public void test_appendLjava_lang_String() {
195: // Test for method java.lang.StringBuffer
196: // java.lang.StringBuffer.append(java.lang.String)
197: StringBuffer sb = new StringBuffer();
198: String buf1 = "Hello";
199: String buf2 = "World";
200: sb.append(buf1);
201: assertEquals("Buffer is invalid length after append", 5, sb
202: .length());
203: sb.append(buf2);
204: assertEquals("Buffer is invalid length after append", 10, sb
205: .length());
206: assertTrue("Buffer contains invalid chars", (sb.toString()
207: .equals("HelloWorld")));
208: }
209:
210: /**
211: * @tests java.lang.StringBuffer#append(boolean)
212: */
213: public void test_appendZ() {
214: // Test for method java.lang.StringBuffer
215: // java.lang.StringBuffer.append(boolean)
216: StringBuffer sb = new StringBuffer();
217: sb.append(false);
218: assertEquals("Buffer is invalid length after append", 5, sb
219: .length());
220: sb.append(true);
221: assertEquals("Buffer is invalid length after append", 9, sb
222: .length());
223: assertTrue("Buffer is invalid length after append", (sb
224: .toString().equals("falsetrue")));
225: }
226:
227: /**
228: * @tests java.lang.StringBuffer#capacity()
229: */
230: public void test_capacity() {
231: // Test for method int java.lang.StringBuffer.capacity()
232: StringBuffer sb = new StringBuffer(10);
233: assertEquals("Returned incorrect capacity", 10, sb.capacity());
234: sb.ensureCapacity(100);
235: assertTrue("Returned incorrect capacity", sb.capacity() >= 100);
236: }
237:
238: /**
239: * @tests java.lang.StringBuffer#charAt(int)
240: */
241: public void test_charAtI() {
242: // Test for method char java.lang.StringBuffer.charAt(int)
243: assertEquals("Returned incorrect char", 's', testBuffer
244: .charAt(3));
245:
246: // Test for StringIndexOutOfBoundsException
247: boolean exception = false;
248: try {
249: testBuffer.charAt(-1);
250: } catch (StringIndexOutOfBoundsException e) {
251: exception = true;
252: } catch (ArrayIndexOutOfBoundsException e) {
253: }
254: assertTrue("Should throw StringIndexOutOfBoundsException",
255: exception);
256: }
257:
258: /**
259: * @tests java.lang.StringBuffer#delete(int, int)
260: */
261: public void test_deleteII() {
262: // Test for method java.lang.StringBuffer
263: // java.lang.StringBuffer.delete(int, int)
264: testBuffer.delete(7, 7);
265: assertEquals("Deleted chars when start == end",
266: "This is a test buffer", testBuffer.toString());
267: testBuffer.delete(4, 14);
268: assertEquals("Deleted incorrect chars", "This buffer",
269: testBuffer.toString());
270:
271: testBuffer = new StringBuffer("This is a test buffer");
272: String sharedStr = testBuffer.toString();
273: testBuffer.delete(0, testBuffer.length());
274: assertEquals("Didn't clone shared buffer",
275: "This is a test buffer", sharedStr);
276: assertTrue("Deleted incorrect chars", testBuffer.toString()
277: .equals(""));
278: testBuffer.append("more stuff");
279: assertEquals("Didn't clone shared buffer 2",
280: "This is a test buffer", sharedStr);
281: assertEquals("Wrong contents", "more stuff", testBuffer
282: .toString());
283: try {
284: testBuffer.delete(-5, 2);
285: } catch (IndexOutOfBoundsException e) {
286: }
287: assertEquals("Wrong contents 2", "more stuff", testBuffer
288: .toString());
289: }
290:
291: /**
292: * @tests java.lang.StringBuffer#deleteCharAt(int)
293: */
294: public void test_deleteCharAtI() {
295: // Test for method java.lang.StringBuffer
296: // java.lang.StringBuffer.deleteCharAt(int)
297: testBuffer.deleteCharAt(3);
298: assertEquals("Deleted incorrect char", "Thi is a test buffer",
299: testBuffer.toString());
300: }
301:
302: /**
303: * @tests java.lang.StringBuffer#ensureCapacity(int)
304: */
305: public void test_ensureCapacityI() {
306: // Test for method void java.lang.StringBuffer.ensureCapacity(int)
307: StringBuffer sb = new StringBuffer(10);
308:
309: sb.ensureCapacity(100);
310: assertTrue("Failed to increase capacity", sb.capacity() >= 100);
311: }
312:
313: /**
314: * @tests java.lang.StringBuffer#getChars(int, int, char[], int)
315: */
316: public void test_getCharsII$CI() {
317: // Test for method void java.lang.StringBuffer.getChars(int, int, char
318: // [], int)
319:
320: char[] buf = new char[10];
321: testBuffer.getChars(4, 8, buf, 2);
322: assertTrue("Returned incorrect chars", new String(buf, 2, 4)
323: .equals(testBuffer.toString().substring(4, 8)));
324:
325: boolean exception = false;
326: try {
327: StringBuffer buf2 = new StringBuffer("");
328: buf2.getChars(0, 0, new char[5], 2);
329: } catch (IndexOutOfBoundsException e) {
330: exception = true;
331: }
332: assertTrue("did not expect IndexOutOfBoundsException",
333: !exception);
334: }
335:
336: /**
337: * @tests java.lang.StringBuffer#insert(int, char[])
338: */
339: public void test_insertI$C() {
340: // Test for method java.lang.StringBuffer
341: // java.lang.StringBuffer.insert(int, char [])
342: char buf[] = new char[4];
343: "char".getChars(0, 4, buf, 0);
344: testBuffer.insert(15, buf);
345: assertEquals("Insert test failed", "This is a test charbuffer",
346: testBuffer.toString());
347:
348: boolean exception = false;
349: StringBuffer buf1 = new StringBuffer("abcd");
350: try {
351: buf1.insert(-1, (char[]) null);
352: } catch (StringIndexOutOfBoundsException e) {
353: exception = true;
354: } catch (NullPointerException e) {
355: }
356: assertTrue("Should throw StringIndexOutOfBoundsException",
357: exception);
358: }
359:
360: /**
361: * @tests java.lang.StringBuffer#insert(int, char[], int, int)
362: */
363: public void test_insertI$CII() {
364: // Test for method java.lang.StringBuffer
365: // java.lang.StringBuffer.insert(int, char [], int, int)
366: char[] c = new char[] { 'n', 'o', 't', ' ' };
367: testBuffer.insert(8, c, 0, 4);
368: assertEquals("This is not a test buffer", testBuffer.toString());
369:
370: StringBuffer buf1 = new StringBuffer("abcd");
371: try {
372: buf1.insert(-1, (char[]) null, 0, 0);
373: fail("Should throw StringIndexOutOfBoundsException");
374: } catch (StringIndexOutOfBoundsException e) {
375: //expected
376: }
377:
378: try {
379: testBuffer.insert(testBuffer.length() - 1, c, -1, 1);
380: } catch (StringIndexOutOfBoundsException e) {
381: //expected
382: }
383:
384: }
385:
386: /**
387: * @tests java.lang.StringBuffer#insert(int, char)
388: */
389: public void test_insertIC() {
390: // Test for method java.lang.StringBuffer
391: // java.lang.StringBuffer.insert(int, char)
392: testBuffer.insert(15, 'T');
393: assertEquals("Insert test failed", "This is a test Tbuffer",
394: testBuffer.toString());
395: }
396:
397: /**
398: * @tests java.lang.StringBuffer#insert(int, double)
399: */
400: public void test_insertID() {
401: // Test for method java.lang.StringBuffer
402: // java.lang.StringBuffer.insert(int, double)
403: testBuffer.insert(15, Double.MAX_VALUE);
404: assertTrue("Insert test failed", testBuffer.toString().equals(
405: "This is a test " + Double.MAX_VALUE + "buffer"));
406: }
407:
408: /**
409: * @tests java.lang.StringBuffer#insert(int, float)
410: */
411: public void test_insertIF() {
412: // Test for method java.lang.StringBuffer
413: // java.lang.StringBuffer.insert(int, float)
414: testBuffer.insert(15, Float.MAX_VALUE);
415: String testBufferString = testBuffer.toString();
416: String expectedResult = "This is a test "
417: + String.valueOf(Float.MAX_VALUE) + "buffer";
418: assertTrue("Insert test failed, got: " + "\'"
419: + testBufferString + "\'" + " but wanted: " + "\'"
420: + expectedResult + "\'", testBufferString
421: .equals(expectedResult));
422: }
423:
424: /**
425: * @tests java.lang.StringBuffer#insert(int, int)
426: */
427: public void test_insertII() {
428: // Test for method java.lang.StringBuffer
429: // java.lang.StringBuffer.insert(int, int)
430: testBuffer.insert(15, 100);
431: assertEquals("Insert test failed", "This is a test 100buffer",
432: testBuffer.toString());
433: }
434:
435: /**
436: * @tests java.lang.StringBuffer#insert(int, long)
437: */
438: public void test_insertIJ() {
439: // Test for method java.lang.StringBuffer
440: // java.lang.StringBuffer.insert(int, long)
441: testBuffer.insert(15, 88888888888888888L);
442: assertEquals("Insert test failed",
443: "This is a test 88888888888888888buffer", testBuffer
444: .toString());
445: }
446:
447: /**
448: * @tests java.lang.StringBuffer#insert(int, java.lang.Object)
449: */
450: public void test_insertILjava_lang_Object() {
451: // Test for method java.lang.StringBuffer
452: // java.lang.StringBuffer.insert(int, java.lang.Object)
453: Object obj1 = new Object();
454: testBuffer.insert(15, obj1);
455: assertTrue("Insert test failed", testBuffer.toString().equals(
456: "This is a test " + obj1.toString() + "buffer"));
457: }
458:
459: /**
460: * @tests java.lang.StringBuffer#insert(int, java.lang.String)
461: */
462: public void test_insertILjava_lang_String() {
463: // Test for method java.lang.StringBuffer
464: // java.lang.StringBuffer.insert(int, java.lang.String)
465:
466: testBuffer.insert(15, "STRING ");
467: assertEquals("Insert test failed",
468: "This is a test STRING buffer", testBuffer.toString());
469: }
470:
471: /**
472: * @tests java.lang.StringBuffer#insert(int, boolean)
473: */
474: public void test_insertIZ() {
475: // Test for method java.lang.StringBuffer
476: // java.lang.StringBuffer.insert(int, boolean)
477: testBuffer.insert(15, true);
478: assertEquals("Insert test failed", "This is a test truebuffer",
479: testBuffer.toString());
480: }
481:
482: /**
483: * @tests java.lang.StringBuffer#length()
484: */
485: public void test_length() {
486: // Test for method int java.lang.StringBuffer.length()
487: assertEquals("Incorrect length returned", 21, testBuffer
488: .length());
489: }
490:
491: /**
492: * @tests java.lang.StringBuffer#replace(int, int, java.lang.String)
493: */
494: public void test_replaceIILjava_lang_String() {
495: // Test for method java.lang.StringBuffer
496: // java.lang.StringBuffer.replace(int, int, java.lang.String)
497: testBuffer.replace(5, 9, "is a replaced");
498: assertTrue("Replace failed, wanted: " + "\'"
499: + "This is a replaced test buffer" + "\'"
500: + " but got: " + "\'" + testBuffer.toString() + "\'",
501: testBuffer.toString().equals(
502: "This is a replaced test buffer"));
503: assertEquals("insert1", "text", new StringBuffer().replace(0,
504: 0, "text").toString());
505: assertEquals("insert2", "123text", new StringBuffer("123")
506: .replace(3, 3, "text").toString());
507: assertEquals("insert2", "1text23", new StringBuffer("123")
508: .replace(1, 1, "text").toString());
509: }
510:
511: private String writeString(String in) {
512: StringBuffer result = new StringBuffer();
513: result.append("\"");
514: for (int i = 0; i < in.length(); i++) {
515: result.append(" 0x" + Integer.toHexString(in.charAt(i)));
516: }
517: result.append("\"");
518: return result.toString();
519: }
520:
521: private void reverseTest(String id, String org, String rev,
522: String back) {
523: // create non-shared StringBuffer
524: StringBuffer sb = new StringBuffer(org);
525: sb.reverse();
526: String reversed = sb.toString();
527: assertTrue("reversed surrogate " + id + ": "
528: + writeString(reversed), reversed.equals(rev));
529: // create non-shared StringBuffer
530: sb = new StringBuffer(reversed);
531: sb.reverse();
532: reversed = sb.toString();
533: assertTrue("reversed surrogate " + id + "a: "
534: + writeString(reversed), reversed.equals(back));
535:
536: // test algorithm when StringBuffer is shared
537: sb = new StringBuffer(org);
538: String copy = sb.toString();
539: assertEquals(org, copy);
540: sb.reverse();
541: reversed = sb.toString();
542: assertTrue("reversed surrogate " + id + ": "
543: + writeString(reversed), reversed.equals(rev));
544: sb = new StringBuffer(reversed);
545: copy = sb.toString();
546: assertEquals(rev, copy);
547: sb.reverse();
548: reversed = sb.toString();
549: assertTrue("reversed surrogate " + id + "a: "
550: + writeString(reversed), reversed.equals(back));
551:
552: }
553:
554: /**
555: * @tests java.lang.StringBuffer#reverse()
556: */
557: public void test_reverse() {
558: // Test for method java.lang.StringBuffer
559: // java.lang.StringBuffer.reverse()
560: String org;
561: org = "a";
562: reverseTest("0", org, org, org);
563:
564: org = "ab";
565: reverseTest("1", org, "ba", org);
566:
567: org = "abcdef";
568: reverseTest("2", org, "fedcba", org);
569:
570: org = "abcdefg";
571: reverseTest("3", org, "gfedcba", org);
572:
573: }
574:
575: /**
576: * @tests java.lang.StringBuffer#setCharAt(int, char)
577: */
578: public void test_setCharAtIC() {
579: // Test for method void java.lang.StringBuffer.setCharAt(int, char)
580: StringBuffer s = new StringBuffer("HelloWorld");
581: s.setCharAt(4, 'Z');
582: assertEquals("Returned incorrect char", 'Z', s.charAt(4));
583: }
584:
585: /**
586: * @tests java.lang.StringBuffer#setLength(int)
587: */
588: public void test_setLengthI() {
589: // Test for method void java.lang.StringBuffer.setLength(int)
590: testBuffer.setLength(1000);
591: assertEquals("Failed to increase length", 1000, testBuffer
592: .length());
593: assertTrue("Increase in length trashed buffer", testBuffer
594: .toString().startsWith("This is a test buffer"));
595: testBuffer.setLength(2);
596: assertEquals("Failed to decrease length", 2, testBuffer
597: .length());
598: assertEquals("Decrease in length failed", "Th", testBuffer
599: .toString());
600: }
601:
602: /**
603: * @tests java.lang.StringBuffer#substring(int)
604: */
605: public void test_substringI() {
606: // Test for method java.lang.String
607: // java.lang.StringBuffer.substring(int)
608: assertEquals("Returned incorrect substring",
609: "is a test buffer", testBuffer.substring(5));
610: }
611:
612: /**
613: * @tests java.lang.StringBuffer#substring(int, int)
614: */
615: public void test_substringII() {
616: // Test for method java.lang.String
617: // java.lang.StringBuffer.substring(int, int)
618: assertEquals("Returned incorrect substring", "is", testBuffer
619: .substring(5, 7));
620: }
621:
622: /**
623: * @tests java.lang.StringBuffer#toString()
624: */
625: public void test_toString() {
626: // Test for method java.lang.String java.lang.StringBuffer.toString()
627: assertEquals("Incorrect string value returned",
628: "This is a test buffer", testBuffer.toString());
629: }
630:
631: @Override
632: protected void setUp() {
633: testBuffer = new StringBuffer("This is a test buffer");
634: }
635: }
|