diff --git a/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java b/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java index d4d44a836b..acbf365046 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java @@ -27,19 +27,12 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assumptions.assumeFalse; import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics2D; -import java.awt.RenderingHints; -import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; -import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URI; @@ -56,8 +49,6 @@ import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; -import javax.imageio.ImageIO; - import org.apache.poi.POIDataSamples; import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.ooxml.POIXMLDocumentPart; @@ -66,6 +57,7 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackagingURIHelper; +import org.apache.poi.sl.draw.DrawFactory; import org.apache.poi.sl.draw.DrawPaint; import org.apache.poi.sl.extractor.SlideShowExtractor; import org.apache.poi.sl.usermodel.Hyperlink; @@ -1051,4 +1043,41 @@ class TestXSLFBugs { assertEquals("Open Jakarta POI HSSF module test ", strings.get(1)); } } + + + @Test + void bug59056() throws IOException { + assumeFalse(xslfOnly); + + final double[][] clips = { + { 50.999999999999986, 51.0, 298.0, 98.0 }, + { 51.00000000000003, 51.0, 298.0, 98.0 }, + { 51.0, 51.0, 298.0, 98.0 }, + { 250.02000796164992, 93.10370370370373, 78.61839367617523, 55.89629629629627 }, + { 79.58198774450841, 53.20887318960063, 109.13118501448272, 9.40935058567127 }, + }; + + DummyGraphics2d dgfx = new DummyGraphics2d(new NullPrintStream()) { + int idx = 0; + @Override + public void clip(java.awt.Shape s) { + assertTrue(s instanceof Rectangle2D); + Rectangle2D r = (Rectangle2D)s; + + double[] clip = clips[idx++]; + assertEquals(clip[0], r.getX(), 0.5); + assertEquals(clip[1], r.getY(), 0.5); + assertEquals(clip[2], r.getWidth(), 0.5); + assertEquals(clip[3], r.getHeight(), 0.5); + } + }; + + Rectangle2D box = new Rectangle2D.Double(51, 51, 298, 98); + DrawFactory df = DrawFactory.getInstance(dgfx); + + try (SlideShow ppt = SlideShowFactory.create(slTests.getFile("54541_cropped_bitmap.ppt"))) { + ppt.getSlides().get(0).getShapes().forEach(shape -> df.drawShape(dgfx, shape, box)); + } + } + } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestLine.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestLine.java index 4456dc5b22..0384b19043 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestLine.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestLine.java @@ -17,10 +17,17 @@ package org.apache.poi.hslf.model; -import java.awt.Color; -import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.awt.Color; +import java.awt.Rectangle; +import java.awt.geom.Rectangle2D; +import java.io.IOException; +import java.util.Arrays; + +import org.apache.poi.hslf.HSLFTestDataSamples; import org.apache.poi.hslf.usermodel.HSLFLine; +import org.apache.poi.hslf.usermodel.HSLFShape; import org.apache.poi.hslf.usermodel.HSLFSlide; import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.poi.sl.usermodel.StrokeStyle.LineCompound; @@ -32,101 +39,73 @@ import org.junit.jupiter.api.Test; */ public final class TestLine { + private static final Object[][] lines = { + // line styles + { 75, 200, LineCompound.SINGLE, Color.blue }, + { 75, 230, LineCompound.DOUBLE, 3.5 }, + { 75, 260, LineCompound.TRIPLE, 6d }, + { 75, 290, LineCompound.THICK_THIN, 4.5d }, + { 75, 320, LineCompound.THIN_THICK, 5.5d }, + // line dashing + { 450, 200, LineDash.SOLID }, + { 450, 230, LineDash.DASH }, + { 450, 260, LineDash.DOT }, + { 450, 290, LineDash.DASH_DOT }, + { 450, 320, LineDash.LG_DASH_DOT_DOT }, + // Combinations + { 75, 400, LineDash.DASH_DOT, LineCompound.TRIPLE, 5d }, + { 75, 430, LineDash.DASH, LineCompound.THICK_THIN, 4d }, + { 75, 460, LineDash.DOT, LineCompound.DOUBLE, 8d } + }; + + @Test void testCreateLines() throws IOException { - HSLFSlideShow ppt = new HSLFSlideShow(); - HSLFSlide slide = ppt.createSlide(); + try (HSLFSlideShow ppt1 = new HSLFSlideShow()) { + HSLFSlide slide1 = ppt1.createSlide(); + slide1.addTitle().setText("Lines tester"); - slide.addTitle().setText("Lines tester"); + for (Object[] line : lines) { + HSLFLine hslfLine = new HSLFLine(); + hslfLine.setAnchor(new Rectangle((Integer)line[0], (Integer)line[1], 300, 0)); + for (Object attr : Arrays.copyOfRange(line, 2, line.length)) { + if (attr instanceof LineCompound) { + hslfLine.setLineCompound((LineCompound)attr); + } else if (attr instanceof Double) { + hslfLine.setLineWidth((Double)attr); + } else if (attr instanceof Color) { + hslfLine.setLineColor((Color)attr); + } else if (attr instanceof LineDash) { + hslfLine.setLineDash((LineDash)attr); + } + } + slide1.addShape(hslfLine); + } - HSLFLine line; + try (HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1)) { + HSLFSlide slide2 = ppt2.getSlides().get(0); - /** - * line styles - */ - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(75, 200, 300, 0)); - line.setLineCompound(LineCompound.SINGLE); - line.setLineColor(Color.blue); - slide.addShape(line); - - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(75, 230, 300, 0)); - line.setLineCompound(LineCompound.DOUBLE); - line.setLineWidth(3.5); - slide.addShape(line); - - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(75, 260, 300, 0)); - line.setLineCompound(LineCompound.TRIPLE); - line.setLineWidth(6); - slide.addShape(line); - - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(75, 290, 300, 0)); - line.setLineCompound(LineCompound.THICK_THIN); - line.setLineWidth(4.5); - slide.addShape(line); - - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(75, 320, 300, 0)); - line.setLineCompound(LineCompound.THIN_THICK); - line.setLineWidth(5.5); - slide.addShape(line); - - /** - * line dashing - */ - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(450, 200, 300, 0)); - line.setLineDash(LineDash.SOLID); - slide.addShape(line); - - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(450, 230, 300, 0)); - line.setLineDash(LineDash.DASH); - slide.addShape(line); - - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(450, 260, 300, 0)); - line.setLineDash(LineDash.DOT); - slide.addShape(line); - - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(450, 290, 300, 0)); - line.setLineDash(LineDash.DASH_DOT); - slide.addShape(line); - - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(450, 320, 300, 0)); - line.setLineDash(LineDash.LG_DASH_DOT_DOT); - slide.addShape(line); - - /** - * Combinations - */ - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(75, 400, 300, 0)); - line.setLineDash(LineDash.DASH_DOT); - line.setLineCompound(LineCompound.TRIPLE); - line.setLineWidth(5.0); - slide.addShape(line); - - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(75, 430, 300, 0)); - line.setLineDash(LineDash.DASH); - line.setLineCompound(LineCompound.THICK_THIN); - line.setLineWidth(4.0); - slide.addShape(line); - - line = new HSLFLine(); - line.setAnchor(new java.awt.Rectangle(75, 460, 300, 0)); - line.setLineDash(LineDash.DOT); - line.setLineCompound(LineCompound.DOUBLE); - line.setLineWidth(8.0); - slide.addShape(line); - - ppt.close(); + int idx = 0; + for (HSLFShape shape : slide2.getShapes().subList(1,14)) { + HSLFLine hslfLine = (HSLFLine)shape; + Object[] line = lines[idx++]; + Rectangle2D anchor = hslfLine.getAnchor(); + assertEquals(line[0], (int)anchor.getX()); + assertEquals(line[1], (int)anchor.getY()); + for (Object attr : Arrays.copyOfRange(line, 2, line.length)) { + if (attr instanceof LineCompound) { + assertEquals(attr, hslfLine.getLineCompound()); + } else if (attr instanceof Double) { + assertEquals(attr, hslfLine.getLineWidth()); + } else if (attr instanceof Color) { + assertEquals(attr, hslfLine.getLineColor()); + } else if (attr instanceof LineDash) { + assertEquals(attr, hslfLine.getLineDash()); + } + } + } + } + } } } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java index b8117a38a7..887814ab43 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java @@ -17,14 +17,23 @@ package org.apache.poi.hslf.record; -import static org.junit.jupiter.api.Assertions.*; +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; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import org.apache.poi.hslf.exceptions.HSLFException; -import org.apache.poi.hslf.model.textproperties.*; +import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp; +import org.apache.poi.hslf.model.textproperties.TextProp; +import org.apache.poi.hslf.model.textproperties.TextPropCollection; import org.apache.poi.util.HexDump; import org.junit.jupiter.api.Test; @@ -702,7 +711,7 @@ public final class TestStyleTextPropAtom { // all the properties the mask says we have // Make sure we just do the best we can StyleTextPropAtom stpc = new StyleTextPropAtom(data_c,0,data_c.length); - stpc.setParentTextSize(data_c_text_len); + assertDoesNotThrow(() -> stpc.setParentTextSize(data_c_text_len)); // If we get here, we didn't break } 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 140501344a..21d8a5fffc 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java @@ -28,11 +28,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.awt.Color; -import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; -import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -67,7 +65,6 @@ import org.apache.poi.hslf.record.VBAInfoAtom; import org.apache.poi.hslf.record.VBAInfoContainer; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.macros.VBAMacroReader; -import org.apache.poi.sl.draw.DrawFactory; import org.apache.poi.sl.draw.DrawPaint; import org.apache.poi.sl.extractor.SlideShowExtractor; import org.apache.poi.sl.usermodel.ColorStyle; @@ -87,6 +84,9 @@ import org.apache.poi.util.LittleEndian; import org.apache.poi.util.StringUtil; import org.apache.poi.util.Units; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; /** * Testcases for bugs entered in bugzilla @@ -220,19 +220,6 @@ public final class TestBugs { } } - /** - * Bug 42486: Failure parsing a seemingly valid PPT - */ - @SuppressWarnings("unused") - @Test - void bug42486 () throws IOException { - try (HSLFSlideShow ppt = open("42486.ppt")) { - for (HSLFSlide slide : ppt.getSlides()) { - List shape = slide.getShapes(); - } - } - } - /** * Bug 42524: NPE in Shape.getShapeType() */ @@ -259,7 +246,7 @@ public final class TestBugs { */ @SuppressWarnings("unused") @Test - void bug42520 () throws IOException { + void bug42520() throws IOException { try (HSLFSlideShow ppt = open("42520.ppt")) { //test case from the bug report @@ -267,6 +254,8 @@ public final class TestBugs { HSLFPictureShape picture = (HSLFPictureShape) shapeGroup.getShapes().get(0); picture.getPictureData(); + boolean found = false; + //walk down the tree and see if there were no errors while reading for (HSLFSlide slide : ppt.getSlides()) { for (HSLFShape shape : slide.getShapes()) { @@ -275,11 +264,15 @@ public final class TestBugs { for (HSLFShape comp : group.getShapes()) { if (comp instanceof HSLFPictureShape) { HSLFPictureData pict = ((HSLFPictureShape) comp).getPictureData(); + assertEquals("Rectangle 35893", comp.getShapeName()); + found = true; } } } } } + + assertTrue(found); } } @@ -363,17 +356,6 @@ public final class TestBugs { } } - /** - * Bug 44770: java.lang.RuntimeException: Couldn't instantiate the class for - * type with id 1036 on class class org.apache.poi.hslf.record.PPDrawing - */ - @Test - void bug44770() throws IOException { - try (HSLFSlideShow ppt = open("44770.ppt")) { - assertNotNull(ppt.getSlides().get(0)); - } - } - /** * Bug 41071: Will not extract text from Powerpoint TextBoxes */ @@ -413,35 +395,18 @@ public final class TestBugs { try (HSLFSlideShow ppt = open("49648.ppt")) { for (HSLFSlide slide : ppt.getSlides()) { for (List run : slide.getTextParagraphs()) { + String repl = "With \u0123\u1234\u5678 unicode"; String text = HSLFTextParagraph.getRawText(run); - text = text.replace("{txtTot}", "With \u0123\u1234\u5678 unicode"); + text = text.replace("{txtTot}", repl); HSLFTextParagraph.setText(run, text); + if (text.contains(repl)) { + assertTrue(HSLFTextParagraph.getText(run).contains(repl)); + } } } } } - /** - * Bug 41246: AIOOB with illegal note references - */ - @Test - void bug41246a() throws IOException { - try (HSLFSlideShow ppt = open("41246-1.ppt")) { - try (HSLFSlideShow ppt2 = writeOutAndReadBack(ppt)) { - assertNotNull(ppt2.getSlides().get(0)); - } - } - } - - @Test - void bug41246b() throws IOException { - try (HSLFSlideShow ppt = open("41246-2.ppt")) { - try (HSLFSlideShow ppt2 = writeOutAndReadBack(ppt)) { - assertNotNull(ppt2.getSlides().get(0)); - } - } - } - /** * Bug 45776: Fix corrupt file problem using TextRun.setText */ @@ -523,26 +488,6 @@ public final class TestBugs { } } - @Test - void bug37625() throws IOException { - try (HSLFSlideShow ppt1 = open("37625.ppt"); - HSLFSlideShow ppt2 = writeOutAndReadBack(ppt1)) { - assertEquals(29, ppt1.getSlides().size()); - assertNotNull(ppt2); - assertEquals(29, ppt2.getSlides().size()); - } - } - - @Test - void bug57272() throws IOException { - try (HSLFSlideShow ppt1 = open("57272_corrupted_usereditatom.ppt"); - HSLFSlideShow ppt2 = writeOutAndReadBack(ppt1)) { - assertEquals(6, ppt1.getSlides().size()); - assertNotNull(ppt2); - assertEquals(6, ppt2.getSlides().size()); - } - } - @Test void bug49541() throws IOException { try (HSLFSlideShow ppt = open("49541_symbol_map.ppt")) { @@ -554,21 +499,36 @@ public final class TestBugs { } } - @Test - void bug47261() throws IOException { - try (HSLFSlideShow ppt = open("bug47261.ppt")) { + @ParameterizedTest + @CsvSource({ + // bug47261.ppt has actually 16 slides, but also non-conforming multiple document records + "bug47261.ppt, 1", + "bug56240.ppt, 105", + "bug58516.ppt, 5", + "57272_corrupted_usereditatom.ppt, 6", + "37625.ppt, 29", + // Bug 41246: AIOOB with illegal note references + "41246-1.ppt, 36", + "41246-2.ppt, 16", + // Bug 44770: java.lang.RuntimeException: Couldn't instantiate the class for + // type with id 1036 on class class org.apache.poi.hslf.record.PPDrawing + "44770.ppt, 19", + // Bug 42486: Failure parsing a seemingly valid PPT + "42486.ppt, 33" + }) + void testFile(String file, int slideCnt) throws IOException { + try (HSLFSlideShow ppt = open(file)) { + for (HSLFSlide slide : ppt.getSlides()) { + List shape = slide.getShapes(); + assertFalse(shape.isEmpty()); + } + + assertNotNull(ppt.getSlides().get(0)); ppt.removeSlide(0); ppt.createSlide(); - writeOutAndReadBack(ppt).close(); - } - } - - @Test - void bug56240() throws IOException { - try (HSLFSlideShow ppt = open("bug56240.ppt")) { - int slideCnt = ppt.getSlides().size(); - assertEquals(105, slideCnt); - writeOutAndReadBack(ppt).close(); + try (HSLFSlideShow ppt2 = writeOutAndReadBack(ppt)) { + assertEquals(slideCnt, ppt2.getSlides().size()); + }; } } @@ -600,11 +560,6 @@ public final class TestBugs { } } - @Test - void bug58516() throws IOException { - open("bug58516.ppt").close(); - } - @Test void bug45124() throws IOException { try (HSLFSlideShow ppt = open("bug45124.ppt")) { @@ -771,23 +726,16 @@ public final class TestBugs { } } - @Test - void bug58718() throws IOException { - String[] files = {"bug58718_008524.ppt", "bug58718_008558.ppt", "bug58718_349008.ppt", "bug58718_008495.ppt",}; - for (String f : files) { - File sample = HSLFTestDataSamples.getSampleFile(f); - try (SlideShowExtractor ex = new SlideShowExtractor<>(SlideShowFactory.create(sample))) { - assertNotNull(ex.getText()); - } - } - } - - @Test - void bug58733() throws IOException { - File sample = HSLFTestDataSamples.getSampleFile("bug58733_671884.ppt"); + @ParameterizedTest + @ValueSource(strings = { + "bug58718_008524.ppt", "bug58718_008558.ppt", "bug58718_349008.ppt", "bug58718_008495.ppt", + "bug58733_671884.ppt" + }) + void bug58718(String file) throws IOException { + File sample = HSLFTestDataSamples.getSampleFile(file); try (SlideShowExtractor ex = new SlideShowExtractor<>(SlideShowFactory.create(sample))) { - assertNotNull(ex.getText()); - } + assertNotNull(ex.getText()); + } } @Test @@ -832,23 +780,6 @@ public final class TestBugs { } } - @Test - void bug59056() throws IOException { - try (HSLFSlideShow ppt = open("54541_cropped_bitmap.ppt")) { - for (HSLFShape shape : ppt.getSlides().get(0).getShapes()) { - BufferedImage img = new BufferedImage(500, 300, BufferedImage.TYPE_INT_ARGB); - Graphics2D graphics = img.createGraphics(); - Rectangle2D box = new Rectangle2D.Double(50, 50, 300, 100); - graphics.setPaint(Color.red); - graphics.fill(box); - box = new Rectangle2D.Double(box.getX() + 1, box.getY() + 1, box.getWidth() - 2, box.getHeight() - 2); - DrawFactory.getInstance(graphics).drawShape(graphics, shape, box); - graphics.dispose(); - } - } - - } - private static HSLFSlideShow open(String fileName) throws IOException { File sample = HSLFTestDataSamples.getSampleFile(fileName); // Note: don't change the code here, it is required for Eclipse to compile the code diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestTextRun.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestTextRun.java index cd1bdec759..6fc4dc6030 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestTextRun.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestTextRun.java @@ -574,12 +574,13 @@ public final class TestTextRun { @Test void testAppendEmpty() throws IOException { - HSLFSlideShow ppt = new HSLFSlideShow(); - HSLFSlide s = ppt.createSlide(); - HSLFTextBox title = s.addTitle(); - title.setText(""); - title.appendText("\n", true); - title.appendText("para", true); - ppt.close(); + try (HSLFSlideShow ppt = new HSLFSlideShow()) { + HSLFSlide s = ppt.createSlide(); + HSLFTextBox title = s.addTitle(); + title.setText(""); + title.appendText("\n", true); + title.appendText("para", true); + assertEquals("\npara", title.getText()); + } } } diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/TestFixedSizedProperties.java b/src/scratchpad/testcases/org/apache/poi/hsmf/TestFixedSizedProperties.java index 4db632fb65..04131f3a23 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/TestFixedSizedProperties.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/TestFixedSizedProperties.java @@ -18,14 +18,13 @@ package org.apache.poi.hsmf; import static org.apache.poi.POITestCase.assertContains; +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; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.PrintStream; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashSet; @@ -45,6 +44,7 @@ import org.apache.poi.hsmf.dev.HSMFDump; import org.apache.poi.hsmf.extractor.OutlookTextExtractor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.NullPrintStream; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -168,19 +168,17 @@ public final class TestFixedSizedProperties { */ @Test void testReadMessageDateSucceedsWithHSMFDump() throws IOException { - PrintStream stream = new PrintStream(new ByteArrayOutputStream(), true, "ISO-8859-1"); HSMFDump dump = new HSMFDump(fsMessageSucceeds); - dump.dump(stream); + assertDoesNotThrow(() -> dump.dump(new NullPrintStream())); } /** * Test to see if we can read the Date Chunk with HSMFDump. */ @Test - void testReadMessageDateFailsWithHSMFDump() throws Exception { - PrintStream stream = new PrintStream(new ByteArrayOutputStream(), true, "ISO-8859-1"); + void testReadMessageDateFailsWithHSMFDump() { HSMFDump dump = new HSMFDump(fsMessageFails); - dump.dump(stream); + assertDoesNotThrow(() -> dump.dump(new NullPrintStream())); } /** diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/HWPFTestDataSamples.java b/src/scratchpad/testcases/org/apache/poi/hwpf/HWPFTestDataSamples.java index 8f1e8c12c6..ea5a499111 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/HWPFTestDataSamples.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/HWPFTestDataSamples.java @@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.net.URL; import java.util.zip.ZipInputStream; import org.apache.poi.POIDataSamples; @@ -87,51 +86,6 @@ public class HWPFTestDataSamples { } } - /** - * Open a remote sample from URL. opening is performd in two phases: - * (1) download content into a byte array - * (2) construct HWPFDocument - * - * @param sampleFileUrl the url to open - */ - public static HWPFDocument openRemoteFile( String sampleFileUrl ) - { - final long start = System.currentTimeMillis(); - try - { - logger.log(POILogger.DEBUG, "Downloading ", sampleFileUrl, " ..."); - - try (InputStream is = new URL(sampleFileUrl).openStream()) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - IOUtils.copy(is, baos); - } finally { - baos.close(); - } - - final long endDownload = System.currentTimeMillis(); - byte[] byteArray = baos.toByteArray(); - - logger.log(POILogger.DEBUG, "Downloaded in ", - Long.valueOf(endDownload - start), " ms -- ", - Long.valueOf(byteArray.length), " byte(s)"); - - ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); - HWPFDocument doc = new HWPFDocument(bais); - final long endParse = System.currentTimeMillis(); - - logger.log(POILogger.DEBUG, "Parsed in ", - Long.valueOf(endParse - start), " ms"); - - return doc; - } - } - catch ( IOException e ) - { - throw new RuntimeException( e ); - } - } - public static HWPFOldDocument openOldSampleFile(String sampleFileName) { try { InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream(sampleFileName); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/TestHWPFPictures.java b/src/scratchpad/testcases/org/apache/poi/hwpf/TestHWPFPictures.java index 30d4e330f9..9a9b942a25 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/TestHWPFPictures.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/TestHWPFPictures.java @@ -32,56 +32,27 @@ import javax.imageio.ImageIO; import org.apache.poi.POIDataSamples; import org.apache.poi.hwpf.model.PicturesTable; import org.apache.poi.hwpf.usermodel.Picture; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; /** * Test picture support in HWPF */ public final class TestHWPFPictures { - private String docAFile; - private String docBFile; - private String docCFile; - private String docDFile; - - private String imgAFile; - private String imgBFile; - private String imgCFile; - private String imgDFile; - - @BeforeEach - void setUp() { - docAFile = "testPictures.doc"; - docBFile = "two_images.doc"; - docCFile = "vector_image.doc"; - docDFile = "GaiaTest.doc"; - - imgAFile = "simple_image.jpg"; - imgBFile = "simple_image.png"; - imgCFile = "vector_image.emf"; - imgDFile = "GaiaTestImg.png"; - + @BeforeAll + static void setUp() { // we use ImageIO in one of the tests here so we should ensure that the temporary directory is created correctly File tempDir = new File(System.getProperty("java.io.tmpdir")); assertTrue( tempDir.exists() || tempDir.mkdirs(), "Could not create temporary directory " + tempDir.getAbsolutePath() + ": " + tempDir.exists() + "/" + tempDir.isDirectory() ); } - /** - * Test just opening the files - */ - @Test - void testOpen() { - HWPFTestDataSamples.openSampleFile(docAFile); - HWPFTestDataSamples.openSampleFile(docBFile); - } - /** * Test that we have the right numbers of images in each file */ @Test void testImageCount() { - HWPFDocument docA = HWPFTestDataSamples.openSampleFile(docAFile); - HWPFDocument docB = HWPFTestDataSamples.openSampleFile(docBFile); + HWPFDocument docA = HWPFTestDataSamples.openSampleFile("testPictures.doc"); + HWPFDocument docB = HWPFTestDataSamples.openSampleFile("two_images.doc"); assertNotNull(docA.getPicturesTable()); assertNotNull(docB.getPicturesTable()); @@ -101,7 +72,7 @@ public final class TestHWPFPictures { */ @Test void testImageData() { - HWPFDocument docB = HWPFTestDataSamples.openSampleFile(docBFile); + HWPFDocument docB = HWPFTestDataSamples.openSampleFile("two_images.doc"); PicturesTable picB = docB.getPicturesTable(); List picturesB = picB.getAllPictures(); @@ -114,8 +85,8 @@ public final class TestHWPFPictures { assertNotNull(pic2); // Check the same - byte[] pic1B = readFile(imgAFile); - byte[] pic2B = readFile(imgBFile); + byte[] pic1B = readFile("simple_image.jpg"); + byte[] pic2B = readFile("simple_image.png"); assertArrayEquals(pic1B, pic1.getContent()); assertArrayEquals(pic2B, pic2.getContent()); @@ -126,7 +97,7 @@ public final class TestHWPFPictures { */ @Test void testCompressedImageData() { - HWPFDocument docC = HWPFTestDataSamples.openSampleFile(docCFile); + HWPFDocument docC = HWPFTestDataSamples.openSampleFile("vector_image.doc"); PicturesTable picC = docC.getPicturesTable(); List picturesC = picC.getAllPictures(); @@ -136,7 +107,7 @@ public final class TestHWPFPictures { assertNotNull(pic); // Check the same - byte[] picBytes = readFile(imgCFile); + byte[] picBytes = readFile("vector_image.emf"); assertArrayEquals(picBytes, pic.getContent()); } @@ -171,14 +142,14 @@ public final class TestHWPFPictures { */ @Test void testEscherDrawing() { - HWPFDocument docD = HWPFTestDataSamples.openSampleFile(docDFile); + HWPFDocument docD = HWPFTestDataSamples.openSampleFile("GaiaTest.doc"); List allPictures = docD.getPicturesTable().getAllPictures(); assertEquals(1, allPictures.size()); Picture pic = allPictures.get(0); assertNotNull(pic); - byte[] picD = readFile(imgDFile); + byte[] picD = readFile("GaiaTestImg.png"); assertEquals(picD.length, pic.getContent().length); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java index c2524ad6f4..a7e8b1fec7 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java @@ -17,7 +17,8 @@ package org.apache.poi.hwpf.converter; import static org.apache.poi.POITestCase.assertContains; -import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.apache.poi.POITestCase.assertNotContained; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.io.StringWriter; @@ -29,223 +30,114 @@ import javax.xml.transform.stream.StreamResult; import org.apache.poi.POIDataSamples; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.util.XMLHelper; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.w3c.dom.Document; /** * Test cases for {@link WordToHtmlConverter} */ public class TestWordToHtmlConverter { - private static String getHtmlText(final String sampleFileName) throws Exception { - return getHtmlText(sampleFileName, false); + private static final POIDataSamples SAMPLES = POIDataSamples.getDocumentInstance(); + + @ParameterizedTest + @CsvSource({ + "AIOOB-Tap.doc, ", + "Bug33519.doc, " + + "\u041F\u043B\u0430\u043D\u0438\u043D\u0441\u043A\u0438 \u0442\u0443\u0440\u043E\u0432\u0435|" + + "\u042F\u0432\u043E\u0440 \u0410\u0441\u0435\u043D\u043E\u0432", + "Bug46610_2.doc, 012345678911234567892123456789312345678941234567890123456789112345678921234567893123456789412345678", + "Bug46817.doc,
", + "Bug47286.doc, " + + "!FORMTEXT|" + + "color:#4f6228;|" + + "Passport No and the date of expire|" + + "mfa.gov.cy", + "Bug48075.doc, \u041F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u0435 \u21162", + "innertable.doc, A", + "o_kurs.doc, \u0412\u0441\u0435 \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B \u043D\u0443\u043C\u0435\u0440\u0443\u044E\u0442\u0441\u044F", + "Bug52583.doc, ", + "Bug53182.doc, !italic", + "documentProperties.doc, " + + "This is document title|" + + "", + // email hyperlink + "Bug47286.doc, provisastpet@mfa.gov.cy", + "endingnote.doc, " + + "1|" + + "1", + "hyperlink.doc, " + + "Before text; Hyperlink text|" + + "; after text", + "lists-margins.doc, " + + ".s1{display: inline-block; text-indent: 0; min-width: 0.4861111in;}|" + + ".s2{display: inline-block; text-indent: 0; min-width: 0.23055555in;}|" + + ".s3{display: inline-block; text-indent: 0; min-width: 0.28541666in;}|" + + ".s4{display: inline-block; text-indent: 0; min-width: 0.28333333in;}|" + + ".p4{text-indent:-0.59652776in;margin-left:-0.70069444in;", + "pageref.doc, " + + "|" + + "|" + + "1", + "table-merges.doc, " + + "
|" + + "", + "52420.doc, " + + "!FORMTEXT|" + + "\u0417\u0410\u0414\u0410\u041d\u0418\u0415|" + + "\u041f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u044c", + "picture.doc, " + + "src=\"0.emf\"|" + + "width:3.1293333in;height:1.7247736in;|" + + "left:-0.09433333;top:-0.2573611;|" + + "width:3.4125in;height:2.3253334in;", + "pictures_escher.doc, " + + "|" + + "" + + }) + void testFile(String file, String contains) throws Exception { + boolean emulatePictureStorage = file.contains("picture"); + + String result = getHtmlText(file, emulatePictureStorage); + assertNotNull(result); + // starting with JDK 9 such unimportant whitespaces may be trimmed + result = result.replace(" suggestedName); - } + try (HWPFDocument hwpfDocument = new HWPFDocument(SAMPLES.openResourceAsStream(sampleFileName))) { + if (emulatePictureStorage) { + wordToHtmlConverter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> suggestedName); + } - wordToHtmlConverter.processDocument(hwpfDocument); + wordToHtmlConverter.processDocument(hwpfDocument); - StringWriter stringWriter = new StringWriter(); + StringWriter stringWriter = new StringWriter(); - Transformer transformer = XMLHelper.newTransformer(); - transformer.setOutputProperty(OutputKeys.METHOD, "html"); - transformer.transform( + Transformer transformer = XMLHelper.newTransformer(); + transformer.setOutputProperty(OutputKeys.METHOD, "html"); + transformer.transform( new DOMSource(wordToHtmlConverter.getDocument()), new StreamResult(stringWriter)); - return stringWriter.toString(); + return stringWriter.toString(); + } } - @Test - void testAIOOBTap() throws Exception { - String result = getHtmlText("AIOOB-Tap.doc"); - assertContains(result, ""); - } - @Test - void testBug33519() throws Exception { - String result = getHtmlText("Bug33519.doc"); - assertContains( - result, - "\u041F\u043B\u0430\u043D\u0438\u043D\u0441\u043A\u0438 \u0442\u0443\u0440\u043E\u0432\u0435"); - assertContains(result, - "\u042F\u0432\u043E\u0440 \u0410\u0441\u0435\u043D\u043E\u0432"); - } - - @Test - void testBug46610_2() throws Exception { - String result = getHtmlText("Bug46610_2.doc"); - assertContains( - result, - "012345678911234567892123456789312345678941234567890123456789112345678921234567893123456789412345678"); - } - - @Test - void testBug46817() throws Exception { - String result = getHtmlText("Bug46817.doc"); - final String substring = "
"; - assertContains(result, substring); - } - - @Test - void testBug47286() throws Exception { - String result = getHtmlText("Bug47286.doc"); - - assertFalse(result.contains("FORMTEXT")); - - assertContains(result, "color:#4f6228;"); - assertContains(result, "Passport No and the date of expire"); - assertContains(result, "mfa.gov.cy"); - } - - @Test - void testBug48075() throws Exception { - getHtmlText("Bug48075.doc"); - } - - @Test - void testBug52583() throws Exception { - String result = getHtmlText("Bug52583.doc"); - assertContains( - result, - ""); - } - - @Test - void testBug53182() throws Exception { - String result = getHtmlText("Bug53182.doc"); - assertFalse(result.contains("italic")); - } - - @Test - void testDocumentProperties() throws Exception { - String result = getHtmlText("documentProperties.doc"); - - assertContains(result, "This is document title"); - assertContains(result, - ""); - } - - @Test - void testEmailhyperlink() throws Exception { - String result = getHtmlText("Bug47286.doc"); - final String substring = "provisastpet@mfa.gov.cy"; - assertContains(result, substring); - } - - @Test - void testEndnote() throws Exception { - String result = getHtmlText("endingnote.doc"); - - assertContains( - result, - "1"); - assertContains( - // starting with JDK 9 such unimportant whitespaces may be trimmed - result.replace("1"); - } - - @Test - void testHyperlink() throws Exception { - String result = getHtmlText("hyperlink.doc"); - - assertContains(result, "Before text; Hyperlink text"); - assertContains(result, "; after text"); - } - - @Test - void testInnerTable() throws Exception { - getHtmlText("innertable.doc"); - } - - @Test - void testListsMargins() throws Exception { - String result = getHtmlText("lists-margins.doc"); - - assertContains(result, - ".s1{display: inline-block; text-indent: 0; min-width: 0.4861111in;}"); - assertContains(result, - ".s2{display: inline-block; text-indent: 0; min-width: 0.23055555in;}"); - assertContains(result, - ".s3{display: inline-block; text-indent: 0; min-width: 0.28541666in;}"); - assertContains(result, - ".s4{display: inline-block; text-indent: 0; min-width: 0.28333333in;}"); - assertContains(result, - ".p4{text-indent:-0.59652776in;margin-left:-0.70069444in;"); - } - - @Test - void testO_kurs_doc() throws Exception { - getHtmlText("o_kurs.doc"); - } - - @Test - void testPageref() throws Exception { - String result = getHtmlText("pageref.doc"); - - assertContains(result, ""); - assertContains(result, ""); - assertContains(result, "1"); - } - - @Test - void testPicture() throws Exception { - String result = getHtmlText("picture.doc", true); - - // picture - assertContains(result, "src=\"0.emf\""); - // visible size - assertContains(result, "width:3.1293333in;height:1.7247736in;"); - // shift due to crop - assertContains(result, "left:-0.09433333;top:-0.2573611;"); - // size without crop - assertContains(result, "width:3.4125in;height:2.3253334in;"); - } - - @Test - void testPicturesEscher() throws Exception { - String result = getHtmlText("pictures_escher.doc", true); - assertContains(result, ""); - assertContains(result, ""); - } - - @Test - void testTableMerges() throws Exception { - String result = getHtmlText("table-merges.doc"); - - assertContains(result, "
"); - assertContains(result, ""); - } - - @Test - void testBug52420() throws Exception { - String result = getHtmlText("52420.doc"); - - assertFalse(result.contains("FORMTEXT")); - - assertContains(result, "\u0417\u0410\u0414\u0410\u041d\u0418\u0415"); - assertContains(result, "\u041f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u044c"); - } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToTextConverter.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToTextConverter.java index 2a72fd8808..9ffea802cf 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToTextConverter.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToTextConverter.java @@ -17,6 +17,7 @@ package org.apache.poi.hwpf.converter; import static org.apache.poi.hwpf.HWPFTestDataSamples.openSampleFile; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import org.apache.poi.hwpf.HWPFDocument; @@ -56,7 +57,7 @@ public class TestWordToTextConverter { @Test void testBug53380_3() throws Exception { try (HWPFDocument doc = openSampleFile( "Bug53380_3.doc" )) { - WordToTextConverter.getText(doc); + assertNotNull(WordToTextConverter.getText(doc)); } } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/dev/TestHWPFLister.java b/src/scratchpad/testcases/org/apache/poi/hwpf/dev/TestHWPFLister.java index f0902f8216..3a94a3efb4 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/dev/TestHWPFLister.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/dev/TestHWPFLister.java @@ -16,56 +16,42 @@ ==================================================================== */ package org.apache.poi.hwpf.dev; -import java.io.File; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + import java.io.PrintStream; +import java.util.Arrays; import org.apache.poi.POIDataSamples; import org.apache.poi.util.NullPrintStream; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; public class TestHWPFLister { - private static PrintStream oldStdOut; + private static final POIDataSamples SAMPLES = POIDataSamples.getDocumentInstance(); + private static final String[] CLEAR_PROPS = { + "org.apache.poi.hwpf.preserveBinTables", + "org.apache.poi.hwpf.preserveTextTable" + }; - @BeforeAll - public static void muteStdout() { - oldStdOut = System.out; + @ParameterizedTest + @ValueSource(strings = { + "", + " --dop --textPieces --textPiecesText --chpx --chpxProperties --chpxSprms --papx --papxProperties --papxSprms --paragraphs --paragraphsText --bookmarks --escher --fields --pictures --officeDrawings --styles --writereadback" + }) + void main(String args) throws Exception { + String fileArgs = SAMPLES.getFile("SampleDoc.doc").getAbsolutePath() + args; + + PrintStream oldStdOut = System.out; System.setOut(new NullPrintStream()); - } + try { - @AfterAll - public static void restoreStdout() { - System.setOut(oldStdOut); - } + assertDoesNotThrow(() -> HWPFLister.main(fileArgs.split(" "))); - @AfterEach - void tearDown() { - // the main-method sets these properties, we need to revert them here to not affect other tests - System.clearProperty("org.apache.poi.hwpf.preserveBinTables"); - System.clearProperty("org.apache.poi.hwpf.preserveTextTable"); - } + } finally { + System.setOut(oldStdOut); - @Test - void main() throws Exception { - File file = POIDataSamples.getDocumentInstance().getFile("SampleDoc.doc"); - HWPFLister.main(new String[] { file.getAbsolutePath() }); - } - - @Test - void mainAll() throws Exception { - File file = POIDataSamples.getDocumentInstance().getFile("SampleDoc.doc"); - HWPFLister.main(new String[] { - file.getAbsolutePath(), - "--dop", "--textPieces", "--textPiecesText", - "--chpx", "--chpxProperties", "--chpxSprms", - "--papx", "--papxProperties", "--papxSprms", - "--paragraphs", "--paragraphsText", - "--bookmarks", "--escher", - "--fields", "--pictures", - "--officeDrawings", "--styles", - "--writereadback" - }); + // the main-method sets these properties, we need to revert them here to not affect other tests + Arrays.stream(CLEAR_PROPS).forEach(System::clearProperty); + } } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractorBugs.java b/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractorBugs.java index e2036a7d53..5575a09291 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractorBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractorBugs.java @@ -25,8 +25,8 @@ import java.io.InputStream; import org.apache.poi.POIDataSamples; import org.apache.poi.extractor.ExtractorFactory; import org.apache.poi.extractor.POITextExtractor; -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; /** * Tests for bugs with the WordExtractor @@ -34,38 +34,27 @@ import org.junit.jupiter.api.Test; public final class TestWordExtractorBugs { private static final POIDataSamples SAMPLES = POIDataSamples.getDocumentInstance(); - @Test - void testProblemMetadata() throws IOException { - InputStream is = SAMPLES.openResourceAsStream("ProblemExtracting.doc"); - WordExtractor extractor = new WordExtractor(is); - is.close(); - - // Check it gives text without error - extractor.getText(); - extractor.getParagraphText(); - extractor.getTextFromPieces(); - extractor.close(); + @ParameterizedTest + @ValueSource(strings = { + "ProblemExtracting.doc", + // bug 50688 + "parentinvguid.doc", + // Bug60374 + "cn.orthodox.www_divenbog_APRIL_30-APRIL.DOC" + }) + void testFile(String file) throws IOException { + try (InputStream is = SAMPLES.openResourceAsStream(file); + POITextExtractor ex = ExtractorFactory.createExtractor(is)) { + // Check it gives text without error + assertNotNull(ex.getText()); + if (ex instanceof WordExtractor) { + WordExtractor extractor = (WordExtractor)ex; + assertNotNull(extractor.getParagraphText()); + assertNotNull(extractor.getTextFromPieces()); + } else { + Word6Extractor extractor = (Word6Extractor)ex; + assertNotNull(extractor.getParagraphText()); + } + } } - - @Test - void testBug50688() throws Exception { - InputStream is = SAMPLES.openResourceAsStream("parentinvguid.doc"); - WordExtractor extractor = new WordExtractor(is); - is.close(); - - // Check it gives text without error - extractor.getText(); - extractor.close(); - } - - @Test - void testBug60374() throws Exception { - POIFSFileSystem fs = new POIFSFileSystem(SAMPLES.openResourceAsStream("cn.orthodox.www_divenbog_APRIL_30-APRIL.DOC")); - final POITextExtractor extractor = ExtractorFactory.createExtractor(fs); - - // Check it gives text without error - assertNotNull(extractor.getText()); - - extractor.close(); - } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestPAPBinTable.java b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestPAPBinTable.java index 9cf77812fe..b65843ab63 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestPAPBinTable.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestPAPBinTable.java @@ -17,29 +17,33 @@ package org.apache.poi.hwpf.model; +import static org.apache.poi.hwpf.HWPFTestDataSamples.openSampleFile; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import org.apache.poi.hwpf.HWPFDocFixture; -import org.apache.poi.hwpf.HWPFTestDataSamples; +import org.apache.poi.hwpf.HWPFDocument; +import org.apache.poi.hwpf.converter.WordToTextConverter; import org.apache.poi.hwpf.model.io.HWPFFileSystem; import org.junit.jupiter.api.Test; public final class TestPAPBinTable { @Test - void testObIs() throws IOException { + void testObIs() throws Exception { // shall not fail with assertions on - HWPFTestDataSamples.openSampleFile( "ob_is.doc" ).close(); + try (HWPFDocument doc = openSampleFile("ob_is.doc")) { + assertNotNull(WordToTextConverter.getText(doc)); + } } @Test void testReadWrite() throws IOException { - /** @todo verify the constructors */ + // @todo verify the constructors HWPFDocFixture _hWPFDocFixture = new HWPFDocFixture( this, HWPFDocFixture.DEFAULT_TEST_FILE ); _hWPFDocFixture.setUp(); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java index a4951b9862..f1d1cb578f 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java @@ -20,6 +20,7 @@ import static org.apache.poi.POIDataSamples.getDocumentInstance; import static org.apache.poi.POITestCase.assertContains; import static org.apache.poi.POITestCase.assertNotContained; import static org.apache.poi.hwpf.HWPFTestDataSamples.openSampleFile; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -402,8 +403,8 @@ class TestBugs { try (InputStream is = getDocumentInstance() .openResourceAsStream("Bug47742-text.txt")) { byte[] expectedBytes = IOUtils.toByteArray(is); - String expectedText = new String(expectedBytes, StandardCharsets.UTF_8) - .substring(1); // strip-off the unicode marker + // strip-off the unicode marker + String expectedText = new String(expectedBytes, StandardCharsets.UTF_8).substring(1); assertEqualsIgnoreNewline(expectedText, foundText); } @@ -413,9 +414,10 @@ class TestBugs { * Bug 47958 - Exception during Escher walk of pictures */ @Test - void test47958() { - HWPFDocument doc = openSampleFile("Bug47958.doc"); - doc.getPicturesTable().getAllPictures(); + void test47958() throws IOException { + try (HWPFDocument doc = openSampleFile("Bug47958.doc")) { + assertDoesNotThrow(doc.getPicturesTable()::getAllPictures); + } } /** @@ -555,42 +557,6 @@ class TestBugs { } } - /** - * [RESOLVED FIXED] Bug 51671 - HWPFDocument.write based on POIFSFileSystem - * throws a NullPointerException - */ - @Test - void test51671() throws Exception { - InputStream is = getDocumentInstance() - .openResourceAsStream("empty.doc"); - try (POIFSFileSystem poifsFileSystem = new POIFSFileSystem(is)) { - HWPFDocument hwpfDocument = new HWPFDocument( - poifsFileSystem.getRoot()); - hwpfDocument.write(new ByteArrayOutputStream()); - hwpfDocument.close(); - } - } - - /** - * Bug 51678 - Extracting text from Bug51524.zip is slow Bug 51524 - - * PapBinTable constructor is slow - */ - @Test - void test51678And51524() throws IOException { - // YK: the test will run only if the poi.test.remote system property is - // set. - // TODO: refactor into something nicer! - if (System.getProperty("poi.test.remote") != null) { - String href = "http://domex.nps.edu/corp/files/govdocs1/007/007488.doc"; - HWPFDocument hwpfDocument = HWPFTestDataSamples - .openRemoteFile(href); - - try (WordExtractor wordExtractor = new WordExtractor(hwpfDocument)) { - wordExtractor.getText(); - } - } - } - /** * [FIXED] Bug 51902 - Picture.fillRawImageContent - * ArrayIndexOutOfBoundsException @@ -648,7 +614,9 @@ class TestBugs { "cap.stanford.edu_profiles_viewbiosketch_facultyid=4009&name=m_maciver.doc, true", "ca.kwsymphony.www_education_School_Concert_Seat_Booking_Form_2011-12.doc, true", // Bug 33519 - HWPF fails to read a file - "Bug33519.doc, false" + "Bug33519.doc, false", + // Bug 51671 - HWPFDocument.write based on POIFSFileSystem throws a NullPointerException + "empty.doc, true" }) void testBug51834(String file, boolean doReadBack) throws Exception { try (HWPFDocument doc = openSampleFile(file)) { @@ -786,7 +754,6 @@ class TestBugs { try (HWPFDocument doc = openSampleFile("61490.doc")) { Range range = doc.getRange(); - System.out.println("print table"); TableIterator tableIter = new TableIterator(range); assertTrue(tableIter.hasNext()); Table table = tableIter.next();