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 SmallDocumentBlock functionality
028: *
029: * @author Marc Johnson
030: */
031:
032: public class TestSmallDocumentBlock extends TestCase {
033: static final private byte[] _testdata;
034: static final private int _testdata_size = 2999;
035:
036: static {
037: _testdata = new byte[_testdata_size];
038: for (int j = 0; j < _testdata.length; j++) {
039: _testdata[j] = (byte) j;
040: }
041: };
042:
043: /**
044: * constructor
045: *
046: * @param name
047: */
048:
049: public TestSmallDocumentBlock(String name) {
050: super (name);
051: }
052:
053: /**
054: * Test conversion from DocumentBlocks
055: *
056: * @exception IOException
057: */
058:
059: public void testConvert1() throws IOException {
060: ByteArrayInputStream stream = new ByteArrayInputStream(
061: _testdata);
062: List documents = new ArrayList();
063:
064: while (true) {
065: DocumentBlock block = new DocumentBlock(stream);
066:
067: documents.add(block);
068: if (block.partiallyRead()) {
069: break;
070: }
071: }
072: SmallDocumentBlock[] results = SmallDocumentBlock.convert(
073: (BlockWritable[]) documents
074: .toArray(new DocumentBlock[0]), _testdata_size);
075:
076: assertEquals("checking correct result size: ",
077: (_testdata_size + 63) / 64, results.length);
078: ByteArrayOutputStream output = new ByteArrayOutputStream();
079:
080: for (int j = 0; j < results.length; j++) {
081: results[j].writeBlocks(output);
082: }
083: byte[] output_array = output.toByteArray();
084:
085: assertEquals("checking correct output size: ",
086: 64 * results.length, output_array.length);
087: int index = 0;
088:
089: for (; index < _testdata_size; index++) {
090: assertEquals("checking output " + index, _testdata[index],
091: output_array[index]);
092: }
093: for (; index < output_array.length; index++) {
094: assertEquals("checking output " + index, (byte) 0xff,
095: output_array[index]);
096: }
097: }
098:
099: /**
100: * Test conversion from byte array
101: *
102: * @exception IOException;
103: *
104: * @exception IOException
105: */
106:
107: public void testConvert2() throws IOException {
108: for (int j = 0; j < 320; j++) {
109: byte[] array = new byte[j];
110:
111: for (int k = 0; k < j; k++) {
112: array[k] = (byte) k;
113: }
114: SmallDocumentBlock[] blocks = SmallDocumentBlock.convert(
115: array, 319);
116:
117: assertEquals(5, blocks.length);
118: ByteArrayOutputStream stream = new ByteArrayOutputStream();
119:
120: for (int k = 0; k < blocks.length; k++) {
121: blocks[k].writeBlocks(stream);
122: }
123: stream.close();
124: byte[] output = stream.toByteArray();
125:
126: for (int k = 0; k < array.length; k++) {
127: assertEquals(String.valueOf(k), array[k], output[k]);
128: }
129: for (int k = array.length; k < 320; k++) {
130: assertEquals(String.valueOf(k), (byte) 0xFF, output[k]);
131: }
132: }
133: }
134:
135: /**
136: * Test read method
137: *
138: * @exception IOException
139: */
140:
141: public void testRead() throws IOException {
142: ByteArrayInputStream stream = new ByteArrayInputStream(
143: _testdata);
144: List documents = new ArrayList();
145:
146: while (true) {
147: DocumentBlock block = new DocumentBlock(stream);
148:
149: documents.add(block);
150: if (block.partiallyRead()) {
151: break;
152: }
153: }
154: SmallDocumentBlock[] blocks = SmallDocumentBlock.convert(
155: (BlockWritable[]) documents
156: .toArray(new DocumentBlock[0]), _testdata_size);
157:
158: for (int j = 1; j <= _testdata_size; j += 38) {
159: byte[] buffer = new byte[j];
160: int offset = 0;
161:
162: for (int k = 0; k < (_testdata_size / j); k++) {
163: SmallDocumentBlock.read(blocks, buffer, offset);
164: for (int n = 0; n < buffer.length; n++) {
165: assertEquals("checking byte " + (k * j) + n,
166: _testdata[(k * j) + n], buffer[n]);
167: }
168: offset += j;
169: }
170: }
171: }
172:
173: /**
174: * test fill
175: *
176: * @exception IOException
177: */
178:
179: public void testFill() throws IOException {
180: for (int j = 0; j <= 8; j++) {
181: List foo = new ArrayList();
182:
183: for (int k = 0; k < j; k++) {
184: foo.add(new Object());
185: }
186: int result = SmallDocumentBlock.fill(foo);
187:
188: assertEquals("correct big block count: ", (j + 7) / 8,
189: result);
190: assertEquals("correct small block count: ", 8 * result, foo
191: .size());
192: for (int m = j; m < foo.size(); m++) {
193: BlockWritable block = (BlockWritable) foo.get(m);
194: ByteArrayOutputStream stream = new ByteArrayOutputStream();
195:
196: block.writeBlocks(stream);
197: byte[] output = stream.toByteArray();
198:
199: assertEquals("correct output size (block[ " + m
200: + " ]): ", 64, output.length);
201: for (int n = 0; n < 64; n++) {
202: assertEquals("correct value (block[ " + m + " ][ "
203: + n + " ]): ", (byte) 0xff, output[n]);
204: }
205: }
206: }
207: }
208:
209: /**
210: * test calcSize
211: */
212:
213: public void testCalcSize() {
214: for (int j = 0; j < 10; j++) {
215: assertEquals("testing " + j, j * 64, SmallDocumentBlock
216: .calcSize(j));
217: }
218: }
219:
220: /**
221: * test extract method
222: *
223: * @exception IOException
224: */
225:
226: public void testExtract() throws IOException {
227: byte[] data = new byte[512];
228: int offset = 0;
229:
230: for (int j = 0; j < 8; j++) {
231: for (int k = 0; k < 64; k++) {
232: data[offset++] = (byte) (k + j);
233: }
234: }
235: RawDataBlock[] blocks = { new RawDataBlock(
236: new ByteArrayInputStream(data)) };
237: List output = SmallDocumentBlock.extract(blocks);
238: Iterator iter = output.iterator();
239:
240: offset = 0;
241: while (iter.hasNext()) {
242: byte[] out_data = ((SmallDocumentBlock) iter.next())
243: .getData();
244:
245: assertEquals("testing block at offset " + offset, 64,
246: out_data.length);
247: for (int j = 0; j < out_data.length; j++) {
248: assertEquals("testing byte at offset " + offset,
249: data[offset], out_data[j]);
250: offset++;
251: }
252: }
253: }
254:
255: /**
256: * main method to run the unit tests
257: *
258: * @param ignored_args
259: */
260:
261: public static void main(String[] ignored_args) {
262: System.out
263: .println("Testing org.apache.poi.poifs.storage.SmallDocumentBlock");
264: junit.textui.TestRunner.run(TestSmallDocumentBlock.class);
265: }
266: }
|