Source Code Cross Referenced for InputStreamReaderTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » io » 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.io 
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.io;
019:
020:        import java.io.BufferedInputStream;
021:        import java.io.ByteArrayInputStream;
022:        import java.io.ByteArrayOutputStream;
023:        import java.io.IOException;
024:        import java.io.InputStream;
025:        import java.io.InputStreamReader;
026:        import java.io.OutputStreamWriter;
027:        import java.io.UnsupportedEncodingException;
028:        import java.nio.charset.Charset;
029:        import java.nio.charset.CharsetDecoder;
030:        import java.nio.charset.CodingErrorAction;
031:        import java.nio.charset.MalformedInputException;
032:        import java.util.Arrays;
033:
034:        import junit.framework.TestCase;
035:
036:        public class InputStreamReaderTest extends TestCase {
037:
038:            static class LimitedByteArrayInputStream extends
039:                    ByteArrayInputStream {
040:
041:                // A ByteArrayInputStream that only returns a single byte per read
042:                byte[] bytes;
043:
044:                int count;
045:
046:                public LimitedByteArrayInputStream(int type) {
047:                    super (new byte[0]);
048:                    switch (type) {
049:                    case 0:
050:                        bytes = new byte[] { 0x61, 0x72 };
051:                        break;
052:                    case 1:
053:                        bytes = new byte[] { (byte) 0xff, (byte) 0xfe, 0x61,
054:                                0x72 };
055:                        break;
056:                    case 2:
057:                        bytes = new byte[] { '\u001b', '$', 'B', '6', 'e', 'B',
058:                                'h', '\u001b', '(', 'B' };
059:                        break;
060:                    }
061:                    count = bytes.length;
062:                }
063:
064:                @Override
065:                public int available() {
066:                    return count;
067:                }
068:
069:                @Override
070:                public int read() {
071:                    if (count == 0) {
072:                        return -1;
073:                    }
074:                    count--;
075:                    return bytes[bytes.length - count];
076:                }
077:
078:                @Override
079:                public int read(byte[] buffer, int offset, int length) {
080:                    if (count == 0) {
081:                        return -1;
082:                    }
083:                    if (length == 0) {
084:                        return 0;
085:                    }
086:                    buffer[offset] = bytes[bytes.length - count];
087:                    count--;
088:                    return 1;
089:                }
090:            }
091:
092:            public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\n";
093:
094:            private InputStream fis;
095:
096:            private InputStream in;
097:
098:            private InputStreamReader is;
099:
100:            private InputStreamReader reader;
101:
102:            private final String source = "This is a test message with Unicode character. \u4e2d\u56fd is China's name in Chinese";
103:
104:            /*
105:             * @see TestCase#setUp()
106:             */
107:            @Override
108:            protected void setUp() throws Exception {
109:                super .setUp();
110:
111:                in = new ByteArrayInputStream(source.getBytes("UTF-8"));
112:                reader = new InputStreamReader(in, "UTF-8");
113:
114:                ByteArrayOutputStream bos = new ByteArrayOutputStream();
115:                OutputStreamWriter osw = new OutputStreamWriter(bos);
116:                char[] buf = new char[fileString.length()];
117:                fileString.getChars(0, fileString.length(), buf, 0);
118:                osw.write(buf);
119:                osw.close();
120:                fis = new ByteArrayInputStream(bos.toByteArray());
121:                is = new InputStreamReader(fis);
122:            }
123:
124:            /*
125:             * @see TestCase#tearDown()
126:             */
127:            @Override
128:            protected void tearDown() throws Exception {
129:                try {
130:                    in.close();
131:                    is.close();
132:                    fis.close();
133:                } catch (IOException e) {
134:                    // Ignored
135:                }
136:
137:                super .tearDown();
138:            }
139:
140:            /**
141:             * @tests java.io.InputStreamReader#close()
142:             */
143:            public void test_close() throws IOException {
144:                is.close();
145:                try {
146:                    is.read();
147:                    fail("Should throw IOException");
148:                } catch (IOException e) {
149:                    // Expected
150:                }
151:
152:                reader.close();
153:                try {
154:                    reader.ready();
155:                    fail("Should throw IOException");
156:                } catch (IOException e) {
157:                    // Expected
158:                }
159:
160:                // Should be a no-op
161:                reader.close();
162:
163:                // Tests after reader closed
164:                in = new BufferedInputStream(
165:                        this 
166:                                .getClass()
167:                                .getClassLoader()
168:                                .getResourceAsStream(
169:                                        "org/apache/harmony/luni/tests/java/io/testfile-utf8.txt"));
170:                reader = new InputStreamReader(in, "utf-8");
171:                in.close();
172:                try {
173:                    int count = reader.read(new char[1]);
174:                    fail("count:" + count);
175:                } catch (IOException e) {
176:                    // Expected
177:                }
178:                try {
179:                    reader.read();
180:                    fail();
181:                } catch (IOException e) {
182:                    // Expected
183:                }
184:
185:                assertFalse(reader.ready());
186:                Charset cs = Charset.forName("utf-8");
187:                assertEquals(cs, Charset.forName(reader.getEncoding()));
188:            }
189:
190:            /**
191:             * @tests java.io.InputStreamReader#InputStreamReader(java.io.InputStream)
192:             */
193:            public void test_ConstructorLjava_io_InputStream()
194:                    throws IOException {
195:                try {
196:                    reader = new InputStreamReader(null);
197:                    fail();
198:                } catch (NullPointerException e) {
199:                    // Expected
200:                }
201:                InputStreamReader reader2 = new InputStreamReader(in);
202:                reader2.close();
203:            }
204:
205:            /**
206:             * @tests java.io.InputStreamReader#InputStreamReader(java.io.InputStream,
207:             *        java.lang.String)
208:             */
209:            public void test_ConstructorLjava_io_InputStreamLjava_lang_String()
210:                    throws IOException {
211:                is = new InputStreamReader(fis, "8859_1");
212:
213:                try {
214:                    is = new InputStreamReader(fis, "Bogus");
215:                    fail("Failed to throw Unsupported Encoding exception");
216:                } catch (UnsupportedEncodingException e) {
217:                    // Expected
218:                }
219:
220:                try {
221:                    reader = new InputStreamReader(null, "utf-8");
222:                    fail();
223:                } catch (NullPointerException e) {
224:                    // Expected
225:                }
226:                try {
227:                    reader = new InputStreamReader(in, (String) null);
228:                    fail();
229:                } catch (NullPointerException e) {
230:                    // Expected
231:                }
232:                try {
233:                    reader = new InputStreamReader(in, "");
234:                    fail();
235:                } catch (UnsupportedEncodingException e) {
236:                    // Expected
237:                }
238:                try {
239:                    reader = new InputStreamReader(in, "badname");
240:                    fail();
241:                } catch (UnsupportedEncodingException e) {
242:                    // Expected
243:                }
244:                InputStreamReader reader2 = new InputStreamReader(in, "utf-8");
245:                assertEquals(Charset.forName(reader2.getEncoding()), Charset
246:                        .forName("utf-8"));
247:                reader2.close();
248:                reader2 = new InputStreamReader(in, "utf8");
249:                assertEquals(Charset.forName(reader2.getEncoding()), Charset
250:                        .forName("utf-8"));
251:                reader2.close();
252:            }
253:
254:            /**
255:             * @tests java.io.InputStreamReader(java.io.InputStream,
256:             *        java.nio.charset.Charset)
257:             */
258:            public void test_ConstructorLjava_io_InputStreamLjava_nio_charset_Charset()
259:                    throws IOException {
260:                Charset cs = Charset.forName("utf-8");
261:                try {
262:                    reader = new InputStreamReader(null, cs);
263:                    fail();
264:                } catch (NullPointerException e) {
265:                    // Expected
266:                }
267:                try {
268:                    reader = new InputStreamReader(in, (Charset) null);
269:                    fail();
270:                } catch (NullPointerException e) {
271:                    // Expected
272:                }
273:                InputStreamReader reader2 = new InputStreamReader(in, cs);
274:                assertEquals(Charset.forName(reader2.getEncoding()), cs);
275:                reader2.close();
276:            }
277:
278:            /**
279:             * @tests java.io.InputStreamReader(java.io.InputStream,
280:             *        java.nio.charset.CharsetDecoder)
281:             */
282:            public void test_ConstructorLjava_io_InputStreamLjava_nio_charset_CharsetDecoder()
283:                    throws IOException {
284:                CharsetDecoder decoder = Charset.forName("utf-8").newDecoder();
285:                try {
286:                    reader = new InputStreamReader(null, decoder);
287:                    fail();
288:                } catch (NullPointerException e) {
289:                    // Expected
290:                }
291:                try {
292:                    reader = new InputStreamReader(in, (CharsetDecoder) null);
293:                    fail();
294:                } catch (NullPointerException e) {
295:                    // Expected
296:                }
297:                InputStreamReader reader2 = new InputStreamReader(in, decoder);
298:                assertEquals(Charset.forName(reader2.getEncoding()), decoder
299:                        .charset());
300:                reader2.close();
301:            }
302:
303:            /**
304:             * @tests java.io.InputStreamReader#getEncoding()
305:             */
306:            public void test_getEncoding() throws IOException {
307:                InputStreamReader isr = new InputStreamReader(fis, "8859_1");
308:                assertEquals("Returned incorrect encoding when setting 8859_1",
309:                        "ISO8859_1", isr.getEncoding());
310:
311:                isr = new InputStreamReader(fis, "ISO-8859-1");
312:                assertEquals(
313:                        "Returned incorrect encoding when setting ISO-8859-1",
314:                        "ISO8859_1", isr.getEncoding());
315:
316:                byte b[] = new byte[5];
317:                isr = new InputStreamReader(new ByteArrayInputStream(b),
318:                        "UTF-16BE");
319:                isr.close();
320:                assertNull(isr.getEncoding());
321:
322:                try {
323:                    isr = new InputStreamReader(System.in, "UTF-16BE");
324:                } catch (UnsupportedEncodingException e) {
325:                    // Ignored
326:                }
327:                assertEquals("UnicodeBigUnmarked", isr.getEncoding());
328:            }
329:
330:            /**
331:             * @tests java.io.InputStreamReader#read()
332:             */
333:            public void test_read() throws IOException {
334:                assertEquals('T', (char) reader.read());
335:                assertEquals('h', (char) reader.read());
336:                assertEquals('i', (char) reader.read());
337:                assertEquals('s', (char) reader.read());
338:                assertEquals(' ', (char) reader.read());
339:                reader.read(new char[source.length() - 5], 0,
340:                        source.length() - 5);
341:                assertEquals(-1, reader.read());
342:
343:                int c = is.read();
344:                assertTrue("returned incorrect char", (char) c == fileString
345:                        .charAt(0));
346:                InputStreamReader reader = new InputStreamReader(
347:                        new ByteArrayInputStream(new byte[] { (byte) 0xe8,
348:                                (byte) 0x9d, (byte) 0xa5 }), "UTF8");
349:                assertTrue("wrong double byte char", reader.read() == '\u8765');
350:
351:                // Regression for HARMONY-166
352:                InputStream in;
353:
354:                in = new LimitedByteArrayInputStream(0);
355:                reader = new InputStreamReader(in, "UTF-16BE");
356:                assertEquals("Incorrect byte UTF-16BE", '\u6172', reader.read());
357:
358:                in = new LimitedByteArrayInputStream(0);
359:                reader = new InputStreamReader(in, "UTF-16LE");
360:                assertEquals("Incorrect byte UTF-16BE", '\u7261', reader.read());
361:
362:                in = new LimitedByteArrayInputStream(1);
363:                reader = new InputStreamReader(in, "UTF-16");
364:                assertEquals("Incorrect byte UTF-16BE", '\u7261', reader.read());
365:
366:                /*
367:                 * Temporarily commented out due to lack of ISO2022 support in ICU4J 3.8
368:                 * in = new LimitedByteArrayInputStream(2); reader = new
369:                 * InputStreamReader(in, "ISO2022JP"); assertEquals("Incorrect byte
370:                 * ISO2022JP 1", '\u4e5d', reader.read()); assertEquals("Incorrect byte
371:                 * ISO2022JP 2", '\u7b2c', reader.read());
372:                 */
373:            }
374:
375:            /*
376:             * Class under test for int read() Regression for Harmony-411
377:             */
378:            public void test_read_1() throws IOException {
379:                // if the decoder is constructed by InputStreamReader itself, the
380:                // decoder's default error action is REPLACE
381:                InputStreamReader isr = new InputStreamReader(
382:                        new ByteArrayInputStream(new byte[] { -32, -96 }),
383:                        "UTF-8");
384:                assertEquals("read() return incorrect value", 65533, isr.read());
385:
386:                InputStreamReader isr2 = new InputStreamReader(
387:                        new ByteArrayInputStream(new byte[] { -32, -96 }),
388:                        Charset.forName("UTF-8"));
389:                assertEquals("read() return incorrect value", 65533, isr2
390:                        .read());
391:
392:                // if the decoder is passed in, keep its status intact
393:                CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
394:                decoder.onMalformedInput(CodingErrorAction.REPORT);
395:                InputStreamReader isr3 = new InputStreamReader(
396:                        new ByteArrayInputStream(new byte[] { -32, -96 }),
397:                        decoder);
398:                try {
399:                    isr3.read();
400:                    fail("Should throw MalformedInputException");
401:                } catch (MalformedInputException e) {
402:                    // expected
403:                }
404:
405:                CharsetDecoder decoder2 = Charset.forName("UTF-8").newDecoder();
406:                decoder2.onMalformedInput(CodingErrorAction.IGNORE);
407:                InputStreamReader isr4 = new InputStreamReader(
408:                        new ByteArrayInputStream(new byte[] { -32, -96 }),
409:                        decoder2);
410:                assertEquals("read() return incorrect value", -1, isr4.read());
411:
412:                CharsetDecoder decoder3 = Charset.forName("UTF-8").newDecoder();
413:                decoder3.onMalformedInput(CodingErrorAction.REPLACE);
414:                InputStreamReader isr5 = new InputStreamReader(
415:                        new ByteArrayInputStream(new byte[] { -32, -96 }),
416:                        decoder3);
417:                assertEquals("read() return incorrect value", 65533, isr5
418:                        .read());
419:            }
420:
421:            public void test_read_specialCharset() throws IOException {
422:                reader.close();
423:                in = this 
424:                        .getClass()
425:                        .getClassLoader()
426:                        .getResourceAsStream(
427:                                "org/apache/harmony/luni/tests/java/io/testfile-utf8.txt");
428:                reader = new InputStreamReader(in, "utf-8");
429:                int c;
430:                StringBuffer sb = new StringBuffer();
431:                while ((c = reader.read()) != -1) {
432:                    sb.append((char) c);
433:                }
434:                // delete BOM
435:                assertEquals(source, sb.deleteCharAt(0).toString());
436:
437:                sb.setLength(0);
438:                reader.close();
439:                in = this .getClass().getClassLoader().getResourceAsStream(
440:                        "org/apache/harmony/luni/tests/java/io/testfile.txt");
441:                try {
442:                    reader = new InputStreamReader(in, "gb18030");
443:                } catch (UnsupportedEncodingException e) {
444:                    System.out
445:                            .println("GB18030 is not supported, abort test InputStreamReaderTest.testSpecialCharsetReading().");
446:                }
447:                while ((c = reader.read()) != -1) {
448:                    sb.append((char) c);
449:                }
450:                assertEquals(source, sb.toString());
451:            }
452:
453:            /**
454:             * @tests java.io.InputStreamReader#read(char[], int, int)
455:             */
456:            public void test_read$CII() throws IOException {
457:                char[] rbuf = new char[100];
458:                char[] sbuf = new char[100];
459:                fileString.getChars(0, 100, sbuf, 0);
460:                is.read(rbuf, 0, 100);
461:                for (int i = 0; i < rbuf.length; i++) {
462:                    assertTrue("returned incorrect chars", rbuf[i] == sbuf[i]);
463:                }
464:
465:                // Test successive reads
466:                byte[] data = new byte[8192 * 2];
467:                Arrays.fill(data, (byte) 116); // 116 = ISO-8859-1 value for 't'
468:                ByteArrayInputStream bis = new ByteArrayInputStream(data);
469:                InputStreamReader isr = new InputStreamReader(bis, "ISO-8859-1");
470:
471:                // One less than the InputStreamReader.BUFFER_SIZE
472:                char[] buf = new char[8191];
473:                int bytesRead = isr.read(buf, 0, buf.length);
474:                assertFalse(-1 == bytesRead);
475:                bytesRead = isr.read(buf, 0, buf.length);
476:                assertFalse(-1 == bytesRead);
477:
478:                bis = new ByteArrayInputStream(source.getBytes("UTF-8"));
479:                isr = new InputStreamReader(in, "UTF-8");
480:                char[] chars = new char[source.length()];
481:                assertEquals(source.length() - 3, isr.read(chars, 0,
482:                        chars.length - 3));
483:                assertEquals(3, isr.read(chars, 0, 10));
484:            }
485:
486:            /*
487:             * Class under test for int read(char[], int, int)
488:             */
489:            public void test_read$CII_1() throws IOException {
490:                try {
491:                    // Throws IndexOutOfBoundsException before NullPointerException
492:                    reader.read(null, -1, 1);
493:                    fail("Should throw IndexOutOfBoundsException");
494:                } catch (IndexOutOfBoundsException e) {
495:                    // expected
496:                }
497:
498:                try {
499:                    // Throws NullPointerException before IndexOutOfBoundsException
500:                    reader.read(null, 0, -1);
501:                    fail("Should throw NullPointerException");
502:                } catch (NullPointerException e) {
503:                    // expected
504:                }
505:
506:                try {
507:                    reader.read(null, 0, 1);
508:                    fail();
509:                } catch (NullPointerException e) {
510:                    // Expected
511:                }
512:                try {
513:                    reader.read(new char[3], -1, 1);
514:                    fail();
515:                } catch (IndexOutOfBoundsException e) {
516:                    // Expected
517:                }
518:                try {
519:                    reader.read(new char[3], 0, -1);
520:                    fail();
521:                } catch (IndexOutOfBoundsException e) {
522:                    // Expected
523:                }
524:                try {
525:                    reader.read(new char[3], 1, 3);
526:                    fail();
527:                } catch (IndexOutOfBoundsException e) {
528:                    // Expected
529:                }
530:                assertEquals(0, reader.read(new char[3], 3, 0));
531:                char[] chars = new char[source.length()];
532:                assertEquals(0, reader.read(chars, 0, 0));
533:                assertEquals(0, chars[0]);
534:                assertEquals(3, reader.read(chars, 0, 3));
535:                assertEquals(5, reader.read(chars, 3, 5));
536:                assertEquals(source.length() - 8, reader.read(chars, 8,
537:                        chars.length - 8));
538:                assertTrue(Arrays.equals(chars, source.toCharArray()));
539:                assertEquals(-1, reader.read(chars, 0, chars.length));
540:                assertTrue(Arrays.equals(chars, source.toCharArray()));
541:            }
542:
543:            /**
544:             * @tests java.io.InputStreamReader#ready()
545:             */
546:            public void test_ready() throws IOException {
547:                assertTrue("Ready test failed", is.ready());
548:                is.read();
549:                assertTrue("More chars, but not ready", is.ready());
550:
551:                assertTrue(reader.ready());
552:                reader.read(new char[source.length()]);
553:                assertFalse(reader.ready());
554:            }
555:        }
w__w___w__.__j___a_v__a2s.___c___o__m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.