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.filesystem;
019:
020: import java.io.*;
021:
022: import java.util.*;
023:
024: import junit.framework.*;
025:
026: import org.apache.poi.util.LittleEndian;
027: import org.apache.poi.util.LittleEndianConsts;
028: import org.apache.poi.poifs.property.DocumentProperty;
029: import org.apache.poi.poifs.storage.RawDataBlock;
030: import org.apache.poi.poifs.storage.SmallDocumentBlock;
031:
032: /**
033: * Class to test POIFSDocument functionality
034: *
035: * @author Marc Johnson
036: */
037:
038: public class TestDocument extends TestCase {
039:
040: /**
041: * Constructor TestDocument
042: *
043: * @param name
044: */
045:
046: public TestDocument(String name) {
047: super (name);
048: }
049:
050: /**
051: * Integration test -- really about all we can do
052: *
053: * @exception IOException
054: */
055:
056: public void testPOIFSDocument() throws IOException {
057:
058: // verify correct number of blocks get created for document
059: // that is exact multituple of block size
060: POIFSDocument document;
061: byte[] array = new byte[4096];
062:
063: for (int j = 0; j < array.length; j++) {
064: array[j] = (byte) j;
065: }
066: document = new POIFSDocument("foo", new SlowInputStream(
067: new ByteArrayInputStream(array)));
068: checkDocument(document, array);
069:
070: // verify correct number of blocks get created for document
071: // that is not an exact multiple of block size
072: array = new byte[4097];
073: for (int j = 0; j < array.length; j++) {
074: array[j] = (byte) j;
075: }
076: document = new POIFSDocument("bar", new ByteArrayInputStream(
077: array));
078: checkDocument(document, array);
079:
080: // verify correct number of blocks get created for document
081: // that is small
082: array = new byte[4095];
083: for (int j = 0; j < array.length; j++) {
084: array[j] = (byte) j;
085: }
086: document = new POIFSDocument("_bar", new ByteArrayInputStream(
087: array));
088: checkDocument(document, array);
089:
090: // verify correct number of blocks get created for document
091: // that is rather small
092: array = new byte[199];
093: for (int j = 0; j < array.length; j++) {
094: array[j] = (byte) j;
095: }
096: document = new POIFSDocument("_bar2", new ByteArrayInputStream(
097: array));
098: checkDocument(document, array);
099:
100: // verify that output is correct
101: array = new byte[4097];
102: for (int j = 0; j < array.length; j++) {
103: array[j] = (byte) j;
104: }
105: document = new POIFSDocument("foobar",
106: new ByteArrayInputStream(array));
107: checkDocument(document, array);
108: document.setStartBlock(0x12345678); // what a big file!!
109: DocumentProperty property = document.getDocumentProperty();
110: ByteArrayOutputStream stream = new ByteArrayOutputStream();
111:
112: property.writeData(stream);
113: byte[] output = stream.toByteArray();
114: byte[] array2 = { (byte) 'f', (byte) 0, (byte) 'o', (byte) 0,
115: (byte) 'o', (byte) 0, (byte) 'b', (byte) 0, (byte) 'a',
116: (byte) 0, (byte) 'r', (byte) 0, (byte) 0, (byte) 0,
117: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
118: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
119: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
120: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
121: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
122: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
123: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
124: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
125: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
126: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
127: (byte) 14, (byte) 0, (byte) 2, (byte) 1, (byte) -1,
128: (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1,
129: (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1,
130: (byte) -1, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
131: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
132: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
133: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
134: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
135: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
136: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
137: (byte) 0, (byte) 0, (byte) 0x78, (byte) 0x56,
138: (byte) 0x34, (byte) 0x12, (byte) 1, (byte) 16,
139: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
140: (byte) 0 };
141:
142: assertEquals(array2.length, output.length);
143: for (int j = 0; j < output.length; j++) {
144: assertEquals("Checking property offset " + j, array2[j],
145: output[j]);
146: }
147: }
148:
149: private POIFSDocument makeCopy(POIFSDocument document,
150: byte[] input, byte[] data) throws IOException {
151: POIFSDocument copy = null;
152:
153: if (input.length >= 4096) {
154: RawDataBlock[] blocks = new RawDataBlock[(input.length + 511) / 512];
155: ByteArrayInputStream stream = new ByteArrayInputStream(data);
156: int index = 0;
157:
158: while (true) {
159: RawDataBlock block = new RawDataBlock(stream);
160:
161: if (block.eof()) {
162: break;
163: }
164: blocks[index++] = block;
165: }
166: copy = new POIFSDocument("test" + input.length, blocks,
167: input.length);
168: } else {
169: copy = new POIFSDocument("test" + input.length,
170: (SmallDocumentBlock[]) document.getSmallBlocks(),
171: input.length);
172: }
173: return copy;
174: }
175:
176: private void checkDocument(final POIFSDocument document,
177: final byte[] input) throws IOException {
178: int big_blocks = 0;
179: int small_blocks = 0;
180: int total_output = 0;
181:
182: if (input.length >= 4096) {
183: big_blocks = (input.length + 511) / 512;
184: total_output = big_blocks * 512;
185: } else {
186: small_blocks = (input.length + 63) / 64;
187: total_output = 0;
188: }
189: checkValues(big_blocks, small_blocks, total_output, makeCopy(
190: document, input, checkValues(big_blocks, small_blocks,
191: total_output, document, input)), input);
192: }
193:
194: private byte[] checkValues(int big_blocks, int small_blocks,
195: int total_output, POIFSDocument document, byte[] input)
196: throws IOException {
197: assertEquals(document, document.getDocumentProperty()
198: .getDocument());
199: int increment = (int) Math.sqrt(input.length);
200:
201: for (int j = 1; j <= input.length; j += increment) {
202: byte[] buffer = new byte[j];
203: int offset = 0;
204:
205: for (int k = 0; k < (input.length / j); k++) {
206: document.read(buffer, offset);
207: for (int n = 0; n < buffer.length; n++) {
208: assertEquals("checking byte " + (k * j) + n,
209: input[(k * j) + n], buffer[n]);
210: }
211: offset += j;
212: }
213: }
214: assertEquals(big_blocks, document.countBlocks());
215: assertEquals(small_blocks, document.getSmallBlocks().length);
216: ByteArrayOutputStream stream = new ByteArrayOutputStream();
217:
218: document.writeBlocks(stream);
219: byte[] output = stream.toByteArray();
220:
221: assertEquals(total_output, output.length);
222: int limit = Math.min(total_output, input.length);
223:
224: for (int j = 0; j < limit; j++) {
225: assertEquals("Checking document offset " + j, input[j],
226: output[j]);
227: }
228: for (int j = limit; j < output.length; j++) {
229: assertEquals("Checking document offset " + j, (byte) -1,
230: output[j]);
231: }
232: return output;
233: }
234:
235: /**
236: * main method to run the unit tests
237: *
238: * @param ignored_args
239: */
240:
241: public static void main(String[] ignored_args) {
242: System.out
243: .println("Testing org.apache.poi.poifs.filesystem.POIFSDocument");
244: junit.textui.TestRunner.run(TestDocument.class);
245: }
246: }
|