From b7b0a0c21579b38b8f1aa553eda35564cb01e3d2 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Mon, 17 Aug 2020 20:25:08 +0000 Subject: Activate Forbidden-Apis-Check also for other tests Max supported Java version is currently Java 14 Fix the check failures git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880944 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hmef/dev/TestHMEFDumper.java | 4 +- .../apache/poi/hslf/dev/BasePPTIteratingTest.java | 32 +++---- .../org/apache/poi/hslf/usermodel/TestBugs.java | 103 ++++++++++----------- .../apache/poi/hsmf/TestExtractEmbeddedMSG.java | 9 +- .../apache/poi/hsmf/TestFixedSizedProperties.java | 4 +- 5 files changed, 73 insertions(+), 79 deletions(-) (limited to 'src/scratchpad') 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 95a7d91460..2ef26a66b7 100644 --- a/src/scratchpad/testcases/org/apache/poi/hmef/dev/TestHMEFDumper.java +++ b/src/scratchpad/testcases/org/apache/poi/hmef/dev/TestHMEFDumper.java @@ -24,7 +24,7 @@ import java.io.File; import java.io.PrintStream; import org.apache.poi.POIDataSamples; -import org.apache.poi.util.NullOutputStream; +import org.apache.poi.util.NullPrintStream; import org.junit.Test; public class TestHMEFDumper { @@ -48,7 +48,7 @@ public class TestHMEFDumper { private static void doMain(String... args) throws Exception { PrintStream ps = System.out; try { - System.setOut(new PrintStream(new NullOutputStream(), true, "UTF-8")); + System.setOut(new NullPrintStream()); HMEFDumper.main(args); } finally { System.setOut(ps); diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/BasePPTIteratingTest.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/BasePPTIteratingTest.java index 5212221e48..681cd46708 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/dev/BasePPTIteratingTest.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/BasePPTIteratingTest.java @@ -16,22 +16,9 @@ ==================================================================== */ package org.apache.poi.hslf.dev; -import org.apache.poi.POIDataSamples; -import org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException; -import org.apache.poi.hslf.exceptions.OldPowerPointFormatException; -import org.apache.poi.util.IOUtils; -import org.apache.poi.util.LocaleUtil; -import org.apache.poi.util.NullOutputStream; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import static org.junit.Assert.assertNotNull; import java.io.File; -import java.io.OutputStream; import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; @@ -42,12 +29,21 @@ import java.util.Locale; import java.util.Map; import java.util.Set; -import static org.junit.Assert.assertNotNull; +import org.apache.poi.POIDataSamples; +import org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException; +import org.apache.poi.hslf.exceptions.OldPowerPointFormatException; +import org.apache.poi.util.IOUtils; +import org.apache.poi.util.NullPrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; @RunWith(Parameterized.class) public abstract class BasePPTIteratingTest { - protected static final OutputStream NULL_OUTPUT_STREAM = new NullOutputStream(); - protected static final Set OLD_FILES = new HashSet<>(); static { OLD_FILES.add("PPT95.ppt"); @@ -89,7 +85,7 @@ public abstract class BasePPTIteratingTest { IOUtils.setByteArrayMaxOverride(5*1024*1024); // redirect standard out during the test to avoid spamming the console with output - System.setOut(new PrintStream(NULL_OUTPUT_STREAM, true, LocaleUtil.CHARSET_1252.name())); + System.setOut(new NullPrintStream()); } @After 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 e23f158703..32ae5077de 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java @@ -35,16 +35,17 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.io.PrintStream; import java.text.AttributedCharacterIterator; import java.text.AttributedCharacterIterator.Attribute; import java.text.CharacterIterator; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; @@ -59,7 +60,6 @@ import org.apache.poi.hslf.exceptions.OldPowerPointFormatException; import org.apache.poi.hslf.model.HeadersFooters; import org.apache.poi.hslf.record.DocInfoListContainer; import org.apache.poi.hslf.record.Document; -import org.apache.poi.hslf.record.Record; import org.apache.poi.hslf.record.RecordTypes; import org.apache.poi.hslf.record.SlideListWithText; import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet; @@ -88,6 +88,7 @@ import org.apache.poi.sl.usermodel.TextParagraph.TextAlign; import org.apache.poi.sl.usermodel.TextRun; import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.NullPrintStream; import org.apache.poi.util.StringUtil; import org.apache.poi.util.Units; import org.junit.Test; @@ -291,7 +292,7 @@ public final class TestBugs { } } } - + ppt.close(); } @@ -375,7 +376,7 @@ public final class TestBugs { HSLFPictureData pict = f.getPictureData(); assertNotNull(pict); assertEquals(PictureType.JPEG, pict.getType()); - + ppt.close(); } @@ -405,7 +406,7 @@ public final class TestBugs { List> run = slide.getTextParagraphs(); assertEquals(3, run.size()); assertEquals("Fundera, planera och involvera.", HSLFTextParagraph.getRawText(run.get(2))); - + ppt.close(); } @@ -445,7 +446,7 @@ public final class TestBugs { HSLFSlideShow ppt = open("41246-1.ppt"); HSLFTestDataSamples.writeOutAndReadBack(ppt).close(); - + ppt.close(); } @@ -454,7 +455,7 @@ public final class TestBugs { HSLFSlideShow ppt = open("41246-2.ppt"); HSLFTestDataSamples.writeOutAndReadBack(ppt).close(); - + ppt.close(); } @@ -463,31 +464,32 @@ public final class TestBugs { */ @Test public void bug45776() throws IOException { - HSLFSlideShow ppt = open("45776.ppt"); + DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT); + try (HSLFSlideShow ppt = open("45776.ppt")) { + + // get slides + for (HSLFSlide slide : ppt.getSlides()) { + for (HSLFShape shape : slide.getShapes()) { + if (!(shape instanceof HSLFTextBox)) { + continue; + } + HSLFTextBox tb = (HSLFTextBox) shape; + // work with TextBox + String str = tb.getText(); - // get slides - for (HSLFSlide slide : ppt.getSlides()) { - for (HSLFShape shape : slide.getShapes()) { - if (!(shape instanceof HSLFTextBox)) { - continue; - } - HSLFTextBox tb = (HSLFTextBox) shape; - // work with TextBox - String str = tb.getText(); + if (!str.contains("$$DATE$$")) { + continue; + } + str = str.replace("$$DATE$$", df.format(new Date())); + tb.setText(str); - if (!str.contains("$$DATE$$")) { - continue; + List tr = tb.getTextParagraphs(); + assertEquals(str.length() + 1, tr.get(0).getParagraphStyle().getCharactersCovered()); + assertEquals(str.length() + 1, tr.get(0).getTextRuns().get(0).getCharacterStyle().getCharactersCovered()); } - str = str.replace("$$DATE$$", new Date().toString()); - tb.setText(str); - - List tr = tb.getTextParagraphs(); - assertEquals(str.length()+1,tr.get(0).getParagraphStyle().getCharactersCovered()); - assertEquals(str.length()+1,tr.get(0).getTextRuns().get(0).getCharacterStyle().getCharactersCovered()); } + } - - ppt.close(); } @Test @@ -501,7 +503,7 @@ public final class TestBugs { HeadersFooters hf = slide.getHeadersFooters(); /*boolean visible =*/ hf.isHeaderVisible(); // exception happens here } - + ppt.close(); } @@ -538,7 +540,7 @@ public final class TestBugs { } } assertEquals(2, str); - + ppt.close(); } @@ -781,7 +783,7 @@ public final class TestBugs { HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1); ppt1.close(); - + HSLFTextShape ts = (HSLFTextShape)ppt2.getSlides().get(0).getShapes().get(0); tp = ts.getTextParagraphs().get(0); tr = tp.getTextRuns().get(0); @@ -841,9 +843,9 @@ public final class TestBugs { @Test public void bug55030() throws IOException { HSLFSlideShow ppt = open("bug55030.ppt"); - + String expFamily = "\u96b6\u4e66"; - + HSLFSlide sl = ppt.getSlides().get(0); for (List paraList : sl.getTextParagraphs()) { for (HSLFTextParagraph htp : paraList) { @@ -853,7 +855,7 @@ public final class TestBugs { } } } - + ppt.close(); } @@ -873,27 +875,22 @@ public final class TestBugs { assertEquals(hlRun.getStartIndex(), hlShape.getStartIndex()); assertEquals(hlRun.getEndIndex(), hlShape.getEndIndex()); - OutputStream nullOutput = new OutputStream(){ - @Override - public void write(int b) throws IOException {} - }; - final boolean[] found = {false}; - DummyGraphics2d dgfx = new DummyGraphics2d(new PrintStream(nullOutput)){ + DummyGraphics2d dgfx = new DummyGraphics2d(new NullPrintStream()){ @Override public void drawString(AttributedCharacterIterator iterator, float x, float y) { // For the test file, common sl draws textruns one by one and not mixed // so we evaluate the whole iterator Map attributes = null; StringBuilder sb = new StringBuilder(); - + for (char c = iterator.first(); c != CharacterIterator.DONE; c = iterator.next()) { sb.append(c); attributes = iterator.getAttributes(); } - + if ("Jakarta HSSF".equals(sb.toString())) { // this is a test for a manually modified ppt, for real hyperlink label // one would need to access the screen tip record @@ -905,17 +902,17 @@ public final class TestBugs { } } }; - + ppt.getSlides().get(1).draw(dgfx); assertTrue(found[0]); - + ppt.close(); } @Test public void bug59056() throws IOException { 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(); @@ -927,11 +924,11 @@ public final class TestBugs { graphics.dispose(); // ImageIO.write(img, "png", new File("bla"+shape.getShapeId()+".png")); } - + ppt.close(); - + } - + 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 @@ -950,10 +947,10 @@ public final class TestBugs { fs.setPath(new Path2D.Double(el)); Color cExp = new Color(50,100,150,200); fs.setFillColor(cExp); - + HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1); ppt1.close(); - + sl = ppt2.getSlides().get(0); fs = (HSLFFreeformShape)sl.getShapes().get(0); Color cAct = fs.getFillColor(); @@ -961,7 +958,7 @@ public final class TestBugs { assertEquals(cExp.getGreen(), cAct.getGreen()); assertEquals(cExp.getBlue(), cAct.getBlue()); assertEquals(cExp.getAlpha(), cAct.getAlpha(), 1); - + PaintStyle ps = fs.getFillStyle().getPaint(); assertTrue(ps instanceof SolidPaint); ColorStyle cs = ((SolidPaint)ps).getSolidColor(); @@ -971,7 +968,7 @@ public final class TestBugs { assertEquals(cExp.getBlue(), cAct.getBlue()); assertEquals(255, cAct.getAlpha()); assertEquals(cExp.getAlpha()*100000./255., cs.getAlpha(), 1); - + ppt2.close(); } @@ -1016,7 +1013,7 @@ public final class TestBugs { } } } - + ppt.close(); } finally { diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/TestExtractEmbeddedMSG.java b/src/scratchpad/testcases/org/apache/poi/hsmf/TestExtractEmbeddedMSG.java index 5d6e2f51b6..d44145aedc 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/TestExtractEmbeddedMSG.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/TestExtractEmbeddedMSG.java @@ -44,6 +44,7 @@ import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.poifs.filesystem.EntryUtils; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.util.LocaleUtil; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -53,7 +54,7 @@ public class TestExtractEmbeddedMSG { /** * Initialize this test, load up the attachment_msg_pdf.msg mapi message. - * + * * @throws Exception */ @BeforeClass @@ -70,9 +71,9 @@ public class TestExtractEmbeddedMSG { /** * Test to see if embedded message properties can be read, extracted, and * re-parsed - * + * * @throws ChunkNotFoundException - * + * */ @Test public void testEmbeddedMSGProperties() throws IOException, ChunkNotFoundException { @@ -102,7 +103,7 @@ public class TestExtractEmbeddedMSG { msg.setReturnNullOnMissingChunk(true); Calendar messageDate = msg.getMessageDate(); assertNotNull(messageDate); - Calendar expectedMessageDate = Calendar.getInstance(); + Calendar expectedMessageDate = LocaleUtil.getLocaleCalendar(); expectedMessageDate.set(2010, 05, 17, 23, 52, 19); // 2010/06/17 23:52:19 GMT expectedMessageDate.setTimeZone(TimeZone.getTimeZone("GMT")); expectedMessageDate.set(Calendar.MILLISECOND, 0); diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/TestFixedSizedProperties.java b/src/scratchpad/testcases/org/apache/poi/hsmf/TestFixedSizedProperties.java index 007fff036a..c559407a1a 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/TestFixedSizedProperties.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/TestFixedSizedProperties.java @@ -168,7 +168,7 @@ public final class TestFixedSizedProperties { */ @Test public void testReadMessageDateSucceedsWithHSMFDump() throws IOException { - PrintStream stream = new PrintStream(new ByteArrayOutputStream()); + PrintStream stream = new PrintStream(new ByteArrayOutputStream(), true, "ISO-8859-1"); HSMFDump dump = new HSMFDump(fsMessageSucceeds); dump.dump(stream); } @@ -178,7 +178,7 @@ public final class TestFixedSizedProperties { */ @Test public void testReadMessageDateFailsWithHSMFDump() throws Exception { - PrintStream stream = new PrintStream(new ByteArrayOutputStream()); + PrintStream stream = new PrintStream(new ByteArrayOutputStream(), true, "ISO-8859-1"); HSMFDump dump = new HSMFDump(fsMessageFails); dump.dump(stream); } -- cgit v1.2.3