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 DocumentBlock functionality
028: *
029: * @author Marc Johnson
030: */
031:
032: public class TestDocumentBlock extends TestCase {
033: static final private byte[] _testdata;
034:
035: static {
036: _testdata = new byte[2000];
037: for (int j = 0; j < _testdata.length; j++) {
038: _testdata[j] = (byte) j;
039: }
040: };
041:
042: /**
043: * Constructor TestDocumentBlock
044: *
045: * @param name
046: */
047:
048: public TestDocumentBlock(String name) {
049: super (name);
050: }
051:
052: /**
053: * Test the writing DocumentBlock constructor.
054: *
055: * @exception IOException
056: */
057:
058: public void testConstructor() throws IOException {
059: ByteArrayInputStream input = new ByteArrayInputStream(_testdata);
060: int index = 0;
061: int size = 0;
062:
063: while (true) {
064: byte[] data = new byte[Math.min(_testdata.length - index,
065: 512)];
066:
067: System.arraycopy(_testdata, index, data, 0, data.length);
068: DocumentBlock block = new DocumentBlock(input);
069:
070: verifyOutput(block, data);
071: size += block.size();
072: if (block.partiallyRead()) {
073: break;
074: }
075: index += 512;
076: }
077: assertEquals(_testdata.length, size);
078: }
079:
080: /**
081: * test static read method
082: *
083: * @exception IOException
084: */
085:
086: public void testRead() throws IOException {
087: DocumentBlock[] blocks = new DocumentBlock[4];
088: ByteArrayInputStream input = new ByteArrayInputStream(_testdata);
089:
090: for (int j = 0; j < 4; j++) {
091: blocks[j] = new DocumentBlock(input);
092: }
093: for (int j = 1; j <= 2000; j += 17) {
094: byte[] buffer = new byte[j];
095: int offset = 0;
096:
097: for (int k = 0; k < (2000 / j); k++) {
098: DocumentBlock.read(blocks, buffer, offset);
099: for (int n = 0; n < buffer.length; n++) {
100: assertEquals("checking byte " + (k * j) + n,
101: _testdata[(k * j) + n], buffer[n]);
102: }
103: offset += j;
104: }
105: }
106: }
107:
108: /**
109: * Test 'reading' constructor
110: *
111: * @exception IOException
112: */
113:
114: public void testReadingConstructor() throws IOException {
115: RawDataBlock input = new RawDataBlock(new ByteArrayInputStream(
116: _testdata));
117:
118: verifyOutput(new DocumentBlock(input), input.getData());
119: }
120:
121: private void verifyOutput(DocumentBlock block, byte[] input)
122: throws IOException {
123: assertEquals(input.length, block.size());
124: if (input.length < 512) {
125: assertTrue(block.partiallyRead());
126: } else {
127: assertTrue(!block.partiallyRead());
128: }
129: ByteArrayOutputStream output = new ByteArrayOutputStream(512);
130:
131: block.writeBlocks(output);
132: byte[] copy = output.toByteArray();
133: int j = 0;
134:
135: for (; j < input.length; j++) {
136: assertEquals(input[j], copy[j]);
137: }
138: for (; j < 512; j++) {
139: assertEquals((byte) 0xFF, copy[j]);
140: }
141: }
142:
143: /**
144: * main method to run the unit tests
145: *
146: * @param ignored_args
147: */
148:
149: public static void main(String[] ignored_args) {
150: System.out
151: .println("Testing org.apache.poi.poifs.storage.DocumentBlock");
152: junit.textui.TestRunner.run(TestDocumentBlock.class);
153: }
154: }
|