diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2017-01-15 23:08:47 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2017-01-15 23:08:47 +0000 |
commit | 3cf6c40004a3beb5d8855478fb202429c25a1555 (patch) | |
tree | 0119eff3108bb6799526a55c04568a99abee930b /src/scratchpad/testcases/org/apache | |
parent | d2d885990fea477e9efdae8858989350904e1c44 (diff) | |
download | poi-3cf6c40004a3beb5d8855478fb202429c25a1555.tar.gz poi-3cf6c40004a3beb5d8855478fb202429c25a1555.zip |
fix eclipse warnings - close resources / type generics
cleanup sources - add parenthesis to statements
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1778955 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/scratchpad/testcases/org/apache')
6 files changed, 276 insertions, 273 deletions
diff --git a/src/scratchpad/testcases/org/apache/poi/hdgf/streams/StreamTest.java b/src/scratchpad/testcases/org/apache/poi/hdgf/streams/StreamTest.java index f29adb7f0c..eeb55a56c5 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdgf/streams/StreamTest.java +++ b/src/scratchpad/testcases/org/apache/poi/hdgf/streams/StreamTest.java @@ -17,12 +17,13 @@ package org.apache.poi.hdgf.streams; -import org.apache.poi.hdgf.pointers.Pointer; -import static org.apache.poi.hdgf.pointers.PointerV6.*; +import static org.apache.poi.hdgf.pointers.PointerV6.getNumPointersOffsetV6; +import static org.apache.poi.hdgf.pointers.PointerV6.getNumPointersV6; +import static org.apache.poi.hdgf.pointers.PointerV6.getPostNumPointersSkipV6; -import junit.framework.TestCase; +import org.apache.poi.hdgf.pointers.Pointer; -public abstract class StreamTest extends TestCase { +public abstract class StreamTest { public static class TestPointer extends Pointer { private final boolean compressed; protected boolean hasPointers = false; diff --git a/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamBasics.java b/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamBasics.java index 26c91d0d19..436286c07e 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamBasics.java +++ b/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamBasics.java @@ -17,73 +17,45 @@ package org.apache.poi.hdgf.streams; +import static org.apache.poi.poifs.storage.RawDataUtil.decompress; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + import org.apache.poi.hdgf.pointers.Pointer; +import org.junit.BeforeClass; +import org.junit.Test; public final class TestStreamBasics extends StreamTest { - /** The header from when compressedStream is decompressed */ - public static final byte[] compressedStreamDCHeader = new byte[] { - -60, 2, 0, 0 - }; - public static final byte[] compressedStream = new byte[] { - 123, -60, 2, -21, -16, 1, 0, 0, -72, -13, -16, 78, -32, -5, 1, - 0, 3, -21, -16, 10, 5, 4, -21, -16, 21, 9, -21, -16, 103, -21, - -16, 34, -36, -1, 52, 15, 70, 15, 120, 88, 15, -7, -2, -28, -9, - -123, 21, 0, 44, -122, 1, -4, 104, 15, -24, -13, 40, -98, 32, - 78, 102, -67, -1, -2, -30, 64, 40, -67, -113, -73, 116, -98, - -85, 2, 66, 123, 9, 109, -85, 2, -89, 14, -56, -69, -83, -79, - -34, -3, 120, 110, 75, -9, -10, 20, -6, -25, -12, 22, -21, -16, - -12, -81, 67, 1, -128, -70, -21, -16, 84, -21, -16, 70, 0, 23, - -21, -16, 76, 47, -40, 79, 1, -44, -21, -16, 32, 3, 18, 12, 17, - -43, -68, 17, 16, -8, 21, 22, -1, -21, -16, -84, -1, -35, 79, - -9, -10, 96, 0, 46, -21, -16, 44, -39, -41, 79, 1, 119, -13, - -16, -106, -13, -16, 84, 0, 125, 26, -21, -16, 68, -38, 79, 1, - 17, 10, 0, -97, 50, 10, 0, 0, -42, -108, 15, 118, 31, 0, -3, 29, - -21, -16, -100, -25, 79, 1, -18, 97, -36, 76, 16, -21, -16, 86, - 0, 36, -5, 1, -5, 79, 63, 1, -124, 98, 0, 0, 28, 3, 20, -34, -3, - 125, 33, -21, -16, 100, -4, 79, 1, -92, -91, 16, -22, 24, 19, 41, - -21, -16, -44, -59, 16, 108, 100, 0, -21, 0, 71, -105, 18, 39, 85, - 17, -3, 79, 1, 95, -108, 113, 0, 0, 104, 3, 18, 49, 49, 17, -1, 64, - 85, 1, 0, 114, 0, 0, -93, -36, -21, -16, 100, 31, 0, 0, -40, -21, - -16, -92, 66, 127, 85, 1, 98, 119, 0, 0, -48, 79, 18, -3, 50, -17, - 1, 67, 85, 1, 81, -127, 0, -41, 0, 14, 6, 4, 17, 63, -63, 17, 68, - 85, -65, 1, 30, -120, 0, 0, 42, 79, 18, 68, 126, -21, -16, -76, 69, - 85, 1, 102, -119, 72, 37, 0, 97, 33 }; - public static final byte[] uncompressedStream = new byte[] { - 0, 1, 0, 0, -72, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, - 0, 9, 0, 0, 0, 103, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - -123, 21, 0, 44, -123, 21, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, -98, 32, 78, 102, -67, - -2, -30, 64, 40, -67, -113, -73, 116, -67, -2, -30, 64, 40, 66, - 123, 9, 109, -67, -2, -30, 64, 40, -98, 32, 78, 102, -67, -2, -30, - 64, 40, -67, -113, -73, 116, -67, -2, -30, 64, -56, -83, -79, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 110, 75, 1, 0, 0, 0, - 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 22, 0, 0, 0, -12, -81, 67, - 1, -128, 0, 0, 0, 84, 0, 0, 0, 70, 0, 23, 0, 0, 0, 76, -40, 79, 1, - -44, 0, 0, 0, 32, 0, 0, 0, 84, 0, 23, 0, 0, 0, -68, -40, 79, 1, -8, - 0, 0, 0, 32, 0, 0, 0, 84, 0, -1, 0, 0, 0, -84, -1, 79, 1, 0, 0, 0, - 0, 0, 0, 0, 0, 96, 0, 46, 0, 0, 0, 44, -39, 79, 1, 119, 1, 0, 0, - -106, 1, 0, 0, 84, 0, 26, 0, 0, 0, 68, -38, 79, 1, 17, 3, 0, 0, - 50, 10, 0, 0, -42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 29, 0, 0, 0, -100, -25, 79, 1, -18, 97, 0, 0, -106, 0, 0, 0, 86, 0, - 36, 0, 0, 0, -12, -5, 79, 1, -124, 98, 0, 0, 28, 0, 0, 0, 84, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 100, - -4, 79, 1, -92, 98, 0, 0, 32, 0, 0, 0, 84, 0, 41, 0, 0, 0, -44, -4, - 79, 1, 108, 100, 0, 0, 71, 0, 0, 0, 86, 0, 39, 0, 0, 0, 68, -3, 79, - 1, -108, 113, 0, 0, 104, 0, 0, 0, 84, 0, 49, 0, 0, 0, -84, 64, 85, - 1, 0, 114, 0, 0, -93, 0, 0, 0, -42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, -40, 0, 0, 0, -92, 66, 85, 1, 98, 119, - 0, 0, -48, 1, 0, 0, 84, 0, 50, 0, 0, 0, 20, 67, 85, 1, 81, -127, - 0, 0, 14, 6, 0, 0, 84, 0, 63, 0, 0, 0, 100, 68, 85, 1, 30, -120, - 0, 0, 42, 1, 0, 0, 84, 0, 68, 0, 0, 0, -76, 69, 85, 1, 102, -119, - 0, 0, 42, 1, 0, 0, 84, 0, 0, 0, 0, 0 - }; + private static byte[] compressedStream, uncompressedStream; + + @BeforeClass + public static void init() throws IOException { + compressedStream = decompress( + "H4sIAAAAAAAAAAFTAaz+e8QC6/ABAAC48/BO4PsBAAPr8AoFBOvwFQnr8Gfr8CLc/zQPRg94WA/5/u"+ + "T3hRUALIYB/GgP6PMoniBOZr3//uJAKL2Pt3SeqwJCewltqwKnDsi7rbHe/XhuS/f2FPrn9Bbr8PSv"+ + "QwGAuuvwVOvwRgAX6/BML9hPAdTr8CADEgwR1bwREPgVFv/r8Kz/3U/39mAALuvwLNnXTwF38/CW8/"+ + "BUAH0a6/BE2k8BEQoAnzIKAADWlA92HwD9HevwnOdPAe5h3EwQ6/BWACT7AftPPwGEYgAAHAMU3v19"+ + "IevwZPxPAaSlEOoYEynr8NTFEGxkAOsAR5cSJ1UR/U8BX5RxAABoAxIxMRH/QFUBAHIAAKPc6/BkHw"+ + "AA2OvwpEJ/VQFidwAA0E8S/TLvAUNVAVGBANcADgYEET/BEURVvwEeiAAAKk8SRH7r8LRFVQFmiUgl"+ + "AGEhwtTYaVMBAAA=" + ); + + uncompressedStream = decompress( + "H4sIAAAAAAAAAGNgZGDYAcSogJGBGUjCMAsQcwJxOhAroSulEkB2Qqsogw4I41KrMU/BL23vv0cOGn"+ + "v7t5eAGU7VnLlgBobUibUb0fVX5HnDrROB0mJA/GW9M2MDkA4BYjcGcSDpc8Of8QqQVgCLgkT2AEV+"+ + "wEX+A8k1//3hpiUw6AFJnZv+jOVAsWmMIDVSQBGXW/6MgsCgNOJiYLhGVHjIAvGc5/6M7xKB5gDZYQ"+ + "wqIBf+9mdsSWJgkIG6Eh0oAnHKH3/GJUkwF2oCyStAkZwUBgZ3sDnqIPf89WecUsjAkAFWYwjyhUMo"+ + "I0MRA8NiBuwuvAHES5xCGZPKGRgugP1lBAo951DGwEYGBj42kIg9yHaXUEa5DgYGLbAaF6DIFtdQxr"+ + "ROmAgIAAD6SJPAdAIAAA==" + ); + } + + @Test public void testCompressedStream() { // Create a fake pointer Pointer ptr = new TestPointer(true, 0, compressedStream.length, -1, (short)-1); @@ -93,7 +65,6 @@ public final class TestStreamBasics extends StreamTest { // Check assertNotNull(stream.getPointer()); assertNotNull(stream.getStore()); - assertTrue(stream.getStore() instanceof StreamStore); assertTrue(stream.getStore() instanceof CompressedStreamStore); assertTrue(stream instanceof UnknownStream); @@ -108,7 +79,8 @@ public final class TestStreamBasics extends StreamTest { } } - public void testUncompressedStream() { + @Test + public void testUncompressedStream() { // Create a fake pointer Pointer ptr = new TestPointer(false, 0, uncompressedStream.length, -1, (short)-1); // Now the stream @@ -117,7 +89,6 @@ public final class TestStreamBasics extends StreamTest { // Check assertNotNull(stream.getPointer()); assertNotNull(stream.getStore()); - assertTrue(stream.getStore() instanceof StreamStore); assertFalse(stream.getStore() instanceof CompressedStreamStore); assertTrue(stream instanceof UnknownStream); } diff --git a/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamBugs.java b/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamBugs.java index 0377b248a5..006021b014 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamBugs.java @@ -17,15 +17,18 @@ package org.apache.poi.hdgf.streams; +import java.io.IOException; import java.io.InputStream; +import org.apache.poi.POIDataSamples; import org.apache.poi.hdgf.HDGFDiagram; import org.apache.poi.hdgf.chunks.ChunkFactory; import org.apache.poi.hdgf.pointers.Pointer; import org.apache.poi.hdgf.pointers.PointerFactory; -import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.apache.poi.POIDataSamples; +import org.apache.poi.util.IOUtils; +import org.junit.Before; +import org.junit.Test; /** * Tests for bugs with streams @@ -36,33 +39,35 @@ public final class TestStreamBugs extends StreamTest { private PointerFactory ptrFactory; private POIFSFileSystem filesystem; - @Override - protected void setUp() throws Exception { + @Before + public void setUp() throws IOException { ptrFactory = new PointerFactory(11); chunkFactory = new ChunkFactory(11); InputStream is = POIDataSamples.getDiagramInstance().openResourceAsStream("44594.vsd"); filesystem = new POIFSFileSystem(is); - - DocumentEntry docProps = - (DocumentEntry)filesystem.getRoot().getEntry("VisioDocument"); + is.close(); // Grab the document stream - contents = new byte[docProps.getSize()]; - filesystem.createDocumentInputStream("VisioDocument").read(contents); + InputStream is2 = filesystem.createDocumentInputStream("VisioDocument"); + contents = IOUtils.toByteArray(is2); + is2.close(); } - public void testGetTrailer() { + @Test + public void testGetTrailer() { Pointer trailerPointer = ptrFactory.createPointer(contents, 0x24); Stream.createStream(trailerPointer, contents, chunkFactory, ptrFactory); } - public void TOIMPLEMENTtestGetCertainChunks() { + @SuppressWarnings("unused") + public void TOIMPLEMENTtestGetCertainChunks() { int offsetA = 3708; int offsetB = 3744; } - public void testGetChildren() { + @Test + public void testGetChildren() { Pointer trailerPointer = ptrFactory.createPointer(contents, 0x24); TrailerStream trailer = (TrailerStream) Stream.createStream(trailerPointer, contents, chunkFactory, ptrFactory); @@ -97,7 +102,8 @@ public final class TestStreamBugs extends StreamTest { trailer.findChildren(contents); } - public void testOpen() throws Exception { - HDGFDiagram dg = new HDGFDiagram(filesystem); + @Test + public void testOpen() throws IOException { + new HDGFDiagram(filesystem).close(); } } diff --git a/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamComplex.java b/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamComplex.java index d1092e2bba..c601a06438 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamComplex.java +++ b/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamComplex.java @@ -17,15 +17,24 @@ package org.apache.poi.hdgf.streams; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; import java.io.InputStream; +import org.apache.poi.POIDataSamples; import org.apache.poi.hdgf.chunks.Chunk; import org.apache.poi.hdgf.chunks.ChunkFactory; import org.apache.poi.hdgf.pointers.Pointer; import org.apache.poi.hdgf.pointers.PointerFactory; -import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.apache.poi.POIDataSamples; +import org.apache.poi.util.IOUtils; +import org.junit.Before; +import org.junit.Test; public final class TestStreamComplex extends StreamTest { private byte[] contents; @@ -34,26 +43,28 @@ public final class TestStreamComplex extends StreamTest { private ChunkFactory chunkFactory; private PointerFactory ptrFactory; - @Override - protected void setUp() throws Exception { + @Before + public void setUp() throws IOException { ptrFactory = new PointerFactory(11); chunkFactory = new ChunkFactory(11); InputStream is = POIDataSamples.getDiagramInstance().openResourceAsStream("Test_Visio-Some_Random_Text.vsd"); POIFSFileSystem filesystem = new POIFSFileSystem(is); - - DocumentEntry docProps = - (DocumentEntry)filesystem.getRoot().getEntry("VisioDocument"); + is.close(); // Grab the document stream - contents = new byte[docProps.getSize()]; - filesystem.createDocumentInputStream("VisioDocument").read(contents); + InputStream is2 = filesystem.createDocumentInputStream("VisioDocument"); + contents = IOUtils.toByteArray(is2); + is2.close(); + + filesystem.close(); } /** * Test creating the trailer, but not looking for children */ - public void testTrailer() { + @Test + public void testTrailer() { // Find the trailer Pointer trailerPtr = ptrFactory.createPointer(contents, trailerPointerAt); @@ -74,7 +85,8 @@ public final class TestStreamComplex extends StreamTest { assertEquals(0xff, ts.getChildPointers()[3].getType()); } - public void testChunks() { + @Test + public void testChunks() { Pointer trailerPtr = ptrFactory.createPointer(contents, trailerPointerAt); TrailerStream ts = (TrailerStream) Stream.createStream(trailerPtr, contents, chunkFactory, ptrFactory); @@ -94,7 +106,8 @@ public final class TestStreamComplex extends StreamTest { cs.findChunks(); } - public void testStrings() { + @Test + public void testStrings() { Pointer trailerPtr = ptrFactory.createPointer(contents, trailerPointerAt); TrailerStream ts = (TrailerStream) Stream.createStream(trailerPtr, contents, chunkFactory, ptrFactory); @@ -110,7 +123,8 @@ public final class TestStreamComplex extends StreamTest { assertTrue(stream instanceof StringsStream); } - public void testPointerToStrings() { + @Test + public void testPointerToStrings() { // The stream at 0x347f has strings // The stream at 0x4312 has a pointer to 0x347f // The stream at 0x44d3 has a pointer to 0x4312 @@ -154,7 +168,8 @@ public final class TestStreamComplex extends StreamTest { assertTrue(s4312.getPointedToStreams()[1] instanceof StringsStream); } - public void testTrailerContents() { + @Test + public void testTrailerContents() { Pointer trailerPtr = ptrFactory.createPointer(contents, trailerPointerAt); TrailerStream ts = (TrailerStream) Stream.createStream(trailerPtr, contents, chunkFactory, ptrFactory); @@ -205,7 +220,8 @@ public final class TestStreamComplex extends StreamTest { assertTrue(s8451.getPointedToStreams()[1] instanceof StringsStream); } - public void testChunkWithText() { + @Test + public void testChunkWithText() { // Parent ChunkStream is at 0x7194 // This is one of the last children of the trailer Pointer trailerPtr = ptrFactory.createPointer(contents, trailerPointerAt); diff --git a/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestEscherParts.java b/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestEscherParts.java index 72d8274ed4..28e1eeca69 100644 --- a/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestEscherParts.java +++ b/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestEscherParts.java @@ -17,18 +17,24 @@ package org.apache.poi.hpbf.model; -import org.apache.poi.hpbf.HPBFDocument; -import org.apache.poi.POIDataSamples; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; -import junit.framework.TestCase; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.poi.POIDataSamples; +import org.apache.poi.hpbf.HPBFDocument; +import org.junit.Test; -public final class TestEscherParts extends TestCase { +public final class TestEscherParts { private static final POIDataSamples _samples = POIDataSamples.getPublisherInstance(); - public void testBasics() throws Exception { - HPBFDocument doc = new HPBFDocument( - _samples.openResourceAsStream("Sample.pub") - ); + @Test + public void testBasics() throws IOException { + InputStream is = _samples.openResourceAsStream("Sample.pub"); + HPBFDocument doc = new HPBFDocument(is); + is.close(); EscherStm es = doc.getEscherStm(); EscherDelayStm eds = doc.getEscherDelayStm(); @@ -40,15 +46,17 @@ public final class TestEscherParts extends TestCase { assertEquals(0, eds.getEscherRecords().length); // TODO - check the contents + doc.close(); } - public void testComplex() throws Exception { - HPBFDocument doc = new HPBFDocument( - _samples.openResourceAsStream("SampleBrochure.pub") - ); + @Test + public void testComplex() throws Exception { + InputStream is = _samples.openResourceAsStream("SampleBrochure.pub"); + HPBFDocument doc1 = new HPBFDocument(is); + is.close(); - EscherStm es = doc.getEscherStm(); - EscherDelayStm eds = doc.getEscherDelayStm(); + EscherStm es = doc1.getEscherStm(); + EscherDelayStm eds = doc1.getEscherDelayStm(); assertNotNull(es); assertNotNull(eds); @@ -57,20 +65,21 @@ public final class TestEscherParts extends TestCase { assertEquals(19, eds.getEscherRecords().length); // TODO - check contents - + doc1.close(); // Now do another complex file - doc = new HPBFDocument( - _samples.openResourceAsStream("SampleNewsletter.pub") - ); + InputStream is2 = _samples.openResourceAsStream("SampleNewsletter.pub"); + HPBFDocument doc2 = new HPBFDocument(is2); + is2.close(); - es = doc.getEscherStm(); - eds = doc.getEscherDelayStm(); + es = doc2.getEscherStm(); + eds = doc2.getEscherDelayStm(); assertNotNull(es); assertNotNull(eds); assertEquals(51, es.getEscherRecords().length); assertEquals(92, eds.getEscherRecords().length); + doc2.close(); } } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java b/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java index 8ab30ee973..d62e954842 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java @@ -25,6 +25,9 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -36,10 +39,9 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; +import org.apache.poi.poifs.filesystem.OPOIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.IOUtils; -import org.junit.After; -import org.junit.Before; import org.junit.Test; /** @@ -49,13 +51,11 @@ public final class TestExtractor { /** * Extractor primed on the 2 page basic test data */ - private PowerPointExtractor ppe; private static final String expectText = "This is a test title\nThis is a test subtitle\nThis is on page 1\nThis is the title on page 2\nThis is page two\nIt has several blocks of text\nNone of them have formatting\n"; /** * Extractor primed on the 1 page but text-box'd test data */ - private PowerPointExtractor ppe2; private static final String expectText2 = "Hello, World!!!\nI am just a poor boy\nThis is Times New Roman\nPlain Text \n"; /** @@ -63,49 +63,59 @@ public final class TestExtractor { */ private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); - @Before - public void setUp() throws Exception { - ppe = new PowerPointExtractor(slTests.getFile("basic_test_ppt_file.ppt").getCanonicalPath()); - ppe2 = new PowerPointExtractor(slTests.getFile("with_textbox.ppt").getCanonicalPath()); - } - - @After - public void closeResources() throws Exception { - ppe2.close(); - ppe.close(); +// @Before +// public void setUp() throws Exception { +// ppe = new PowerPointExtractor(slTests.getFile("basic_test_ppt_file.ppt").getCanonicalPath()); +// ppe2 = new PowerPointExtractor(slTests.getFile("with_textbox.ppt").getCanonicalPath()); +// } + +// @After +// public void closeResources() throws Exception { +// ppe2.close(); +// ppe.close(); +// } + + private PowerPointExtractor openExtractor(String fileName) throws IOException { + InputStream is = slTests.openResourceAsStream(fileName); + try { + return new PowerPointExtractor(is); + } finally { + is.close(); + } } - + @Test - public void testReadSheetText() { + public void testReadSheetText() throws IOException { // Basic 2 page example - String sheetText = ppe.getText(); - - ensureTwoStringsTheSame(expectText, sheetText); - + PowerPointExtractor ppe = openExtractor("basic_test_ppt_file.ppt"); + ensureTwoStringsTheSame(expectText, ppe.getText()); + ppe.close(); // 1 page example with text boxes - sheetText = ppe2.getText(); - - ensureTwoStringsTheSame(expectText2, sheetText); + PowerPointExtractor ppe2 = openExtractor("with_textbox.ppt"); + ensureTwoStringsTheSame(expectText2, ppe2.getText()); + ppe2.close(); } @Test - public void testReadNoteText() { + public void testReadNoteText() throws IOException { // Basic 2 page example + PowerPointExtractor ppe = openExtractor("basic_test_ppt_file.ppt"); String notesText = ppe.getNotes(); String expText = "These are the notes for page 1\nThese are the notes on page two, again lacking formatting\n"; - ensureTwoStringsTheSame(expText, notesText); + ppe.close(); // Other one doesn't have notes + PowerPointExtractor ppe2 = openExtractor("with_textbox.ppt"); notesText = ppe2.getNotes(); expText = ""; - ensureTwoStringsTheSame(expText, notesText); + ppe2.close(); } @Test - public void testReadBoth() { + public void testReadBoth() throws IOException { String[] slText = new String[]{ "This is a test title\nThis is a test subtitle\nThis is on page 1\n", "This is the title on page 2\nThis is page two\nIt has several blocks of text\nNone of them have formatting\n" @@ -115,6 +125,7 @@ public final class TestExtractor { "These are the notes on page two, again lacking formatting\n" }; + PowerPointExtractor ppe = openExtractor("basic_test_ppt_file.ppt"); ppe.setSlidesByDefault(true); ppe.setNotesByDefault(false); assertEquals(slText[0] + slText[1], ppe.getText()); @@ -126,6 +137,7 @@ public final class TestExtractor { ppe.setSlidesByDefault(true); ppe.setNotesByDefault(true); assertEquals(slText[0] + slText[1] + "\n" + ntText[0] + ntText[1], ppe.getText()); + ppe.close(); } /** @@ -135,9 +147,8 @@ public final class TestExtractor { * @throws Exception */ @Test - public void testMissingCoreRecords() throws Exception { - ppe.close(); - ppe = new PowerPointExtractor(slTests.openResourceAsStream("missing_core_records.ppt")); + public void testMissingCoreRecords() throws IOException { + PowerPointExtractor ppe = openExtractor("missing_core_records.ppt"); String text = ppe.getText(true, false); String nText = ppe.getNotes(); @@ -150,6 +161,8 @@ public final class TestExtractor { // Slide records were fine assertTrue(text.startsWith("Using Disease Surveillance and Response")); + + ppe.close(); } private void ensureTwoStringsTheSame(String exp, String act) { @@ -163,49 +176,37 @@ public final class TestExtractor { } @Test - public void testExtractFromEmbeded() throws Exception { - POIFSFileSystem fs = new POIFSFileSystem( - POIDataSamples.getSpreadSheetInstance().openResourceAsStream("excel_with_embeded.xls") - ); - HSLFSlideShowImpl ss; - - DirectoryNode dirA = (DirectoryNode) - fs.getRoot().getEntry("MBD0000A3B6"); - DirectoryNode dirB = (DirectoryNode) - fs.getRoot().getEntry("MBD0000A3B3"); - - assertNotNull(dirA.getEntry("PowerPoint Document")); - assertNotNull(dirB.getEntry("PowerPoint Document")); + public void testExtractFromEmbeded() throws IOException { + InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("excel_with_embeded.xls"); + POIFSFileSystem fs = new POIFSFileSystem(is); + DirectoryNode root = fs.getRoot(); + PowerPointExtractor ppe1 = assertExtractFromEmbedded(root, "MBD0000A3B6", "Sample PowerPoint file\nThis is the 1st file\nNot much too it\n"); + PowerPointExtractor ppe2 = assertExtractFromEmbedded(root, "MBD0000A3B3", "Sample PowerPoint file\nThis is the 2nd file\nNot much too it either\n"); + ppe2.close(); + ppe1.close(); + fs.close(); + } + + private PowerPointExtractor assertExtractFromEmbedded(DirectoryNode root, String entryName, String expected) + throws IOException { + DirectoryNode dir = (DirectoryNode)root.getEntry(entryName); + assertTrue(dir.hasEntry("PowerPoint Document")); // Check the first file - ss = new HSLFSlideShowImpl(dirA); - ppe.close(); - ppe = new PowerPointExtractor(ss); - assertEquals("Sample PowerPoint file\nThis is the 1st file\nNot much too it\n", - ppe.getText(true, false) - ); - - // And the second - ss = new HSLFSlideShowImpl(dirB); - ppe.close(); - ppe = new PowerPointExtractor(ss); - assertEquals("Sample PowerPoint file\nThis is the 2nd file\nNot much too it either\n", - ppe.getText(true, false) - ); - fs.close(); + HSLFSlideShowImpl ppt = new HSLFSlideShowImpl(dir); + PowerPointExtractor ppe = new PowerPointExtractor(ppt); + assertEquals(expected, ppe.getText(true, false)); + return ppe; } /** * A powerpoint file with embeded powerpoint files */ - @SuppressWarnings("unused") @Test - public void testExtractFromOwnEmbeded() throws Exception { - String path = "ppt_with_embeded.ppt"; - ppe.close(); - ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path)); + public void testExtractFromOwnEmbeded() throws IOException { + PowerPointExtractor ppe = openExtractor("ppt_with_embeded.ppt"); List<OLEShape> shapes = ppe.getOLEShapes(); - assertEquals("Expected 6 ole shapes in " + path, 6, shapes.size()); + assertEquals("Expected 6 ole shapes", 6, shapes.size()); int num_ppt = 0, num_doc = 0, num_xls = 0; for (OLEShape ole : shapes) { String name = ole.getInstanceName(); @@ -217,6 +218,7 @@ public final class TestExtractor { } else if ("Document".equals(name)) { HWPFDocument doc = new HWPFDocument(data); num_doc++; + doc.close(); } else if ("Presentation".equals(name)) { num_ppt++; HSLFSlideShow ppt = new HSLFSlideShow(data); @@ -227,153 +229,147 @@ public final class TestExtractor { assertEquals("Expected 2 embedded Word Documents", 2, num_doc); assertEquals("Expected 2 embedded Excel Spreadsheets", 2, num_xls); assertEquals("Expected 2 embedded PowerPoint Presentations", 2, num_ppt); + ppe.close(); } /** * A powerpoint file with embeded powerpoint files */ @Test - public void test52991() throws Exception { - String path = "badzip.ppt"; - ppe.close(); - ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path)); - List<OLEShape> shapes = ppe.getOLEShapes(); - - for (OLEShape shape : shapes) { + public void test52991() throws IOException { + PowerPointExtractor ppe = openExtractor("badzip.ppt"); + for (OLEShape shape : ppe.getOLEShapes()) { IOUtils.copy(shape.getObjectData().getData(), new ByteArrayOutputStream()); } + ppe.close(); } /** * From bug #45543 */ @Test - public void testWithComments() throws Exception { - ppe.close(); - ppe = new PowerPointExtractor(slTests.openResourceAsStream("WithComments.ppt")); - - String text = ppe.getText(); + public void testWithComments() throws IOException { + PowerPointExtractor ppe1 = openExtractor("WithComments.ppt"); + String text = ppe1.getText(); assertFalse("Comments not in by default", text.contains("This is a test comment")); - ppe.setCommentsByDefault(true); + ppe1.setCommentsByDefault(true); - text = ppe.getText(); + text = ppe1.getText(); assertContains(text, "This is a test comment"); + ppe1.close(); // And another file - ppe.close(); - ppe = new PowerPointExtractor(slTests.openResourceAsStream("45543.ppt")); - - text = ppe.getText(); + PowerPointExtractor ppe2 = openExtractor("45543.ppt"); + text = ppe2.getText(); assertFalse("Comments not in by default", text.contains("testdoc")); - ppe.setCommentsByDefault(true); + ppe2.setCommentsByDefault(true); - text = ppe.getText(); + text = ppe2.getText(); assertContains(text, "testdoc"); + ppe2.close(); } /** * From bug #45537 */ @Test - public void testHeaderFooter() throws Exception { + public void testHeaderFooter() throws IOException { String text; // With a header on the notes - HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("45537_Header.ppt")); - HSLFSlideShow ss = new HSLFSlideShow(hslf); - assertNotNull(ss.getNotesHeadersFooters()); - assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getHeaderText()); - ppe.close(); + InputStream is1 = slTests.openResourceAsStream("45537_Header.ppt"); + HSLFSlideShow ppt1 = new HSLFSlideShow(is1); + is1.close(); + assertNotNull(ppt1.getNotesHeadersFooters()); + assertEquals("testdoc test phrase", ppt1.getNotesHeadersFooters().getHeaderText()); - ppe = new PowerPointExtractor(hslf); + PowerPointExtractor ppe1 = new PowerPointExtractor(ppt1.getSlideShowImpl()); - text = ppe.getText(); + text = ppe1.getText(); assertFalse("Header shouldn't be there by default\n" + text, text.contains("testdoc")); assertFalse("Header shouldn't be there by default\n" + text, text.contains("test phrase")); - ppe.setNotesByDefault(true); - text = ppe.getText(); + ppe1.setNotesByDefault(true); + text = ppe1.getText(); assertContains(text, "testdoc"); assertContains(text, "test phrase"); - ss.close(); + ppe1.close(); + ppt1.close(); // And with a footer, also on notes - hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("45537_Footer.ppt")); - ss = new HSLFSlideShow(hslf); - assertNotNull(ss.getNotesHeadersFooters()); - assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getFooterText()); - ppe.close(); + InputStream is2 = slTests.openResourceAsStream("45537_Footer.ppt"); + HSLFSlideShow ppt2 = new HSLFSlideShow(is2); + is2.close(); + + assertNotNull(ppt2.getNotesHeadersFooters()); + assertEquals("testdoc test phrase", ppt2.getNotesHeadersFooters().getFooterText()); + ppt2.close(); - ppe = new PowerPointExtractor(slTests.openResourceAsStream("45537_Footer.ppt")); + PowerPointExtractor ppe2 = openExtractor("45537_Footer.ppt"); - text = ppe.getText(); + text = ppe2.getText(); assertFalse("Header shouldn't be there by default\n" + text, text.contains("testdoc")); assertFalse("Header shouldn't be there by default\n" + text, text.contains("test phrase")); - ppe.setNotesByDefault(true); - text = ppe.getText(); + ppe2.setNotesByDefault(true); + text = ppe2.getText(); assertContains(text, "testdoc"); assertContains(text, "test phrase"); + ppe2.close(); } @SuppressWarnings("unused") @Test - public void testSlideMasterText() throws Exception { + public void testSlideMasterText() throws IOException { String masterTitleText = "This is the Master Title"; String masterRandomText = "This text comes from the Master Slide"; String masterFooterText = "Footer from the master slide"; - HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("WithMaster.ppt")); - ppe.close(); - - ppe = new PowerPointExtractor(hslf); + PowerPointExtractor ppe = openExtractor("WithMaster.ppt"); + ppe.setMasterByDefault(true); String text = ppe.getText(); - //assertContains(text, masterTitleText); // TODO Is this available in PPT? - //assertContains(text, masterRandomText); // TODO Extract + assertContains(text, masterRandomText); assertContains(text, masterFooterText); + ppe.close(); } @Test - public void testMasterText() throws Exception { - ppe.close(); - ppe = new PowerPointExtractor(slTests.openResourceAsStream("master_text.ppt")); + public void testMasterText() throws IOException { + PowerPointExtractor ppe1 = openExtractor("master_text.ppt"); // Initially not there - String text = ppe.getText(); + String text = ppe1.getText(); assertFalse(text.contains("Text that I added to the master slide")); // Enable, shows up - ppe.setMasterByDefault(true); - text = ppe.getText(); + ppe1.setMasterByDefault(true); + text = ppe1.getText(); assertTrue(text.contains("Text that I added to the master slide")); // Make sure placeholder text does not come out assertFalse(text.contains("Click to edit Master")); + ppe1.close(); // Now with another file only containing master text // Will always show up + PowerPointExtractor ppe2 = openExtractor("WithMaster.ppt"); String masterText = "Footer from the master slide"; - HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("WithMaster.ppt")); - ppe.close(); - ppe = new PowerPointExtractor(hslf); - - text = ppe.getText(); + text = ppe2.getText(); assertContainsIgnoreCase(text, "master"); assertContains(text, masterText); + ppe2.close(); } /** * Bug #54880 Chinese text not extracted properly */ @Test - public void testChineseText() throws Exception { - HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("54880_chinese.ppt")); - ppe.close(); - ppe = new PowerPointExtractor(hslf); + public void testChineseText() throws IOException { + PowerPointExtractor ppe = openExtractor("54880_chinese.ppt"); String text = ppe.getText(); @@ -388,6 +384,7 @@ public final class TestExtractor { // Check for the chinese only text line assertContains(text, "\uff8a\uff9d\uff76\uff78"); + ppe.close(); } /** @@ -396,12 +393,15 @@ public final class TestExtractor { */ @SuppressWarnings("resource") @Test - public void testDifferentPOIFS() throws Exception { + public void testDifferentPOIFS() throws IOException { // Open the two filesystems - DirectoryNode[] files = new DirectoryNode[2]; - files[0] = (new POIFSFileSystem(slTests.openResourceAsStream("basic_test_ppt_file.ppt"))).getRoot(); - NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(slTests.getFile("basic_test_ppt_file.ppt")); - files[1] = npoifsFileSystem.getRoot(); + File pptFile = slTests.getFile("basic_test_ppt_file.ppt"); + InputStream is1 = new FileInputStream(pptFile); + OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is1); + is1.close(); + NPOIFSFileSystem npoifs = new NPOIFSFileSystem(pptFile); + + DirectoryNode[] files = { opoifs.getRoot(), npoifs.getRoot() }; // Open directly for (DirectoryNode dir : files) { @@ -409,48 +409,48 @@ public final class TestExtractor { assertEquals(expectText, extractor.getText()); } - // Open via a HWPFDocument + // Open via a HSLFSlideShow for (DirectoryNode dir : files) { HSLFSlideShowImpl slideshow = new HSLFSlideShowImpl(dir); PowerPointExtractor extractor = new PowerPointExtractor(slideshow); assertEquals(expectText, extractor.getText()); + extractor.close(); + slideshow.close(); } - npoifsFileSystem.close(); + npoifs.close(); } @Test public void testTable() throws Exception { -// ppe = new PowerPointExtractor(slTests.openResourceAsStream("54111.ppt")); -// String text = ppe.getText(); -// String target = "TH Cell 1\tTH Cell 2\tTH Cell 3\tTH Cell 4\n"+ -// "Row 1, Cell 1\tRow 1, Cell 2\tRow 1, Cell 3\tRow 1, Cell 4\n"+ -// "Row 2, Cell 1\tRow 2, Cell 2\tRow 2, Cell 3\tRow 2, Cell 4\n"+ -// "Row 3, Cell 1\tRow 3, Cell 2\tRow 3, Cell 3\tRow 3, Cell 4\n"+ -// "Row 4, Cell 1\tRow 4, Cell 2\tRow 4, Cell 3\tRow 4, Cell 4\n"+ -// "Row 5, Cell 1\tRow 5, Cell 2\tRow 5, Cell 3\tRow 5, Cell 4\n"; -// assertTrue(text.contains(target)); - ppe.close(); - - ppe = new PowerPointExtractor(slTests.openResourceAsStream("54722.ppt")); - String text = ppe.getText(); - - String target = "this\tText\tis\twithin\ta\n" + + PowerPointExtractor ppe1 = openExtractor("54111.ppt"); + String text1 = ppe1.getText(); + String target1 = "TH Cell 1\tTH Cell 2\tTH Cell 3\tTH Cell 4\n"+ + "Row 1, Cell 1\tRow 1, Cell 2\tRow 1, Cell 3\tRow 1, Cell 4\n"+ + "Row 2, Cell 1\tRow 2, Cell 2\tRow 2, Cell 3\tRow 2, Cell 4\n"+ + "Row 3, Cell 1\tRow 3, Cell 2\tRow 3, Cell 3\tRow 3, Cell 4\n"+ + "Row 4, Cell 1\tRow 4, Cell 2\tRow 4, Cell 3\tRow 4, Cell 4\n"+ + "Row 5, Cell 1\tRow 5, Cell 2\tRow 5, Cell 3\tRow 5, Cell 4\n"; + assertTrue(text1.contains(target1)); + ppe1.close(); + + PowerPointExtractor ppe2 = openExtractor("54722.ppt"); + String text2 = ppe2.getText(); + + String target2 = "this\tText\tis\twithin\ta\n" + "table\t1\t2\t3\t4"; - assertTrue(text.contains(target)); + assertTrue(text2.contains(target2)); + ppe2.close(); } // bug 60003 @Test public void testExtractMasterSlideFooterText() throws Exception { - HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("60003.ppt")); - ppe.close(); - - ppe = new PowerPointExtractor(hslf); + PowerPointExtractor ppe = openExtractor("60003.ppt"); ppe.setMasterByDefault(true); String text = ppe.getText(); assertContains(text, "Prague"); - hslf.close(); + ppe.close(); } } |