KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > storage > TestSmallDocumentBlock


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.storage;
20
21 import java.io.*;
22
23 import java.util.*;
24
25 import junit.framework.*;
26
27 /**
28  * Class to test SmallDocumentBlock functionality
29  *
30  * @author Marc Johnson
31  */

32
33 public class TestSmallDocumentBlock
34     extends TestCase
35 {
36     static final private byte[] _testdata;
37     static final private int _testdata_size = 2999;
38
39     static
40     {
41         _testdata = new byte[ _testdata_size ];
42         for (int j = 0; j < _testdata.length; j++)
43         {
44             _testdata[ j ] = ( byte ) j;
45         }
46     }
47     ;
48
49     /**
50      * constructor
51      *
52      * @param name
53      */

54
55     public TestSmallDocumentBlock(String JavaDoc name)
56     {
57         super(name);
58     }
59
60     /**
61      * Test conversion from DocumentBlocks
62      *
63      * @exception IOException
64      */

65
66     public void testConvert1()
67         throws IOException
68     {
69         ByteArrayInputStream stream = new ByteArrayInputStream(_testdata);
70         List documents = new ArrayList();
71
72         while (true)
73         {
74             DocumentBlock block = new DocumentBlock(stream);
75
76             documents.add(block);
77             if (block.partiallyRead())
78             {
79                 break;
80             }
81         }
82         SmallDocumentBlock[] results =
83             SmallDocumentBlock
84                 .convert(( BlockWritable [] ) documents
85                     .toArray(new DocumentBlock[ 0 ]), _testdata_size);
86
87         assertEquals("checking correct result size: ",
88                      (_testdata_size + 63) / 64, results.length);
89         ByteArrayOutputStream output = new ByteArrayOutputStream();
90
91         for (int j = 0; j < results.length; j++)
92         {
93             results[ j ].writeBlocks(output);
94         }
95         byte[] output_array = output.toByteArray();
96
97         assertEquals("checking correct output size: ", 64 * results.length,
98                      output_array.length);
99         int index = 0;
100
101         for (; index < _testdata_size; index++)
102         {
103             assertEquals("checking output " + index, _testdata[ index ],
104                          output_array[ index ]);
105         }
106         for (; index < output_array.length; index++)
107         {
108             assertEquals("checking output " + index, ( byte ) 0xff,
109                          output_array[ index ]);
110         }
111     }
112
113     /**
114      * Test conversion from byte array
115      *
116      * @exception IOException;
117      *
118      * @exception IOException
119      */

120
121     public void testConvert2()
122         throws IOException
123     {
124         for (int j = 0; j < 320; j++)
125         {
126             byte[] array = new byte[ j ];
127
128             for (int k = 0; k < j; k++)
129             {
130                 array[ k ] = ( byte ) k;
131             }
132             SmallDocumentBlock[] blocks = SmallDocumentBlock.convert(array,
133                                               319);
134
135             assertEquals(5, blocks.length);
136             ByteArrayOutputStream stream = new ByteArrayOutputStream();
137
138             for (int k = 0; k < blocks.length; k++)
139             {
140                 blocks[ k ].writeBlocks(stream);
141             }
142             stream.close();
143             byte[] output = stream.toByteArray();
144
145             for (int k = 0; k < array.length; k++)
146             {
147                 assertEquals(String.valueOf(k), array[ k ], output[ k ]);
148             }
149             for (int k = array.length; k < 320; k++)
150             {
151                 assertEquals(String.valueOf(k), ( byte ) 0xFF, output[ k ]);
152             }
153         }
154     }
155
156     /**
157      * Test read method
158      *
159      * @exception IOException
160      */

161
162     public void testRead()
163         throws IOException
164     {
165         ByteArrayInputStream stream = new ByteArrayInputStream(_testdata);
166         List documents = new ArrayList();
167
168         while (true)
169         {
170             DocumentBlock block = new DocumentBlock(stream);
171
172             documents.add(block);
173             if (block.partiallyRead())
174             {
175                 break;
176             }
177         }
178         SmallDocumentBlock[] blocks =
179             SmallDocumentBlock
180                 .convert(( BlockWritable [] ) documents
181                     .toArray(new DocumentBlock[ 0 ]), _testdata_size);
182
183         for (int j = 1; j <= _testdata_size; j += 38)
184         {
185             byte[] buffer = new byte[ j ];
186             int offset = 0;
187
188             for (int k = 0; k < (_testdata_size / j); k++)
189             {
190                 SmallDocumentBlock.read(blocks, buffer, offset);
191                 for (int n = 0; n < buffer.length; n++)
192                 {
193                     assertEquals("checking byte " + (k * j) + n,
194                                  _testdata[ (k * j) + n ], buffer[ n ]);
195                 }
196                 offset += j;
197             }
198         }
199     }
200
201     /**
202      * test fill
203      *
204      * @exception IOException
205      */

206
207     public void testFill()
208         throws IOException
209     {
210         for (int j = 0; j <= 8; j++)
211         {
212             List foo = new ArrayList();
213
214             for (int k = 0; k < j; k++)
215             {
216                 foo.add(new Object JavaDoc());
217             }
218             int result = SmallDocumentBlock.fill(foo);
219
220             assertEquals("correct big block count: ", (j + 7) / 8, result);
221             assertEquals("correct small block count: ", 8 * result,
222                          foo.size());
223             for (int m = j; m < foo.size(); m++)
224             {
225                 BlockWritable block = ( BlockWritable ) foo.get(m);
226                 ByteArrayOutputStream stream = new ByteArrayOutputStream();
227
228                 block.writeBlocks(stream);
229                 byte[] output = stream.toByteArray();
230
231                 assertEquals("correct output size (block[ " + m + " ]): ",
232                              64, output.length);
233                 for (int n = 0; n < 64; n++)
234                 {
235                     assertEquals("correct value (block[ " + m + " ][ " + n
236                                  + " ]): ", ( byte ) 0xff, output[ n ]);
237                 }
238             }
239         }
240     }
241
242     /**
243      * test calcSize
244      */

245
246     public void testCalcSize()
247     {
248         for (int j = 0; j < 10; j++)
249         {
250             assertEquals("testing " + j, j * 64,
251                          SmallDocumentBlock.calcSize(j));
252         }
253     }
254
255     /**
256      * test extract method
257      *
258      * @exception IOException
259      */

260
261     public void testExtract()
262         throws IOException
263     {
264         byte[] data = new byte[ 512 ];
265         int offset = 0;
266
267         for (int j = 0; j < 8; j++)
268         {
269             for (int k = 0; k < 64; k++)
270             {
271                 data[ offset++ ] = ( byte ) (k + j);
272             }
273         }
274         RawDataBlock[] blocks =
275         {
276             new RawDataBlock(new ByteArrayInputStream(data))
277         };
278         List output = SmallDocumentBlock.extract(blocks);
279         Iterator iter = output.iterator();
280
281         offset = 0;
282         while (iter.hasNext())
283         {
284             byte[] out_data = (( SmallDocumentBlock ) iter.next()).getData();
285
286             assertEquals("testing block at offset " + offset, 64,
287                          out_data.length);
288             for (int j = 0; j < out_data.length; j++)
289             {
290                 assertEquals("testing byte at offset " + offset,
291                              data[ offset ], out_data[ j ]);
292                 offset++;
293             }
294         }
295     }
296
297     /**
298      * main method to run the unit tests
299      *
300      * @param ignored_args
301      */

302
303     public static void main(String JavaDoc [] ignored_args)
304     {
305         System.out.println(
306             "Testing org.apache.poi.poifs.storage.SmallDocumentBlock");
307         junit.textui.TestRunner.run(TestSmallDocumentBlock.class);
308     }
309 }
310
Popular Tags