Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestExtendedPivotTableViewFieldsRecord.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestExtendedPivotTableViewFieldsRecord.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestExtendedPivotTableViewFieldsRecord.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestExtendedPivotTableViewFieldsRecord.java Fri Dec 27 23:00:13 2019 @@ -17,60 +17,46 @@ package org.apache.poi.hssf.record.pivot; +import static org.apache.poi.hssf.record.TestcaseRecordInputStream.confirmRecordEncoding; +import static org.junit.Assert.assertEquals; + import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.record.TestcaseRecordInputStream; import org.apache.poi.hssf.record.pivottable.ExtendedPivotTableViewFieldsRecord; import org.apache.poi.util.HexRead; -import org.apache.poi.util.RecordFormatException; - -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import org.junit.Test; /** * Tests for {@link ExtendedPivotTableViewFieldsRecord} - * - * @author Josh Micich */ -public final class TestExtendedPivotTableViewFieldsRecord extends TestCase { - +public final class TestExtendedPivotTableViewFieldsRecord { + + @Test public void testSubNameNotPresent_bug46693() { // This data came from attachment 23347 of bug 46693 at offset 0xAA43 byte[] data = HexRead.readFromString( "00 01 14 00" + // BIFF header "1E 14 00 0A FF FF FF FF 00 00 FF FF 00 00 00 00 00 00 00 00"); RecordInputStream in = TestcaseRecordInputStream.create(data); - ExtendedPivotTableViewFieldsRecord rec; - try { - rec = new ExtendedPivotTableViewFieldsRecord(in); - } catch (RecordFormatException e) { - if (e.getMessage().equals("Expected to find a ContinueRecord in order to read remaining 65535 of 65535 chars")) { - throw new AssertionFailedError("Identified bug 46693a"); - } - throw e; - } - + // bug 46693a - Expected to find a ContinueRecord in order to read remaining 65535 of 65535 chars + ExtendedPivotTableViewFieldsRecord rec = new ExtendedPivotTableViewFieldsRecord(in); + assertEquals(data.length, rec.getRecordSize()); } - + + @Test public void testOlderFormat_bug46918() { // There are 10 SXVDEX records in the file (not uploaded) that originated bugzilla 46918 // They all had the following hex encoding: byte[] data = HexRead.readFromString("00 01 0A 00 1E 14 00 0A FF FF FF FF 00 00"); RecordInputStream in = TestcaseRecordInputStream.create(data); - ExtendedPivotTableViewFieldsRecord rec; - try { - rec = new ExtendedPivotTableViewFieldsRecord(in); - } catch (RecordFormatException e) { - if (e.getMessage().equals("Not enough data (0) to read requested (2) bytes")) { - throw new AssertionFailedError("Identified bug 46918"); - } - throw e; - } + // bug 46918 - Not enough data (0) to read requested (2) bytes + ExtendedPivotTableViewFieldsRecord rec = new ExtendedPivotTableViewFieldsRecord(in); byte[] expReserData = HexRead.readFromString("1E 14 00 0A FF FF FF FF 00 00" + "FF FF 00 00 00 00 00 00 00 00"); - - TestcaseRecordInputStream.confirmRecordEncoding(ExtendedPivotTableViewFieldsRecord.sid, expReserData, rec.serialize()); + + confirmRecordEncoding(ExtendedPivotTableViewFieldsRecord.sid, expReserData, rec.serialize()); } }
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestPageItemRecord.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestPageItemRecord.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestPageItemRecord.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestPageItemRecord.java Fri Dec 27 23:00:13 2019 @@ -17,47 +17,45 @@ package org.apache.poi.hssf.record.pivot; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import static org.apache.poi.hssf.record.TestcaseRecordInputStream.confirmRecordEncoding; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.record.TestcaseRecordInputStream; import org.apache.poi.hssf.record.pivottable.PageItemRecord; import org.apache.poi.util.HexRead; +import org.junit.Test; /** * Tests for {@link PageItemRecord} - * - * @author Josh Micich */ -public final class TestPageItemRecord extends TestCase { - +public final class TestPageItemRecord { + @Test public void testMoreThanOneInfoItem_bug46917() { byte[] data = HexRead.readFromString("01 02 03 04 05 06 07 08 09 0A 0B 0C"); RecordInputStream in = TestcaseRecordInputStream.create(PageItemRecord.sid, data); PageItemRecord rec = new PageItemRecord(in); - if (in.remaining() == 6) { - throw new AssertionFailedError("Identified bug 46917"); - } + assertNotEquals("Identified bug 46917", 6, in.remaining()); assertEquals(0, in.remaining()); - + assertEquals(4+data.length, rec.getRecordSize()); } - + + @Test public void testSerialize() { confirmSerialize("01 02 03 04 05 06"); confirmSerialize("01 02 03 04 05 06 07 08 09 0A 0B 0C"); confirmSerialize("01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12"); } - private static PageItemRecord confirmSerialize(String hexDump) { + private static void confirmSerialize(String hexDump) { byte[] data = HexRead.readFromString(hexDump); RecordInputStream in = TestcaseRecordInputStream.create(PageItemRecord.sid, data); PageItemRecord rec = new PageItemRecord(in); assertEquals(0, in.remaining()); assertEquals(4+data.length, rec.getRecordSize()); byte[] data2 = rec.serialize(); - TestcaseRecordInputStream.confirmRecordEncoding(PageItemRecord.sid, data, data2); - return rec; + confirmRecordEncoding(PageItemRecord.sid, data, data2); } } Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestViewFieldsRecord.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestViewFieldsRecord.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestViewFieldsRecord.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/record/pivot/TestViewFieldsRecord.java Fri Dec 27 23:00:13 2019 @@ -17,48 +17,47 @@ package org.apache.poi.hssf.record.pivot; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import static org.apache.poi.hssf.record.TestcaseRecordInputStream.confirmRecordEncoding; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.record.TestcaseRecordInputStream; import org.apache.poi.hssf.record.pivottable.ViewFieldsRecord; import org.apache.poi.util.HexRead; +import org.junit.Test; /** * Tests for {@link ViewFieldsRecord} - * - * @author Josh Micich */ -public final class TestViewFieldsRecord extends TestCase { - +public final class TestViewFieldsRecord { + + @Test public void testUnicodeFlag_bug46693() { byte[] data = HexRead.readFromString("01 00 01 00 01 00 04 00 05 00 00 6D 61 72 63 6F"); RecordInputStream in = TestcaseRecordInputStream.create(ViewFieldsRecord.sid, data); ViewFieldsRecord rec = new ViewFieldsRecord(in); - if (in.remaining() == 1) { - throw new AssertionFailedError("Identified bug 46693b"); - } + assertNotEquals("Identified bug 46693b", 1, in.remaining()); assertEquals(0, in.remaining()); assertEquals(4+data.length, rec.getRecordSize()); } - + + @Test public void testSerialize() { - // This hex data was produced by changing the 'Custom Name' property, + // This hex data was produced by changing the 'Custom Name' property, // available under 'Field Settings' from the 'PivotTable Field List' (Excel 2007) confirmSerialize("00 00 01 00 01 00 00 00 FF FF"); confirmSerialize("01 00 01 00 01 00 04 00 05 00 00 6D 61 72 63 6F"); confirmSerialize("01 00 01 00 01 00 04 00 0A 00 01 48 00 69 00 73 00 74 00 6F 00 72 00 79 00 2D 00 82 69 81 89"); } - private static ViewFieldsRecord confirmSerialize(String hexDump) { + private static void confirmSerialize(String hexDump) { byte[] data = HexRead.readFromString(hexDump); RecordInputStream in = TestcaseRecordInputStream.create(ViewFieldsRecord.sid, data); ViewFieldsRecord rec = new ViewFieldsRecord(in); assertEquals(0, in.remaining()); assertEquals(4+data.length, rec.getRecordSize()); byte[] data2 = rec.serialize(); - TestcaseRecordInputStream.confirmRecordEncoding(ViewFieldsRecord.sid, data, data2); - return rec; + confirmRecordEncoding(ViewFieldsRecord.sid, data, data2); } } Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java Fri Dec 27 23:00:13 2019 @@ -17,6 +17,14 @@ package org.apache.poi.hssf.usermodel; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -39,13 +47,11 @@ import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.TempFile; import org.junit.Test; -import junit.framework.TestCase; - /** * Class to test cell styling functionality */ -public final class TestCellStyle extends TestCase { +public final class TestCellStyle { private static HSSFWorkbook openSample(String sampleFileName) { return HSSFTestDataSamples.openSampleWorkbook(sampleFileName); @@ -61,39 +67,39 @@ public final class TestCellStyle extends * HSSFSheet last row or first row is incorrect. <P> * */ + @Test public void testWriteSheetFont() throws IOException{ - File file = TempFile.createTempFile("testWriteSheetFont", - ".xls"); - FileOutputStream out = new FileOutputStream(file); - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); - HSSFRow r = null; - HSSFCell c = null; - HSSFFont fnt = wb.createFont(); - HSSFCellStyle cs = wb.createCellStyle(); - - fnt.setColor(HSSFFont.COLOR_RED); - fnt.setBold(true); - cs.setFont(fnt); - for (int rownum = 0; rownum < 100; rownum++) { - r = s.createRow(rownum); - - for (int cellnum = 0; cellnum < 50; cellnum += 2) { - c = r.createCell(cellnum); - c.setCellValue(rownum * 10000 + cellnum - + ((( double ) rownum / 1000) - + (( double ) cellnum / 10000))); - c = r.createCell(cellnum + 1); - c.setCellValue("TEST"); - c.setCellStyle(cs); + File file = TempFile.createTempFile("testWriteSheetFont", ".xls"); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet s = wb.createSheet(); + HSSFFont fnt = wb.createFont(); + HSSFCellStyle cs = wb.createCellStyle(); + + fnt.setColor(HSSFFont.COLOR_RED); + fnt.setBold(true); + cs.setFont(fnt); + for (int rownum = 0; rownum < 100; rownum++) { + HSSFRow r = s.createRow(rownum); + + for (int cellnum = 0; cellnum < 50; cellnum += 2) { + HSSFCell c = r.createCell(cellnum); + c.setCellValue(rownum * 10000 + cellnum + + (((double) rownum / 1000) + + ((double) cellnum / 10000))); + c = r.createCell(cellnum + 1); + c.setCellValue("TEST"); + c.setCellStyle(cs); + } + } + try (FileOutputStream out = new FileOutputStream(file)) { + wb.write(out); } + + SanityChecker sanityChecker = new SanityChecker(); + sanityChecker.checkHSSFWorkbook(wb); + assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); + assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); } - wb.write(out); - out.close(); - SanityChecker sanityChecker = new SanityChecker(); - sanityChecker.checkHSSFWorkbook(wb); - assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); - assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); // assert((s.getLastRowNum() == 99)); } @@ -101,66 +107,67 @@ public final class TestCellStyle extends /** * Tests that is creating a file with a date or an calendar works correctly. */ + @Test public void testDataStyle() throws IOException { - File file = TempFile.createTempFile("testWriteSheetStyleDate", - ".xls"); - FileOutputStream out = new FileOutputStream(file); - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); - HSSFCellStyle cs = wb.createCellStyle(); - HSSFRow row = s.createRow(0); - - // with Date: - HSSFCell cell = row.createCell(1); - cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); - cell.setCellStyle(cs); - cell.setCellValue(new Date()); - - // with Calendar: - cell = row.createCell(2); - cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); - cell.setCellStyle(cs); - Calendar cal = LocaleUtil.getLocaleCalendar(); - cell.setCellValue(cal); - - wb.write(out); - out.close(); - SanityChecker sanityChecker = new SanityChecker(); - sanityChecker.checkHSSFWorkbook(wb); + File file = TempFile.createTempFile("testWriteSheetStyleDate", ".xls"); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet s = wb.createSheet(); + HSSFCellStyle cs = wb.createCellStyle(); + HSSFRow row = s.createRow(0); + + // with Date: + HSSFCell cell = row.createCell(1); + cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); + cell.setCellStyle(cs); + cell.setCellValue(new Date()); + + // with Calendar: + cell = row.createCell(2); + cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); + cell.setCellStyle(cs); + Calendar cal = LocaleUtil.getLocaleCalendar(); + cell.setCellValue(cal); + + try (FileOutputStream out = new FileOutputStream(file)) { + wb.write(out); + } + SanityChecker sanityChecker = new SanityChecker(); + sanityChecker.checkHSSFWorkbook(wb); - assertEquals("LAST ROW ", 0, s.getLastRowNum()); - assertEquals("FIRST ROW ", 0, s.getFirstRowNum()); + assertEquals("LAST ROW ", 0, s.getLastRowNum()); + assertEquals("FIRST ROW ", 0, s.getFirstRowNum()); + } } - + + @Test public void testHashEquals() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); - HSSFCellStyle cs1 = wb.createCellStyle(); - HSSFCellStyle cs2 = wb.createCellStyle(); - HSSFRow row = s.createRow(0); - HSSFCell cell1 = row.createCell(1); - HSSFCell cell2 = row.createCell(2); - - cs1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); - cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/dd/yy")); - - cell1.setCellStyle(cs1); - cell1.setCellValue(new Date()); - - cell2.setCellStyle(cs2); - cell2.setCellValue(new Date()); - - assertEquals(cs1.hashCode(), cs1.hashCode()); - assertEquals(cs2.hashCode(), cs2.hashCode()); - assertEquals(cs1, cs1); - assertEquals(cs2, cs2); - - // Change cs1, hash will alter - int hash1 = cs1.hashCode(); - cs1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/dd/yy")); - assertFalse(hash1 == cs1.hashCode()); - - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet s = wb.createSheet(); + HSSFCellStyle cs1 = wb.createCellStyle(); + HSSFCellStyle cs2 = wb.createCellStyle(); + HSSFRow row = s.createRow(0); + HSSFCell cell1 = row.createCell(1); + HSSFCell cell2 = row.createCell(2); + + cs1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); + cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/dd/yy")); + + cell1.setCellStyle(cs1); + cell1.setCellValue(new Date()); + + cell2.setCellStyle(cs2); + cell2.setCellValue(new Date()); + + assertEquals(cs1.hashCode(), cs1.hashCode()); + assertEquals(cs2.hashCode(), cs2.hashCode()); + assertEquals(cs1, cs1); + assertEquals(cs2, cs2); + + // Change cs1, hash will alter + int hash1 = cs1.hashCode(); + cs1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/dd/yy")); + assertNotEquals(hash1, cs1.hashCode()); + } } /** @@ -173,252 +180,265 @@ public final class TestCellStyle extends * HSSFSheet last row or first row is incorrect. <P> * */ + @Test public void testWriteSheetStyle() throws IOException { - File file = TempFile.createTempFile("testWriteSheetStyle", - ".xls"); - FileOutputStream out = new FileOutputStream(file); - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); - HSSFRow r = null; - HSSFCell c = null; - HSSFFont fnt = wb.createFont(); - HSSFCellStyle cs = wb.createCellStyle(); - HSSFCellStyle cs2 = wb.createCellStyle(); - - cs.setBorderBottom(BorderStyle.THIN); - cs.setBorderLeft(BorderStyle.THIN); - cs.setBorderRight(BorderStyle.THIN); - cs.setBorderTop(BorderStyle.THIN); - cs.setFillForegroundColor(( short ) 0xA); - cs.setFillPattern(FillPatternType.DIAMONDS); - fnt.setColor(( short ) 0xf); - fnt.setItalic(true); - cs2.setFillForegroundColor(( short ) 0x0); - cs2.setFillPattern(FillPatternType.DIAMONDS); - cs2.setFont(fnt); - for (int rownum = 0; rownum < 100; rownum++) { - r = s.createRow(rownum); - - for (int cellnum = 0; cellnum < 50; cellnum += 2) { - c = r.createCell(cellnum); - c.setCellValue(rownum * 10000 + cellnum - + ((( double ) rownum / 1000) - + (( double ) cellnum / 10000))); - c.setCellStyle(cs); - c = r.createCell(cellnum + 1); - c.setCellValue("TEST"); - c.setCellStyle(cs2); + File file = TempFile.createTempFile("testWriteSheetStyle", ".xls"); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet s = wb.createSheet(); + HSSFRow r = null; + HSSFCell c = null; + HSSFFont fnt = wb.createFont(); + HSSFCellStyle cs = wb.createCellStyle(); + HSSFCellStyle cs2 = wb.createCellStyle(); + + cs.setBorderBottom(BorderStyle.THIN); + cs.setBorderLeft(BorderStyle.THIN); + cs.setBorderRight(BorderStyle.THIN); + cs.setBorderTop(BorderStyle.THIN); + cs.setFillForegroundColor((short) 0xA); + cs.setFillPattern(FillPatternType.DIAMONDS); + fnt.setColor((short) 0xf); + fnt.setItalic(true); + cs2.setFillForegroundColor((short) 0x0); + cs2.setFillPattern(FillPatternType.DIAMONDS); + cs2.setFont(fnt); + for (int rownum = 0; rownum < 100; rownum++) { + r = s.createRow(rownum); + + for (int cellnum = 0; cellnum < 50; cellnum += 2) { + c = r.createCell(cellnum); + c.setCellValue(rownum * 10000 + cellnum + + (((double) rownum / 1000) + + ((double) cellnum / 10000))); + c.setCellStyle(cs); + c = r.createCell(cellnum + 1); + c.setCellValue("TEST"); + c.setCellStyle(cs2); + } + } + try (FileOutputStream out = new FileOutputStream(file)) { + wb.write(out); } + SanityChecker sanityChecker = new SanityChecker(); + sanityChecker.checkHSSFWorkbook(wb); + assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); + assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); } - wb.write(out); - out.close(); - SanityChecker sanityChecker = new SanityChecker(); - sanityChecker.checkHSSFWorkbook(wb); - assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); - assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); } - + /** * Cloning one HSSFCellStyle onto Another, same * HSSFWorkbook */ - public void testCloneStyleSameWB() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFFont fnt = wb.createFont(); - fnt.setFontName("TestingFont"); - assertEquals(5, wb.getNumberOfFonts()); - - HSSFCellStyle orig = wb.createCellStyle(); - orig.setAlignment(HorizontalAlignment.JUSTIFY); - orig.setFont(fnt); - orig.setDataFormat((short)18); - - assertEquals(HorizontalAlignment.JUSTIFY, orig.getAlignment()); - assertEquals(fnt, orig.getFont(wb)); - assertEquals(18, orig.getDataFormat()); - - HSSFCellStyle clone = wb.createCellStyle(); - assertNotSame(HorizontalAlignment.RIGHT, clone.getAlignment()); - assertNotSame(fnt, clone.getFont(wb)); - assertFalse(18 == clone.getDataFormat()); - - clone.cloneStyleFrom(orig); - assertEquals(HorizontalAlignment.JUSTIFY, clone.getAlignment()); - assertEquals(fnt, clone.getFont(wb)); - assertEquals(18, clone.getDataFormat()); - assertEquals(5, wb.getNumberOfFonts()); + @Test + public void testCloneStyleSameWB() throws IOException { + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFFont fnt = wb.createFont(); + fnt.setFontName("TestingFont"); + assertEquals(5, wb.getNumberOfFonts()); + + HSSFCellStyle orig = wb.createCellStyle(); + orig.setAlignment(HorizontalAlignment.JUSTIFY); + orig.setFont(fnt); + orig.setDataFormat((short) 18); + + assertEquals(HorizontalAlignment.JUSTIFY, orig.getAlignment()); + assertEquals(fnt, orig.getFont(wb)); + assertEquals(18, orig.getDataFormat()); + + HSSFCellStyle clone = wb.createCellStyle(); + assertNotSame(HorizontalAlignment.RIGHT, clone.getAlignment()); + assertNotSame(fnt, clone.getFont(wb)); + assertNotEquals(18, clone.getDataFormat()); + + clone.cloneStyleFrom(orig); + assertEquals(HorizontalAlignment.JUSTIFY, clone.getAlignment()); + assertEquals(fnt, clone.getFont(wb)); + assertEquals(18, clone.getDataFormat()); + assertEquals(5, wb.getNumberOfFonts()); + } } - + /** * Cloning one HSSFCellStyle onto Another, across * two different HSSFWorkbooks */ - public void testCloneStyleDiffWB() { - HSSFWorkbook wbOrig = new HSSFWorkbook(); - - HSSFFont fnt = wbOrig.createFont(); - fnt.setFontName("TestingFont"); - assertEquals(5, wbOrig.getNumberOfFonts()); - - HSSFDataFormat fmt = wbOrig.createDataFormat(); - fmt.getFormat("MadeUpOne"); - fmt.getFormat("MadeUpTwo"); - - HSSFCellStyle orig = wbOrig.createCellStyle(); - orig.setAlignment(HorizontalAlignment.RIGHT); - orig.setFont(fnt); - orig.setDataFormat(fmt.getFormat("Test##")); - - assertEquals(HorizontalAlignment.RIGHT, orig.getAlignment()); - assertEquals(fnt, orig.getFont(wbOrig)); - assertEquals(fmt.getFormat("Test##"), orig.getDataFormat()); - - // Now a style on another workbook - HSSFWorkbook wbClone = new HSSFWorkbook(); - assertEquals(4, wbClone.getNumberOfFonts()); - HSSFDataFormat fmtClone = wbClone.createDataFormat(); - - HSSFCellStyle clone = wbClone.createCellStyle(); - assertEquals(4, wbClone.getNumberOfFonts()); - - assertNotSame(HorizontalAlignment.RIGHT, clone.getAlignment()); - assertNotSame("TestingFont", clone.getFont(wbClone).getFontName()); - - clone.cloneStyleFrom(orig); - assertEquals(HorizontalAlignment.RIGHT, clone.getAlignment()); - assertEquals("TestingFont", clone.getFont(wbClone).getFontName()); - assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat()); - assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##")); - assertEquals(5, wbClone.getNumberOfFonts()); - } - - public void testStyleNames() { - HSSFWorkbook wb = openSample("WithExtendedStyles.xls"); - HSSFSheet s = wb.getSheetAt(0); - HSSFCell c1 = s.getRow(0).getCell(0); - HSSFCell c2 = s.getRow(1).getCell(0); - HSSFCell c3 = s.getRow(2).getCell(0); - - HSSFCellStyle cs1 = c1.getCellStyle(); - HSSFCellStyle cs2 = c2.getCellStyle(); - HSSFCellStyle cs3 = c3.getCellStyle(); - - assertNotNull(cs1); - assertNotNull(cs2); - assertNotNull(cs3); - - // Check we got the styles we'd expect - assertEquals(10, cs1.getFont(wb).getFontHeightInPoints()); - assertEquals(9, cs2.getFont(wb).getFontHeightInPoints()); - assertEquals(12, cs3.getFont(wb).getFontHeightInPoints()); - - assertEquals(15, cs1.getIndex()); - assertEquals(23, cs2.getIndex()); - assertEquals(24, cs3.getIndex()); - - assertNull(cs1.getParentStyle()); - assertNotNull(cs2.getParentStyle()); - assertNotNull(cs3.getParentStyle()); - - assertEquals(21, cs2.getParentStyle().getIndex()); - assertEquals(22, cs3.getParentStyle().getIndex()); - - // Now check we can get style records for - // the parent ones - assertNull(wb.getWorkbook().getStyleRecord(15)); - assertNull(wb.getWorkbook().getStyleRecord(23)); - assertNull(wb.getWorkbook().getStyleRecord(24)); - - assertNotNull(wb.getWorkbook().getStyleRecord(21)); - assertNotNull(wb.getWorkbook().getStyleRecord(22)); - - // Now check the style names - assertNull(cs1.getUserStyleName()); - assertNull(cs2.getUserStyleName()); - assertNull(cs3.getUserStyleName()); - assertEquals("style1", cs2.getParentStyle().getUserStyleName()); - assertEquals("style2", cs3.getParentStyle().getUserStyleName()); - - // now apply a named style to a new cell - HSSFCell c4 = s.getRow(0).createCell(1); - c4.setCellStyle(cs2); - assertEquals("style1", c4.getCellStyle().getParentStyle().getUserStyleName()); - } - - public void testGetSetBorderHair() { - HSSFWorkbook wb = openSample("55341_CellStyleBorder.xls"); - HSSFSheet s = wb.getSheetAt(0); - HSSFCellStyle cs; - - cs = s.getRow(0).getCell(0).getCellStyle(); - assertEquals(BorderStyle.HAIR, cs.getBorderRight()); - - cs = s.getRow(1).getCell(1).getCellStyle(); - assertEquals(BorderStyle.DOTTED, cs.getBorderRight()); - - cs = s.getRow(2).getCell(2).getCellStyle(); - assertEquals(BorderStyle.DASH_DOT_DOT, cs.getBorderRight()); - - cs = s.getRow(3).getCell(3).getCellStyle(); - assertEquals(BorderStyle.DASHED, cs.getBorderRight()); - - cs = s.getRow(4).getCell(4).getCellStyle(); - assertEquals(BorderStyle.THIN, cs.getBorderRight()); - - cs = s.getRow(5).getCell(5).getCellStyle(); - assertEquals(BorderStyle.MEDIUM_DASH_DOT_DOT, cs.getBorderRight()); - - cs = s.getRow(6).getCell(6).getCellStyle(); - assertEquals(BorderStyle.SLANTED_DASH_DOT, cs.getBorderRight()); - - cs = s.getRow(7).getCell(7).getCellStyle(); - assertEquals(BorderStyle.MEDIUM_DASH_DOT, cs.getBorderRight()); - - cs = s.getRow(8).getCell(8).getCellStyle(); - assertEquals(BorderStyle.MEDIUM_DASHED, cs.getBorderRight()); - - cs = s.getRow(9).getCell(9).getCellStyle(); - assertEquals(BorderStyle.MEDIUM, cs.getBorderRight()); + @Test + public void testCloneStyleDiffWB() throws IOException { + try (HSSFWorkbook wbOrig = new HSSFWorkbook()) { + + HSSFFont fnt = wbOrig.createFont(); + fnt.setFontName("TestingFont"); + assertEquals(5, wbOrig.getNumberOfFonts()); + + HSSFDataFormat fmt = wbOrig.createDataFormat(); + fmt.getFormat("MadeUpOne"); + fmt.getFormat("MadeUpTwo"); + + HSSFCellStyle orig = wbOrig.createCellStyle(); + orig.setAlignment(HorizontalAlignment.RIGHT); + orig.setFont(fnt); + orig.setDataFormat(fmt.getFormat("Test##")); + + assertEquals(HorizontalAlignment.RIGHT, orig.getAlignment()); + assertEquals(fnt, orig.getFont(wbOrig)); + assertEquals(fmt.getFormat("Test##"), orig.getDataFormat()); + + // Now a style on another workbook + try (HSSFWorkbook wbClone = new HSSFWorkbook()) { + assertEquals(4, wbClone.getNumberOfFonts()); + HSSFDataFormat fmtClone = wbClone.createDataFormat(); + + HSSFCellStyle clone = wbClone.createCellStyle(); + assertEquals(4, wbClone.getNumberOfFonts()); + + assertNotSame(HorizontalAlignment.RIGHT, clone.getAlignment()); + assertNotSame("TestingFont", clone.getFont(wbClone).getFontName()); + + clone.cloneStyleFrom(orig); + assertEquals(HorizontalAlignment.RIGHT, clone.getAlignment()); + assertEquals("TestingFont", clone.getFont(wbClone).getFontName()); + assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat()); + assertNotEquals(fmtClone.getFormat("Test##"), fmt.getFormat("Test##")); + assertEquals(5, wbClone.getNumberOfFonts()); + } + } + } + + @Test + public void testStyleNames() throws IOException { + try (HSSFWorkbook wb = openSample("WithExtendedStyles.xls")) { + HSSFSheet s = wb.getSheetAt(0); + HSSFCell c1 = s.getRow(0).getCell(0); + HSSFCell c2 = s.getRow(1).getCell(0); + HSSFCell c3 = s.getRow(2).getCell(0); + + HSSFCellStyle cs1 = c1.getCellStyle(); + HSSFCellStyle cs2 = c2.getCellStyle(); + HSSFCellStyle cs3 = c3.getCellStyle(); + + assertNotNull(cs1); + assertNotNull(cs2); + assertNotNull(cs3); + + // Check we got the styles we'd expect + assertEquals(10, cs1.getFont(wb).getFontHeightInPoints()); + assertEquals(9, cs2.getFont(wb).getFontHeightInPoints()); + assertEquals(12, cs3.getFont(wb).getFontHeightInPoints()); + + assertEquals(15, cs1.getIndex()); + assertEquals(23, cs2.getIndex()); + assertEquals(24, cs3.getIndex()); + + assertNull(cs1.getParentStyle()); + assertNotNull(cs2.getParentStyle()); + assertNotNull(cs3.getParentStyle()); + + assertEquals(21, cs2.getParentStyle().getIndex()); + assertEquals(22, cs3.getParentStyle().getIndex()); + + // Now check we can get style records for the parent ones + assertNull(wb.getWorkbook().getStyleRecord(15)); + assertNull(wb.getWorkbook().getStyleRecord(23)); + assertNull(wb.getWorkbook().getStyleRecord(24)); + + assertNotNull(wb.getWorkbook().getStyleRecord(21)); + assertNotNull(wb.getWorkbook().getStyleRecord(22)); + + // Now check the style names + assertNull(cs1.getUserStyleName()); + assertNull(cs2.getUserStyleName()); + assertNull(cs3.getUserStyleName()); + assertEquals("style1", cs2.getParentStyle().getUserStyleName()); + assertEquals("style2", cs3.getParentStyle().getUserStyleName()); + + // now apply a named style to a new cell + HSSFCell c4 = s.getRow(0).createCell(1); + c4.setCellStyle(cs2); + assertEquals("style1", c4.getCellStyle().getParentStyle().getUserStyleName()); + } + } + + @Test + public void testGetSetBorderHair() throws IOException { + try (HSSFWorkbook wb = openSample("55341_CellStyleBorder.xls")) { + HSSFSheet s = wb.getSheetAt(0); + HSSFCellStyle cs; + + cs = s.getRow(0).getCell(0).getCellStyle(); + assertEquals(BorderStyle.HAIR, cs.getBorderRight()); + + cs = s.getRow(1).getCell(1).getCellStyle(); + assertEquals(BorderStyle.DOTTED, cs.getBorderRight()); + + cs = s.getRow(2).getCell(2).getCellStyle(); + assertEquals(BorderStyle.DASH_DOT_DOT, cs.getBorderRight()); + + cs = s.getRow(3).getCell(3).getCellStyle(); + assertEquals(BorderStyle.DASHED, cs.getBorderRight()); - cs = s.getRow(10).getCell(10).getCellStyle(); - assertEquals(BorderStyle.THICK, cs.getBorderRight()); + cs = s.getRow(4).getCell(4).getCellStyle(); + assertEquals(BorderStyle.THIN, cs.getBorderRight()); - cs = s.getRow(11).getCell(11).getCellStyle(); - assertEquals(BorderStyle.DOUBLE, cs.getBorderRight()); + cs = s.getRow(5).getCell(5).getCellStyle(); + assertEquals(BorderStyle.MEDIUM_DASH_DOT_DOT, cs.getBorderRight()); + + cs = s.getRow(6).getCell(6).getCellStyle(); + assertEquals(BorderStyle.SLANTED_DASH_DOT, cs.getBorderRight()); + + cs = s.getRow(7).getCell(7).getCellStyle(); + assertEquals(BorderStyle.MEDIUM_DASH_DOT, cs.getBorderRight()); + + cs = s.getRow(8).getCell(8).getCellStyle(); + assertEquals(BorderStyle.MEDIUM_DASHED, cs.getBorderRight()); + + cs = s.getRow(9).getCell(9).getCellStyle(); + assertEquals(BorderStyle.MEDIUM, cs.getBorderRight()); + + cs = s.getRow(10).getCell(10).getCellStyle(); + assertEquals(BorderStyle.THICK, cs.getBorderRight()); + + cs = s.getRow(11).getCell(11).getCellStyle(); + assertEquals(BorderStyle.DOUBLE, cs.getBorderRight()); + } } - public void testShrinkToFit() { + @Test + public void testShrinkToFit() throws IOException { // Existing file - HSSFWorkbook wb = openSample("ShrinkToFit.xls"); - HSSFSheet s = wb.getSheetAt(0); - HSSFRow r = s.getRow(0); - HSSFCellStyle cs = r.getCell(0).getCellStyle(); - - assertTrue(cs.getShrinkToFit()); - - // New file - HSSFWorkbook wbOrig = new HSSFWorkbook(); - s = wbOrig.createSheet(); - r = s.createRow(0); - - cs = wbOrig.createCellStyle(); - cs.setShrinkToFit(false); - r.createCell(0).setCellStyle(cs); - - cs = wbOrig.createCellStyle(); - cs.setShrinkToFit(true); - r.createCell(1).setCellStyle(cs); - - // Write out, read, and check - wb = HSSFTestDataSamples.writeOutAndReadBack(wbOrig); - s = wb.getSheetAt(0); - r = s.getRow(0); - assertFalse(r.getCell(0).getCellStyle().getShrinkToFit()); - assertTrue(r.getCell(1).getCellStyle().getShrinkToFit()); - } - - - + try (HSSFWorkbook wb1 = openSample("ShrinkToFit.xls")) { + HSSFSheet s = wb1.getSheetAt(0); + HSSFRow r = s.getRow(0); + HSSFCellStyle cs = r.getCell(0).getCellStyle(); + + assertTrue(cs.getShrinkToFit()); + } + + // New file + try (HSSFWorkbook wbOrig = new HSSFWorkbook()) { + HSSFSheet s = wbOrig.createSheet(); + HSSFRow r = s.createRow(0); + + HSSFCellStyle cs = wbOrig.createCellStyle(); + cs.setShrinkToFit(false); + r.createCell(0).setCellStyle(cs); + + cs = wbOrig.createCellStyle(); + cs.setShrinkToFit(true); + r.createCell(1).setCellStyle(cs); + + // Write out, read, and check + try (HSSFWorkbook wb = HSSFTestDataSamples.writeOutAndReadBack(wbOrig)) { + s = wb.getSheetAt(0); + r = s.getRow(0); + assertFalse(r.getCell(0).getCellStyle().getShrinkToFit()); + assertTrue(r.getCell(1).getCellStyle().getShrinkToFit()); + } + } + } + + + private static class CellFormatBugExample extends Thread { private final String fileName; private Throwable exception; @@ -431,18 +451,17 @@ public final class TestCellStyle extends public void run() { try { for(int i = 0;i< 10;i++) { - Workbook wb = HSSFTestDataSamples.openSampleWorkbook(fileName); - Sheet sheet = wb.getSheetAt(0); - - for (Row row : sheet) { - for (Integer idxCell = 0; idxCell < row.getLastCellNum(); idxCell++) { - - Cell cell = row.getCell(idxCell); - cell.getCellStyle().getDataFormatString(); - if (cell.getCellType() == CellType.NUMERIC) { - boolean isDate = DateUtil.isCellDateFormatted(cell); - if (idxCell > 0 && isDate) { - fail("cell " + idxCell + " is not a date: " + idxCell); + try (Workbook wb = HSSFTestDataSamples.openSampleWorkbook(fileName)) { + Sheet sheet = wb.getSheetAt(0); + + for (Row row : sheet) { + for (Integer idxCell = 0; idxCell < row.getLastCellNum(); idxCell++) { + + Cell cell = row.getCell(idxCell); + cell.getCellStyle().getDataFormatString(); + if (cell.getCellType() == CellType.NUMERIC) { + boolean isDate = DateUtil.isCellDateFormatted(cell); + assertFalse("cell " + idxCell + " is not a date.", idxCell > 0 && isDate); } } } @@ -458,15 +477,16 @@ public final class TestCellStyle extends } } + @Test public void test56563() throws Throwable { CellFormatBugExample threadA = new CellFormatBugExample("56563a.xls"); threadA.start(); CellFormatBugExample threadB = new CellFormatBugExample("56563b.xls"); threadB.start(); - + threadA.join(); threadB.join(); - + if(threadA.getException() != null) { throw threadA.getException(); } @@ -474,79 +494,73 @@ public final class TestCellStyle extends throw threadB.getException(); } } - + + @Test public void test56959() throws IOException { - Workbook wb = new HSSFWorkbook(); - Sheet sheet = wb.createSheet("somesheet"); - - Row row = sheet.createRow(0); - - // Create a new font and alter it. - Font font = wb.createFont(); - font.setFontHeightInPoints((short)24); - font.setFontName("Courier New"); - font.setItalic(true); - font.setStrikeout(true); - font.setColor(Font.COLOR_RED); - - CellStyle style = wb.createCellStyle(); - style.setBorderBottom(BorderStyle.DOTTED); - style.setFont(font); - - Cell cell = row.createCell(0); - cell.setCellStyle(style); - cell.setCellValue("testtext"); - - Cell newCell = row.createCell(1); - - newCell.setCellStyle(style); - newCell.setCellValue("2testtext2"); - - CellStyle newStyle = newCell.getCellStyle(); - assertEquals(BorderStyle.DOTTED, newStyle.getBorderBottom()); - assertEquals(Font.COLOR_RED, ((HSSFCellStyle)newStyle).getFont(wb).getColor()); - -// OutputStream out = new FileOutputStream("/tmp/56959.xls"); -// try { -// wb.write(out); -// } finally { -// out.close(); -// } + try (Workbook wb = new HSSFWorkbook()) { + Sheet sheet = wb.createSheet("somesheet"); + + Row row = sheet.createRow(0); + + // Create a new font and alter it. + Font font = wb.createFont(); + font.setFontHeightInPoints((short) 24); + font.setFontName("Courier New"); + font.setItalic(true); + font.setStrikeout(true); + font.setColor(Font.COLOR_RED); + + CellStyle style = wb.createCellStyle(); + style.setBorderBottom(BorderStyle.DOTTED); + style.setFont(font); + + Cell cell = row.createCell(0); + cell.setCellStyle(style); + cell.setCellValue("testtext"); + + Cell newCell = row.createCell(1); + + newCell.setCellStyle(style); + newCell.setCellValue("2testtext2"); + + CellStyle newStyle = newCell.getCellStyle(); + assertEquals(BorderStyle.DOTTED, newStyle.getBorderBottom()); + assertEquals(Font.COLOR_RED, ((HSSFCellStyle) newStyle).getFont(wb).getColor()); + } } @Test public void test58043() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFCellStyle cellStyle = wb.createCellStyle(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFCellStyle cellStyle = wb.createCellStyle(); - assertEquals(0, cellStyle.getRotation()); + assertEquals(0, cellStyle.getRotation()); - cellStyle.setRotation((short)89); - assertEquals(89, cellStyle.getRotation()); - - cellStyle.setRotation((short)90); - assertEquals(90, cellStyle.getRotation()); - - cellStyle.setRotation((short)-1); - assertEquals(-1, cellStyle.getRotation()); - - cellStyle.setRotation((short)-89); - assertEquals(-89, cellStyle.getRotation()); - - cellStyle.setRotation((short)-90); - assertEquals(-90, cellStyle.getRotation()); - - cellStyle.setRotation((short)-89); - assertEquals(-89, cellStyle.getRotation()); - - // values above 90 are mapped to the correct values for compatibility between HSSF and XSSF - cellStyle.setRotation((short)179); - assertEquals(-89, cellStyle.getRotation()); - - cellStyle.setRotation((short)180); - assertEquals(-90, cellStyle.getRotation()); - - wb.close(); + cellStyle.setRotation((short) 89); + assertEquals(89, cellStyle.getRotation()); + + cellStyle.setRotation((short) 90); + assertEquals(90, cellStyle.getRotation()); + + cellStyle.setRotation((short) -1); + assertEquals(-1, cellStyle.getRotation()); + + cellStyle.setRotation((short) -89); + assertEquals(-89, cellStyle.getRotation()); + + cellStyle.setRotation((short) -90); + assertEquals(-90, cellStyle.getRotation()); + + cellStyle.setRotation((short) -89); + assertEquals(-89, cellStyle.getRotation()); + + // values above 90 are mapped to the correct values for compatibility between HSSF and XSSF + cellStyle.setRotation((short) 179); + assertEquals(-89, cellStyle.getRotation()); + + cellStyle.setRotation((short) 180); + assertEquals(-90, cellStyle.getRotation()); + } } } Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestEmbeddedObjects.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestEmbeddedObjects.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestEmbeddedObjects.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestEmbeddedObjects.java Fri Dec 27 23:00:13 2019 @@ -17,46 +17,55 @@ package org.apache.poi.hssf.usermodel; -import junit.framework.TestCase; -import org.apache.poi.hssf.HSSFTestDataSamples; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import java.io.IOException; import java.util.List; +import org.apache.poi.hssf.HSSFTestDataSamples; +import org.junit.Test; + /** * Tests for the embedded object fetching support in HSSF */ -public class TestEmbeddedObjects extends TestCase{ +public class TestEmbeddedObjects { + @Test public void testReadExistingObject() throws IOException { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls"); - List<HSSFObjectData> list = wb.getAllEmbeddedObjects(); - assertEquals(list.size(), 1); - HSSFObjectData obj = list.get(0); - assertNotNull(obj.getObjectData()); - assertNotNull(obj.getDirectory()); - assertNotNull(obj.getOLE2ClassName()); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls")) { + List<HSSFObjectData> list = wb.getAllEmbeddedObjects(); + assertEquals(list.size(), 1); + HSSFObjectData obj = list.get(0); + assertNotNull(obj.getObjectData()); + assertNotNull(obj.getDirectory()); + assertNotNull(obj.getOLE2ClassName()); + } } - + /** * Need to recurse into the shapes to find this one * See https://github.com/apache/poi/pull/2 */ + @Test public void testReadNestedObject() throws IOException { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithCheckBoxes.xls"); - List<HSSFObjectData> list = wb.getAllEmbeddedObjects(); - assertEquals(list.size(), 1); - HSSFObjectData obj = list.get(0); - assertNotNull(obj.getObjectData()); - assertNotNull(obj.getOLE2ClassName()); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithCheckBoxes.xls")) { + List<HSSFObjectData> list = wb.getAllEmbeddedObjects(); + assertEquals(list.size(), 1); + HSSFObjectData obj = list.get(0); + assertNotNull(obj.getObjectData()); + assertNotNull(obj.getOLE2ClassName()); + } } - + /** * One with large numbers of recursivly embedded resources * See https://github.com/apache/poi/pull/2 */ + @Test public void testReadManyNestedObjects() throws IOException { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("45538_form_Header.xls"); - List<HSSFObjectData> list = wb.getAllEmbeddedObjects(); - assertEquals(list.size(), 40); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("45538_form_Header.xls")) { + List<HSSFObjectData> list = wb.getAllEmbeddedObjects(); + assertEquals(list.size(), 40); + } } } Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestExternalReferenceChange.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestExternalReferenceChange.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestExternalReferenceChange.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestExternalReferenceChange.java Fri Dec 27 23:00:13 2019 @@ -16,53 +16,52 @@ ==================================================================== */ package org.apache.poi.hssf.usermodel; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import java.io.IOException; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.ss.usermodel.CellType; +import org.junit.Test; -import junit.framework.TestCase; - -public class TestExternalReferenceChange extends TestCase { +public class TestExternalReferenceChange { private static final String MAIN_WORKBOOK_FILENAME = "52575_main.xls"; private static final String SOURCE_DUMMY_WORKBOOK_FILENAME = "source_dummy.xls"; private static final String SOURCE_WORKBOOK_FILENAME = "52575_source.xls"; - - private HSSFWorkbook mainWorkbook; - private HSSFWorkbook sourceWorkbook; - - @Override - protected void setUp() throws Exception { - mainWorkbook = HSSFTestDataSamples.openSampleWorkbook(MAIN_WORKBOOK_FILENAME); - sourceWorkbook = HSSFTestDataSamples.openSampleWorkbook(SOURCE_WORKBOOK_FILENAME); - - assertNotNull(mainWorkbook); - assertNotNull(sourceWorkbook); - } - + + @Test public void testDummyToSource() throws IOException { - boolean changed = mainWorkbook.changeExternalReference("DOESNOTEXIST", SOURCE_WORKBOOK_FILENAME); - assertFalse(changed); - - changed = mainWorkbook.changeExternalReference(SOURCE_DUMMY_WORKBOOK_FILENAME, SOURCE_WORKBOOK_FILENAME); - assertTrue(changed); - - HSSFSheet lSheet = mainWorkbook.getSheetAt(0); - HSSFCell lA1Cell = lSheet.getRow(0).getCell(0); - - assertEquals(CellType.FORMULA, lA1Cell.getCellType()); - - HSSFFormulaEvaluator lMainWorkbookEvaluator = new HSSFFormulaEvaluator(mainWorkbook); - HSSFFormulaEvaluator lSourceEvaluator = new HSSFFormulaEvaluator(sourceWorkbook); - HSSFFormulaEvaluator.setupEnvironment( - new String[]{MAIN_WORKBOOK_FILENAME, SOURCE_WORKBOOK_FILENAME}, - new HSSFFormulaEvaluator[] {lMainWorkbookEvaluator, lSourceEvaluator}); - - assertEquals(CellType.NUMERIC, lMainWorkbookEvaluator.evaluateFormulaCell(lA1Cell)); + try (HSSFWorkbook mainWorkbook = HSSFTestDataSamples.openSampleWorkbook(MAIN_WORKBOOK_FILENAME); + HSSFWorkbook sourceWorkbook = HSSFTestDataSamples.openSampleWorkbook(SOURCE_WORKBOOK_FILENAME)) { + + assertNotNull(mainWorkbook); + assertNotNull(sourceWorkbook); + + boolean changed = mainWorkbook.changeExternalReference("DOESNOTEXIST", SOURCE_WORKBOOK_FILENAME); + assertFalse(changed); - assertEquals(20.0d, lA1Cell.getNumericCellValue(), 0.00001d); + changed = mainWorkbook.changeExternalReference(SOURCE_DUMMY_WORKBOOK_FILENAME, SOURCE_WORKBOOK_FILENAME); + assertTrue(changed); + HSSFSheet lSheet = mainWorkbook.getSheetAt(0); + HSSFCell lA1Cell = lSheet.getRow(0).getCell(0); + + assertEquals(CellType.FORMULA, lA1Cell.getCellType()); + + HSSFFormulaEvaluator lMainWorkbookEvaluator = new HSSFFormulaEvaluator(mainWorkbook); + HSSFFormulaEvaluator lSourceEvaluator = new HSSFFormulaEvaluator(sourceWorkbook); + HSSFFormulaEvaluator.setupEnvironment( + new String[]{MAIN_WORKBOOK_FILENAME, SOURCE_WORKBOOK_FILENAME}, + new HSSFFormulaEvaluator[]{lMainWorkbookEvaluator, lSourceEvaluator}); + + assertEquals(CellType.NUMERIC, lMainWorkbookEvaluator.evaluateFormulaCell(lA1Cell)); + + assertEquals(20.0d, lA1Cell.getNumericCellValue(), 0.00001d); + } } - + } Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestFontDetails.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestFontDetails.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestFontDetails.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestFontDetails.java Fri Dec 27 23:00:13 2019 @@ -17,38 +17,41 @@ package org.apache.poi.hssf.usermodel; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; import java.util.Properties; +import org.junit.Before; +import org.junit.Test; + /** * Tests the implementation of the FontDetails class. - * - * @author Glen Stampoultzis (glens at apache.org) */ -public final class TestFontDetails extends TestCase { - private Properties properties; +public final class TestFontDetails { private FontDetails fontDetails; - @Override - protected void setUp() { - properties = new Properties(); + @Before + public void setUp() { + Properties properties = new Properties(); properties.setProperty("font.Arial.height", "13"); properties.setProperty("font.Arial.characters", "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "); - properties.setProperty("font.Arial.widths", "6, 6, 6, 6, 6, 3, 6, 6, 3, 4, 6, 3, 9, 6, 6, 6, 6, 4, 6, 3, 6, 7, 9, 6, 5, 5, 7, 7, 7, 7, 7, 6, 8, 7, 3, 6, 7, 6, 9, 7, 8, 7, 8, 7, 7, 5, 7, 7, 9, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, "); + properties.setProperty("font.Arial.widths", "6, 6, 6, 6, 6, 3, 6, 6, 3, 4, 6, 3, 9, 6, 6, 6, 6, 4, 6, 3, 6, 7, 9, 6, 5, 5, 7, 7, 7, 7, 7, 6, 8, 7, 3, 6, 7, 6, 9, 7, 8, 7, 8, 7, 7, 5, 7, 7, 9, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, "); fontDetails = FontDetails.create("Arial", properties); } + @Test public void testCreate() { assertEquals(13, fontDetails.getHeight()); assertEquals(6, fontDetails.getCharWidth('a')); assertEquals(3, fontDetails.getCharWidth('f')); } + @Test public void testGetStringWidth() { assertEquals(9, fontDetails.getStringWidth("af")); } + @Test public void testGetCharWidth() { assertEquals(6, fontDetails.getCharWidth('a')); assertEquals(9, fontDetails.getCharWidth('=')); Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorDocs.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorDocs.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorDocs.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorDocs.java Fri Dec 27 23:00:13 2019 @@ -17,107 +17,112 @@ package org.apache.poi.hssf.usermodel; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; - -import junit.framework.TestCase; +import org.junit.Test; /** * Tests to show that our documentation at * http://poi.apache.org/hssf/eval.html * all actually works as we'd expect them to */ -public final class TestFormulaEvaluatorDocs extends TestCase { +public final class TestFormulaEvaluatorDocs { /** * http://poi.apache.org/hssf/eval.html#EvaluateAll */ - public void testEvaluateAll() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s1 = wb.createSheet(); - HSSFSheet s2 = wb.createSheet(); - wb.setSheetName(0, "S1"); - wb.setSheetName(1, "S2"); - - HSSFRow s1r1 = s1.createRow(0); - HSSFRow s1r2 = s1.createRow(1); - HSSFRow s2r1 = s2.createRow(0); - - HSSFCell s1r1c1 = s1r1.createCell(0); - HSSFCell s1r1c2 = s1r1.createCell(1); - HSSFCell s1r1c3 = s1r1.createCell(2); - s1r1c1.setCellValue(22.3); - s1r1c2.setCellValue(33.4); - s1r1c3.setCellFormula("SUM(A1:B1)"); - - HSSFCell s1r2c1 = s1r2.createCell(0); - HSSFCell s1r2c2 = s1r2.createCell(1); - HSSFCell s1r2c3 = s1r2.createCell(2); - s1r2c1.setCellValue(-1.2); - s1r2c2.setCellValue(-3.4); - s1r2c3.setCellFormula("SUM(A2:B2)"); - - HSSFCell s2r1c1 = s2r1.createCell(0); - s2r1c1.setCellFormula("S1!A1"); - - // Not evaluated yet - assertEquals(0.0, s1r1c3.getNumericCellValue(), 0); - assertEquals(0.0, s1r2c3.getNumericCellValue(), 0); - assertEquals(0.0, s2r1c1.getNumericCellValue(), 0); - - // Do a full evaluate, as per our docs - // uses evaluateFormulaCell() - for(Sheet sheet : wb) { - HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb); - for(Row r : sheet) { - for(Cell c : r) { - if(c.getCellType() == CellType.FORMULA) { - evaluator.evaluateFormulaCell(c); - - // For testing - all should be numeric - assertEquals(CellType.NUMERIC, evaluator.evaluateFormulaCell(c)); + @Test + public void testEvaluateAll() throws IOException { + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet s1 = wb.createSheet(); + HSSFSheet s2 = wb.createSheet(); + wb.setSheetName(0, "S1"); + wb.setSheetName(1, "S2"); + + HSSFRow s1r1 = s1.createRow(0); + HSSFRow s1r2 = s1.createRow(1); + HSSFRow s2r1 = s2.createRow(0); + + HSSFCell s1r1c1 = s1r1.createCell(0); + HSSFCell s1r1c2 = s1r1.createCell(1); + HSSFCell s1r1c3 = s1r1.createCell(2); + s1r1c1.setCellValue(22.3); + s1r1c2.setCellValue(33.4); + s1r1c3.setCellFormula("SUM(A1:B1)"); + + HSSFCell s1r2c1 = s1r2.createCell(0); + HSSFCell s1r2c2 = s1r2.createCell(1); + HSSFCell s1r2c3 = s1r2.createCell(2); + s1r2c1.setCellValue(-1.2); + s1r2c2.setCellValue(-3.4); + s1r2c3.setCellFormula("SUM(A2:B2)"); + + HSSFCell s2r1c1 = s2r1.createCell(0); + s2r1c1.setCellFormula("S1!A1"); + + // Not evaluated yet + assertEquals(0.0, s1r1c3.getNumericCellValue(), 0); + assertEquals(0.0, s1r2c3.getNumericCellValue(), 0); + assertEquals(0.0, s2r1c1.getNumericCellValue(), 0); + + // Do a full evaluate, as per our docs + // uses evaluateFormulaCell() + for (Sheet sheet : wb) { + HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb); + for (Row r : sheet) { + for (Cell c : r) { + if (c.getCellType() == CellType.FORMULA) { + evaluator.evaluateFormulaCell(c); + + // For testing - all should be numeric + assertEquals(CellType.NUMERIC, evaluator.evaluateFormulaCell(c)); + } } } } - } - - // Check now as expected - assertEquals(55.7, wb.getSheetAt(0).getRow(0).getCell(2).getNumericCellValue(), 0); - assertEquals("SUM(A1:B1)", wb.getSheetAt(0).getRow(0).getCell(2).getCellFormula()); - assertEquals(CellType.FORMULA, wb.getSheetAt(0).getRow(0).getCell(2).getCellType()); - - assertEquals(-4.6, wb.getSheetAt(0).getRow(1).getCell(2).getNumericCellValue(), 0); - assertEquals("SUM(A2:B2)", wb.getSheetAt(0).getRow(1).getCell(2).getCellFormula()); - assertEquals(CellType.FORMULA, wb.getSheetAt(0).getRow(1).getCell(2).getCellType()); - - assertEquals(22.3, wb.getSheetAt(1).getRow(0).getCell(0).getNumericCellValue(), 0); - assertEquals("'S1'!A1", wb.getSheetAt(1).getRow(0).getCell(0).getCellFormula()); - assertEquals(CellType.FORMULA, wb.getSheetAt(1).getRow(0).getCell(0).getCellType()); - - - // Now do the alternate call, which zaps the formulas - // uses evaluateInCell() - for(Sheet sheet : wb) { - HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb); - - for(Row r : sheet) { - for(Cell c : r) { - if(c.getCellType() == CellType.FORMULA) { - evaluator.evaluateInCell(c); + + // Check now as expected + assertEquals(55.7, wb.getSheetAt(0).getRow(0).getCell(2).getNumericCellValue(), 0); + assertEquals("SUM(A1:B1)", wb.getSheetAt(0).getRow(0).getCell(2).getCellFormula()); + assertEquals(CellType.FORMULA, wb.getSheetAt(0).getRow(0).getCell(2).getCellType()); + + assertEquals(-4.6, wb.getSheetAt(0).getRow(1).getCell(2).getNumericCellValue(), 0); + assertEquals("SUM(A2:B2)", wb.getSheetAt(0).getRow(1).getCell(2).getCellFormula()); + assertEquals(CellType.FORMULA, wb.getSheetAt(0).getRow(1).getCell(2).getCellType()); + + assertEquals(22.3, wb.getSheetAt(1).getRow(0).getCell(0).getNumericCellValue(), 0); + assertEquals("'S1'!A1", wb.getSheetAt(1).getRow(0).getCell(0).getCellFormula()); + assertEquals(CellType.FORMULA, wb.getSheetAt(1).getRow(0).getCell(0).getCellType()); + + + // Now do the alternate call, which zaps the formulas + // uses evaluateInCell() + for (Sheet sheet : wb) { + HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb); + + for (Row r : sheet) { + for (Cell c : r) { + if (c.getCellType() == CellType.FORMULA) { + evaluator.evaluateInCell(c); + } } } } + + assertEquals(55.7, wb.getSheetAt(0).getRow(0).getCell(2).getNumericCellValue(), 0); + assertEquals(CellType.NUMERIC, wb.getSheetAt(0).getRow(0).getCell(2).getCellType()); + + assertEquals(-4.6, wb.getSheetAt(0).getRow(1).getCell(2).getNumericCellValue(), 0); + assertEquals(CellType.NUMERIC, wb.getSheetAt(0).getRow(1).getCell(2).getCellType()); + + assertEquals(22.3, wb.getSheetAt(1).getRow(0).getCell(0).getNumericCellValue(), 0); + assertEquals(CellType.NUMERIC, wb.getSheetAt(1).getRow(0).getCell(0).getCellType()); } - - assertEquals(55.7, wb.getSheetAt(0).getRow(0).getCell(2).getNumericCellValue(), 0); - assertEquals(CellType.NUMERIC, wb.getSheetAt(0).getRow(0).getCell(2).getCellType()); - - assertEquals(-4.6, wb.getSheetAt(0).getRow(1).getCell(2).getNumericCellValue(), 0); - assertEquals(CellType.NUMERIC, wb.getSheetAt(0).getRow(1).getCell(2).getCellType()); - - assertEquals(22.3, wb.getSheetAt(1).getRow(0).getCell(0).getNumericCellValue(), 0); - assertEquals(CellType.NUMERIC, wb.getSheetAt(1).getRow(0).getCell(0).getCellType()); } } Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java Fri Dec 27 23:00:13 2019 @@ -20,13 +20,15 @@ package org.apache.poi.hssf.usermodel; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import java.io.IOException; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import java.util.List; +import java.util.ListIterator; import org.apache.poi.hssf.HSSFITestDataProvider; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -67,22 +69,22 @@ public final class TestHSSFCell extends Date date = cal.getTime(); // first check a file with 1900 Date Windowing - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("1900DateWindowing.xls"); - HSSFSheet sheet = wb.getSheetAt(0); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("1900DateWindowing.xls")) { + HSSFSheet sheet = wb.getSheetAt(0); - assertEquals("Date from file using 1900 Date Windowing", - date.getTime(), - sheet.getRow(0).getCell(0).getDateCellValue().getTime()); - wb.close(); + assertEquals("Date from file using 1900 Date Windowing", + date.getTime(), + sheet.getRow(0).getCell(0).getDateCellValue().getTime()); + } // now check a file with 1904 Date Windowing - wb = HSSFTestDataSamples.openSampleWorkbook("1904DateWindowing.xls"); - sheet = wb.getSheetAt(0); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("1904DateWindowing.xls")) { + HSSFSheet sheet = wb.getSheetAt(0); - assertEquals("Date from file using 1904 Date Windowing", - date.getTime(), - sheet.getRow(0).getCell(0).getDateCellValue().getTime()); - wb.close(); + assertEquals("Date from file using 1904 Date Windowing", + date.getTime(), + sheet.getRow(0).getCell(0).getDateCellValue().getTime()); + } } @@ -99,26 +101,26 @@ public final class TestHSSFCell extends Date date = cal.getTime(); // first check a file with 1900 Date Windowing - HSSFWorkbook wb1 = HSSFTestDataSamples.openSampleWorkbook("1900DateWindowing.xls"); + try (HSSFWorkbook wb1 = HSSFTestDataSamples.openSampleWorkbook("1900DateWindowing.xls")) { - setCell(wb1, 0, 1, date); - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + setCell(wb1, 0, 1, date); + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { - assertEquals("Date from file using 1900 Date Windowing", - date.getTime(), - readCell(wb2, 0, 1).getTime()); - wb1.close(); - wb2.close(); + assertEquals("Date from file using 1900 Date Windowing", + date.getTime(), + readCell(wb2, 0, 1).getTime()); + } + } // now check a file with 1904 Date Windowing - wb1 = HSSFTestDataSamples.openSampleWorkbook("1904DateWindowing.xls"); - setCell(wb1, 0, 1, date); - wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - assertEquals("Date from file using 1900 Date Windowing", - date.getTime(), - readCell(wb2, 0, 1).getTime()); - wb1.close(); - wb2.close(); + try (HSSFWorkbook wb1 = HSSFTestDataSamples.openSampleWorkbook("1904DateWindowing.xls")) { + setCell(wb1, 0, 1, date); + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + assertEquals("Date from file using 1900 Date Windowing", + date.getTime(), + readCell(wb2, 0, 1).getTime()); + } + } } private static void setCell(HSSFWorkbook workbook, int rowIdx, int colIdx, Date date) { @@ -145,93 +147,75 @@ public final class TestHSSFCell extends @Test public void testActiveCell() throws IOException { //read in sample - HSSFWorkbook wb1 = HSSFTestDataSamples.openSampleWorkbook("Simple.xls"); + try (HSSFWorkbook wb1 = HSSFTestDataSamples.openSampleWorkbook("Simple.xls")) { - //check initial position - HSSFSheet umSheet = wb1.getSheetAt(0); - InternalSheet s = umSheet.getSheet(); - assertEquals("Initial active cell should be in col 0", - (short) 0, s.getActiveCellCol()); - assertEquals("Initial active cell should be on row 1", - 1, s.getActiveCellRow()); - - //modify position through HSSFCell - HSSFCell cell = umSheet.createRow(3).createCell(2); - cell.setAsActiveCell(); - assertEquals("After modify, active cell should be in col 2", - (short) 2, s.getActiveCellCol()); - assertEquals("After modify, active cell should be on row 3", - 3, s.getActiveCellRow()); - - //write book to temp file; read and verify that position is serialized - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - - umSheet = wb2.getSheetAt(0); - s = umSheet.getSheet(); - - assertEquals("After serialize, active cell should be in col 2", - (short) 2, s.getActiveCellCol()); - assertEquals("After serialize, active cell should be on row 3", - 3, s.getActiveCellRow()); - - wb2.close(); + //check initial position + HSSFSheet umSheet = wb1.getSheetAt(0); + InternalSheet s = umSheet.getSheet(); + assertEquals("Initial active cell should be in col 0", + (short) 0, s.getActiveCellCol()); + assertEquals("Initial active cell should be on row 1", + 1, s.getActiveCellRow()); + + //modify position through HSSFCell + HSSFCell cell = umSheet.createRow(3).createCell(2); + cell.setAsActiveCell(); + assertEquals("After modify, active cell should be in col 2", + (short) 2, s.getActiveCellCol()); + assertEquals("After modify, active cell should be on row 3", + 3, s.getActiveCellRow()); + + //write book to temp file; read and verify that position is serialized + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + umSheet = wb2.getSheetAt(0); + s = umSheet.getSheet(); + + assertEquals("After serialize, active cell should be in col 2", + (short) 2, s.getActiveCellCol()); + assertEquals("After serialize, active cell should be on row 3", + 3, s.getActiveCellRow()); + } + } } @Test public void testActiveCellBug56114() throws IOException { - Workbook wb = new HSSFWorkbook(); - Sheet sh = wb.createSheet(); + try (Workbook wb = new HSSFWorkbook()) { + Sheet sh = wb.createSheet(); + + sh.createRow(0); + sh.createRow(1); + sh.createRow(2); + sh.createRow(3); + + Cell cell = sh.getRow(1).createCell(3); + sh.getRow(3).createCell(3); - sh.createRow(0); - sh.createRow(1); - sh.createRow(2); - sh.createRow(3); - - Cell cell = sh.getRow(1).createCell(3); - sh.getRow(3).createCell(3); - - assertEquals(0, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellRow()); - assertEquals(0, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellCol()); - - cell.setAsActiveCell(); - cell.setCellValue("this should be active"); - - assertEquals(1, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellRow()); - assertEquals(3, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellCol()); - - /*OutputStream fos = new FileOutputStream("c:/temp/56114.xls"); - try { - wb.write(fos); - } finally { - fos.close(); - }*/ - - Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb); - wb.close(); - - assertEquals(1, ((HSSFSheet)wbBack.getSheetAt(0)).getSheet().getActiveCellRow()); - assertEquals(3, ((HSSFSheet)wbBack.getSheetAt(0)).getSheet().getActiveCellCol()); - - wbBack.getSheetAt(0).getRow(3).getCell(3).setAsActiveCell(); - - assertEquals(3, ((HSSFSheet)wbBack.getSheetAt(0)).getSheet().getActiveCellRow()); - assertEquals(3, ((HSSFSheet)wbBack.getSheetAt(0)).getSheet().getActiveCellCol()); - - /*fos = new FileOutputStream("c:/temp/56114a.xls"); - try { - wb.write(fos); - } finally { - fos.close(); - }*/ - - Workbook wbBack2 = _testDataProvider.writeOutAndReadBack(wbBack); - wbBack.close(); - - assertEquals(3, ((HSSFSheet)wbBack2.getSheetAt(0)).getSheet().getActiveCellRow()); - assertEquals(3, ((HSSFSheet)wbBack2.getSheetAt(0)).getSheet().getActiveCellCol()); - wbBack2.close(); + assertEquals(0, ((HSSFSheet) wb.getSheetAt(0)).getSheet().getActiveCellRow()); + assertEquals(0, ((HSSFSheet) wb.getSheetAt(0)).getSheet().getActiveCellCol()); + + cell.setAsActiveCell(); + cell.setCellValue("this should be active"); + + assertEquals(1, ((HSSFSheet) wb.getSheetAt(0)).getSheet().getActiveCellRow()); + assertEquals(3, ((HSSFSheet) wb.getSheetAt(0)).getSheet().getActiveCellCol()); + + try (Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb)) { + assertEquals(1, ((HSSFSheet) wbBack.getSheetAt(0)).getSheet().getActiveCellRow()); + assertEquals(3, ((HSSFSheet) wbBack.getSheetAt(0)).getSheet().getActiveCellCol()); + + wbBack.getSheetAt(0).getRow(3).getCell(3).setAsActiveCell(); + + assertEquals(3, ((HSSFSheet) wbBack.getSheetAt(0)).getSheet().getActiveCellRow()); + assertEquals(3, ((HSSFSheet) wbBack.getSheetAt(0)).getSheet().getActiveCellCol()); + + try (Workbook wbBack2 = _testDataProvider.writeOutAndReadBack(wbBack)) { + assertEquals(3, ((HSSFSheet) wbBack2.getSheetAt(0)).getSheet().getActiveCellRow()); + assertEquals(3, ((HSSFSheet) wbBack2.getSheetAt(0)).getSheet().getActiveCellCol()); + } + } + } } /** @@ -239,20 +223,18 @@ public final class TestHSSFCell extends */ @Test public void testWithHyperlink() throws IOException { + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithHyperlink.xls")) { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithHyperlink.xls"); - - HSSFSheet sheet = wb.getSheetAt(0); - HSSFCell cell = sheet.getRow(4).getCell(0); - HSSFHyperlink link = cell.getHyperlink(); - assertNotNull(link); - - assertEquals("Foo", link.getLabel()); - assertEquals("http://poi.apache.org/", link.getAddress()); - assertEquals(4, link.getFirstRow()); - assertEquals(0, link.getFirstColumn()); - - wb.close(); + HSSFSheet sheet = wb.getSheetAt(0); + HSSFCell cell = sheet.getRow(4).getCell(0); + HSSFHyperlink link = cell.getHyperlink(); + assertNotNull(link); + + assertEquals("Foo", link.getLabel()); + assertEquals("http://poi.apache.org/", link.getAddress()); + assertEquals(4, link.getFirstRow()); + assertEquals(0, link.getFirstColumn()); + } } /** @@ -260,28 +242,26 @@ public final class TestHSSFCell extends */ @Test public void testWithTwoHyperlinks() throws IOException { + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithTwoHyperLinks.xls")) { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithTwoHyperLinks.xls"); + HSSFSheet sheet = wb.getSheetAt(0); - HSSFSheet sheet = wb.getSheetAt(0); - - HSSFCell cell1 = sheet.getRow(4).getCell(0); - HSSFHyperlink link1 = cell1.getHyperlink(); - assertNotNull(link1); - assertEquals("Foo", link1.getLabel()); - assertEquals("http://poi.apache.org/", link1.getAddress()); - assertEquals(4, link1.getFirstRow()); - assertEquals(0, link1.getFirstColumn()); - - HSSFCell cell2 = sheet.getRow(8).getCell(1); - HSSFHyperlink link2 = cell2.getHyperlink(); - assertNotNull(link2); - assertEquals("Bar", link2.getLabel()); - assertEquals("http://poi.apache.org/hssf/", link2.getAddress()); - assertEquals(8, link2.getFirstRow()); - assertEquals(1, link2.getFirstColumn()); - - wb.close(); + HSSFCell cell1 = sheet.getRow(4).getCell(0); + HSSFHyperlink link1 = cell1.getHyperlink(); + assertNotNull(link1); + assertEquals("Foo", link1.getLabel()); + assertEquals("http://poi.apache.org/", link1.getAddress()); + assertEquals(4, link1.getFirstRow()); + assertEquals(0, link1.getFirstColumn()); + + HSSFCell cell2 = sheet.getRow(8).getCell(1); + HSSFHyperlink link2 = cell2.getHyperlink(); + assertNotNull(link2); + assertEquals("Bar", link2.getLabel()); + assertEquals("http://poi.apache.org/hssf/", link2.getAddress()); + assertEquals(8, link2.getFirstRow()); + assertEquals(1, link2.getFirstColumn()); + } } /** @@ -290,47 +270,45 @@ public final class TestHSSFCell extends */ @Test public void testCellStyleWorkbookMatch() throws IOException { - HSSFWorkbook wbA = new HSSFWorkbook(); - HSSFWorkbook wbB = new HSSFWorkbook(); + try (HSSFWorkbook wbA = new HSSFWorkbook(); + HSSFWorkbook wbB = new HSSFWorkbook()) { - HSSFCellStyle styA = wbA.createCellStyle(); - HSSFCellStyle styB = wbB.createCellStyle(); + HSSFCellStyle styA = wbA.createCellStyle(); + HSSFCellStyle styB = wbB.createCellStyle(); - styA.verifyBelongsToWorkbook(wbA); - styB.verifyBelongsToWorkbook(wbB); - try { - styA.verifyBelongsToWorkbook(wbB); - fail("expected IllegalArgumentException"); - } catch (IllegalArgumentException e) { - // expected during successful test - } - try { - styB.verifyBelongsToWorkbook(wbA); - fail("expected IllegalArgumentException"); - } catch (IllegalArgumentException e) { - // expected during successful test - } - - Cell cellA = wbA.createSheet().createRow(0).createCell(0); - Cell cellB = wbB.createSheet().createRow(0).createCell(0); - - cellA.setCellStyle(styA); - cellB.setCellStyle(styB); - try { - cellA.setCellStyle(styB); - fail("expected IllegalArgumentException"); - } catch (IllegalArgumentException e) { - // expected during successful test - } - try { - cellB.setCellStyle(styA); - fail("expected IllegalArgumentException"); - } catch (IllegalArgumentException e) { - // expected during successful test - } - - wbB.close(); - wbA.close(); + styA.verifyBelongsToWorkbook(wbA); + styB.verifyBelongsToWorkbook(wbB); + try { + styA.verifyBelongsToWorkbook(wbB); + fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected during successful test + } + try { + styB.verifyBelongsToWorkbook(wbA); + fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected during successful test + } + + Cell cellA = wbA.createSheet().createRow(0).createCell(0); + Cell cellB = wbB.createSheet().createRow(0).createCell(0); + + cellA.setCellStyle(styA); + cellB.setCellStyle(styB); + try { + cellA.setCellStyle(styB); + fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected during successful test + } + try { + cellB.setCellStyle(styA); + fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected during successful test + } + } } /** @@ -341,47 +319,44 @@ public final class TestHSSFCell extends */ @Test public void testCachedTypeChange() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("Sheet1"); - Cell cell = sheet.createRow(0).createCell(0); - cell.setCellFormula("A1"); - cell.setCellValue("abc"); - confirmStringRecord(sheet, true); - cell.setCellValue(123); - Record[] recs = RecordInspector.getRecords(sheet, 0); - if (recs.length == 28 && recs[23] instanceof StringRecord) { - wb.close(); - fail("Identified bug - leftover StringRecord"); - } - confirmStringRecord(sheet, false); - - // string to error code - cell.setCellValue("abc"); - confirmStringRecord(sheet, true); - cell.setCellErrorValue(FormulaError.REF.getCode()); - confirmStringRecord(sheet, false); - - // string to boolean - cell.setCellValue("abc"); - confirmStringRecord(sheet, true); - cell.setCellValue(false); - confirmStringRecord(sheet, false); - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet("Sheet1"); + Cell cell = sheet.createRow(0).createCell(0); + cell.setCellFormula("A1"); + cell.setCellValue("abc"); + confirmStringRecord(sheet, true); + cell.setCellValue(123); + + final List<Record> recs = new ArrayList<>(); + sheet.getSheet().visitContainedRecords(recs::add, 0); + assertFalse("Identified bug - leftover StringRecord", recs.size() == 28 && recs.get(23) instanceof StringRecord); + confirmStringRecord(sheet, false); + + // string to error code + cell.setCellValue("abc"); + confirmStringRecord(sheet, true); + cell.setCellErrorValue(FormulaError.REF.getCode()); + confirmStringRecord(sheet, false); + + // string to boolean + cell.setCellValue("abc"); + confirmStringRecord(sheet, true); + cell.setCellValue(false); + confirmStringRecord(sheet, false); + } } private static void confirmStringRecord(HSSFSheet sheet, boolean isPresent) { - Record[] recs = RecordInspector.getRecords(sheet, 0); - assertEquals(isPresent ? 28 : 27, recs.length); - int index = 22; - Record fr = recs[index++]; - assertEquals(FormulaRecord.class, fr.getClass()); + List<Record> recs = new ArrayList<>(); + sheet.getSheet().visitContainedRecords(recs::add, 0); + assertEquals(isPresent ? 28 : 27, recs.size()); + + ListIterator<Record> iter = recs.listIterator(22); + assertEquals(FormulaRecord.class, iter.next().getClass()); if (isPresent) { - assertEquals(StringRecord.class, recs[index++].getClass()); - } else { - assertNotSame(StringRecord.class, recs[index].getClass()); + assertEquals(StringRecord.class, iter.next().getClass()); } - Record dbcr = recs[index]; - assertEquals(DBCellRecord.class, dbcr.getClass()); + assertEquals(DBCellRecord.class, iter.next().getClass()); } /** @@ -389,86 +364,87 @@ public final class TestHSSFCell extends */ @Test public void testReadNaN() throws IOException { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49761.xls"); - assertNotNull(wb); - wb.close(); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49761.xls")) { + assertNotNull(wb); + } } @Test public void testHSSFCell() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - HSSFRow row = sheet.createRow(0); - row.createCell(0); - HSSFCell cell = new HSSFCell(wb, sheet, 0, (short)0); - assertNotNull(cell); - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + HSSFRow row = sheet.createRow(0); + row.createCell(0); + HSSFCell cell = new HSSFCell(wb, sheet, 0, (short) 0); + assertNotNull(cell); + } } @Test public void testDeprecatedMethods() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - HSSFRow row = sheet.createRow(0); - HSSFCell cell = row.createCell(0); - - // cover some deprecated methods and other smaller stuff... - assertEquals(wb.getWorkbook(), cell.getBoundWorkbook()); - - try { - cell.getCachedFormulaResultType(); - fail("Should catch exception"); - } catch (IllegalStateException e) { - // expected here - } - - cell.removeCellComment(); - cell.removeCellComment(); - - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + HSSFRow row = sheet.createRow(0); + HSSFCell cell = row.createCell(0); + + // cover some deprecated methods and other smaller stuff... + assertEquals(wb.getWorkbook(), cell.getBoundWorkbook()); + + try { + cell.getCachedFormulaResultType(); + fail("Should catch exception"); + } catch (IllegalStateException e) { + // expected here + } + + cell.removeCellComment(); + cell.removeCellComment(); + } } @Test public void testCellType() throws IOException { - Workbook wb = _testDataProvider.createWorkbook(); - Sheet sheet = wb.createSheet(); - Row row = sheet.createRow(0); - Cell cell = row.createCell(0); - - cell.setBlank(); - assertNull(null, cell.getDateCellValue()); - assertFalse(cell.getBooleanCellValue()); - assertEquals("", cell.toString()); - - cell.setCellType(CellType.STRING); - assertEquals("", cell.toString()); - cell.setCellValue(1.2); - assertEquals("1.2", cell.toString()); - cell.setCellType(CellType.BOOLEAN); - assertEquals("TRUE", cell.toString()); - cell.setCellType(CellType.BOOLEAN); - cell.setCellValue("" + FormulaError.VALUE.name()); - assertEquals(CellType.STRING, cell.getCellType()); - cell.setCellType(CellType.BOOLEAN); - assertEquals("FALSE", cell.toString()); - cell.setCellValue(1.2); - assertEquals("1.2", cell.toString()); - cell.setCellType(CellType.BOOLEAN); - cell.setCellType(CellType.STRING); - cell.setCellType(CellType.ERROR); - cell.setCellType(CellType.STRING); - cell.setCellValue(1.2); - cell.setCellType(CellType.STRING); - assertEquals("1.2", cell.toString()); - - cell.setCellValue((String)null); - cell.setCellValue((RichTextString)null); - wb.close(); + try (Workbook wb = _testDataProvider.createWorkbook()) { + Sheet sheet = wb.createSheet(); + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); + + cell.setBlank(); + assertNull(null, cell.getDateCellValue()); + assertFalse(cell.getBooleanCellValue()); + assertEquals("", cell.toString()); + + cell.setCellType(CellType.STRING); + assertEquals("", cell.toString()); + cell.setCellValue(1.2); + assertEquals("1.2", cell.toString()); + cell.setCellType(CellType.BOOLEAN); + assertEquals("TRUE", cell.toString()); + cell.setCellType(CellType.BOOLEAN); + cell.setCellValue("" + FormulaError.VALUE.name()); + assertEquals(CellType.STRING, cell.getCellType()); + cell.setCellType(CellType.BOOLEAN); + assertEquals("FALSE", cell.toString()); + cell.setCellValue(1.2); + assertEquals("1.2", cell.toString()); + cell.setCellType(CellType.BOOLEAN); + cell.setCellType(CellType.STRING); + cell.setCellType(CellType.ERROR); + cell.setCellType(CellType.STRING); + cell.setCellValue(1.2); + cell.setCellType(CellType.STRING); + assertEquals("1.2", cell.toString()); + + cell.setCellValue((String) null); + cell.setCellValue((RichTextString) null); + } } @Test(expected = IllegalStateException.class) - public void getErrorCellValue_throwsISE_onABlankCell() { - Cell cell = new HSSFWorkbook().createSheet().createRow(0).createCell(0); - cell.getErrorCellValue(); + public void getErrorCellValue_throwsISE_onABlankCell() throws IOException { + try (HSSFWorkbook wb = new HSSFWorkbook()) { + Cell cell = wb.createSheet().createRow(0).createCell(0); + cell.getErrorCellValue(); + } } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org For additional commands, e-mail: commits-h...@poi.apache.org