KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.poi.util.LittleEndian;
28 import org.apache.poi.util.LittleEndianConsts;
29
30 /**
31  * Class to test BlockListImpl functionality
32  *
33  * @author Marc Johnson
34  */

35
36 public class TestBlockListImpl
37     extends TestCase
38 {
39
40     /**
41      * Constructor TestBlockListImpl
42      *
43      * @param name
44      */

45
46     public TestBlockListImpl(String JavaDoc name)
47     {
48         super(name);
49     }
50
51     /**
52      * test zap method
53      *
54      * @exception IOException
55      */

56
57     public void testZap()
58         throws IOException
59     {
60         BlockListImpl list = new BlockListImpl();
61
62         // verify that you can zap anything
63
for (int j = -2; j < 10; j++)
64         {
65             list.zap(j);
66         }
67         RawDataBlock[] blocks = new RawDataBlock[ 5 ];
68
69         for (int j = 0; j < 5; j++)
70         {
71             blocks[ j ] =
72                 new RawDataBlock(new ByteArrayInputStream(new byte[ 512 ]));
73         }
74         list.setBlocks(blocks);
75         for (int j = -2; j < 10; j++)
76         {
77             list.zap(j);
78         }
79
80         // verify that all blocks are gone
81
for (int j = 0; j < 5; j++)
82         {
83             try
84             {
85                 list.remove(j);
86                 fail("removing item " + j + " should not have succeeded");
87             }
88             catch (IOException ignored)
89             {
90             }
91         }
92     }
93
94     /**
95      * test remove method
96      *
97      * @exception IOException
98      */

99
100     public void testRemove()
101         throws IOException
102     {
103         BlockListImpl list = new BlockListImpl();
104         RawDataBlock[] blocks = new RawDataBlock[ 5 ];
105         byte[] data = new byte[ 512 * 5 ];
106
107         for (int j = 0; j < 5; j++)
108         {
109             Arrays.fill(data, j * 512, (j * 512) + 512, ( byte ) j);
110         }
111         ByteArrayInputStream stream = new ByteArrayInputStream(data);
112
113         for (int j = 0; j < 5; j++)
114         {
115             blocks[ j ] = new RawDataBlock(stream);
116         }
117         list.setBlocks(blocks);
118
119         // verify that you can't remove illegal indices
120
for (int j = -2; j < 10; j++)
121         {
122             if ((j < 0) || (j >= 5))
123             {
124                 try
125                 {
126                     list.remove(j);
127                     fail("removing item " + j + " should have failed");
128                 }
129                 catch (IOException ignored)
130                 {
131                 }
132             }
133         }
134
135         // verify we can safely and correctly remove all blocks
136
for (int j = 0; j < 5; j++)
137         {
138             byte[] output = list.remove(j).getData();
139
140             for (int k = 0; k < 512; k++)
141             {
142                 assertEquals("testing block " + j + ", index " + k,
143                              data[ (j * 512) + k ], output[ k ]);
144             }
145         }
146
147         // verify that all blocks are gone
148
for (int j = 0; j < 5; j++)
149         {
150             try
151             {
152                 list.remove(j);
153                 fail("removing item " + j + " should not have succeeded");
154             }
155             catch (IOException ignored)
156             {
157             }
158         }
159     }
160
161     /**
162      * test setBAT
163      *
164      * @exception IOException
165      */

166
167     public void testSetBAT()
168         throws IOException
169     {
170         BlockListImpl list = new BlockListImpl();
171
172         list.setBAT(null);
173         list.setBAT(new BlockAllocationTableReader());
174         try
175         {
176             list.setBAT(new BlockAllocationTableReader());
177             fail("second attempt should have failed");
178         }
179         catch (IOException ignored)
180         {
181         }
182     }
183
184     /**
185      * Test fetchBlocks
186      *
187      * @exception IOException
188      */

189
190     public void testFetchBlocks()
191         throws IOException
192     {
193
194         // strategy:
195
//
196
// 1. set up a single BAT block from which to construct a
197
// BAT. create nonsense blocks in the raw data block list
198
// corresponding to the indices in the BAT block.
199
// 2. The indices will include very short documents (0 and 1
200
// block in length), longer documents, and some screwed up
201
// documents (one with a loop, one that will peek into
202
// another document's data, one that includes an unused
203
// document, one that includes a reserved (BAT) block, one
204
// that includes a reserved (XBAT) block, and one that
205
// points off into space somewhere
206
BlockListImpl list = new BlockListImpl();
207         List raw_blocks = new ArrayList();
208         byte[] data = new byte[ 512 ];
209         int offset = 0;
210
211         LittleEndian.putInt(data, offset, -3); // for the BAT block itself
212
offset += LittleEndianConsts.INT_SIZE;
213
214         // document 1: is at end of file already; start block = -2
215
// document 2: has only one block; start block = 1
216
LittleEndian.putInt(data, offset, -2);
217         offset += LittleEndianConsts.INT_SIZE;
218
219         // document 3: has a loop in it; start block = 2
220
LittleEndian.putInt(data, offset, 2);
221         offset += LittleEndianConsts.INT_SIZE;
222
223         // document 4: peeks into document 2's data; start block = 3
224
LittleEndian.putInt(data, offset, 4);
225         offset += LittleEndianConsts.INT_SIZE;
226         LittleEndian.putInt(data, offset, 1);
227         offset += LittleEndianConsts.INT_SIZE;
228
229         // document 5: includes an unused block; start block = 5
230
LittleEndian.putInt(data, offset, 6);
231         offset += LittleEndianConsts.INT_SIZE;
232         LittleEndian.putInt(data, offset, -1);
233         offset += LittleEndianConsts.INT_SIZE;
234
235         // document 6: includes a BAT block; start block = 7
236
LittleEndian.putInt(data, offset, 8);
237         offset += LittleEndianConsts.INT_SIZE;
238         LittleEndian.putInt(data, offset, 0);
239         offset += LittleEndianConsts.INT_SIZE;
240
241         // document 7: includes an XBAT block; start block = 9
242
LittleEndian.putInt(data, offset, 10);
243         offset += LittleEndianConsts.INT_SIZE;
244         LittleEndian.putInt(data, offset, -4);
245         offset += LittleEndianConsts.INT_SIZE;
246
247         // document 8: goes off into space; start block = 11;
248
LittleEndian.putInt(data, offset, 1000);
249         offset += LittleEndianConsts.INT_SIZE;
250
251         // document 9: no screw ups; start block = 12;
252
int index = 13;
253
254         for (; offset < 508; offset += LittleEndianConsts.INT_SIZE)
255         {
256             LittleEndian.putInt(data, offset, index++);
257         }
258         LittleEndian.putInt(data, offset, -2);
259         raw_blocks.add(new RawDataBlock(new ByteArrayInputStream(data)));
260         for (int j = raw_blocks.size(); j < 128; j++)
261         {
262             raw_blocks.add(
263                 new RawDataBlock(new ByteArrayInputStream(new byte[ 0 ])));
264         }
265         list.setBlocks(( RawDataBlock [] ) raw_blocks
266             .toArray(new RawDataBlock[ 0 ]));
267         int[] blocks =
268         {
269             0
270         };
271         BlockAllocationTableReader table =
272             new BlockAllocationTableReader(1, blocks, 0, -2, list);
273         int[] start_blocks =
274         {
275             -2, 1, 2, 3, 5, 7, 9, 11, 12
276         };
277         int[] expected_length =
278         {
279             0, 1, -1, -1, -1, -1, -1, -1, 116
280         };
281
282         for (int j = 0; j < start_blocks.length; j++)
283         {
284             try
285             {
286                 ListManagedBlock[] dataBlocks =
287                     list.fetchBlocks(start_blocks[ j ]);
288
289                 if (expected_length[ j ] == -1)
290                 {
291                     fail("document " + j + " should have failed");
292                 }
293                 else
294                 {
295                     assertEquals(expected_length[ j ], dataBlocks.length);
296                 }
297             }
298             catch (IOException e)
299             {
300                 if (expected_length[ j ] == -1)
301                 {
302
303                     // no problem, we expected a failure here
304
}
305                 else
306                 {
307                     throw e;
308                 }
309             }
310         }
311     }
312
313     /**
314      * main method to run the unit tests
315      *
316      * @param ignored_args
317      */

318
319     public static void main(String JavaDoc [] ignored_args)
320     {
321         System.out
322             .println("Testing org.apache.poi.poifs.storage.BlockListImpl");
323         junit.textui.TestRunner.run(TestBlockListImpl.class);
324     }
325 }
326
Popular Tags