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: import org.apache.poi.poifs.common.POIFSConstants;
027: import org.apache.poi.util.LittleEndian;
028: import org.apache.poi.util.LittleEndianConsts;
029:
030: /**
031: * Class to test BlockAllocationTableWriter functionality
032: *
033: * @author Marc Johnson
034: */
035:
036: public class TestBlockAllocationTableWriter extends TestCase {
037:
038: /**
039: * Constructor TestBlockAllocationTableWriter
040: *
041: * @param name
042: */
043:
044: public TestBlockAllocationTableWriter(String name) {
045: super (name);
046: }
047:
048: /**
049: * Test the allocateSpace method.
050: */
051:
052: public void testAllocateSpace() {
053: BlockAllocationTableWriter table = new BlockAllocationTableWriter();
054: int[] blockSizes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
055: 4, 5, 6, 7, 8, 9 };
056: int expectedIndex = 0;
057:
058: for (int j = 0; j < blockSizes.length; j++) {
059: assertEquals(expectedIndex, table
060: .allocateSpace(blockSizes[j]));
061: expectedIndex += blockSizes[j];
062: }
063: }
064:
065: /**
066: * Test the createBlocks method
067: *
068: * @exception IOException
069: */
070:
071: public void testCreateBlocks() throws IOException {
072: BlockAllocationTableWriter table = new BlockAllocationTableWriter();
073:
074: table.allocateSpace(127);
075: table.createBlocks();
076: verifyBlocksCreated(table, 1);
077: table = new BlockAllocationTableWriter();
078: table.allocateSpace(128);
079: table.createBlocks();
080: verifyBlocksCreated(table, 2);
081: table = new BlockAllocationTableWriter();
082: table.allocateSpace(254);
083: table.createBlocks();
084: verifyBlocksCreated(table, 2);
085: table = new BlockAllocationTableWriter();
086: table.allocateSpace(255);
087: table.createBlocks();
088: verifyBlocksCreated(table, 3);
089: table = new BlockAllocationTableWriter();
090: table.allocateSpace(13843);
091: table.createBlocks();
092: verifyBlocksCreated(table, 109);
093: table = new BlockAllocationTableWriter();
094: table.allocateSpace(13844);
095: table.createBlocks();
096: verifyBlocksCreated(table, 110);
097: table = new BlockAllocationTableWriter();
098: table.allocateSpace(13969);
099: table.createBlocks();
100: verifyBlocksCreated(table, 110);
101: table = new BlockAllocationTableWriter();
102: table.allocateSpace(13970);
103: table.createBlocks();
104: verifyBlocksCreated(table, 111);
105: }
106:
107: /**
108: * Test content produced by BlockAllocationTableWriter
109: *
110: * @exception IOException
111: */
112:
113: public void testProduct() throws IOException {
114: BlockAllocationTableWriter table = new BlockAllocationTableWriter();
115:
116: for (int k = 1; k <= 22; k++) {
117: table.allocateSpace(k);
118: }
119: table.createBlocks();
120: ByteArrayOutputStream stream = new ByteArrayOutputStream();
121:
122: table.writeBlocks(stream);
123: byte[] output = stream.toByteArray();
124:
125: assertEquals(1024, output.length);
126: byte[] expected = new byte[1024];
127:
128: Arrays.fill(expected, (byte) 0xFF);
129: int offset = 0;
130: int block_index = 1;
131:
132: for (int k = 1; k <= 22; k++) {
133: int limit = k - 1;
134:
135: for (int j = 0; j < limit; j++) {
136: LittleEndian.putInt(expected, offset, block_index++);
137: offset += LittleEndianConsts.INT_SIZE;
138: }
139: LittleEndian.putInt(expected, offset,
140: POIFSConstants.END_OF_CHAIN);
141: offset += 4;
142: block_index++;
143: }
144:
145: // add BAT block indices
146: LittleEndian.putInt(expected, offset, block_index++);
147: offset += LittleEndianConsts.INT_SIZE;
148: LittleEndian.putInt(expected, offset,
149: POIFSConstants.END_OF_CHAIN);
150: for (int k = 0; k < expected.length; k++) {
151: assertEquals("At offset " + k, expected[k], output[k]);
152: }
153: }
154:
155: private void verifyBlocksCreated(BlockAllocationTableWriter table,
156: int count) throws IOException {
157: ByteArrayOutputStream stream = new ByteArrayOutputStream();
158:
159: table.writeBlocks(stream);
160: byte[] output = stream.toByteArray();
161:
162: assertEquals(count * 512, output.length);
163: }
164:
165: /**
166: * main method to run the unit tests
167: *
168: * @param ignored_args
169: */
170:
171: public static void main(String[] ignored_args) {
172: System.out
173: .println("Testing org.apache.poi.poifs.storage.BlockAllocationTableWriter");
174: junit.textui.TestRunner
175: .run(TestBlockAllocationTableWriter.class);
176: }
177: }
|