001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.poifs.storage;
019:
020: import java.io.*;
021:
022: import java.util.*;
023:
024: import junit.framework.*;
025:
026: /**
027: * Class to test BATBlock functionality
028: *
029: * @author Marc Johnson
030: */
031:
032: public class TestBATBlock extends TestCase {
033:
034: /**
035: * Constructor TestBATBlock
036: *
037: * @param name
038: */
039:
040: public TestBATBlock(String name) {
041: super (name);
042: }
043:
044: /**
045: * Test the createBATBlocks method. The test involves setting up
046: * various arrays of int's and ensuring that the correct number of
047: * BATBlocks is created for each array, and that the data from
048: * each array is correctly written to the BATBlocks.
049: *
050: * @exception IOException
051: */
052:
053: public void testCreateBATBlocks() throws IOException {
054:
055: // test 0 length array (basic sanity)
056: BATBlock[] rvalue = BATBlock
057: .createBATBlocks(createTestArray(0));
058:
059: assertEquals(0, rvalue.length);
060:
061: // test array of length 1
062: rvalue = BATBlock.createBATBlocks(createTestArray(1));
063: assertEquals(1, rvalue.length);
064: verifyContents(rvalue, 1);
065:
066: // test array of length 127
067: rvalue = BATBlock.createBATBlocks(createTestArray(127));
068: assertEquals(1, rvalue.length);
069: verifyContents(rvalue, 127);
070:
071: // test array of length 128
072: rvalue = BATBlock.createBATBlocks(createTestArray(128));
073: assertEquals(1, rvalue.length);
074: verifyContents(rvalue, 128);
075:
076: // test array of length 129
077: rvalue = BATBlock.createBATBlocks(createTestArray(129));
078: assertEquals(2, rvalue.length);
079: verifyContents(rvalue, 129);
080: }
081:
082: private int[] createTestArray(int count) {
083: int[] rvalue = new int[count];
084:
085: for (int j = 0; j < count; j++) {
086: rvalue[j] = j;
087: }
088: return rvalue;
089: }
090:
091: private void verifyContents(BATBlock[] blocks, int entries)
092: throws IOException {
093: byte[] expected = new byte[512 * blocks.length];
094:
095: Arrays.fill(expected, (byte) 0xFF);
096: int offset = 0;
097:
098: for (int j = 0; j < entries; j++) {
099: expected[offset++] = (byte) j;
100: expected[offset++] = 0;
101: expected[offset++] = 0;
102: expected[offset++] = 0;
103: }
104: ByteArrayOutputStream stream = new ByteArrayOutputStream(
105: 512 * blocks.length);
106:
107: for (int j = 0; j < blocks.length; j++) {
108: blocks[j].writeBlocks(stream);
109: }
110: byte[] actual = stream.toByteArray();
111:
112: assertEquals(expected.length, actual.length);
113: for (int j = 0; j < expected.length; j++) {
114: assertEquals(expected[j], actual[j]);
115: }
116: }
117:
118: /**
119: * test createXBATBlocks
120: *
121: * @exception IOException
122: */
123:
124: public void testCreateXBATBlocks() throws IOException {
125:
126: // test 0 length array (basic sanity)
127: BATBlock[] rvalue = BATBlock.createXBATBlocks(
128: createTestArray(0), 1);
129:
130: assertEquals(0, rvalue.length);
131:
132: // test array of length 1
133: rvalue = BATBlock.createXBATBlocks(createTestArray(1), 1);
134: assertEquals(1, rvalue.length);
135: verifyXBATContents(rvalue, 1, 1);
136:
137: // test array of length 127
138: rvalue = BATBlock.createXBATBlocks(createTestArray(127), 1);
139: assertEquals(1, rvalue.length);
140: verifyXBATContents(rvalue, 127, 1);
141:
142: // test array of length 128
143: rvalue = BATBlock.createXBATBlocks(createTestArray(128), 1);
144: assertEquals(2, rvalue.length);
145: verifyXBATContents(rvalue, 128, 1);
146:
147: // test array of length 254
148: rvalue = BATBlock.createXBATBlocks(createTestArray(254), 1);
149: assertEquals(2, rvalue.length);
150: verifyXBATContents(rvalue, 254, 1);
151:
152: // test array of length 255
153: rvalue = BATBlock.createXBATBlocks(createTestArray(255), 1);
154: assertEquals(3, rvalue.length);
155: verifyXBATContents(rvalue, 255, 1);
156: }
157:
158: private void verifyXBATContents(BATBlock[] blocks, int entries,
159: int start_block) throws IOException {
160: byte[] expected = new byte[512 * blocks.length];
161:
162: Arrays.fill(expected, (byte) 0xFF);
163: int offset = 0;
164:
165: for (int j = 0; j < entries; j++) {
166: if ((j % 127) == 0) {
167: if (j != 0) {
168: offset += 4;
169: }
170: }
171: expected[offset++] = (byte) j;
172: expected[offset++] = 0;
173: expected[offset++] = 0;
174: expected[offset++] = 0;
175: }
176: for (int j = 0; j < (blocks.length - 1); j++) {
177: offset = 508 + (j * 512);
178: expected[offset++] = (byte) (start_block + j + 1);
179: expected[offset++] = 0;
180: expected[offset++] = 0;
181: expected[offset++] = 0;
182: }
183: offset = (blocks.length * 512) - 4;
184: expected[offset++] = (byte) -2;
185: expected[offset++] = (byte) -1;
186: expected[offset++] = (byte) -1;
187: expected[offset++] = (byte) -1;
188: ByteArrayOutputStream stream = new ByteArrayOutputStream(
189: 512 * blocks.length);
190:
191: for (int j = 0; j < blocks.length; j++) {
192: blocks[j].writeBlocks(stream);
193: }
194: byte[] actual = stream.toByteArray();
195:
196: assertEquals(expected.length, actual.length);
197: for (int j = 0; j < expected.length; j++) {
198: assertEquals("offset " + j, expected[j], actual[j]);
199: }
200: }
201:
202: /**
203: * test calculateXBATStorageRequirements
204: */
205:
206: public void testCalculateXBATStorageRequirements() {
207: int[] blockCounts = { 0, 1, 127, 128 };
208: int[] requirements = { 0, 1, 1, 2 };
209:
210: for (int j = 0; j < blockCounts.length; j++) {
211: assertEquals(
212: "requirement for " + blockCounts[j],
213: requirements[j],
214: BATBlock
215: .calculateXBATStorageRequirements(blockCounts[j]));
216: }
217: }
218:
219: /**
220: * test entriesPerBlock
221: */
222:
223: public void testEntriesPerBlock() {
224: assertEquals(128, BATBlock.entriesPerBlock());
225: }
226:
227: /**
228: * test entriesPerXBATBlock
229: */
230:
231: public void testEntriesPerXBATBlock() {
232: assertEquals(127, BATBlock.entriesPerXBATBlock());
233: }
234:
235: /**
236: * test getXBATChainOffset
237: */
238:
239: public void testGetXBATChainOffset() {
240: assertEquals(508, BATBlock.getXBATChainOffset());
241: }
242:
243: /**
244: * main method to run the unit tests
245: *
246: * @param ignored_args
247: */
248:
249: public static void main(String[] ignored_args) {
250: System.out
251: .println("Testing org.apache.poi.poifs.storage.BATBlock");
252: junit.textui.TestRunner.run(TestBATBlock.class);
253: }
254: }
|