Source Code Cross Referenced for String2Test.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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