KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > filesystem > TestEmptyDocument


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 package org.apache.poi.poifs.filesystem;
18
19 import java.io.IOException JavaDoc;
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
26 import org.apache.poi.poifs.filesystem.POIFSWriterEvent;
27 import org.apache.poi.poifs.filesystem.POIFSWriterListener;
28 import org.apache.poi.poifs.filesystem.DirectoryEntry;
29
30 public class TestEmptyDocument extends TestCase {
31
32   public static void main(String JavaDoc[] args) {
33     TestEmptyDocument driver = new TestEmptyDocument();
34
35     System.out.println();
36     System.out.println("As only file...");
37     System.out.println();
38
39     System.out.print("Trying using createDocument(String,InputStream): ");
40     try {
41       driver.testSingleEmptyDocument();
42       System.out.println("Worked!");
43     } catch (IOException JavaDoc exception) {
44       System.out.println("failed! ");
45       System.out.println(exception.toString());
46     }
47     System.out.println();
48
49     System.out.print
50       ("Trying using createDocument(String,int,POIFSWriterListener): ");
51     try {
52       driver.testSingleEmptyDocumentEvent();
53       System.out.println("Worked!");
54     } catch (IOException JavaDoc exception) {
55       System.out.println("failed!");
56       System.out.println(exception.toString());
57     }
58     System.out.println();
59
60     System.out.println();
61     System.out.println("After another file...");
62     System.out.println();
63
64     System.out.print("Trying using createDocument(String,InputStream): ");
65     try {
66       driver.testEmptyDocumentWithFriend();
67       System.out.println("Worked!");
68     } catch (IOException JavaDoc exception) {
69       System.out.println("failed! ");
70       System.out.println(exception.toString());
71     }
72     System.out.println();
73
74     System.out.print
75       ("Trying using createDocument(String,int,POIFSWriterListener): ");
76     try {
77       driver.testEmptyDocumentWithFriend();
78       System.out.println("Worked!");
79     } catch (IOException JavaDoc exception) {
80       System.out.println("failed!");
81       System.out.println(exception.toString());
82     }
83     System.out.println();
84   }
85
86   public void testSingleEmptyDocument() throws IOException JavaDoc {
87     POIFSFileSystem fs = new POIFSFileSystem();
88     DirectoryEntry dir = fs.getRoot();
89     dir = fs.getRoot();
90     dir.createDocument("Foo", new ByteArrayInputStream JavaDoc(new byte[] { }));
91     
92     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
93     fs.writeFilesystem(out);
94     new POIFSFileSystem(new ByteArrayInputStream JavaDoc(out.toByteArray()));
95   }
96
97   public void testSingleEmptyDocumentEvent() throws IOException JavaDoc {
98     POIFSFileSystem fs = new POIFSFileSystem();
99     DirectoryEntry dir = fs.getRoot();
100     dir = fs.getRoot();
101     dir.createDocument("Foo", 0, new POIFSWriterListener() {
102       public void processPOIFSWriterEvent(POIFSWriterEvent event) {
103         System.out.println("written");
104       }
105     });
106     
107     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
108     fs.writeFilesystem(out);
109     new POIFSFileSystem(new ByteArrayInputStream JavaDoc(out.toByteArray()));
110   }
111
112   public void testEmptyDocumentWithFriend() throws IOException JavaDoc {
113     POIFSFileSystem fs = new POIFSFileSystem();
114     DirectoryEntry dir = fs.getRoot();
115     dir = fs.getRoot();
116     dir.createDocument("Bar", new ByteArrayInputStream JavaDoc(new byte[] { 0 }));
117     dir.createDocument("Foo", new ByteArrayInputStream JavaDoc(new byte[] { }));
118     
119     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
120     fs.writeFilesystem(out);
121     new POIFSFileSystem(new ByteArrayInputStream JavaDoc(out.toByteArray()));
122   }
123
124   public void testEmptyDocumentEventWithFriend() throws IOException JavaDoc {
125     POIFSFileSystem fs = new POIFSFileSystem();
126     DirectoryEntry dir = fs.getRoot();
127     dir = fs.getRoot();
128     dir.createDocument("Bar", 1, new POIFSWriterListener() {
129       public void processPOIFSWriterEvent(POIFSWriterEvent event) {
130         try {
131           event.getStream().write(0);
132         } catch (IOException JavaDoc exception) {
133           throw new RuntimeException JavaDoc("exception on write: " + exception);
134         }
135       }
136     });
137     dir.createDocument("Foo", 0, new POIFSWriterListener() {
138       public void processPOIFSWriterEvent(POIFSWriterEvent event) {
139       }
140     });
141     
142     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
143     fs.writeFilesystem(out);
144     new POIFSFileSystem(new ByteArrayInputStream JavaDoc(out.toByteArray()));
145   }
146 }
147
Popular Tags