1 2 17 18 19 package org.apache.poi.poifs.filesystem; 20 21 import java.io.*; 22 23 import java.util.*; 24 25 import junit.framework.*; 26 27 import org.apache.poi.poifs.property.DirectoryProperty; 28 import org.apache.poi.poifs.property.DocumentProperty; 29 import org.apache.poi.poifs.storage.RawDataBlock; 30 31 36 37 public class TestDocumentOutputStream 38 extends TestCase 39 { 40 41 48 49 public TestDocumentOutputStream(String name) 50 throws IOException 51 { 52 super(name); 53 } 54 55 60 61 public void testWrite1() 62 throws IOException 63 { 64 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 65 DocumentOutputStream dstream = new DocumentOutputStream(stream, 25); 66 67 for (int j = 0; j < 25; j++) 68 { 69 dstream.write(j); 70 } 71 try 72 { 73 dstream.write(0); 74 fail("Should have caught IOException"); 75 } 76 catch (IOException ignored) 77 { 78 } 79 byte[] output = stream.toByteArray(); 80 81 assertEquals(25, output.length); 82 for (int j = 0; j < 25; j++) 83 { 84 assertEquals(( byte ) j, output[ j ]); 85 } 86 stream.close(); 87 } 88 89 94 95 public void testWrite2() 96 throws IOException 97 { 98 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 99 DocumentOutputStream dstream = new DocumentOutputStream(stream, 25); 100 101 for (int j = 0; j < 6; j++) 102 { 103 byte[] array = new byte[ 4 ]; 104 105 Arrays.fill(array, ( byte ) j); 106 dstream.write(array); 107 } 108 try 109 { 110 byte[] array = new byte[ 4 ]; 111 112 Arrays.fill(array, ( byte ) 7); 113 dstream.write(array); 114 fail("Should have caught IOException"); 115 } 116 catch (IOException ignored) 117 { 118 } 119 byte[] output = stream.toByteArray(); 120 121 assertEquals(24, output.length); 122 for (int j = 0; j < 6; j++) 123 { 124 for (int k = 0; k < 4; k++) 125 { 126 assertEquals(String.valueOf((j * 4) + k), ( byte ) j, 127 output[ (j * 4) + k ]); 128 } 129 } 130 stream.close(); 131 } 132 133 138 139 public void testWrite3() 140 throws IOException 141 { 142 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 143 DocumentOutputStream dstream = new DocumentOutputStream(stream, 25); 144 byte[] array = new byte[ 50 ]; 145 146 for (int j = 0; j < 50; j++) 147 { 148 array[ j ] = ( byte ) j; 149 } 150 dstream.write(array, 1, 25); 151 try 152 { 153 dstream.write(array, 0, 1); 154 fail("Should have caught IOException"); 155 } 156 catch (IOException ignored) 157 { 158 } 159 byte[] output = stream.toByteArray(); 160 161 assertEquals(25, output.length); 162 for (int j = 0; j < 25; j++) 163 { 164 assertEquals(( byte ) (j + 1), output[ j ]); 165 } 166 stream.close(); 167 } 168 169 174 175 public void testWriteFiller() 176 throws IOException 177 { 178 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 179 DocumentOutputStream dstream = new DocumentOutputStream(stream, 25); 180 181 for (int j = 0; j < 25; j++) 182 { 183 dstream.write(j); 184 } 185 try 186 { 187 dstream.write(0); 188 fail("Should have caught IOException"); 189 } 190 catch (IOException ignored) 191 { 192 } 193 dstream.writeFiller(100, ( byte ) 0xff); 194 byte[] output = stream.toByteArray(); 195 196 assertEquals(100, output.length); 197 for (int j = 0; j < 25; j++) 198 { 199 assertEquals(( byte ) j, output[ j ]); 200 } 201 for (int j = 25; j < 100; j++) 202 { 203 assertEquals(String.valueOf(j), ( byte ) 0xff, output[ j ]); 204 } 205 stream.close(); 206 } 207 208 213 214 public static void main(String [] ignored_args) 215 { 216 System.out.println( 217 "Testing org.apache.poi.poifs.filesystem.DocumentOutputStream"); 218 junit.textui.TestRunner.run(TestDocumentOutputStream.class); 219 } 220 } 221 | Popular Tags |