KickJava   Java API By Example, From Geeks To Geeks.

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


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 BATBlock functionality
29  *
30  * @author Marc Johnson
31  */

32
33 public class TestBATBlock
34     extends TestCase
35 {
36
37     /**
38      * Constructor TestBATBlock
39      *
40      * @param name
41      */

42
43     public TestBATBlock(String JavaDoc name)
44     {
45         super(name);
46     }
47
48     /**
49      * Test the createBATBlocks method. The test involves setting up
50      * various arrays of int's and ensuring that the correct number of
51      * BATBlocks is created for each array, and that the data from
52      * each array is correctly written to the BATBlocks.
53      *
54      * @exception IOException
55      */

56
57     public void testCreateBATBlocks()
58         throws IOException
59     {
60
61         // test 0 length array (basic sanity)
62
BATBlock[] rvalue = BATBlock.createBATBlocks(createTestArray(0));
63
64         assertEquals(0, rvalue.length);
65
66         // test array of length 1
67
rvalue = BATBlock.createBATBlocks(createTestArray(1));
68         assertEquals(1, rvalue.length);
69         verifyContents(rvalue, 1);
70
71         // test array of length 127
72
rvalue = BATBlock.createBATBlocks(createTestArray(127));
73         assertEquals(1, rvalue.length);
74         verifyContents(rvalue, 127);
75
76         // test array of length 128
77
rvalue = BATBlock.createBATBlocks(createTestArray(128));
78         assertEquals(1, rvalue.length);
79         verifyContents(rvalue, 128);
80
81         // test array of length 129
82
rvalue = BATBlock.createBATBlocks(createTestArray(129));
83         assertEquals(2, rvalue.length);
84         verifyContents(rvalue, 129);
85     }
86
87     private int [] createTestArray(int count)
88     {
89         int[] rvalue = new int[ count ];
90
91         for (int j = 0; j < count; j++)
92         {
93             rvalue[ j ] = j;
94         }
95         return rvalue;
96     }
97
98     private void verifyContents(BATBlock [] blocks, int entries)
99         throws IOException
100     {
101         byte[] expected = new byte[ 512 * blocks.length ];
102
103         Arrays.fill(expected, ( byte ) 0xFF);
104         int offset = 0;
105
106         for (int j = 0; j < entries; j++)
107         {
108             expected[ offset++ ] = ( byte ) j;
109             expected[ offset++ ] = 0;
110             expected[ offset++ ] = 0;
111             expected[ offset++ ] = 0;
112         }
113         ByteArrayOutputStream stream = new ByteArrayOutputStream(512
114                                            * blocks.length);
115
116         for (int j = 0; j < blocks.length; j++)
117         {
118             blocks[ j ].writeBlocks(stream);
119         }
120         byte[] actual = stream.toByteArray();
121
122         assertEquals(expected.length, actual.length);
123         for (int j = 0; j < expected.length; j++)
124         {
125             assertEquals(expected[ j ], actual[ j ]);
126         }
127     }
128
129     /**
130      * test createXBATBlocks
131      *
132      * @exception IOException
133      */

134
135     public void testCreateXBATBlocks()
136         throws IOException
137     {
138
139         // test 0 length array (basic sanity)
140
BATBlock[] rvalue = BATBlock.createXBATBlocks(createTestArray(0), 1);
141
142         assertEquals(0, rvalue.length);
143
144         // test array of length 1
145
rvalue = BATBlock.createXBATBlocks(createTestArray(1), 1);
146         assertEquals(1, rvalue.length);
147         verifyXBATContents(rvalue, 1, 1);
148
149         // test array of length 127
150
rvalue = BATBlock.createXBATBlocks(createTestArray(127), 1);
151         assertEquals(1, rvalue.length);
152         verifyXBATContents(rvalue, 127, 1);
153
154         // test array of length 128
155
rvalue = BATBlock.createXBATBlocks(createTestArray(128), 1);
156         assertEquals(2, rvalue.length);
157         verifyXBATContents(rvalue, 128, 1);
158
159         // test array of length 254
160
rvalue = BATBlock.createXBATBlocks(createTestArray(254), 1);
161         assertEquals(2, rvalue.length);
162         verifyXBATContents(rvalue, 254, 1);
163
164         // test array of length 255
165
rvalue = BATBlock.createXBATBlocks(createTestArray(255), 1);
166         assertEquals(3, rvalue.length);
167         verifyXBATContents(rvalue, 255, 1);
168     }
169
170     private void verifyXBATContents(BATBlock [] blocks, int entries,
171                                     int start_block)
172         throws IOException
173     {
174         byte[] expected = new byte[ 512 * blocks.length ];
175
176         Arrays.fill(expected, ( byte ) 0xFF);
177         int offset = 0;
178
179         for (int j = 0; j < entries; j++)
180         {
181             if ((j % 127) == 0)
182             {
183                 if (j != 0)
184                 {
185                     offset += 4;
186                 }
187             }
188             expected[ offset++ ] = ( byte ) j;
189             expected[ offset++ ] = 0;
190             expected[ offset++ ] = 0;
191             expected[ offset++ ] = 0;
192         }
193         for (int j = 0; j < (blocks.length - 1); j++)
194         {
195             offset = 508 + (j * 512);
196             expected[ offset++ ] = ( byte ) (start_block + j + 1);
197             expected[ offset++ ] = 0;
198             expected[ offset++ ] = 0;
199             expected[ offset++ ] = 0;
200         }
201         offset = (blocks.length * 512) - 4;
202         expected[ offset++ ] = ( byte ) -2;
203         expected[ offset++ ] = ( byte ) -1;
204         expected[ offset++ ] = ( byte ) -1;
205         expected[ offset++ ] = ( byte ) -1;
206         ByteArrayOutputStream stream = new ByteArrayOutputStream(512
207                                            * blocks.length);
208
209         for (int j = 0; j < blocks.length; j++)
210         {
211             blocks[ j ].writeBlocks(stream);
212         }
213         byte[] actual = stream.toByteArray();
214
215         assertEquals(expected.length, actual.length);
216         for (int j = 0; j < expected.length; j++)
217         {
218             assertEquals("offset " + j, expected[ j ], actual[ j ]);
219         }
220     }
221
222     /**
223      * test calculateXBATStorageRequirements
224      */

225
226     public void testCalculateXBATStorageRequirements()
227     {
228         int[] blockCounts =
229         {
230             0, 1, 127, 128
231         };
232         int[] requirements =
233         {
234             0, 1, 1, 2
235         };
236
237         for (int j = 0; j < blockCounts.length; j++)
238         {
239             assertEquals(
240                 "requirement for " + blockCounts[ j ], requirements[ j ],
241                 BATBlock.calculateXBATStorageRequirements(blockCounts[ j ]));
242         }
243     }
244
245     /**
246      * test entriesPerBlock
247      */

248
249     public void testEntriesPerBlock()
250     {
251         assertEquals(128, BATBlock.entriesPerBlock());
252     }
253
254     /**
255      * test entriesPerXBATBlock
256      */

257
258     public void testEntriesPerXBATBlock()
259     {
260         assertEquals(127, BATBlock.entriesPerXBATBlock());
261     }
262
263     /**
264      * test getXBATChainOffset
265      */

266
267     public void testGetXBATChainOffset()
268     {
269         assertEquals(508, BATBlock.getXBATChainOffset());
270     }
271
272     /**
273      * main method to run the unit tests
274      *
275      * @param ignored_args
276      */

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