Source Code Cross Referenced for BufferedInputStreamTest.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.File;
023:        import java.io.FileInputStream;
024:        import java.io.FileOutputStream;
025:        import java.io.IOException;
026:        import java.io.InputStream;
027:        import java.io.OutputStream;
028:        import junit.framework.TestCase;
029:        import tests.support.Support_PlatformFile;
030:
031:        public class BufferedInputStreamTest extends TestCase {
032:
033:            public String fileName;
034:
035:            private BufferedInputStream is;
036:
037:            private FileInputStream isFile;
038:
039:            byte[] ibuf = new byte[4096];
040:
041:            public String fileString = "Test_All_Tests\nTest_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
042:
043:            /*
044:             * @tests java.io.BufferedInputStream(InputStream)
045:             */
046:            public void test_ConstructorLjava_io_InputStream() {
047:                try {
048:                    BufferedInputStream str = new BufferedInputStream(null);
049:                    str.read();
050:                    fail("Expected an IOException");
051:                } catch (IOException e) {
052:                    // Expected
053:                }
054:            }
055:
056:            /*
057:             * @tests java.io.BufferedInputStream(InputStream)
058:             */
059:            public void test_ConstructorLjava_io_InputStreamI()
060:                    throws IOException {
061:                try {
062:                    BufferedInputStream str = new BufferedInputStream(null, 1);
063:                    str.read();
064:                    fail("Expected an IOException");
065:                } catch (IOException e) {
066:                    // Expected
067:                }
068:
069:                // Test for method java.io.BufferedInputStream(java.io.InputStream, int)
070:
071:                // Create buffer with exact size of file
072:                is = new BufferedInputStream(isFile, this .fileString.length());
073:                // Ensure buffer gets filled by evaluating one read
074:                is.read();
075:                // Close underlying FileInputStream, all but 1 buffered bytes should
076:                // still be available.
077:                isFile.close();
078:                // Read the remaining buffered characters, no IOException should
079:                // occur.
080:                is.skip(this .fileString.length() - 2);
081:                is.read();
082:                try {
083:                    // is.read should now throw an exception because it will have to
084:                    // be filled.
085:                    is.read();
086:                    fail("Exception should have been triggered by read()");
087:                } catch (IOException e) {
088:                    // Expected
089:                }
090:
091:                // regression test for harmony-2407
092:                new MockBufferedInputStream(null);
093:                assertNotNull(MockBufferedInputStream.buf);
094:                MockBufferedInputStream.buf = null;
095:                new MockBufferedInputStream(null, 100);
096:                assertNotNull(MockBufferedInputStream.buf);
097:            }
098:
099:            static class MockBufferedInputStream extends BufferedInputStream {
100:                static byte[] buf;
101:
102:                MockBufferedInputStream(InputStream is) throws IOException {
103:                    super (is);
104:                    buf = super .buf;
105:                }
106:
107:                MockBufferedInputStream(InputStream is, int size)
108:                        throws IOException {
109:                    super (is, size);
110:                    buf = super .buf;
111:                }
112:            }
113:
114:            /**
115:             * @tests java.io.BufferedInputStream#available()
116:             */
117:            public void test_available() throws IOException {
118:                assertTrue("Returned incorrect number of available bytes", is
119:                        .available() == fileString.length());
120:
121:                // Test that a closed stream throws an IOE for available()
122:                BufferedInputStream bis = new BufferedInputStream(
123:                        new ByteArrayInputStream(new byte[] { 'h', 'e', 'l',
124:                                'l', 'o', ' ', 't', 'i', 'm' }));
125:                int available = bis.available();
126:                bis.close();
127:                assertTrue(available != 0);
128:
129:                try {
130:                    bis.available();
131:                    fail("Expected test to throw IOE.");
132:                } catch (IOException ex) {
133:                    // expected
134:                }
135:            }
136:
137:            /**
138:             * @tests java.io.BufferedInputStream#close()
139:             */
140:            public void test_close() throws IOException {
141:                new BufferedInputStream(isFile).close();
142:
143:                // regression for HARMONY-667
144:                BufferedInputStream buf = new BufferedInputStream(null, 5);
145:                buf.close();
146:            }
147:
148:            /**
149:             * @tests java.io.BufferedInputStream#mark(int)
150:             */
151:            public void test_markI() throws IOException {
152:                byte[] buf1 = new byte[100];
153:                byte[] buf2 = new byte[100];
154:                is.skip(3000);
155:                is.mark(1000);
156:                is.read(buf1, 0, buf1.length);
157:                is.reset();
158:                is.read(buf2, 0, buf2.length);
159:                is.reset();
160:                assertTrue("Failed to mark correct position", new String(buf1,
161:                        0, buf1.length)
162:                        .equals(new String(buf2, 0, buf2.length)));
163:
164:                byte[] bytes = new byte[256];
165:                for (int i = 0; i < 256; i++) {
166:                    bytes[i] = (byte) i;
167:                }
168:                InputStream in = new BufferedInputStream(
169:                        new ByteArrayInputStream(bytes), 12);
170:                in.skip(6);
171:                in.mark(14);
172:                in.read(new byte[14], 0, 14);
173:                in.reset();
174:                assertTrue("Wrong bytes", in.read() == 6 && in.read() == 7);
175:
176:                in = new BufferedInputStream(new ByteArrayInputStream(bytes),
177:                        12);
178:                in.skip(6);
179:                in.mark(8);
180:                in.skip(7);
181:                in.reset();
182:                assertTrue("Wrong bytes 2", in.read() == 6 && in.read() == 7);
183:
184:                BufferedInputStream buf = new BufferedInputStream(
185:                        new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4 }),
186:                        2);
187:                buf.mark(3);
188:                bytes = new byte[3];
189:                int result = buf.read(bytes);
190:                assertEquals(3, result);
191:                assertEquals("Assert 0:", 0, bytes[0]);
192:                assertEquals("Assert 1:", 1, bytes[1]);
193:                assertEquals("Assert 2:", 2, bytes[2]);
194:                assertEquals("Assert 3:", 3, buf.read());
195:
196:                buf = new BufferedInputStream(new ByteArrayInputStream(
197:                        new byte[] { 0, 1, 2, 3, 4 }), 2);
198:                buf.mark(3);
199:                bytes = new byte[4];
200:                result = buf.read(bytes);
201:                assertEquals(4, result);
202:                assertEquals("Assert 4:", 0, bytes[0]);
203:                assertEquals("Assert 5:", 1, bytes[1]);
204:                assertEquals("Assert 6:", 2, bytes[2]);
205:                assertEquals("Assert 7:", 3, bytes[3]);
206:                assertEquals("Assert 8:", 4, buf.read());
207:                assertEquals("Assert 9:", -1, buf.read());
208:
209:                buf = new BufferedInputStream(new ByteArrayInputStream(
210:                        new byte[] { 0, 1, 2, 3, 4 }), 2);
211:                buf.mark(Integer.MAX_VALUE);
212:                buf.read();
213:                buf.close();
214:            }
215:
216:            /**
217:             * @tests java.io.BufferedInputStream#markSupported()
218:             */
219:            public void test_markSupported() {
220:                assertTrue("markSupported returned incorrect value", is
221:                        .markSupported());
222:            }
223:
224:            /**
225:             * @tests java.io.BufferedInputStream#read()
226:             */
227:            public void test_read() throws IOException {
228:                int c = is.read();
229:                assertTrue("read returned incorrect char", c == fileString
230:                        .charAt(0));
231:
232:                byte[] bytes = new byte[256];
233:                for (int i = 0; i < 256; i++) {
234:                    bytes[i] = (byte) i;
235:                }
236:                InputStream in = new BufferedInputStream(
237:                        new ByteArrayInputStream(bytes), 12);
238:                assertEquals("Wrong initial byte", 0, in.read()); // Fill the
239:                // buffer
240:                byte[] buf = new byte[14];
241:                in.read(buf, 0, 14); // Read greater than the buffer
242:                assertTrue("Wrong block read data", new String(buf, 0, 14)
243:                        .equals(new String(bytes, 1, 14)));
244:                assertEquals("Wrong bytes", 15, in.read()); // Check next byte
245:            }
246:
247:            /**
248:             * @tests java.io.BufferedInputStream#read(byte[], int, int)
249:             */
250:            public void test_read$BII_Exception() throws IOException {
251:                BufferedInputStream bis = new BufferedInputStream(null);
252:                try {
253:                    bis.read(null, -1, -1);
254:                    fail("should throw NullPointerException");
255:                } catch (NullPointerException e) {
256:                    // expected
257:                }
258:
259:                try {
260:                    bis.read(new byte[0], -1, -1);
261:                    fail("should throw IndexOutOfBoundsException");
262:                } catch (IndexOutOfBoundsException e) {
263:                    // expected
264:                }
265:
266:                try {
267:                    bis.read(new byte[0], 1, -1);
268:                    fail("should throw IndexOutOfBoundsException");
269:                } catch (IndexOutOfBoundsException e) {
270:                    // expected
271:                }
272:
273:                try {
274:                    bis.read(new byte[0], 1, 1);
275:                    fail("should throw IndexOutOfBoundsException");
276:                } catch (IndexOutOfBoundsException e) {
277:                    // expected
278:                }
279:
280:                bis.close();
281:
282:                try {
283:                    bis.read(null, -1, -1);
284:                    fail("should throw IOException");
285:                } catch (IOException e) {
286:                    // expected
287:                }
288:            }
289:
290:            /**
291:             * @tests java.io.BufferedInputStream#read(byte[], int, int)
292:             */
293:            public void test_read$BII() throws IOException {
294:                byte[] buf1 = new byte[100];
295:                is.skip(3000);
296:                is.mark(1000);
297:                is.read(buf1, 0, buf1.length);
298:                assertTrue("Failed to read correct data", new String(buf1, 0,
299:                        buf1.length).equals(fileString.substring(3000, 3100)));
300:
301:                BufferedInputStream bufin = new BufferedInputStream(
302:                        new InputStream() {
303:                            int size = 2, pos = 0;
304:
305:                            byte[] contents = new byte[size];
306:
307:                            @Override
308:                            public int read() throws IOException {
309:                                if (pos >= size) {
310:                                    throw new IOException(
311:                                            "Read past end of data");
312:                                }
313:                                return contents[pos++];
314:                            }
315:
316:                            @Override
317:                            public int read(byte[] buf, int off, int len)
318:                                    throws IOException {
319:                                if (pos >= size) {
320:                                    throw new IOException(
321:                                            "Read past end of data");
322:                                }
323:                                int toRead = len;
324:                                if (toRead > available()) {
325:                                    toRead = available();
326:                                }
327:                                System.arraycopy(contents, pos, buf, off,
328:                                        toRead);
329:                                pos += toRead;
330:                                return toRead;
331:                            }
332:
333:                            @Override
334:                            public int available() {
335:                                return size - pos;
336:                            }
337:                        });
338:                bufin.read();
339:                int result = bufin.read(new byte[2], 0, 2);
340:                assertTrue("Incorrect result: " + result, result == 1);
341:            }
342:
343:            /**
344:             * @tests java.io.BufferedInputStream#reset()
345:             */
346:            public void test_reset() throws IOException {
347:                byte[] buf1 = new byte[10];
348:                byte[] buf2 = new byte[10];
349:                is.mark(2000);
350:                is.read(buf1, 0, 10);
351:                is.reset();
352:                is.read(buf2, 0, 10);
353:                is.reset();
354:                assertTrue("Reset failed", new String(buf1, 0, buf1.length)
355:                        .equals(new String(buf2, 0, buf2.length)));
356:
357:                BufferedInputStream bIn = new BufferedInputStream(
358:                        new ByteArrayInputStream("1234567890".getBytes()));
359:                bIn.mark(10);
360:                for (int i = 0; i < 11; i++) {
361:                    bIn.read();
362:                }
363:                bIn.reset();
364:            }
365:
366:            /**
367:             * @tests java.io.BufferedInputStream#reset()
368:             */
369:            public void test_reset_Exception() throws IOException {
370:                BufferedInputStream bis = new BufferedInputStream(null);
371:
372:                // throws IOException with message "Mark has been invalidated"
373:                try {
374:                    bis.reset();
375:                    fail("should throw IOException");
376:                } catch (IOException e) {
377:                    // expected
378:                }
379:
380:                // does not throw IOException
381:                bis.mark(1);
382:                bis.reset();
383:
384:                bis.close();
385:
386:                // throws IOException with message "stream is closed"
387:                try {
388:                    bis.reset();
389:                    fail("should throw IOException");
390:                } catch (IOException e) {
391:                    // expected
392:                }
393:            }
394:
395:            /**
396:             * @tests java.io.BufferedInputStream#skip(long)
397:             */
398:            public void test_skipJ() throws IOException {
399:                byte[] buf1 = new byte[10];
400:                is.mark(2000);
401:                is.skip(1000);
402:                is.read(buf1, 0, buf1.length);
403:                is.reset();
404:                assertTrue("Failed to skip to correct position", new String(
405:                        buf1, 0, buf1.length).equals(fileString.substring(1000,
406:                        1010)));
407:
408:                // regression for HARMONY-667
409:                try {
410:                    BufferedInputStream buf = new BufferedInputStream(null, 5);
411:                    buf.skip(10);
412:                    fail("Should throw IOException");
413:                } catch (IOException e) {
414:                    // Expected
415:                }
416:            }
417:
418:            /**
419:             * Sets up the fixture, for example, open a network connection. This method
420:             * is called before a test is executed.
421:             */
422:            @Override
423:            protected void setUp() throws IOException {
424:                fileName = System.getProperty("user.dir");
425:                String separator = System.getProperty("file.separator");
426:                if (fileName.charAt(fileName.length() - 1) == separator
427:                        .charAt(0)) {
428:                    fileName = Support_PlatformFile.getNewPlatformFile(
429:                            fileName, "input.tst");
430:                } else {
431:                    fileName = Support_PlatformFile.getNewPlatformFile(fileName
432:                            + separator, "input.tst");
433:                }
434:                OutputStream fos = new FileOutputStream(fileName);
435:                fos.write(fileString.getBytes());
436:                fos.close();
437:                isFile = new FileInputStream(fileName);
438:                is = new BufferedInputStream(isFile);
439:            }
440:
441:            /**
442:             * Tears down the fixture, for example, close a network connection. This
443:             * method is called after a test is executed.
444:             */
445:            @Override
446:            protected void tearDown() {
447:                try {
448:                    is.close();
449:                } catch (Exception e) {
450:                }
451:                try {
452:                    File f = new File(fileName);
453:                    f.delete();
454:                } catch (Exception e) {
455:                }
456:            }
457:        }
ww_w__._j_a_va2___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.