Source Code Cross Referenced for TestDocumentInputStream.java in  » Collaboration » poi-3.0.2-beta2 » org » apache » poi » poifs » filesystem » 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 » Collaboration » poi 3.0.2 beta2 » org.apache.poi.poifs.filesystem 
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.poi.poifs.filesystem;
019:
020:        import java.io.*;
021:
022:        import java.util.*;
023:
024:        import junit.framework.*;
025:
026:        import org.apache.poi.poifs.property.DirectoryProperty;
027:        import org.apache.poi.poifs.property.DocumentProperty;
028:        import org.apache.poi.poifs.storage.RawDataBlock;
029:
030:        /**
031:         * Class to test DocumentInputStream functionality
032:         *
033:         * @author Marc Johnson
034:         */
035:
036:        public class TestDocumentInputStream extends TestCase {
037:
038:            /**
039:             * Constructor TestDocumentInputStream
040:             *
041:             * @param name
042:             *
043:             * @exception IOException
044:             */
045:
046:            public TestDocumentInputStream(String name) throws IOException {
047:                super (name);
048:                int blocks = (_workbook_size + 511) / 512;
049:
050:                _workbook_data = new byte[512 * blocks];
051:                Arrays.fill(_workbook_data, (byte) -1);
052:                for (int j = 0; j < _workbook_size; j++) {
053:                    _workbook_data[j] = (byte) (j * j);
054:                }
055:                RawDataBlock[] rawBlocks = new RawDataBlock[blocks];
056:                ByteArrayInputStream stream = new ByteArrayInputStream(
057:                        _workbook_data);
058:
059:                for (int j = 0; j < blocks; j++) {
060:                    rawBlocks[j] = new RawDataBlock(stream);
061:                }
062:                POIFSDocument document = new POIFSDocument("Workbook",
063:                        rawBlocks, _workbook_size);
064:
065:                _workbook = new DocumentNode(document.getDocumentProperty(),
066:                        new DirectoryNode(new DirectoryProperty("Root Entry"),
067:                                null, null));
068:            }
069:
070:            private DocumentNode _workbook;
071:            private byte[] _workbook_data;
072:            private static final int _workbook_size = 5000;
073:
074:            // non-even division of _workbook_size, also non-even division of
075:            // any block size
076:            private static final int _buffer_size = 6;
077:
078:            /**
079:             * test constructor
080:             *
081:             * @exception IOException
082:             */
083:
084:            public void testConstructor() throws IOException {
085:                DocumentInputStream stream = new DocumentInputStream(_workbook);
086:
087:                assertEquals(_workbook_size, stream.available());
088:            }
089:
090:            /**
091:             * test available() behavior
092:             *
093:             * @exception IOException
094:             */
095:
096:            public void testAvailable() throws IOException {
097:                DocumentInputStream stream = new DocumentInputStream(_workbook);
098:
099:                assertEquals(_workbook_size, stream.available());
100:                stream.close();
101:                try {
102:                    stream.available();
103:                    fail("Should have caught IOException");
104:                } catch (IOException ignored) {
105:
106:                    // as expected
107:                }
108:            }
109:
110:            /**
111:             * test mark/reset/markSupported.
112:             *
113:             * @exception IOException
114:             */
115:
116:            public void testMarkFunctions() throws IOException {
117:                DocumentInputStream stream = new DocumentInputStream(_workbook);
118:                byte[] buffer = new byte[_workbook_size / 5];
119:
120:                stream.read(buffer);
121:                for (int j = 0; j < buffer.length; j++) {
122:                    assertEquals("checking byte " + j, _workbook_data[j],
123:                            buffer[j]);
124:                }
125:                assertEquals(_workbook_size - buffer.length, stream.available());
126:                stream.reset();
127:                assertEquals(_workbook_size, stream.available());
128:                stream.read(buffer);
129:                stream.mark(12);
130:                stream.read(buffer);
131:                assertEquals(_workbook_size - (2 * buffer.length), stream
132:                        .available());
133:                for (int j = buffer.length; j < (2 * buffer.length); j++) {
134:                    assertEquals("checking byte " + j, _workbook_data[j],
135:                            buffer[j - buffer.length]);
136:                }
137:                stream.reset();
138:                assertEquals(_workbook_size - buffer.length, stream.available());
139:                stream.read(buffer);
140:                assertEquals(_workbook_size - (2 * buffer.length), stream
141:                        .available());
142:                for (int j = buffer.length; j < (2 * buffer.length); j++) {
143:                    assertEquals("checking byte " + j, _workbook_data[j],
144:                            buffer[j - buffer.length]);
145:                }
146:                assertTrue(stream.markSupported());
147:            }
148:
149:            /**
150:             * test simple read method
151:             *
152:             * @exception IOException
153:             */
154:
155:            public void testReadSingleByte() throws IOException {
156:                DocumentInputStream stream = new DocumentInputStream(_workbook);
157:                int remaining = _workbook_size;
158:
159:                for (int j = 0; j < _workbook_size; j++) {
160:                    int b = stream.read();
161:                    assertTrue("checking sign of " + j, b >= 0);
162:                    assertEquals("validating byte " + j, _workbook_data[j],
163:                            (byte) b);
164:                    remaining--;
165:                    assertEquals("checking remaining after reading byte " + j,
166:                            remaining, stream.available());
167:                }
168:                assertEquals(-1, stream.read());
169:                stream.close();
170:                try {
171:                    stream.read();
172:                    fail("Should have caught IOException");
173:                } catch (IOException ignored) {
174:
175:                    // as expected
176:                }
177:            }
178:
179:            /**
180:             * Test buffered read
181:             *
182:             * @exception IOException
183:             */
184:
185:            public void testBufferRead() throws IOException {
186:                DocumentInputStream stream = new DocumentInputStream(_workbook);
187:
188:                try {
189:                    stream.read(null);
190:                    fail("Should have caught NullPointerException");
191:                } catch (NullPointerException ignored) {
192:
193:                    // as expected
194:                }
195:
196:                // test reading zero length buffer
197:                assertEquals(0, stream.read(new byte[0]));
198:                assertEquals(_workbook_size, stream.available());
199:                byte[] buffer = new byte[_buffer_size];
200:                int offset = 0;
201:
202:                while (stream.available() >= buffer.length) {
203:                    assertEquals(_buffer_size, stream.read(buffer));
204:                    for (int j = 0; j < buffer.length; j++) {
205:                        assertEquals("in main loop, byte " + offset,
206:                                _workbook_data[offset], buffer[j]);
207:                        offset++;
208:                    }
209:                    assertEquals("offset " + offset, _workbook_size - offset,
210:                            stream.available());
211:                }
212:                assertEquals(_workbook_size % _buffer_size, stream.available());
213:                Arrays.fill(buffer, (byte) 0);
214:                int count = stream.read(buffer);
215:
216:                assertEquals(_workbook_size % _buffer_size, count);
217:                for (int j = 0; j < count; j++) {
218:                    assertEquals("past main loop, byte " + offset,
219:                            _workbook_data[offset], buffer[j]);
220:                    offset++;
221:                }
222:                assertEquals(_workbook_size, offset);
223:                for (int j = count; j < buffer.length; j++) {
224:                    assertEquals("checking remainder, byte " + j, 0, buffer[j]);
225:                }
226:                assertEquals(-1, stream.read(buffer));
227:                stream.close();
228:                try {
229:                    stream.read(buffer);
230:                    fail("Should have caught IOException");
231:                } catch (IOException ignored) {
232:
233:                    // as expected
234:                }
235:            }
236:
237:            /**
238:             * Test complex buffered read
239:             *
240:             * @exception IOException
241:             */
242:
243:            public void testComplexBufferRead() throws IOException {
244:                DocumentInputStream stream = new DocumentInputStream(_workbook);
245:
246:                try {
247:                    stream.read(null, 0, 1);
248:                    fail("Should have caught NullPointerException");
249:                } catch (NullPointerException ignored) {
250:
251:                    // as expected
252:                }
253:
254:                // test illegal offsets and lengths
255:                try {
256:                    stream.read(new byte[5], -4, 0);
257:                    fail("Should have caught IndexOutOfBoundsException");
258:                } catch (IndexOutOfBoundsException ignored) {
259:
260:                    // as expected
261:                }
262:                try {
263:                    stream.read(new byte[5], 0, -4);
264:                    fail("Should have caught IndexOutOfBoundsException");
265:                } catch (IndexOutOfBoundsException ignored) {
266:
267:                    // as expected
268:                }
269:                try {
270:                    stream.read(new byte[5], 0, 6);
271:                    fail("Should have caught IndexOutOfBoundsException");
272:                } catch (IndexOutOfBoundsException ignored) {
273:
274:                    // as expected
275:                }
276:
277:                // test reading zero
278:                assertEquals(0, stream.read(new byte[5], 0, 0));
279:                assertEquals(_workbook_size, stream.available());
280:                byte[] buffer = new byte[_workbook_size];
281:                int offset = 0;
282:
283:                while (stream.available() >= _buffer_size) {
284:                    Arrays.fill(buffer, (byte) 0);
285:                    assertEquals(_buffer_size, stream.read(buffer, offset,
286:                            _buffer_size));
287:                    for (int j = 0; j < offset; j++) {
288:                        assertEquals("checking byte " + j, 0, buffer[j]);
289:                    }
290:                    for (int j = offset; j < (offset + _buffer_size); j++) {
291:                        assertEquals("checking byte " + j, _workbook_data[j],
292:                                buffer[j]);
293:                    }
294:                    for (int j = offset + _buffer_size; j < buffer.length; j++) {
295:                        assertEquals("checking byte " + j, 0, buffer[j]);
296:                    }
297:                    offset += _buffer_size;
298:                    assertEquals("offset " + offset, _workbook_size - offset,
299:                            stream.available());
300:                }
301:                assertEquals(_workbook_size % _buffer_size, stream.available());
302:                Arrays.fill(buffer, (byte) 0);
303:                int count = stream.read(buffer, offset, _workbook_size
304:                        % _buffer_size);
305:
306:                assertEquals(_workbook_size % _buffer_size, count);
307:                for (int j = 0; j < offset; j++) {
308:                    assertEquals("checking byte " + j, 0, buffer[j]);
309:                }
310:                for (int j = offset; j < buffer.length; j++) {
311:                    assertEquals("checking byte " + j, _workbook_data[j],
312:                            buffer[j]);
313:                }
314:                assertEquals(_workbook_size, offset + count);
315:                for (int j = count; j < offset; j++) {
316:                    assertEquals("byte " + j, 0, buffer[j]);
317:                }
318:                assertEquals(-1, stream.read(buffer, 0, 1));
319:                stream.close();
320:                try {
321:                    stream.read(buffer, 0, 1);
322:                    fail("Should have caught IOException");
323:                } catch (IOException ignored) {
324:
325:                    // as expected
326:                }
327:            }
328:
329:            /**
330:             * test skip
331:             *
332:             * @exception IOException
333:             */
334:
335:            public void testSkip() throws IOException {
336:                DocumentInputStream stream = new DocumentInputStream(_workbook);
337:
338:                assertEquals(_workbook_size, stream.available());
339:                int count = stream.available();
340:
341:                while (stream.available() >= _buffer_size) {
342:                    assertEquals(_buffer_size, stream.skip(_buffer_size));
343:                    count -= _buffer_size;
344:                    assertEquals(count, stream.available());
345:                }
346:                assertEquals(_workbook_size % _buffer_size, stream
347:                        .skip(_buffer_size));
348:                assertEquals(0, stream.available());
349:                stream.reset();
350:                assertEquals(_workbook_size, stream.available());
351:                assertEquals(_workbook_size, stream.skip(_workbook_size * 2));
352:                assertEquals(0, stream.available());
353:                stream.reset();
354:                assertEquals(_workbook_size, stream.available());
355:                assertEquals(_workbook_size, stream
356:                        .skip(2 + (long) Integer.MAX_VALUE));
357:                assertEquals(0, stream.available());
358:            }
359:
360:            /**
361:             * main method to run the unit tests
362:             *
363:             * @param ignored_args
364:             */
365:
366:            public static void main(String[] ignored_args) {
367:                System.out
368:                        .println("Testing org.apache.poi.poifs.filesystem.DocumentInputStream");
369:                junit.textui.TestRunner.run(TestDocumentInputStream.class);
370:            }
371:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.