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, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.lang;
019:
020: import java.io.UnsupportedEncodingException;
021: import java.lang.reflect.Constructor;
022:
023: import junit.framework.TestCase;
024:
025: public class StringTest extends TestCase {
026:
027: private static final Constructor<String> UNSAFE_CONSTRUCTOR;
028: static {
029: Constructor<String> uc;
030: try {
031: uc = String.class.getDeclaredConstructor(new Class[] {
032: int.class, int.class, char[].class });
033: uc.setAccessible(true);
034: } catch (Exception e) {
035: uc = null;
036: }
037: UNSAFE_CONSTRUCTOR = uc;
038: }
039:
040: private static String newString(int start, int len, char[] data)
041: throws Exception {
042: if (UNSAFE_CONSTRUCTOR == null) {
043: return new String(data, start, len);
044: }
045:
046: return UNSAFE_CONSTRUCTOR.newInstance(Integer.valueOf(start),
047: Integer.valueOf(len), data);
048: }
049:
050: /**
051: * @tests java.lang.String#String()
052: */
053: public void test_Constructor() {
054: assertEquals("Created incorrect string", "", new String());
055: }
056:
057: /**
058: * @tests java.lang.String#String(byte[])
059: */
060: public void test_Constructor$B() {
061: assertEquals("Failed to create string", "HelloWorld",
062: new String("HelloWorld".getBytes()));
063: }
064:
065: /**
066: * @tests java.lang.String#String(byte[], int)
067: */
068: @SuppressWarnings("deprecation")
069: public void test_Constructor$BI() {
070: String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0);
071: assertEquals("Incorrect string returned: " + s, "ABCDE", s);
072: s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1);
073: assertFalse("Did not use nonzero hibyte", s.equals("ABCDE"));
074: }
075:
076: /**
077: * @tests java.lang.String#String(byte[], int, int)
078: */
079: public void test_Constructor$BII() {
080: byte[] hwba = "HelloWorld".getBytes();
081: assertEquals("Failed to create string", "HelloWorld",
082: new String(hwba, 0, hwba.length));
083:
084: try {
085: new String(new byte[0], 0, Integer.MAX_VALUE);
086: fail("No IndexOutOfBoundsException");
087: } catch (IndexOutOfBoundsException e) {
088: }
089: }
090:
091: /**
092: * @tests java.lang.String#String(byte[], int, int, int)
093: */
094: @SuppressWarnings("deprecation")
095: public void test_Constructor$BIII() {
096: String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 1,
097: 3);
098: assertEquals("Incorrect string returned: " + s, "BCD", s);
099: s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1, 0, 5);
100: assertFalse("Did not use nonzero hibyte", s.equals("ABCDE"));
101: }
102:
103: /**
104: * @tests java.lang.String#String(byte[], int, int, java.lang.String)
105: */
106: public void test_Constructor$BIILjava_lang_String()
107: throws Exception {
108: String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5,
109: "8859_1");
110: assertEquals("Incorrect string returned: " + s, "ABCDE", s);
111:
112: try {
113: new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5, "");
114: fail("Should throw UnsupportedEncodingException");
115: } catch (UnsupportedEncodingException e) {
116: //expected
117: }
118: }
119:
120: /**
121: * @tests java.lang.String#String(byte[], java.lang.String)
122: */
123: public void test_Constructor$BLjava_lang_String() throws Exception {
124: String s = new String(new byte[] { 65, 66, 67, 68, 69 },
125: "8859_1");
126: assertEquals("Incorrect string returned: " + s, "ABCDE", s);
127: }
128:
129: /**
130: * @tests java.lang.String#String(char[])
131: */
132: public void test_Constructor$C() {
133: assertEquals("Failed Constructor test", "World", new String(
134: new char[] { 'W', 'o', 'r', 'l', 'd' }));
135: }
136:
137: /**
138: * @tests java.lang.String#String(char[], int, int)
139: */
140: public void test_Constructor$CII() throws Exception {
141: char[] buf = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
142: String s = new String(buf, 0, buf.length);
143: assertEquals("Incorrect string created", "HelloWorld", s);
144:
145: try {
146: new String(new char[0], 0, Integer.MAX_VALUE);
147: fail("No IndexOutOfBoundsException");
148: } catch (IndexOutOfBoundsException e) {
149: }
150: }
151:
152: /**
153: * @tests java.lang.String#String(java.lang.String)
154: */
155: public void test_ConstructorLjava_lang_String() {
156: String s = new String("Hello World");
157: assertEquals("Failed to construct correct string",
158: "Hello World", s);
159: }
160:
161: /**
162: * @tests java.lang.String#String(java.lang.StringBuffer)
163: */
164: public void test_ConstructorLjava_lang_StringBuffer() {
165: StringBuffer sb = new StringBuffer();
166: sb.append("HelloWorld");
167: assertEquals("Created incorrect string", "HelloWorld",
168: new String(sb));
169: }
170:
171: /**
172: * @tests java.lang.String#String(java.lang.StringBuilder)
173: */
174: public void test_ConstructorLjava_lang_StringBuilder() {
175: StringBuilder sb = new StringBuilder(32);
176: sb.append("HelloWorld");
177: assertEquals("HelloWorld", new String(sb));
178:
179: try {
180: new String((StringBuilder) null);
181: fail("No NPE");
182: } catch (NullPointerException e) {
183: }
184: }
185:
186: /**
187: * @tests java.lang.String#String(int[],int,int)
188: */
189: public void test_Constructor$III() {
190: assertEquals("HelloWorld", new String(new int[] { 'H', 'e',
191: 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' }, 0, 10));
192: assertEquals("Hello", new String(new int[] { 'H', 'e', 'l',
193: 'l', 'o', 'W', 'o', 'r', 'l', 'd' }, 0, 5));
194: assertEquals("World", new String(new int[] { 'H', 'e', 'l',
195: 'l', 'o', 'W', 'o', 'r', 'l', 'd' }, 5, 5));
196: assertEquals("", new String(new int[] { 'H', 'e', 'l', 'l',
197: 'o', 'W', 'o', 'r', 'l', 'd' }, 5, 0));
198:
199: assertEquals("\uD800\uDC00", new String(new int[] { 0x010000 },
200: 0, 1));
201: assertEquals("\uD800\uDC00a\uDBFF\uDFFF", new String(new int[] {
202: 0x010000, 'a', 0x010FFFF }, 0, 3));
203:
204: try {
205: new String((int[]) null, 0, 1);
206: fail("No NPE");
207: } catch (NullPointerException e) {
208: }
209:
210: try {
211: new String(new int[] { 'a', 'b' }, -1, 2);
212: fail("No IOOBE, negative offset");
213: } catch (IndexOutOfBoundsException e) {
214: }
215:
216: try {
217: new String(new int[] { 'a', 'b' }, 0, -1);
218: fail("No IOOBE, negative count");
219: } catch (IndexOutOfBoundsException e) {
220: }
221:
222: try {
223: new String(new int[] { 'a', 'b' }, 0, -1);
224: fail("No IOOBE, negative count");
225: } catch (IndexOutOfBoundsException e) {
226: }
227:
228: try {
229: new String(new int[] { 'a', 'b' }, 0, 3);
230: fail("No IOOBE, too large");
231: } catch (IndexOutOfBoundsException e) {
232: }
233: }
234:
235: /**
236: * @tests java.lang.String#contentEquals(CharSequence)
237: */
238: public void test_contentEqualsLjava_lang_CharSequence()
239: throws Exception {
240: String s = "abc";
241: assertTrue(s.contentEquals((CharSequence) new StringBuffer(
242: "abc")));
243: assertFalse(s.contentEquals((CharSequence) new StringBuffer(
244: "def")));
245: assertFalse(s.contentEquals((CharSequence) new StringBuffer(
246: "ghij")));
247:
248: s = newString(1, 3, "_abc_".toCharArray());
249: assertTrue(s.contentEquals((CharSequence) new StringBuffer(
250: "abc")));
251: assertFalse(s.contentEquals((CharSequence) new StringBuffer(
252: "def")));
253: assertFalse(s.contentEquals((CharSequence) new StringBuffer(
254: "ghij")));
255:
256: try {
257: s.contentEquals((CharSequence) null);
258: fail("No NPE");
259: } catch (NullPointerException e) {
260: }
261: }
262:
263: /**
264: * @tests java.lang.String#contentEquals(StringBuffer)
265: */
266: @SuppressWarnings("nls")
267: public void test_boolean_contentEquals_StringBuffer()
268: throws Exception {
269: String s = "abc";
270: assertTrue(s.contentEquals(new StringBuffer("abc")));
271: assertFalse(s.contentEquals(new StringBuffer("def")));
272: assertFalse(s.contentEquals(new StringBuffer("ghij")));
273:
274: s = newString(1, 3, "_abc_".toCharArray());
275: assertTrue(s.contentEquals(new StringBuffer("abc")));
276: assertFalse(s.contentEquals(new StringBuffer("def")));
277: assertFalse(s.contentEquals(new StringBuffer("ghij")));
278:
279: try {
280: s.contentEquals((StringBuffer) null);
281: fail("Should throw a NullPointerException");
282: } catch (NullPointerException e) {
283: // expected
284: }
285: }
286:
287: /**
288: * @tests java.lang.String#contains(CharSequence)
289: */
290: @SuppressWarnings("cast")
291: public void test_containsLjava_lang_CharSequence() throws Exception {
292: String s = "abcdefghijklmnopqrstuvwxyz";
293: assertTrue(s.contains((CharSequence) new StringBuffer("abc")));
294: assertTrue(s.contains((CharSequence) new StringBuffer("def")));
295: assertFalse(s.contains((CharSequence) new StringBuffer("ac")));
296:
297: s = newString(1, 26, "_abcdefghijklmnopqrstuvwxyz_"
298: .toCharArray());
299: assertTrue(s.contains((CharSequence) new StringBuffer("abc")));
300: assertTrue(s.contains((CharSequence) new StringBuffer("def")));
301: assertFalse(s.contains((CharSequence) new StringBuffer("ac")));
302:
303: try {
304: s.contentEquals((CharSequence) null);
305: fail("No NPE");
306: } catch (NullPointerException e) {
307: }
308: }
309:
310: /**
311: * @tests java.lang.String.offsetByCodePoints(int, int)'
312: */
313: public void test_offsetByCodePointsII() throws Exception {
314: int result = new String("a\uD800\uDC00b").offsetByCodePoints(0,
315: 2);
316: assertEquals(3, result);
317:
318: result = new String("abcd").offsetByCodePoints(3, -1);
319: assertEquals(2, result);
320:
321: result = new String("a\uD800\uDC00b").offsetByCodePoints(0, 3);
322: assertEquals(4, result);
323:
324: result = new String("a\uD800\uDC00b").offsetByCodePoints(3, -1);
325: assertEquals(1, result);
326:
327: result = new String("a\uD800\uDC00b").offsetByCodePoints(3, 0);
328: assertEquals(3, result);
329:
330: result = new String("\uD800\uDC00bc").offsetByCodePoints(3, 0);
331: assertEquals(3, result);
332:
333: result = new String("a\uDC00bc").offsetByCodePoints(3, -1);
334: assertEquals(2, result);
335:
336: result = new String("a\uD800bc").offsetByCodePoints(3, -1);
337: assertEquals(2, result);
338:
339: result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray())
340: .offsetByCodePoints(0, 2);
341: assertEquals(3, result);
342:
343: result = newString(2, 4, "__abcd__".toCharArray())
344: .offsetByCodePoints(3, -1);
345: assertEquals(2, result);
346:
347: result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray())
348: .offsetByCodePoints(0, 3);
349: assertEquals(4, result);
350:
351: result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray())
352: .offsetByCodePoints(3, -1);
353: assertEquals(1, result);
354:
355: result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray())
356: .offsetByCodePoints(3, 0);
357: assertEquals(3, result);
358:
359: result = newString(2, 4, "__\uD800\uDC00bc__".toCharArray())
360: .offsetByCodePoints(3, 0);
361: assertEquals(3, result);
362:
363: result = newString(2, 4, "__a\uDC00bc__".toCharArray())
364: .offsetByCodePoints(3, -1);
365: assertEquals(2, result);
366:
367: result = newString(2, 4, "__a\uD800bc__".toCharArray())
368: .offsetByCodePoints(3, -1);
369: assertEquals(2, result);
370:
371: String s = "abc";
372: try {
373: s.offsetByCodePoints(-1, 1);
374: fail("No IOOBE for negative index.");
375: } catch (IndexOutOfBoundsException e) {
376: }
377:
378: try {
379: s.offsetByCodePoints(0, 4);
380: fail("No IOOBE for offset that's too large.");
381: } catch (IndexOutOfBoundsException e) {
382: }
383:
384: try {
385: s.offsetByCodePoints(3, -4);
386: fail("No IOOBE for offset that's too small.");
387: } catch (IndexOutOfBoundsException e) {
388: }
389:
390: try {
391: s.offsetByCodePoints(3, 1);
392: fail("No IOOBE for index that's too large.");
393: } catch (IndexOutOfBoundsException e) {
394: }
395:
396: try {
397: s.offsetByCodePoints(4, -1);
398: fail("No IOOBE for index that's too large.");
399: } catch (IndexOutOfBoundsException e) {
400: }
401:
402: s = newString(2, 3, "__abc__".toCharArray());
403: try {
404: s.offsetByCodePoints(-1, 1);
405: fail("No IOOBE for negative index.");
406: } catch (IndexOutOfBoundsException e) {
407: }
408:
409: try {
410: s.offsetByCodePoints(0, 4);
411: fail("No IOOBE for offset that's too large.");
412: } catch (IndexOutOfBoundsException e) {
413: }
414:
415: try {
416: s.offsetByCodePoints(3, -4);
417: fail("No IOOBE for offset that's too small.");
418: } catch (IndexOutOfBoundsException e) {
419: }
420:
421: try {
422: s.offsetByCodePoints(3, 1);
423: fail("No IOOBE for index that's too large.");
424: } catch (IndexOutOfBoundsException e) {
425: }
426:
427: try {
428: s.offsetByCodePoints(4, -1);
429: fail("No IOOBE for index that's too large.");
430: } catch (IndexOutOfBoundsException e) {
431: }
432: }
433:
434: /**
435: * @tests java.lang.StringBuilder.codePointAt(int)
436: */
437: public void test_codePointAtI() throws Exception {
438: String s = "abc";
439: assertEquals('a', s.codePointAt(0));
440: assertEquals('b', s.codePointAt(1));
441: assertEquals('c', s.codePointAt(2));
442:
443: s = newString(2, 3, "__abc__".toCharArray());
444: assertEquals('a', s.codePointAt(0));
445: assertEquals('b', s.codePointAt(1));
446: assertEquals('c', s.codePointAt(2));
447:
448: s = "\uD800\uDC00";
449: assertEquals(0x10000, s.codePointAt(0));
450: assertEquals('\uDC00', s.codePointAt(1));
451:
452: s = newString(2, 2, "__\uD800\uDC00__".toCharArray());
453: assertEquals(0x10000, s.codePointAt(0));
454: assertEquals('\uDC00', s.codePointAt(1));
455:
456: s = "abc";
457: try {
458: s.codePointAt(-1);
459: fail("No IOOBE on negative index.");
460: } catch (IndexOutOfBoundsException e) {
461: }
462:
463: try {
464: s.codePointAt(s.length());
465: fail("No IOOBE on index equal to length.");
466: } catch (IndexOutOfBoundsException e) {
467: }
468:
469: try {
470: s.codePointAt(s.length() + 1);
471: fail("No IOOBE on index greater than length.");
472: } catch (IndexOutOfBoundsException e) {
473: }
474:
475: s = newString(2, 3, "__abc__".toCharArray());
476: try {
477: s.codePointAt(-1);
478: fail("No IOOBE on negative index.");
479: } catch (IndexOutOfBoundsException e) {
480: }
481:
482: try {
483: s.codePointAt(s.length());
484: fail("No IOOBE on index equal to length.");
485: } catch (IndexOutOfBoundsException e) {
486: }
487:
488: try {
489: s.codePointAt(s.length() + 1);
490: fail("No IOOBE on index greater than length.");
491: } catch (IndexOutOfBoundsException e) {
492: }
493: }
494:
495: /**
496: * @tests java.lang.StringBuilder.codePointBefore(int)
497: */
498: public void test_codePointBeforeI() throws Exception {
499: String s = "abc";
500: assertEquals('a', s.codePointBefore(1));
501: assertEquals('b', s.codePointBefore(2));
502: assertEquals('c', s.codePointBefore(3));
503:
504: s = newString(2, 3, "__abc__".toCharArray());
505: assertEquals('a', s.codePointBefore(1));
506: assertEquals('b', s.codePointBefore(2));
507: assertEquals('c', s.codePointBefore(3));
508:
509: s = "\uD800\uDC00";
510: assertEquals(0x10000, s.codePointBefore(2));
511: assertEquals('\uD800', s.codePointBefore(1));
512:
513: s = newString(2, 2, "__\uD800\uDC00__".toCharArray());
514: assertEquals(0x10000, s.codePointBefore(2));
515: assertEquals('\uD800', s.codePointBefore(1));
516:
517: s = "abc";
518: try {
519: s.codePointBefore(0);
520: fail("No IOOBE on zero index.");
521: } catch (IndexOutOfBoundsException e) {
522: }
523:
524: try {
525: s.codePointBefore(-1);
526: fail("No IOOBE on negative index.");
527: } catch (IndexOutOfBoundsException e) {
528: }
529:
530: try {
531: s.codePointBefore(s.length() + 1);
532: fail("No IOOBE on index greater than length.");
533: } catch (IndexOutOfBoundsException e) {
534: }
535:
536: s = newString(2, 3, "__abc__".toCharArray());
537: try {
538: s.codePointBefore(0);
539: fail("No IOOBE on zero index.");
540: } catch (IndexOutOfBoundsException e) {
541: }
542:
543: try {
544: s.codePointBefore(-1);
545: fail("No IOOBE on negative index.");
546: } catch (IndexOutOfBoundsException e) {
547: }
548:
549: try {
550: s.codePointBefore(s.length() + 1);
551: fail("No IOOBE on index greater than length.");
552: } catch (IndexOutOfBoundsException e) {
553: }
554: }
555:
556: /**
557: * @tests java.lang.StringBuilder.codePointCount(int, int)
558: */
559: public void test_codePointCountII() throws Exception {
560: assertEquals(1, "\uD800\uDC00".codePointCount(0, 2));
561: assertEquals(1, "\uD800\uDC01".codePointCount(0, 2));
562: assertEquals(1, "\uD801\uDC01".codePointCount(0, 2));
563: assertEquals(1, "\uDBFF\uDFFF".codePointCount(0, 2));
564:
565: assertEquals(3, "a\uD800\uDC00b".codePointCount(0, 4));
566: assertEquals(4, "a\uD800\uDC00b\uD800".codePointCount(0, 5));
567:
568: assertEquals(1, newString(2, 2,
569: "__\uD800\uDC00__".toCharArray()).codePointCount(0, 2));
570: assertEquals(1, newString(2, 2,
571: "__\uD800\uDC01__".toCharArray()).codePointCount(0, 2));
572: assertEquals(1, newString(2, 2,
573: "__\uD801\uDC01__".toCharArray()).codePointCount(0, 2));
574: assertEquals(1, newString(2, 2,
575: "__\uDBFF\uDFFF__".toCharArray()).codePointCount(0, 2));
576:
577: assertEquals(3, newString(2, 4,
578: "__a\uD800\uDC00b__".toCharArray())
579: .codePointCount(0, 4));
580: assertEquals(4, newString(2, 5,
581: "__a\uD800\uDC00b\uD800__".toCharArray())
582: .codePointCount(0, 5));
583:
584: String s = "abc";
585: try {
586: s.codePointCount(-1, 2);
587: fail("No IOOBE for negative begin index.");
588: } catch (IndexOutOfBoundsException e) {
589: }
590:
591: try {
592: s.codePointCount(0, 4);
593: fail("No IOOBE for end index that's too large.");
594: } catch (IndexOutOfBoundsException e) {
595: }
596:
597: try {
598: s.codePointCount(3, 2);
599: fail("No IOOBE for begin index larger than end index.");
600: } catch (IndexOutOfBoundsException e) {
601: }
602:
603: s = newString(2, 3, "__abc__".toCharArray());
604: try {
605: s.codePointCount(-1, 2);
606: fail("No IOOBE for negative begin index.");
607: } catch (IndexOutOfBoundsException e) {
608: }
609:
610: try {
611: s.codePointCount(0, 4);
612: fail("No IOOBE for end index that's too large.");
613: } catch (IndexOutOfBoundsException e) {
614: }
615:
616: try {
617: s.codePointCount(3, 2);
618: fail("No IOOBE for begin index larger than end index.");
619: } catch (IndexOutOfBoundsException e) {
620: }
621: }
622: }
|