From 91e617e369b0374daef45bd2d1242626a00bcf15 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Thu, 5 Nov 2015 00:15:51 +0000 Subject: [PATCH] extended forbidden-apis-check to ooxml-tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1712693 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 2 +- .../org/apache/poi/TestPOIXMLProperties.java | 4 +- .../apache/poi/openxml4j/opc/TestPackage.java | 96 ++++++++++++------- .../poi/poifs/crypt/TestSignatureInfo.java | 2 +- .../xssf/extractor/TestXSSFImportFromXML.java | 50 ++++++---- .../xssf/model/TestSharedStringsTable.java | 40 ++++---- .../poi/xssf/usermodel/TestUnfixedBugs.java | 2 +- .../poi/xssf/usermodel/TestXSSFFont.java | 81 ++++++++++------ .../poi/xssf/usermodel/TestXSSFPicture.java | 11 ++- .../xssf/usermodel/TestXSSFPictureData.java | 31 +++--- .../poi/xssf/usermodel/TestXSSFSheet.java | 8 +- .../poi/xssf/usermodel/TestXSSFWorkbook.java | 2 +- .../poi/hssf/usermodel/TestHSSFFont.java | 6 +- .../poi/hssf/usermodel/TestUnfixedBugs.java | 96 +++++++++++-------- .../apache/poi/ss/usermodel/BaseTestFont.java | 66 +++++++------ 15 files changed, 306 insertions(+), 191 deletions(-) diff --git a/build.xml b/build.xml index ebd455cacc..92945e3184 100644 --- a/build.xml +++ b/build.xml @@ -1776,9 +1776,9 @@ under the License. + diff --git a/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java b/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java index 61e57362c8..17c8d02103 100644 --- a/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java +++ b/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java @@ -185,13 +185,13 @@ public final class TestPOIXMLProperties { Date dateCreated = LocaleUtil.getLocaleCalendar(2010, 6, 15, 10, 0, 0).getTime(); cp.setCreated(new Nullable(dateCreated)); - assertEquals(dateCreated.toString(), cp.getCreated().toString()); + assertEquals(dateCreated, cp.getCreated()); XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc); doc.close(); cp = doc.getProperties().getCoreProperties(); Date dt3 = cp.getCreated(); - assertEquals(dateCreated.toString(), dt3.toString()); + assertEquals(dateCreated, dt3); doc2.close(); } 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 6e3b89f53e..5c3f688cb4 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java @@ -35,6 +35,7 @@ import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.net.URI; +import java.net.URISyntaxException; import java.util.Enumeration; import java.util.HashMap; import java.util.List; @@ -44,6 +45,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; +import org.apache.poi.EncryptedDocumentException; import org.apache.poi.POIXMLException; import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; @@ -65,6 +67,7 @@ import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; public final class TestPackage { private static final POILogger logger = POILogFactory.getLogger(TestPackage.class); @@ -73,11 +76,12 @@ public final class TestPackage { * Test that just opening and closing the file doesn't alter the document. */ @Test - public void openSave() throws Exception { + public void openSave() throws IOException, InvalidFormatException { String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx"); File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageOpenSaveTMP.docx"); - OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE); + @SuppressWarnings("resource") + OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE); try { p.save(targetFile.getAbsoluteFile()); @@ -94,15 +98,21 @@ public final class TestPackage { /** * Test that when we create a new Package, we give it * the correct default content types + * @throws IllegalAccessException + * @throws NoSuchFieldException + * @throws IllegalArgumentException + * @throws SecurityException */ @Test - public void createGetsContentTypes() throws Exception { + public void createGetsContentTypes() + throws IOException, InvalidFormatException, SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestCreatePackageTMP.docx"); // Zap the target file, in case of an earlier run if(targetFile.exists()) targetFile.delete(); - OPCPackage pkg = OPCPackage.create(targetFile); + @SuppressWarnings("resource") + OPCPackage pkg = OPCPackage.create(targetFile); // Check it has content types for rels and xml ContentTypeManager ctm = getContentTypeManager(pkg); @@ -123,13 +133,15 @@ public final class TestPackage { PackagingURIHelper.createPartName("/foo.txt") ) ); + + pkg.revert(); } /** * Test package creation. */ @Test - public void createPackageAddPart() throws Exception { + public void createPackageAddPart() throws IOException, InvalidFormatException { File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestCreatePackageTMP.docx"); File expectedFile = OpenXML4JTestDataSamples.getSampleFile("TestCreatePackageOUTPUT.docx"); @@ -174,9 +186,10 @@ public final class TestPackage { * Tests that we can create a new package, add a core * document and another part, save and re-load and * have everything setup as expected + * @throws SAXException */ @Test - public void createPackageWithCoreDocument() throws Exception { + public void createPackageWithCoreDocument() throws IOException, InvalidFormatException, URISyntaxException, SAXException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OPCPackage pkg = OPCPackage.create(baos); @@ -188,7 +201,7 @@ public final class TestPackage { PackagePart corePart = pkg.createPart(corePartName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"); // Put in some dummy content OutputStream coreOut = corePart.getOutputStream(); - coreOut.write("".getBytes()); + coreOut.write("".getBytes("UTF-8")); coreOut.close(); // And another bit @@ -200,7 +213,7 @@ public final class TestPackage { // Dummy content again coreOut = corePart.getOutputStream(); - coreOut.write("".getBytes()); + coreOut.write("".getBytes("UTF-8")); coreOut.close(); //add a relationship with internal target: "#Sheet1!A1" @@ -250,7 +263,7 @@ public final class TestPackage { } } - private void assertMSCompatibility(OPCPackage pkg) throws Exception { + private void assertMSCompatibility(OPCPackage pkg) throws IOException, InvalidFormatException, SAXException { PackagePartName relName = PackagingURIHelper.createPartName(PackageRelationship.getContainerPartRelationship()); PackagePart relPart = pkg.getPart(relName); @@ -271,7 +284,7 @@ public final class TestPackage { * Test package opening. */ @Test - public void openPackage() throws Exception { + public void openPackage() throws IOException, InvalidFormatException { File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestOpenPackageTMP.docx"); File inputFile = OpenXML4JTestDataSamples.getSampleFile("TestOpenPackageINPUT.docx"); @@ -331,11 +344,12 @@ public final class TestPackage { * to a file */ @Test - public void saveToOutputStream() throws Exception { + public void saveToOutputStream() throws IOException, InvalidFormatException { String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx"); File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageOpenSaveTMP.docx"); - OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE); + @SuppressWarnings("resource") + OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE); try { FileOutputStream fout = new FileOutputStream(targetFile); try { @@ -360,12 +374,13 @@ public final class TestPackage { * reading from a file */ @Test - public void openFromInputStream() throws Exception { + public void openFromInputStream() throws IOException, InvalidFormatException { String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx"); FileInputStream finp = new FileInputStream(originalFile); - OPCPackage p = OPCPackage.open(finp); + @SuppressWarnings("resource") + OPCPackage p = OPCPackage.open(finp); assertNotNull(p); assertNotNull(p.getRelationships()); @@ -374,19 +389,24 @@ public final class TestPackage { // Check it has the usual bits assertTrue(p.hasRelationships()); assertTrue(p.containPart(PackagingURIHelper.createPartName("/_rels/.rels"))); + + p.revert(); + finp.close(); } /** * TODO: fix and enable + * @throws URISyntaxException */ @Test @Ignore - public void removePartRecursive() throws Exception { + public void removePartRecursive() throws IOException, InvalidFormatException, URISyntaxException { String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx"); File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageRemovePartRecursiveOUTPUT.docx"); File tempFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageRemovePartRecursiveTMP.docx"); - OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE); + @SuppressWarnings("resource") + OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE); p.removePartRecursive(PackagingURIHelper.createPartName(new URI( "/word/document.xml"))); p.save(tempFile.getAbsoluteFile()); @@ -395,6 +415,8 @@ public final class TestPackage { assertTrue(targetFile.exists()); ZipFileAssert.assertEquals(targetFile, tempFile); assertTrue(targetFile.delete()); + + p.revert(); } @Test @@ -437,7 +459,8 @@ public final class TestPackage { String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); - OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ_WRITE); + @SuppressWarnings("resource") + OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ_WRITE); // Remove the core part p.deletePart(PackagingURIHelper.createPartName("/word/document.xml")); @@ -476,7 +499,8 @@ public final class TestPackage { String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); - OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ_WRITE); + @SuppressWarnings("resource") + OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ_WRITE); // Remove the core part p.deletePartRecursive(PackagingURIHelper.createPartName("/word/document.xml")); @@ -499,7 +523,7 @@ public final class TestPackage { * write changes to it. */ @Test - public void openFileThenOverwrite() throws Exception { + public void openFileThenOverwrite() throws IOException, InvalidFormatException { File tempFile = TempFile.createTempFile("poiTesting","tmp"); File origFile = OpenXML4JTestDataSamples.getSampleFile("TestPackageCommon.docx"); FileHelper.copyFile(origFile, tempFile); @@ -537,7 +561,7 @@ public final class TestPackage { * to another file, then delete both */ @Test - public void openFileThenSaveDelete() throws Exception { + public void openFileThenSaveDelete() throws IOException, InvalidFormatException { File tempFile = TempFile.createTempFile("poiTesting","tmp"); File tempFile2 = TempFile.createTempFile("poiTesting","tmp"); File origFile = OpenXML4JTestDataSamples.getSampleFile("TestPackageCommon.docx"); @@ -555,16 +579,18 @@ public final class TestPackage { assertTrue(tempFile2.delete()); } - private static ContentTypeManager getContentTypeManager(OPCPackage pkg) throws Exception { + private static ContentTypeManager getContentTypeManager(OPCPackage pkg) + throws IOException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field f = OPCPackage.class.getDeclaredField("contentTypeManager"); f.setAccessible(true); return (ContentTypeManager)f.get(pkg); } @Test - public void getPartsByName() throws Exception { + public void getPartsByName() throws IOException, InvalidFormatException { String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); + @SuppressWarnings("resource") OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ_WRITE); try { List rs = pkg.getPartsByName(Pattern.compile("/word/.*?\\.xml")); @@ -587,7 +613,7 @@ public final class TestPackage { } @Test - public void getPartSize() throws Exception { + public void getPartSize() throws IOException, InvalidFormatException { String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ); try { @@ -620,8 +646,10 @@ public final class TestPackage { } @Test - public void replaceContentType() throws Exception { + public void replaceContentType() + throws IOException, InvalidFormatException, SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { InputStream is = OpenXML4JTestDataSamples.openSampleStream("sample.xlsx"); + @SuppressWarnings("resource") OPCPackage p = OPCPackage.open(is); ContentTypeManager mgr = getContentTypeManager(p); @@ -637,10 +665,13 @@ public final class TestPackage { assertFalse(mgr.isContentTypeRegister("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")); assertTrue(mgr.isContentTypeRegister("application/vnd.ms-excel.sheet.macroEnabled.main+xml")); + p.revert(); + is.close(); } @Test(expected=IOException.class) - public void zipBombCreateAndHandle() throws Exception { + public void zipBombCreateAndHandle() + throws IOException, EncryptedDocumentException, InvalidFormatException { // #50090 / #56865 ZipFile zipFile = ZipHelper.openZipFile(OpenXML4JTestDataSamples.getSampleFile("sample.xlsx")); ByteArrayOutputStream bos = new ByteArrayOutputStream(); @@ -668,12 +699,13 @@ public final class TestPackage { append.write(spam); size += spam.length; } - append.write("".getBytes()); + append.write("".getBytes("UTF-8")); size += 8; e.setSize(size); } else { IOUtils.copy(is, append); } + is.close(); } append.closeEntry(); } @@ -690,7 +722,8 @@ public final class TestPackage { } @Test - public void zipBombCheckSizes() throws Exception { + public void zipBombCheckSizes() + throws IOException, EncryptedDocumentException, InvalidFormatException { File file = OpenXML4JTestDataSamples.getSampleFile("sample.xlsx"); try { @@ -709,14 +742,12 @@ public final class TestPackage { // use values close to, but within the limits ZipSecureFile.setMinInflateRatio(min_ratio-0.002); ZipSecureFile.setMaxEntrySize(max_size+1); - Workbook wb = WorkbookFactory.create(file, null, true); - wb.close(); + WorkbookFactory.create(file, null, true).close(); // check ratio out of bounds ZipSecureFile.setMinInflateRatio(min_ratio+0.002); try { - wb = WorkbookFactory.create(file, null, true); - wb.close(); + WorkbookFactory.create(file, null, true).close(); // this is a bit strange, as there will be different exceptions thrown // depending if this executed via "ant test" or within eclipse // maybe a difference in JDK ... @@ -730,8 +761,7 @@ public final class TestPackage { ZipSecureFile.setMinInflateRatio(min_ratio-0.002); ZipSecureFile.setMaxEntrySize(max_size-1); try { - wb = WorkbookFactory.create(file, null, true); - wb.close(); + WorkbookFactory.create(file, null, true).close(); } catch (InvalidFormatException e) { checkForZipBombException(e); } catch (POIXMLException e) { diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java index 3f316ed4ee..e3a6e47d76 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java @@ -338,7 +338,7 @@ public class TestSignatureInfo { @Override public byte[] timeStamp(byte[] data, RevocationData revocationData) throws Exception { revocationData.addCRL(crl); - return "time-stamp-token".getBytes(); + return "time-stamp-token".getBytes(LocaleUtil.CHARSET_1252); } @Override public void setSignatureConfig(SignatureConfig config) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java index 12a0bbf566..a545fa4b83 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java @@ -17,26 +17,37 @@ package org.apache.poi.xssf.extractor; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.text.DateFormatSymbols; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Locale; -import junit.framework.TestCase; +import javax.xml.xpath.XPathExpressionException; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.usermodel.XSSFMap; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Test; +import org.xml.sax.SAXException; /** * * @author Roberto Manicardi * */ -public class TestXSSFImportFromXML extends TestCase { +public class TestXSSFImportFromXML { - - public void testImportFromXML() throws Exception{ + @Test + public void testImportFromXML() throws IOException, XPathExpressionException, SAXException{ XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx"); try { @@ -82,10 +93,8 @@ public class TestXSSFImportFromXML extends TestCase { } } - - - - public void testMultiTable() throws Exception{ + @Test + public void testMultiTable() throws IOException, XPathExpressionException, SAXException{ XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings-complex-type.xlsx"); try { String cellC6 = "c6"; @@ -128,7 +137,8 @@ public class TestXSSFImportFromXML extends TestCase { } - public void testSingleAttributeCellWithNamespace() throws Exception{ + @Test + public void testSingleAttributeCellWithNamespace() throws IOException, XPathExpressionException, SAXException{ XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx"); try { int id = 1; @@ -148,16 +158,17 @@ public class TestXSSFImportFromXML extends TestCase { //Check for Schema element XSSFSheet sheet=wb.getSheetAt(0); - assertEquals(new Double(id), sheet.getRow(28).getCell(1).getNumericCellValue()); + assertEquals(new Double(id), sheet.getRow(28).getCell(1).getNumericCellValue(), 0); assertEquals(displayName, sheet.getRow(11).getCell(5).getStringCellValue()); assertEquals(ref, sheet.getRow(14).getCell(7).getStringCellValue()); - assertEquals(new Double(count), sheet.getRow(18).getCell(3).getNumericCellValue()); + assertEquals(new Double(count), sheet.getRow(18).getCell(3).getNumericCellValue(), 0); } finally { wb.close(); } } - - public void testOptionalFields_Bugzilla_55864() throws Exception { + + @Test + public void testOptionalFields_Bugzilla_55864() throws IOException, XPathExpressionException, SAXException { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55864.xlsx"); try { String testXML = "" + @@ -197,7 +208,8 @@ public class TestXSSFImportFromXML extends TestCase { } } - public void testOptionalFields_Bugzilla_57890() throws Exception { + @Test + public void testOptionalFields_Bugzilla_57890() throws IOException, ParseException, XPathExpressionException, SAXException { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57890.xlsx"); String testXML = "" + "" @@ -216,16 +228,18 @@ public class TestXSSFImportFromXML extends TestCase { XSSFRow rowData = sheet.getRow(1); assertEquals("Date", rowHeadings.getCell(0).getStringCellValue()); - Date date = new SimpleDateFormat("yyyy-MM-dd").parse("1991-3-14"); + Date date = new SimpleDateFormat("yyyy-MM-dd", DateFormatSymbols.getInstance(Locale.ROOT)).parse("1991-3-14"); assertEquals(date, rowData.getCell(0).getDateCellValue()); assertEquals("Amount Int", rowHeadings.getCell(1).getStringCellValue()); - assertEquals(new Double(Integer.MIN_VALUE), rowData.getCell(1).getNumericCellValue()); + assertEquals(new Double(Integer.MIN_VALUE), rowData.getCell(1).getNumericCellValue(), 0); assertEquals("Amount Double", rowHeadings.getCell(2).getStringCellValue()); - assertEquals(1.0000123, rowData.getCell(2).getNumericCellValue()); + assertEquals(1.0000123, rowData.getCell(2).getNumericCellValue(), 0); assertEquals("Amount UnsignedInt", rowHeadings.getCell(3).getStringCellValue()); - assertEquals(new Double(12345), rowData.getCell(3).getNumericCellValue()); + assertEquals(new Double(12345), rowData.getCell(3).getNumericCellValue(), 0); + + wb.close(); } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java index e06bd213f3..ef87a9fd06 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java @@ -23,10 +23,7 @@ import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; -import junit.framework.TestCase; - import org.apache.poi.POIDataSamples; -import org.apache.poi.POIXMLException; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.XSSFTestDataSamples; @@ -36,6 +33,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; +import junit.framework.TestCase; + /** * Test {@link SharedStringsTable}, the cache of strings in a workbook * @@ -114,12 +113,13 @@ public final class TestSharedStringsTable extends TestCase { assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(2)).toString()); } - public void testReadWrite() { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx"); - SharedStringsTable sst1 = wb.getSharedStringSource(); + public void testReadWrite() throws IOException { + XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx"); + SharedStringsTable sst1 = wb1.getSharedStringSource(); //serialize, read back and compare with the original - SharedStringsTable sst2 = XSSFTestDataSamples.writeOutAndReadBack(wb).getSharedStringSource(); + XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1); + SharedStringsTable sst2 = wb2.getSharedStringSource(); assertEquals(sst1.getCount(), sst2.getCount()); assertEquals(sst1.getUniqueCount(), sst2.getUniqueCount()); @@ -133,7 +133,11 @@ public final class TestSharedStringsTable extends TestCase { assertEquals(st1.toString(), st2.toString()); } - assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); + XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2); + assertNotNull(wb3); + wb3.close(); + wb2.close(); + wb1.close(); } /** @@ -144,34 +148,34 @@ public final class TestSharedStringsTable extends TestCase { * @author Philippe Laflamme */ public void testBug48936() throws IOException { - Workbook w = new XSSFWorkbook(); - Sheet s = w.createSheet(); + Workbook w1 = new XSSFWorkbook(); + Sheet s = w1.createSheet(); int i = 0; List lst = readStrings("48936-strings.txt"); for (String str : lst) { s.createRow(i++).createCell(0).setCellValue(str); } - try { - w = XSSFTestDataSamples.writeOutAndReadBack(w); - } catch (POIXMLException e){ - fail("Detected Bug #48936"); - } - s = w.getSheetAt(0); + Workbook w2 = XSSFTestDataSamples.writeOutAndReadBack(w1); + w1.close(); + s = w2.getSheetAt(0); i = 0; for (String str : lst) { String val = s.getRow(i++).getCell(0).getStringCellValue(); assertEquals(str, val); } - assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(w)); + Workbook w3 = XSSFTestDataSamples.writeOutAndReadBack(w2); + w2.close(); + assertNotNull(w3); + w3.close(); } private List readStrings(String filename) throws IOException { List strs = new ArrayList(); POIDataSamples samples = POIDataSamples.getSpreadSheetInstance(); BufferedReader br = new BufferedReader( - new InputStreamReader(samples.openResourceAsStream(filename))); + new InputStreamReader(samples.openResourceAsStream(filename), "UTF-8")); String s; while ((s = br.readLine()) != null) { if (s.trim().length() > 0) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestUnfixedBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestUnfixedBugs.java index c9bcdc0256..642c8ffbe1 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestUnfixedBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestUnfixedBugs.java @@ -367,7 +367,7 @@ public final class TestUnfixedBugs { } // verify that the resulting XML has the rows in correct order as required by Excel - String xml = new String(stream.toByteArray()); + String xml = new String(stream.toByteArray(), "UTF-8"); int posR12 = xml.indexOf(" pictures = wb.getAllPictures(); assertEquals(0, pictures.size()); @@ -97,13 +98,13 @@ public final class TestXSSFPicture extends BaseTestPicture { XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 1, 1, 10, 30); - byte[] jpegData = "picture1".getBytes(); + byte[] jpegData = "picture1".getBytes(LocaleUtil.CHARSET_1252); int jpegIdx = wb.addPicture(jpegData, XSSFWorkbook.PICTURE_TYPE_JPEG); XSSFPicture shape1 = drawing.createPicture(anchor, jpegIdx); assertEquals(1, shape1.getCTPicture().getNvPicPr().getCNvPr().getId()); - jpegData = "picture2".getBytes(); + jpegData = "picture2".getBytes(LocaleUtil.CHARSET_1252); jpegIdx = wb.addPicture(jpegData, XSSFWorkbook.PICTURE_TYPE_JPEG); XSSFPicture shape2 = drawing.createPicture(anchor, jpegIdx); assertEquals(2, shape2.getCTPicture().getNvPicPr().getCNvPr().getId()); @@ -118,8 +119,8 @@ public final class TestXSSFPicture extends BaseTestPicture { public void multiRelationShips() throws IOException { XSSFWorkbook wb = new XSSFWorkbook(); - byte[] pic1Data = "test jpeg data".getBytes(); - byte[] pic2Data = "test png data".getBytes(); + byte[] pic1Data = "test jpeg data".getBytes(LocaleUtil.CHARSET_1252); + byte[] pic2Data = "test png data".getBytes(LocaleUtil.CHARSET_1252); List pictures = wb.getAllPictures(); assertEquals(0, pictures.size()); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPictureData.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPictureData.java index df56819396..db2801fc3d 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPictureData.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPictureData.java @@ -18,18 +18,23 @@ package org.apache.poi.xssf.usermodel; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import java.io.IOException; import java.util.List; -import junit.framework.TestCase; - +import org.apache.poi.util.LocaleUtil; import org.apache.poi.xssf.XSSFTestDataSamples; +import org.junit.Test; /** * @author Yegor Kozlov */ -public final class TestXSSFPictureData extends TestCase { - public void testRead(){ +public final class TestXSSFPictureData { + @Test + public void testRead() throws IOException { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx"); List pictures = wb.getAllPictures(); //wb.getAllPictures() should return the same instance across multiple calls @@ -54,16 +59,18 @@ public final class TestXSSFPictureData extends TestCase { XSSFPictureData pict = pictures.get(idx); assertEquals("jpeg", pict.suggestFileExtension()); assertArrayEquals(pictureData, pict.getData()); + wb.close(); } - public void testNew(){ + @Test + public void testNew() throws IOException { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); XSSFDrawing drawing = sheet.createDrawingPatriarch(); - byte[] jpegData = "test jpeg data".getBytes(); - byte[] wmfData = "test wmf data".getBytes(); - byte[] pngData = "test png data".getBytes(); + byte[] jpegData = "test jpeg data".getBytes(LocaleUtil.CHARSET_1252); + byte[] wmfData = "test wmf data".getBytes(LocaleUtil.CHARSET_1252); + byte[] pngData = "test png data".getBytes(LocaleUtil.CHARSET_1252); List pictures = wb.getAllPictures(); assertEquals(0, pictures.size()); @@ -104,13 +111,15 @@ public final class TestXSSFPictureData extends TestCase { assertEquals("png", pictures2.get(pngIdx).suggestFileExtension()); assertArrayEquals(pngData, pictures2.get(pngIdx).getData()); - + wbBack.close(); + wb.close(); } /** * Bug 53568: XSSFPicture.getPictureData() can return null. */ - public void test53568(){ + @Test + public void test53568() throws IOException { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53568.xlsx"); List pictures = wb.getAllPictures(); assertNotNull(pictures); @@ -132,6 +141,6 @@ public final class TestXSSFPictureData extends TestCase { } } } - + wb.close(); } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 5fd62aa498..5043a3b977 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -31,7 +31,6 @@ import java.io.IOException; import java.util.Arrays; import java.util.Calendar; import java.util.Date; -import java.util.GregorianCalendar; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -53,6 +52,7 @@ import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellUtil; +import org.apache.poi.util.LocaleUtil; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.model.CalculationChain; @@ -1479,7 +1479,7 @@ public final class TestXSSFSheet extends BaseTestSheet { // Date cell = CellUtil.getCell(destRow, col++); assertEquals("[Date] E7 cell type", Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - final Date date = new GregorianCalendar(2000, Calendar.JANUARY, 1).getTime(); + final Date date = LocaleUtil.getLocaleCalendar(2000, Calendar.JANUARY, 1).getTime(); assertEquals("[Date] E7 cell value", date, cell.getDateCellValue()); // Boolean @@ -1641,12 +1641,12 @@ public final class TestXSSFSheet extends BaseTestSheet { col++; cell = CellUtil.getCell(destRow1, col); assertEquals("[Date] E10 cell type", Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - Date date = new GregorianCalendar(2000, Calendar.JANUARY, 1).getTime(); + Date date = LocaleUtil.getLocaleCalendar(2000, Calendar.JANUARY, 1).getTime(); assertEquals("[Date] E10 cell value", date, cell.getDateCellValue()); cell = CellUtil.getCell(destRow2, col); assertEquals("[Date] E11 cell type", Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - date = new GregorianCalendar(2000, Calendar.JANUARY, 2).getTime(); + date = LocaleUtil.getLocaleCalendar(2000, Calendar.JANUARY, 2).getTime(); assertEquals("[Date] E11 cell value", date, cell.getDateCellValue()); // Boolean diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 2bed6b6910..3e80022ca3 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -742,7 +742,7 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook { sheetBack.commit(); - String str = new String(IOUtils.toByteArray(sheetBack.getPackagePart().getInputStream())); + String str = new String(IOUtils.toByteArray(sheetBack.getPackagePart().getInputStream()), "UTF-8"); assertEquals(1, countMatches(str, "