KickJava   Java API By Example, From Geeks To Geeks.

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


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

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.util.LittleEndian;
28 import org.apache.poi.util.LittleEndianConsts;
29 import org.apache.poi.poifs.property.DocumentProperty;
30 import org.apache.poi.poifs.storage.RawDataBlock;
31 import org.apache.poi.poifs.storage.SmallDocumentBlock;
32
33 /**
34  * Class to test POIFSDocument functionality
35  *
36  * @author Marc Johnson
37  */

38
39 public class TestDocument
40     extends TestCase
41 {
42
43     /**
44      * Constructor TestDocument
45      *
46      * @param name
47      */

48
49     public TestDocument(String JavaDoc name)
50     {
51         super(name);
52     }
53
54     /**
55      * Integration test -- really about all we can do
56      *
57      * @exception IOException
58      */

59
60     public void testPOIFSDocument()
61         throws IOException
62     {
63
64         // verify correct number of blocks get created for document
65
// that is exact multituple of block size
66
POIFSDocument document;
67         byte[] array = new byte[ 4096 ];
68
69         for (int j = 0; j < array.length; j++)
70         {
71             array[ j ] = ( byte ) j;
72         }
73         document = new POIFSDocument("foo", new SlowInputStream(new ByteArrayInputStream(array)));
74         checkDocument(document, array);
75
76         // verify correct number of blocks get created for document
77
// that is not an exact multiple of block size
78
array = new byte[ 4097 ];
79         for (int j = 0; j < array.length; j++)
80         {
81             array[ j ] = ( byte ) j;
82         }
83         document = new POIFSDocument("bar", new ByteArrayInputStream(array));
84         checkDocument(document, array);
85
86         // verify correct number of blocks get created for document
87
// that is small
88
array = new byte[ 4095 ];
89         for (int j = 0; j < array.length; j++)
90         {
91             array[ j ] = ( byte ) j;
92         }
93         document = new POIFSDocument("_bar", new ByteArrayInputStream(array));
94         checkDocument(document, array);
95
96         // verify correct number of blocks get created for document
97
// that is rather small
98
array = new byte[ 199 ];
99         for (int j = 0; j < array.length; j++)
100         {
101             array[ j ] = ( byte ) j;
102         }
103         document = new POIFSDocument("_bar2",
104                                      new ByteArrayInputStream(array));
105         checkDocument(document, array);
106
107         // verify that output is correct
108
array = new byte[ 4097 ];
109         for (int j = 0; j < array.length; j++)
110         {
111             array[ j ] = ( byte ) j;
112         }
113         document = new POIFSDocument("foobar",
114                                      new ByteArrayInputStream(array));
115         checkDocument(document, array);
116         document.setStartBlock(0x12345678); // what a big file!!
117
DocumentProperty property = document.getDocumentProperty();
118         ByteArrayOutputStream stream = new ByteArrayOutputStream();
119
120         property.writeData(stream);
121         byte[] output = stream.toByteArray();
122         byte[] array2 =
123         {
124             ( byte ) 'f', ( byte ) 0, ( byte ) 'o', ( byte ) 0, ( byte ) 'o',
125             ( byte ) 0, ( byte ) 'b', ( byte ) 0, ( byte ) 'a', ( byte ) 0,
126             ( byte ) 'r', ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
127             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
128             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
129             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
130             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
131             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
132             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
133             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
134             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
135             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
136             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 14,
137             ( byte ) 0, ( byte ) 2, ( byte ) 1, ( byte ) -1, ( byte ) -1,
138             ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1,
139             ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1,
140             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
141             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
142             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
143             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
144             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
145             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
146             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
147             ( byte ) 0, ( byte ) 0x78, ( byte ) 0x56, ( byte ) 0x34,
148             ( byte ) 0x12, ( byte ) 1, ( byte ) 16, ( byte ) 0, ( byte ) 0,
149             ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0
150         };
151
152         assertEquals(array2.length, output.length);
153         for (int j = 0; j < output.length; j++)
154         {
155             assertEquals("Checking property offset " + j, array2[ j ],
156                          output[ j ]);
157         }
158     }
159
160     private POIFSDocument makeCopy(POIFSDocument document, byte [] input,
161                                    byte [] data)
162         throws IOException
163     {
164         POIFSDocument copy = null;
165
166         if (input.length >= 4096)
167         {
168             RawDataBlock[] blocks =
169                 new RawDataBlock[ (input.length + 511) / 512 ];
170             ByteArrayInputStream stream = new ByteArrayInputStream(data);
171             int index = 0;
172
173             while (true)
174             {
175                 RawDataBlock block = new RawDataBlock(stream);
176
177                 if (block.eof())
178                 {
179                     break;
180                 }
181                 blocks[ index++ ] = block;
182             }
183             copy = new POIFSDocument("test" + input.length, blocks,
184                                      input.length);
185         }
186         else
187         {
188             copy = new POIFSDocument(
189                 "test" + input.length,
190                 ( SmallDocumentBlock [] ) document.getSmallBlocks(),
191                 input.length);
192         }
193         return copy;
194     }
195
196     private void checkDocument(final POIFSDocument document,
197                                final byte [] input)
198         throws IOException
199     {
200         int big_blocks = 0;
201         int small_blocks = 0;
202         int total_output = 0;
203
204         if (input.length >= 4096)
205         {
206             big_blocks = (input.length + 511) / 512;
207             total_output = big_blocks * 512;
208         }
209         else
210         {
211             small_blocks = (input.length + 63) / 64;
212             total_output = 0;
213         }
214         checkValues(
215             big_blocks, small_blocks, total_output,
216             makeCopy(
217             document, input,
218             checkValues(
219                 big_blocks, small_blocks, total_output, document,
220                 input)), input);
221     }
222
223     private byte [] checkValues(int big_blocks, int small_blocks,
224                                 int total_output, POIFSDocument document,
225                                 byte [] input)
226         throws IOException
227     {
228         assertEquals(document, document.getDocumentProperty().getDocument());
229         int increment = ( int ) Math.sqrt(input.length);
230
231         for (int j = 1; j <= input.length; j += increment)
232         {
233             byte[] buffer = new byte[ j ];
234             int offset = 0;
235
236             for (int k = 0; k < (input.length / j); k++)
237             {
238                 document.read(buffer, offset);
239                 for (int n = 0; n < buffer.length; n++)
240                 {
241                     assertEquals("checking byte " + (k * j) + n,
242                                  input[ (k * j) + n ], buffer[ n ]);
243                 }
244                 offset += j;
245             }
246         }
247         assertEquals(big_blocks, document.countBlocks());
248         assertEquals(small_blocks, document.getSmallBlocks().length);
249         ByteArrayOutputStream stream = new ByteArrayOutputStream();
250
251         document.writeBlocks(stream);
252         byte[] output = stream.toByteArray();
253
254         assertEquals(total_output, output.length);
255         int limit = Math.min(total_output, input.length);
256
257         for (int j = 0; j < limit; j++)
258         {
259             assertEquals("Checking document offset " + j, input[ j ],
260                          output[ j ]);
261         }
262         for (int j = limit; j < output.length; j++)
263         {
264             assertEquals("Checking document offset " + j, ( byte ) -1,
265                          output[ j ]);
266         }
267         return output;
268     }
269
270     /**
271      * main method to run the unit tests
272      *
273      * @param ignored_args
274      */

275
276     public static void main(String JavaDoc [] ignored_args)
277     {
278         System.out
279             .println("Testing org.apache.poi.poifs.filesystem.POIFSDocument");
280         junit.textui.TestRunner.run(TestDocument.class);
281     }
282 }
283
Popular Tags