Source Code Cross Referenced for NullInputStreamTest.java in  » Library » apache-common-IO » org » apache » commons » io » input » 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 » Library » apache common IO » org.apache.commons.io.input 
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:        package org.apache.commons.io.input;
018:
019:        import java.io.EOFException;
020:        import java.io.IOException;
021:        import java.io.InputStream;
022:
023:        import junit.framework.TestCase;
024:
025:        /**
026:         * JUnit Test Case for {@link NullInputStream}.
027:         *
028:         * @version $Id: NullInputStreamTest.java 463529 2006-10-13 00:37:09Z niallp $
029:         */
030:        public class NullInputStreamTest extends TestCase {
031:
032:            /** Constructor */
033:            public NullInputStreamTest(String name) {
034:                super (name);
035:            }
036:
037:            /** Set up */
038:            protected void setUp() throws Exception {
039:                super .setUp();
040:            }
041:
042:            /** Tear Down */
043:            protected void tearDown() throws Exception {
044:                super .tearDown();
045:            }
046:
047:            /**
048:             * Test <code>available()</code> method.
049:             */
050:            public void testRead() throws Exception {
051:                int size = 5;
052:                InputStream input = new TestNullInputStream(size);
053:                for (int i = 0; i < size; i++) {
054:                    assertEquals("Check Size [" + i + "]", (size - i), input
055:                            .available());
056:                    assertEquals("Check Value [" + i + "]", i, input.read());
057:                }
058:                assertEquals("Available after contents all read", 0, input
059:                        .available());
060:
061:                // Check availbale is zero after End of file
062:                assertEquals("End of File", -1, input.read());
063:                assertEquals("Available after End of File", 0, input
064:                        .available());
065:
066:                // Test reading after the end of file
067:                try {
068:                    int result = input.read();
069:                    fail("Should have thrown an IOException, byte=[" + result
070:                            + "]");
071:                } catch (IOException e) {
072:                    assertEquals("Read after end of file", e.getMessage());
073:                }
074:
075:                // Close - should reset
076:                input.close();
077:                assertEquals("Available after close", size, input.available());
078:            }
079:
080:            /**
081:             * Test <code>read(byte[])</code> method.
082:             */
083:            public void testReadByteArray() throws Exception {
084:                byte[] bytes = new byte[10];
085:                InputStream input = new TestNullInputStream(15);
086:
087:                // Read into array
088:                int count1 = input.read(bytes);
089:                assertEquals("Read 1", bytes.length, count1);
090:                for (int i = 0; i < count1; i++) {
091:                    assertEquals("Check Bytes 1", i, bytes[i]);
092:                }
093:
094:                // Read into array
095:                int count2 = input.read(bytes);
096:                assertEquals("Read 2", 5, count2);
097:                for (int i = 0; i < count2; i++) {
098:                    assertEquals("Check Bytes 2", count1 + i, bytes[i]);
099:                }
100:
101:                // End of File
102:                int count3 = input.read(bytes);
103:                assertEquals("Read 3 (EOF)", -1, count3);
104:
105:                // Test reading after the end of file
106:                try {
107:                    int count4 = input.read(bytes);
108:                    fail("Should have thrown an IOException, byte=[" + count4
109:                            + "]");
110:                } catch (IOException e) {
111:                    assertEquals("Read after end of file", e.getMessage());
112:                }
113:
114:                // reset by closing
115:                input.close();
116:
117:                // Read into array using offset & length
118:                int offset = 2;
119:                int lth = 4;
120:                int count5 = input.read(bytes, offset, lth);
121:                assertEquals("Read 5", lth, count5);
122:                for (int i = offset; i < lth; i++) {
123:                    assertEquals("Check Bytes 2", i, bytes[i]);
124:                }
125:            }
126:
127:            /**
128:             * Test when configured to throw an EOFException at the end of file
129:             * (rather than return -1).
130:             */
131:            public void testEOFException() throws Exception {
132:                InputStream input = new TestNullInputStream(2, false, true);
133:                assertEquals("Read 1", 0, input.read());
134:                assertEquals("Read 2", 1, input.read());
135:                try {
136:                    int result = input.read();
137:                    fail("Should have thrown an EOFException, byte=[" + result
138:                            + "]");
139:                } catch (EOFException e) {
140:                    // expected
141:                }
142:            }
143:
144:            /**
145:             * Test <code>mark()</code> and <code>reset()</code> methods.
146:             */
147:            public void testMarkAndReset() throws Exception {
148:                int position = 0;
149:                int readlimit = 10;
150:                InputStream input = new TestNullInputStream(100, true, false);
151:
152:                assertTrue("Mark Should be Supported", input.markSupported());
153:
154:                // No Mark
155:                try {
156:                    input.reset();
157:                    fail("Read limit exceeded, expected IOException ");
158:                } catch (Exception e) {
159:                    assertEquals("No Mark IOException message",
160:                            "No position has been marked", e.getMessage());
161:                }
162:
163:                for (; position < 3; position++) {
164:                    assertEquals("Read Before Mark [" + position + "]",
165:                            position, input.read());
166:                }
167:
168:                // Mark
169:                input.mark(readlimit);
170:
171:                // Read further
172:                for (int i = 0; i < 3; i++) {
173:                    assertEquals("Read After Mark [" + i + "]", (position + i),
174:                            input.read());
175:                }
176:
177:                // Reset
178:                input.reset();
179:
180:                // Read From marked position
181:                for (int i = 0; i < readlimit + 1; i++) {
182:                    assertEquals("Read After Reset [" + i + "]",
183:                            (position + i), input.read());
184:                }
185:
186:                // Reset after read limit passed
187:                try {
188:                    input.reset();
189:                    fail("Read limit exceeded, expected IOException ");
190:                } catch (Exception e) {
191:                    assertEquals(
192:                            "Read limit IOException message",
193:                            "Marked position ["
194:                                    + position
195:                                    + "] is no longer valid - passed the read limit ["
196:                                    + readlimit + "]", e.getMessage());
197:                }
198:            }
199:
200:            /**
201:             * Test <code>mark()</code> not supported.
202:             */
203:            public void testMarkNotSupported() throws Exception {
204:                InputStream input = new TestNullInputStream(100, false, true);
205:                assertFalse("Mark Should NOT be Supported", input
206:                        .markSupported());
207:
208:                try {
209:                    input.mark(5);
210:                    fail("mark() should throw UnsupportedOperationException");
211:                } catch (UnsupportedOperationException e) {
212:                    assertEquals("mark() error message", "Mark not supported",
213:                            e.getMessage());
214:                }
215:
216:                try {
217:                    input.reset();
218:                    fail("reset() should throw UnsupportedOperationException");
219:                } catch (UnsupportedOperationException e) {
220:                    assertEquals("reset() error message", "Mark not supported",
221:                            e.getMessage());
222:                }
223:            }
224:
225:            /**
226:             * Test <code>skip()</code> method.
227:             */
228:            public void testSkip() throws Exception {
229:                InputStream input = new TestNullInputStream(10, true, false);
230:                assertEquals("Read 1", 0, input.read());
231:                assertEquals("Read 2", 1, input.read());
232:                assertEquals("Skip 1", 5, input.skip(5));
233:                assertEquals("Read 3", 7, input.read());
234:                assertEquals("Skip 2", 2, input.skip(5)); // only 2 left to skip
235:                assertEquals("Skip 3 (EOF)", -1, input.skip(5)); // End of file
236:                try {
237:                    input.skip(5); //
238:                    fail("Expected IOException for skipping after end of file");
239:                } catch (Exception e) {
240:                    assertEquals("Skip after EOF IOException message",
241:                            "Skip after end of file", e.getMessage());
242:                }
243:            }
244:
245:            // ------------- Test NullInputStream implementation -------------
246:
247:            private static final class TestNullInputStream extends
248:                    NullInputStream {
249:                public TestNullInputStream(int size) {
250:                    super (size);
251:                }
252:
253:                public TestNullInputStream(int size, boolean markSupported,
254:                        boolean throwEofException) {
255:                    super (size, markSupported, throwEofException);
256:                }
257:
258:                protected int processByte() {
259:                    return ((int) getPosition() - 1);
260:                }
261:
262:                protected void processBytes(byte[] bytes, int offset, int length) {
263:                    int startPos = (int) getPosition() - length;
264:                    for (int i = offset; i < length; i++) {
265:                        bytes[i] = (byte) (startPos + i);
266:                    }
267:                }
268:
269:            }
270:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.