KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

46
47     public TestBlockAllocationTableWriter(String JavaDoc name)
48     {
49         super(name);
50     }
51
52     /**
53      * Test the allocateSpace method.
54      */

55
56     public void testAllocateSpace()
57     {
58         BlockAllocationTableWriter table =
59             new BlockAllocationTableWriter();
60         int[] blockSizes =
61         {
62             0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
63         };
64         int expectedIndex = 0;
65
66         for (int j = 0; j < blockSizes.length; j++)
67         {
68             assertEquals(expectedIndex, table.allocateSpace(blockSizes[ j ]));
69             expectedIndex += blockSizes[ j ];
70         }
71     }
72
73     /**
74      * Test the createBlocks method
75      *
76      * @exception IOException
77      */

78
79     public void testCreateBlocks()
80         throws IOException
81     {
82         BlockAllocationTableWriter table = new BlockAllocationTableWriter();
83
84         table.allocateSpace(127);
85         table.createBlocks();
86         verifyBlocksCreated(table, 1);
87         table = new BlockAllocationTableWriter();
88         table.allocateSpace(128);
89         table.createBlocks();
90         verifyBlocksCreated(table, 2);
91         table = new BlockAllocationTableWriter();
92         table.allocateSpace(254);
93         table.createBlocks();
94         verifyBlocksCreated(table, 2);
95         table = new BlockAllocationTableWriter();
96         table.allocateSpace(255);
97         table.createBlocks();
98         verifyBlocksCreated(table, 3);
99         table = new BlockAllocationTableWriter();
100         table.allocateSpace(13843);
101         table.createBlocks();
102         verifyBlocksCreated(table, 109);
103         table = new BlockAllocationTableWriter();
104         table.allocateSpace(13844);
105         table.createBlocks();
106         verifyBlocksCreated(table, 110);
107         table = new BlockAllocationTableWriter();
108         table.allocateSpace(13969);
109         table.createBlocks();
110         verifyBlocksCreated(table, 110);
111         table = new BlockAllocationTableWriter();
112         table.allocateSpace(13970);
113         table.createBlocks();
114         verifyBlocksCreated(table, 111);
115     }
116
117     /**
118      * Test content produced by BlockAllocationTableWriter
119      *
120      * @exception IOException
121      */

122
123     public void testProduct()
124         throws IOException
125     {
126         BlockAllocationTableWriter table = new BlockAllocationTableWriter();
127
128         for (int k = 1; k <= 22; k++)
129         {
130             table.allocateSpace(k);
131         }
132         table.createBlocks();
133         ByteArrayOutputStream stream = new ByteArrayOutputStream();
134
135         table.writeBlocks(stream);
136         byte[] output = stream.toByteArray();
137
138         assertEquals(1024, output.length);
139         byte[] expected = new byte[ 1024 ];
140
141         Arrays.fill(expected, ( byte ) 0xFF);
142         int offset = 0;
143         int block_index = 1;
144
145         for (int k = 1; k <= 22; k++)
146         {
147             int limit = k - 1;
148
149             for (int j = 0; j < limit; j++)
150             {
151                 LittleEndian.putInt(expected, offset, block_index++);
152                 offset += LittleEndianConsts.INT_SIZE;
153             }
154             LittleEndian.putInt(expected, offset,
155                                 POIFSConstants.END_OF_CHAIN);
156             offset += 4;
157             block_index++;
158         }
159
160         // add BAT block indices
161
LittleEndian.putInt(expected, offset, block_index++);
162         offset += LittleEndianConsts.INT_SIZE;
163         LittleEndian.putInt(expected, offset, POIFSConstants.END_OF_CHAIN);
164         for (int k = 0; k < expected.length; k++)
165         {
166             assertEquals("At offset " + k, expected[ k ], output[ k ]);
167         }
168     }
169
170     private void verifyBlocksCreated(BlockAllocationTableWriter table,
171                                      int count)
172         throws IOException
173     {
174         ByteArrayOutputStream stream = new ByteArrayOutputStream();
175
176         table.writeBlocks(stream);
177         byte[] output = stream.toByteArray();
178
179         assertEquals(count * 512, output.length);
180     }
181
182     /**
183      * main method to run the unit tests
184      *
185      * @param ignored_args
186      */

187
188     public static void main(String JavaDoc [] ignored_args)
189     {
190         System.out.println(
191             "Testing org.apache.poi.poifs.storage.BlockAllocationTableWriter");
192         junit.textui.TestRunner.run(TestBlockAllocationTableWriter.class);
193     }
194 }
195
Popular Tags