String2Test.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 org.apache.harmony.luni.tests.java.lang;
19
20import java.io.UnsupportedEncodingException;
21import java.util.Locale;
22import java.nio.charset.Charset;
23
24public class String2Test extends junit.framework.TestCase {
25
26    String hw1 = "HelloWorld";
27
28    String hw2 = "HelloWorld";
29
30    String hwlc = "helloworld";
31
32    String hwuc = "HELLOWORLD";
33
34    String hello1 = "Hello";
35
36    String world1 = "World";
37
38    String comp11 = "Test String";
39
40    Object obj = new Object();
41
42    char[] buf = { 'W', 'o', 'r', 'l', 'd' };
43
44    char[] rbuf = new char[5];
45
46    /**
47     * @tests java.lang.String#String()
48     */
49    public void test_Constructor() {
50        // Test for method java.lang.String()
51        assertTrue("Created incorrect string", new String().equals(""));
52    }
53
54    /**
55     * @tests java.lang.String#String(byte[])
56     */
57    public void test_Constructor$B() {
58        // Test for method java.lang.String(byte [])
59        assertTrue("Failed to create string", new String(hw1.getBytes())
60                .equals(hw1));
61    }
62
63    /**
64     * @tests java.lang.String#String(byte[], int)
65     */
66    @SuppressWarnings("deprecation")
67    public void test_Constructor$BI() {
68        // Test for method java.lang.String(byte [], int)
69        String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0);
70        assertTrue("Incorrect string returned: " + s, s.equals("ABCDE"));
71        s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1);
72        assertTrue("Did not use nonzero hibyte", !s.equals("ABCDE"));
73    }
74
75    /**
76     * @tests java.lang.String#String(byte[], int, int)
77     */
78    public void test_Constructor$BII() {
79        // Test for method java.lang.String(byte [], int, int)
80        assertTrue("Failed to create string", new String(hw1.getBytes(), 0, hw1
81                .getBytes().length).equals(hw1));
82
83        boolean exception = false;
84        try {
85            new String(new byte[0], 0, Integer.MAX_VALUE);
86        } catch (IndexOutOfBoundsException e) {
87            exception = true;
88        }
89        assertTrue("Did not throw exception", exception);
90    }
91
92    /**
93     * @tests java.lang.String#String(byte[], int, int, int)
94     */
95    @SuppressWarnings("deprecation")
96    public void test_Constructor$BIII() {
97        // Test for method java.lang.String(byte [], int, int, int)
98        String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 1, 3);
99        assertTrue("Incorrect string returned: " + s, s.equals("BCD"));
100        s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1, 0, 5);
101        assertTrue("Did not use nonzero hibyte", !s.equals("ABCDE"));
102    }
103
104    /**
105     * @tests java.lang.String#String(byte[], int, int, java.lang.String)
106     */
107    public void test_Constructor$BIILjava_lang_String() throws Exception {
108        // Test for method java.lang.String(byte [], int, int, java.lang.String)
109        String s = null;
110        s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5, "8859_1");
111        assertTrue("Incorrect string returned: " + s, s.equals("ABCDE"));
112        // Regression for HARMONY-1111
113        assertNotNull(new String(new byte[] { (byte) 0xC0 }, 0, 1, "UTF-8"));
114    }
115
116    /**
117     * @tests java.lang.String#String(byte[], java.lang.String)
118     */
119    public void test_Constructor$BLjava_lang_String() throws Exception {
120        // Test for method java.lang.String(byte [], java.lang.String)
121        String s = null;
122        s = new String(new byte[] { 65, 66, 67, 68, 69 }, "8859_1");
123        assertTrue("Incorrect string returned: " + s, s.equals("ABCDE"));
124    }
125
126    /**
127     * @tests java.lang.String#String(char[])
128     */
129    public void test_Constructor$C() {
130        // Test for method java.lang.String(char [])
131        assertEquals("Failed Constructor test", "World", new String(buf));
132    }
133
134    /**
135     * @tests java.lang.String#String(char[], int, int)
136     */
137    public void test_Constructor$CII() {
138        // Test for method java.lang.String(char [], int, int)
139        char[] buf = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
140        String s = new String(buf, 0, buf.length);
141        assertTrue("Incorrect string created", hw1.equals(s));
142
143        boolean exception = false;
144        try {
145            new String(new char[0], 0, Integer.MAX_VALUE);
146        } catch (IndexOutOfBoundsException e) {
147            exception = true;
148        }
149        assertTrue("Did not throw exception", exception);
150    }
151
152    /**
153     * @tests java.lang.String#String(int[], int, int)
154     */
155    public void test_Constructor$III() {
156        // Test for method java.lang.String(int [], int, int)
157        try {
158            new String(new int[0], 2, Integer.MAX_VALUE);
159            fail("Did not throw exception");
160        } catch (IndexOutOfBoundsException e) {
161            // expected
162        }
163    }
164
165    /**
166     * @tests java.lang.String#String(java.lang.String)
167     */
168    public void test_ConstructorLjava_lang_String() {
169        // Test for method java.lang.String(java.lang.String)
170        String s = new String("Hello World");
171        assertEquals("Failed to construct correct string", "Hello World", s);
172    }
173
174    /**
175     * @tests java.lang.String#String(java.lang.StringBuffer)
176     */
177    public void test_ConstructorLjava_lang_StringBuffer() {
178        // Test for method java.lang.String(java.lang.StringBuffer)
179        StringBuffer sb = new StringBuffer();
180        sb.append("HelloWorld");
181        assertEquals("Created incorrect string", "HelloWorld", new String(sb));
182    }
183
184    /**
185     * @tests java.lang.String#charAt(int)
186     */
187    public void test_charAtI() {
188        // Test for method char java.lang.String.charAt(int)
189        assertTrue("Incorrect character returned", hw1.charAt(5) == 'W'
190                && (hw1.charAt(1) != 'Z'));
191    }
192
193    /**
194     * @tests java.lang.String#compareTo(java.lang.String)
195     */
196    public void test_compareToLjava_lang_String() {
197        // Test for method int java.lang.String.compareTo(java.lang.String)
198        assertTrue("Returned incorrect value for first < second", "aaaaab"
199                .compareTo("aaaaac") < 0);
200        assertEquals("Returned incorrect value for first = second", 0, "aaaaac"
201                .compareTo("aaaaac"));
202        assertTrue("Returned incorrect value for first > second", "aaaaac"
203                .compareTo("aaaaab") > 0);
204        assertTrue("Considered case to not be of importance", !("A"
205                .compareTo("a") == 0));
206
207        try {
208            "fixture".compareTo(null);
209            fail("No NPE");
210        } catch (NullPointerException e) {
211        }
212    }
213
214    /**
215     * @tests java.lang.String#compareToIgnoreCase(java.lang.String)
216     */
217    public void test_compareToIgnoreCaseLjava_lang_String() {
218        // Test for method int
219        // java.lang.String.compareToIgnoreCase(java.lang.String)
220        assertTrue("Returned incorrect value for first < second", "aaaaab"
221                .compareToIgnoreCase("aaaaac") < 0);
222        assertEquals("Returned incorrect value for first = second", 0, "aaaaac"
223                .compareToIgnoreCase("aaaaac"));
224        assertTrue("Returned incorrect value for first > second", "aaaaac"
225                .compareToIgnoreCase("aaaaab") > 0);
226        assertEquals("Considered case to not be of importance", 0, "A"
227                .compareToIgnoreCase("a"));
228
229        assertTrue("0xbf should not compare = to 'ss'", "\u00df"
230                .compareToIgnoreCase("ss") != 0);
231        assertEquals("0x130 should compare = to 'i'", 0, "\u0130"
232                .compareToIgnoreCase("i"));
233        assertEquals("0x131 should compare = to 'i'", 0, "\u0131"
234                .compareToIgnoreCase("i"));
235
236        Locale defLocale = Locale.getDefault();
237        try {
238            Locale.setDefault(new Locale("tr", ""));
239            assertEquals("Locale tr: 0x130 should compare = to 'i'", 0,
240                    "\u0130".compareToIgnoreCase("i"));
241            assertEquals("Locale tr: 0x131 should compare = to 'i'", 0,
242                    "\u0131".compareToIgnoreCase("i"));
243        } finally {
244            Locale.setDefault(defLocale);
245        }
246
247        try {
248            "fixture".compareToIgnoreCase(null);
249            fail("No NPE");
250        } catch (NullPointerException e) {
251        }
252    }
253
254    /**
255     * @tests java.lang.String#concat(java.lang.String)
256     */
257    public void test_concatLjava_lang_String() {
258        // Test for method java.lang.String
259        // java.lang.String.concat(java.lang.String)
260        assertTrue("Concatenation failed to produce correct string", hello1
261                .concat(world1).equals(hw1));
262        boolean exception = false;
263        try {
264            String a = new String("test");
265            String b = null;
266            a.concat(b);
267        } catch (NullPointerException e) {
268            exception = true;
269        }
270        assertTrue("Concatenation failed to throw NP exception (1)", exception);
271        exception = false;
272        try {
273            String a = new String("");
274            String b = null;
275            a.concat(b);
276        } catch (NullPointerException e) {
277            exception = true;
278        }
279        assertTrue("Concatenation failed to throw NP exception (2)", exception);
280
281        String s1 = "";
282        String s2 = "s2";
283        String s3 = s1.concat(s2);
284        assertEquals(s2, s3);
285        assertNotSame(s2, s3);
286
287        s3 = s2.concat(s1);
288        assertSame(s2, s3);
289    }
290
291    /**
292     * @tests java.lang.String#copyValueOf(char[])
293     */
294    public void test_copyValueOf$C() {
295        // Test for method java.lang.String java.lang.String.copyValueOf(char
296        // [])
297        char[] t = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
298        assertEquals("copyValueOf returned incorrect String", "HelloWorld",
299                String.copyValueOf(t));
300    }
301
302    /**
303     * @tests java.lang.String#copyValueOf(char[], int, int)
304     */
305    public void test_copyValueOf$CII() {
306        // Test for method java.lang.String java.lang.String.copyValueOf(char
307        // [], int, int)
308        char[] t = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
309        assertEquals("copyValueOf returned incorrect String", "World", String
310                .copyValueOf(t, 5, 5));
311    }
312
313    /**
314     * @tests java.lang.String#endsWith(java.lang.String)
315     */
316    public void test_endsWithLjava_lang_String() {
317        // Test for method boolean java.lang.String.endsWith(java.lang.String)
318        assertTrue("Failed to fine ending string", hw1.endsWith("ld"));
319    }
320
321    /**
322     * @tests java.lang.String#equals(java.lang.Object)
323     */
324    public void test_equalsLjava_lang_Object() {
325        assertEquals("String not equal", hw1, hw2);
326        assertEquals("Empty string equals check", "", "");
327        assertEquals("Null string equals check", (String)null, (String)null);
328
329        assertFalse("Unequal strings reports as equal", hw1.equals(comp11));
330        assertFalse("Null string comparison failed", hw1.equals((String)null));
331    }
332
333    /**
334     * @tests java.lang.String#equalsIgnoreCase(java.lang.String)
335     */
336    public void test_equalsIgnoreCaseLjava_lang_String() {
337        // Test for method boolean
338        // java.lang.String.equalsIgnoreCase(java.lang.String)
339        assertTrue("lc version returned unequal to uc", hwlc
340                .equalsIgnoreCase(hwuc));
341    }
342
343    /**
344     * @tests java.lang.String#getBytes()
345     */
346    public void test_getBytes() {
347        // Test for method byte [] java.lang.String.getBytes()
348        byte[] sbytes = hw1.getBytes();
349
350        boolean isEbcdic = Charset.defaultCharset().equals(Charset.forName("IBM1047"));
351        if (!isEbcdic) {
352            for (int i = 0; i < hw1.length(); i++)
353                assertTrue("Returned incorrect bytes", sbytes[i] == (byte) hw1
354                        .charAt(i));
355        } else {
356            // On EBCDIC platforms, getBytes() returns different values
357            // Reference values taken from J9 5.0
358            byte[] expectedValues = {-56, -123, -109, -109, -106, -26, -106,
359                -103, -109, -124};
360            for (int i = 0; i < hw1.length(); i++)
361                assertEquals(expectedValues[i], sbytes[i]);
362        }
363
364        char[] chars = new char[1];
365        for (int i = 0; i < 65536; i++) {
366            // skip surrogates
367            if (i == 0xd800)
368                i = 0xe000;
369            byte[] result = null;
370            chars[0] = (char) i;
371            String string = new String(chars);
372            try {
373                result = string.getBytes("8859_1");
374                if (i < 256) {
375                    assertEquals((byte) i, result[0]);
376                } else {
377                    /*
378                     * Substitute character should be 0x1A [1], but may be '?'
379                     * character. [1]
380                     * http://en.wikipedia.org/wiki/Substitute_character
381                     */
382                    assertTrue(result[0] == '?' || result[0] == 0x1a);
383                }
384            } catch (java.io.UnsupportedEncodingException e) {
385            }
386            try {
387                result = string.getBytes("UTF8");
388                int length = i < 0x80 ? 1 : (i < 0x800 ? 2 : 3);
389                assertTrue("Wrong length UTF8: " + Integer.toHexString(i),
390                        result.length == length);
391                assertTrue(
392                        "Wrong bytes UTF8: " + Integer.toHexString(i),
393                        (i < 0x80 && result[0] == i)
394                                || (i >= 0x80
395                                        && i < 0x800
396                                        && result[0] == (byte) (0xc0 | ((i & 0x7c0) >> 6)) && result[1] == (byte) (0x80 | (i & 0x3f)))
397                                || (i >= 0x800
398                                        && result[0] == (byte) (0xe0 | (i >> 12))
399                                        && result[1] == (byte) (0x80 | ((i & 0xfc0) >> 6)) && result[2] == (byte) (0x80 | (i & 0x3f))));
400            } catch (java.io.UnsupportedEncodingException e) {
401            }
402
403            String bytes = null;
404            try {
405                bytes = new String(result, "UTF8");
406                assertTrue("Wrong UTF8 byte length: " + bytes.length() + "("
407                        + i + ")", bytes.length() == 1);
408                assertTrue(
409                        "Wrong char UTF8: "
410                                + Integer.toHexString(bytes.charAt(0)) + " ("
411                                + i + ")", bytes.charAt(0) == i);
412            } catch (java.io.UnsupportedEncodingException e) {
413            }
414        }
415
416        byte[] bytes = new byte[1];
417        for (int i = 0; i < 256; i++) {
418            bytes[0] = (byte) i;
419            String result = null;
420            try {
421                result = new String(bytes, "8859_1");
422                assertEquals("Wrong char length", 1, result.length());
423                assertTrue("Wrong char value", result.charAt(0) == (char) i);
424            } catch (java.io.UnsupportedEncodingException e) {
425            }
426        }
427    }
428
429    /**
430     * @tests java.lang.String#getBytes(int, int, byte[], int)
431     */
432    @SuppressWarnings("deprecation")
433    public void test_getBytesII$BI() {
434        // Test for method void java.lang.String.getBytes(int, int, byte [],
435        // int)
436        byte[] buf = new byte[5];
437        "Hello World".getBytes(6, 11, buf, 0);
438
439        boolean isEbcdic = Charset.defaultCharset().equals(Charset.forName("IBM1047"));
440        if (!isEbcdic) {
441            assertEquals("Returned incorrect bytes", "World", new String(buf));
442        } else {
443            // On EBCDIC platforms, getBytes() returns different values
444            // Reference values taken from J9 5.0
445            byte[] expectedValues = {87, 111, 114, 108, 100};
446            for (int i = 0; i < 5; i++)
447                assertEquals(expectedValues[i], buf[i]);
448        }
449
450        try {
451            "Hello World".getBytes(-1, 1, null, 0);
452            fail("Expected StringIndexOutOfBoundsException");
453        } catch (StringIndexOutOfBoundsException e) {
454        } catch (NullPointerException e) {
455            fail("Threw wrong exception");
456        }
457    }
458
459    /**
460     * @tests java.lang.String#getBytes(java.lang.String)
461     */
462    public void test_getBytesLjava_lang_String() throws Exception {
463        // Test for method byte [] java.lang.String.getBytes(java.lang.String)
464        byte[] buf = "Hello World".getBytes();
465        assertEquals("Returned incorrect bytes", "Hello World", new String(buf));
466
467        try {
468            "string".getBytes("8849_1");
469            fail("No UnsupportedEncodingException");
470        } catch (UnsupportedEncodingException e) {
471        }
472
473        byte[] bytes = "\u3048".getBytes("UTF-8");
474        byte[] expected = new byte[] { (byte) 0xE3, (byte) 0x81, (byte) 0x88 };
475        assertEquals(expected[0], bytes[0]);
476        assertEquals(expected[1], bytes[1]);
477        assertEquals(expected[2], bytes[2]);
478
479        // Regression for HARMONY-663
480        try {
481            "string".getBytes("?Q?D??_??_6ffa?+vG?_??\u951f\ufffd??");
482            fail("No UnsupportedEncodingException");
483        } catch (UnsupportedEncodingException e) {
484            // expected
485        }
486
487        // Regression for HARMONY-4135
488        bytes = "-".getBytes("UTF-16");
489        expected = new byte[] { -2, -1 };
490        assertEquals(expected[0], bytes[0]);
491        assertEquals(expected[1], bytes[1]);
492    }
493
494    /*
495     * @tests java.lang.String#getBytes()
496     */
497    public void test_getBytes_NPE() throws Exception {
498        try {
499            "abc".getBytes((String) null);
500            fail("Should throw NullPointerException");
501        } catch (NullPointerException e) {
502            // Expected
503        }
504
505        try {
506            "Hello World".getBytes(1, 2, null, 1);
507            fail("Should throw NullPointerException");
508        } catch (NullPointerException e) {
509            // Expected
510        }
511    }
512
513    /**
514     * @tests java.lang.String#getChars(int, int, char[], int)
515     */
516    public void test_getCharsII$CI() {
517        // Test for method void java.lang.String.getChars(int, int, char [],
518        // int)
519        hw1.getChars(5, hw1.length(), rbuf, 0);
520
521        for (int i = 0; i < rbuf.length; i++)
522            assertTrue("getChars returned incorrect char(s)", rbuf[i] == buf[i]);
523    }
524
525    /**
526     * @tests java.lang.String#hashCode()
527     */
528    public void test_hashCode() {
529        // Test for method int java.lang.String.hashCode()
530        int hwHashCode = 0;
531        final int hwLength = hw1.length();
532        int powerOfThirtyOne = 1;
533        for (int counter = hwLength - 1; counter >= 0; counter--) {
534            hwHashCode += hw1.charAt(counter) * powerOfThirtyOne;
535            powerOfThirtyOne *= 31;
536        }
537        assertEquals("String did not hash to correct value", hwHashCode, hw1.hashCode());
538        assertEquals("The empty string \"\" did not hash to zero", 0, "".hashCode());
539        assertEquals("Calculated wrong string hashcode", -1933545242, "Harmony".hashCode());
540    }
541
542    /**
543     * @tests java.lang.String#indexOf(int)
544     */
545    public void test_indexOfI() {
546        // Test for method int java.lang.String.indexOf(int)
547        assertEquals("Invalid index returned", 1, hw1.indexOf('e'));
548        assertEquals("Invalid index returned", 1, "a\ud800\udc00".indexOf(0x10000));
549    }
550
551    /**
552     * @tests java.lang.String#indexOf(int, int)
553     */
554    public void test_indexOfII() {
555        // Test for method int java.lang.String.indexOf(int, int)
556        assertEquals("Invalid character index returned", 5, hw1.indexOf('W', 2));
557        assertEquals("Invalid index returned", 2, "ab\ud800\udc00".indexOf(0x10000, 1));
558    }
559
560    /**
561     * @tests java.lang.String#indexOf(java.lang.String)
562     */
563    public void test_indexOfLjava_lang_String() {
564        // Test for method int java.lang.String.indexOf(java.lang.String)
565        assertTrue("Failed to find string", hw1.indexOf("World") > 0);
566        assertTrue("Failed to find string", !(hw1.indexOf("ZZ") > 0));
567    }
568
569    /**
570     * @tests java.lang.String#indexOf(java.lang.String, int)
571     */
572    public void test_indexOfLjava_lang_StringI() {
573        // Test for method int java.lang.String.indexOf(java.lang.String, int)
574        assertTrue("Failed to find string", hw1.indexOf("World", 0) > 0);
575        assertTrue("Found string outside index", !(hw1.indexOf("Hello", 6) > 0));
576        assertEquals("Did not accept valid negative starting position", 0,
577                hello1.indexOf("", -5));
578        assertEquals("Reported wrong error code", 5, hello1.indexOf("", 5));
579        assertEquals("Wrong for empty in empty", 0, "".indexOf("", 0));
580    }
581
582    /**
583     * @tests java.lang.String#intern()
584     */
585    public void test_intern() {
586        // Test for method java.lang.String java.lang.String.intern()
587        assertTrue("Intern returned incorrect result", hw1.intern() == hw2
588                .intern());
589    }
590
591    /**
592     * @tests java.lang.String#lastIndexOf(int)
593     */
594    public void test_lastIndexOfI() {
595        // Test for method int java.lang.String.lastIndexOf(int)
596        assertEquals("Failed to return correct index", 5, hw1.lastIndexOf('W'));
597        assertEquals("Returned index for non-existent char", -1, hw1
598                .lastIndexOf('Z'));
599        assertEquals("Failed to return correct index", 1, "a\ud800\udc00"
600                .lastIndexOf(0x10000));
601    }
602
603    /**
604     * @tests java.lang.String#lastIndexOf(int, int)
605     */
606    public void test_lastIndexOfII() {
607        // Test for method int java.lang.String.lastIndexOf(int, int)
608        assertEquals("Failed to return correct index", 5, hw1.lastIndexOf('W',
609                6));
610        assertEquals("Returned index for char out of specified range", -1, hw1
611                .lastIndexOf('W', 4));
612        assertEquals("Returned index for non-existent char", -1, hw1
613                .lastIndexOf('Z', 9));
614
615    }
616
617    /**
618     * @tests java.lang.String#lastIndexOf(java.lang.String)
619     */
620    public void test_lastIndexOfLjava_lang_String() {
621        // Test for method int java.lang.String.lastIndexOf(java.lang.String)
622        assertEquals("Returned incorrect index", 5, hw1.lastIndexOf("World"));
623        assertEquals("Found String outside of index", -1, hw1
624                .lastIndexOf("HeKKKKKKKK"));
625    }
626
627    /**
628     * @tests java.lang.String#lastIndexOf(java.lang.String, int)
629     */
630    public void test_lastIndexOfLjava_lang_StringI() {
631        // Test for method int java.lang.String.lastIndexOf(java.lang.String,
632        // int)
633        assertEquals("Returned incorrect index", 5, hw1.lastIndexOf("World", 9));
634        int result = hw1.lastIndexOf("Hello", 2);
635        assertTrue("Found String outside of index: " + result, result == 0);
636        assertEquals("Reported wrong error code", -1, hello1
637                .lastIndexOf("", -5));
638        assertEquals("Did not accept valid large starting position", 5, hello1
639                .lastIndexOf("", 5));
640    }
641
642    /**
643     * @tests java.lang.String#length()
644     */
645    public void test_length() {
646        // Test for method int java.lang.String.length()
647        assertEquals("Invalid length returned", 11, comp11.length());
648    }
649
650    /**
651     * @tests java.lang.String#regionMatches(int, java.lang.String, int, int)
652     */
653    public void test_regionMatchesILjava_lang_StringII() {
654        // Test for method boolean java.lang.String.regionMatches(int,
655        // java.lang.String, int, int)
656        String bogusString = "xxcedkedkleiorem lvvwr e''' 3r3r 23r";
657
658        assertTrue("identical regions failed comparison", hw1.regionMatches(2,
659                hw2, 2, 5));
660        assertTrue("Different regions returned true", !hw1.regionMatches(2,
661                bogusString, 2, 5));
662    }
663
664    /**
665     * @tests java.lang.String#regionMatches(boolean, int, java.lang.String,
666     *        int, int)
667     */
668    public void test_regionMatchesZILjava_lang_StringII() {
669        // Test for method boolean java.lang.String.regionMatches(boolean, int,
670        // java.lang.String, int, int)
671
672        String bogusString = "xxcedkedkleiorem lvvwr e''' 3r3r 23r";
673
674        assertTrue("identical regions failed comparison", hw1.regionMatches(
675                false, 2, hw2, 2, 5));
676        assertTrue("identical regions failed comparison with different cases",
677                hw1.regionMatches(true, 2, hw2, 2, 5));
678        assertTrue("Different regions returned true", !hw1.regionMatches(true,
679                2, bogusString, 2, 5));
680        assertTrue("identical regions failed comparison with different cases",
681                hw1.regionMatches(false, 2, hw2, 2, 5));
682    }
683
684    /**
685     * @tests java.lang.String#replace(char, char)
686     */
687    public void test_replaceCC() {
688        // Test for method java.lang.String java.lang.String.replace(char, char)
689        assertEquals("Failed replace", "HezzoWorzd", hw1.replace('l', 'z'));
690    }
691
692    /**
693     * @tests java.lang.String#replace(CharSequence, CharSequence)
694     */
695    public void test_replaceLjava_langCharSequenceLjava_langCharSequence() {
696        assertEquals("Failed replace", "aaccdd", "aabbdd".replace(
697                new StringBuffer("bb"), "cc"));
698        assertEquals("Failed replace by bigger seq", "cccbccc", "aba".replace(
699                "a", "ccc"));
700        assertEquals("Failed replace by smaller seq", "$bba^", "$aaaaa^"
701                .replace(new StringBuilder("aa"), "b"));
702        assertEquals("Failed to replace empty string", "%%a%%b%%c%%",
703                "abc".replace("", "%%"));
704        assertEquals("Failed to replace with empty string", "aacc",
705                "aabbcc".replace("b", ""));
706        assertEquals("Failed to replace in empty string", "abc",
707                "".replace("", "abc"));
708    }
709
710    /**
711     * @tests java.lang.String#startsWith(java.lang.String)
712     */
713    public void test_startsWithLjava_lang_String() {
714        // Test for method boolean java.lang.String.startsWith(java.lang.String)
715        assertTrue("Failed to find string", hw1.startsWith("Hello"));
716        assertTrue("Found incorrect string", !hw1.startsWith("T"));
717    }
718
719    /**
720     * @tests java.lang.String#startsWith(java.lang.String, int)
721     */
722    public void test_startsWithLjava_lang_StringI() {
723        // Test for method boolean java.lang.String.startsWith(java.lang.String,
724        // int)
725        assertTrue("Failed to find string", hw1.startsWith("World", 5));
726        assertTrue("Found incorrect string", !hw1.startsWith("Hello", 5));
727    }
728
729    /**
730     * @tests java.lang.String#substring(int)
731     */
732    public void test_substringI() {
733        // Test for method java.lang.String java.lang.String.substring(int)
734        assertEquals("Incorrect substring returned", "World", hw1.substring(5));
735        assertTrue("not identical", hw1.substring(0) == hw1);
736    }
737
738    /**
739     * @tests java.lang.String#substring(int, int)
740     */
741    public void test_substringII() {
742        // Test for method java.lang.String java.lang.String.substring(int, int)
743        assertTrue("Incorrect substring returned", hw1.substring(0, 5).equals(
744                "Hello")
745                && (hw1.substring(5, 10).equals("World")));
746        assertTrue("not identical", hw1.substring(0, hw1.length()) == hw1);
747    }
748
749	/**
750	 * @tests java.lang.String#substring(int, int)
751	 */
752	public void test_substringErrorMessage() {
753		try {
754			hw1.substring(-1, 1);
755		} catch (StringIndexOutOfBoundsException ex) {
756			String msg = ex.getMessage();
757			assertTrue("Expected message to contain -1: " + msg, msg
758			        .indexOf("-1") != -1);
759		}
760		try {
761			hw1.substring(4, 1);
762		} catch (StringIndexOutOfBoundsException ex) {
763			String msg = ex.getMessage();
764			assertTrue("Expected message to contain -3: " + msg, msg
765			        .indexOf("-3") != -1);
766		}
767		try {
768			hw1.substring(0, 100);
769		} catch (StringIndexOutOfBoundsException ex) {
770			String msg = ex.getMessage();
771			assertTrue("Expected message to contain 100: " + msg, msg
772			        .indexOf("100") != -1);
773		}
774	}
775
776    /**
777     * @tests java.lang.String#toCharArray()
778     */
779    public void test_toCharArray() {
780        // Test for method char [] java.lang.String.toCharArray()
781
782        String s = new String(buf, 0, buf.length);
783        char[] schars = s.toCharArray();
784        for (int i = 0; i < s.length(); i++)
785            assertTrue("Returned incorrect char aray", buf[i] == schars[i]);
786    }
787
788    /**
789     * @tests java.lang.String#toLowerCase()
790     */
791    public void test_toLowerCase() {
792        // Test for method java.lang.String java.lang.String.toLowerCase()
793        assertTrue("toLowerCase case conversion did not succeed", hwuc
794                .toLowerCase().equals(hwlc));
795
796        assertEquals(
797                "a) Sigma has ordinary lower case value when isolated with Unicode 4.0",
798                "\u03c3", "\u03a3".toLowerCase());
799        assertEquals(
800                "b) Sigma has final form lower case value at end of word with Unicode 4.0",
801                "a\u03c2", "a\u03a3".toLowerCase());
802
803        assertEquals("toLowerCase case conversion did not succeed",
804        		"\uD801\uDC44", "\uD801\uDC1C".toLowerCase());
805    }
806
807    /**
808     * @tests java.lang.String#toLowerCase(java.util.Locale)
809     */
810    public void test_toLowerCaseLjava_util_Locale() {
811        // Test for method java.lang.String
812        // java.lang.String.toLowerCase(java.util.Locale)
813        assertTrue("toLowerCase case conversion did not succeed", hwuc
814                .toLowerCase(java.util.Locale.getDefault()).equals(hwlc));
815        assertEquals("Invalid \\u0049 for English", "\u0069", "\u0049"
816                .toLowerCase(Locale.ENGLISH));
817        assertEquals("Invalid \\u0049 for Turkish", "\u0131", "\u0049"
818                .toLowerCase(new Locale("tr", "")));
819    }
820
821    /**
822     * @tests java.lang.String#toString()
823     */
824    public void test_toString() {
825        // Test for method java.lang.String java.lang.String.toString()
826        assertTrue("Incorrect string returned", hw1.toString().equals(hw1));
827    }
828
829    /**
830     * @tests java.lang.String#toUpperCase()
831     */
832    public void test_toUpperCase() {
833        // Test for method java.lang.String java.lang.String.toUpperCase()
834        assertTrue("Returned string is not UpperCase", hwlc.toUpperCase()
835                .equals(hwuc));
836
837        assertEquals("Wrong conversion", "SS", "\u00df".toUpperCase());
838
839        String s = "a\u00df\u1f56";
840        assertTrue("Invalid conversion", !s.toUpperCase().equals(s));
841
842        assertEquals("toUpperCase case conversion did not succeed",
843        		"\uD801\uDC1C", "\uD801\uDC44".toUpperCase());
844    }
845
846    /**
847     * @tests java.lang.String#toUpperCase(java.util.Locale)
848     */
849    public void test_toUpperCaseLjava_util_Locale() {
850        // Test for method java.lang.String
851        // java.lang.String.toUpperCase(java.util.Locale)
852        assertTrue("Returned string is not UpperCase", hwlc.toUpperCase()
853                .equals(hwuc));
854        assertEquals("Invalid \\u0069 for English", "\u0049", "\u0069"
855                .toUpperCase(Locale.ENGLISH));
856        assertEquals("Invalid \\u0069 for Turkish", "\u0130", "\u0069"
857                .toUpperCase(new Locale("tr", "")));
858    }
859
860    /**
861     * @tests java.lang.String#toUpperCase(java.util.Locale)
862     */
863    public void test_toUpperCaseLjava_util_Locale_subtest0() {
864        // Test for method java.lang.String
865        // java.lang.String.toUpperCase(java.util.Locale)
866    }
867
868    /**
869     * @tests java.lang.String#trim()
870     */
871    public void test_trim() {
872        // Test for method java.lang.String java.lang.String.trim()
873        assertTrue("Incorrect string returned", " HelloWorld ".trim().equals(
874                hw1));
875    }
876
877    /**
878     * @tests java.lang.String#valueOf(char[])
879     */
880    public void test_valueOf$C() {
881        // Test for method java.lang.String java.lang.String.valueOf(char [])
882        assertEquals("Returned incorrect String", "World", String.valueOf(buf));
883    }
884
885    /**
886     * @tests java.lang.String#valueOf(char[], int, int)
887     */
888    public void test_valueOf$CII() {
889        // Test for method java.lang.String java.lang.String.valueOf(char [],
890        // int, int)
891        char[] t = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
892        assertEquals("copyValueOf returned incorrect String", "World", String
893                .valueOf(t, 5, 5));
894    }
895
896    /**
897     * @tests java.lang.String#valueOf(char)
898     */
899    public void test_valueOfC() {
900        // Test for method java.lang.String java.lang.String.valueOf(char)
901        for (int i = 0; i < 65536; i++)
902            assertTrue("Incorrect valueOf(char) returned: " + i, String
903                    .valueOf((char) i).charAt(0) == (char) i);
904    }
905
906    /**
907     * @tests java.lang.String#valueOf(double)
908     */
909    public void test_valueOfD() {
910        // Test for method java.lang.String java.lang.String.valueOf(double)
911        assertEquals("Incorrect double string returned",
912                "1.7976931348623157E308", String.valueOf(Double.MAX_VALUE));
913    }
914
915    /**
916     * @tests java.lang.String#valueOf(float)
917     */
918    public void test_valueOfF() {
919        // Test for method java.lang.String java.lang.String.valueOf(float)
920        assertTrue("incorrect float string returned--got: "
921                + String.valueOf(1.0F) + " wanted: 1.0", String.valueOf(1.0F)
922                .equals("1.0"));
923        assertTrue("incorrect float string returned--got: "
924                + String.valueOf(0.9F) + " wanted: 0.9", String.valueOf(0.9F)
925                .equals("0.9"));
926        assertTrue("incorrect float string returned--got: "
927                + String.valueOf(109.567F) + " wanted: 109.567", String
928                .valueOf(109.567F).equals("109.567"));
929    }
930
931    /**
932     * @tests java.lang.String#valueOf(int)
933     */
934    public void test_valueOfI() {
935        // Test for method java.lang.String java.lang.String.valueOf(int)
936        assertEquals("returned invalid int string", "1", String.valueOf(1));
937    }
938
939    /**
940     * @tests java.lang.String#valueOf(long)
941     */
942    public void test_valueOfJ() {
943        // Test for method java.lang.String java.lang.String.valueOf(long)
944        assertEquals("returned incorrect long string", "927654321098", String
945                .valueOf(927654321098L));
946    }
947
948    /**
949     * @tests java.lang.String#valueOf(java.lang.Object)
950     */
951    public void test_valueOfLjava_lang_Object() {
952        // Test for method java.lang.String
953        // java.lang.String.valueOf(java.lang.Object)
954        assertTrue("Incorrect Object string returned", obj.toString().equals(
955                String.valueOf(obj)));
956    }
957
958    /**
959     * @tests java.lang.String#valueOf(boolean)
960     */
961    public void test_valueOfZ() {
962        // Test for method java.lang.String java.lang.String.valueOf(boolean)
963        assertTrue("Incorrect boolean string returned", String.valueOf(false)
964                .equals("false")
965                && (String.valueOf(true).equals("true")));
966    }
967
968    /**
969     * @tests java.lang.String#contentEquals(CharSequence cs)
970     */
971    public void test_contentEqualsLjava_lang_CharSequence() {
972        // Test for method java.lang.String
973        // java.lang.String.contentEquals(CharSequence cs)
974        assertFalse("Incorrect result of compare", "qwerty".contentEquals(""));
975    }
976
977    /**
978     * @tests java.lang.String#format(Locale, String, Object[])
979     */
980    @SuppressWarnings("boxing")
981    public void test_format() {
982        assertEquals("13% of sum is 0x11", String.format("%d%% of %s is 0x%x",
983                13, "sum", 17));
984        assertEquals("empty format", "", String.format("", 123, this));
985        try {
986            String.format(null);
987            fail("NPE is expected on null format");
988        } catch (NullPointerException ok) {
989        }
990    }
991}
992