diff options
author | Andrew C. Oliver <acoliver@apache.org> | 2002-01-31 02:22:28 +0000 |
---|---|---|
committer | Andrew C. Oliver <acoliver@apache.org> | 2002-01-31 02:22:28 +0000 |
commit | 833d29b9e0dac737fb1608740c7582b54d462b5c (patch) | |
tree | 38def28b2ff11d77cb76b19b3ca5fba154ed61fa /src/testcases/org/apache/poi/poifs/filesystem | |
parent | 6c234ab6c8d9b392bc93f28edf13136b3c053894 (diff) | |
download | poi-833d29b9e0dac737fb1608740c7582b54d462b5c.tar.gz poi-833d29b9e0dac737fb1608740c7582b54d462b5c.zip |
Initial revision
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352063 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/poifs/filesystem')
8 files changed, 2321 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java b/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java new file mode 100644 index 0000000000..0ed493d64f --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java @@ -0,0 +1,224 @@ + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache POI" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache POI", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + */ + +package org.apache.poi.poifs.filesystem; + +import java.util.*; + +import java.io.*; + +import org.apache.poi.poifs.eventfilesystem.POIFSReader; +import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent; +import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener; + +/** + * Test (Proof of concept) program that employs the + * POIFSReaderListener and POIFSWriterListener interfaces + * + * @author Marc Johnson (mjohnson at apache dot org) + */ + +public class ReaderWriter + implements POIFSReaderListener, POIFSWriterListener +{ + private POIFSFileSystem filesystem; + private DirectoryEntry root; + + // keys are DocumentDescriptors, values are byte[]s + private Map dataMap; + + /** + * Constructor ReaderWriter + * + * + * @param filesystem + * + */ + + ReaderWriter(final POIFSFileSystem filesystem) + { + this.filesystem = filesystem; + root = this.filesystem.getRoot(); + dataMap = new HashMap(); + } + + /** + * Method main + * + * + * @param args + * + * @exception IOException + * + */ + + public static void main(String [] args) + throws IOException + { + if (args.length != 2) + { + System.err.println( + "two arguments required: one input file name and one output file name"); + } + else + { + POIFSReader reader = new POIFSReader(); + POIFSFileSystem filesystem = new POIFSFileSystem(); + + reader.registerListener(new ReaderWriter(filesystem)); + FileInputStream istream = new FileInputStream(args[ 0 ]); + + reader.read(istream); + istream.close(); + FileOutputStream ostream = new FileOutputStream(args[ 1 ]); + + filesystem.writeFilesystem(ostream); + ostream.close(); + } + } + + /* ********** START implementation of POIFSReaderListener ********** */ + + /** + * Process a POIFSReaderEvent that this listener had registered + * for + * + * @param event the POIFSReaderEvent + */ + + public void processPOIFSReaderEvent(final POIFSReaderEvent event) + { + DocumentInputStream istream = event.getStream(); + POIFSDocumentPath path = event.getPath(); + String name = event.getName(); + + try + { + int size = istream.available(); + byte[] data = new byte[ istream.available() ]; + + istream.read(data); + DocumentDescriptor descriptor = new DocumentDescriptor(path, + name); + + System.out.println("adding document: " + descriptor + " (" + size + + " bytes)"); + dataMap.put(descriptor, data); + int pathLength = path.length(); + DirectoryEntry entry = root; + + for (int k = 0; k < path.length(); k++) + { + String componentName = path.getComponent(k); + Entry nextEntry = null; + + try + { + nextEntry = entry.getEntry(componentName); + } + catch (FileNotFoundException ignored) + { + try + { + nextEntry = entry.createDirectory(componentName); + } + catch (IOException e) + { + System.out.println("Unable to create directory"); + e.printStackTrace(); + System.exit(1); + } + } + entry = ( DirectoryEntry ) nextEntry; + } + entry.createDocument(name, size, this); + } + catch (IOException ignored) + { + } + } + + /* ********** END implementation of POIFSReaderListener ********** */ + /* ********** START implementation of POIFSWriterListener ********** */ + + /** + * Process a POIFSWriterEvent that this listener had registered + * for + * + * @param event the POIFSWriterEvent + */ + + public void processPOIFSWriterEvent(final POIFSWriterEvent event) + { + try + { + DocumentDescriptor descriptor = + new DocumentDescriptor(event.getPath(), event.getName()); + + System.out.println("looking up document: " + descriptor + " (" + + event.getLimit() + " bytes)"); + event.getStream().write(( byte [] ) dataMap.get(descriptor)); + } + catch (IOException e) + { + System.out.println("Unable to write document"); + e.printStackTrace(); + System.exit(1); + } + } + + /* ********** END implementation of POIFSWriterListener ********** */ +} // end public class ReaderWriter + diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDirectoryNode.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDirectoryNode.java new file mode 100644 index 0000000000..289bf8c04b --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDirectoryNode.java @@ -0,0 +1,289 @@ + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache POI" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache POI", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + */ + +package org.apache.poi.poifs.filesystem; + +import java.io.*; + +import java.util.*; + +import junit.framework.*; + +import org.apache.poi.poifs.property.DirectoryProperty; +import org.apache.poi.poifs.property.DocumentProperty; + +/** + * Class to test DirectoryNode functionality + * + * @author Marc Johnson + */ + +public class TestDirectoryNode + extends TestCase +{ + + /** + * Constructor TestDirectoryNode + * + * @param name + */ + + public TestDirectoryNode(String name) + { + super(name); + } + + /** + * test trivial constructor (a DirectoryNode with no children) + * + * @exception IOException + */ + + public void testEmptyConstructor() + throws IOException + { + POIFSFileSystem fs = new POIFSFileSystem(); + DirectoryProperty property1 = new DirectoryProperty("parent"); + DirectoryProperty property2 = new DirectoryProperty("child"); + DirectoryNode parent = new DirectoryNode(property1, fs, null); + DirectoryNode node = new DirectoryNode(property2, fs, + parent); + + assertEquals(0, parent.getPath().length()); + assertEquals(1, node.getPath().length()); + assertEquals("child", node.getPath().getComponent(0)); + + // verify that getEntries behaves correctly + int count = 0; + Iterator iter = node.getEntries(); + + while (iter.hasNext()) + { + count++; + iter.next(); + } + assertEquals(0, count); + + // verify behavior of isEmpty + assertTrue(node.isEmpty()); + + // verify behavior of getEntryCount + assertEquals(0, node.getEntryCount()); + + // verify behavior of getEntry + try + { + node.getEntry("foo"); + fail("should have caught FileNotFoundException"); + } + catch (FileNotFoundException ignored) + { + + // as expected + } + + // verify behavior of isDirectoryEntry + assertTrue(node.isDirectoryEntry()); + + // verify behavior of getName + assertEquals(property2.getName(), node.getName()); + + // verify behavior of isDocumentEntry + assertTrue(!node.isDocumentEntry()); + + // verify behavior of getParent + assertEquals(parent, node.getParent()); + } + + /** + * test non-trivial constructor (a DirectoryNode with children) + * + * @exception IOException + */ + + public void testNonEmptyConstructor() + throws IOException + { + DirectoryProperty property1 = new DirectoryProperty("parent"); + DirectoryProperty property2 = new DirectoryProperty("child1"); + + property1.addChild(property2); + property1.addChild(new DocumentProperty("child2", 2000)); + property2.addChild(new DocumentProperty("child3", 30000)); + DirectoryNode node = new DirectoryNode(property1, + new POIFSFileSystem(), null); + + // verify that getEntries behaves correctly + int count = 0; + Iterator iter = node.getEntries(); + + while (iter.hasNext()) + { + count++; + iter.next(); + } + assertEquals(2, count); + + // verify behavior of isEmpty + assertTrue(!node.isEmpty()); + + // verify behavior of getEntryCount + assertEquals(2, node.getEntryCount()); + + // verify behavior of getEntry + DirectoryNode child1 = ( DirectoryNode ) node.getEntry("child1"); + + child1.getEntry("child3"); + node.getEntry("child2"); + try + { + node.getEntry("child3"); + fail("should have caught FileNotFoundException"); + } + catch (FileNotFoundException ignored) + { + + // as expected + } + + // verify behavior of isDirectoryEntry + assertTrue(node.isDirectoryEntry()); + + // verify behavior of getName + assertEquals(property1.getName(), node.getName()); + + // verify behavior of isDocumentEntry + assertTrue(!node.isDocumentEntry()); + + // verify behavior of getParent + assertNull(node.getParent()); + } + + /** + * test deletion methods + * + * @exception IOException + */ + + public void testDeletion() + throws IOException + { + POIFSFileSystem fs = new POIFSFileSystem(); + DirectoryEntry root = fs.getRoot(); + + // verify cannot delete the root directory + assertTrue(!root.delete()); + DirectoryEntry dir = fs.createDirectory("myDir"); + + assertTrue(!root.isEmpty()); + + // verify can delete empty directory + assertTrue(dir.delete()); + dir = fs.createDirectory("NextDir"); + DocumentEntry doc = + dir.createDocument("foo", + new ByteArrayInputStream(new byte[ 1 ])); + + assertTrue(!dir.isEmpty()); + + // verify cannot delete empty directory + assertTrue(!dir.delete()); + assertTrue(doc.delete()); + + // verify now we can delete it + assertTrue(dir.delete()); + assertTrue(root.isEmpty()); + } + + /** + * test change name methods + * + * @exception IOException + */ + + public void testRename() + throws IOException + { + POIFSFileSystem fs = new POIFSFileSystem(); + DirectoryEntry root = fs.getRoot(); + + // verify cannot rename the root directory + assertTrue(!root.renameTo("foo")); + DirectoryEntry dir = fs.createDirectory("myDir"); + + assertTrue(dir.renameTo("foo")); + assertEquals("foo", dir.getName()); + DirectoryEntry dir2 = fs.createDirectory("myDir"); + + assertTrue(!dir2.renameTo("foo")); + assertEquals("myDir", dir2.getName()); + assertTrue(dir.renameTo("FirstDir")); + assertTrue(dir2.renameTo("foo")); + assertEquals("foo", dir2.getName()); + } + + /** + * 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.filesystem.DirectoryNode"); + junit.textui.TestRunner.run(TestDirectoryNode.class); + } +} diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java new file mode 100644 index 0000000000..522b3ed7e0 --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java @@ -0,0 +1,319 @@ + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache POI" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache POI", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + */ + +package org.apache.poi.poifs.filesystem; + +import java.io.*; + +import java.util.*; + +import junit.framework.*; + +import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.LittleEndianConsts; +import org.apache.poi.poifs.property.DocumentProperty; +import org.apache.poi.poifs.storage.RawDataBlock; +import org.apache.poi.poifs.storage.SmallDocumentBlock; + +/** + * Class to test POIFSDocument functionality + * + * @author Marc Johnson + */ + +public class TestDocument + extends TestCase +{ + + /** + * Constructor TestDocument + * + * @param name + */ + + public TestDocument(String name) + { + super(name); + } + + /** + * Integration test -- really about all we can do + * + * @exception IOException + */ + + public void testPOIFSDocument() + throws IOException + { + + // verify correct number of blocks get created for document + // that is exact multituple of block size + POIFSDocument document; + byte[] array = new byte[ 4096 ]; + + for (int j = 0; j < array.length; j++) + { + array[ j ] = ( byte ) j; + } + document = new POIFSDocument("foo", new ByteArrayInputStream(array)); + checkDocument(document, array); + + // verify correct number of blocks get created for document + // that is not an exact multiple of block size + array = new byte[ 4097 ]; + for (int j = 0; j < array.length; j++) + { + array[ j ] = ( byte ) j; + } + document = new POIFSDocument("bar", new ByteArrayInputStream(array)); + checkDocument(document, array); + + // verify correct number of blocks get created for document + // that is small + array = new byte[ 4095 ]; + for (int j = 0; j < array.length; j++) + { + array[ j ] = ( byte ) j; + } + document = new POIFSDocument("_bar", new ByteArrayInputStream(array)); + checkDocument(document, array); + + // verify correct number of blocks get created for document + // that is rather small + array = new byte[ 199 ]; + for (int j = 0; j < array.length; j++) + { + array[ j ] = ( byte ) j; + } + document = new POIFSDocument("_bar2", + new ByteArrayInputStream(array)); + checkDocument(document, array); + + // verify that output is correct + array = new byte[ 4097 ]; + for (int j = 0; j < array.length; j++) + { + array[ j ] = ( byte ) j; + } + document = new POIFSDocument("foobar", + new ByteArrayInputStream(array)); + checkDocument(document, array); + document.setStartBlock(0x12345678); // what a big file!! + DocumentProperty property = document.getDocumentProperty(); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + + property.writeData(stream); + byte[] output = stream.toByteArray(); + byte[] array2 = + { + ( byte ) 'f', ( byte ) 0, ( byte ) 'o', ( byte ) 0, ( byte ) 'o', + ( byte ) 0, ( byte ) 'b', ( byte ) 0, ( byte ) 'a', ( byte ) 0, + ( byte ) 'r', ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 14, + ( byte ) 0, ( byte ) 2, ( byte ) 1, ( byte ) -1, ( byte ) -1, + ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1, + ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0x78, ( byte ) 0x56, ( byte ) 0x34, + ( byte ) 0x12, ( byte ) 1, ( byte ) 16, ( byte ) 0, ( byte ) 0, + ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0 + }; + + assertEquals(array2.length, output.length); + for (int j = 0; j < output.length; j++) + { + assertEquals("Checking property offset " + j, array2[ j ], + output[ j ]); + } + } + + private POIFSDocument makeCopy(POIFSDocument document, byte [] input, + byte [] data) + throws IOException + { + POIFSDocument copy = null; + + if (input.length >= 4096) + { + RawDataBlock[] blocks = + new RawDataBlock[ (input.length + 511) / 512 ]; + ByteArrayInputStream stream = new ByteArrayInputStream(data); + int index = 0; + + while (true) + { + RawDataBlock block = new RawDataBlock(stream); + + if (block.eof()) + { + break; + } + blocks[ index++ ] = block; + } + copy = new POIFSDocument("test" + input.length, blocks, + input.length); + } + else + { + copy = new POIFSDocument( + "test" + input.length, + ( SmallDocumentBlock [] ) document.getSmallBlocks(), + input.length); + } + return copy; + } + + private void checkDocument(final POIFSDocument document, + final byte [] input) + throws IOException + { + int big_blocks = 0; + int small_blocks = 0; + int total_output = 0; + + if (input.length >= 4096) + { + big_blocks = (input.length + 511) / 512; + total_output = big_blocks * 512; + } + else + { + small_blocks = (input.length + 63) / 64; + total_output = 0; + } + checkValues( + big_blocks, small_blocks, total_output, + makeCopy( + document, input, + checkValues( + big_blocks, small_blocks, total_output, document, + input)), input); + } + + private byte [] checkValues(int big_blocks, int small_blocks, + int total_output, POIFSDocument document, + byte [] input) + throws IOException + { + assertEquals(document, document.getDocumentProperty().getDocument()); + int increment = ( int ) Math.sqrt(input.length); + + for (int j = 1; j <= input.length; j += increment) + { + byte[] buffer = new byte[ j ]; + int offset = 0; + + for (int k = 0; k < (input.length / j); k++) + { + document.read(buffer, offset); + for (int n = 0; n < buffer.length; n++) + { + assertEquals("checking byte " + (k * j) + n, + input[ (k * j) + n ], buffer[ n ]); + } + offset += j; + } + } + assertEquals(big_blocks, document.countBlocks()); + assertEquals(small_blocks, document.getSmallBlocks().length); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + + document.writeBlocks(stream); + byte[] output = stream.toByteArray(); + + assertEquals(total_output, output.length); + int limit = Math.min(total_output, input.length); + + for (int j = 0; j < limit; j++) + { + assertEquals("Checking document offset " + j, input[ j ], + output[ j ]); + } + for (int j = limit; j < output.length; j++) + { + assertEquals("Checking document offset " + j, ( byte ) -1, + output[ j ]); + } + return output; + } + + /** + * 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.filesystem.POIFSDocument"); + junit.textui.TestRunner.run(TestDocument.class); + } +} diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentDescriptor.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentDescriptor.java new file mode 100644 index 0000000000..3dbcb1be1e --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentDescriptor.java @@ -0,0 +1,248 @@ + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache POI" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache POI", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + */ + +package org.apache.poi.poifs.filesystem; + +import junit.framework.*; + +/** + * Class to test DocumentDescriptor functionality + * + * @author Marc Johnson + */ + +public class TestDocumentDescriptor + extends TestCase +{ + + /** + * Constructor TestDocumentDescriptor + * + * @param name + */ + + public TestDocumentDescriptor(String name) + { + super(name); + } + + /** + * test equality + */ + + public void testEquality() + { + String[] names = + { + "c1", "c2", "c3", "c4", "c5" + }; + POIFSDocumentPath a1 = new POIFSDocumentPath(); + POIFSDocumentPath a2 = new POIFSDocumentPath(null); + POIFSDocumentPath a3 = new POIFSDocumentPath(new String[ 0 ]); + POIFSDocumentPath a4 = new POIFSDocumentPath(a1, null); + POIFSDocumentPath a5 = new POIFSDocumentPath(a1, + new String[ 0 ]); + POIFSDocumentPath[] paths = + { + a1, a2, a3, a4, a5 + }; + + for (int j = 0; j < paths.length; j++) + { + for (int k = 0; k < paths.length; k++) + { + for (int m = 0; m < names.length; m++) + { + DocumentDescriptor d1 = new DocumentDescriptor(paths[ j ], + names[ m ]); + + for (int n = 0; n < names.length; n++) + { + DocumentDescriptor d2 = + new DocumentDescriptor(paths[ k ], names[ n ]); + + if (m == n) + { + assertEquals("" + j + "," + k + "," + m + "," + + n, d1, d2); + } + else + { + assertTrue("" + j + "," + k + "," + m + "," + n, + !d1.equals(d2)); + } + } + } + } + } + a2 = new POIFSDocumentPath(a1, new String[] + { + "foo" + }); + a3 = new POIFSDocumentPath(a2, new String[] + { + "bar" + }); + a4 = new POIFSDocumentPath(a3, new String[] + { + "fubar" + }); + a5 = new POIFSDocumentPath(a4, new String[] + { + "foobar" + }); + POIFSDocumentPath[] builtUpPaths = + { + a1, a2, a3, a4, a5 + }; + POIFSDocumentPath[] fullPaths = + { + new POIFSDocumentPath(), new POIFSDocumentPath(new String[] + { + "foo" + }), new POIFSDocumentPath(new String[] + { + "foo", "bar" + }), new POIFSDocumentPath(new String[] + { + "foo", "bar", "fubar" + }), new POIFSDocumentPath(new String[] + { + "foo", "bar", "fubar", "foobar" + }) + }; + + for (int k = 0; k < builtUpPaths.length; k++) + { + for (int j = 0; j < fullPaths.length; j++) + { + for (int m = 0; m < names.length; m++) + { + DocumentDescriptor d1 = + new DocumentDescriptor(fullPaths[ j ], names[ m ]); + + for (int n = 0; n < names.length; n++) + { + DocumentDescriptor d2 = + new DocumentDescriptor(builtUpPaths[ k ], + names[ n ]); + + if ((k == j) && (m == n)) + { + assertEquals("" + j + "," + k + "," + m + "," + + n, d1, d2); + } + else + { + assertTrue("" + j + "," + k + "," + m + "," + n, + !(d1.equals(d2))); + } + } + } + } + } + POIFSDocumentPath[] badPaths = + { + new POIFSDocumentPath(new String[] + { + "_foo" + }), new POIFSDocumentPath(new String[] + { + "foo", "_bar" + }), new POIFSDocumentPath(new String[] + { + "foo", "bar", "_fubar" + }), new POIFSDocumentPath(new String[] + { + "foo", "bar", "fubar", "_foobar" + }) + }; + + for (int k = 0; k < builtUpPaths.length; k++) + { + for (int j = 0; j < badPaths.length; j++) + { + for (int m = 0; m < names.length; m++) + { + DocumentDescriptor d1 = + new DocumentDescriptor(badPaths[ j ], names[ m ]); + + for (int n = 0; n < names.length; n++) + { + DocumentDescriptor d2 = + new DocumentDescriptor(builtUpPaths[ k ], + names[ n ]); + + assertTrue("" + j + "," + k + "," + m + "," + n, + !(d1.equals(d2))); + } + } + } + } + } + + /** + * 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.eventfilesystem.DocumentDescriptor"); + junit.textui.TestRunner.run(TestDocumentDescriptor.class); + } +} diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java new file mode 100644 index 0000000000..e77fee566a --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java @@ -0,0 +1,472 @@ + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache POI" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache POI", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + */ + +package org.apache.poi.poifs.filesystem; + +import java.io.*; + +import java.util.*; + +import junit.framework.*; + +import org.apache.poi.poifs.property.DirectoryProperty; +import org.apache.poi.poifs.property.DocumentProperty; +import org.apache.poi.poifs.storage.RawDataBlock; + +/** + * Class to test DocumentInputStream functionality + * + * @author Marc Johnson + */ + +public class TestDocumentInputStream + extends TestCase +{ + + /** + * Constructor TestDocumentInputStream + * + * @param name + * + * @exception IOException + */ + + public TestDocumentInputStream(String name) + throws IOException + { + super(name); + int blocks = (_workbook_size + 511) / 512; + + _workbook_data = new byte[ 512 * blocks ]; + Arrays.fill(_workbook_data, ( byte ) -1); + for (int j = 0; j < _workbook_size; j++) + { + _workbook_data[ j ] = ( byte ) (j * j); + } + RawDataBlock[] rawBlocks = new RawDataBlock[ blocks ]; + ByteArrayInputStream stream = + new ByteArrayInputStream(_workbook_data); + + for (int j = 0; j < blocks; j++) + { + rawBlocks[ j ] = new RawDataBlock(stream); + } + POIFSDocument document = new POIFSDocument("Workbook", rawBlocks, + _workbook_size); + + _workbook = new DocumentNode( + document.getDocumentProperty(), + new DirectoryNode( + new DirectoryProperty("Root Entry"), null, null)); + } + + private DocumentNode _workbook; + private byte[] _workbook_data; + private static final int _workbook_size = 5000; + + // non-even division of _workbook_size, also non-even division of + // any block size + private static final int _buffer_size = 6; + + /** + * test constructor + * + * @exception IOException + */ + + public void testConstructor() + throws IOException + { + DocumentInputStream stream = new DocumentInputStream(_workbook); + + assertEquals(_workbook_size, stream.available()); + } + + /** + * test available() behavior + * + * @exception IOException + */ + + public void testAvailable() + throws IOException + { + DocumentInputStream stream = new DocumentInputStream(_workbook); + + assertEquals(_workbook_size, stream.available()); + stream.close(); + try + { + stream.available(); + fail("Should have caught IOException"); + } + catch (IOException ignored) + { + + // as expected + } + } + + /** + * test mark/reset/markSupported. + * + * @exception IOException + */ + + public void testMarkFunctions() + throws IOException + { + DocumentInputStream stream = new DocumentInputStream(_workbook); + byte[] buffer = new byte[ _workbook_size / 5 ]; + + stream.read(buffer); + for (int j = 0; j < buffer.length; j++) + { + assertEquals("checking byte " + j, _workbook_data[ j ], + buffer[ j ]); + } + assertEquals(_workbook_size - buffer.length, stream.available()); + stream.reset(); + assertEquals(_workbook_size, stream.available()); + stream.read(buffer); + stream.mark(12); + stream.read(buffer); + assertEquals(_workbook_size - (2 * buffer.length), + stream.available()); + for (int j = buffer.length; j < (2 * buffer.length); j++) + { + assertEquals("checking byte " + j, _workbook_data[ j ], + buffer[ j - buffer.length ]); + } + stream.reset(); + assertEquals(_workbook_size - buffer.length, stream.available()); + stream.read(buffer); + assertEquals(_workbook_size - (2 * buffer.length), + stream.available()); + for (int j = buffer.length; j < (2 * buffer.length); j++) + { + assertEquals("checking byte " + j, _workbook_data[ j ], + buffer[ j - buffer.length ]); + } + assertTrue(stream.markSupported()); + } + + /** + * test simple read method + * + * @exception IOException + */ + + public void testReadSingleByte() + throws IOException + { + DocumentInputStream stream = new DocumentInputStream(_workbook); + int remaining = _workbook_size; + + for (int j = 0; j < _workbook_size; j++) + { + assertEquals("validating byte " + j, _workbook_data[ j ], + ( byte ) stream.read()); + remaining--; + assertEquals("checking remaining after reading byte " + j, + remaining, stream.available()); + } + assertEquals(-1, stream.read()); + stream.close(); + try + { + stream.read(); + fail("Should have caught IOException"); + } + catch (IOException ignored) + { + + // as expected + } + } + + /** + * Test buffered read + * + * @exception IOException + */ + + public void testBufferRead() + throws IOException + { + DocumentInputStream stream = new DocumentInputStream(_workbook); + + try + { + stream.read(null); + fail("Should have caught NullPointerException"); + } + catch (NullPointerException ignored) + { + + // as expected + } + + // test reading zero length buffer + assertEquals(0, stream.read(new byte[ 0 ])); + assertEquals(_workbook_size, stream.available()); + byte[] buffer = new byte[ _buffer_size ]; + int offset = 0; + + while (stream.available() >= buffer.length) + { + assertEquals(_buffer_size, stream.read(buffer)); + for (int j = 0; j < buffer.length; j++) + { + assertEquals("in main loop, byte " + offset, + _workbook_data[ offset ], buffer[ j ]); + offset++; + } + assertEquals("offset " + offset, _workbook_size - offset, + stream.available()); + } + assertEquals(_workbook_size % _buffer_size, stream.available()); + Arrays.fill(buffer, ( byte ) 0); + int count = stream.read(buffer); + + assertEquals(_workbook_size % _buffer_size, count); + for (int j = 0; j < count; j++) + { + assertEquals("past main loop, byte " + offset, + _workbook_data[ offset ], buffer[ j ]); + offset++; + } + assertEquals(_workbook_size, offset); + for (int j = count; j < buffer.length; j++) + { + assertEquals("checking remainder, byte " + j, 0, buffer[ j ]); + } + assertEquals(-1, stream.read(buffer)); + stream.close(); + try + { + stream.read(buffer); + fail("Should have caught IOException"); + } + catch (IOException ignored) + { + + // as expected + } + } + + /** + * Test complex buffered read + * + * @exception IOException + */ + + public void testComplexBufferRead() + throws IOException + { + DocumentInputStream stream = new DocumentInputStream(_workbook); + + try + { + stream.read(null, 0, 1); + fail("Should have caught NullPointerException"); + } + catch (NullPointerException ignored) + { + + // as expected + } + + // test illegal offsets and lengths + try + { + stream.read(new byte[ 5 ], -4, 0); + fail("Should have caught IndexOutOfBoundsException"); + } + catch (IndexOutOfBoundsException ignored) + { + + // as expected + } + try + { + stream.read(new byte[ 5 ], 0, -4); + fail("Should have caught IndexOutOfBoundsException"); + } + catch (IndexOutOfBoundsException ignored) + { + + // as expected + } + try + { + stream.read(new byte[ 5 ], 0, 6); + fail("Should have caught IndexOutOfBoundsException"); + } + catch (IndexOutOfBoundsException ignored) + { + + // as expected + } + + // test reading zero + assertEquals(0, stream.read(new byte[ 5 ], 0, 0)); + assertEquals(_workbook_size, stream.available()); + byte[] buffer = new byte[ _workbook_size ]; + int offset = 0; + + while (stream.available() >= _buffer_size) + { + Arrays.fill(buffer, ( byte ) 0); + assertEquals(_buffer_size, + stream.read(buffer, offset, _buffer_size)); + for (int j = 0; j < offset; j++) + { + assertEquals("checking byte " + j, 0, buffer[ j ]); + } + for (int j = offset; j < (offset + _buffer_size); j++) + { + assertEquals("checking byte " + j, _workbook_data[ j ], + buffer[ j ]); + } + for (int j = offset + _buffer_size; j < buffer.length; j++) + { + assertEquals("checking byte " + j, 0, buffer[ j ]); + } + offset += _buffer_size; + assertEquals("offset " + offset, _workbook_size - offset, + stream.available()); + } + assertEquals(_workbook_size % _buffer_size, stream.available()); + Arrays.fill(buffer, ( byte ) 0); + int count = stream.read(buffer, offset, + _workbook_size % _buffer_size); + + assertEquals(_workbook_size % _buffer_size, count); + for (int j = 0; j < offset; j++) + { + assertEquals("checking byte " + j, 0, buffer[ j ]); + } + for (int j = offset; j < buffer.length; j++) + { + assertEquals("checking byte " + j, _workbook_data[ j ], + buffer[ j ]); + } + assertEquals(_workbook_size, offset + count); + for (int j = count; j < offset; j++) + { + assertEquals("byte " + j, 0, buffer[ j ]); + } + assertEquals(-1, stream.read(buffer, 0, 1)); + stream.close(); + try + { + stream.read(buffer, 0, 1); + fail("Should have caught IOException"); + } + catch (IOException ignored) + { + + // as expected + } + } + + /** + * test skip + * + * @exception IOException + */ + + public void testSkip() + throws IOException + { + DocumentInputStream stream = new DocumentInputStream(_workbook); + + assertEquals(_workbook_size, stream.available()); + int count = stream.available(); + + while (stream.available() >= _buffer_size) + { + assertEquals(_buffer_size, stream.skip(_buffer_size)); + count -= _buffer_size; + assertEquals(count, stream.available()); + } + assertEquals(_workbook_size % _buffer_size, + stream.skip(_buffer_size)); + assertEquals(0, stream.available()); + stream.reset(); + assertEquals(_workbook_size, stream.available()); + assertEquals(_workbook_size, stream.skip(_workbook_size * 2)); + assertEquals(0, stream.available()); + stream.reset(); + assertEquals(_workbook_size, stream.available()); + assertEquals(_workbook_size, + stream.skip(2 + ( long ) Integer.MAX_VALUE)); + assertEquals(0, stream.available()); + } + + /** + * 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.filesystem.DocumentInputStream"); + junit.textui.TestRunner.run(TestDocumentInputStream.class); + } +} diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java new file mode 100644 index 0000000000..7a63ad7c69 --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java @@ -0,0 +1,142 @@ + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache POI" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache POI", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + */ + +package org.apache.poi.poifs.filesystem; + +import java.io.*; + +import junit.framework.*; + +import org.apache.poi.poifs.property.DirectoryProperty; +import org.apache.poi.poifs.property.DocumentProperty; +import org.apache.poi.poifs.storage.RawDataBlock; + +/** + * Class to test DocumentNode functionality + * + * @author Marc Johnson + */ + +public class TestDocumentNode + extends TestCase +{ + + /** + * Constructor TestDocumentNode + * + * @param name + */ + + public TestDocumentNode(String name) + { + super(name); + } + + /** + * test constructor + * + * @exception IOException + */ + + public void testConstructor() + throws IOException + { + DirectoryProperty property1 = new DirectoryProperty("directory"); + RawDataBlock[] rawBlocks = new RawDataBlock[ 4 ]; + ByteArrayInputStream stream = + new ByteArrayInputStream(new byte[ 2048 ]); + + for (int j = 0; j < 4; j++) + { + rawBlocks[ j ] = new RawDataBlock(stream); + } + POIFSDocument document = new POIFSDocument("document", rawBlocks, + 2000); + DocumentProperty property2 = document.getDocumentProperty(); + DirectoryNode parent = new DirectoryNode(property1, null, null); + DocumentNode node = new DocumentNode(property2, parent); + + // verify we can retrieve the document + assertEquals(property2.getDocument(), node.getDocument()); + + // verify we can get the size + assertEquals(property2.getSize(), node.getSize()); + + // verify isDocumentEntry returns true + assertTrue(node.isDocumentEntry()); + + // verify isDirectoryEntry returns false + assertTrue(!node.isDirectoryEntry()); + + // verify getName behaves correctly + assertEquals(property2.getName(), node.getName()); + + // verify getParent behaves correctly + assertEquals(parent, node.getParent()); + } + + /** + * 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.filesystem.DocumentNode"); + junit.textui.TestRunner.run(TestDocumentNode.class); + } +} diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java new file mode 100644 index 0000000000..de0d241919 --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java @@ -0,0 +1,257 @@ + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache POI" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache POI", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + */ + +package org.apache.poi.poifs.filesystem; + +import java.io.*; + +import java.util.*; + +import junit.framework.*; + +import org.apache.poi.poifs.property.DirectoryProperty; +import org.apache.poi.poifs.property.DocumentProperty; +import org.apache.poi.poifs.storage.RawDataBlock; + +/** + * Class to test DocumentOutputStream functionality + * + * @author Marc Johnson + */ + +public class TestDocumentOutputStream + extends TestCase +{ + + /** + * Constructor TestDocumentOutputStream + * + * @param name + * + * @exception IOException + */ + + public TestDocumentOutputStream(String name) + throws IOException + { + super(name); + } + + /** + * test write(int) behavior + * + * @exception IOException + */ + + public void testWrite1() + throws IOException + { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DocumentOutputStream dstream = new DocumentOutputStream(stream, 25); + + for (int j = 0; j < 25; j++) + { + dstream.write(j); + } + try + { + dstream.write(0); + fail("Should have caught IOException"); + } + catch (IOException ignored) + { + } + byte[] output = stream.toByteArray(); + + assertEquals(25, output.length); + for (int j = 0; j < 25; j++) + { + assertEquals(( byte ) j, output[ j ]); + } + stream.close(); + } + + /** + * test write(byte[]) behavior + * + * @exception IOException + */ + + public void testWrite2() + throws IOException + { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DocumentOutputStream dstream = new DocumentOutputStream(stream, 25); + + for (int j = 0; j < 6; j++) + { + byte[] array = new byte[ 4 ]; + + Arrays.fill(array, ( byte ) j); + dstream.write(array); + } + try + { + byte[] array = new byte[ 4 ]; + + Arrays.fill(array, ( byte ) 7); + dstream.write(array); + fail("Should have caught IOException"); + } + catch (IOException ignored) + { + } + byte[] output = stream.toByteArray(); + + assertEquals(24, output.length); + for (int j = 0; j < 6; j++) + { + for (int k = 0; k < 4; k++) + { + assertEquals(String.valueOf((j * 4) + k), ( byte ) j, + output[ (j * 4) + k ]); + } + } + stream.close(); + } + + /** + * test write(byte[], int, int) behavior + * + * @exception IOException + */ + + public void testWrite3() + throws IOException + { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DocumentOutputStream dstream = new DocumentOutputStream(stream, 25); + byte[] array = new byte[ 50 ]; + + for (int j = 0; j < 50; j++) + { + array[ j ] = ( byte ) j; + } + dstream.write(array, 1, 25); + try + { + dstream.write(array, 0, 1); + fail("Should have caught IOException"); + } + catch (IOException ignored) + { + } + byte[] output = stream.toByteArray(); + + assertEquals(25, output.length); + for (int j = 0; j < 25; j++) + { + assertEquals(( byte ) (j + 1), output[ j ]); + } + stream.close(); + } + + /** + * test writeFiller() + * + * @exception IOException + */ + + public void testWriteFiller() + throws IOException + { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DocumentOutputStream dstream = new DocumentOutputStream(stream, 25); + + for (int j = 0; j < 25; j++) + { + dstream.write(j); + } + try + { + dstream.write(0); + fail("Should have caught IOException"); + } + catch (IOException ignored) + { + } + dstream.writeFiller(100, ( byte ) 0xff); + byte[] output = stream.toByteArray(); + + assertEquals(100, output.length); + for (int j = 0; j < 25; j++) + { + assertEquals(( byte ) j, output[ j ]); + } + for (int j = 25; j < 100; j++) + { + assertEquals(String.valueOf(j), ( byte ) 0xff, output[ j ]); + } + stream.close(); + } + + /** + * 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.filesystem.DocumentOutputStream"); + junit.textui.TestRunner.run(TestDocumentOutputStream.class); + } +} diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java new file mode 100644 index 0000000000..7781196d14 --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java @@ -0,0 +1,370 @@ + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache POI" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache POI", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + */ + +package org.apache.poi.poifs.filesystem; + +import junit.framework.*; + +/** + * Class to test POIFSDocumentPath functionality + * + * @author Marc Johnson + */ + +public class TestPOIFSDocumentPath + extends TestCase +{ + + /** + * Constructor TestPOIFSDocumentPath + * + * @param name + */ + + public TestPOIFSDocumentPath(String name) + { + super(name); + } + + /** + * Test default constructor + */ + + public void testDefaultConstructor() + { + POIFSDocumentPath path = new POIFSDocumentPath(); + + assertEquals(0, path.length()); + } + + /** + * Test full path constructor + */ + + public void testFullPathConstructor() + { + String[] components = + { + "foo", "bar", "foobar", "fubar" + }; + + for (int j = 0; j < components.length; j++) + { + String[] params = new String[ j ]; + + for (int k = 0; k < j; k++) + { + params[ k ] = components[ k ]; + } + POIFSDocumentPath path = new POIFSDocumentPath(params); + + assertEquals(j, path.length()); + for (int k = 0; k < j; k++) + { + assertEquals(components[ k ], path.getComponent(k)); + } + if (j == 0) + { + assertNull(path.getParent()); + } + else + { + POIFSDocumentPath parent = path.getParent(); + + assertNotNull(parent); + assertEquals(j - 1, parent.length()); + for (int k = 0; k < j - 1; k++) + { + assertEquals(components[ k ], parent.getComponent(k)); + } + } + } + + // test weird variants + assertEquals(0, new POIFSDocumentPath(null).length()); + try + { + new POIFSDocumentPath(new String[] + { + "fu", "" + }); + fail("should have caught IllegalArgumentException"); + } + catch (IllegalArgumentException ignored) + { + } + try + { + new POIFSDocumentPath(new String[] + { + "fu", null + }); + fail("should have caught IllegalArgumentException"); + } + catch (IllegalArgumentException ignored) + { + } + } + + /** + * Test relative path constructor + */ + + public void testRelativePathConstructor() + { + String[] initialComponents = + { + "a", "b", "c" + }; + + for (int n = 0; n < initialComponents.length; n++) + { + String[] initialParams = new String[ n ]; + + for (int k = 0; k < n; k++) + { + initialParams[ k ] = initialComponents[ k ]; + } + POIFSDocumentPath base = + new POIFSDocumentPath(initialParams); + String[] components = + { + "foo", "bar", "foobar", "fubar" + }; + + for (int j = 0; j < components.length; j++) + { + String[] params = new String[ j ]; + + for (int k = 0; k < j; k++) + { + params[ k ] = components[ k ]; + } + POIFSDocumentPath path = new POIFSDocumentPath(base, params); + + assertEquals(j + n, path.length()); + for (int k = 0; k < n; k++) + { + assertEquals(initialComponents[ k ], + path.getComponent(k)); + } + for (int k = 0; k < j; k++) + { + assertEquals(components[ k ], path.getComponent(k + n)); + } + if ((j + n) == 0) + { + assertNull(path.getParent()); + } + else + { + POIFSDocumentPath parent = path.getParent(); + + assertNotNull(parent); + assertEquals(j + n - 1, parent.length()); + for (int k = 0; k < (j + n - 1); k++) + { + assertEquals(path.getComponent(k), + parent.getComponent(k)); + } + } + } + + // test weird variants + assertEquals(n, new POIFSDocumentPath(base, null).length()); + try + { + new POIFSDocumentPath(base, new String[] + { + "fu", "" + }); + fail("should have caught IllegalArgumentException"); + } + catch (IllegalArgumentException ignored) + { + } + try + { + new POIFSDocumentPath(base, new String[] + { + "fu", null + }); + fail("should have caught IllegalArgumentException"); + } + catch (IllegalArgumentException ignored) + { + } + } + } + + /** + * test equality + */ + + public void testEquality() + { + POIFSDocumentPath a1 = new POIFSDocumentPath(); + POIFSDocumentPath a2 = new POIFSDocumentPath(null); + POIFSDocumentPath a3 = new POIFSDocumentPath(new String[ 0 ]); + POIFSDocumentPath a4 = new POIFSDocumentPath(a1, null); + POIFSDocumentPath a5 = new POIFSDocumentPath(a1, + new String[ 0 ]); + POIFSDocumentPath[] paths = + { + a1, a2, a3, a4, a5 + }; + + for (int j = 0; j < paths.length; j++) + { + for (int k = 0; k < paths.length; k++) + { + assertEquals(String.valueOf(j) + "<>" + String.valueOf(k), + paths[ j ], paths[ k ]); + } + } + a2 = new POIFSDocumentPath(a1, new String[] + { + "foo" + }); + a3 = new POIFSDocumentPath(a2, new String[] + { + "bar" + }); + a4 = new POIFSDocumentPath(a3, new String[] + { + "fubar" + }); + a5 = new POIFSDocumentPath(a4, new String[] + { + "foobar" + }); + POIFSDocumentPath[] builtUpPaths = + { + a1, a2, a3, a4, a5 + }; + POIFSDocumentPath[] fullPaths = + { + new POIFSDocumentPath(), new POIFSDocumentPath(new String[] + { + "foo" + }), new POIFSDocumentPath(new String[] + { + "foo", "bar" + }), new POIFSDocumentPath(new String[] + { + "foo", "bar", "fubar" + }), new POIFSDocumentPath(new String[] + { + "foo", "bar", "fubar", "foobar" + }) + }; + + for (int k = 0; k < builtUpPaths.length; k++) + { + for (int j = 0; j < fullPaths.length; j++) + { + if (k == j) + { + assertEquals(String.valueOf(j) + "<>" + + String.valueOf(k), fullPaths[ j ], + builtUpPaths[ k ]); + } + else + { + assertTrue(String.valueOf(j) + "<>" + String.valueOf(k), + !(fullPaths[ j ].equals(builtUpPaths[ k ]))); + } + } + } + POIFSDocumentPath[] badPaths = + { + new POIFSDocumentPath(new String[] + { + "_foo" + }), new POIFSDocumentPath(new String[] + { + "foo", "_bar" + }), new POIFSDocumentPath(new String[] + { + "foo", "bar", "_fubar" + }), new POIFSDocumentPath(new String[] + { + "foo", "bar", "fubar", "_foobar" + }) + }; + + for (int k = 0; k < builtUpPaths.length; k++) + { + for (int j = 0; j < badPaths.length; j++) + { + assertTrue(String.valueOf(j) + "<>" + String.valueOf(k), + !(fullPaths[ k ].equals(badPaths[ j ]))); + } + } + } + + /** + * 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.eventfilesystem.POIFSDocumentPath"); + junit.textui.TestRunner.run(TestPOIFSDocumentPath.class); + } +} |