diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2021-01-22 23:00:51 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2021-01-22 23:00:51 +0000 |
commit | 0e5f513830b8ecb2923f02d3f5eb1e6f1388cb05 (patch) | |
tree | d75a488d9be999bac359e928ab06050be13c3733 /src/scratchpad | |
parent | 1b55a7930e8c011c27ab3b901bb32512bd398066 (diff) | |
download | poi-0e5f513830b8ecb2923f02d3f5eb1e6f1388cb05.tar.gz poi-0e5f513830b8ecb2923f02d3f5eb1e6f1388cb05.zip |
Sonar fixes
add asserts to tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885819 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/scratchpad')
8 files changed, 272 insertions, 288 deletions
diff --git a/src/scratchpad/testcases/org/apache/poi/hdgf/TestHDGFCore.java b/src/scratchpad/testcases/org/apache/poi/hdgf/TestHDGFCore.java index ff82c40afb..9c78ba3b61 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdgf/TestHDGFCore.java +++ b/src/scratchpad/testcases/org/apache/poi/hdgf/TestHDGFCore.java @@ -20,6 +20,9 @@ package org.apache.poi.hdgf; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.io.IOException; +import java.io.InputStream; + import org.apache.poi.POIDataSamples; import org.apache.poi.hdgf.extractor.VisioTextExtractor; import org.apache.poi.hdgf.streams.PointerContainingStream; @@ -30,49 +33,40 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public final class TestHDGFCore { - private static POIDataSamples _dgTests = POIDataSamples.getDiagramInstance(); - - private POIFSFileSystem fs; - private HDGFDiagram hdgf; - private VisioTextExtractor textExtractor; - - @BeforeEach - void setUp() throws Exception { - fs = new POIFSFileSystem(_dgTests.openResourceAsStream("Test_Visio-Some_Random_Text.vsd")); - } - @AfterEach - void tearDown() throws Exception { - if (textExtractor != null) textExtractor.close(); - if (hdgf != null) hdgf.close(); - } + private static final POIDataSamples SAMPLES = POIDataSamples.getDiagramInstance(); @Test void testCreate() throws Exception { - hdgf = new HDGFDiagram(fs); + try (POIFSFileSystem fs = openFS("Test_Visio-Some_Random_Text.vsd"); + HDGFDiagram hdgf = new HDGFDiagram(fs)) { + assertNotNull(hdgf); + } } @Test void testTrailer() throws Exception { - hdgf = new HDGFDiagram(fs); - assertNotNull(hdgf); - assertNotNull(hdgf.getTrailerStream()); + try (POIFSFileSystem fs = openFS("Test_Visio-Some_Random_Text.vsd"); + HDGFDiagram hdgf = new HDGFDiagram(fs)) { + assertNotNull(hdgf); + assertNotNull(hdgf.getTrailerStream()); - // Check it has what we'd expect - TrailerStream trailer = hdgf.getTrailerStream(); - assertEquals(0x8a94, trailer.getPointer().getOffset()); + // Check it has what we'd expect + TrailerStream trailer = hdgf.getTrailerStream(); + assertEquals(0x8a94, trailer.getPointer().getOffset()); - assertNotNull(trailer.getPointedToStreams()); - assertEquals(20, trailer.getPointedToStreams().length); + assertNotNull(trailer.getPointedToStreams()); + assertEquals(20, trailer.getPointedToStreams().length); - assertEquals(20, hdgf.getTopLevelStreams().length); + assertEquals(20, hdgf.getTopLevelStreams().length); - // 9th one should have children - assertNotNull(trailer.getPointedToStreams()[8]); - assertNotNull(trailer.getPointedToStreams()[8].getPointer()); - PointerContainingStream ps8 = (PointerContainingStream) + // 9th one should have children + assertNotNull(trailer.getPointedToStreams()[8]); + assertNotNull(trailer.getPointedToStreams()[8].getPointer()); + PointerContainingStream ps8 = (PointerContainingStream) trailer.getPointedToStreams()[8]; - assertNotNull(ps8.getPointedToStreams()); - assertEquals(8, ps8.getPointedToStreams().length); + assertNotNull(ps8.getPointedToStreams()); + assertEquals(8, ps8.getPointedToStreams().length); + } } /** @@ -81,15 +75,16 @@ public final class TestHDGFCore { */ @Test void testNegativeChunkLength() throws Exception { - fs = new POIFSFileSystem(_dgTests.openResourceAsStream("NegativeChunkLength.vsd")); - - hdgf = new HDGFDiagram(fs); - assertNotNull(hdgf); + try (POIFSFileSystem fs = openFS("NegativeChunkLength.vsd"); + HDGFDiagram hdgf = new HDGFDiagram(fs)) { + assertNotNull(hdgf); + } // And another file - fs = new POIFSFileSystem(_dgTests.openResourceAsStream("NegativeChunkLength2.vsd")); - hdgf = new HDGFDiagram(fs); - assertNotNull(hdgf); + try (POIFSFileSystem fs = openFS("NegativeChunkLength2.vsd"); + HDGFDiagram hdgf = new HDGFDiagram(fs)) { + assertNotNull(hdgf); + } } /** @@ -98,49 +93,55 @@ public final class TestHDGFCore { * chunk commands. */ @Test - void DISABLEDtestAIOOB() throws Exception { - fs = new POIFSFileSystem(_dgTests.openResourceAsStream("44501.vsd")); - - hdgf = new HDGFDiagram(fs); - assertNotNull(hdgf); + void testAIOOB() throws Exception { + try (POIFSFileSystem fs = openFS("44501.vsd"); + HDGFDiagram hdgf = new HDGFDiagram(fs)) { + assertNotNull(hdgf); + } } @Test void testV5() throws Exception { - fs = new POIFSFileSystem(_dgTests.openResourceAsStream("v5_Connection_Types.vsd")); - - hdgf = new HDGFDiagram(fs); - assertNotNull(hdgf); - - textExtractor = new VisioTextExtractor(hdgf); - String text = textExtractor.getText().replace("\u0000", "").trim(); - - assertEquals("Static to Static\nDynamic to Static\nDynamic to Dynamic", text); + try (POIFSFileSystem fs = openFS("v5_Connection_Types.vsd"); + HDGFDiagram hdgf = new HDGFDiagram(fs)) { + assertNotNull(hdgf); + + try (VisioTextExtractor textExtractor = new VisioTextExtractor(hdgf)) { + String text = textExtractor.getText().replace("\u0000", "").trim(); + assertEquals("Static to Static\nDynamic to Static\nDynamic to Dynamic", text); + } + } } @Test void testV6NonUtf16LE() throws Exception { - fs = new POIFSFileSystem(_dgTests.openResourceAsStream("v6-non-utf16le.vsd")); - - hdgf = new HDGFDiagram(fs); - assertNotNull(hdgf); - - textExtractor = new VisioTextExtractor(hdgf); - String text = textExtractor.getText().replace("\u0000", "").trim(); - - assertEquals("Table\n\n\nPropertySheet\n\n\n\nPropertySheetField", text); + try (POIFSFileSystem fs = openFS("v6-non-utf16le.vsd"); + HDGFDiagram hdgf = new HDGFDiagram(fs)) { + assertNotNull(hdgf); + + try (VisioTextExtractor textExtractor = new VisioTextExtractor(hdgf)) { + String text = textExtractor.getText().replace("\u0000", "").trim(); + assertEquals("Table\n\n\nPropertySheet\n\n\n\nPropertySheetField", text); + } + } } @Test void testUtf16LE() throws Exception { - fs = new POIFSFileSystem(_dgTests.openResourceAsStream("Test_Visio-Some_Random_Text.vsd")); - - hdgf = new HDGFDiagram(fs); - assertNotNull(hdgf); - - textExtractor = new VisioTextExtractor(hdgf); - String text = textExtractor.getText().trim(); + try (POIFSFileSystem fs = openFS("Test_Visio-Some_Random_Text.vsd"); + HDGFDiagram hdgf = new HDGFDiagram(fs)) { + assertNotNull(hdgf); + + try (VisioTextExtractor textExtractor = new VisioTextExtractor(hdgf)) { + String text = textExtractor.getText().trim(); + assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page", text); + } + } + } - assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page", text); + private POIFSFileSystem openFS(String file) throws IOException { + try (InputStream is = SAMPLES.openResourceAsStream(file)) { + return new POIFSFileSystem(is); + } } } diff --git a/src/scratchpad/testcases/org/apache/poi/hdgf/dev/TestVSDDumper.java b/src/scratchpad/testcases/org/apache/poi/hdgf/dev/TestVSDDumper.java index 10f131b7c4..302eacc928 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdgf/dev/TestVSDDumper.java +++ b/src/scratchpad/testcases/org/apache/poi/hdgf/dev/TestVSDDumper.java @@ -20,32 +20,26 @@ ==================================================================== */ package org.apache.poi.hdgf.dev; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + import java.io.File; import java.io.PrintStream; import org.apache.poi.POIDataSamples; import org.apache.poi.util.NullPrintStream; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class TestVSDDumper { - private static PrintStream oldStdOut; - - @BeforeAll - public static void muteStdout() { - oldStdOut = System.out; - System.setOut(new NullPrintStream()); - } - - @AfterAll - public static void restoreStdout() { - System.setOut(oldStdOut); - } - @Test - void main() throws Exception { - File file = POIDataSamples.getDiagramInstance().getFile("Test_Visio-Some_Random_Text.vsd"); - VSDDumper.main(new String[] { file.getAbsolutePath() }); + void main() { + PrintStream oldStdOut = System.out; + System.setOut(new NullPrintStream()); + try { + File file = POIDataSamples.getDiagramInstance().getFile("Test_Visio-Some_Random_Text.vsd"); + String[] args = { file.getAbsolutePath() }; + assertDoesNotThrow(() -> VSDDumper.main(args)); + } finally { + System.setOut(oldStdOut); + } } }
\ No newline at end of file diff --git a/src/scratchpad/testcases/org/apache/poi/hdgf/extractor/TestVisioExtractor.java b/src/scratchpad/testcases/org/apache/poi/hdgf/extractor/TestVisioExtractor.java index 5ee7bc9009..dc747a07a5 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdgf/extractor/TestVisioExtractor.java +++ b/src/scratchpad/testcases/org/apache/poi/hdgf/extractor/TestVisioExtractor.java @@ -27,6 +27,8 @@ import org.apache.poi.POIDataSamples; import org.apache.poi.hdgf.HDGFDiagram; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; public final class TestVisioExtractor { private static final POIDataSamples _dgTests = POIDataSamples.getDiagramInstance(); @@ -39,70 +41,61 @@ public final class TestVisioExtractor { */ @Test void testCreation() throws IOException { - VisioTextExtractor extractor1 = openExtractor(defFilename); - assertNotNull(extractor1); - assertNotNull(extractor1.getAllText()); - assertEquals(defTextChunks, extractor1.getAllText().length); - extractor1.close(); - - InputStream is2 = _dgTests.openResourceAsStream(defFilename); - POIFSFileSystem poifs2 = new POIFSFileSystem(is2); - is2.close(); - VisioTextExtractor extractor2 = new VisioTextExtractor(poifs2); - assertNotNull(extractor2); - assertNotNull(extractor2.getAllText()); - assertEquals(defTextChunks, extractor2.getAllText().length); - extractor2.close(); - poifs2.close(); - - InputStream is3 = _dgTests.openResourceAsStream(defFilename); - POIFSFileSystem poifs3 = new POIFSFileSystem(is3); - is3.close(); - HDGFDiagram hdgf3 = new HDGFDiagram(poifs3); - - - VisioTextExtractor extractor3 = new VisioTextExtractor(hdgf3); - assertNotNull(extractor3); - assertNotNull(extractor3.getAllText()); - assertEquals(defTextChunks, extractor3.getAllText().length); - extractor3.close(); - hdgf3.close(); - poifs3.close(); + try (VisioTextExtractor extractor1 = openExtractor(defFilename)) { + assertNotNull(extractor1); + assertNotNull(extractor1.getAllText()); + assertEquals(defTextChunks, extractor1.getAllText().length); + } + + try (InputStream is2 = _dgTests.openResourceAsStream(defFilename); + POIFSFileSystem poifs2 = new POIFSFileSystem(is2); + VisioTextExtractor extractor2 = new VisioTextExtractor(poifs2)) { + assertNotNull(extractor2); + assertNotNull(extractor2.getAllText()); + assertEquals(defTextChunks, extractor2.getAllText().length); + } + + try (InputStream is3 = _dgTests.openResourceAsStream(defFilename); + POIFSFileSystem poifs3 = new POIFSFileSystem(is3); + HDGFDiagram hdgf3 = new HDGFDiagram(poifs3); + VisioTextExtractor extractor3 = new VisioTextExtractor(hdgf3)) { + assertNotNull(extractor3); + assertNotNull(extractor3.getAllText()); + assertEquals(defTextChunks, extractor3.getAllText().length); + } } @Test void testExtraction() throws Exception { - VisioTextExtractor extractor = openExtractor(defFilename); - - // Check the array fetch - String[] text = extractor.getAllText(); - assertNotNull(text); - assertEquals(defTextChunks, text.length); - - assertEquals("text\n", text[0]); - assertEquals("View\n", text[1]); - assertEquals("Test View\n", text[2]); - assertEquals("I am a test view\n", text[3]); - assertEquals("Some random text, on a page\n", text[4]); - - // And the all-in fetch - String textS = extractor.getText(); - assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page\n", textS); - extractor.close(); + try (VisioTextExtractor extractor = openExtractor(defFilename)) { + + // Check the array fetch + String[] text = extractor.getAllText(); + assertNotNull(text); + assertEquals(defTextChunks, text.length); + + assertEquals("text\n", text[0]); + assertEquals("View\n", text[1]); + assertEquals("Test View\n", text[2]); + assertEquals("I am a test view\n", text[3]); + assertEquals("Some random text, on a page\n", text[4]); + + // And the all-in fetch + String textS = extractor.getText(); + assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page\n", textS); + } } - @Test - void testProblemFiles() throws Exception { - String[] files = { - "44594.vsd", "44594-2.vsd", - "ShortChunk1.vsd", "ShortChunk2.vsd", "ShortChunk3.vsd", - "NegativeChunkLength.vsd", "NegativeChunkLength2.vsd" - }; - for(String file : files){ - VisioTextExtractor ex = openExtractor(file); - ex.getText(); - ex.close(); - } + @ParameterizedTest + @ValueSource(strings = { + "44594.vsd", "44594-2.vsd", + "ShortChunk1.vsd", "ShortChunk2.vsd", "ShortChunk3.vsd", + "NegativeChunkLength.vsd", "NegativeChunkLength2.vsd" + }) + void testProblemFiles(String file) throws Exception { + try (VisioTextExtractor ex = openExtractor(file)) { + assertNotNull(ex.getText()); + } } private VisioTextExtractor openExtractor(String fileName) throws IOException { 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 5748d2575d..f7cd422ddb 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hdgf/streams/TestStreamBugs.java @@ -17,6 +17,9 @@ package org.apache.poi.hdgf.streams; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.io.IOException; import java.io.InputStream; @@ -44,14 +47,14 @@ public final class TestStreamBugs extends StreamTest { ptrFactory = new PointerFactory(11); chunkFactory = new ChunkFactory(11); - InputStream is = POIDataSamples.getDiagramInstance().openResourceAsStream("44594.vsd"); - filesystem = new POIFSFileSystem(is); - is.close(); + try (InputStream is = POIDataSamples.getDiagramInstance().openResourceAsStream("44594.vsd")) { + filesystem = new POIFSFileSystem(is); + } // Grab the document stream - InputStream is2 = filesystem.createDocumentInputStream("VisioDocument"); - contents = IOUtils.toByteArray(is2); - is2.close(); + try (InputStream is2 = filesystem.createDocumentInputStream("VisioDocument")) { + contents = IOUtils.toByteArray(is2); + } } @Test @@ -80,22 +83,19 @@ public final class TestStreamBugs extends StreamTest { // Get with recursing into chunks for (Pointer ptr : ptrs) { - Stream stream = - Stream.createStream(ptr, contents, chunkFactory, ptrFactory); + Stream stream = Stream.createStream(ptr, contents, chunkFactory, ptrFactory); if(stream instanceof ChunkStream) { ChunkStream cStream = (ChunkStream)stream; - cStream.findChunks(); + assertDoesNotThrow(cStream::findChunks); } } // Get with recursing into chunks and pointers for (Pointer ptr : ptrs) { - Stream stream = - Stream.createStream(ptr, contents, chunkFactory, ptrFactory); + Stream stream = Stream.createStream(ptr, contents, chunkFactory, ptrFactory); if(stream instanceof PointerContainingStream) { - PointerContainingStream pStream = - (PointerContainingStream)stream; - pStream.findChildren(contents); + PointerContainingStream pStream = (PointerContainingStream)stream; + assertDoesNotThrow(() -> pStream.findChildren(contents)); } } @@ -104,6 +104,8 @@ public final class TestStreamBugs extends StreamTest { @Test void testOpen() throws IOException { - new HDGFDiagram(filesystem).close(); + try (HDGFDiagram dia = new HDGFDiagram(filesystem)) { + assertEquals(20, dia.getTopLevelStreams().length); + } } } diff --git a/src/scratchpad/testcases/org/apache/poi/hmef/dev/TestHMEFDumper.java b/src/scratchpad/testcases/org/apache/poi/hmef/dev/TestHMEFDumper.java index d56b7bdc4f..a24901a96d 100644 --- a/src/scratchpad/testcases/org/apache/poi/hmef/dev/TestHMEFDumper.java +++ b/src/scratchpad/testcases/org/apache/poi/hmef/dev/TestHMEFDumper.java @@ -21,6 +21,7 @@ package org.apache.poi.hmef.dev; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.File; @@ -39,13 +40,13 @@ public class TestHMEFDumper { @Test void main() throws Exception { File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat"); - doMain(file.getAbsolutePath()); + assertDoesNotThrow(() -> doMain(file.getAbsolutePath())); } @Test void mainFull() throws Exception { File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat"); - doMain("--full", file.getAbsolutePath()); + assertDoesNotThrow(() -> doMain("--full", file.getAbsolutePath())); } private static void doMain(String... args) throws Exception { diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java b/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java index d7cc878fa4..5aec58964a 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java @@ -17,6 +17,9 @@ package org.apache.poi.hslf; +import static org.apache.poi.hslf.usermodel.HSLFSlideShow.POWERPOINT_DOCUMENT; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -26,121 +29,89 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; import org.apache.poi.POIDataSamples; import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl; import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.TempFile; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Tests that HSLFSlideShow writes the powerpoint bit of data back out * correctly. Currently, that means being the same as what it read in - * - * @author Nick Burch (nick at torchbox dot com) */ public final class TestReWrite { - // HSLFSlideShow primed on the test data - private HSLFSlideShowImpl hssA; - private HSLFSlideShowImpl hssB; - private HSLFSlideShowImpl hssC; - // POIFS primed on the test data - private POIFSFileSystem pfsA; - private POIFSFileSystem pfsB; - private POIFSFileSystem pfsC; - - @BeforeEach - void setUp() throws Exception { - - POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); - - pfsA = new POIFSFileSystem(slTests.openResourceAsStream("basic_test_ppt_file.ppt")); - hssA = new HSLFSlideShowImpl(pfsA); - - pfsB = new POIFSFileSystem(slTests.openResourceAsStream("ParagraphStylesShorterThanCharStyles.ppt")); - hssB = new HSLFSlideShowImpl(pfsB); - - pfsC = new POIFSFileSystem(slTests.openResourceAsStream("WithMacros.ppt")); - hssC = new HSLFSlideShowImpl(pfsC); - } - - @Test - void testWritesOutTheSame() throws Exception { - assertWritesOutTheSame(hssA, pfsA); - assertWritesOutTheSame(hssB, pfsB); - } - - void assertWritesOutTheSame(HSLFSlideShowImpl hss, POIFSFileSystem pfs) throws Exception { - // Write out to a byte array, and to a temp file - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - hss.write(baos); - - final File file = TempFile.createTempFile("TestHSLF", ".ppt"); - final File file2 = TempFile.createTempFile("TestHSLF", ".ppt"); - hss.write(file); - hss.write(file2); - - - // Build an input stream of it, and read back as a POIFS from the stream - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); - POIFSFileSystem npfS = new POIFSFileSystem(bais); - - // And the same on the temp file - POIFSFileSystem npfF = new POIFSFileSystem(file); - - // And another where we do an in-place write - POIFSFileSystem npfRF = new POIFSFileSystem(file2, false); - HSLFSlideShowImpl hssRF = new HSLFSlideShowImpl(npfRF); - hssRF.write(); - hssRF.close(); - npfRF = new POIFSFileSystem(file2); - - // Check all of them in turn - for (POIFSFileSystem npf : new POIFSFileSystem[] { npfS, npfF, npfRF }) { - // Check that the "PowerPoint Document" sections have the same size - DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT); - DocumentEntry nProps = (DocumentEntry)npf.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT); - assertEquals(oProps.getSize(),nProps.getSize()); - - // Check that they contain the same data - byte[] _oData = new byte[oProps.getSize()]; - byte[] _nData = new byte[nProps.getSize()]; - pfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_oData); - npf.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_nData); - for(int i=0; i<_oData.length; i++) { - //System.out.println(i + "\t" + Integer.toHexString(i)); - assertEquals(_oData[i], _nData[i]); + private static final POIDataSamples SAMPLES = POIDataSamples.getSlideShowInstance(); + + @ParameterizedTest + @ValueSource(strings = { "basic_test_ppt_file.ppt", "ParagraphStylesShorterThanCharStyles.ppt" }) + void testWritesOutTheSame(String testfile) throws Exception { + try (POIFSFileSystem pfs = new POIFSFileSystem(SAMPLES.openResourceAsStream(testfile)); + HSLFSlideShowImpl hss = new HSLFSlideShowImpl(pfs)) { + + // Write out to a byte array, and to a temp file + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + hss.write(baos); + + final File file = TempFile.createTempFile("TestHSLF", ".ppt"); + final File file2 = TempFile.createTempFile("TestHSLF", ".ppt"); + hss.write(file); + hss.write(file2); + + + // Build an input stream of it, and read back as a POIFS from the stream + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + try (POIFSFileSystem npfS = new POIFSFileSystem(bais); + // And the same on the temp file + POIFSFileSystem npfF = new POIFSFileSystem(file)) { + + // And another where we do an in-place write + try (POIFSFileSystem npfRF = new POIFSFileSystem(file2, false); + HSLFSlideShowImpl hssRF = new HSLFSlideShowImpl(npfRF)) { + hssRF.write(); + } + try (POIFSFileSystem npfRF = new POIFSFileSystem(file2)) { + // Check all of them in turn + for (POIFSFileSystem npf : new POIFSFileSystem[]{npfS, npfF, npfRF}) { + assertSame(pfs, npf); + } + } } - npf.close(); } } @Test void testWithMacroStreams() throws IOException { - // Check that they're apparently the same - assertSlideShowWritesOutTheSame(hssC, pfsC); - - // Currently has a Macros stream - assertNotNull( pfsC.getRoot().getEntry("Macros") ); + try (POIFSFileSystem pfsC = new POIFSFileSystem(SAMPLES.openResourceAsStream("WithMacros.ppt")); + HSLFSlideShowImpl hssC = new HSLFSlideShowImpl(pfsC)) { + // Check that they're apparently the same + assertSlideShowWritesOutTheSame(hssC, pfsC); + + // Currently has a Macros stream + assertNotNull(pfsC.getRoot().getEntry("Macros")); + + // Write out normally, will loose the macro stream + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + hssC.write(baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + try (POIFSFileSystem pfsNew = new POIFSFileSystem(bais)) { + assertFalse(pfsNew.getRoot().hasEntry("Macros")); + } - // Write out normally, will loose the macro stream - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - hssC.write(baos); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); - POIFSFileSystem pfsNew = new POIFSFileSystem(bais); - assertFalse(pfsNew.getRoot().hasEntry("Macros")); - pfsNew.close(); - - // But if we write out with nodes preserved, will be there - baos.reset(); - hssC.write(baos, true); - bais = new ByteArrayInputStream(baos.toByteArray()); - pfsNew = new POIFSFileSystem(bais); - assertTrue( pfsNew.getRoot().hasEntry("Macros") ); - pfsNew.close(); + // But if we write out with nodes preserved, will be there + baos.reset(); + hssC.write(baos, true); + bais = new ByteArrayInputStream(baos.toByteArray()); + try (POIFSFileSystem pfsNew = new POIFSFileSystem(bais)) { + assertTrue(pfsNew.getRoot().hasEntry("Macros")); + } + } } /** @@ -149,7 +120,10 @@ public final class TestReWrite { */ @Test void testSlideShowWritesOutTheSame() throws Exception { - assertSlideShowWritesOutTheSame(hssA, pfsA); + try (POIFSFileSystem pfsA = new POIFSFileSystem(SAMPLES.openResourceAsStream("basic_test_ppt_file.ppt")); + HSLFSlideShowImpl hssA = new HSLFSlideShowImpl(pfsA)) { + assertSlideShowWritesOutTheSame(hssA, pfsA); + } // Some bug in StyleTextPropAtom rewriting means this will fail // We need to identify and fix that first @@ -160,8 +134,8 @@ public final class TestReWrite { // Create a slideshow covering it @SuppressWarnings("resource") HSLFSlideShow ss = new HSLFSlideShow(hss); - ss.getSlides(); - ss.getNotes(); + assertDoesNotThrow(ss::getSlides); + assertDoesNotThrow(ss::getNotes); // Now write out to a byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -171,40 +145,46 @@ public final class TestReWrite { ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); // Use POIFS to query that lot - POIFSFileSystem npfs = new POIFSFileSystem(bais); + try (POIFSFileSystem npfs = new POIFSFileSystem(bais)) { + assertSame(pfs, npfs); + } + } + private void assertSame(POIFSFileSystem origPFS, POIFSFileSystem newPFS) throws IOException { // Check that the "PowerPoint Document" sections have the same size - DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT); - DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT); - assertEquals(oProps.getSize(),nProps.getSize()); + DocumentEntry oProps = (DocumentEntry) origPFS.getRoot().getEntry(POWERPOINT_DOCUMENT); + DocumentEntry nProps = (DocumentEntry) newPFS.getRoot().getEntry(POWERPOINT_DOCUMENT); + assertEquals(oProps.getSize(), nProps.getSize()); + // Check that they contain the same data - byte[] _oData = new byte[oProps.getSize()]; - byte[] _nData = new byte[nProps.getSize()]; - pfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_oData); - npfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_nData); - for(int i=0; i<_oData.length; i++) { - if(_oData[i] != _nData[i]) - System.out.println(i + "\t" + Integer.toHexString(i)); - assertEquals(_oData[i], _nData[i]); + try (InputStream os = origPFS.createDocumentInputStream(POWERPOINT_DOCUMENT); + InputStream ns = newPFS.createDocumentInputStream(POWERPOINT_DOCUMENT)) { + + byte[] _oData = IOUtils.toByteArray(os, oProps.getSize()); + byte[] _nData = IOUtils.toByteArray(ns, nProps.getSize()); + + assertArrayEquals(_oData, _nData); } - npfs.close(); } + @Test void test48593() throws IOException { - HSLFSlideShow ppt1 = new HSLFSlideShow(); - ppt1.createSlide(); - HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1); - ppt2.createSlide(); - HSLFSlideShow ppt3 = HSLFTestDataSamples.writeOutAndReadBack(ppt2); - ppt3.createSlide(); - HSLFSlideShow ppt4 = HSLFTestDataSamples.writeOutAndReadBack(ppt3); - ppt4.createSlide(); - HSLFTestDataSamples.writeOutAndReadBack(ppt4).close(); - ppt4.close(); - ppt3.close(); - ppt2.close(); - ppt1.close(); + try (HSLFSlideShow ppt1 = new HSLFSlideShow()) { + ppt1.createSlide(); + try (HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1)) { + ppt2.createSlide(); + try (HSLFSlideShow ppt3 = HSLFTestDataSamples.writeOutAndReadBack(ppt2)) { + ppt3.createSlide(); + try (HSLFSlideShow ppt4 = HSLFTestDataSamples.writeOutAndReadBack(ppt3)) { + ppt4.createSlide(); + try (HSLFSlideShow ppt5 = HSLFTestDataSamples.writeOutAndReadBack(ppt4)) { + assertEquals(4, ppt5.getSlides().size()); + } + } + } + } + } } } 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 6b385b8e20..bd8d634b12 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java @@ -29,15 +29,20 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.security.MessageDigest; import java.util.BitSet; import java.util.List; import com.zaxxer.sparsebits.SparseBitSet; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.binary.Hex; import org.apache.poi.POIDataSamples; import org.apache.poi.hslf.usermodel.HSLFObjectShape; import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hwpf.HWPFDocument; +import org.apache.poi.poifs.crypt.CryptoFunctions; +import org.apache.poi.poifs.crypt.HashAlgorithm; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.sl.extractor.SlideShowExtractor; @@ -45,6 +50,7 @@ import org.apache.poi.sl.usermodel.ObjectShape; import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.util.IOUtils; +import org.apache.poi.util.NullOutputStream; import org.junit.jupiter.api.Test; /** @@ -231,9 +237,15 @@ public final class TestExtractor { @Test void test52991() throws IOException { try (SlideShowExtractor<?,?> ppe = openExtractor("badzip.ppt")) { - for (ObjectShape<?,?> shape : ppe.getOLEShapes()) { - IOUtils.copy(shape.getObjectData().getInputStream(), new ByteArrayOutputStream()); + List<? extends ObjectShape<?, ?>> shapes = ppe.getOLEShapes(); + assertEquals(1, shapes.size()); + MessageDigest sha2 = CryptoFunctions.getMessageDigest(HashAlgorithm.sha256); + try (InputStream is = shapes.get(0).getObjectData().getInputStream()) { + sha2.update(IOUtils.toByteArray(is)); } + String exp = "lIRRfGMin6B4++WR4XvA82usdQ3ijeHBHU85j523sKY="; + String act = Base64.encodeBase64String(sha2.digest()); + assertEquals(exp, act); } } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java index 21d8a5fffc..17ddfa4a8a 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java @@ -20,6 +20,7 @@ package org.apache.poi.hslf.usermodel; import static org.apache.poi.POITestCase.assertContains; import static org.apache.poi.POITestCase.assertStartsWith; import static org.apache.poi.hslf.HSLFTestDataSamples.writeOutAndReadBack; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -446,7 +447,7 @@ public final class TestBugs { /* Iterate over slides and extract text */ for (HSLFSlide slide : ppt.getSlides()) { HeadersFooters hf = slide.getHeadersFooters(); - hf.isHeaderVisible(); // exception happens here + assertDoesNotThrow(hf::isHeaderVisible); } } } |