diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2015-09-07 20:19:50 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2015-09-07 20:19:50 +0000 |
commit | a3d2eb57ff4ac10ae215b9b7404260103eb6f538 (patch) | |
tree | 7b44ec32a7cdd0e0828779c793061ac6a00e4c00 /src/ooxml/testcases/org/apache/poi | |
parent | f2e85496125f7fc68f7434244b9d9d5bb026933d (diff) | |
download | poi-a3d2eb57ff4ac10ae215b9b7404260103eb6f538.tar.gz poi-a3d2eb57ff4ac10ae215b9b7404260103eb6f538.zip |
Bug 58326 - Forbidden APIs patches - first set of changes for locale and timezone settings
also includes fixes for
- name shadowing
- unused deprecated method "getClipRect" in classes extending Graphics2d
- HexDump - replaced intermediate String.format calls with custom padding
- convert testcases to junit4
- closing resources
also tested with an arbitary timezone (PST) and locale (ru)
supresses forbidden apis check for
- LocaleUtil (the only place where Locale.getDefault() and TimeZone.getDefault() should be called)
- Classes using FontMetrics - without the actual text it's difficult to return something sane
Some usage of UTC and Locale.ROOT might be still wrong, e.g. in MapiMessage we don't access the
extended mapi properties, which might contain the timezone
DataFormatter has now a Observable property which need to be observed when custom formats are used
and the Locale changes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1701688 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/testcases/org/apache/poi')
9 files changed, 789 insertions, 660 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java b/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java index e185543020..61e57362c8 100644 --- a/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java +++ b/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java @@ -17,37 +17,49 @@ package org.apache.poi; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.IOException; import java.util.Calendar; import java.util.Date; -import java.util.GregorianCalendar; -import java.util.Locale; -import java.util.TimeZone; - -import junit.framework.TestCase; import org.apache.poi.POIXMLProperties.CoreProperties; import org.apache.poi.openxml4j.util.Nullable; +import org.apache.poi.util.LocaleUtil; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; /** * Test setting extended and custom OOXML properties */ -public final class TestPOIXMLProperties extends TestCase { +public final class TestPOIXMLProperties { + private XWPFDocument sampleDoc; private POIXMLProperties _props; private CoreProperties _coreProperties; + @Before public void setUp() throws IOException { - XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("documentProperties.docx"); + sampleDoc = XWPFTestDataSamples.openSampleDocument("documentProperties.docx"); _props = sampleDoc.getProperties(); _coreProperties = _props.getCoreProperties(); assertNotNull(_props); } + + @After + public void closeResources() throws Exception { + sampleDoc.close(); + } - public void testWorkbookExtendedProperties() { + @Test + public void testWorkbookExtendedProperties() throws Exception { XSSFWorkbook workbook = new XSSFWorkbook(); POIXMLProperties props = workbook.getProperties(); assertNotNull(props); @@ -71,7 +83,7 @@ public final class TestPOIXMLProperties extends TestCase { XSSFWorkbook newWorkbook = XSSFTestDataSamples.writeOutAndReadBack(workbook); - + workbook.close(); assertTrue(workbook != newWorkbook); @@ -88,16 +100,19 @@ public final class TestPOIXMLProperties extends TestCase { assertEquals(application, newCtProps.getApplication()); assertEquals(appVersion, newCtProps.getAppVersion()); + + newWorkbook.close(); } /** * Test usermodel API for setting custom properties */ - public void testCustomProperties() { - POIXMLDocument wb = new XSSFWorkbook(); + @Test + public void testCustomProperties() throws Exception { + POIXMLDocument wb1 = new XSSFWorkbook(); - POIXMLProperties.CustomProperties customProps = wb.getProperties().getCustomProperties(); + POIXMLProperties.CustomProperties customProps = wb1.getProperties().getCustomProperties(); customProps.addProperty("test-1", "string val"); customProps.addProperty("test-2", 1974); customProps.addProperty("test-3", 36.6); @@ -110,9 +125,10 @@ public final class TestPOIXMLProperties extends TestCase { } customProps.addProperty("test-4", true); - wb = XSSFTestDataSamples.writeOutAndReadBack((XSSFWorkbook)wb); + POIXMLDocument wb2 = XSSFTestDataSamples.writeOutAndReadBack((XSSFWorkbook)wb1); + wb1.close(); org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties ctProps = - wb.getProperties().getCustomProperties().getUnderlyingProperties(); + wb2.getProperties().getCustomProperties().getUnderlyingProperties(); assertEquals(4, ctProps.sizeOfPropertyArray()); org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty p; @@ -131,7 +147,7 @@ public final class TestPOIXMLProperties extends TestCase { p = ctProps.getPropertyArray(2); assertEquals("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.getFmtid()); assertEquals("test-3", p.getName()); - assertEquals(36.6, p.getR8()); + assertEquals(36.6, p.getR8(), 0); assertEquals(4, p.getPid()); p = ctProps.getPropertyArray(3); @@ -139,9 +155,12 @@ public final class TestPOIXMLProperties extends TestCase { assertEquals("test-4", p.getName()); assertEquals(true, p.getBool()); assertEquals(5, p.getPid()); + + wb2.close(); } - public void testDocumentProperties() { + @Test + public void testDocumentProperties() { String category = _coreProperties.getCategory(); assertEquals("test", category); String contentStatus = "Draft"; @@ -158,21 +177,25 @@ public final class TestPOIXMLProperties extends TestCase { assertEquals("Hello World", title); } - public void testTransitiveSetters() throws IOException { + @Test + public void testTransitiveSetters() throws IOException { XWPFDocument doc = new XWPFDocument(); CoreProperties cp = doc.getProperties().getCoreProperties(); - Date dateCreated = new GregorianCalendar(2010, 6, 15, 10, 0, 0).getTime(); + + Date dateCreated = LocaleUtil.getLocaleCalendar(2010, 6, 15, 10, 0, 0).getTime(); cp.setCreated(new Nullable<Date>(dateCreated)); assertEquals(dateCreated.toString(), cp.getCreated().toString()); - doc = XWPFTestDataSamples.writeOutAndReadBack(doc); + XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc); + doc.close(); cp = doc.getProperties().getCoreProperties(); Date dt3 = cp.getCreated(); assertEquals(dateCreated.toString(), dt3.toString()); - + doc2.close(); } + @Test public void testGetSetRevision() { String revision = _coreProperties.getRevision(); assertTrue("Revision number is 1", Integer.parseInt(revision) > 1); @@ -183,7 +206,7 @@ public final class TestPOIXMLProperties extends TestCase { } public static boolean dateTimeEqualToUTCString(Date dateTime, String utcString) { - Calendar utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.UK); + Calendar utcCalendar = LocaleUtil.getLocaleCalendar(LocaleUtil.TIMEZONE_UTC); utcCalendar.setTimeInMillis(dateTime.getTime()); String dateTimeUtcString = utcCalendar.get(Calendar.YEAR) + "-" + zeroPad((utcCalendar.get(Calendar.MONTH)+1)) + "-" + diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java index ddc36af0be..bbc53d6b97 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java @@ -17,111 +17,97 @@ package org.apache.poi.openxml4j.opc; +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.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.TimeZone; - -import junit.framework.TestCase; +import java.util.Locale; import org.apache.poi.POIDataSamples; import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.openxml4j.util.Nullable; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; +import org.apache.poi.util.LocaleUtil; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Test; import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty; -public final class TestPackageCoreProperties extends TestCase { - private static final POILogger logger = POILogFactory.getLogger(TestPackageCoreProperties.class); - +public final class TestPackageCoreProperties { /** * Test package core properties getters. */ - public void testGetProperties() { - try { - // Open the package - OPCPackage p = OPCPackage.open(OpenXML4JTestDataSamples.openSampleStream("TestPackageCoreProperiesGetters.docx")); - compareProperties(p); - p.revert(); - } catch (OpenXML4JException e) { - logger.log(POILogger.DEBUG, e.getMessage()); - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); - } + @Test + public void testGetProperties() throws Exception { + // Open the package + @SuppressWarnings("resource") + OPCPackage p = OPCPackage.open(OpenXML4JTestDataSamples.openSampleStream("TestPackageCoreProperiesGetters.docx")); + compareProperties(p); + p.revert(); } /** * Test package core properties setters. */ - public void testSetProperties() throws Exception { + @Test + public void testSetProperties() throws Exception { String inputPath = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCoreProperiesSetters.docx"); File outputFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageCoreProperiesSettersOUTPUT.docx"); // Open package - OPCPackage p = OPCPackage.open(inputPath, PackageAccess.READ_WRITE); - try { - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - df.setTimeZone(TimeZone.getTimeZone("UTC")); - Date dateToInsert = df.parse("2007-05-12T08:00:00Z", new ParsePosition( - 0)); - - PackageProperties props = p.getPackageProperties(); - props.setCategoryProperty("MyCategory"); - props.setContentStatusProperty("MyContentStatus"); - props.setContentTypeProperty("MyContentType"); - props.setCreatedProperty(new Nullable<Date>(dateToInsert)); - props.setCreatorProperty("MyCreator"); - props.setDescriptionProperty("MyDescription"); - props.setIdentifierProperty("MyIdentifier"); - props.setKeywordsProperty("MyKeywords"); - props.setLanguageProperty("MyLanguage"); - props.setLastModifiedByProperty("Julien Chable"); - props.setLastPrintedProperty(new Nullable<Date>(dateToInsert)); - props.setModifiedProperty(new Nullable<Date>(dateToInsert)); - props.setRevisionProperty("2"); - props.setTitleProperty("MyTitle"); - props.setSubjectProperty("MySubject"); - props.setVersionProperty("2"); - // Save the package in the output directory - p.save(outputFile); - - // Open the newly created file to check core properties saved values. - OPCPackage p2 = OPCPackage.open(outputFile.getAbsolutePath(), PackageAccess.READ); - try { - compareProperties(p2); - p2.revert(); - } finally { - p2.close(); - } - outputFile.delete(); - } finally { - // use revert to not re-write the input file - p.revert(); - } + @SuppressWarnings("resource") + OPCPackage p = OPCPackage.open(inputPath, PackageAccess.READ_WRITE); + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT); + df.setTimeZone(LocaleUtil.TIMEZONE_UTC); + Date dateToInsert = df.parse("2007-05-12T08:00:00Z", new ParsePosition(0)); + + PackageProperties props = p.getPackageProperties(); + props.setCategoryProperty("MyCategory"); + props.setContentStatusProperty("MyContentStatus"); + props.setContentTypeProperty("MyContentType"); + props.setCreatedProperty(new Nullable<Date>(dateToInsert)); + props.setCreatorProperty("MyCreator"); + props.setDescriptionProperty("MyDescription"); + props.setIdentifierProperty("MyIdentifier"); + props.setKeywordsProperty("MyKeywords"); + props.setLanguageProperty("MyLanguage"); + props.setLastModifiedByProperty("Julien Chable"); + props.setLastPrintedProperty(new Nullable<Date>(dateToInsert)); + props.setModifiedProperty(new Nullable<Date>(dateToInsert)); + props.setRevisionProperty("2"); + props.setTitleProperty("MyTitle"); + props.setSubjectProperty("MySubject"); + props.setVersionProperty("2"); + // Save the package in the output directory + p.save(outputFile); + p.revert(); + + // Open the newly created file to check core properties saved values. + @SuppressWarnings("resource") + OPCPackage p2 = OPCPackage.open(outputFile.getAbsolutePath(), PackageAccess.READ); + compareProperties(p2); + p2.revert(); + outputFile.delete(); } private void compareProperties(OPCPackage p) throws InvalidFormatException { - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - df.setTimeZone(TimeZone.getTimeZone("UTC")); - Date expectedDate = df.parse("2007-05-12T08:00:00Z", new ParsePosition( - 0)); + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT); + df.setTimeZone(LocaleUtil.TIMEZONE_UTC); + Date expectedDate = df.parse("2007-05-12T08:00:00Z", new ParsePosition(0)); // Gets the core properties PackageProperties props = p.getPackageProperties(); assertEquals("MyCategory", props.getCategoryProperty().getValue()); - assertEquals("MyContentStatus", props.getContentStatusProperty() - .getValue()); + assertEquals("MyContentStatus", props.getContentStatusProperty().getValue()); assertEquals("MyContentType", props.getContentTypeProperty().getValue()); assertEquals(expectedDate, props.getCreatedProperty().getValue()); assertEquals("MyCreator", props.getCreatorProperty().getValue()); @@ -129,8 +115,7 @@ public final class TestPackageCoreProperties extends TestCase { assertEquals("MyIdentifier", props.getIdentifierProperty().getValue()); assertEquals("MyKeywords", props.getKeywordsProperty().getValue()); assertEquals("MyLanguage", props.getLanguageProperty().getValue()); - assertEquals("Julien Chable", props.getLastModifiedByProperty() - .getValue()); + assertEquals("Julien Chable", props.getLastModifiedByProperty().getValue()); assertEquals(expectedDate, props.getLastPrintedProperty().getValue()); assertEquals(expectedDate, props.getModifiedProperty().getValue()); assertEquals("2", props.getRevisionProperty().getValue()); @@ -139,9 +124,10 @@ public final class TestPackageCoreProperties extends TestCase { assertEquals("2", props.getVersionProperty().getValue()); } - public void testCoreProperties_bug51374() throws Exception { - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - df.setTimeZone(TimeZone.getTimeZone("UTC")); + @Test + public void testCoreProperties_bug51374() throws Exception { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT); + df.setTimeZone(LocaleUtil.TIMEZONE_UTC); String strDate = "2007-05-12T08:00:00Z"; Date date = df.parse(strDate); @@ -197,7 +183,8 @@ public final class TestPackageCoreProperties extends TestCase { pkg.close(); } - public void testGetPropertiesLO() throws Exception { + @Test + public void testGetPropertiesLO() throws Exception { // Open the package OPCPackage pkg1 = OPCPackage.open(OpenXML4JTestDataSamples.openSampleStream("51444.xlsx")); PackageProperties props1 = pkg1.getPackageProperties(); @@ -206,13 +193,16 @@ public final class TestPackageCoreProperties extends TestCase { ByteArrayOutputStream out = new ByteArrayOutputStream(); pkg1.save(out); out.close(); + pkg1.close(); OPCPackage pkg2 = OPCPackage.open(new ByteArrayInputStream(out.toByteArray())); PackageProperties props2 = pkg2.getPackageProperties(); props2.setTitleProperty("Bug 51444 fixed"); + pkg2.close(); } - public void testEntitiesInCoreProps_56164() throws Exception { + @Test + public void testEntitiesInCoreProps_56164() throws Exception { InputStream is = OpenXML4JTestDataSamples.openSampleStream("CorePropertiesHasEntities.ooxml"); OPCPackage p = OPCPackage.open(is); is.close(); @@ -236,9 +226,12 @@ public final class TestPackageCoreProperties extends TestCase { // Check assertEquals("Stefan Kopf", props.getCreatorProperty().getValue()); + + p.close(); } - public void testListOfCustomProperties() throws Exception { + @Test + public void testListOfCustomProperties() throws Exception { File inp = POIDataSamples.getSpreadSheetInstance().getFile("ExcelWithAttachments.xlsm"); OPCPackage pkg = OPCPackage.open(inp, PackageAccess.READ); XSSFWorkbook wb = new XSSFWorkbook(pkg); diff --git a/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java b/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java index 9fd6664ad3..7d19525773 100644 --- a/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java +++ b/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java @@ -16,14 +16,36 @@ ==================================================================== */ package org.apache.poi.ss.format; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.xssf.XSSFITestDataProvider; +import static org.junit.Assert.assertEquals; +import java.util.Locale; +import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.util.LocaleUtil; +import org.apache.poi.xssf.XSSFITestDataProvider; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + /** Test the individual CellFormatPart types. */ public class TestCellFormatPart extends CellFormatTestBase { + + private static Locale userLocale; + + @BeforeClass + public static void setLocale() { + userLocale = LocaleUtil.getUserLocale(); + LocaleUtil.setUserLocale(Locale.ROOT); + } + + @AfterClass + public static void unsetLocale() { + LocaleUtil.setUserLocale(userLocale); + } + private static final Pattern NUMBER_EXTRACT_FMT = Pattern.compile( "([-+]?[0-9]+)(\\.[0-9]+)?.*(?:(e).*?([+-]?[0-9]+))", Pattern.CASE_INSENSITIVE); @@ -32,6 +54,7 @@ public class TestCellFormatPart extends CellFormatTestBase { super(XSSFITestDataProvider.instance); } + @Test public void testGeneralFormat() throws Exception { runFormatTests("GeneralFormatTests.xlsx", new CellValue() { public Object getValue(Cell cell) { @@ -54,6 +77,7 @@ public class TestCellFormatPart extends CellFormatTestBase { }); } + @Test public void testNumberApproxFormat() throws Exception { runFormatTests("NumberFormatApproxTests.xlsx", new CellValue() { public Object getValue(Cell cell) { @@ -73,14 +97,22 @@ public class TestCellFormatPart extends CellFormatTestBase { }); } + @Test public void testDateFormat() throws Exception { - runFormatTests("DateFormatTests.xlsx", new CellValue() { - public Object getValue(Cell cell) { - return cell.getDateCellValue(); - } - }); + TimeZone tz = LocaleUtil.getUserTimeZone(); + LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET")); + try { + runFormatTests("DateFormatTests.xlsx", new CellValue() { + public Object getValue(Cell cell) { + return cell.getDateCellValue(); + } + }); + } finally { + LocaleUtil.setUserTimeZone(tz); + } } + @Test public void testElapsedFormat() throws Exception { runFormatTests("ElapsedFormatTests.xlsx", new CellValue() { public Object getValue(Cell cell) { @@ -89,6 +121,7 @@ public class TestCellFormatPart extends CellFormatTestBase { }); } + @Test public void testTextFormat() throws Exception { runFormatTests("TextFormatTests.xlsx", new CellValue() { public Object getValue(Cell cell) { @@ -100,6 +133,7 @@ public class TestCellFormatPart extends CellFormatTestBase { }); } + @Test public void testConditions() throws Exception { runFormatTests("FormatConditionTests.xlsx", new CellValue() { Object getValue(Cell cell) { diff --git a/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java b/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java index f78819ca30..947ab212d7 100644 --- a/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java +++ b/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java @@ -17,13 +17,13 @@ package org.apache.poi.ss.usermodel; +import static org.junit.Assert.assertEquals; + import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.ss.ITestDataProvider; -import org.apache.poi.ss.SpreadsheetVersion; -import org.apache.poi.xssf.SXSSFITestDataProvider; -import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.usermodel.XSSFCell; +import org.junit.Test; /** * Class for combined testing of XML-specific functionality of @@ -37,6 +37,7 @@ public abstract class BaseTestXCell extends BaseTestCell { super(testDataProvider); } + @Test public void testXmlEncoding(){ Workbook wb = _testDataProvider.createWorkbook(); Sheet sh = wb.createSheet(); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java index 25f1d19179..9daf0ccb1a 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java @@ -19,6 +19,8 @@ package org.apache.poi.xssf.streaming; +import static org.junit.Assert.assertEquals; + import java.io.IOException; import javax.xml.namespace.QName; @@ -30,6 +32,8 @@ import org.apache.poi.xssf.SXSSFITestDataProvider; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.xmlbeans.XmlCursor; +import org.junit.AfterClass; +import org.junit.Test; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; /** @@ -42,11 +46,12 @@ public class TestSXSSFCell extends BaseTestXCell { super(SXSSFITestDataProvider.instance); } - @Override - public void tearDown(){ + @AfterClass + public static void tearDown(){ SXSSFITestDataProvider.instance.cleanup(); } + @Test public void testPreserveSpaces() throws IOException { String[] samplesWithSpaces = { " POI", diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java index 1b680d24e8..dd966bddc3 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java @@ -17,14 +17,18 @@ package org.apache.poi.xssf.usermodel; -import java.io.InputStream; -import java.io.PrintStream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; -import junit.framework.Assert; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Locale; import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.ss.formula.eval.TestFormulasFromSpreadsheet; import org.apache.poi.ss.formula.functions.TestMathX; import org.apache.poi.ss.usermodel.Cell; @@ -32,7 +36,13 @@ import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.util.LocaleUtil; +import org.junit.AfterClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; /** * Performs much the same role as {@link TestFormulasFromSpreadsheet}, @@ -44,206 +54,127 @@ import org.apache.poi.openxml4j.opc.OPCPackage; * Excel 2007, and re-save it as FormulaEvalTestData_Copy.xlsx * */ -public final class TestFormulaEvaluatorOnXSSF extends TestCase { - - private static final class Result { - public static final int SOME_EVALUATIONS_FAILED = -1; - public static final int ALL_EVALUATIONS_SUCCEEDED = +1; - public static final int NO_EVALUATIONS_FOUND = 0; - } +@RunWith(Parameterized.class) +public final class TestFormulaEvaluatorOnXSSF { + private static XSSFWorkbook workbook; + private static Sheet sheet; + private static FormulaEvaluator evaluator; + private static Locale userLocale; + /** * This class defines constants for navigating around the test data spreadsheet used for these tests. */ - private static final class SS { + private static interface SS { /** * Name of the test spreadsheet (found in the standard test data folder) */ - public final static String FILENAME = "FormulaEvalTestData_Copy.xlsx"; + String FILENAME = "FormulaEvalTestData_Copy.xlsx"; /** * Row (zero-based) in the test spreadsheet where the operator examples start. */ - public static final int START_OPERATORS_ROW_INDEX = 22; // Row '23' + int START_OPERATORS_ROW_INDEX = 22; // Row '23' /** * Row (zero-based) in the test spreadsheet where the function examples start. */ - public static final int START_FUNCTIONS_ROW_INDEX = 95; // Row '96' + int START_FUNCTIONS_ROW_INDEX = 95; // Row '96' /** * Index of the column that contains the function names */ - public static final int COLUMN_INDEX_FUNCTION_NAME = 1; // Column 'B' + int COLUMN_INDEX_FUNCTION_NAME = 1; // Column 'B' /** * Used to indicate when there are no more functions left */ - public static final String FUNCTION_NAMES_END_SENTINEL = "<END-OF-FUNCTIONS>"; + String FUNCTION_NAMES_END_SENTINEL = "<END-OF-FUNCTIONS>"; /** * Index of the column where the test values start (for each function) */ - public static final short COLUMN_INDEX_FIRST_TEST_VALUE = 3; // Column 'D' + short COLUMN_INDEX_FIRST_TEST_VALUE = 3; // Column 'D' /** * Each function takes 4 rows in the test spreadsheet */ - public static final int NUMBER_OF_ROWS_PER_FUNCTION = 4; + int NUMBER_OF_ROWS_PER_FUNCTION = 4; } - private XSSFWorkbook workbook; - private Sheet sheet; - // Note - multiple failures are aggregated before ending. - // If one or more functions fail, a single AssertionFailedError is thrown at the end - private int _functionFailureCount; - private int _functionSuccessCount; - private int _evaluationFailureCount; - private int _evaluationSuccessCount; + @Parameter(value = 0) + public String targetFunctionName; + @Parameter(value = 1) + public int formulasRowIdx; + @Parameter(value = 2) + public int expectedValuesRowIdx; - private static final Cell getExpectedValueCell(Row row, short columnIndex) { - if (row == null) { - return null; - } - return row.getCell(columnIndex); - } + @AfterClass + public static void closeResource() throws Exception { + LocaleUtil.setUserLocale(userLocale); + workbook.close(); + } + + @Parameters(name="{0}") + public static Collection<Object[]> data() throws Exception { + // Function "Text" uses custom-formats which are locale specific + // can't set the locale on a per-testrun execution, as some settings have been + // already set, when we would try to change the locale by then + userLocale = LocaleUtil.getUserLocale(); + LocaleUtil.setUserLocale(Locale.ROOT); + workbook = new XSSFWorkbook( OPCPackage.open(HSSFTestDataSamples.getSampleFile(SS.FILENAME), PackageAccess.READ) ); + sheet = workbook.getSheetAt( 0 ); + evaluator = new XSSFFormulaEvaluator(workbook); + + List<Object[]> data = new ArrayList<Object[]>(); + + processFunctionGroup(data, SS.START_OPERATORS_ROW_INDEX, null); + processFunctionGroup(data, SS.START_FUNCTIONS_ROW_INDEX, null); + // example for debugging individual functions/operators: + // processFunctionGroup(data, SS.START_OPERATORS_ROW_INDEX, "ConcatEval"); + // processFunctionGroup(data, SS.START_FUNCTIONS_ROW_INDEX, "Text"); - private static void confirmExpectedResult(String msg, Cell expected, CellValue actual) { - if (expected == null) { - throw new AssertionFailedError(msg + " - Bad setup data expected value is null"); - } - if(actual == null) { - throw new AssertionFailedError(msg + " - actual value was null"); - } - - switch (expected.getCellType()) { - case Cell.CELL_TYPE_BLANK: - assertEquals(msg, Cell.CELL_TYPE_BLANK, actual.getCellType()); - break; - case Cell.CELL_TYPE_BOOLEAN: - assertEquals(msg, Cell.CELL_TYPE_BOOLEAN, actual.getCellType()); - assertEquals(msg, expected.getBooleanCellValue(), actual.getBooleanValue()); - break; - case Cell.CELL_TYPE_ERROR: - assertEquals(msg, Cell.CELL_TYPE_ERROR, actual.getCellType()); - if(false) { // TODO: fix ~45 functions which are currently returning incorrect error values - assertEquals(msg, expected.getErrorCellValue(), actual.getErrorValue()); - } - break; - case Cell.CELL_TYPE_FORMULA: // will never be used, since we will call method after formula evaluation - throw new AssertionFailedError("Cannot expect formula as result of formula evaluation: " + msg); - case Cell.CELL_TYPE_NUMERIC: - assertEquals(msg, Cell.CELL_TYPE_NUMERIC, actual.getCellType()); - TestMathX.assertEquals(msg, expected.getNumericCellValue(), actual.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR); -// double delta = Math.abs(expected.getNumericCellValue()-actual.getNumberValue()); -// double pctExpected = Math.abs(0.00001*expected.getNumericCellValue()); -// assertTrue(msg, delta <= pctExpected); - break; - case Cell.CELL_TYPE_STRING: - assertEquals(msg, Cell.CELL_TYPE_STRING, actual.getCellType()); - assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getStringValue()); - break; - } - } - + return data; + } + + /** + * @param startRowIndex row index in the spreadsheet where the first function/operator is found + * @param testFocusFunctionName name of a single function/operator to test alone. + * Typically pass <code>null</code> to test all functions + */ + private static void processFunctionGroup(List<Object[]> data, int startRowIndex, String testFocusFunctionName) { + for (int rowIndex = startRowIndex; true; rowIndex += SS.NUMBER_OF_ROWS_PER_FUNCTION) { + Row r = sheet.getRow(rowIndex); + String targetFunctionName = getTargetFunctionName(r); + if(targetFunctionName == null) { + fail("Test spreadsheet cell empty on row (" + + (rowIndex+1) + "). Expected function name or '" + + SS.FUNCTION_NAMES_END_SENTINEL + "'"); + } + if(targetFunctionName.equals(SS.FUNCTION_NAMES_END_SENTINEL)) { + // found end of functions list + break; + } + if(testFocusFunctionName == null || targetFunctionName.equalsIgnoreCase(testFocusFunctionName)) { + + // expected results are on the row below + Row expectedValuesRow = sheet.getRow(rowIndex + 1); + if(expectedValuesRow == null) { + int missingRowNum = rowIndex + 2; //+1 for 1-based, +1 for next row + fail("Missing expected values row for function '" + + targetFunctionName + " (row " + missingRowNum + ")"); + } + + data.add(new Object[]{targetFunctionName, rowIndex, rowIndex + 1}); + } + } + } - protected void setUp() throws Exception { - if (workbook == null) { - InputStream is = HSSFTestDataSamples.openSampleFileStream(SS.FILENAME); - OPCPackage pkg = OPCPackage.open(is); - workbook = new XSSFWorkbook( pkg ); - sheet = workbook.getSheetAt( 0 ); - } - _functionFailureCount = 0; - _functionSuccessCount = 0; - _evaluationFailureCount = 0; - _evaluationSuccessCount = 0; - } - - /** - * Checks that we can actually open the file - */ - public void testOpen() { - assertNotNull(workbook); - } - - /** - * Disabled for now, as many things seem to break - * for XSSF, which is a shame - */ - public void testFunctionsFromTestSpreadsheet() { - - processFunctionGroup(SS.START_OPERATORS_ROW_INDEX, null); - processFunctionGroup(SS.START_FUNCTIONS_ROW_INDEX, null); - // example for debugging individual functions/operators: -// processFunctionGroup(SS.START_OPERATORS_ROW_INDEX, "ConcatEval"); -// processFunctionGroup(SS.START_FUNCTIONS_ROW_INDEX, "AVERAGE"); - - // confirm results - String successMsg = "There were " - + _evaluationSuccessCount + " successful evaluation(s) and " - + _functionSuccessCount + " function(s) without error"; - if(_functionFailureCount > 0) { - String msg = _functionFailureCount + " function(s) failed in " - + _evaluationFailureCount + " evaluation(s). " + successMsg; - throw new AssertionFailedError(msg); - } - if(false) { // normally no output for successful tests - System.out.println(getClass().getName() + ": " + successMsg); - } - } - - /** - * @param startRowIndex row index in the spreadsheet where the first function/operator is found - * @param testFocusFunctionName name of a single function/operator to test alone. - * Typically pass <code>null</code> to test all functions - */ - private void processFunctionGroup(int startRowIndex, String testFocusFunctionName) { - - FormulaEvaluator evaluator = new XSSFFormulaEvaluator(workbook); - int rowIndex = startRowIndex; - while (true) { - Row r = sheet.getRow(rowIndex); - String targetFunctionName = getTargetFunctionName(r); - if(targetFunctionName == null) { - throw new AssertionFailedError("Test spreadsheet cell empty on row (" - + (rowIndex+1) + "). Expected function name or '" - + SS.FUNCTION_NAMES_END_SENTINEL + "'"); - } - if(targetFunctionName.equals(SS.FUNCTION_NAMES_END_SENTINEL)) { - // found end of functions list - break; - } - if(testFocusFunctionName == null || targetFunctionName.equalsIgnoreCase(testFocusFunctionName)) { - - // expected results are on the row below - Row expectedValuesRow = sheet.getRow(rowIndex + 1); - if(expectedValuesRow == null) { - int missingRowNum = rowIndex + 2; //+1 for 1-based, +1 for next row - throw new AssertionFailedError("Missing expected values row for function '" - + targetFunctionName + " (row " + missingRowNum + ")"); - } - switch(processFunctionRow(evaluator, targetFunctionName, r, expectedValuesRow)) { - case Result.ALL_EVALUATIONS_SUCCEEDED: _functionSuccessCount++; break; - case Result.SOME_EVALUATIONS_FAILED: _functionFailureCount++; break; - default: - throw new RuntimeException("unexpected result"); - case Result.NO_EVALUATIONS_FOUND: // do nothing - } - } - rowIndex += SS.NUMBER_OF_ROWS_PER_FUNCTION; - } - } - - /** - * - * @return a constant from the local Result class denoting whether there were any evaluation - * cases, and whether they all succeeded. - */ - private int processFunctionRow(FormulaEvaluator evaluator, String targetFunctionName, - Row formulasRow, Row expectedValuesRow) { - - int result = Result.NO_EVALUATIONS_FOUND; // so far + @Test + public void processFunctionRow() { + Row formulasRow = sheet.getRow(formulasRowIdx); + Row expectedValuesRow = sheet.getRow(expectedValuesRowIdx); + short endcolnum = formulasRow.getLastCellNum(); // iterate across the row for all the evaluation cases @@ -256,31 +187,44 @@ public final class TestFormulaEvaluatorOnXSSF extends TestCase { continue; } - CellValue actualValue; - try { - actualValue = evaluator.evaluate(c); - } catch (RuntimeException e) { - _evaluationFailureCount ++; - printShortStackTrace(System.err, e); - result = Result.SOME_EVALUATIONS_FAILED; - continue; - } + CellValue actValue = evaluator.evaluate(c); + Cell expValue = (expectedValuesRow == null) ? null : expectedValuesRow.getCell(colnum); - Cell expectedValueCell = getExpectedValueCell(expectedValuesRow, colnum); - try { - confirmExpectedResult("Function '" + targetFunctionName + "': Formula: " + c.getCellFormula() + " @ " + formulasRow.getRowNum() + ":" + colnum, - expectedValueCell, actualValue); - _evaluationSuccessCount ++; - if(result != Result.SOME_EVALUATIONS_FAILED) { - result = Result.ALL_EVALUATIONS_SUCCEEDED; - } - } catch (AssertionFailedError e) { - _evaluationFailureCount ++; - printShortStackTrace(System.err, e); - result = Result.SOME_EVALUATIONS_FAILED; - } + String msg = String.format(Locale.ROOT, "Function '%s': Formula: %s @ %d:%d" + , targetFunctionName, c.getCellFormula(), formulasRow.getRowNum(), colnum); + + assertNotNull(msg + " - Bad setup data expected value is null", expValue); + assertNotNull(msg + " - actual value was null", actValue); + + switch (expValue.getCellType()) { + case Cell.CELL_TYPE_BLANK: + assertEquals(msg, Cell.CELL_TYPE_BLANK, actValue.getCellType()); + break; + case Cell.CELL_TYPE_BOOLEAN: + assertEquals(msg, Cell.CELL_TYPE_BOOLEAN, actValue.getCellType()); + assertEquals(msg, expValue.getBooleanCellValue(), actValue.getBooleanValue()); + break; + case Cell.CELL_TYPE_ERROR: + assertEquals(msg, Cell.CELL_TYPE_ERROR, actValue.getCellType()); +// if(false) { // TODO: fix ~45 functions which are currently returning incorrect error values +// assertEquals(msg, expValue.getErrorCellValue(), actValue.getErrorValue()); +// } + break; + case Cell.CELL_TYPE_FORMULA: // will never be used, since we will call method after formula evaluation + fail("Cannot expect formula as result of formula evaluation: " + msg); + case Cell.CELL_TYPE_NUMERIC: + assertEquals(msg, Cell.CELL_TYPE_NUMERIC, actValue.getCellType()); + TestMathX.assertEquals(msg, expValue.getNumericCellValue(), actValue.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR); +// double delta = Math.abs(expValue.getNumericCellValue()-actValue.getNumberValue()); +// double pctExpValue = Math.abs(0.00001*expValue.getNumericCellValue()); +// assertTrue(msg, delta <= pctExpValue); + break; + case Cell.CELL_TYPE_STRING: + assertEquals(msg, Cell.CELL_TYPE_STRING, actValue.getCellType()); + assertEquals(msg, expValue.getRichStringCellValue().getString(), actValue.getStringValue()); + break; + } } - return result; } /* @@ -301,40 +245,6 @@ public final class TestFormulaEvaluatorOnXSSF extends TestCase { return false; } - - /** - * Useful to keep output concise when expecting many failures to be reported by this test case - */ - private static void printShortStackTrace(PrintStream ps, Throwable e) { - StackTraceElement[] stes = e.getStackTrace(); - - int startIx = 0; - // skip any top frames inside junit.framework.Assert - while(startIx<stes.length) { - if(!stes[startIx].getClassName().equals(Assert.class.getName())) { - break; - } - startIx++; - } - // skip bottom frames (part of junit framework) - int endIx = startIx+1; - while(endIx < stes.length) { - if(stes[endIx].getClassName().equals(TestCase.class.getName())) { - break; - } - endIx++; - } - if(startIx >= endIx) { - // something went wrong. just print the whole stack trace - e.printStackTrace(ps); - } - endIx -= 4; // skip 4 frames of reflection invocation - ps.println(e.toString()); - for(int i=startIx; i<endIx; i++) { - ps.println("\tat " + stes[i].toString()); - } - } - /** * @return <code>null</code> if cell is missing, empty or blank */ @@ -355,7 +265,7 @@ public final class TestFormulaEvaluatorOnXSSF extends TestCase { return cell.getRichStringCellValue().getString(); } - throw new AssertionFailedError("Bad cell type for 'function name' column: (" - + cell.getCellType() + ") row (" + (r.getRowNum() +1) + ")"); + fail("Bad cell type for 'function name' column: ("+cell.getColumnIndex()+") row ("+(r.getRowNum()+1)+")"); + return null; } } 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 bea8e7129b..c9bcdc0256 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestUnfixedBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestUnfixedBugs.java @@ -17,6 +17,12 @@ package org.apache.poi.xssf.usermodel;
+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 java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
@@ -25,10 +31,6 @@ import java.io.OutputStream; import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.Date;
-import java.util.GregorianCalendar;
-import java.util.Locale;
-
-import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ss.usermodel.Cell;
@@ -42,6 +44,7 @@ import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.ss.util.RegionUtil;
+import org.apache.poi.util.LocaleUtil;
import org.apache.poi.xssf.SXSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
@@ -57,7 +60,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow; * Bugzilla id's PLEASE MOVE tests from this class to TestBugs once the bugs are
* fixed, so that they are then run automatically.
*/
-public final class TestUnfixedBugs extends TestCase {
+public final class TestUnfixedBugs {
+ @Test
public void testBug54084Unicode() throws IOException {
// sample XLSX with the same text-contents as the text-file above
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("54084 - Greek - beyond BMP.xlsx");
@@ -76,8 +80,14 @@ public final class TestUnfixedBugs extends TestCase { verifyBug54084Unicode(wbWritten);
// finally also write it out via the streaming interface and verify that we still can read it back in
- Workbook wbStreamingWritten = SXSSFITestDataProvider.instance.writeOutAndReadBack(new SXSSFWorkbook(wb));
+ SXSSFWorkbook swb = new SXSSFWorkbook(wb);
+ Workbook wbStreamingWritten = SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
verifyBug54084Unicode(wbStreamingWritten);
+
+ wbWritten.close();
+ swb.close();
+ wbStreamingWritten.close();
+ wb.close();
}
private void verifyBug54084Unicode(Workbook wb) {
@@ -95,7 +105,8 @@ public final class TestUnfixedBugs extends TestCase { assertEquals("The data in the text-file should exactly match the data that we read from the workbook", testData, value);
}
- public void test54071() {
+ @Test
+ public void test54071() throws Exception {
Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("54071.xlsx");
Sheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
@@ -120,8 +131,11 @@ public final class TestUnfixedBugs extends TestCase { }
}
}
+
+ workbook.close();
}
+ @Test
public void test54071Simple() {
double value1 = 41224.999988425923;
double value2 = 41224.999988368058;
@@ -143,15 +157,13 @@ public final class TestUnfixedBugs extends TestCase { // second to be different here!
int startYear = 1900;
int dayAdjust = -1; // Excel thinks 2/29/1900 is a valid date, which it isn't
- Calendar calendar1 = new GregorianCalendar(Locale.ROOT);
- calendar1.set(startYear,0, wholeDays1 + dayAdjust, 0, 0, 0);
+ Calendar calendar1 = LocaleUtil.getLocaleCalendar(startYear,0, wholeDays1 + dayAdjust);
calendar1.set(Calendar.MILLISECOND, millisecondsInDay1);
// this is the rounding part:
calendar1.add(Calendar.MILLISECOND, 500);
calendar1.clear(Calendar.MILLISECOND);
- Calendar calendar2 = new GregorianCalendar(Locale.ROOT);
- calendar2.set(startYear,0, wholeDays2 + dayAdjust, 0, 0, 0);
+ Calendar calendar2 = LocaleUtil.getLocaleCalendar(startYear,0, wholeDays2 + dayAdjust);
calendar2.set(Calendar.MILLISECOND, millisecondsInDay2);
// this is the rounding part:
calendar2.add(Calendar.MILLISECOND, 500);
@@ -163,7 +175,8 @@ public final class TestUnfixedBugs extends TestCase { assertEquals(DateUtil.getJavaDate(value1, false), DateUtil.getJavaDate(value2, false));
}
- public void test57236() {
+ @Test
+ public void test57236() throws Exception {
// Having very small numbers leads to different formatting, Excel uses the scientific notation, but POI leads to "0"
/*
@@ -189,10 +202,12 @@ public final class TestUnfixedBugs extends TestCase { }
}
}
+ wb.close();
}
// When this is fixed, the test case should go to BaseTestXCell with
// adjustments to use _testDataProvider to also verify this for XSSF
+ @Test
public void testBug57294() throws IOException {
Workbook wb = SXSSFITestDataProvider.instance.createWorkbook();
@@ -217,6 +232,8 @@ public final class TestUnfixedBugs extends TestCase { assertEquals(0, strBack.getIndexOfFormattingRun(0));
assertEquals(2, strBack.getIndexOfFormattingRun(1));
assertEquals(4, strBack.getIndexOfFormattingRun(2));
+
+ wbBack.close();
}
@Test
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index f487726c95..d6f573c1ca 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -38,6 +38,7 @@ import java.util.Calendar; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TimeZone; import java.util.TreeMap; import org.apache.poi.EncryptedDocumentException; @@ -85,6 +86,7 @@ import org.apache.poi.ss.usermodel.WorkbookFactory; 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.util.LocaleUtil; import org.apache.poi.util.TempFile; import org.apache.poi.xssf.XLSBUnsupportedException; import org.apache.poi.xssf.XSSFITestDataProvider; @@ -120,7 +122,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { * the wrong sheet name */ @Test - public void bug45430() { + public void bug45430() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45430.xlsx"); assertFalse(wb.isMacroEnabled()); assertEquals(3, wb.getNumberOfNames()); @@ -144,6 +146,9 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb); assertEquals(3, nwb.getNumberOfNames()); assertEquals("SheetA!$A$1", nwb.getNameAt(0).getRefersToFormula()); + + nwb.close(); + wb.close(); } /** @@ -151,69 +156,79 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { */ @Test public void bug45431() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45431.xlsm"); - OPCPackage pkg = wb.getPackage(); - assertTrue(wb.isMacroEnabled()); + XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("45431.xlsm"); + OPCPackage pkg1 = wb1.getPackage(); + assertTrue(wb1.isMacroEnabled()); // Check the various macro related bits can be found - PackagePart vba = pkg.getPart( + PackagePart vba = pkg1.getPart( PackagingURIHelper.createPartName("/xl/vbaProject.bin") ); assertNotNull(vba); // And the drawing bit - PackagePart drw = pkg.getPart( + PackagePart drw = pkg1.getPart( PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml") ); assertNotNull(drw); // Save and re-open, both still there - XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb); - OPCPackage nPkg = nwb.getPackage(); - assertTrue(nwb.isMacroEnabled()); + XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1); + pkg1.close(); + wb1.close(); + + OPCPackage pkg2 = wb2.getPackage(); + assertTrue(wb2.isMacroEnabled()); - vba = nPkg.getPart( + vba = pkg2.getPart( PackagingURIHelper.createPartName("/xl/vbaProject.bin") ); assertNotNull(vba); - drw = nPkg.getPart( + drw = pkg2.getPart( PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml") ); assertNotNull(drw); // And again, just to be sure - nwb = XSSFTestDataSamples.writeOutAndReadBack(nwb); - nPkg = nwb.getPackage(); - assertTrue(nwb.isMacroEnabled()); + XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2); + pkg2.close(); + wb2.close(); + OPCPackage pkg3 = wb3.getPackage(); + assertTrue(wb3.isMacroEnabled()); - vba = nPkg.getPart( + vba = pkg3.getPart( PackagingURIHelper.createPartName("/xl/vbaProject.bin") ); assertNotNull(vba); - drw = nPkg.getPart( + drw = pkg3.getPart( PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml") ); assertNotNull(drw); + + pkg3.close(); + wb3.close(); } @Test - public void bug47504() { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47504.xlsx"); - assertEquals(1, wb.getNumberOfSheets()); - XSSFSheet sh = wb.getSheetAt(0); + public void bug47504() throws Exception { + XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47504.xlsx"); + assertEquals(1, wb1.getNumberOfSheets()); + XSSFSheet sh = wb1.getSheetAt(0); XSSFDrawing drawing = sh.createDrawingPatriarch(); List<POIXMLDocumentPart> rels = drawing.getRelations(); assertEquals(1, rels.size()); assertEquals("Sheet1!A1", rels.get(0).getPackageRelationship().getTargetURI().getFragment()); // And again, just to be sure - wb = XSSFTestDataSamples.writeOutAndReadBack(wb); - assertEquals(1, wb.getNumberOfSheets()); - sh = wb.getSheetAt(0); + XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1); + wb1.close(); + assertEquals(1, wb2.getNumberOfSheets()); + sh = wb2.getSheetAt(0); drawing = sh.createDrawingPatriarch(); rels = drawing.getRelations(); assertEquals(1, rels.size()); assertEquals("Sheet1!A1", rels.get(0).getPackageRelationship().getTargetURI().getFragment()); + wb2.close(); } /** @@ -224,7 +239,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { */ @Test public void bug49020() throws Exception { - /*XSSFWorkbook wb =*/ XSSFTestDataSamples.openSampleWorkbook("BrNotClosed.xlsx"); + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("BrNotClosed.xlsx"); + wb.close(); } /** @@ -235,11 +251,12 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49325.xlsx"); CTWorksheet sh = wb.getSheetAt(0).getCTWorksheet(); assertNotNull(sh.getPhoneticPr()); + wb.close(); } /** * Names which are defined with a Sheet - * should return that sheet index properly + * should return that sheet index properly */ @Test public void bug48923() throws Exception { @@ -270,6 +287,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("Test", test.getNameName()); assertEquals("Sheet1", test.getSheetName()); assertEquals(-1, test.getSheetIndex()); + + wb.close(); } /** @@ -277,49 +296,53 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { * NameXPtgs. * Blows up on: * IF(B6= (ROUNDUP(B6,0) + ROUNDDOWN(B6,0))/2, MROUND(B6,2),ROUND(B6,0)) - * + * * TODO: delete this test case when MROUND and VAR are implemented */ @Test public void bug48539() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48539.xlsx"); - assertEquals(3, wb.getNumberOfSheets()); - assertEquals(0, wb.getNumberOfNames()); - - // Try each cell individually - XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb); - for(int i=0; i<wb.getNumberOfSheets(); i++) { - Sheet s = wb.getSheetAt(i); - for(Row r : s) { - for(Cell c : r) { - if(c.getCellType() == Cell.CELL_TYPE_FORMULA) { - String formula = c.getCellFormula(); - CellValue cv; - try { - cv = eval.evaluate(c); - } catch (Exception e) { - throw new RuntimeException("Can't evaluate formula: " + formula, e); - } - - if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) { - // assert that the calculated value agrees with - // the cached formula result calculated by Excel - double cachedFormulaResult = c.getNumericCellValue(); - double evaluatedFormulaResult = cv.getNumberValue(); - assertEquals(c.getCellFormula(), cachedFormulaResult, evaluatedFormulaResult, 1E-7); + try { + assertEquals(3, wb.getNumberOfSheets()); + assertEquals(0, wb.getNumberOfNames()); + + // Try each cell individually + XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb); + for(int i=0; i<wb.getNumberOfSheets(); i++) { + Sheet s = wb.getSheetAt(i); + for(Row r : s) { + for(Cell c : r) { + if(c.getCellType() == Cell.CELL_TYPE_FORMULA) { + String formula = c.getCellFormula(); + CellValue cv; + try { + cv = eval.evaluate(c); + } catch (Exception e) { + throw new RuntimeException("Can't evaluate formula: " + formula, e); + } + + if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) { + // assert that the calculated value agrees with + // the cached formula result calculated by Excel + double cachedFormulaResult = c.getNumericCellValue(); + double evaluatedFormulaResult = cv.getNumberValue(); + assertEquals(c.getCellFormula(), cachedFormulaResult, evaluatedFormulaResult, 1E-7); + } } } } } - } - // Now all of them - XSSFFormulaEvaluator.evaluateAllFormulaCells(wb); + // Now all of them + XSSFFormulaEvaluator.evaluateAllFormulaCells(wb); + } finally { + wb.close(); + } } /** * Foreground colours should be found even if - * a theme is used + * a theme is used */ @Test public void bug48779() throws Exception { @@ -351,11 +374,13 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(64, cs.getFillBackgroundColor()); assertEquals(null, cs.getFillBackgroundXSSFColor().getARGBHex()); assertEquals(null, cs.getFillBackgroundColorColor().getARGBHex()); + + wb.close(); } /** * Ensure General and @ format are working properly - * for integers + * for integers */ @Test public void bug47490() throws Exception { @@ -384,6 +409,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("123", df.formatCellValue(r.getCell(2))); assertEquals("123", df.formatRawCellContents(123.0, -1, "@")); assertEquals("123", df.formatRawCellContents(123.0, -1, "General")); + + wb.close(); } /** @@ -399,6 +426,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49609.xlsx"); assertEquals("FAM", wb.getSheetName(0)); assertEquals("Cycle", wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue()); + wb.close(); } @@ -425,6 +453,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("sale_1*sale_2", cell.getCellFormula()); assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType()); assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString()); + + wb.close(); } /** @@ -436,8 +466,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { */ @Test public void bug49941() throws Exception { - XSSFWorkbook wb = new XSSFWorkbook(); - XSSFSheet s = wb.createSheet(); + XSSFWorkbook wb1 = new XSSFWorkbook(); + XSSFSheet s = wb1.createSheet(); XSSFRow r = s.createRow(0); XSSFCell c = r.createCell(0); @@ -459,9 +489,9 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { ); // Save and check - XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb); - wb.close(); - s = wbBack.getSheetAt(0); + XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1); + wb1.close(); + s = wb2.getSheetAt(0); r = s.getRow(0); c = r.getCell(0); assertEquals(" with spaces ", c.getRichStringCellValue().toString()); @@ -480,7 +510,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { ); // Apply a font - XSSFFont f = wbBack.createFont(); + XSSFFont f = wb2.createFont(); f.setBold(true); c.getRichStringCellValue().applyFont(0, 5, f); assertEquals("hello world", c.getRichStringCellValue().toString()); @@ -500,12 +530,14 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { ); // Save and check - wbBack = XSSFTestDataSamples.writeOutAndReadBack(wbBack); - s = wbBack.getSheetAt(0); + XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2); + wb2.close(); + + s = wb3.getSheetAt(0); r = s.getRow(0); c = r.getCell(0); assertEquals("hello world", c.getRichStringCellValue().toString()); - wbBack.close(); + wb3.close(); } /** @@ -528,10 +560,13 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { b1.toByteArray(), b2.toByteArray(), b3.toByteArray() }) { ByteArrayInputStream bais = new ByteArrayInputStream(data); - wb = new XSSFWorkbook(bais); - assertEquals(3, wb.getNumberOfSheets()); - assertEquals(10, wb.getStylesSource().getNumCellStyles()); + XSSFWorkbook wb2 = new XSSFWorkbook(bais); + assertEquals(3, wb2.getNumberOfSheets()); + assertEquals(10, wb2.getStylesSource().getNumCellStyles()); + wb2.close(); } + + wb.close(); } /** @@ -540,14 +575,14 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { */ @Test public void bug49966() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples + XSSFWorkbook wb1 = XSSFTestDataSamples .openSampleWorkbook("shared_formulas.xlsx"); - XSSFSheet sheet = wb.getSheetAt(0); + XSSFSheet sheet = wb1.getSheetAt(0); - Workbook wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb); + XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); // CalcChain has lots of entries - CalculationChain cc = wb.getCalculationChain(); + CalculationChain cc = wb1.getCalculationChain(); assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR()); assertEquals("A3", cc.getCTCalcChain().getCArray(1).getR()); assertEquals("A4", cc.getCTCalcChain().getCArray(2).getR()); @@ -556,9 +591,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("A7", cc.getCTCalcChain().getCArray(5).getR()); assertEquals("A8", cc.getCTCalcChain().getCArray(6).getR()); assertEquals(40, cc.getCTCalcChain().sizeOfCArray()); - wbRead.close(); - wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb); + XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); // Try various ways of changing the formulas // If it stays a formula, chain entry should remain @@ -566,35 +600,33 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay sheet.getRow(2).getCell(0).setCellFormula(null); // go sheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_FORMULA); // stay - wbRead.close(); - wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb); + XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); + sheet.getRow(4).getCell(0).setCellType(Cell.CELL_TYPE_STRING); // go - wbRead.close(); - wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb); + XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); validateCells(sheet); sheet.getRow(5).removeCell(sheet.getRow(5).getCell(0)); // go validateCells(sheet); - wbRead.close(); - wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb); + XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); sheet.getRow(6).getCell(0).setCellType(Cell.CELL_TYPE_BLANK); // go - wbRead.close(); - wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb); + XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); + sheet.getRow(7).getCell(0).setCellValue((String) null); // go - wbRead.close(); - wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb); + XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); // Save and check - wb = XSSFTestDataSamples.writeOutAndReadBack(wb); + XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1); + wb1.close(); assertEquals(35, cc.getCTCalcChain().sizeOfCArray()); - cc = wb.getCalculationChain(); + cc = wb2.getCalculationChain(); assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR()); assertEquals("A4", cc.getCTCalcChain().getCArray(1).getR()); assertEquals("A9", cc.getCTCalcChain().getCArray(2).getR()); - wbRead.close(); + wb2.close(); } @Test @@ -606,6 +638,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { validateCells(sheet); sheet.getRow(5).removeCell(sheet.getRow(5).getCell(0)); // go validateCells(sheet); + + wb.close(); } private void validateCells(XSSFSheet sheet) { @@ -628,6 +662,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { } } } + wb.close(); } /** @@ -653,6 +688,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { formulaEvaluator.evaluateFormulaCell(b3); assertEquals("B1+B2", b3.getCellFormula()); // The newline is lost for shared formulas assertEquals(3.0, b3.getNumericCellValue(), 0); + + wb.close(); } /** @@ -660,8 +697,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { */ @Test public void bug50795() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50795.xlsx"); - XSSFSheet sheet = wb.getSheetAt(0); + XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("50795.xlsx"); + XSSFSheet sheet = wb1.getSheetAt(0); XSSFRow row = sheet.getRow(0); XSSFCell cellWith = row.getCell(0); @@ -676,8 +713,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // Check we can write it out and read it back as-is - wb = XSSFTestDataSamples.writeOutAndReadBack(wb); - sheet = wb.getSheetAt(0); + XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1); + wb1.close(); + + sheet = wb2.getSheetAt(0); row = sheet.getRow(0); cellWith = row.getCell(0); cellWithoutComment = row.getCell(1); @@ -694,8 +733,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // Write out and re-check - wb = XSSFTestDataSamples.writeOutAndReadBack(wb); - sheet = wb.getSheetAt(0); + XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2); + wb2.close(); + + sheet = wb3.getSheetAt(0); row = sheet.getRow(0); // Ensure it swapped over @@ -706,11 +747,13 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { comment = cellWithoutComment.getCellComment(); assertEquals(exp, comment.getString().getString()); + + wb3.close(); } /** * When the cell background colour is set with one of the first - * two columns of the theme colour palette, the colours are + * two columns of the theme colour palette, the colours are * shades of white or black. * For those cases, ensure we don't break on reading the colour */ @@ -739,6 +782,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(64, s.getRow(0).getCell(8).getCellStyle().getFillBackgroundColor()); assertEquals(0, s.getRow(1).getCell(8).getCellStyle().getFillForegroundColor()); assertEquals(64, s.getRow(1).getCell(8).getCellStyle().getFillBackgroundColor()); + + wb.close(); } /** @@ -761,10 +806,11 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(42, cs.getFillForegroundColorColor().getIndexed()); assertNotNull(cs.getFillForegroundColorColor().getRgb()); assertEquals("FFCCFFCC", cs.getFillForegroundColorColor().getARGBHex()); + wb.close(); } /** - * If the border colours are set with themes, then we + * If the border colours are set with themes, then we * should still be able to get colours */ @Test @@ -789,6 +835,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(0, colorS.getTheme()); assertEquals("FFFF0000", colorS.getARGBHex()); + wb.close(); } /** @@ -821,6 +868,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertNotNull( themeC.getRgb() ); assertNotNull( colt.getRgb() ); assertEquals( themeC.getARGBHex(), colt.getARGBHex() ); // The same colour + wb.close(); } /** @@ -832,20 +880,20 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { String text = "Use \n with word wrap on to create a new line.\n" + "This line finishes with two trailing spaces. "; - XSSFWorkbook wb = new XSSFWorkbook(); - XSSFSheet sheet = wb.createSheet(); + XSSFWorkbook wb1 = new XSSFWorkbook(); + XSSFSheet sheet = wb1.createSheet(); - Font font1 = wb.createFont(); + Font font1 = wb1.createFont(); font1.setColor((short) 20); - Font font2 = wb.createFont(); + Font font2 = wb1.createFont(); font2.setColor(Font.COLOR_RED); - Font font3 = wb.getFontAt((short)0); + Font font3 = wb1.getFontAt((short)0); XSSFRow row = sheet.createRow(2); XSSFCell cell = row.createCell(2); XSSFRichTextString richTextString = - wb.getCreationHelper().createRichTextString(text); + wb1.getCreationHelper().createRichTextString(text); // Check the text has the newline assertEquals(text, richTextString.getString()); @@ -856,7 +904,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { cell.setCellValue(richTextString); // To enable newlines you need set a cell styles with wrap=true - CellStyle cs = wb.createCellStyle(); + CellStyle cs = wb1.createCellStyle(); cs.setWrapText(true); cell.setCellStyle(cs); @@ -864,9 +912,9 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(text, cell.getStringCellValue()); // Save the file and re-read it - XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb); - wb.close(); - sheet = wbBack.getSheetAt(0); + XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1); + wb1.close(); + sheet = wb2.getSheetAt(0); row = sheet.getRow(2); cell = row.getCell(2); assertEquals(text, cell.getStringCellValue()); @@ -883,13 +931,15 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("line.\n", r3.substring(r3.length()-6)); // Save and re-check - wbBack = XSSFTestDataSamples.writeOutAndReadBack(wbBack); - sheet = wbBack.getSheetAt(0); + XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2); + wb2.close(); + + sheet = wb3.getSheetAt(0); row = sheet.getRow(2); cell = row.getCell(2); assertEquals(text, cell.getStringCellValue()); - wbBack.close(); - + wb3.close(); + // FileOutputStream out = new FileOutputStream("/tmp/test48877.xlsx"); // wb.write(out); // out.close(); @@ -900,12 +950,12 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { */ @Test public void bug50867() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50867_with_table.xlsx"); - assertEquals(3, wb.getNumberOfSheets()); + XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("50867_with_table.xlsx"); + assertEquals(3, wb1.getNumberOfSheets()); - XSSFSheet s1 = wb.getSheetAt(0); - XSSFSheet s2 = wb.getSheetAt(1); - XSSFSheet s3 = wb.getSheetAt(2); + XSSFSheet s1 = wb1.getSheetAt(0); + XSSFSheet s2 = wb1.getSheetAt(1); + XSSFSheet s3 = wb1.getSheetAt(2); assertEquals(1, s1.getTables().size()); assertEquals(0, s2.getTables().size()); assertEquals(0, s3.getTables().size()); @@ -916,8 +966,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("A1:C3", t.getCTTable().getRef()); // Add a sheet and re-order - XSSFSheet s4 = wb.createSheet("NewSheet"); - wb.setSheetOrder(s4.getSheetName(), 0); + XSSFSheet s4 = wb1.createSheet("NewSheet"); + wb1.setSheetOrder(s4.getSheetName(), 0); // Check on tables assertEquals(1, s1.getTables().size()); @@ -926,21 +976,23 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(0, s4.getTables().size()); // Refetch to get the new order - s1 = wb.getSheetAt(0); - s2 = wb.getSheetAt(1); - s3 = wb.getSheetAt(2); - s4 = wb.getSheetAt(3); + s1 = wb1.getSheetAt(0); + s2 = wb1.getSheetAt(1); + s3 = wb1.getSheetAt(2); + s4 = wb1.getSheetAt(3); assertEquals(0, s1.getTables().size()); assertEquals(1, s2.getTables().size()); assertEquals(0, s3.getTables().size()); assertEquals(0, s4.getTables().size()); // Save and re-load - wb = XSSFTestDataSamples.writeOutAndReadBack(wb); - s1 = wb.getSheetAt(0); - s2 = wb.getSheetAt(1); - s3 = wb.getSheetAt(2); - s4 = wb.getSheetAt(3); + XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1); + wb1.close(); + + s1 = wb2.getSheetAt(0); + s2 = wb2.getSheetAt(1); + s3 = wb2.getSheetAt(2); + s4 = wb2.getSheetAt(3); assertEquals(0, s1.getTables().size()); assertEquals(1, s2.getTables().size()); assertEquals(0, s3.getTables().size()); @@ -960,11 +1012,12 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { t.setName("New 3"); t.setDisplayName("New 3"); - wb = XSSFTestDataSamples.writeOutAndReadBack(wb); - s1 = wb.getSheetAt(0); - s2 = wb.getSheetAt(1); - s3 = wb.getSheetAt(2); - s4 = wb.getSheetAt(3); + XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2); + wb2.close(); + s1 = wb3.getSheetAt(0); + s2 = wb3.getSheetAt(1); + s3 = wb3.getSheetAt(2); + s4 = wb3.getSheetAt(3); assertEquals(0, s1.getTables().size()); assertEquals(2, s2.getTables().size()); assertEquals(1, s3.getTables().size()); @@ -990,25 +1043,26 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(0, s4.getRelations().size()); assertEquals( - XSSFRelation.PRINTER_SETTINGS.getContentType(), + XSSFRelation.PRINTER_SETTINGS.getContentType(), s2.getRelations().get(0).getPackagePart().getContentType() ); assertEquals( - XSSFRelation.TABLE.getContentType(), + XSSFRelation.TABLE.getContentType(), s2.getRelations().get(1).getPackagePart().getContentType() ); assertEquals( - XSSFRelation.TABLE.getContentType(), + XSSFRelation.TABLE.getContentType(), s2.getRelations().get(2).getPackagePart().getContentType() ); assertEquals( - XSSFRelation.TABLE.getContentType(), + XSSFRelation.TABLE.getContentType(), s3.getRelations().get(0).getPackagePart().getContentType() ); assertEquals( "/xl/tables/table3.xml", s3.getRelations().get(0).getPackagePart().getPartName().toString() ); + wb3.close(); } /** @@ -1022,7 +1076,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFWorkbook wb2 = new XSSFWorkbook(); // No print settings before repeating - XSSFSheet s1 = wb1.createSheet(); + XSSFSheet s1 = wb1.createSheet(); assertEquals(false, s1.getCTWorksheet().isSetPageSetup()); assertEquals(true, s1.getCTWorksheet().isSetPageMargins()); @@ -1122,7 +1176,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(blueStyle.getIndex(), cols.getColArray(1).getStyle()); - // Save, re-load and re-check + // Save, re-load and re-check XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb); wb.close(); s = wbBack.getSheetAt(0); @@ -1143,23 +1197,25 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { @Test public void bug46662() throws Exception { // New file - XSSFWorkbook wb = new XSSFWorkbook(); - XSSFTestDataSamples.writeOutAndReadBack(wb); - XSSFTestDataSamples.writeOutAndReadBack(wb); - XSSFTestDataSamples.writeOutAndReadBack(wb); + XSSFWorkbook wb1 = new XSSFWorkbook(); + XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); + XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); + XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); + wb1.close(); // Simple file - wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx"); - XSSFTestDataSamples.writeOutAndReadBack(wb); - XSSFTestDataSamples.writeOutAndReadBack(wb); - XSSFTestDataSamples.writeOutAndReadBack(wb); + XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx"); + XSSFTestDataSamples.writeOutAndReadBack(wb2).close(); + XSSFTestDataSamples.writeOutAndReadBack(wb2).close(); + XSSFTestDataSamples.writeOutAndReadBack(wb2).close(); + wb2.close(); // Complex file // TODO } /** - * Colours and styles when the list has gaps in it + * Colours and styles when the list has gaps in it */ @Test public void bug51222() throws Exception { @@ -1198,6 +1254,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // TODO fix // assertEquals("FFEEECE1", cA4_EEECE1.getCellStyle().getFillForegroundXSSFColor().getARGBHex()); // assertEquals("FF1F497D", cA5_1F497D.getCellStyle().getFillForegroundXSSFColor().getARGBHex()); + + wb.close(); } @Test @@ -1211,6 +1269,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(1, rels1.size()); assertEquals(rels0.get(0).getPackageRelationship(), rels1.get(0).getPackageRelationship()); + wb.close(); } /** @@ -1218,10 +1277,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { * comments (so /xl/comments1.xml is taken) */ @Test - public void bug51850() { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51850.xlsx"); - XSSFSheet sh1 = wb.getSheetAt(0); - XSSFSheet sh2 = wb.getSheetAt(1); + public void bug51850() throws Exception { + XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("51850.xlsx"); + XSSFSheet sh1 = wb1.getSheetAt(0); + XSSFSheet sh2 = wb1.getSheetAt(1); // Sheet 2 has comments assertNotNull(sh2.getCommentsTable(false)); @@ -1231,7 +1290,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertNull(sh1.getCommentsTable(false)); // Try to add comments to Sheet 1 - CreationHelper factory = wb.getCreationHelper(); + CreationHelper factory = wb1.getCreationHelper(); Drawing drawing = sh1.createDrawingPatriarch(); ClientAnchor anchor = factory.createClientAnchor(); @@ -1265,9 +1324,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // Save and re-load - wb = XSSFTestDataSamples.writeOutAndReadBack(wb); - sh1 = wb.getSheetAt(0); - sh2 = wb.getSheetAt(1); + XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1); + wb1.close(); + sh1 = wb2.getSheetAt(0); + sh2 = wb2.getSheetAt(1); // Check the comments assertNotNull(sh2.getCommentsTable(false)); @@ -1275,6 +1335,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertNotNull(sh1.getCommentsTable(false)); assertEquals(2, sh1.getCommentsTable(false).getNumberOfComments()); + wb2.close(); } /** @@ -1295,6 +1356,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(0, ref.getFirstCell().getCol()); assertEquals(1, ref.getLastCell().getRow()); assertEquals(0, ref.getLastCell().getCol()); + wb.close(); } /** @@ -1326,13 +1388,14 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(20.0, c1.getNumericCellValue(), 0); assertEquals(20.0, c2.getNumericCellValue(), 0); + wb.close(); } /** * Bugzilla 51710: problems reading shared formuals from .xlsx */ @Test - public void bug51710() { + public void bug51710() throws Exception { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("51710.xlsx"); final String[] columns = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N"}; @@ -1357,34 +1420,36 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { } } + wb.close(); } /** * Bug 53101: */ @Test - public void bug5301(){ - Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("53101.xlsx"); + public void bug5301() throws Exception { + Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53101.xlsx"); FormulaEvaluator evaluator = - workbook.getCreationHelper().createFormulaEvaluator(); + wb.getCreationHelper().createFormulaEvaluator(); // A1: SUM(B1: IZ1) double a1Value = - evaluator.evaluate(workbook.getSheetAt(0).getRow(0).getCell(0)).getNumberValue(); + evaluator.evaluate(wb.getSheetAt(0).getRow(0).getCell(0)).getNumberValue(); // Assert assertEquals(259.0, a1Value, 0.0); // KY: SUM(B1: IZ1) /*double ky1Value =*/ - evaluator.evaluate(workbook.getSheetAt(0).getRow(0).getCell(310)).getNumberValue(); + evaluator.evaluate(wb.getSheetAt(0).getRow(0).getCell(310)).getNumberValue(); // Assert assertEquals(259.0, a1Value, 0.0); + wb.close(); } @Test - public void bug54436(){ - Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("54436.xlsx"); + public void bug54436() throws Exception { + Workbook wb = XSSFTestDataSamples.openSampleWorkbook("54436.xlsx"); if(!WorkbookEvaluator.getSupportedFunctionNames().contains("GETPIVOTDATA")){ Function func = new Function() { @Override @@ -1395,9 +1460,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { WorkbookEvaluator.registerFunction("GETPIVOTDATA", func); } - workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); + wb.getCreationHelper().createFormulaEvaluator().evaluateAll(); + wb.close(); } - + /** * Password Protected .xlsx files should give a helpful * error message when called via WorkbookFactory with no password @@ -1406,10 +1472,15 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { public void bug55692_poifs() throws Exception { // Via a POIFSFileSystem POIFSFileSystem fsP = new POIFSFileSystem( - POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx")); - WorkbookFactory.create(fsP); + POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx")); + try { + WorkbookFactory.create(fsP); + } finally { + fsP.close(); + } } - + + @Test public void bug55692_stream() throws Exception { // Directly on a Stream, will go via NPOIFS and spot it's // actually a .xlsx file encrypted with the default password, and open @@ -1417,8 +1488,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx")); assertNotNull(wb); assertEquals(3, wb.getNumberOfSheets()); + wb.close(); } - + + @Test public void bug55692_npoifs() throws Exception { // Via a NPOIFSFileSystem, will spot it's actually a .xlsx file // encrypted with the default password, and open @@ -1427,16 +1500,19 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { Workbook wb = WorkbookFactory.create(fsNP); assertNotNull(wb); assertEquals(3, wb.getNumberOfSheets()); + wb.close(); + fsNP.close(); } @Test - public void bug53282() { + public void bug53282() throws Exception { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53282b.xlsx"); Cell c = wb.getSheetAt(0).getRow(1).getCell(0); - assertEquals("#@_#", c.getStringCellValue()); - assertEquals("http://invalid.uri", c.getHyperlink().getAddress()); + assertEquals("#@_#", c.getStringCellValue()); + assertEquals("http://invalid.uri", c.getHyperlink().getAddress()); + wb.close(); } - + /** * Was giving NullPointerException * at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead @@ -1446,19 +1522,22 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { public void bug56278() throws Exception { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56278.xlsx"); assertEquals(0, wb.getSheetIndex("Market Rates")); - + // Save and re-check Workbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb); assertEquals(0, nwb.getSheetIndex("Market Rates")); + nwb.close(); + wb.close(); } @Test - public void bug56315() { + public void bug56315() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56315.xlsx"); Cell c = wb.getSheetAt(0).getRow(1).getCell(0); CellValue cv = wb.getCreationHelper().createFormulaEvaluator().evaluate(c); double rounded = cv.getNumberValue(); assertEquals(0.1, rounded, 0.0); + wb.close(); } @Test @@ -1469,7 +1548,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFCell cell = row.createCell(0); cell.setCellValue("Hi"); sheet.setRepeatingRows(new CellRangeAddress(0, 0, 0, 0)); - + // small hack to try to make this test stable, previously it failed whenever the two written ZIP files had different file-creation // dates stored. // We try to do a loop until the current second changes in order to avoid problems with some date information that is written to the ZIP and thus @@ -1478,42 +1557,50 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { while(System.currentTimeMillis()/1000 == start) { Thread.sleep(10); } - + ByteArrayOutputStream bos = new ByteArrayOutputStream(8096); wb.write(bos); byte firstSave[] = bos.toByteArray(); bos.reset(); wb.write(bos); byte secondSave[] = bos.toByteArray(); - + /*OutputStream stream = new FileOutputStream("C:\\temp\\poi.xlsx"); try { wb.write(stream); } finally { stream.close(); }*/ - - assertArrayEquals("Had: \n" + Arrays.toString(firstSave) + " and \n" + Arrays.toString(secondSave), + + assertArrayEquals("Had: \n" + Arrays.toString(firstSave) + " and \n" + Arrays.toString(secondSave), firstSave, secondSave); - + wb.close(); } - + /** * ISO-8601 style cell formats with a T in them, eg * cell format of "yyyy-MM-ddTHH:mm:ss" */ @Test public void bug54034() throws IOException { - Workbook wb = XSSFTestDataSamples.openSampleWorkbook("54034.xlsx"); - Sheet sheet = wb.getSheet("Sheet1"); - Row row = sheet.getRow(1); - Cell cell = row.getCell(2); - assertTrue(DateUtil.isCellDateFormatted(cell)); - - DataFormatter fmt = new DataFormatter(); - assertEquals("yyyy\\-mm\\-dd\\Thh:mm", cell.getCellStyle().getDataFormatString()); - assertEquals("2012-08-08T22:59", fmt.formatCellValue(cell)); + TimeZone tz = LocaleUtil.getUserTimeZone(); + LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET")); + try { + Workbook wb = XSSFTestDataSamples.openSampleWorkbook("54034.xlsx"); + Sheet sheet = wb.getSheet("Sheet1"); + Row row = sheet.getRow(1); + Cell cell = row.getCell(2); + assertTrue(DateUtil.isCellDateFormatted(cell)); + + DataFormatter fmt = new DataFormatter(); + assertEquals("yyyy\\-mm\\-dd\\Thh:mm", cell.getCellStyle().getDataFormatString()); + assertEquals("2012-08-08T22:59", fmt.formatCellValue(cell)); + + wb.close(); + } finally { + LocaleUtil.setUserTimeZone(tz); + } } @@ -1522,6 +1609,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xlsx"); File xlsOutput = TempFile.createTempFile("testBug53798", ".xlsx"); bug53798Work(wb, xlsOutput); + wb.close(); } @Ignore("Shifting rows is not yet implemented in SXSSFSheet") @@ -1529,7 +1617,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { public void testBug53798XLSXStream() throws IOException { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xlsx"); File xlsOutput = TempFile.createTempFile("testBug53798", ".xlsx"); - bug53798Work(new SXSSFWorkbook(wb), xlsOutput); + SXSSFWorkbook wb2 = new SXSSFWorkbook(wb); + bug53798Work(wb2, xlsOutput); + wb2.close(); + wb.close(); } @Test @@ -1537,15 +1628,16 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { Workbook wb = HSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xls"); File xlsOutput = TempFile.createTempFile("testBug53798", ".xls"); bug53798Work(wb, xlsOutput); + wb.close(); } - + /** * SUMIF was throwing a NPE on some formulas */ @Test public void testBug56420SumIfNPE() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56420.xlsx"); - + FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); Sheet sheet = wb.getSheetAt(0); @@ -1554,6 +1646,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("SUMIF($A$1:$A$4,A3,$B$1:$B$4)", c.getCellFormula()); Cell eval = evaluator.evaluateInCell(c); assertEquals(0.0, eval.getNumericCellValue(), 0.0001); + wb.close(); } private void bug53798Work(Workbook wb, File xlsOutput) throws IOException { @@ -1608,62 +1701,64 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { @Test public void bug56702() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56702.xlsx"); - + Sheet sheet = wb.getSheetAt(0); // Get wrong cell by row 8 & column 7 Cell cell = sheet.getRow(8).getCell(7); assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - + // Check the value - will be zero as it is <c><v/></c> assertEquals(0.0, cell.getNumericCellValue(), 0.001); - + // Try to format DataFormatter formatter = new DataFormatter(); formatter.formatCellValue(cell); - + // Check the formatting assertEquals("0", formatter.formatCellValue(cell)); + wb.close(); } - + /** * Formulas which reference named ranges, either in other * sheets, or workbook scoped but in other workbooks. * Used to fail with with errors like * org.apache.poi.ss.formula.FormulaParseException: Cell reference expected after sheet name at index 9 - * org.apache.poi.ss.formula.FormulaParseException: Parse error near char 0 '[' in specified formula '[0]!NR_Global_B2'. Expected number, string, or defined name + * org.apache.poi.ss.formula.FormulaParseException: Parse error near char 0 '[' in specified formula '[0]!NR_Global_B2'. Expected number, string, or defined name */ @Test public void bug56737() throws IOException { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56737.xlsx"); - + // Check the named range definitions Name nSheetScope = wb.getName("NR_To_A1"); Name nWBScope = wb.getName("NR_Global_B2"); assertNotNull(nSheetScope); assertNotNull(nWBScope); - + assertEquals("Defines!$A$1", nSheetScope.getRefersToFormula()); assertEquals("Defines!$B$2", nWBScope.getRefersToFormula()); - + // Check the different kinds of formulas Sheet s = wb.getSheetAt(0); Cell cRefSName = s.getRow(1).getCell(3); Cell cRefWName = s.getRow(2).getCell(3); - + assertEquals("Defines!NR_To_A1", cRefSName.getCellFormula()); // Note the formula, as stored in the file, has the external name index not filename // TODO Provide a way to get the one with the filename assertEquals("[0]!NR_Global_B2", cRefWName.getCellFormula()); - + // Try to evaluate them FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator(); assertEquals("Test A1", eval.evaluate(cRefSName).getStringValue()); assertEquals(142, (int)eval.evaluate(cRefWName).getNumberValue()); - + // Try to evaluate everything eval.evaluateAll(); + wb.close(); } private void saveAndReloadReport(Workbook wb, File outFile) throws IOException { @@ -1704,40 +1799,45 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { is.close(); } } - + @Test - public void testBug56688_1() { + public void testBug56688_1() throws Exception { XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_1.xlsx"); checkValue(excel, "-1.0"); /* Not 0.0 because POI sees date "0" minus one month as invalid date, which is -1! */ + excel.close(); } - + @Test - public void testBug56688_2() { + public void testBug56688_2() throws Exception { XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_2.xlsx"); checkValue(excel, "#VALUE!"); + excel.close(); } - + @Test - public void testBug56688_3() { + public void testBug56688_3() throws Exception { XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_3.xlsx"); checkValue(excel, "#VALUE!"); + excel.close(); } - + @Test - public void testBug56688_4() { + public void testBug56688_4() throws Exception { XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_4.xlsx"); - - Calendar calendar = Calendar.getInstance(); + + Calendar calendar = LocaleUtil.getLocaleCalendar(); calendar.add(Calendar.MONTH, 2); double excelDate = DateUtil.getExcelDate(calendar.getTime()); NumberEval eval = new NumberEval(Math.floor(excelDate)); checkValue(excel, eval.getStringValue() + ".0"); + + excel.close(); } - + /** * New hyperlink with no initial cell reference, still need * to be able to change it - * @throws IOException + * @throws IOException */ @Test public void testBug56527() throws IOException { @@ -1745,7 +1845,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFSheet sheet = wb.createSheet(); XSSFCreationHelper creationHelper = wb.getCreationHelper(); XSSFHyperlink hyperlink; - + // Try with a cell reference hyperlink = creationHelper.createHyperlink(Hyperlink.LINK_URL); sheet.addHyperlink(hyperlink); @@ -1755,56 +1855,57 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(1, hyperlink.getFirstColumn()); assertEquals(3, hyperlink.getLastRow()); assertEquals(1, hyperlink.getLastColumn()); - + // Try with explicit rows / columns hyperlink = creationHelper.createHyperlink(Hyperlink.LINK_URL); sheet.addHyperlink(hyperlink); hyperlink.setAddress("http://myurl"); hyperlink.setFirstRow(5); hyperlink.setFirstColumn(3); - + assertEquals(5, hyperlink.getFirstRow()); assertEquals(3, hyperlink.getFirstColumn()); assertEquals(5, hyperlink.getLastRow()); assertEquals(3, hyperlink.getLastColumn()); wb.close(); } - + /** - * Shifting rows with a formula that references a + * Shifting rows with a formula that references a * function in another file */ @Test public void bug56502() throws Exception { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56502.xlsx"); Sheet sheet = wb.getSheetAt(0); - + Cell cFunc = sheet.getRow(3).getCell(0); assertEquals("[1]!LUCANET(\"Ist\")", cFunc.getCellFormula()); Cell cRef = sheet.getRow(3).createCell(1); cRef.setCellFormula("A3"); - + // Shift it down one row sheet.shiftRows(1, sheet.getLastRowNum(), 1); - + // Check the new formulas: Function won't change, Reference will cFunc = sheet.getRow(4).getCell(0); assertEquals("[1]!LUCANET(\"Ist\")", cFunc.getCellFormula()); cRef = sheet.getRow(4).getCell(1); assertEquals("A4", cRef.getCellFormula()); + wb.close(); } - + @Test public void bug54764() throws Exception { OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("54764.xlsx"); - + // Check the core properties - will be found but empty, due // to the expansion being too much to be considered valid POIXMLProperties props = new POIXMLProperties(pkg); assertEquals(null, props.getCoreProperties().getTitle()); assertEquals(null, props.getCoreProperties().getSubject()); assertEquals(null, props.getCoreProperties().getDescription()); - + // Now check the spreadsheet itself try { new XSSFWorkbook(pkg).close(); @@ -1812,20 +1913,22 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { } catch(POIXMLException e) { // Expected } - + pkg.close(); + // Try with one with the entities in the Content Types try { - XSSFTestDataSamples.openSamplePackage("54764-2.xlsx"); + XSSFTestDataSamples.openSamplePackage("54764-2.xlsx").close(); fail("Should fail as too much expansion occurs"); } catch(Exception e) { // Expected } - + // Check we can still parse valid files after all that Workbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx"); assertEquals(3, wb.getNumberOfSheets()); + wb.close(); } - + /** * CTDefinedNamesImpl should be included in the smaller * poi-ooxml-schemas jar @@ -1840,8 +1943,9 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertNotNull(defName.getStringValue()); } assertEquals("TestDefinedName", definedNameList.get(0).getName()); + wb.close(); } - + /** * .xlsb files are not supported, but we should generate a helpful * error message if given one @@ -1850,7 +1954,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { public void bug56800_xlsb() throws Exception { // Can be opened at the OPC level OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("Simple.xlsb"); - + // XSSF Workbook gives helpful error try { new XSSFWorkbook(pkg).close(); @@ -1858,35 +1962,37 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { } catch (XLSBUnsupportedException e) { // Good, detected and warned } - + // Workbook Factory gives helpful error on package try { - WorkbookFactory.create(pkg); + WorkbookFactory.create(pkg).close(); fail(".xlsb files not supported"); } catch (XLSBUnsupportedException e) { // Good, detected and warned } - + // Workbook Factory gives helpful error on file File xlsbFile = HSSFTestDataSamples.getSampleFile("Simple.xlsb"); try { - WorkbookFactory.create(xlsbFile); + WorkbookFactory.create(xlsbFile).close(); fail(".xlsb files not supported"); } catch (XLSBUnsupportedException e) { // Good, detected and warned } + + pkg.close(); } private void checkValue(XSSFWorkbook excel, String expect) { XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(excel); evaluator.evaluateAll(); - + XSSFCell cell = excel.getSheetAt(0).getRow(1).getCell(1); CellValue value = evaluator.evaluate(cell); - + assertEquals(expect, value.formatAsString()); } - + @Test public void testBug57196() throws IOException { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57196.xlsx"); @@ -1899,9 +2005,9 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // fileOutput.close(); wb.close(); } - + @Test - public void test57196_Detail() { + public void test57196_Detail() throws Exception { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet("Sheet1"); XSSFRow row = sheet.createRow(0); @@ -1911,10 +2017,11 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { CellValue cv = fe.evaluate(cell); assertNotNull(cv); - } - + wb.close(); + } + @Test - public void test57196_Detail2() { + public void test57196_Detail2() throws Exception { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet("Sheet1"); XSSFRow row = sheet.createRow(0); @@ -1924,10 +2031,11 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { CellValue cv = fe.evaluate(cell); assertNotNull(cv); - } + wb.close(); + } @Test - public void test57196_WorkbookEvaluator() { + public void test57196_WorkbookEvaluator() throws Exception { String previousLogger = System.getProperty("org.apache.poi.util.POILogger"); //System.setProperty("org.apache.poi.util.POILogger", "org.apache.poi.util.SystemOutLogger"); //System.setProperty("poi.log.level", "3"); @@ -1944,15 +2052,15 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // simple formula worked cell.setCellFormula("DEC2HEX(O2+D2)"); - + WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null); workbookEvaluator.setDebugEvaluationOutputForNextEval(true); workbookEvaluator.evaluate(new XSSFEvaluationCell(cell)); - + // this already failed! Hex2Dec did not correctly handle RefEval cell.setCellFormula("HEX2DEC(O8)"); workbookEvaluator.clearAllCachedResultValues(); - + workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null); workbookEvaluator.setDebugEvaluationOutputForNextEval(true); workbookEvaluator.evaluate(new XSSFEvaluationCell(cell)); @@ -1960,7 +2068,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // slightly more complex one failed cell.setCellFormula("HEX2DEC(O8)-O2+D2"); workbookEvaluator.clearAllCachedResultValues(); - + workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null); workbookEvaluator.setDebugEvaluationOutputForNextEval(true); workbookEvaluator.evaluate(new XSSFEvaluationCell(cell)); @@ -1975,7 +2083,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // what other similar functions cell.setCellFormula("DEC2BIN(O8)-O2+D2"); workbookEvaluator.clearAllCachedResultValues(); - + workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null); workbookEvaluator.setDebugEvaluationOutputForNextEval(true); workbookEvaluator.evaluate(new XSSFEvaluationCell(cell)); @@ -1983,7 +2091,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // what other similar functions cell.setCellFormula("DEC2BIN(A1)"); workbookEvaluator.clearAllCachedResultValues(); - + workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null); workbookEvaluator.setDebugEvaluationOutputForNextEval(true); workbookEvaluator.evaluate(new XSSFEvaluationCell(cell)); @@ -1991,10 +2099,12 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // what other similar functions cell.setCellFormula("BIN2DEC(B1)"); workbookEvaluator.clearAllCachedResultValues(); - + workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null); workbookEvaluator.setDebugEvaluationOutputForNextEval(true); workbookEvaluator.evaluate(new XSSFEvaluationCell(cell)); + + wb.close(); } finally { if(previousLogger == null) { System.clearProperty("org.apache.poi.util.POILogger"); @@ -2004,7 +2114,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { System.clearProperty("poi.log.level"); } } - + /** * A .xlsx file with no Shared Strings table should open fine * in read-only mode @@ -2022,17 +2132,17 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFWorkbook wb = new XSSFWorkbook(pkg); assertNotNull(wb.getSharedStringSource()); assertEquals(0, wb.getSharedStringSource().getCount()); - + DataFormatter fmt = new DataFormatter(); XSSFSheet s = wb.getSheetAt(0); assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0))); assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1))); assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0))); - + // Add a text cell s.getRow(0).createCell(3).setCellValue("Testing"); assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3))); - + // Try to write-out and read again, should only work // in read-write mode, not read-only mode try { @@ -2047,7 +2157,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { throw e; } } - + // Check again s = wb.getSheetAt(0); assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0))); @@ -2059,7 +2169,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { } } } - + /** * "Unknown error type: -60" fetching formula error value */ @@ -2068,20 +2178,22 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57535.xlsx"); FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); evaluator.clearAllCachedResultValues(); - + Sheet sheet = wb.getSheet("Sheet1"); Cell cell = sheet.getRow(5).getCell(4); assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals("E4+E5", cell.getCellFormula()); - + CellValue value = evaluator.evaluate(cell); assertEquals(Cell.CELL_TYPE_ERROR, value.getCellType()); assertEquals(-60, value.getErrorValue()); assertEquals("~CIRCULAR~REF~", FormulaError.forInt(value.getErrorValue()).getString()); assertEquals("CIRCULAR_REF", FormulaError.forInt(value.getErrorValue()).toString()); + + wb.close(); } - + @Test public void test57165() throws IOException { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx"); @@ -2090,10 +2202,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { wb.cloneSheet(0); // Throws exception here wb.setSheetName(1, "New Sheet"); //saveWorkbook(wb, fileName); - + XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb); try { - + } finally { wbBack.close(); } @@ -2110,10 +2222,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { wb.createSheet("newsheet"); // Throws exception here wb.setSheetName(1, "New Sheet"); //saveWorkbook(wb, fileName); - + XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb); try { - + } finally { wbBack.close(); } @@ -2208,14 +2320,14 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { wb.close(); } } - + @Test public void test56467() throws IOException { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("picture.xlsx"); try { Sheet orig = wb.getSheetAt(0); assertNotNull(orig); - + Sheet sheet = wb.cloneSheet(0); Drawing drawing = sheet.createDrawingPatriarch(); for (XSSFShape shape : ((XSSFDrawing) drawing).getShapes()) { @@ -2224,7 +2336,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertNotNull(pictureData); } } - + // OutputStream out = new FileOutputStream("/tmp/56467.xls"); // try { // wb.write(out); @@ -2235,7 +2347,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { wb.close(); } } - + /** * OOXML-Strict files * Not currently working - namespace mis-match from XMLBeans @@ -2247,11 +2359,14 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(3, wb.getNumberOfSheets()); // TODO Check sheet contents // TODO Check formula evaluation - + XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb); assertEquals(3, wbBack.getNumberOfSheets()); // TODO Re-check sheet contents // TODO Re-check formula evaluation + + wb.close(); + wbBack.close(); } @Test @@ -2261,16 +2376,17 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFRow srcRow = sheet.getRow(0); XSSFCell oldCell = srcRow.getCell(0); XSSFCellStyle cellStyle = oldCell.getCellStyle(); - + checkStyle(cellStyle); - + // StylesTable table = xlsToAppendWorkbook.getStylesSource(); // List<XSSFCellFill> fills = table.getFills(); // System.out.println("Having " + fills.size() + " fills"); // for(XSSFCellFill fill : fills) { // System.out.println("Fill: " + fill.getFillBackgroundColor() + "/" + fill.getFillForegroundColor()); -// } - +// } + xlsToAppendWorkbook.close(); + XSSFWorkbook targetWorkbook = new XSSFWorkbook(); XSSFSheet newSheet = targetWorkbook.createSheet(sheet.getSheetName()); XSSFRow destRow = newSheet.createRow(0); @@ -2289,19 +2405,22 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // } finally { // os.close(); // } - + XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(targetWorkbook); XSSFCellStyle styleBack = wbBack.getSheetAt(0).getRow(0).getCell(0).getCellStyle(); checkStyle(styleBack); + + targetWorkbook.close(); + wbBack.close(); } /** - * Paragraph with property BuFont but none of the properties + * Paragraph with property BuFont but none of the properties * BuNone, BuChar, and BuAutoNum, used to trigger a NPE * Excel treats this as not-bulleted, so now do we */ @Test - public void testBug57826() { + public void testBug57826() throws Exception { XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("57826.xlsx"); assertTrue("no sheets in workbook", workbook.getNumberOfSheets() >= 1); @@ -2321,6 +2440,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // No bulleting info included assertEquals("test ok", text); + + workbook.close(); } private void checkStyle(XSSFCellStyle cellStyle) { @@ -2352,7 +2473,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("ISERROR(CSN!A1)", c.getCellFormula()); c = s.getRow(1).getCell(1); assertEquals("ISERROR(B2)", c.getCellFormula()); - + wb.close(); } @@ -2383,15 +2504,15 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { File tmp = TempFile.createTempFile("poi-test", ".bug57880"); OutputStream fos = new FileOutputStream(tmp); try { - wb.write(fos); + wb.write(fos); } finally { - fos.close(); + fos.close(); } - + wb.close(); fmt = null; /*s = null;*/ wb = null; // System.gc(); - + wb = new XSSFWorkbook(tmp); fmt = wb.getCreationHelper().createDataFormat(); // s = wb.getSheetAt(0); @@ -2427,7 +2548,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { data.put("3", new Object[] {1, "Lokesh", "Gupta"}); data.put("4", new Object[] {4, "John", "Adwards"}); data.put("5", new Object[] {2, "Brian", "Schultz"}); - + Set<String> keyset = data.keySet(); int rownum = 1; for (String key : keyset) @@ -2475,14 +2596,14 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertFalse(calc.getR().equals("A5")); assertFalse(calc.getR().equals("A6")); } - + /*FileOutputStream out = new FileOutputStream(new File("C:\\temp\\56574.xlsx")); try { wb.write(out); } finally { out.close(); }*/ - + Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb); Sheet sheetBack = wbBack.getSheet("Func"); assertNotNull(sheetBack); @@ -2497,10 +2618,11 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertFalse(calc.getR().equals("A5")); assertFalse(calc.getR().equals("A6")); } - + + wbBack.close(); wb.close(); } - + /** * Excel 2007 generated Macro-Enabled .xlsm file */ @@ -2508,8 +2630,9 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { public void bug57181() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57181.xlsm"); assertEquals(9, wb.getNumberOfSheets()); + wb.close(); } - + @Test public void bug52111() throws Exception { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("Intersection-52111-xssf.xlsx"); @@ -2517,8 +2640,9 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertFormula(wb, s.getRow(2).getCell(0), "(C2:D3 D3:E4)", "4.0"); assertFormula(wb, s.getRow(6).getCell(0), "Tabelle2!E:E Tabelle2!11:11", "5.0"); assertFormula(wb, s.getRow(8).getCell(0), "Tabelle2!E:F Tabelle2!11:12", null); + wb.close(); } - + private void assertFormula(Workbook wb, Cell intF, String expectedFormula, String expectedResultOrNull) { assertEquals(Cell.CELL_TYPE_FORMULA, intF.getCellType()); if (null == expectedResultOrNull) { @@ -2528,7 +2652,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { else { assertEquals(Cell.CELL_TYPE_NUMERIC, intF.getCachedFormulaResultType()); } - + assertEquals(expectedFormula, intF.getCellFormula()); // Check we can evaluate it correctly @@ -2537,31 +2661,32 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { } @Test - public void test48962() { + public void test48962() throws Exception { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("48962.xlsx"); Sheet sh = wb.getSheetAt(0); Row row = sh.getRow(1); Cell cell = row.getCell(0); - + CellStyle style = cell.getCellStyle(); assertNotNull(style); - + // color index assertEquals(64, style.getFillBackgroundColor()); XSSFColor color = ((XSSFCellStyle)style).getFillBackgroundXSSFColor(); assertNotNull(color); - + // indexed color assertEquals(64, color.getIndexed()); assertEquals(64, color.getIndex()); - + // not an RGB color assertFalse(color.isRGB()); - assertNull(color.getRGB()); + assertNull(color.getRGB()); + wb.close(); } - + @Test - public void test50755_workday_formula_example() { + public void test50755_workday_formula_example() throws Exception { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("50755_workday_formula_example.xlsx"); Sheet sheet = wb.getSheet("Sheet1"); for(Row aRow : sheet) { @@ -2575,6 +2700,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertNotNull(cell.toString()); } } + wb.close(); } @Test @@ -2582,31 +2708,31 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("51626.xlsx"); assertNotNull(wb); wb.close(); - + InputStream stream = HSSFTestDataSamples.openSampleFileStream("51626.xlsx"); wb = WorkbookFactory.create(stream); stream.close(); wb.close(); - + wb = XSSFTestDataSamples.openSampleWorkbook("51626_contact.xlsx"); assertNotNull(wb); wb.close(); - + stream = HSSFTestDataSamples.openSampleFileStream("51626_contact.xlsx"); wb = WorkbookFactory.create(stream); stream.close(); wb.close(); } - + @Test public void test51451() throws IOException { Workbook wb = new XSSFWorkbook(); Sheet sh = wb.createSheet(); - + Row row = sh.createRow(0); Cell cell = row.createCell(0); cell.setCellValue(239827342); - + CellStyle style = wb.createCellStyle(); //style.setHidden(false); DataFormat excelFormat = wb.createDataFormat(); @@ -2616,7 +2742,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // FileOutputStream out = new FileOutputStream("/tmp/51451.xlsx"); // wb.write(out); // out.close(); - + wb.close(); } @@ -2624,7 +2750,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { public void test53105() throws IOException { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53105.xlsx"); assertNotNull(wb); - + // Act // evaluate SUM('Skye Lookup Input'!A4:XFD4), cells in range each contain "1" @@ -2633,7 +2759,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // Assert assertEquals(16384.0, numericValue, 0.0); - + wb.close(); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index 1d020e9a33..c8a237d51d 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -17,6 +17,11 @@ package org.apache.poi.xssf.usermodel; +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 org.apache.poi.hssf.HSSFITestDataProvider; @@ -34,6 +39,7 @@ import org.apache.poi.xssf.SXSSFITestDataProvider; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.model.SharedStringsTable; +import org.junit.Test; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; @@ -50,6 +56,7 @@ public final class TestXSSFCell extends BaseTestXCell { * Bug 47026: trouble changing cell type when workbook doesn't contain * Shared String Table */ + @Test public void test47026_1() { Workbook source = _testDataProvider.openSampleWorkbook("47026.xlsm"); Sheet sheet = source.getSheetAt(0); @@ -59,6 +66,7 @@ public final class TestXSSFCell extends BaseTestXCell { cell.setCellValue("456"); } + @Test public void test47026_2() { Workbook source = _testDataProvider.openSampleWorkbook("47026.xlsm"); Sheet sheet = source.getSheetAt(0); @@ -75,6 +83,7 @@ public final class TestXSSFCell extends BaseTestXCell { * Some programs, for example, Microsoft Excel Driver for .xlsx insert inline string * instead of using the shared string table. See bug 47206 */ + @Test public void testInlineString() { XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("xlsx-jdbc.xlsx"); XSSFSheet sheet = wb.getSheetAt(0); @@ -99,6 +108,7 @@ public final class TestXSSFCell extends BaseTestXCell { /** * Bug 47278 - xsi:nil attribute for <t> tag caused Excel 2007 to fail to open workbook */ + @Test public void test47278() { XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); @@ -121,6 +131,7 @@ public final class TestXSSFCell extends BaseTestXCell { assertEquals(Cell.CELL_TYPE_BLANK, cell_1.getCellType()); } + @Test public void testFormulaString() throws IOException { XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.createWorkbook(); try { @@ -162,6 +173,7 @@ public final class TestXSSFCell extends BaseTestXCell { /** * Bug 47889: problems when calling XSSFCell.getStringCellValue() on a workbook created in Gnumeric */ + @Test public void test47889() { XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("47889.xlsx"); XSSFSheet sh = wb.getSheetAt(0); @@ -180,13 +192,14 @@ public final class TestXSSFCell extends BaseTestXCell { //try a numeric cell cell = sh.getRow(1).getCell(0); assertEquals(XSSFCell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertEquals(1.0, cell.getNumericCellValue()); + assertEquals(1.0, cell.getNumericCellValue(), 0); assertEquals("1.0", cell.toString()); //Gnumeric produces spreadsheets without styles //make sure we return null for that instead of throwing OutOfBounds assertEquals(null, cell.getCellStyle()); } + @Test public void testMissingRAttribute() { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); @@ -238,6 +251,7 @@ public final class TestXSSFCell extends BaseTestXCell { assertEquals("F1", a6.getReference()); } + @Test public void testMissingRAttributeBug54288() { // workbook with cells missing the R attribute XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("54288.xlsx"); @@ -278,6 +292,7 @@ public final class TestXSSFCell extends BaseTestXCell { } } + @Test public void test56170() throws IOException { final Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56170.xlsx"); final XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0); @@ -326,6 +341,7 @@ public final class TestXSSFCell extends BaseTestXCell { stream.close();*/ } + @Test public void test56170Reproduce() throws IOException { final Workbook wb = new XSSFWorkbook(); try { @@ -370,6 +386,7 @@ public final class TestXSSFCell extends BaseTestXCell { } } + @Test public void testBug56644ReturnNull() throws IOException { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx"); try { @@ -382,6 +399,7 @@ public final class TestXSSFCell extends BaseTestXCell { } } + @Test public void testBug56644ReturnBlank() throws IOException { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx"); try { @@ -394,6 +412,7 @@ public final class TestXSSFCell extends BaseTestXCell { } } + @Test public void testBug56644CreateBlank() throws IOException { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx"); try { @@ -406,6 +425,7 @@ public final class TestXSSFCell extends BaseTestXCell { } } + @Test public void testEncodingbeloAscii(){ StringBuffer sb = new StringBuffer(); // test all possible characters |