You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2009/06/13 02:13:09 UTC

svn commit: r784316 [4/8] - in /poi/trunk/src/testcases/org/apache/poi/poifs: property/ storage/

Modified: poi/trunk/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java?rev=784316&r1=784315&r2=784316&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java Sat Jun 13 00:13:09 2009
@@ -23,6 +23,7 @@
 import junit.framework.TestCase;
 
 import org.apache.poi.poifs.common.POIFSConstants;
+import org.apache.poi.poifs.storage.RawDataUtil;
 
 /**
  * Class to test RootProperty functionality
@@ -30,141 +31,101 @@
  * @author Marc Johnson
  */
 public final class TestRootProperty extends TestCase {
-    private RootProperty _property;
-    private byte[]       _testblock;
+	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() 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);
-    }
+	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);
+	}
 }

Added: poi/trunk/src/testcases/org/apache/poi/poifs/storage/RawDataUtil.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/storage/RawDataUtil.java?rev=784316&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/storage/RawDataUtil.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/storage/RawDataUtil.java Sat Jun 13 00:13:09 2009
@@ -0,0 +1,70 @@
+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");
+		}
+	}
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org