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.IOException;
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.poi.util.IOUtils;
027: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
028: import org.apache.poi.poifs.filesystem.POIFSWriterEvent;
029: import org.apache.poi.poifs.filesystem.POIFSWriterListener;
030: import org.apache.poi.poifs.filesystem.DirectoryEntry;
031:
032: public class TestEmptyDocument extends TestCase {
033:
034: public static void main(String[] args) {
035: TestEmptyDocument driver = new TestEmptyDocument();
036:
037: System.out.println();
038: System.out.println("As only file...");
039: System.out.println();
040:
041: System.out
042: .print("Trying using createDocument(String,InputStream): ");
043: try {
044: driver.testSingleEmptyDocument();
045: System.out.println("Worked!");
046: } catch (IOException exception) {
047: System.out.println("failed! ");
048: System.out.println(exception.toString());
049: }
050: System.out.println();
051:
052: System.out
053: .print("Trying using createDocument(String,int,POIFSWriterListener): ");
054: try {
055: driver.testSingleEmptyDocumentEvent();
056: System.out.println("Worked!");
057: } catch (IOException exception) {
058: System.out.println("failed!");
059: System.out.println(exception.toString());
060: }
061: System.out.println();
062:
063: System.out.println();
064: System.out.println("After another file...");
065: System.out.println();
066:
067: System.out
068: .print("Trying using createDocument(String,InputStream): ");
069: try {
070: driver.testEmptyDocumentWithFriend();
071: System.out.println("Worked!");
072: } catch (IOException exception) {
073: System.out.println("failed! ");
074: System.out.println(exception.toString());
075: }
076: System.out.println();
077:
078: System.out
079: .print("Trying using createDocument(String,int,POIFSWriterListener): ");
080: try {
081: driver.testEmptyDocumentWithFriend();
082: System.out.println("Worked!");
083: } catch (IOException exception) {
084: System.out.println("failed!");
085: System.out.println(exception.toString());
086: }
087: System.out.println();
088: }
089:
090: public void testSingleEmptyDocument() throws IOException {
091: POIFSFileSystem fs = new POIFSFileSystem();
092: DirectoryEntry dir = fs.getRoot();
093: dir.createDocument("Foo", new ByteArrayInputStream(
094: new byte[] {}));
095:
096: ByteArrayOutputStream out = new ByteArrayOutputStream();
097: fs.writeFilesystem(out);
098: new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
099: }
100:
101: public void testSingleEmptyDocumentEvent() throws IOException {
102: POIFSFileSystem fs = new POIFSFileSystem();
103: DirectoryEntry dir = fs.getRoot();
104: dir.createDocument("Foo", 0, new POIFSWriterListener() {
105: public void processPOIFSWriterEvent(POIFSWriterEvent event) {
106: System.out.println("written");
107: }
108: });
109:
110: ByteArrayOutputStream out = new ByteArrayOutputStream();
111: fs.writeFilesystem(out);
112: new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
113: }
114:
115: public void testEmptyDocumentWithFriend() throws IOException {
116: POIFSFileSystem fs = new POIFSFileSystem();
117: DirectoryEntry dir = fs.getRoot();
118: dir.createDocument("Bar", new ByteArrayInputStream(
119: new byte[] { 0 }));
120: dir.createDocument("Foo", new ByteArrayInputStream(
121: new byte[] {}));
122:
123: ByteArrayOutputStream out = new ByteArrayOutputStream();
124: fs.writeFilesystem(out);
125: new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
126: }
127:
128: public void testEmptyDocumentEventWithFriend() throws IOException {
129: POIFSFileSystem fs = new POIFSFileSystem();
130: DirectoryEntry dir = fs.getRoot();
131: dir.createDocument("Bar", 1, new POIFSWriterListener() {
132: public void processPOIFSWriterEvent(POIFSWriterEvent event) {
133: try {
134: event.getStream().write(0);
135: } catch (IOException exception) {
136: throw new RuntimeException("exception on write: "
137: + exception);
138: }
139: }
140: });
141: dir.createDocument("Foo", 0, new POIFSWriterListener() {
142: public void processPOIFSWriterEvent(POIFSWriterEvent event) {
143: }
144: });
145:
146: ByteArrayOutputStream out = new ByteArrayOutputStream();
147: fs.writeFilesystem(out);
148: new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
149: }
150:
151: public void testEmptyDocumentBug11744() throws Exception {
152: byte[] testData = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
153:
154: POIFSFileSystem fs = new POIFSFileSystem();
155: fs.createDocument(new ByteArrayInputStream(new byte[0]),
156: "Empty");
157: fs.createDocument(new ByteArrayInputStream(testData),
158: "NotEmpty");
159: ByteArrayOutputStream out = new ByteArrayOutputStream();
160: fs.writeFilesystem(out);
161: out.toByteArray();
162:
163: // This line caused the error.
164: fs = new POIFSFileSystem(new ByteArrayInputStream(out
165: .toByteArray()));
166:
167: DocumentEntry entry = (DocumentEntry) fs.getRoot().getEntry(
168: "Empty");
169: assertEquals("Expected zero size", 0, entry.getSize());
170: assertEquals("Expected zero read from stream", 0, IOUtils
171: .toByteArray(new DocumentInputStream(entry)).length);
172:
173: entry = (DocumentEntry) fs.getRoot().getEntry("NotEmpty");
174: assertEquals("Expected size was wrong", testData.length, entry
175: .getSize());
176: assertEquals("Expected different data read from stream",
177: testData, IOUtils.toByteArray(new DocumentInputStream(
178: entry)));
179: }
180: }
|