diff options
Diffstat (limited to 'src/testcases')
7 files changed, 328 insertions, 378 deletions
diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestMetaDataIPI.java b/src/testcases/org/apache/poi/hpsf/basic/TestMetaDataIPI.java index de4dea27ef..9347699566 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestMetaDataIPI.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestMetaDataIPI.java @@ -18,6 +18,7 @@ package org.apache.poi.hpsf.basic; 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; @@ -55,7 +56,7 @@ public final class TestMetaDataIPI { public void tearDown() throws Exception { poifs.close(); } - + /** * Setup is used to get the document ready. Gets the DocumentSummaryInformation and the * SummaryInformation to reasonable values @@ -96,12 +97,12 @@ public final class TestMetaDataIPI { /* Insert some custom properties into the container. */ customProperties.put("Key1", "Value1"); customProperties.put("Schl\u00fcssel2", "Wert2"); - customProperties.put("Sample Integer", new Integer(12345)); - customProperties.put("Sample Boolean", Boolean.TRUE); + customProperties.put("Sample Integer", 12345); + customProperties.put("Sample Boolean", true); Date date = new Date(); customProperties.put("Sample Date", date); - customProperties.put("Sample Double", new Double(-1.0001)); - customProperties.put("Sample Negative Integer", new Integer(-100000)); + customProperties.put("Sample Double", -1.0001); + customProperties.put("Sample Negative Integer", -100000); dsi.setCustomProperties(customProperties); @@ -136,17 +137,17 @@ public final class TestMetaDataIPI { String a2 = (String) customProperties.get("Schl\u00fcssel2"); assertEquals("Schl\u00fcssel2", "Wert2", a2); Integer a3 = (Integer) customProperties.get("Sample Integer"); - assertEquals("Sample Number", new Integer(12345), a3); + assertEquals("Sample Number", 12345, (int)a3); Boolean a4 = (Boolean) customProperties.get("Sample Boolean"); - assertEquals("Sample Boolean", Boolean.TRUE, a4); + assertTrue("Sample Boolean", a4); Date a5 = (Date) customProperties.get("Sample Date"); assertEquals("Custom Date:", date, a5); Double a6 = (Double) customProperties.get("Sample Double"); - assertEquals("Custom Float", new Double(-1.0001), a6); + assertEquals("Custom Float", -1.0001, a6, 0); Integer a7 = (Integer) customProperties.get("Sample Negative Integer"); - assertEquals("Neg", new Integer(-100000), a7); + assertEquals("Neg", -100000, (int)a7); } /** @@ -185,7 +186,7 @@ public final class TestMetaDataIPI { /* Insert some custom properties into the container. */ customProperties.put(k1, p1); customProperties.put(k2, p2); - customProperties.put("Sample Number", new Integer(12345)); + customProperties.put("Sample Number", 12345); customProperties.put("Sample Boolean", Boolean.TRUE); Date date = new Date(); customProperties.put("Sample Date", date); @@ -226,9 +227,9 @@ public final class TestMetaDataIPI { String a2 = (String) customProperties.get(k2); assertEquals("Schl\u00fcssel2", p2, a2); Integer a3 = (Integer) customProperties.get("Sample Number"); - assertEquals("Sample Number", new Integer(12345), a3); + assertEquals("Sample Number", 12345, (int)a3); Boolean a4 = (Boolean) customProperties.get("Sample Boolean"); - assertEquals("Sample Boolean", Boolean.TRUE, a4); + assertTrue("Sample Boolean", a4); Date a5 = (Date) customProperties.get("Sample Date"); assertEquals("Custom Date:", date, a5); @@ -271,8 +272,8 @@ public final class TestMetaDataIPI { /* Insert some custom properties into the container. */ customProperties.put(k1, p1); customProperties.put(k2, p2); - customProperties.put("Sample Number", new Integer(12345)); - customProperties.put("Sample Boolean", Boolean.FALSE); + customProperties.put("Sample Number", 12345); + customProperties.put("Sample Boolean", false); Date date = new Date(0); customProperties.put("Sample Date", date); @@ -313,9 +314,9 @@ public final class TestMetaDataIPI { String a2 = (String) customProperties.get(k2); assertEquals("Schl\u00fcssel2", p2, a2); Integer a3 = (Integer) customProperties.get("Sample Number"); - assertEquals("Sample Number", new Integer(12345), a3); + assertEquals("Sample Number", 12345, (int)a3); Boolean a4 = (Boolean) customProperties.get("Sample Boolean"); - assertEquals("Sample Boolean", Boolean.FALSE, a4); + assertFalse("Sample Boolean", a4); Date a5 = (Date) customProperties.get("Sample Date"); assertEquals("Custom Date:", date, a5); @@ -359,13 +360,13 @@ public final class TestMetaDataIPI { si.setComments(comments); si.setKeywords(keywords); si.setSubject(subject); - + CustomProperties customProperties = new CustomProperties(); /* Insert some custom properties into the container. */ customProperties.put(k1, p1); customProperties.put(k2, p2); - customProperties.put("Sample Number", new Integer(12345)); - customProperties.put("Sample Boolean", Boolean.TRUE); + customProperties.put("Sample Number", 12345); + customProperties.put("Sample Boolean", true); Date date = new Date(); customProperties.put("Sample Date", date); @@ -404,9 +405,9 @@ public final class TestMetaDataIPI { String a2 = (String) customProperties.get(k2); assertEquals("Schl\u00fcssel2", p2, a2); Integer a3 = (Integer) customProperties.get("Sample Number"); - assertEquals("Sample Number", new Integer(12345), a3); + assertEquals("Sample Number", 12345, (int)a3); Boolean a4 = (Boolean) customProperties.get("Sample Boolean"); - assertEquals("Sample Boolean", Boolean.TRUE, a4); + assertTrue("Sample Boolean", a4); Date a5 = (Date) customProperties.get("Sample Date"); assertEquals("Custom Date:", date, a5); } @@ -437,16 +438,16 @@ public final class TestMetaDataIPI { } /* Insert some custom properties into the container. */ - customProperties.put("int", new Integer(12345)); - customProperties.put("negint", new Integer(-12345)); - customProperties.put("long", new Long(12345)); - customProperties.put("neglong", new Long(-12345)); - customProperties.put("boolean", Boolean.TRUE); + customProperties.put("int", 12345); + customProperties.put("negint", -12345); + customProperties.put("long", 12345L); + customProperties.put("neglong", -12345L); + customProperties.put("boolean", true); customProperties.put("string", "a String"); // customProperties.put("float", new Float(12345.0)); is not valid // customProperties.put("negfloat", new Float(-12345.1)); is not valid - customProperties.put("double", new Double(12345.2)); - customProperties.put("negdouble", new Double(-12345.3)); + customProperties.put("double", 12345.2); + customProperties.put("negdouble", -12345.3); // customProperties.put("char", new Character('a')); is not valid Date date = new Date(); @@ -485,28 +486,28 @@ public final class TestMetaDataIPI { /* Insert some custom properties into the container. */ Integer a3 = (Integer) customProperties.get("int"); - assertEquals("int", new Integer(12345), a3); + assertEquals("int", 12345, (int)a3); a3 = (Integer) customProperties.get("negint"); - assertEquals("negint", new Integer(-12345), a3); + assertEquals("negint", -12345, (int)a3); Long al = (Long) customProperties.get("neglong"); - assertEquals("neglong", new Long(-12345), al); + assertEquals("neglong", -12345L, (long)al); al = (Long) customProperties.get("long"); - assertEquals("long", new Long(12345), al); + assertEquals("long", 12345L, (long)al); Boolean a4 = (Boolean) customProperties.get("boolean"); - assertEquals("boolean", Boolean.TRUE, a4); + assertTrue("boolean", a4); Date a5 = (Date) customProperties.get("date"); assertEquals("Custom Date:", date, a5); Double d = (Double) customProperties.get("double"); - assertEquals("int", new Double(12345.2), d); + assertEquals("int", 12345.2, d, 0); d = (Double) customProperties.get("negdouble"); - assertEquals("string", new Double(-12345.3), d); + assertEquals("string", -12345.3, d, 0); String s = (String) customProperties.get("string"); assertEquals("sring", "a String", s); @@ -543,7 +544,7 @@ public final class TestMetaDataIPI { /* Read the document summary information. */ DirectoryEntry dir = poifs.getRoot(); - + dsi = (DocumentSummaryInformation)PropertySetFactory.create(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME); si = (SummaryInformation)PropertySetFactory.create(dir, SummaryInformation.DEFAULT_STREAM_NAME); } @@ -595,7 +596,6 @@ public final class TestMetaDataIPI { sb.append(" "); char j = (char) rand.nextInt(220); j += 33; - // System.out.println(j); sb.append(">"); sb.append(Character.valueOf(j)); sb.append("="); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/DummyGraphics2d.java b/src/testcases/org/apache/poi/hssf/usermodel/DummyGraphics2d.java index 6275fc0d02..a10cc1309d 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/DummyGraphics2d.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/DummyGraphics2d.java @@ -56,13 +56,13 @@ public class DummyGraphics2d extends Graphics2D { public DummyGraphics2d() { this(System.out); } - + public DummyGraphics2d(PrintStream log) { bufimg = new BufferedImage(1000, 1000, 2); g2D = (Graphics2D)bufimg.getGraphics(); this.log = log; } - + public DummyGraphics2d(PrintStream log, Graphics2D g2D) { this.g2D = g2D; this.log = log; @@ -86,7 +86,7 @@ public class DummyGraphics2d extends Graphics2D { public void draw(Shape s) { String l = - "draw(Shape):" + + "draw(Shape):" + "\n s = " + s; log.println( l ); g2D.draw( s ); @@ -715,8 +715,8 @@ public class DummyGraphics2d extends Graphics2D { @Override public final void finalize() { log.println( "finalize():" ); - g2D.finalize(); // NOSOLAR - super.finalize(); + g2D.dispose(); + dispose(); } public Shape getClip() { diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java b/src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java index e5ddcaa7f7..1e3fc381cc 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java @@ -61,14 +61,14 @@ public final class TestDataValidation extends BaseTestDataValidation { public void assertDataValidation(Workbook wb) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(22000); - try { + byte[] generatedContent; + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(22000)) { wb.write(baos); - baos.close(); + generatedContent = baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } - byte[] generatedContent = baos.toByteArray(); + boolean isSame; // if (false) { // // TODO - add proof spreadsheet and compare @@ -76,23 +76,21 @@ public final class TestDataValidation extends BaseTestDataValidation { // isSame = compareStreams(proofStream, generatedContent); // } isSame = true; - + if (isSame) { return; } File tempDir = new File(System.getProperty("java.io.tmpdir")); File generatedFile = new File(tempDir, "GeneratedTestDataValidation.xls"); - try { - FileOutputStream fileOut = new FileOutputStream(generatedFile); + try (FileOutputStream fileOut = new FileOutputStream(generatedFile)) { fileOut.write(generatedContent); - fileOut.close(); } catch (IOException e) { throw new RuntimeException(e); } - + PrintStream ps = System.out; - - ps.println("This test case has failed because the generated file differs from proof copy '" + + ps.println("This test case has failed because the generated file differs from proof copy '" ); // TODO+ proofFile.getAbsolutePath() + "'."); ps.println("The cause is usually a change to this test, or some common spreadsheet generation code. " + "The developer has to decide whether the changes were wanted or unwanted."); @@ -104,68 +102,43 @@ public final class TestDataValidation extends BaseTestDataValidation { ps.println("One other possible (but less likely) cause of a failed test is a problem in the " + "comparison logic used here. Perhaps some extra file regions need to be ignored."); ps.println("The generated file has been saved to '" + generatedFile.getAbsolutePath() + "' for manual inspection."); - - fail("Generated file differs from proof copy. See sysout comments for details on how to fix."); - - } - -// private static boolean compareStreams(InputStream isA, byte[] generatedContent) { -// -// InputStream isB = new ByteArrayInputStream(generatedContent); -// -// // The allowable regions where the generated file can differ from the -// // proof should be small (i.e. much less than 1K) -// int[] allowableDifferenceRegions = { -// 0x0228, 16, // a region of the file containing the OS username -// 0x506C, 8, // See RootProperty (super fields _seconds_2 and _days_2) -// }; -// int[] diffs = StreamUtility.diffStreams(isA, isB, allowableDifferenceRegions); -// if (diffs == null) { -// return true; -// } -// System.err.println("Diff from proof: "); -// for (int i = 0; i < diffs.length; i++) { -// System.err.println("diff at offset: 0x" + Integer.toHexString(diffs[i])); -// } -// return false; -// } - - + fail("Generated file differs from proof copy. See sysout comments for details on how to fix."); + } /* package */ static void setCellValue(HSSFCell cell, String text) { cell.setCellValue(new HSSFRichTextString(text)); - + } - + @Test public void testAddToExistingSheet() throws Exception { - // dvEmpty.xls is a simple one sheet workbook. With a DataValidations header record but no + // dvEmpty.xls is a simple one sheet workbook. With a DataValidations header record but no // DataValidations. It's important that the example has one SHEETPROTECTION record. // Such a workbook can be created in Excel (2007) by adding datavalidation for one cell // and then deleting the row that contains the cell. - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("dvEmpty.xls"); - int dvRow = 0; - Sheet sheet = wb.getSheetAt(0); - DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); - DataValidationConstraint dc = dataValidationHelper.createIntegerConstraint(OperatorType.EQUAL, "42", null); - DataValidation dv = dataValidationHelper.createValidation(dc,new CellRangeAddressList(dvRow, dvRow, 0, 0)); - - dv.setEmptyCellAllowed(false); - dv.setErrorStyle(ErrorStyle.STOP); - dv.setShowPromptBox(true); - dv.createErrorBox("Xxx", "Yyy"); - dv.setSuppressDropDownArrow(true); - - sheet.addValidationData(dv); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - wb.write(baos); - - byte[] wbData = baos.toByteArray(); - + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("dvEmpty.xls")) { + int dvRow = 0; + Sheet sheet = wb.getSheetAt(0); + DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); + DataValidationConstraint dc = dataValidationHelper.createIntegerConstraint(OperatorType.EQUAL, "42", null); + DataValidation dv = dataValidationHelper.createValidation(dc, new CellRangeAddressList(dvRow, dvRow, 0, 0)); + + dv.setEmptyCellAllowed(false); + dv.setErrorStyle(ErrorStyle.STOP); + dv.setShowPromptBox(true); + dv.createErrorBox("Xxx", "Yyy"); + dv.setSuppressDropDownArrow(true); + + sheet.addValidationData(dv); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + wb.write(baos); + + byte[] wbData = baos.toByteArray(); + // if (false) { // TODO (Jul 2008) fix EventRecordFactory to process unknown records, (and DV records for that matter) // // ERFListener erfListener = null; // new MyERFListener(); @@ -179,27 +152,25 @@ public final class TestDataValidation extends BaseTestDataValidation { // throw new RuntimeException(e); // } // } - // else verify record ordering by navigating the raw bytes - - byte[] dvHeaderRecStart= { (byte)0xB2, 0x01, 0x12, 0x00, }; - int dvHeaderOffset = findIndex(wbData, dvHeaderRecStart); - assertTrue(dvHeaderOffset > 0); - int nextRecIndex = dvHeaderOffset + 22; - int nextSid - = ((wbData[nextRecIndex + 0] << 0) & 0x00FF) - + ((wbData[nextRecIndex + 1] << 8) & 0xFF00) - ; - // nextSid should be for a DVRecord. If anything comes between the DV header record - // and the DV records, Excel will not be able to open the workbook without error. - - if (nextSid == 0x0867) { - fail("Identified bug 45519"); - } - assertEquals(DVRecord.sid, nextSid); - - wb.close(); + // else verify record ordering by navigating the raw bytes + + byte[] dvHeaderRecStart = {(byte) 0xB2, 0x01, 0x12, 0x00,}; + int dvHeaderOffset = findIndex(wbData, dvHeaderRecStart); + assertTrue(dvHeaderOffset > 0); + int nextRecIndex = dvHeaderOffset + 22; + int nextSid + = ((wbData[nextRecIndex + 0] << 0) & 0x00FF) + + ((wbData[nextRecIndex + 1] << 8) & 0xFF00); + // nextSid should be for a DVRecord. If anything comes between the DV header record + // and the DV records, Excel will not be able to open the workbook without error. + + if (nextSid == 0x0867) { + fail("Identified bug 45519"); + } + assertEquals(DVRecord.sid, nextSid); + } } - + private int findIndex(byte[] largeData, byte[] searchPattern) { byte firstByte = searchPattern[0]; for (int i = 0; i < largeData.length; i++) { @@ -222,256 +193,243 @@ public final class TestDataValidation extends BaseTestDataValidation { @Test public void testGetDataValidationsAny() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - List<HSSFDataValidation> list = sheet.getDataValidations(); - assertEquals(0, list.size()); - - DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); - DataValidationConstraint constraint = dataValidationHelper.createNumericConstraint(ValidationType.ANY, - OperatorType.IGNORED, null, null); - CellRangeAddressList addressList = new CellRangeAddressList(1, 2, 3, 4); - DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); - validation.setEmptyCellAllowed(true); - validation.createErrorBox("error-title", "error-text"); - validation.createPromptBox("prompt-title", "prompt-text"); - sheet.addValidationData(validation); - - list = sheet.getDataValidations(); // <-- works - assertEquals(1, list.size()); - - HSSFDataValidation dv = list.get(0); - { - CellRangeAddressList regions = dv.getRegions(); - assertEquals(1, regions.countRanges()); - - CellRangeAddress address = regions.getCellRangeAddress(0); - assertEquals(1, address.getFirstRow()); - assertEquals(2, address.getLastRow()); - assertEquals(3, address.getFirstColumn()); - assertEquals(4, address.getLastColumn()); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + List<HSSFDataValidation> list = sheet.getDataValidations(); + assertEquals(0, list.size()); + + DataValidationHelper dvh = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = dvh.createNumericConstraint(ValidationType.ANY, OperatorType.IGNORED, null, null); + CellRangeAddressList addressList = new CellRangeAddressList(1, 2, 3, 4); + DataValidation validation = dvh.createValidation(constraint, addressList); + validation.setEmptyCellAllowed(true); + validation.createErrorBox("error-title", "error-text"); + validation.createPromptBox("prompt-title", "prompt-text"); + sheet.addValidationData(validation); + + list = sheet.getDataValidations(); // <-- works + assertEquals(1, list.size()); + + HSSFDataValidation dv = list.get(0); + { + CellRangeAddressList regions = dv.getRegions(); + assertEquals(1, regions.countRanges()); + + CellRangeAddress address = regions.getCellRangeAddress(0); + assertEquals(1, address.getFirstRow()); + assertEquals(2, address.getLastRow()); + assertEquals(3, address.getFirstColumn()); + assertEquals(4, address.getLastColumn()); + } + assertTrue(dv.getEmptyCellAllowed()); + assertFalse(dv.getSuppressDropDownArrow()); + assertTrue(dv.getShowErrorBox()); + assertEquals("error-title", dv.getErrorBoxTitle()); + assertEquals("error-text", dv.getErrorBoxText()); + assertTrue(dv.getShowPromptBox()); + assertEquals("prompt-title", dv.getPromptBoxTitle()); + assertEquals("prompt-text", dv.getPromptBoxText()); + + DataValidationConstraint c = dv.getValidationConstraint(); + assertEquals(ValidationType.ANY, c.getValidationType()); + assertEquals(OperatorType.IGNORED, c.getOperator()); } - assertTrue(dv.getEmptyCellAllowed()); - assertFalse(dv.getSuppressDropDownArrow()); - assertTrue(dv.getShowErrorBox()); - assertEquals("error-title", dv.getErrorBoxTitle()); - assertEquals("error-text", dv.getErrorBoxText()); - assertTrue(dv.getShowPromptBox()); - assertEquals("prompt-title", dv.getPromptBoxTitle()); - assertEquals("prompt-text", dv.getPromptBoxText()); - - DataValidationConstraint c = dv.getValidationConstraint(); - assertEquals(ValidationType.ANY, c.getValidationType()); - assertEquals(OperatorType.IGNORED, c.getOperator()); - - wb.close(); } @Test public void testGetDataValidationsIntegerFormula() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - List<HSSFDataValidation> list = sheet.getDataValidations(); - assertEquals(0, list.size()); - - DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); - DataValidationConstraint constraint = dataValidationHelper.createIntegerConstraint(OperatorType.BETWEEN, "=A2", - "=A3"); - CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); - DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); - sheet.addValidationData(validation); - - list = sheet.getDataValidations(); // <-- works - assertEquals(1, list.size()); - - HSSFDataValidation dv = list.get(0); - DVConstraint c = dv.getConstraint(); - assertEquals(ValidationType.INTEGER, c.getValidationType()); - assertEquals(OperatorType.BETWEEN, c.getOperator()); - assertEquals("A2", c.getFormula1()); - assertEquals("A3", c.getFormula2()); - assertNull(c.getValue1()); - assertNull(c.getValue2()); - - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + List<HSSFDataValidation> list = sheet.getDataValidations(); + assertEquals(0, list.size()); + + DataValidationHelper dvh = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = dvh.createIntegerConstraint(OperatorType.BETWEEN, "=A2", "=A3"); + CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); + DataValidation validation = dvh.createValidation(constraint, addressList); + sheet.addValidationData(validation); + + list = sheet.getDataValidations(); // <-- works + assertEquals(1, list.size()); + + HSSFDataValidation dv = list.get(0); + DVConstraint c = dv.getConstraint(); + assertEquals(ValidationType.INTEGER, c.getValidationType()); + assertEquals(OperatorType.BETWEEN, c.getOperator()); + assertEquals("A2", c.getFormula1()); + assertEquals("A3", c.getFormula2()); + assertNull(c.getValue1()); + assertNull(c.getValue2()); + } } @Test public void testGetDataValidationsIntegerValue() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - List<HSSFDataValidation> list = sheet.getDataValidations(); - assertEquals(0, list.size()); - - DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); - DataValidationConstraint constraint = dataValidationHelper.createIntegerConstraint(OperatorType.BETWEEN, "100", - "200"); - CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); - DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); - sheet.addValidationData(validation); - - list = sheet.getDataValidations(); // <-- works - assertEquals(1, list.size()); - - HSSFDataValidation dv = list.get(0); - DVConstraint c = dv.getConstraint(); - assertEquals(ValidationType.INTEGER, c.getValidationType()); - assertEquals(OperatorType.BETWEEN, c.getOperator()); - assertNull(c.getFormula1()); - assertNull(c.getFormula2()); - assertEquals(new Double("100"), c.getValue1()); - assertEquals(new Double("200"), c.getValue2()); - - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + List<HSSFDataValidation> list = sheet.getDataValidations(); + assertEquals(0, list.size()); + + DataValidationHelper dvh = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = dvh.createIntegerConstraint(OperatorType.BETWEEN, "100", "200"); + CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); + DataValidation validation = dvh.createValidation(constraint, addressList); + sheet.addValidationData(validation); + + list = sheet.getDataValidations(); // <-- works + assertEquals(1, list.size()); + + HSSFDataValidation dv = list.get(0); + DVConstraint c = dv.getConstraint(); + assertEquals(ValidationType.INTEGER, c.getValidationType()); + assertEquals(OperatorType.BETWEEN, c.getOperator()); + assertNull(c.getFormula1()); + assertNull(c.getFormula2()); + assertEquals(100d, c.getValue1(), 0); + assertEquals(200d, c.getValue2(), 0); + } } @Test public void testGetDataValidationsDecimal() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - List<HSSFDataValidation> list = sheet.getDataValidations(); - assertEquals(0, list.size()); - - DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); - DataValidationConstraint constraint = dataValidationHelper.createDecimalConstraint(OperatorType.BETWEEN, "=A2", - "200"); - CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); - DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); - sheet.addValidationData(validation); - - list = sheet.getDataValidations(); // <-- works - assertEquals(1, list.size()); - - HSSFDataValidation dv = list.get(0); - DVConstraint c = dv.getConstraint(); - assertEquals(ValidationType.DECIMAL, c.getValidationType()); - assertEquals(OperatorType.BETWEEN, c.getOperator()); - assertEquals("A2", c.getFormula1()); - assertNull(c.getFormula2()); - assertNull(c.getValue1()); - assertEquals(new Double("200"), c.getValue2()); - - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + List<HSSFDataValidation> list = sheet.getDataValidations(); + assertEquals(0, list.size()); + + DataValidationHelper dvh = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = dvh.createDecimalConstraint(OperatorType.BETWEEN, "=A2", "200"); + CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); + DataValidation validation = dvh.createValidation(constraint, addressList); + sheet.addValidationData(validation); + + list = sheet.getDataValidations(); // <-- works + assertEquals(1, list.size()); + + HSSFDataValidation dv = list.get(0); + DVConstraint c = dv.getConstraint(); + assertEquals(ValidationType.DECIMAL, c.getValidationType()); + assertEquals(OperatorType.BETWEEN, c.getOperator()); + assertEquals("A2", c.getFormula1()); + assertNull(c.getFormula2()); + assertNull(c.getValue1()); + assertEquals(200, c.getValue2(), 0); + } } @Test public void testGetDataValidationsDate() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - List<HSSFDataValidation> list = sheet.getDataValidations(); - assertEquals(0, list.size()); - - DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); - DataValidationConstraint constraint = dataValidationHelper.createDateConstraint(OperatorType.EQUAL, - "2014/10/25", null, null); - CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); - DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); - sheet.addValidationData(validation); - - list = sheet.getDataValidations(); // <-- works - assertEquals(1, list.size()); - - HSSFDataValidation dv = list.get(0); - DVConstraint c = dv.getConstraint(); - assertEquals(ValidationType.DATE, c.getValidationType()); - assertEquals(OperatorType.EQUAL, c.getOperator()); - assertNull(c.getFormula1()); - assertNull(c.getFormula2()); - assertEquals(DateUtil.getExcelDate(DateUtil.parseYYYYMMDDDate("2014/10/25")), c.getValue1(), 0); - assertNull(c.getValue2()); - - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + List<HSSFDataValidation> list = sheet.getDataValidations(); + assertEquals(0, list.size()); + + DataValidationHelper dvh = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = dvh.createDateConstraint(OperatorType.EQUAL, "2014/10/25", null, null); + CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); + DataValidation validation = dvh.createValidation(constraint, addressList); + sheet.addValidationData(validation); + + list = sheet.getDataValidations(); // <-- works + assertEquals(1, list.size()); + + HSSFDataValidation dv = list.get(0); + DVConstraint c = dv.getConstraint(); + assertEquals(ValidationType.DATE, c.getValidationType()); + assertEquals(OperatorType.EQUAL, c.getOperator()); + assertNull(c.getFormula1()); + assertNull(c.getFormula2()); + assertEquals(DateUtil.getExcelDate(DateUtil.parseYYYYMMDDDate("2014/10/25")), c.getValue1(), 0); + assertNull(c.getValue2()); + } } @Test public void testGetDataValidationsListExplicit() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - List<HSSFDataValidation> list = sheet.getDataValidations(); - assertEquals(0, list.size()); - - DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); - DataValidationConstraint constraint = dataValidationHelper.createExplicitListConstraint(new String[] { "aaa", - "bbb", "ccc" }); - CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); - DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); - validation.setSuppressDropDownArrow(true); - sheet.addValidationData(validation); - - list = sheet.getDataValidations(); // <-- works - assertEquals(1, list.size()); - - HSSFDataValidation dv = list.get(0); - assertTrue(dv.getSuppressDropDownArrow()); - - DVConstraint c = dv.getConstraint(); - assertEquals(ValidationType.LIST, c.getValidationType()); - assertNull(c.getFormula1()); - assertNull(c.getFormula2()); - assertNull(c.getValue1()); - assertNull(c.getValue2()); - String[] values = c.getExplicitListValues(); - assertEquals(3, values.length); - assertEquals("aaa", values[0]); - assertEquals("bbb", values[1]); - assertEquals("ccc", values[2]); - - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + List<HSSFDataValidation> list = sheet.getDataValidations(); + assertEquals(0, list.size()); + + DataValidationHelper dvh = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = dvh.createExplicitListConstraint(new String[]{"aaa", "bbb", "ccc"}); + CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); + DataValidation validation = dvh.createValidation(constraint, addressList); + validation.setSuppressDropDownArrow(true); + sheet.addValidationData(validation); + + list = sheet.getDataValidations(); // <-- works + assertEquals(1, list.size()); + + HSSFDataValidation dv = list.get(0); + assertTrue(dv.getSuppressDropDownArrow()); + + DVConstraint c = dv.getConstraint(); + assertEquals(ValidationType.LIST, c.getValidationType()); + assertNull(c.getFormula1()); + assertNull(c.getFormula2()); + assertNull(c.getValue1()); + assertNull(c.getValue2()); + String[] values = c.getExplicitListValues(); + assertEquals(3, values.length); + assertEquals("aaa", values[0]); + assertEquals("bbb", values[1]); + assertEquals("ccc", values[2]); + } } @Test public void testGetDataValidationsListFormula() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - List<HSSFDataValidation> list = sheet.getDataValidations(); - assertEquals(0, list.size()); - - DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); - DataValidationConstraint constraint = dataValidationHelper.createFormulaListConstraint("A2"); - CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); - DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); - validation.setSuppressDropDownArrow(true); - sheet.addValidationData(validation); - - list = sheet.getDataValidations(); // <-- works - assertEquals(1, list.size()); - - HSSFDataValidation dv = list.get(0); - assertTrue(dv.getSuppressDropDownArrow()); - - DVConstraint c = dv.getConstraint(); - assertEquals(ValidationType.LIST, c.getValidationType()); - assertEquals("A2", c.getFormula1()); - assertNull(c.getFormula2()); - assertNull(c.getValue1()); - assertNull(c.getValue2()); - - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + List<HSSFDataValidation> list = sheet.getDataValidations(); + assertEquals(0, list.size()); + + DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = dataValidationHelper.createFormulaListConstraint("A2"); + CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); + DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); + validation.setSuppressDropDownArrow(true); + sheet.addValidationData(validation); + + list = sheet.getDataValidations(); // <-- works + assertEquals(1, list.size()); + + HSSFDataValidation dv = list.get(0); + assertTrue(dv.getSuppressDropDownArrow()); + + DVConstraint c = dv.getConstraint(); + assertEquals(ValidationType.LIST, c.getValidationType()); + assertEquals("A2", c.getFormula1()); + assertNull(c.getFormula2()); + assertNull(c.getValue1()); + assertNull(c.getValue2()); + } } @Test public void testGetDataValidationsFormula() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - List<HSSFDataValidation> list = sheet.getDataValidations(); - assertEquals(0, list.size()); - - DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); - DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("A2:A3"); - CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); - DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); - sheet.addValidationData(validation); - - list = sheet.getDataValidations(); // <-- works - assertEquals(1, list.size()); - - HSSFDataValidation dv = list.get(0); - DVConstraint c = dv.getConstraint(); - assertEquals(ValidationType.FORMULA, c.getValidationType()); - assertEquals("A2:A3", c.getFormula1()); - assertNull(c.getFormula2()); - assertNull(c.getValue1()); - assertNull(c.getValue2()); - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + List<HSSFDataValidation> list = sheet.getDataValidations(); + assertEquals(0, list.size()); + + DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("A2:A3"); + CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); + DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); + sheet.addValidationData(validation); + + list = sheet.getDataValidations(); // <-- works + assertEquals(1, list.size()); + + HSSFDataValidation dv = list.get(0); + DVConstraint c = dv.getConstraint(); + assertEquals(ValidationType.FORMULA, c.getValidationType()); + assertEquals("A2:A3", c.getFormula1()); + assertNull(c.getFormula2()); + assertNull(c.getValue1()); + assertNull(c.getValue2()); + } } } diff --git a/src/testcases/org/apache/poi/ss/formula/constant/TestConstantValueParser.java b/src/testcases/org/apache/poi/ss/formula/constant/TestConstantValueParser.java index 08ce2d96e4..72a9bcc0d8 100644 --- a/src/testcases/org/apache/poi/ss/formula/constant/TestConstantValueParser.java +++ b/src/testcases/org/apache/poi/ss/formula/constant/TestConstantValueParser.java @@ -34,7 +34,7 @@ public final class TestConstantValueParser { private static final Object[] SAMPLE_VALUES = { Boolean.TRUE, null, - new Double(1.1), + 1.1, "Sample text", ErrorConstant.valueOf(FormulaError.DIV0.getCode()), }; @@ -44,29 +44,29 @@ public final class TestConstantValueParser { "01 9A 99 99 99 99 99 F1 3F " + "02 0B 00 00 53 61 6D 70 6C 65 20 74 65 78 74 " + "10 07 00 00 00 00 00 00 00"); - + @Test public void testGetEncodedSize() { int actual = ConstantValueParser.getEncodedSize(SAMPLE_VALUES); assertEquals(51, actual); } - + @Test public void testEncode() { int size = ConstantValueParser.getEncodedSize(SAMPLE_VALUES); byte[] data = new byte[size]; - + ConstantValueParser.encode(new LittleEndianByteArrayOutputStream(data, 0), SAMPLE_VALUES); - + if (!Arrays.equals(data, SAMPLE_ENCODING)) { fail("Encoding differs"); } } - + @Test public void testDecode() { LittleEndianInput in = TestcaseRecordInputStream.createLittleEndian(SAMPLE_ENCODING); - + Object[] values = ConstantValueParser.parse(in, 4); for (int i = 0; i < values.length; i++) { if(!isEqual(SAMPLE_VALUES[i], values[i])) { diff --git a/src/testcases/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java b/src/testcases/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java index ac2551665d..c408dd527b 100644 --- a/src/testcases/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java +++ b/src/testcases/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java @@ -39,19 +39,20 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.Stack; -import java.util.zip.ZipException; import java.util.zip.ZipFile; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + import org.apache.poi.poifs.crypt.CryptoFunctions; import org.apache.poi.poifs.crypt.HashAlgorithm; import org.apache.poi.util.TempFile; +import org.apache.poi.util.XMLHelper; import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; import org.xml.sax.Locator; import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.XMLReaderFactory; +import org.xml.sax.helpers.DefaultHandler; /** * This class is not used during normal POI run-time but is used at development time to generate @@ -229,7 +230,7 @@ public final class ExcelFileFormatDocFunctionExtractor { /** * To avoid drag-in - parse XML using only JDK. */ - private static class EFFDocHandler implements ContentHandler { + private static class EFFDocHandler extends DefaultHandler { private static final String[] HEADING_PATH_NAMES = { "office:document-content", "office:body", "office:text", "text:h", }; @@ -339,7 +340,7 @@ public final class ExcelFileFormatDocFunctionExtractor { processFunction(cellData, noteFlags, 0); processFunction(cellData, noteFlags, 8); } - + public void processFunction(String[] cellData, Boolean[] noteFlags, int i) { String funcIxStr = cellData[i + 0]; if (funcIxStr.length() < 1) { @@ -428,27 +429,18 @@ public final class ExcelFileFormatDocFunctionExtractor { } private static void extractFunctionData(FunctionDataCollector fdc, InputStream is) { - XMLReader xr; + SAXParserFactory sf = XMLHelper.getSaxParserFactory(); + SAXParser xr; try { // First up, try the default one - xr = XMLReaderFactory.createXMLReader(); - } catch (SAXException e) { - // Try one for java 1.4 - System.setProperty("org.xml.sax.driver", "org.apache.crimson.parser.XMLReaderImpl"); - try { - xr = XMLReaderFactory.createXMLReader(); - } catch (SAXException e2) { - throw new RuntimeException(e2); - } + xr = sf.newSAXParser(); + } catch (SAXException | ParserConfigurationException e) { + throw new RuntimeException(e); } - xr.setContentHandler(new EFFDocHandler(fdc)); - InputSource inSrc = new InputSource(is); - - try { - xr.parse(inSrc); - is.close(); + try (InputStream is2 = is) { + xr.parse(is2, new EFFDocHandler(fdc)); } catch (IOException | SAXException e) { throw new RuntimeException(e); } @@ -463,7 +455,7 @@ public final class ExcelFileFormatDocFunctionExtractor { public SimpleAsciiOutputStream(OutputStream os) { _os = os; } - + @Override public void write(int b) throws IOException { checkByte(b); diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestStatsLib.java b/src/testcases/org/apache/poi/ss/formula/functions/TestStatsLib.java index bb8b8e62bb..9c4fdcb2f2 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestStatsLib.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestStatsLib.java @@ -220,7 +220,7 @@ public class TestStatsLib extends BaseTestNumeric { } private static void confirmMode(double[] v, double expectedResult) { - confirmMode(v, new Double(expectedResult)); + confirmMode(v, (Double)expectedResult); } private static void confirmMode(double[] v, Double expectedResult) { diff --git a/src/testcases/org/apache/poi/ss/formula/ptg/TestArrayPtg.java b/src/testcases/org/apache/poi/ss/formula/ptg/TestArrayPtg.java index 960209681b..170c06719b 100644 --- a/src/testcases/org/apache/poi/ss/formula/ptg/TestArrayPtg.java +++ b/src/testcases/org/apache/poi/ss/formula/ptg/TestArrayPtg.java @@ -67,7 +67,7 @@ public final class TestArrayPtg { assertEquals(Boolean.TRUE, values[0][0]); assertEquals("ABCD", values[0][1]); - assertEquals(new Double(0), values[1][0]); + assertEquals(0d, values[1][0]); assertEquals(Boolean.FALSE, values[1][1]); assertEquals("FG", values[1][2]); |