diff --git a/build.gradle b/build.gradle index c2abcc5ac..7aa362a90 100644 --- a/build.gradle +++ b/build.gradle @@ -176,6 +176,10 @@ project('main') { compile 'commons-logging:commons-logging:1.2' compile 'org.apache.commons:commons-collections4:4.2' compile 'org.apache.commons:commons-math3:3.6.1' + compile 'javax.xml.bind:jaxb-api:2.3.0' + compile 'com.sun.xml.bind:jaxb-impl:2.3.0.1' + compile 'com.sun.xml.bind:jaxb-core:2.3.0.1' + compile 'javax.activation:activation:1.1.1' testCompile 'junit:junit:4.12' } @@ -234,7 +238,7 @@ project('ooxml') { compile files('../../ooxml-lib/ooxml-security-1.1.jar') testCompile 'junit:junit:4.12' - testCompile 'org.mockito:mockito-core:2.13.0' + testCompile 'org.mockito:mockito-core:2.21.0' testCompile 'org.xmlunit:xmlunit-core:2.5.1' testCompile project(path: ':main', configuration: 'tests') testCompile 'org.openjdk.jmh:jmh-core:1.19' diff --git a/build.xml b/build.xml index 3bdf45c9b..46ac73cb9 100644 --- a/build.xml +++ b/build.xml @@ -185,6 +185,18 @@ under the License. + + + + + + + + @@ -327,6 +339,10 @@ under the License. + + + + @@ -684,6 +700,10 @@ under the License. + + + + @@ -716,6 +736,10 @@ under the License. + + + + @@ -2025,6 +2049,10 @@ under the License. + + + + @@ -2297,6 +2325,10 @@ under the License. + + + + diff --git a/sonar/main/pom.xml b/sonar/main/pom.xml index 21a8abda0..89d3011d7 100644 --- a/sonar/main/pom.xml +++ b/sonar/main/pom.xml @@ -130,5 +130,25 @@ commons-logging 1.2 + + javax.xml.bind + jaxb-api + 2.3.0 + + + com.sun.xml.bind + jaxb-impl + 2.3.0.1 + + + com.sun.xml.bind + jaxb-core + 2.3.0.1 + + + javax.activation + activation + 1.1.1 + diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java index ad7fe80c8..d649c515a 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java @@ -17,15 +17,53 @@ package org.apache.poi.openxml4j.opc; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; +import org.apache.commons.compress.archivers.zip.ZipFile; +import org.apache.poi.EncryptedDocumentException; +import org.apache.poi.POIDataSamples; +import org.apache.poi.POITestCase; +import org.apache.poi.UnsupportedFileFormatException; +import org.apache.poi.extractor.POITextExtractor; +import org.apache.poi.ooxml.POIXMLException; +import org.apache.poi.ooxml.extractor.ExtractorFactory; +import org.apache.poi.ooxml.util.DocumentHelper; +import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.exceptions.InvalidOperationException; +import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; +import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException; +import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException; +import org.apache.poi.openxml4j.exceptions.OpenXML4JException; +import org.apache.poi.openxml4j.opc.internal.ContentTypeManager; +import org.apache.poi.openxml4j.opc.internal.FileHelper; +import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; +import org.apache.poi.openxml4j.opc.internal.ZipHelper; +import org.apache.poi.openxml4j.util.ZipSecureFile; +import org.apache.poi.sl.usermodel.SlideShow; +import org.apache.poi.sl.usermodel.SlideShowFactory; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.apache.poi.util.IOUtils; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; +import org.apache.poi.util.TempFile; +import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.xwpf.usermodel.XWPFRelation; +import org.apache.xmlbeans.XmlException; +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; @@ -47,54 +85,12 @@ import java.util.TreeMap; import java.util.function.BiConsumer; import java.util.regex.Pattern; -import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; -import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; -import org.apache.commons.compress.archivers.zip.ZipFile; -import org.apache.poi.EncryptedDocumentException; -import org.apache.poi.POIDataSamples; -import org.apache.poi.POITestCase; -import org.apache.poi.extractor.POITextExtractor; -import org.apache.poi.ooxml.POIXMLException; -import org.apache.poi.UnsupportedFileFormatException; -import org.apache.poi.ooxml.extractor.ExtractorFactory; -import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; -import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import org.apache.poi.openxml4j.exceptions.InvalidOperationException; -import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; -import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException; -import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException; -import org.apache.poi.openxml4j.exceptions.OpenXML4JException; -import org.apache.poi.openxml4j.opc.internal.ContentTypeManager; -import org.apache.poi.openxml4j.opc.internal.FileHelper; -import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; -import org.apache.poi.openxml4j.opc.internal.ZipHelper; -import org.apache.poi.openxml4j.util.ZipSecureFile; -import org.apache.poi.sl.usermodel.SlideShow; -import org.apache.poi.sl.usermodel.SlideShowFactory; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.ss.usermodel.WorkbookFactory; -import org.apache.poi.ooxml.util.DocumentHelper; -import org.apache.poi.util.IOUtils; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; -import org.apache.poi.util.TempFile; -import org.apache.poi.xssf.XSSFTestDataSamples; -import org.apache.poi.xssf.streaming.SXSSFWorkbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.poi.xwpf.usermodel.XWPFRelation; -import org.apache.xmlbeans.XmlException; -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.mockito.Mockito; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public final class TestPackage { private static final POILogger logger = POILogFactory.getLogger(TestPackage.class); @@ -784,7 +780,7 @@ public final class TestPackage { */ @Test public void zipBombCreateAndHandle() - throws IOException, EncryptedDocumentException, InvalidFormatException { + throws IOException, EncryptedDocumentException { ByteArrayOutputStream bos = new ByteArrayOutputStream(2500000); try (ZipFile zipFile = ZipHelper.openZipFile(OpenXML4JTestDataSamples.getSampleFile("sample.xlsx")); @@ -884,7 +880,7 @@ public final class TestPackage { } @Test - public void zipBombCheckSizesWithinLimits() throws IOException, EncryptedDocumentException, InvalidFormatException { + public void zipBombCheckSizesWithinLimits() throws IOException, EncryptedDocumentException { getZipStatsAndConsume((max_size, min_ratio) -> { // use values close to, but within the limits ZipSecureFile.setMinInflateRatio(min_ratio - 0.002); @@ -895,7 +891,7 @@ public final class TestPackage { } @Test - public void zipBombCheckSizesRatioTooSmall() throws IOException, EncryptedDocumentException, InvalidFormatException { + public void zipBombCheckSizesRatioTooSmall() throws IOException, EncryptedDocumentException { expectedEx.expect(POIXMLException.class); expectedEx.expectMessage("You can adjust this limit via ZipSecureFile.setMinInflateRatio()"); getZipStatsAndConsume((max_size, min_ratio) -> { @@ -905,7 +901,7 @@ public final class TestPackage { } @Test - public void zipBombCheckSizesSizeTooBig() throws IOException, EncryptedDocumentException, InvalidFormatException { + public void zipBombCheckSizesSizeTooBig() throws IOException, EncryptedDocumentException { expectedEx.expect(POIXMLException.class); expectedEx.expectMessage("You can adjust this limit via ZipSecureFile.setMaxEntrySize()"); getZipStatsAndConsume((max_size, min_ratio) -> { @@ -915,7 +911,7 @@ public final class TestPackage { }); } - private void getZipStatsAndConsume(BiConsumer ratioCon) throws IOException, InvalidFormatException { + private void getZipStatsAndConsume(BiConsumer ratioCon) throws IOException { // use a test file with a xml file bigger than 100k (ZipArchiveThresholdInputStream.GRACE_ENTRY_SIZE) final File file = XSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"); @@ -980,7 +976,7 @@ public final class TestPackage { // bug 61381 @Test - public void testTooShortFilterStreams() throws IOException, InvalidFormatException { + public void testTooShortFilterStreams() throws IOException { File xssf = OpenXML4JTestDataSamples.getSampleFile("sample.xlsx"); File hssf = POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xls"); @@ -1093,7 +1089,7 @@ public final class TestPackage { @Test(expected = InvalidFormatException.class) public void testBug62592() throws Exception { InputStream is = OpenXML4JTestDataSamples.openSampleStream("62592.thmx"); - OPCPackage p = OPCPackage.open(is); + /*OPCPackage p =*/ OPCPackage.open(is); } @Test @@ -1107,18 +1103,29 @@ public final class TestPackage { @Test public void testDoNotCloseStream() throws IOException { - OutputStream os = Mockito.mock(OutputStream.class); + // up to JDK 10 we did use Mockito here, but OutputStream is + // an abstract class and fails mocking with some changes in JDK 11 + // so we use a simple empty output stream implementation instead + OutputStream os = new OutputStream() { + @Override + public void write(int b) { + } + + @Override + public void close() { + throw new IllegalStateException("close should not be called here"); + } + }; + try (XSSFWorkbook wb = new XSSFWorkbook()) { wb.createSheet(); wb.write(os); } - verify(os, never()).close(); try (SXSSFWorkbook wb = new SXSSFWorkbook()) { wb.createSheet(); wb.write(os); } - verify(os, never()).close(); } diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFFreeformShape.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFFreeformShape.java index 1e3700aa3..226368d14 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFFreeformShape.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFFreeformShape.java @@ -17,13 +17,13 @@ package org.apache.poi.xslf.usermodel; -import static org.apache.poi.xslf.usermodel.TestXSLFSimpleShape.getSpPr; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import org.apache.poi.sl.draw.SLGraphics; +import org.junit.Test; +import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape; import java.awt.Color; import java.awt.Graphics2D; +import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; @@ -33,10 +33,8 @@ import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.IOException; -import org.junit.Test; -import org.mockito.AdditionalAnswers; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; +import static org.apache.poi.xslf.usermodel.TestXSLFSimpleShape.getSpPr; +import static org.junit.Assert.assertEquals; public class TestXSLFFreeformShape { @@ -78,17 +76,32 @@ public class TestXSLFFreeformShape { BufferedImage img = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = img.createGraphics(); try { - Graphics2D graphicsMock = Mockito.mock(Graphics2D.class, AdditionalAnswers.delegatesTo(graphics)); + // previously we used Mockito here, but since JDK 11 mocking the Graphics2D does + // not work any longer + Graphics2D graphicsMock = new SLGraphics(new XSLFGroupShape(CTGroupShape.Factory.newInstance(), slide)) { + boolean called; + + @Override + public void draw(Shape shape) { + if(called) { + throw new IllegalStateException("Should only be called once, but was called a second time"); + } + called = true; + + if(!(shape instanceof Path2D.Double)) { + throw new IllegalStateException("Expecting a shape of type Path2D.Double, but had " + shape.getClass()); + } + + Path2D.Double actual = (Path2D.Double) shape; + PathIterator pi = actual.getPathIterator(new AffineTransform()); + comparePoint(pi, PathIterator.SEG_MOVETO, 100, 150); + pi.next(); + comparePoint(pi, PathIterator.SEG_LINETO, 100, 300); + + super.draw(shape); + } + }; slide.draw(graphicsMock); - - ArgumentCaptor captor = ArgumentCaptor.forClass(Path2D.Double.class); - verify(graphicsMock, times(1)).draw(captor.capture()); - - Path2D.Double actual = captor.getValue(); - PathIterator pi = actual.getPathIterator(new AffineTransform()); - comparePoint(pi, PathIterator.SEG_MOVETO, 100, 150); - pi.next(); - comparePoint(pi, PathIterator.SEG_LINETO, 100, 300); } finally { graphics.dispose(); } diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextRun.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextRun.java index 8e77f59f2..90f07e29c 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextRun.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextRun.java @@ -22,14 +22,15 @@ import static org.apache.poi.sl.TestCommonSL.sameColor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import java.awt.Color; import java.io.IOException; import org.apache.poi.sl.draw.DrawTextParagraph; import org.junit.Test; +import org.openxmlformats.schemas.drawingml.x2006.main.CTTextLineBreak; +import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph; +import org.openxmlformats.schemas.presentationml.x2006.main.CTShape; /** * @author Yegor Kozlov @@ -104,14 +105,16 @@ public class TestXSLFTextRun { } @Test - public void testCopyNullFontSize() throws IOException { + public void testCopyNullFontSize() { XMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide = ppt.createSlide(); XSLFTextShape sh = slide.createAutoShape(); XSLFTextRun r = sh.addNewTextParagraph().addNewTextRun(); - XSLFTextRun s = mock(XSLFTextRun.class); - when(s.getFontSize()).thenReturn(null); + + XSLFTextRun s = new XSLFTextRun(CTTextLineBreak.Factory.newInstance(), + new XSLFTextParagraph(CTTextParagraph.Factory.newInstance(), + new XSLFTextBox(CTShape.Factory.newInstance(), slide))); r.copy(s); } diff --git a/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java b/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java index b7bcca647..0d2710425 100644 --- a/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java +++ b/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java @@ -27,8 +27,6 @@ import java.util.Arrays; import java.util.Date; import java.util.Locale; -import junit.framework.TestCase; - import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; @@ -37,10 +35,11 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.TempFile; +import org.junit.Ignore; +import org.junit.Test; -public final class TestDateFormatConverter extends TestCase { +public final class TestDateFormatConverter { private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception { - try (Workbook workbook = new HSSFWorkbook()) { String sheetName; if (dates) { @@ -100,8 +99,18 @@ public final class TestDateFormatConverter extends TestCase { row.createCell(5).setCellValue(javaDateFormatPattern); row.createCell(6).setCellValue(excelFormatPattern); } catch (Exception e) { - throw new RuntimeException("Failed for locale: " + locale + ", having locales: " + - Arrays.toString(DateFormat.getAvailableLocales()), e); + // this can be removed after https://bugs.openjdk.java.net/browse/JDK-8209047 is available + // in JDK 11 ea > 26 + if(locale.toString().startsWith("my") && + e.getMessage().contains("Illegal pattern character 'B'") && + System.getProperty("java.version").startsWith("11")) { + System.out.println("DateFormat.getDateTimeInstance() fails for Malaysian Locale on JDK 11, submitted bug report to Oracle"); + continue; + } + + throw new RuntimeException( + "Failed for locale: " + locale + " and style " + style + "\n" + + "Having locales: " + Arrays.toString(DateFormat.getAvailableLocales()), e); } } @@ -114,6 +123,7 @@ public final class TestDateFormatConverter extends TestCase { } } + @Test public void testJavaDateFormatsInExcel() throws Exception { Date date = new Date(); @@ -135,11 +145,17 @@ public final class TestDateFormatConverter extends TestCase { outputLocaleDataFormats(date, false, true, DateFormat.LONG, "Long" ); outputLocaleDataFormats(date, false, true, DateFormat.FULL, "Full" ); } - + + @Test public void testJDK8EmptyLocale() { // JDK 8 seems to add an empty locale-string to the list returned via DateFormat.getAvailableLocales() // therefore we now cater for this special locale as well DateFormatConverter.getPrefixForLocale(new Locale("")); } + @Ignore("Fails on JDK 11, submitted as ID : 9056763") + @Test + public void testJDK11MyLocale() { + DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.forLanguageTag("my")); + } }