You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2010/12/30 03:35:11 UTC
svn commit: r1053791 [13/18] - in /poi/branches/NIO_32_BRANCH: ./
src/contrib/src/org/apache/poi/contrib/poibrowser/ src/java/org/apache/poi/
src/java/org/apache/poi/hpsf/ src/java/org/apache/poi/hpsf/extractor/
src/java/org/apache/poi/hssf/record/ src...
Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java Thu Dec 30 02:35:06 2010
@@ -1,4 +1,3 @@
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@@ -15,210 +14,118 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
package org.apache.poi.poifs.property;
-import java.io.*;
-
-import java.util.*;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
-import junit.framework.*;
+import junit.framework.TestCase;
import org.apache.poi.poifs.common.POIFSConstants;
+import org.apache.poi.poifs.storage.RawDataUtil;
/**
* Class to test RootProperty functionality
*
* @author Marc Johnson
*/
-
-public class TestRootProperty
- extends TestCase
-{
- private RootProperty _property;
- private byte[] _testblock;
-
- /**
- * Constructor TestRootProperty
- *
- * @param name
- */
-
- public TestRootProperty(String name)
- {
- super(name);
- }
-
- /**
- * Test constructing RootProperty
- *
- * @exception IOException
- */
-
- public void testConstructor()
- throws IOException
- {
- createBasicRootProperty();
- verifyProperty();
- }
-
- private void createBasicRootProperty()
- {
- _property = new RootProperty();
- _testblock = new byte[ 128 ];
- int index = 0;
-
- for (; index < 0x40; index++)
- {
- _testblock[ index ] = ( byte ) 0;
- }
- String name = "Root Entry";
- int limit = Math.min(31, name.length());
-
- _testblock[ index++ ] = ( byte ) (2 * (limit + 1));
- _testblock[ index++ ] = ( byte ) 0;
- _testblock[ index++ ] = ( byte ) 5;
- _testblock[ index++ ] = ( byte ) 1;
- for (; index < 0x50; index++)
- {
- _testblock[ index ] = ( byte ) 0xff;
- }
- for (; index < 0x74; index++)
- {
- _testblock[ index ] = ( byte ) 0;
- }
- _testblock[ index++ ] = ( byte ) POIFSConstants.END_OF_CHAIN;
- for (; index < 0x78; index++)
- {
- _testblock[ index ] = ( byte ) 0xff;
- }
- for (; index < 0x80; index++)
- {
- _testblock[ index ] = ( byte ) 0;
- }
- byte[] name_bytes = name.getBytes();
-
- for (index = 0; index < limit; index++)
- {
- _testblock[ index * 2 ] = name_bytes[ index ];
- }
- }
-
- private void verifyProperty()
- throws IOException
- {
- ByteArrayOutputStream stream = new ByteArrayOutputStream(512);
-
- _property.writeData(stream);
- byte[] output = stream.toByteArray();
-
- assertEquals(_testblock.length, output.length);
- for (int j = 0; j < _testblock.length; j++)
- {
- assertEquals("mismatch at offset " + j, _testblock[ j ],
- output[ j ]);
- }
- }
-
- /**
- * test setSize
- */
-
- public void testSetSize()
- {
- for (int j = 0; j < 10; j++)
- {
- createBasicRootProperty();
- _property.setSize(j);
- assertEquals("trying block count of " + j, j * 64,
- _property.getSize());
- }
- }
-
- /**
- * Test reading constructor
- *
- * @exception IOException
- */
-
- public void testReadingConstructor()
- throws IOException
- {
- byte[] input =
- {
- ( byte ) 0x52, ( byte ) 0x00, ( byte ) 0x6F, ( byte ) 0x00,
- ( byte ) 0x6F, ( byte ) 0x00, ( byte ) 0x74, ( byte ) 0x00,
- ( byte ) 0x20, ( byte ) 0x00, ( byte ) 0x45, ( byte ) 0x00,
- ( byte ) 0x6E, ( byte ) 0x00, ( byte ) 0x74, ( byte ) 0x00,
- ( byte ) 0x72, ( byte ) 0x00, ( byte ) 0x79, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x16, ( byte ) 0x00, ( byte ) 0x05, ( byte ) 0x01,
- ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
- ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
- ( byte ) 0x02, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x20, ( byte ) 0x08, ( byte ) 0x02, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0xC0, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x46,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0xC0, ( byte ) 0x5C, ( byte ) 0xE8, ( byte ) 0x23,
- ( byte ) 0x9E, ( byte ) 0x6B, ( byte ) 0xC1, ( byte ) 0x01,
- ( byte ) 0xFE, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
- ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00
- };
-
- verifyReadingProperty(0, input, 0, "Root Entry", "{00020820-0000-0000-C000-000000000046}");
- }
-
- private void verifyReadingProperty(int index, byte [] input, int offset,
- String name, String sClsId)
- throws IOException
- {
- RootProperty property = new RootProperty(index, input,
- offset);
- ByteArrayOutputStream stream = new ByteArrayOutputStream(128);
- byte[] expected = new byte[ 128 ];
-
- System.arraycopy(input, offset, expected, 0, 128);
- property.writeData(stream);
- byte[] output = stream.toByteArray();
-
- assertEquals(128, output.length);
- for (int j = 0; j < 128; j++)
- {
- assertEquals("mismatch at offset " + j, expected[ j ],
- output[ j ]);
- }
- assertEquals(index, property.getIndex());
- assertEquals(name, property.getName());
- assertTrue(!property.getChildren().hasNext());
- assertEquals(property.getStorageClsid().toString(), sClsId);
- }
-
- /**
- * main method to run the unit tests
- *
- * @param ignored_args
- */
-
- public static void main(String [] ignored_args)
- {
- System.out
- .println("Testing org.apache.poi.poifs.property.RootProperty");
- junit.textui.TestRunner.run(TestRootProperty.class);
- }
+public final class TestRootProperty extends TestCase {
+ private RootProperty _property;
+ private byte[] _testblock;
+
+ public void testConstructor() throws IOException {
+ createBasicRootProperty();
+ verifyProperty();
+ }
+
+ private void createBasicRootProperty() {
+ _property = new RootProperty();
+ _testblock = new byte[128];
+ int index = 0;
+
+ for (; index < 0x40; index++) {
+ _testblock[index] = (byte) 0;
+ }
+ String name = "Root Entry";
+ int limit = Math.min(31, name.length());
+
+ _testblock[index++] = (byte) (2 * (limit + 1));
+ _testblock[index++] = (byte) 0;
+ _testblock[index++] = (byte) 5;
+ _testblock[index++] = (byte) 1;
+ for (; index < 0x50; index++) {
+ _testblock[index] = (byte) 0xff;
+ }
+ for (; index < 0x74; index++) {
+ _testblock[index] = (byte) 0;
+ }
+ _testblock[index++] = (byte) POIFSConstants.END_OF_CHAIN;
+ for (; index < 0x78; index++) {
+ _testblock[index] = (byte) 0xff;
+ }
+ for (; index < 0x80; index++) {
+ _testblock[index] = (byte) 0;
+ }
+ byte[] name_bytes = name.getBytes();
+
+ for (index = 0; index < limit; index++) {
+ _testblock[index * 2] = name_bytes[index];
+ }
+ }
+
+ private void verifyProperty() throws IOException {
+ ByteArrayOutputStream stream = new ByteArrayOutputStream(512);
+
+ _property.writeData(stream);
+ byte[] output = stream.toByteArray();
+
+ assertEquals(_testblock.length, output.length);
+ for (int j = 0; j < _testblock.length; j++) {
+ assertEquals("mismatch at offset " + j, _testblock[j], output[j]);
+ }
+ }
+
+ public void testSetSize() {
+ for (int j = 0; j < 10; j++) {
+ createBasicRootProperty();
+ _property.setSize(j);
+ assertEquals("trying block count of " + j, j * 64, _property.getSize());
+ }
+ }
+
+ public void testReadingConstructor() {
+ String[] input = {
+ "52 00 6F 00 6F 00 74 00 20 00 45 00 6E 00 74 00 72 00 79 00 00 00 00 00 00 00 00 00 00 00 00 00",
+ "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
+ "16 00 05 01 FF FF FF FF FF FF FF FF 02 00 00 00 20 08 02 00 00 00 00 00 C0 00 00 00 00 00 00 46",
+ "00 00 00 00 00 00 00 00 00 00 00 00 C0 5C E8 23 9E 6B C1 01 FE FF FF FF 00 00 00 00 00 00 00 00",
+ };
+ verifyReadingProperty(0, RawDataUtil.decode(input), 0, "Root Entry",
+ "{00020820-0000-0000-C000-000000000046}");
+ }
+
+ private void verifyReadingProperty(int index, byte[] input, int offset, String name,
+ String sClsId) {
+ RootProperty property = new RootProperty(index, input, offset);
+ ByteArrayOutputStream stream = new ByteArrayOutputStream(128);
+ byte[] expected = new byte[128];
+
+ System.arraycopy(input, offset, expected, 0, 128);
+ try {
+ property.writeData(stream);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ byte[] output = stream.toByteArray();
+
+ assertEquals(128, output.length);
+ for (int j = 0; j < 128; j++) {
+ assertEquals("mismatch at offset " + j, expected[j], output[j]);
+ }
+ assertEquals(index, property.getIndex());
+ assertEquals(name, property.getName());
+ assertTrue(!property.getChildren().hasNext());
+ assertEquals(property.getStorageClsid().toString(), sClsId);
+ }
}
Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java Thu Dec 30 02:35:06 2010
@@ -26,22 +26,22 @@ import junit.framework.TestSuite;
*/
public final class AllPOIFSStorageTests {
- public static Test suite() {
- TestSuite result = new TestSuite("Tests for org.apache.poi.poifs.storage");
- result.addTestSuite(TestBATBlock.class);
- result.addTestSuite(TestBlockAllocationTableReader.class);
- result.addTestSuite(TestBlockAllocationTableWriter.class);
- result.addTestSuite(TestBlockListImpl.class);
- result.addTestSuite(TestDocumentBlock.class);
- result.addTestSuite(TestHeaderBlockReader.class);
- result.addTestSuite(TestHeaderBlockWriter.class);
- result.addTestSuite(TestPropertyBlock.class);
- result.addTestSuite(TestRawDataBlock.class);
- result.addTestSuite(TestRawDataBlockList.class);
- result.addTestSuite(TestSmallBlockTableReader.class);
- result.addTestSuite(TestSmallBlockTableWriter.class);
- result.addTestSuite(TestSmallDocumentBlock.class);
- result.addTestSuite(TestSmallDocumentBlockList.class);
- return result;
- }
+ public static Test suite() {
+ TestSuite result = new TestSuite(AllPOIFSStorageTests.class.getName());
+ result.addTestSuite(TestBATBlock.class);
+ result.addTestSuite(TestBlockAllocationTableReader.class);
+ result.addTestSuite(TestBlockAllocationTableWriter.class);
+ result.addTestSuite(TestBlockListImpl.class);
+ result.addTestSuite(TestDocumentBlock.class);
+ result.addTestSuite(TestHeaderBlockReading.class);
+ result.addTestSuite(TestHeaderBlockWriting.class);
+ result.addTestSuite(TestPropertyBlock.class);
+ result.addTestSuite(TestRawDataBlock.class);
+ result.addTestSuite(TestRawDataBlockList.class);
+ result.addTestSuite(TestSmallBlockTableReader.class);
+ result.addTestSuite(TestSmallBlockTableWriter.class);
+ result.addTestSuite(TestSmallDocumentBlock.class);
+ result.addTestSuite(TestSmallDocumentBlockList.class);
+ return result;
+ }
}
Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/LocalRawDataBlockList.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/LocalRawDataBlockList.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/LocalRawDataBlockList.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/LocalRawDataBlockList.java Thu Dec 30 02:35:06 2010
@@ -1,4 +1,3 @@
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@@ -15,42 +14,32 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
package org.apache.poi.poifs.storage;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
import org.apache.poi.poifs.common.POIFSConstants;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
-import java.io.*;
-
-import java.util.*;
-
/**
* Class LocalRawDataBlockList
*
* @author Marc Johnson(mjohnson at apache dot org)
*/
-
-public class LocalRawDataBlockList
- extends RawDataBlockList
-{
- private List _list;
+public final class LocalRawDataBlockList extends RawDataBlockList {
+ private List<RawDataBlock> _list;
private RawDataBlock[] _array;
- /**
- * Constructor LocalRawDataBlockList
- *
- * @exception IOException
- */
-
public LocalRawDataBlockList()
throws IOException
{
- super(new ByteArrayInputStream(new byte[ 0 ]), POIFSConstants.BIG_BLOCK_SIZE);
- _list = new ArrayList();
+ super(new ByteArrayInputStream(new byte[ 0 ]), POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
+ _list = new ArrayList<RawDataBlock>();
_array = null;
}
@@ -60,10 +49,7 @@ public class LocalRawDataBlockList
* @param start index of first BAT block
* @param end index of last BAT block
* @param chain index of next XBAT block
- *
- * @exception IOException
*/
-
public void createNewXBATBlock(final int start, final int end,
final int chain)
throws IOException
@@ -89,10 +75,7 @@ public class LocalRawDataBlockList
* create a BAT block and add it to the list
*
* @param start_index initial index for the block list
- *
- * @exception IOException
*/
-
public void createNewBATBlock(final int start_index)
throws IOException
{
@@ -124,10 +107,7 @@ public class LocalRawDataBlockList
* fill the list with dummy blocks
*
* @param count of blocks
- *
- * @exception IOException
*/
-
public void fill(final int count)
throws IOException
{
@@ -144,7 +124,6 @@ public class LocalRawDataBlockList
*
* @param block new block to add
*/
-
public void add(RawDataBlock block)
{
_list.add(block);
@@ -156,10 +135,7 @@ public class LocalRawDataBlockList
* @param index of block to be removed
*
* @return desired block
- *
- * @exception IOException
*/
-
public ListManagedBlock remove(final int index)
throws IOException
{
@@ -189,7 +165,6 @@ public class LocalRawDataBlockList
* @param index the index of the specified block; if the index is
* out of range, that's ok
*/
-
public void zap(final int index)
{
ensureArrayExists();
@@ -203,7 +178,11 @@ public class LocalRawDataBlockList
{
if (_array == null)
{
- _array = ( RawDataBlock [] ) _list.toArray(new RawDataBlock[ 0 ]);
+ _array = _list.toArray(new RawDataBlock[ 0 ]);
}
}
+
+ public int blockCount() {
+ return _list.size();
+ }
}
Added: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/RawDataUtil.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/RawDataUtil.java?rev=1053791&view=auto
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/RawDataUtil.java (added)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/RawDataUtil.java Thu Dec 30 02:35:06 2010
@@ -0,0 +1,86 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.poifs.storage;
+
+import java.io.ByteArrayOutputStream;
+import java.util.Arrays;
+
+import org.apache.poi.util.HexDump;
+import org.apache.poi.util.HexRead;
+
+/**
+ * Test utility class.<br/>
+ *
+ * Creates raw <code>byte[]</code> data from hex-dump String arrays.
+ *
+ * @author Josh Micich
+ */
+public final class RawDataUtil {
+
+ public static byte[] decode(String[] hexDataLines) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(hexDataLines.length * 32 + 32);
+
+ for (int i = 0; i < hexDataLines.length; i++) {
+ byte[] lineData = HexRead.readFromString(hexDataLines[i]);
+ baos.write(lineData, 0, lineData.length);
+ }
+ return baos.toByteArray();
+ }
+
+ /**
+ * Development time utility method.<br/>
+ * Transforms a byte array into hex-dump String lines in java source code format.
+ */
+ public static void dumpData(byte[] data) {
+ int i=0;
+ System.out.println("String[] hexDataLines = {");
+ System.out.print("\t\"");
+ while(true) {
+ char[] cc = HexDump.byteToHex(data[i]);
+ System.out.print(cc[2]);
+ System.out.print(cc[3]);
+ i++;
+ if (i>=data.length) {
+ break;
+ }
+ if (i % 32 == 0) {
+ System.out.println("\",");
+ System.out.print("\t\"");
+ } else {
+ System.out.print(" ");
+ }
+ }
+ System.out.println("\",");
+ System.out.println("};");
+ }
+
+ /**
+ * Development time utility method.<br/>
+ * Confirms that the specified byte array is equivalent to the hex-dump String lines.
+ */
+ public static void confirmEqual(byte[] expected, String[] hexDataLines) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(hexDataLines.length * 32 + 32);
+
+ for (int i = 0; i < hexDataLines.length; i++) {
+ byte[] lineData = HexRead.readFromString(hexDataLines[i]);
+ baos.write(lineData, 0, lineData.length);
+ }
+ if (!Arrays.equals(expected, baos.toByteArray())) {
+ throw new RuntimeException("different");
+ }
+ }
+}
Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/TestBATBlock.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/TestBATBlock.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/TestBATBlock.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/storage/TestBATBlock.java Thu Dec 30 02:35:06 2010
@@ -1,4 +1,3 @@
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@@ -15,78 +14,62 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
package org.apache.poi.poifs.storage;
-import java.io.*;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
-import java.util.*;
+import org.apache.poi.poifs.common.POIFSConstants;
-import junit.framework.*;
+import junit.framework.TestCase;
/**
* Class to test BATBlock functionality
*
* @author Marc Johnson
*/
-
-public class TestBATBlock
- extends TestCase
-{
-
- /**
- * Constructor TestBATBlock
- *
- * @param name
- */
-
- public TestBATBlock(String name)
- {
- super(name);
- }
+public final class TestBATBlock extends TestCase {
/**
* Test the createBATBlocks method. The test involves setting up
* various arrays of int's and ensuring that the correct number of
* BATBlocks is created for each array, and that the data from
* each array is correctly written to the BATBlocks.
- *
- * @exception IOException
*/
-
- public void testCreateBATBlocks()
- throws IOException
- {
+ public void testCreateBATBlocks() throws IOException {
// test 0 length array (basic sanity)
- BATBlock[] rvalue = BATBlock.createBATBlocks(createTestArray(0));
+ BATBlock[] rvalue = BATBlock.createBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(0));
assertEquals(0, rvalue.length);
// test array of length 1
- rvalue = BATBlock.createBATBlocks(createTestArray(1));
+ rvalue = BATBlock.createBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(1));
assertEquals(1, rvalue.length);
verifyContents(rvalue, 1);
// test array of length 127
- rvalue = BATBlock.createBATBlocks(createTestArray(127));
+ rvalue = BATBlock.createBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(127));
assertEquals(1, rvalue.length);
verifyContents(rvalue, 127);
// test array of length 128
- rvalue = BATBlock.createBATBlocks(createTestArray(128));
+ rvalue = BATBlock.createBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(128));
assertEquals(1, rvalue.length);
verifyContents(rvalue, 128);
// test array of length 129
- rvalue = BATBlock.createBATBlocks(createTestArray(129));
+ rvalue = BATBlock.createBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(129));
assertEquals(2, rvalue.length);
verifyContents(rvalue, 129);
}
- private int [] createTestArray(int count)
- {
+ private static int[] createTestArray(int count) {
int[] rvalue = new int[ count ];
for (int j = 0; j < count; j++)
@@ -96,9 +79,7 @@ public class TestBATBlock
return rvalue;
}
- private void verifyContents(BATBlock [] blocks, int entries)
- throws IOException
- {
+ private static void verifyContents(BATBlock[] blocks, int entries) throws IOException {
byte[] expected = new byte[ 512 * blocks.length ];
Arrays.fill(expected, ( byte ) 0xFF);
@@ -127,51 +108,40 @@ public class TestBATBlock
}
}
- /**
- * test createXBATBlocks
- *
- * @exception IOException
- */
-
- public void testCreateXBATBlocks()
- throws IOException
- {
-
+ public void testCreateXBATBlocks() throws IOException {
// test 0 length array (basic sanity)
- BATBlock[] rvalue = BATBlock.createXBATBlocks(createTestArray(0), 1);
+ BATBlock[] rvalue = BATBlock.createXBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(0), 1);
assertEquals(0, rvalue.length);
// test array of length 1
- rvalue = BATBlock.createXBATBlocks(createTestArray(1), 1);
+ rvalue = BATBlock.createXBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(1), 1);
assertEquals(1, rvalue.length);
verifyXBATContents(rvalue, 1, 1);
// test array of length 127
- rvalue = BATBlock.createXBATBlocks(createTestArray(127), 1);
+ rvalue = BATBlock.createXBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(127), 1);
assertEquals(1, rvalue.length);
verifyXBATContents(rvalue, 127, 1);
// test array of length 128
- rvalue = BATBlock.createXBATBlocks(createTestArray(128), 1);
+ rvalue = BATBlock.createXBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(128), 1);
assertEquals(2, rvalue.length);
verifyXBATContents(rvalue, 128, 1);
// test array of length 254
- rvalue = BATBlock.createXBATBlocks(createTestArray(254), 1);
+ rvalue = BATBlock.createXBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(254), 1);
assertEquals(2, rvalue.length);
verifyXBATContents(rvalue, 254, 1);
// test array of length 255
- rvalue = BATBlock.createXBATBlocks(createTestArray(255), 1);
+ rvalue = BATBlock.createXBATBlocks(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, createTestArray(255), 1);
assertEquals(3, rvalue.length);
verifyXBATContents(rvalue, 255, 1);
}
- private void verifyXBATContents(BATBlock [] blocks, int entries,
- int start_block)
- throws IOException
- {
+ private static void verifyXBATContents(BATBlock[] blocks, int entries, int start_block)
+ throws IOException {
byte[] expected = new byte[ 512 * blocks.length ];
Arrays.fill(expected, ( byte ) 0xFF);
@@ -220,65 +190,197 @@ public class TestBATBlock
}
}
- /**
- * test calculateXBATStorageRequirements
- */
-
- public void testCalculateXBATStorageRequirements()
- {
- int[] blockCounts =
- {
- 0, 1, 127, 128
- };
- int[] requirements =
- {
- 0, 1, 1, 2
- };
+ public void testCalculateXBATStorageRequirements() {
+ int[] blockCounts = { 0, 1, 127, 128 };
+ int[] requirements = { 0, 1, 1, 2 };
for (int j = 0; j < blockCounts.length; j++)
{
assertEquals(
"requirement for " + blockCounts[ j ], requirements[ j ],
- BATBlock.calculateXBATStorageRequirements(blockCounts[ j ]));
+ BATBlock.calculateXBATStorageRequirements(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, blockCounts[ j ]));
}
}
- /**
- * test entriesPerBlock
- */
-
- public void testEntriesPerBlock()
- {
- assertEquals(128, BATBlock.entriesPerBlock());
+ public void testEntriesPerBlock() {
+ assertEquals(128, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS.getBATEntriesPerBlock());
}
-
- /**
- * test entriesPerXBATBlock
- */
-
- public void testEntriesPerXBATBlock()
- {
- assertEquals(127, BATBlock.entriesPerXBATBlock());
+ public void testEntriesPerXBATBlock() {
+ assertEquals(127, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS.getXBATEntriesPerBlock());
}
-
- /**
- * test getXBATChainOffset
- */
-
- public void testGetXBATChainOffset()
- {
- assertEquals(508, BATBlock.getXBATChainOffset());
+ public void testGetXBATChainOffset() {
+ assertEquals(508, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS.getNextXBATChainOffset());
}
-
- /**
- * main method to run the unit tests
- *
- * @param ignored_args
- */
-
- public static void main(String [] ignored_args)
- {
- System.out.println("Testing org.apache.poi.poifs.storage.BATBlock");
- junit.textui.TestRunner.run(TestBATBlock.class);
+
+ public void testCalculateMaximumSize() throws Exception {
+ // Zero fat blocks isn't technically valid, but it'd be header only
+ assertEquals(
+ 512,
+ BATBlock.calculateMaximumSize(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, 0)
+ );
+ assertEquals(
+ 4096,
+ BATBlock.calculateMaximumSize(POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS, 0)
+ );
+
+ // A single FAT block can address 128/1024 blocks
+ assertEquals(
+ 512 + 512*128,
+ BATBlock.calculateMaximumSize(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, 1)
+ );
+ assertEquals(
+ 4096 + 4096*1024,
+ BATBlock.calculateMaximumSize(POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS, 1)
+ );
+
+ assertEquals(
+ 512 + 4*512*128,
+ BATBlock.calculateMaximumSize(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, 4)
+ );
+ assertEquals(
+ 4096 + 4*4096*1024,
+ BATBlock.calculateMaximumSize(POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS, 4)
+ );
+
+ // One XBAT block holds 127/1023 individual BAT blocks, so they can address
+ // a fairly hefty amount of space themselves
+ // However, the BATs continue as before
+ assertEquals(
+ 512 + 109*512*128,
+ BATBlock.calculateMaximumSize(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, 109)
+ );
+ assertEquals(
+ 4096 + 109*4096*1024,
+ BATBlock.calculateMaximumSize(POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS, 109)
+ );
+
+ assertEquals(
+ 512 + 110*512*128,
+ BATBlock.calculateMaximumSize(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, 110)
+ );
+ assertEquals(
+ 4096 + 110*4096*1024,
+ BATBlock.calculateMaximumSize(POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS, 110)
+ );
+
+ assertEquals(
+ 512 + 112*512*128,
+ BATBlock.calculateMaximumSize(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, 112)
+ );
+ assertEquals(
+ 4096 + 112*4096*1024,
+ BATBlock.calculateMaximumSize(POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS, 112)
+ );
+ }
+
+ public void testGetBATBlockAndIndex() throws Exception {
+ HeaderBlock header = new HeaderBlock(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
+ List<BATBlock> blocks = new ArrayList<BATBlock>();
+ int offset;
+
+
+ // First, try a one BAT block file
+ header.setBATCount(1);
+ blocks.add(
+ BATBlock.createBATBlock(header.getBigBlockSize(), ByteBuffer.allocate(512))
+ );
+
+ offset = 0;
+ assertEquals(0, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 1;
+ assertEquals(1, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 127;
+ assertEquals(127, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+
+ // Now go for one with multiple BAT blocks
+ header.setBATCount(2);
+ blocks.add(
+ BATBlock.createBATBlock(header.getBigBlockSize(), ByteBuffer.allocate(512))
+ );
+
+ offset = 0;
+ assertEquals(0, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 127;
+ assertEquals(127, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 128;
+ assertEquals(0, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(1, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 129;
+ assertEquals(1, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(1, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+
+ // The XBAT count makes no difference, as we flatten in memory
+ header.setBATCount(1);
+ header.setXBATCount(1);
+ offset = 0;
+ assertEquals(0, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 126;
+ assertEquals(126, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 127;
+ assertEquals(127, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 128;
+ assertEquals(0, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(1, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 129;
+ assertEquals(1, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(1, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+
+ // Check with the bigger block size too
+ header = new HeaderBlock(POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS);
+
+ offset = 0;
+ assertEquals(0, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 1022;
+ assertEquals(1022, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 1023;
+ assertEquals(1023, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 1024;
+ assertEquals(0, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(1, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ // Biggr block size, back to real BATs
+ header.setBATCount(2);
+
+ offset = 0;
+ assertEquals(0, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 1022;
+ assertEquals(1022, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 1023;
+ assertEquals(1023, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(0, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
+
+ offset = 1024;
+ assertEquals(0, BATBlock.getBATBlockAndIndex(offset, header, blocks).getIndex());
+ assertEquals(1, blocks.indexOf( BATBlock.getBATBlockAndIndex(offset, header, blocks).getBlock() ));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org