1 16 17 package org.apache.poi.poifs.filesystem; 18 19 import java.io.IOException ; 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 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 [] 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 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 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 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 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 { 87 POIFSFileSystem fs = new POIFSFileSystem(); 88 DirectoryEntry dir = fs.getRoot(); 89 dir = fs.getRoot(); 90 dir.createDocument("Foo", new ByteArrayInputStream (new byte[] { })); 91 92 ByteArrayOutputStream out = new ByteArrayOutputStream (); 93 fs.writeFilesystem(out); 94 new POIFSFileSystem(new ByteArrayInputStream (out.toByteArray())); 95 } 96 97 public void testSingleEmptyDocumentEvent() throws IOException { 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 out = new ByteArrayOutputStream (); 108 fs.writeFilesystem(out); 109 new POIFSFileSystem(new ByteArrayInputStream (out.toByteArray())); 110 } 111 112 public void testEmptyDocumentWithFriend() throws IOException { 113 POIFSFileSystem fs = new POIFSFileSystem(); 114 DirectoryEntry dir = fs.getRoot(); 115 dir = fs.getRoot(); 116 dir.createDocument("Bar", new ByteArrayInputStream (new byte[] { 0 })); 117 dir.createDocument("Foo", new ByteArrayInputStream (new byte[] { })); 118 119 ByteArrayOutputStream out = new ByteArrayOutputStream (); 120 fs.writeFilesystem(out); 121 new POIFSFileSystem(new ByteArrayInputStream (out.toByteArray())); 122 } 123 124 public void testEmptyDocumentEventWithFriend() throws IOException { 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 exception) { 133 throw new RuntimeException ("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 out = new ByteArrayOutputStream (); 143 fs.writeFilesystem(out); 144 new POIFSFileSystem(new ByteArrayInputStream (out.toByteArray())); 145 } 146 } 147 | Popular Tags |