Modified: poi/trunk/src/testcases/org/apache/poi/hssf/eventusermodel/TestMissingRecordAwareHSSFListener.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/eventusermodel/TestMissingRecordAwareHSSFListener.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/eventusermodel/TestMissingRecordAwareHSSFListener.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/eventusermodel/TestMissingRecordAwareHSSFListener.java Fri Dec 27 23:00:13 2019 @@ -16,343 +16,95 @@ ==================================================================== */ package org.apache.poi.hssf.eventusermodel; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; - -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import java.util.function.Predicate; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord; import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord; import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord; -import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.BlankRecord; import org.apache.poi.hssf.record.CellValueRecordInterface; import org.apache.poi.hssf.record.DimensionsRecord; -import org.apache.poi.hssf.record.FormulaRecord; import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.MulBlankRecord; import org.apache.poi.hssf.record.NumberRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.RowRecord; import org.apache.poi.hssf.record.SharedFormulaRecord; -import org.apache.poi.hssf.record.StringRecord; import org.apache.poi.hssf.record.WindowTwoRecord; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.junit.Test; + /** * Tests for MissingRecordAwareHSSFListener */ -public final class TestMissingRecordAwareHSSFListener extends TestCase { - - private Record[] r; +public final class TestMissingRecordAwareHSSFListener { - private void readRecords(String sampleFileName) { + private final List<Record> _records = new ArrayList<>(); + + private void readRecords(String sampleFileName) throws IOException { + _records.clear(); HSSFRequest req = new HSSFRequest(); - MockHSSFListener mockListen = new MockHSSFListener(); - MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(mockListen); - req.addListenerForAllRecords(listener); - + req.addListenerForAllRecords(new MissingRecordAwareHSSFListener(_records::add)); + HSSFEventFactory factory = new HSSFEventFactory(); - try { - InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName); - POIFSFileSystem fs = new POIFSFileSystem(is); + try (InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName); + POIFSFileSystem fs = new POIFSFileSystem(is)) { factory.processWorkbookEvents(req, fs); - } catch (IOException e) { - throw new RuntimeException(e); } - - r = mockListen.getRecords(); - assertTrue(r.length > 100); - } - public void openNormal() { - readRecords("MissingBits.xls"); + + assertTrue(_records.size() > 100); } - - public void testMissingRowRecords() { - openNormal(); - + + @Test + public void testMissingRowRecords() throws IOException { + readRecords("MissingBits.xls"); + // We have rows 0, 1, 2, 20 and 21 - int row0 = -1; - for(int i=0; i<r.length; i++) { - if(r[i] instanceof RowRecord) { - RowRecord rr = (RowRecord)r[i]; - if(rr.getRowNumber() == 0) { row0 = i; } - } - } + int row0 = lastIndexOf(r -> r instanceof RowRecord && ((RowRecord)r).getRowNumber() == 0); assertTrue(row0 > -1); - - // Following row 0, we should have 1, 2, then dummy, then 20+21+22 - assertTrue(r[row0] instanceof RowRecord); - assertTrue(r[row0+1] instanceof RowRecord); - assertTrue(r[row0+2] instanceof RowRecord); - assertTrue(r[row0+3] instanceof MissingRowDummyRecord); - assertTrue(r[row0+4] instanceof MissingRowDummyRecord); - assertTrue(r[row0+5] instanceof MissingRowDummyRecord); - assertTrue(r[row0+6] instanceof MissingRowDummyRecord); - // ... - assertTrue(r[row0+18] instanceof MissingRowDummyRecord); - assertTrue(r[row0+19] instanceof MissingRowDummyRecord); - assertTrue(r[row0+20] instanceof RowRecord); - assertTrue(r[row0+21] instanceof RowRecord); - assertTrue(r[row0+22] instanceof RowRecord); - - // Check things had the right row numbers - RowRecord rr; - rr = (RowRecord)r[row0+2]; - assertEquals(2, rr.getRowNumber()); - rr = (RowRecord)r[row0+20]; - assertEquals(20, rr.getRowNumber()); - rr = (RowRecord)r[row0+21]; - assertEquals(21, rr.getRowNumber()); - - MissingRowDummyRecord mr; - mr = (MissingRowDummyRecord)r[row0+3]; - assertEquals(3, mr.getRowNumber()); - mr = (MissingRowDummyRecord)r[row0+4]; - assertEquals(4, mr.getRowNumber()); - mr = (MissingRowDummyRecord)r[row0+5]; - assertEquals(5, mr.getRowNumber()); - mr = (MissingRowDummyRecord)r[row0+18]; - assertEquals(18, mr.getRowNumber()); - mr = (MissingRowDummyRecord)r[row0+19]; - assertEquals(19, mr.getRowNumber()); - } - - public void testEndOfRowRecords() { - openNormal(); - - // Find the cell at 0,0 - int cell00 = -1; - for(int i=0; i<r.length; i++) { - if(r[i] instanceof LabelSSTRecord) { - LabelSSTRecord lr = (LabelSSTRecord)r[i]; - if(lr.getRow() == 0 && lr.getColumn() == 0) { cell00 = i; } - } - } - assertTrue(cell00 > -1); - - // We have rows 0, 1, 2, 20 and 21 - // Row 0 has 1 entry - // Row 1 has 4 entries - // Row 2 has 6 entries - // Row 20 has 5 entries - // Row 21 has 7 entries - // Row 22 has 12 entries - - // Row 0 - assertFalse(r[cell00+0] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+1] instanceof LastCellOfRowDummyRecord); - // Row 1 - assertFalse(r[cell00+2] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+3] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+4] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+5] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+6] instanceof LastCellOfRowDummyRecord); - // Row 2 - assertFalse(r[cell00+7] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+8] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+9] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+10] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+11] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+12] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+13] instanceof LastCellOfRowDummyRecord); - // Row 3 -> 19 - assertTrue(r[cell00+14] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+15] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+16] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+17] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+18] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+19] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+20] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+21] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+22] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+23] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+24] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+25] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+26] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+27] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+28] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+29] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+30] instanceof LastCellOfRowDummyRecord); - // Row 20 - assertFalse(r[cell00+31] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+32] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+33] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+34] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+35] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+36] instanceof LastCellOfRowDummyRecord); - // Row 21 - assertFalse(r[cell00+37] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+38] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+39] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+40] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+41] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+42] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+43] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+44] instanceof LastCellOfRowDummyRecord); - // Row 22 - assertFalse(r[cell00+45] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+46] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+47] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+48] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+49] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+50] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+51] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+52] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+53] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+54] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+55] instanceof LastCellOfRowDummyRecord); - assertFalse(r[cell00+56] instanceof LastCellOfRowDummyRecord); - assertTrue(r[cell00+57] instanceof LastCellOfRowDummyRecord); - - // Check the numbers of the last seen columns - LastCellOfRowDummyRecord[] lrs = new LastCellOfRowDummyRecord[24]; - int lrscount = 0; - for (final Record rec : r) { - if(rec instanceof LastCellOfRowDummyRecord) { - lrs[lrscount] = (LastCellOfRowDummyRecord)rec; - lrscount++; - } - } - - assertEquals(0, lrs[0].getLastColumnNumber()); - assertEquals(0, lrs[0].getRow()); - - assertEquals(3, lrs[1].getLastColumnNumber()); - assertEquals(1, lrs[1].getRow()); - - assertEquals(5, lrs[2].getLastColumnNumber()); - assertEquals(2, lrs[2].getRow()); - - for(int i=3; i<=19; i++) { - assertEquals(-1, lrs[i].getLastColumnNumber()); - assertEquals(i, lrs[i].getRow()); - } - - assertEquals(4, lrs[20].getLastColumnNumber()); - assertEquals(20, lrs[20].getRow()); - - assertEquals(6, lrs[21].getLastColumnNumber()); - assertEquals(21, lrs[21].getRow()); - - assertEquals(11, lrs[22].getLastColumnNumber()); - assertEquals(22, lrs[22].getRow()); - } - - - public void testMissingCellRecords() { - openNormal(); - + + // Records: row 0: column 1, 2), then missing rows, rows 20,21,22 each 1 column + String exp1 = + "0:rr,1:rr,2:rr,3:mr,4:mr,5:mr,6:mr,7:mr,8:mr,9:mr,10:mr,11:mr,12:mr,13:mr,14:mr," + + "15:mr,16:mr,17:mr,18:mr,19:mr,20:rr,21:rr,22:rr"; + String act1 = digest(row0, 22); + assertEquals(exp1, act1); + + // Find the cell at 0,0 - int cell00 = -1; - for(int i=0; i<r.length; i++) { - if(r[i] instanceof LabelSSTRecord) { - LabelSSTRecord lr = (LabelSSTRecord)r[i]; - if(lr.getRow() == 0 && lr.getColumn() == 0) { cell00 = i; } - } - } - assertTrue(cell00 > -1); - - // We have rows 0, 1, 2, 20 and 21 - // Row 0 has 1 entry, 0 - // Row 1 has 4 entries, 0+3 - // Row 2 has 6 entries, 0+5 - // Row 20 has 5 entries, 0-5 - // Row 21 has 7 entries, 0+1+3+5+6 - // Row 22 has 12 entries, 0+3+4+11 - - // Row 0 - assertFalse(r[cell00+0] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+1] instanceof MissingCellDummyRecord); - - // Row 1 - assertFalse(r[cell00+2] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+3] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+4] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+5] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+6] instanceof MissingCellDummyRecord); - - // Row 2 - assertFalse(r[cell00+7] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+8] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+9] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+10] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+11] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+12] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+13] instanceof MissingCellDummyRecord); - - // Row 3-19 - assertFalse(r[cell00+14] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+15] instanceof MissingCellDummyRecord); - - // Row 20 - assertFalse(r[cell00+31] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+32] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+33] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+34] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+35] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+36] instanceof MissingCellDummyRecord); - - // Row 21 - assertFalse(r[cell00+37] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+38] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+39] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+40] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+41] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+42] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+43] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+44] instanceof MissingCellDummyRecord); - - // Row 22 - assertFalse(r[cell00+45] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+46] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+47] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+48] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+49] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+50] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+51] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+52] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+53] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+54] instanceof MissingCellDummyRecord); - assertTrue(r[cell00+55] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+56] instanceof MissingCellDummyRecord); - assertFalse(r[cell00+57] instanceof MissingCellDummyRecord); - - // Check some numbers - MissingCellDummyRecord mc; - - mc = (MissingCellDummyRecord)r[cell00+3]; - assertEquals(1, mc.getRow()); - assertEquals(1, mc.getColumn()); - mc = (MissingCellDummyRecord)r[cell00+4]; - assertEquals(1, mc.getRow()); - assertEquals(2, mc.getColumn()); - - mc = (MissingCellDummyRecord)r[cell00+8]; - assertEquals(2, mc.getRow()); - assertEquals(1, mc.getColumn()); - mc = (MissingCellDummyRecord)r[cell00+9]; - assertEquals(2, mc.getRow()); - assertEquals(2, mc.getColumn()); - - mc = (MissingCellDummyRecord)r[cell00+55]; - assertEquals(22, mc.getRow()); - assertEquals(10, mc.getColumn()); - } - - // Make sure we don't put in any extra new lines - // that aren't already there - public void testNoExtraNewLines() { + int cell00 = lastIndexOf(r -> r instanceof LabelSSTRecord && ((LabelSSTRecord)r).getRow() == 0 && ((LabelSSTRecord)r).getColumn() == 0); + + String exp2 = + "0:ls0lc0," + + "1:nr0/11mc1mc2nr3/23lc3," + + "2:nr0/45mc1mc2mc3mc4nr5/22lc5," + + "3:lc,4:lc,5:lc,6:lc,7:lc,8:lc,9:lc,10:lc,11:lc,12:lc,13:lc,14:lc,15:lc,16:lc,17:lc,18:lc,19:lc," + + "20:nr0/50nr1/51nr2/52nr3/53nr4/54lc4," + + "21:ls0ls1mc2nr3/12mc4nr5/23nr6/42lc6," + + "22:ls0mc1mc2ls3ls4mc5mc6mc7mc8mc9mc10ls11lc11"; + String act2 = digest(cell00, 57); + assertEquals(exp2, act2); + } + + // Make sure we don't put in any extra new lines that aren't already there + @Test + public void testNoExtraNewLines() throws IOException { // Load a different file // This file has has something in lines 1-33 readRecords("MRExtraLines.xls"); - + int rowCount=0; - for (Record rec : r) { - if(rec instanceof LastCellOfRowDummyRecord) { + for (Record rec : _records) { + if (rec instanceof LastCellOfRowDummyRecord) { LastCellOfRowDummyRecord eor = (LastCellOfRowDummyRecord) rec; assertEquals(rowCount, eor.getRow()); rowCount++; @@ -362,534 +114,114 @@ public final class TestMissingRecordAwar assertEquals(33, rowCount); } - private static final class MockHSSFListener implements HSSFListener { - public MockHSSFListener() {} - private final List<Record> _records = new ArrayList<>(); - private final boolean logToStdOut = false; - - @Override - public void processRecord(Record record) { - _records.add(record); - - if(record instanceof MissingRowDummyRecord) { - MissingRowDummyRecord mr = (MissingRowDummyRecord)record; - log("Got dummy row " + mr.getRowNumber()); - } - if(record instanceof MissingCellDummyRecord) { - MissingCellDummyRecord mc = (MissingCellDummyRecord)record; - log("Got dummy cell " + mc.getRow() + " " + mc.getColumn()); - } - if(record instanceof LastCellOfRowDummyRecord) { - LastCellOfRowDummyRecord lc = (LastCellOfRowDummyRecord)record; - log("Got end-of row, row was " + lc.getRow() + ", last column was " + lc.getLastColumnNumber()); - } - - if(record instanceof BOFRecord) { - BOFRecord r = (BOFRecord)record; - if(r.getType() == BOFRecord.TYPE_WORKSHEET) { - log("On new sheet"); - } - } - if(record instanceof RowRecord) { - RowRecord rr = (RowRecord)record; - log("Starting row #" + rr.getRowNumber()); - } - } - private void log(String msg) { - if(logToStdOut) { - System.out.println(msg); - } - } - public Record[] getRecords() { - Record[] result = new Record[_records.size()]; - _records.toArray(result); - return result; - } - } - /** - * Make sure that the presence of shared formulas does not cause extra - * end-of-row records. + * Make sure that the presence of shared formulas does not cause extra end-of-row records. */ - public void testEndOfRow_bug45672() { + @Test + public void testEndOfRow_bug45672() throws IOException { readRecords("ex45672.xls"); - Record[] rr = r; - int eorCount=0; - int sfrCount=0; - for (Record record : rr) { - if (record instanceof SharedFormulaRecord) { - sfrCount++; - } - if (record instanceof LastCellOfRowDummyRecord) { - eorCount++; - } - } - if (eorCount == 2) { - throw new AssertionFailedError("Identified bug 45672"); - } - assertEquals(1, eorCount); - assertEquals(1, sfrCount); + assertEquals(1, matches(r -> r instanceof SharedFormulaRecord)); + assertEquals(1, matches(r -> r instanceof LastCellOfRowDummyRecord)); } - + /** - * MulBlank records hold multiple blank cells. Check we - * can handle them correctly. + * MulBlank records hold multiple blank cells. + * Check that we don't have any MulBlankRecords, but do have lots of BlankRecords */ - public void testMulBlankHandling() { + @Test + public void testMulBlankHandling() throws IOException { readRecords("45672.xls"); - - // Check that we don't have any MulBlankRecords, but do - // have lots of BlankRecords - Record[] rr = r; - int eorCount=0; - int mbrCount=0; - int brCount=0; - for (Record record : rr) { - if (record instanceof MulBlankRecord) { - mbrCount++; + assertEquals(20, matches(r -> r instanceof BlankRecord)); + assertEquals(2, matches(r -> r instanceof LastCellOfRowDummyRecord)); + assertEquals(0, matches(r -> r instanceof MulBlankRecord)); + } + + @Test + public void testStringRecordHandling() throws IOException { + readRecords("53588.xls"); + assertEquals(1, matches(r -> r instanceof MissingCellDummyRecord)); + assertEquals(1, matches(r -> r instanceof LastCellOfRowDummyRecord)); + } + + @Test + public void testFormulasWithStringResultsHandling() throws IOException { + readRecords("53433.xls"); + + String exp = + "dr0:mr,1:mr,2:mr,3:rr,4:rr,5:rr,6:rr,7:rr,8:rr,9:rr,10:mr,11:mr,12:mr,13:mr,14:mr,15:rr,16:rr,17:mr,18:rr," + + "0:lc,1:lc,2:lc,3:mc0ls1ls2ls3ls4lc4,4:mc0ls1cv2urnr3/12cv4urlc4," + + "5:mc0cv1urnr2/23nr3/23cv4urlc4,6:mc0mc1nr2/25nr3/45nr4/32815lc4," + + "7:mc0ls1cv2cv3urcv4lc4,8:mc0mc1mc2mc3cv4lc4,9:mc0mc1mc2ls3lc3," + + "10:lc,11:lc,12:lc,13:lc,14:lc,15:mc0ls1lc1,16:mc0mc1mc2ls3lc3," + + "17:lc,18:mc0mc1mc2mc3ls4lc4wr"; + String act = digest(95, 89); + assertEquals(exp, act); + } + + private int lastIndexOf(Predicate<Record> pre) { + int found = -1; + int i = 0; + for (Record r : _records) { + if (pre.test(r)) { + found = i; + } + i++; + } + return found; + } + + private String digest(int start, int len) { + StringBuilder sb = new StringBuilder(len*10); + int lastRow = -1; + for (Record r : _records.subList(start, start+len+1)) { + String dig = null; + int row = -1; + if (r instanceof RowRecord) { + RowRecord rr = (RowRecord)r; + row = rr.getRowNumber(); + dig = "rr"; + } else if (r instanceof MissingRowDummyRecord) { + MissingRowDummyRecord mr = (MissingRowDummyRecord)r; + row = mr.getRowNumber(); + dig = "mr"; + } else if (r instanceof MissingCellDummyRecord) { + MissingCellDummyRecord mc = (MissingCellDummyRecord)r; + row = mc.getRow(); + dig = "mc" + mc.getColumn(); + } else if (r instanceof LastCellOfRowDummyRecord) { + LastCellOfRowDummyRecord lc = (LastCellOfRowDummyRecord)r; + row = lc.getRow(); + dig = "lc" + (lc.getLastColumnNumber() > -1 ? lc.getLastColumnNumber() : ""); + } else if (r instanceof NumberRecord) { + NumberRecord nr = (NumberRecord)r; + row = nr.getRow(); + dig = "nr" + nr.getColumn() + "/" + (int)nr.getValue(); + } else if (r instanceof LabelSSTRecord) { + LabelSSTRecord ls = (LabelSSTRecord) r; + row = ls.getRow(); + dig = "ls" + ls.getColumn(); + } else if (r instanceof WindowTwoRecord) { + dig = "wr"; + } else if (r instanceof DimensionsRecord) { + dig = "dr"; + } else if (r instanceof CellValueRecordInterface) { + CellValueRecordInterface cv = (CellValueRecordInterface) r; + row = cv.getRow(); + dig = "cv" + cv.getColumn(); + } else { + // unhandled record + dig = "ur"; + } + if (lastRow != row && row > -1) { + sb.append((lastRow > -1 ? "," : "") + row + ":"); + lastRow = row; } - if (record instanceof BlankRecord) { - brCount++; - } - if (record instanceof LastCellOfRowDummyRecord) { - eorCount++; - } - } - if (mbrCount > 0) { - throw new AssertionFailedError("Identified bug 45672"); + sb.append(dig); } - if (brCount < 20) { - throw new AssertionFailedError("Identified bug 45672"); - } - if (eorCount != 2) { - throw new AssertionFailedError("Identified bug 45672"); - } - assertEquals(2, eorCount); + return sb.toString(); } - public void testStringRecordHandling(){ - readRecords("53588.xls"); - Record[] rr = r; - int missingCount=0; - int lastCount=0; - for (Record record : rr) { - if (record instanceof MissingCellDummyRecord) { - missingCount++; - } - if (record instanceof LastCellOfRowDummyRecord) { - lastCount++; - } - } - assertEquals(1, missingCount); - assertEquals(1, lastCount); - } - - public void testFormulasWithStringResultsHandling() { - readRecords("53433.xls"); - - int pos = 95; - - // First three rows are blank - assertEquals(DimensionsRecord.class, r[pos++].getClass()); - - assertEquals(MissingRowDummyRecord.class, r[pos].getClass()); - assertEquals(0, ((MissingRowDummyRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(MissingRowDummyRecord.class, r[pos].getClass()); - assertEquals(1, ((MissingRowDummyRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(MissingRowDummyRecord.class, r[pos].getClass()); - assertEquals(2, ((MissingRowDummyRecord)r[pos]).getRowNumber()); - pos++; - - // Then rows 4-10 are defined - assertEquals(RowRecord.class, r[pos].getClass()); - assertEquals(3, ((RowRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(RowRecord.class, r[pos].getClass()); - assertEquals(4, ((RowRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(RowRecord.class, r[pos].getClass()); - assertEquals(5, ((RowRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(RowRecord.class, r[pos].getClass()); - assertEquals(6, ((RowRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(RowRecord.class, r[pos].getClass()); - assertEquals(7, ((RowRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(RowRecord.class, r[pos].getClass()); - assertEquals(8, ((RowRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(RowRecord.class, r[pos].getClass()); - assertEquals(9, ((RowRecord)r[pos]).getRowNumber()); - pos++; - - // 5 more blank rows - assertEquals(MissingRowDummyRecord.class, r[pos].getClass()); - assertEquals(10, ((MissingRowDummyRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(MissingRowDummyRecord.class, r[pos].getClass()); - assertEquals(11, ((MissingRowDummyRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(MissingRowDummyRecord.class, r[pos].getClass()); - assertEquals(12, ((MissingRowDummyRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(MissingRowDummyRecord.class, r[pos].getClass()); - assertEquals(13, ((MissingRowDummyRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(MissingRowDummyRecord.class, r[pos].getClass()); - assertEquals(14, ((MissingRowDummyRecord)r[pos]).getRowNumber()); - pos++; - - // 2 defined rows - assertEquals(RowRecord.class, r[pos].getClass()); - assertEquals(15, ((RowRecord)r[pos]).getRowNumber()); - pos++; - assertEquals(RowRecord.class, r[pos].getClass()); - assertEquals(16, ((RowRecord)r[pos]).getRowNumber()); - pos++; - - // one blank row - assertEquals(MissingRowDummyRecord.class, r[pos].getClass()); - assertEquals(17, ((MissingRowDummyRecord)r[pos]).getRowNumber()); - pos++; - - // one last real row - assertEquals(RowRecord.class, r[pos].getClass()); - assertEquals(18, ((RowRecord)r[pos]).getRowNumber()); - pos++; - - - - // Now onto the cells - - // Because the 3 first rows are missing, should have last-of-row records first - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(0, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(-1, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(1, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(-1, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(2, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(-1, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Onto row 4 (=3) - - // Now we have blank cell A4 - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(3, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(0, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - - // Now 4 real cells, all strings - assertEquals(LabelSSTRecord.class, r[pos].getClass()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(1, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(LabelSSTRecord.class, r[pos].getClass()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(2, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(LabelSSTRecord.class, r[pos].getClass()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(LabelSSTRecord.class, r[pos].getClass()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - - // Final dummy cell for the end-of-row - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(3, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(4, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Row 5 has string, formula of string, number, formula of string - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(4, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(0, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - - assertEquals(LabelSSTRecord.class, r[pos].getClass()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(1, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(FormulaRecord.class, r[pos].getClass()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(2, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(StringRecord.class, r[pos].getClass()); - assertEquals("s1", ((StringRecord)r[pos]).getString()); - pos++; - assertEquals(NumberRecord.class, r[pos].getClass()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(FormulaRecord.class, r[pos].getClass()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(StringRecord.class, r[pos].getClass()); - assertEquals("s3845", ((StringRecord)r[pos]).getString()); - pos++; - - // Final dummy cell for the end-of-row - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(4, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(4, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Row 6 is blank / string formula / number / number / string formula - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(5, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(0, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - - assertEquals(FormulaRecord.class, r[pos].getClass()); - assertEquals(5, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(1, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(StringRecord.class, r[pos].getClass()); - assertEquals("s4", ((StringRecord)r[pos]).getString()); - pos++; - assertEquals(NumberRecord.class, r[pos].getClass()); - assertEquals(5, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(2, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(NumberRecord.class, r[pos].getClass()); - assertEquals(5, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(FormulaRecord.class, r[pos].getClass()); - assertEquals(5, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(StringRecord.class, r[pos].getClass()); - assertEquals("s3845", ((StringRecord)r[pos]).getString()); - pos++; - - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(5, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(4, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Row 7 is blank / blank / number / number / number - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(6, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(0, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(6, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(1, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - - assertEquals(NumberRecord.class, r[pos].getClass()); - assertEquals(6, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(2, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(NumberRecord.class, r[pos].getClass()); - assertEquals(6, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(NumberRecord.class, r[pos].getClass()); - assertEquals(6, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(6, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(4, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Row 8 is blank / string / number formula / string formula / blank - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(7, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(0, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - - assertEquals(LabelSSTRecord.class, r[pos].getClass()); - assertEquals(7, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(1, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(FormulaRecord.class, r[pos].getClass()); - assertEquals(7, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(2, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(FormulaRecord.class, r[pos].getClass()); - assertEquals(7, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(StringRecord.class, r[pos].getClass()); - assertEquals("s4", ((StringRecord)r[pos]).getString()); - pos++; - assertEquals(BlankRecord.class, r[pos].getClass()); - assertEquals(7, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(7, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(4, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Row 9 is empty, but with a blank at E9 - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(8, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(0, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(8, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(1, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(8, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(2, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(8, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(3, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(BlankRecord.class, r[pos].getClass()); - assertEquals(8, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(8, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(4, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Row 10 has a string in D10 - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(9, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(0, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(9, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(1, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(9, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(2, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(LabelSSTRecord.class, r[pos].getClass()); - assertEquals(9, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(9, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(3, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Now 5 blank rows - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(10, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(-1, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(11, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(-1, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(12, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(-1, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(13, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(-1, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(14, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(-1, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Row 16 has a single string in B16 - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(15, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(0, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(LabelSSTRecord.class, r[pos].getClass()); - assertEquals(15, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(1, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(15, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(1, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Row 17 has a single string in D17 - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(16, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(0, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(16, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(1, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(16, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(2, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(LabelSSTRecord.class, r[pos].getClass()); - assertEquals(16, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(3, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(16, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(3, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Row 18 is blank - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(17, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(-1, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // Row 19 has a single string in E19 - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(18, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(0, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(18, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(1, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(18, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(2, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(MissingCellDummyRecord.class, r[pos].getClass()); - assertEquals(18, ((MissingCellDummyRecord)r[pos]).getRow()); - assertEquals(3, ((MissingCellDummyRecord)r[pos]).getColumn()); - pos++; - assertEquals(LabelSSTRecord.class, r[pos].getClass()); - assertEquals(18, ((CellValueRecordInterface)r[pos]).getRow()); - assertEquals(4, ((CellValueRecordInterface)r[pos]).getColumn()); - pos++; - assertEquals(LastCellOfRowDummyRecord.class, r[pos].getClass()); - assertEquals(18, ((LastCellOfRowDummyRecord)r[pos]).getRow()); - assertEquals(4, ((LastCellOfRowDummyRecord)r[pos]).getLastColumnNumber()); - pos++; - - - // And that's it! - assertEquals(WindowTwoRecord.class, r[pos++].getClass()); - } + private long matches(Predicate<Record> r) { + return _records.stream().filter(r).count(); + } }
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestEscherRecordFactory.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestEscherRecordFactory.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestEscherRecordFactory.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestEscherRecordFactory.java Fri Dec 27 23:00:13 2019 @@ -17,7 +17,16 @@ package org.apache.poi.hssf.model; -import junit.framework.TestCase; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.List; +import java.util.Random; + import org.apache.poi.ddf.DefaultEscherRecordFactory; import org.apache.poi.ddf.EscherContainerRecord; import org.apache.poi.ddf.EscherTextboxRecord; @@ -28,19 +37,9 @@ import org.apache.poi.hssf.record.Record import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFTestHelper; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.junit.Test; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.util.List; -import java.util.Random; - -import static org.junit.Assert.assertArrayEquals; - -/** - * @author Evgeniy Berlog - * @date 18.06.12 - */ -public class TestEscherRecordFactory extends TestCase{ +public class TestEscherRecordFactory { private static byte[] toByteArray(List<RecordBase> records) { ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -55,6 +54,7 @@ public class TestEscherRecordFactory ext return out.toByteArray(); } + @Test public void testDetectContainer() { Random rnd = new Random(); assertTrue(DefaultEscherRecordFactory.isContainer((short) 0x0, EscherContainerRecord.DG_CONTAINER)); @@ -79,6 +79,7 @@ public class TestEscherRecordFactory ext assertFalse(DefaultEscherRecordFactory.isContainer((short) 0xCCCC, EscherTextboxRecord.RECORD_ID)); } + @Test public void testDgContainerMustBeRootOfHSSFSheetEscherRecords() throws IOException { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("47251.xls"); HSSFSheet sh = wb.getSheetAt(0); Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java Fri Dec 27 23:00:13 2019 @@ -17,29 +17,29 @@ package org.apache.poi.hssf.model; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; -import org.apache.poi.ss.formula.ptg.AttrPtg; -import org.apache.poi.ss.formula.ptg.NamePtg; -import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; import org.apache.poi.hssf.usermodel.HSSFName; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.ss.formula.FormulaParseException; +import org.apache.poi.ss.formula.ptg.AttrPtg; +import org.apache.poi.ss.formula.ptg.NamePtg; +import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.CellValue; +import org.junit.Test; /** * Test the low level formula parser functionality, * but using parts which need to use * HSSFFormulaEvaluator. */ -public final class TestFormulaParserEval extends TestCase { +public final class TestFormulaParserEval { + @Test public void testWithNamedRange() { HSSFWorkbook workbook = new HSSFWorkbook(); @@ -75,6 +75,7 @@ public final class TestFormulaParserEval assertEquals(AttrPtg.class, ptgs[1].getClass()); } + @Test public void testEvaluateFormulaWithRowBeyond32768_Bug44539() { HSSFWorkbook wb = new HSSFWorkbook(); @@ -90,15 +91,8 @@ public final class TestFormulaParserEval sheet.createRow(32769).createCell(0).setCellValue(11); HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); - CellValue result; - try { - result = fe.evaluate(cell); - } catch (FormulaParseException e) { - if (!e.getMessage().equals("Found reference to named range \"A\", but that named range wasn't defined!")) { - throw new AssertionFailedError("Identifed bug 44539"); - } - throw e; - } + // Check for: Found reference to named range "A", but that named range wasn't defined! + CellValue result= fe.evaluate(cell); assertEquals(CellType.NUMERIC, result.getCellType()); assertEquals(42.0, result.getNumberValue(), 0.0); } Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java Fri Dec 27 23:00:13 2019 @@ -17,8 +17,8 @@ package org.apache.poi.hssf.model; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import org.apache.poi.ss.formula.ptg.AddPtg; import org.apache.poi.ss.formula.ptg.AttrPtg; @@ -33,11 +33,12 @@ import org.apache.poi.ss.formula.ptg.Not import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.RefPtg; import org.apache.poi.ss.formula.ptg.StringPtg; +import org.junit.Test; /** * Tests <tt>FormulaParser</tt> specifically with respect to IF() functions */ -public final class TestFormulaParserIf extends TestCase { +public final class TestFormulaParserIf { private static Ptg[] parseFormula(String formula) { return TestFormulaParser.parseFormula(formula); } @@ -48,18 +49,15 @@ public final class TestFormulaParserIf e private static void confirmAttrData(Ptg[] ptgs, int i, int expectedData) { Ptg ptg = ptgs[i]; - if (!(ptg instanceof AttrPtg)) { - throw new AssertionFailedError("Token[" + i + "] was not AttrPtg as expected"); - } + assertTrue("Token[" + i + "] was not AttrPtg as expected", ptg instanceof AttrPtg); AttrPtg attrPtg = (AttrPtg) ptg; assertEquals(expectedData, attrPtg.getData()); } + @Test public void testSimpleIf() { - Class<?>[] expClss; - - expClss = new Class[] { + Class<?>[] expClss = { RefPtg.class, AttrPtg.class, // tAttrIf IntPtg.class, @@ -76,11 +74,10 @@ public final class TestFormulaParserIf e confirmAttrData(ptgs, 5, 3); } + @Test public void testSimpleIfNoFalseParam() { - Class<?>[] expClss; - - expClss = new Class[] { + Class<?>[] expClss = { RefPtg.class, AttrPtg.class, // tAttrIf RefPtg.class, @@ -94,11 +91,10 @@ public final class TestFormulaParserIf e confirmAttrData(ptgs, 3, 3); } + @Test public void testIfWithLargeParams() { - Class<?>[] expClss; - - expClss = new Class[] { + Class<?>[] expClss = { RefPtg.class, AttrPtg.class, // tAttrIf @@ -126,12 +122,10 @@ public final class TestFormulaParserIf e confirmAttrData(ptgs, 13, 3); } + @Test public void testNestedIf() { - Class<?>[] expClss; - - expClss = new Class[] { - + Class<?>[] expClss = { RefPtg.class, AttrPtg.class, // A tAttrIf RefPtg.class, @@ -165,6 +159,7 @@ public final class TestFormulaParserIf e confirmAttrData(ptgs, 17, 3); } + @Test public void testEmbeddedIf() { Ptg[] ptgs = parseFormula("IF(3>=1,\"*\",IF(4<>1,\"first\",\"second\"))"); assertEquals(17, ptgs.length); @@ -174,19 +169,22 @@ public final class TestFormulaParserIf e assertEquals("15th Ptg is not the inner IF variable function ptg",FuncVarPtg.class,ptgs[14].getClass()); } - + @Test public void testSimpleLogical() { Ptg[] ptgs = parseFormula("IF(A1<A2,B1,B2)"); assertEquals(9, ptgs.length); assertEquals("3rd Ptg is less than", LessThanPtg.class, ptgs[2].getClass()); } + @Test public void testParenIf() { Ptg[] ptgs = parseFormula("IF((A1+A2)<=3,\"yes\",\"no\")"); assertEquals(12, ptgs.length); assertEquals("6th Ptg is less than equal",LessEqualPtg.class,ptgs[5].getClass()); assertEquals("11th Ptg is not a goto (Attr) ptg",AttrPtg.class,ptgs[10].getClass()); } + + @Test public void testYN() { Ptg[] ptgs = parseFormula("IF(TRUE,\"Y\",\"N\")"); assertEquals(7, ptgs.length); @@ -204,10 +202,11 @@ public final class TestFormulaParserIf e assertEquals("IF", funif.toFormulaString()); assertTrue("tAttrSkip ptg exists", goto1.isSkip()); } + /** * Make sure the ptgs are generated properly with two functions embedded - * */ + @Test public void testNestedFunctionIf() { Ptg[] ptgs = parseFormula("IF(A1=B1,AVERAGE(A1:B1),AVERAGE(A2:B2))"); assertEquals(11, ptgs.length); @@ -219,6 +218,7 @@ public final class TestFormulaParserIf e assertTrue("Average Function set correctly", (ptgs[5] instanceof FuncVarPtg)); } + @Test public void testIfSingleCondition(){ Ptg[] ptgs = parseFormula("IF(1=1,10)"); assertEquals(7, ptgs.length); Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java Fri Dec 27 23:00:13 2019 @@ -17,7 +17,12 @@ package org.apache.poi.hssf.model; -import junit.framework.TestCase; +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.assertTrue; + import org.apache.poi.ddf.EscherChildAnchorRecord; import org.apache.poi.ddf.EscherClientAnchorRecord; import org.apache.poi.ddf.EscherClientDataRecord; @@ -34,10 +39,11 @@ import org.apache.poi.hssf.usermodel.HSS import org.apache.poi.hssf.usermodel.HSSFTestHelper; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType; -import org.junit.Assert; +import org.junit.Test; -public class TestHSSFAnchor extends TestCase { +public class TestHSSFAnchor { + @Test public void testDefaultValues(){ HSSFClientAnchor clientAnchor = new HSSFClientAnchor(); assertEquals(clientAnchor.getAnchorType(), AnchorType.MOVE_AND_RESIZE); @@ -74,6 +80,7 @@ public class TestHSSFAnchor extends Test assertEquals(childAnchor.getDy2(), 0); } + @Test public void testCorrectOrderInSpContainer(){ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls"); HSSFSheet sheet = wb.getSheet("pictures"); @@ -94,6 +101,7 @@ public class TestHSSFAnchor extends Test assertEquals(HSSFTestHelper.getEscherContainer(rectangle).getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID); } + @Test public void testCreateClientAnchorFromContainer(){ EscherContainerRecord container = new EscherContainerRecord(); EscherClientAnchorRecord escher = new EscherClientAnchorRecord(); @@ -128,6 +136,7 @@ public class TestHSSFAnchor extends Test assertEquals(escher.getDy2(), 18); } + @Test public void testCreateChildAnchorFromContainer(){ EscherContainerRecord container = new EscherContainerRecord(); EscherChildAnchorRecord escher = new EscherChildAnchorRecord(); @@ -149,6 +158,7 @@ public class TestHSSFAnchor extends Test assertEquals(escher.getDy2(), 18); } + @Test public void testShapeEscherMustHaveAnchorRecord(){ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); @@ -167,6 +177,7 @@ public class TestHSSFAnchor extends Test assertEquals(HSSFTestHelper.getEscherAnchor(anchor), HSSFTestHelper.getEscherContainer(rectangle).getChildById(EscherClientAnchorRecord.RECORD_ID)); } + @Test public void testClientAnchorFromEscher(){ EscherClientAnchorRecord escher = new EscherClientAnchorRecord(); escher.setCol1((short)11); @@ -197,6 +208,7 @@ public class TestHSSFAnchor extends Test assertEquals(escher.getDy2(), 18); } + @Test public void testClientAnchorFromScratch(){ HSSFClientAnchor anchor = new HSSFClientAnchor(); EscherClientAnchorRecord escher = (EscherClientAnchorRecord) HSSFTestHelper.getEscherAnchor(anchor); @@ -245,6 +257,7 @@ public class TestHSSFAnchor extends Test assertEquals(escher.getDy2(), 118); } + @Test public void testChildAnchorFromEscher(){ EscherChildAnchorRecord escher = new EscherChildAnchorRecord(); escher.setDx1((short) 15); @@ -263,6 +276,7 @@ public class TestHSSFAnchor extends Test assertEquals(escher.getDy2(), 18); } + @Test public void testChildAnchorFromScratch(){ HSSFChildAnchor anchor = new HSSFChildAnchor(); EscherChildAnchorRecord escher = (EscherChildAnchorRecord) HSSFTestHelper.getEscherAnchor(anchor); @@ -291,6 +305,7 @@ public class TestHSSFAnchor extends Test assertEquals(escher.getDy2(), 118); } + @Test public void testEqualsToSelf(){ HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7); assertEquals(clientAnchor, clientAnchor); @@ -299,6 +314,7 @@ public class TestHSSFAnchor extends Test assertEquals(childAnchor, childAnchor); } + @Test public void testPassIncompatibleTypeIsFalse(){ HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7); assertNotSame(clientAnchor, "wrongType"); @@ -307,14 +323,16 @@ public class TestHSSFAnchor extends Test assertNotSame(childAnchor, "wrongType"); } + @Test public void testNullReferenceIsFalse() { HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7); - Assert.assertNotNull("Passing null to equals should return false", clientAnchor); + assertNotNull("Passing null to equals should return false", clientAnchor); HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3); assertNotNull("Passing null to equals should return false", childAnchor); } + @Test public void testEqualsIsReflexiveIsSymmetric() { HSSFClientAnchor clientAnchor1 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7); HSSFClientAnchor clientAnchor2 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7); @@ -329,6 +347,7 @@ public class TestHSSFAnchor extends Test assertEquals(childAnchor2, childAnchor1); } + @Test public void testEqualsValues(){ HSSFClientAnchor clientAnchor1 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7); HSSFClientAnchor clientAnchor2 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7); @@ -403,6 +422,7 @@ public class TestHSSFAnchor extends Test assertEquals(childAnchor1, childAnchor2); } + @Test public void testFlipped(){ HSSFChildAnchor child = new HSSFChildAnchor(2,2,1,1); assertTrue(child.isHorizontallyFlipped()); Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestLinkTable.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestLinkTable.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestLinkTable.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestLinkTable.java Fri Dec 27 23:00:13 2019 @@ -17,15 +17,19 @@ package org.apache.poi.hssf.model; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; - import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.CountryRecord; @@ -40,13 +44,12 @@ import org.apache.poi.hssf.record.SupBoo import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.formula.ptg.NameXPtg; +import org.junit.Test; /** * Tests for {@link LinkTable} - * - * @author Josh Micich */ -public final class TestLinkTable extends TestCase { +public final class TestLinkTable { /** * The example file attached to bugzilla 45046 is a clear example of Name records being present @@ -55,45 +58,30 @@ public final class TestLinkTable extends * * It's not clear what exact steps need to be taken in Excel to create such a workbook */ + @Test public void testLinkTableWithoutExternalBookRecord_bug45046() { - HSSFWorkbook wb; - - try { - wb = HSSFTestDataSamples.openSampleWorkbook("ex45046-21984.xls"); - } catch (RuntimeException e) { - if ("DEFINEDNAME is part of LinkTable".equals(e.getMessage())) { - throw new AssertionFailedError("Identified bug 45046 b"); - } - throw e; - } + // Bug 45046 b: DEFINEDNAME is part of LinkTable + HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex45046-21984.xls"); // some other sanity checks assertEquals(3, wb.getNumberOfSheets()); String formula = wb.getSheetAt(0).getRow(4).getCell(13).getCellFormula(); - if ("ipcSummenproduktIntern($P5,N$6,$A$9,N$5)".equals(formula)) { - // The reported symptom of this bugzilla is an earlier bug (already fixed) - throw new AssertionFailedError("Identified bug 41726"); - // This is observable in version 3.0 - } + // The reported symptom of this bugzilla is an earlier bug (already fixed) + // This is observable in version 3.0 + assertNotEquals("ipcSummenproduktIntern($P5,N$6,$A$9,N$5)", formula); assertEquals("ipcSummenproduktIntern($C5,N$2,$A$9,N$1)", formula); } + @Test public void testMultipleExternSheetRecords_bug45698() { - HSSFWorkbook wb; - - try { - wb = HSSFTestDataSamples.openSampleWorkbook("ex45698-22488.xls"); - } catch (RuntimeException e) { - if ("Extern sheet is part of LinkTable".equals(e.getMessage())) { - throw new AssertionFailedError("Identified bug 45698"); - } - throw e; - } + // Bug: Extern sheet is part of LinkTable + HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex45698-22488.xls"); // some other sanity checks assertEquals(7, wb.getNumberOfSheets()); } + @Test public void testExtraSheetRefs_bug45978() { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex45978-extraLinkTableSheets.xls"); /* @@ -123,15 +111,8 @@ public final class TestLinkTable extends */ HSSFCell cell = wb.getSheetAt(0).getRow(1).getCell(1); - String cellFormula; - try { - cellFormula = cell.getCellFormula(); - } catch (IndexOutOfBoundsException e) { - if (e.getMessage().equals("Index: 2, Size: 2")) { - throw new AssertionFailedError("Identified bug 45798"); - } - throw e; - } + // Bug: IndexOutOfBoundsException - Index: 2, Size: 2 + String cellFormula = cell.getCellFormula(); assertEquals("Data!$A2", cellFormula); } @@ -139,31 +120,22 @@ public final class TestLinkTable extends * This problem was visible in POI svn r763332 * when reading the workbook of attachment 23468 from bugzilla 47001 */ + @Test public void testMissingExternSheetRecord_bug47001b() { - + Record[] recs = { SupBookRecord.createAddInFunctions(), new SSTRecord(), }; List<Record> recList = Arrays.asList(recs); WorkbookRecordList wrl = new WorkbookRecordList(); - - LinkTable lt; - try { - lt = new LinkTable(recList, 0, wrl, Collections.emptyMap()); - } catch (RuntimeException e) { - if (e.getMessage().equals("Expected an EXTERNSHEET record but got (org.apache.poi.hssf.record.SSTRecord)")) { - throw new AssertionFailedError("Identified bug 47001b"); - } - - throw e; - } + + // Bug 47001b: Expected an EXTERNSHEET record but got (org.apache.poi.hssf.record.SSTRecord) + LinkTable lt = new LinkTable(recList, 0, wrl, Collections.emptyMap()); assertNotNull(lt); - } + } - /** - * - */ + @Test public void testNameCommentRecordBetweenNameRecords() { final Record[] recs = { @@ -184,9 +156,10 @@ public final class TestLinkTable extends assertSame(recs[1], commentRecords.get("name1")); //== is intentionally not .equals()! assertSame(recs[3], commentRecords.get("name2")); //== is intentionally not .equals()! - assertEquals(2, lt.getNumNames()); + assertEquals(2, lt.getNumNames()); } + @Test public void testAddNameX(){ WorkbookRecordList wrl = new WorkbookRecordList(); wrl.add(0, new BOFRecord()); @@ -217,13 +190,17 @@ public final class TestLinkTable extends NameXPtg namex1 = tbl.addNameXPtg("ISODD"); // adds two new rercords assertEquals(0, namex1.getSheetRefIndex()); assertEquals(0, namex1.getNameIndex()); - assertEquals(namex1.toString(), tbl.getNameXPtg("ISODD", -1).toString()); - + NameXPtg act = tbl.getNameXPtg("ISODD", -1); + assertNotNull(act); + assertEquals(namex1.toString(), act.toString()); + // Can only find on the right sheet ref, if restricting - assertEquals(namex1.toString(), tbl.getNameXPtg("ISODD", 0).toString()); + act = tbl.getNameXPtg("ISODD", 0); + assertNotNull(act); + assertEquals(namex1.toString(), act.toString()); assertNull(tbl.getNameXPtg("ISODD", 1)); assertNull(tbl.getNameXPtg("ISODD", 2)); - + // assure they are in place: // [BOFRecord] // [CountryRecord] @@ -251,7 +228,9 @@ public final class TestLinkTable extends NameXPtg namex2 = tbl.addNameXPtg("ISEVEN"); // adds two new rercords assertEquals(0, namex2.getSheetRefIndex()); assertEquals(1, namex2.getNameIndex()); // name index increased by one - assertEquals(namex2.toString(), tbl.getNameXPtg("ISEVEN", -1).toString()); + act = tbl.getNameXPtg("ISEVEN", -1); + assertNotNull(act); + assertEquals(namex2.toString(), act.toString()); assertEquals(8, wrl.getRecords().size()); // assure they are in place: // [BOFRecord] Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestOperandClassTransformer.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestOperandClassTransformer.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestOperandClassTransformer.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestOperandClassTransformer.java Fri Dec 27 23:00:13 2019 @@ -17,8 +17,10 @@ package org.apache.poi.hssf.model; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +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 org.apache.poi.ss.formula.eval.BlankEval; import org.apache.poi.ss.formula.eval.ErrorEval; @@ -29,13 +31,13 @@ import org.apache.poi.ss.formula.functio import org.apache.poi.ss.formula.ptg.AbstractFunctionPtg; import org.apache.poi.ss.formula.ptg.FuncVarPtg; import org.apache.poi.ss.formula.ptg.Ptg; +import org.junit.Ignore; +import org.junit.Test; /** * Tests specific formula examples in <tt>OperandClassTransformer</tt>. - * - * @author Josh Micich */ -public final class TestOperandClassTransformer extends TestCase { +public final class TestOperandClassTransformer { private static Ptg[] parseFormula(String formula) { Ptg[] result = HSSFFormulaParser.parse(formula, null); @@ -43,6 +45,7 @@ public final class TestOperandClassTrans return result; } + @Test public void testMdeterm() { String formula = "MDETERM(ABS(A1))"; Ptg[] ptgs = parseFormula(formula); @@ -52,6 +55,7 @@ public final class TestOperandClassTrans confirmFuncClass(ptgs, 2, "MDETERM", Ptg.CLASS_VALUE); } + @Test public void testMdetermReturnsValueInvalidOnABlankCell() { ValueEval matrixRef = EvalFactory.createAreaEval("A1:B2", new ValueEval[]{ @@ -74,7 +78,9 @@ public final class TestOperandClassTrans * <p> * This test has been added but disabled in order to document this issue. */ - public void DISABLED_testIndexPi1() { + @Test + @Ignore + public void testIndexPi1() { String formula = "INDEX(PI(),1)"; Ptg[] ptgs = parseFormula(formula); @@ -86,13 +92,11 @@ public final class TestOperandClassTrans * Even though count expects args of type R, because A1 is a direct operand of a * value operator it must get type V */ + @Test public void testDirectOperandOfValueOperator() { String formula = "COUNT(A1*1)"; Ptg[] ptgs = parseFormula(formula); - if (ptgs[0].getPtgClass() == Ptg.CLASS_REF) { - throw new AssertionFailedError("Identified bug 45348"); - } - + assertNotEquals(Ptg.CLASS_REF, ptgs[0].getPtgClass()); confirmTokenClass(ptgs, 0, Ptg.CLASS_VALUE); confirmTokenClass(ptgs, 3, Ptg.CLASS_VALUE); } @@ -100,6 +104,7 @@ public final class TestOperandClassTrans /** * A cell ref passed to a function expecting type V should be converted to type V */ + @Test public void testRtoV() { String formula = "lookup(A1, A3:A52, B3:B52)"; @@ -107,6 +112,7 @@ public final class TestOperandClassTrans confirmTokenClass(ptgs, 0, Ptg.CLASS_VALUE); } + @Test public void testComplexIRR_bug45041() { String formula = "(1+IRR(SUMIF(A:A,ROW(INDIRECT(MIN(A:A)&\":\"&MAX(A:A))),B:B),0))^365-1"; Ptg[] ptgs = parseFormula(formula); @@ -116,9 +122,8 @@ public final class TestOperandClassTrans assertEquals("ROW", rowFunc.getName()); assertEquals("SUMIF", sumifFunc.getName()); - if (rowFunc.getPtgClass() == Ptg.CLASS_VALUE || sumifFunc.getPtgClass() == Ptg.CLASS_VALUE) { - throw new AssertionFailedError("Identified bug 45041"); - } + assertNotEquals(Ptg.CLASS_VALUE, rowFunc.getPtgClass()); + assertNotEquals(Ptg.CLASS_VALUE, sumifFunc.getPtgClass()); confirmTokenClass(ptgs, 1, Ptg.CLASS_REF); confirmTokenClass(ptgs, 2, Ptg.CLASS_REF); confirmFuncClass(ptgs, 3, "MIN", Ptg.CLASS_VALUE); @@ -139,25 +144,7 @@ public final class TestOperandClassTrans private void confirmTokenClass(Ptg[] ptgs, int i, byte operandClass) { Ptg ptg = ptgs[i]; - if (ptg.isBaseToken()) { - throw new AssertionFailedError("ptg[" + i + "] is a base token"); - } - if (operandClass != ptg.getPtgClass()) { - throw new AssertionFailedError("Wrong operand class for ptg (" - + ptg + "). Expected " + getOperandClassName(operandClass) - + " but got " + getOperandClassName(ptg.getPtgClass())); - } - } - - private static String getOperandClassName(byte ptgClass) { - switch (ptgClass) { - case Ptg.CLASS_REF: - return "R"; - case Ptg.CLASS_VALUE: - return "V"; - case Ptg.CLASS_ARRAY: - return "A"; - } - throw new RuntimeException("Unknown operand class (" + ptgClass + ")"); + assertFalse("ptg[" + i + "] is a base token", ptg.isBaseToken()); + assertEquals(operandClass, ptg.getPtgClass()); } } Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestSheet.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestSheet.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestSheet.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestSheet.java Fri Dec 27 23:00:13 2019 @@ -19,40 +19,19 @@ package org.apache.poi.hssf.model; 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.assertTrue; import static org.junit.Assert.fail; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Iterator; import java.util.List; import org.apache.poi.ddf.EscherDggRecord; import org.apache.poi.hssf.HSSFTestDataSamples; -import org.apache.poi.hssf.record.BOFRecord; -import org.apache.poi.hssf.record.BlankRecord; -import org.apache.poi.hssf.record.CellValueRecordInterface; -import org.apache.poi.hssf.record.ColumnInfoRecord; -import org.apache.poi.hssf.record.DimensionsRecord; -import org.apache.poi.hssf.record.DrawingRecord; -import org.apache.poi.hssf.record.EOFRecord; -import org.apache.poi.hssf.record.EscherAggregate; -import org.apache.poi.hssf.record.FormulaRecord; -import org.apache.poi.hssf.record.GutsRecord; -import org.apache.poi.hssf.record.IndexRecord; -import org.apache.poi.hssf.record.MergeCellsRecord; -import org.apache.poi.hssf.record.MulBlankRecord; -import org.apache.poi.hssf.record.NoteRecord; -import org.apache.poi.hssf.record.NumberRecord; -import org.apache.poi.hssf.record.ObjRecord; -import org.apache.poi.hssf.record.Record; -import org.apache.poi.hssf.record.RecordBase; -import org.apache.poi.hssf.record.RowRecord; -import org.apache.poi.hssf.record.StringRecord; -import org.apache.poi.hssf.record.TextObjectRecord; -import org.apache.poi.hssf.record.UncalcedRecord; -import org.apache.poi.hssf.record.WindowTwoRecord; +import org.apache.poi.hssf.record.*; import org.apache.poi.hssf.record.aggregates.ConditionalFormattingTable; import org.apache.poi.hssf.record.aggregates.PageSettingsBlock; import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; @@ -61,15 +40,12 @@ import org.apache.poi.hssf.usermodel.HSS import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.formula.FormulaShifter; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.util.HexRead; import org.junit.Test; -import junit.framework.AssertionFailedError; - /** * Unit test for the {@link InternalSheet} class. */ @@ -78,12 +54,6 @@ public final class TestSheet { return InternalSheet.createSheet(new RecordStream(inRecs, 0)); } - private static Record[] getSheetRecords(InternalSheet s, int offset) { - RecordCollector rc = new RecordCollector(); - s.visitContainedRecords(rc, offset); - return rc.getRecords(); - } - @Test public void testCreateSheet() { // Check we're adding row and cell aggregates @@ -93,14 +63,16 @@ public final class TestSheet { records.add(createWindow2Record()); records.add(EOFRecord.instance); InternalSheet sheet = createSheet(records); - Record[] outRecs = getSheetRecords(sheet, 0); - int pos = 0; - assertTrue(outRecs[pos++] instanceof BOFRecord ); - assertTrue(outRecs[pos++] instanceof IndexRecord); - assertTrue(outRecs[pos++] instanceof DimensionsRecord); - assertTrue(outRecs[pos++] instanceof WindowTwoRecord ); - assertTrue(outRecs[pos++] instanceof EOFRecord); + List<Record> outRecs = new ArrayList<>(); + sheet.visitContainedRecords(outRecs::add, 0); + + Iterator<Record> iter = outRecs.iterator(); + assertTrue(iter.next() instanceof BOFRecord ); + assertTrue(iter.next() instanceof IndexRecord); + assertTrue(iter.next() instanceof DimensionsRecord); + assertTrue(iter.next() instanceof WindowTwoRecord ); + assertTrue(iter.next() instanceof EOFRecord); } private static Record createWindow2Record() { @@ -134,7 +106,7 @@ public final class TestSheet { @Test public void testAddMergedRegion() { InternalSheet sheet = InternalSheet.createSheet(); - int regionsToAdd = 4096; + final int regionsToAdd = 4096; //simple test that adds a load of regions for (int n = 0; n < regionsToAdd; n++) @@ -151,8 +123,10 @@ public final class TestSheet { sheet.visitContainedRecords(mcListener, 0); int recordsAdded = mcListener.getCount(); int recordsExpected = regionsToAdd/1027; - if ((regionsToAdd % 1027) != 0) + //noinspection ConstantConditions + if ((regionsToAdd % 1027) != 0) { recordsExpected++; + } assertEquals("The " + regionsToAdd + " merged regions should have been spread out over " + recordsExpected + " records, not " + recordsAdded, recordsAdded, recordsExpected); // Check we can't add one with invalid date @@ -387,11 +361,10 @@ public final class TestSheet { public void testXFIndexForColumn() { final short TEST_IDX = 10; final short DEFAULT_IDX = 0xF; // 15 - short xfindex = Short.MIN_VALUE; InternalSheet sheet = InternalSheet.createSheet(); // without ColumnInfoRecord - xfindex = sheet.getXFIndexForColAt((short) 0); + int xfindex = sheet.getXFIndexForColAt((short) 0); assertEquals(DEFAULT_IDX, xfindex); xfindex = sheet.getXFIndexForColAt((short) 1); assertEquals(DEFAULT_IDX, xfindex); @@ -450,28 +423,6 @@ public final class TestSheet { assertEquals(DEFAULT_IDX, xfindex); } - private static final class SizeCheckingRecordVisitor implements RecordVisitor { - - private int _totalSize; - public SizeCheckingRecordVisitor() { - _totalSize = 0; - } - @Override - public void visitRecord(Record r) { - - int estimatedSize=r.getRecordSize(); - byte[] buf = new byte[estimatedSize]; - int serializedSize = r.serialize(0, buf); - if (estimatedSize != serializedSize) { - throw new AssertionFailedError("serialized size mismatch for record (" - + r.getClass().getName() + ")"); - } - _totalSize += estimatedSize; - } - public int getTotalSize() { - return _totalSize; - } - } /** * Prior to bug 45066, POI would get the estimated sheet size wrong * when an <tt>UncalcedRecord</tt> was present.<p> @@ -490,9 +441,16 @@ public final class TestSheet { // The original bug was due to different logic for collecting records for sizing and // serialization. The code has since been refactored into a single method for visiting // all contained records. Now this test is much less interesting - SizeCheckingRecordVisitor scrv = new SizeCheckingRecordVisitor(); - sheet.visitContainedRecords(scrv, 0); - assertEquals(90, scrv.getTotalSize()); + int[] totalSize = { 0 }; + byte[] buf = new byte[100]; + + sheet.visitContainedRecords(r -> { + int estimatedSize = r.getRecordSize(); + int serializedSize = r.serialize(0, buf); + assertEquals("serialized size mismatch for record (" + r.getClass().getName() + ")", estimatedSize, serializedSize); + totalSize[0] += estimatedSize; + }, 0); + assertEquals(90, totalSize[0]); } /** @@ -517,11 +475,9 @@ public final class TestSheet { int dbCellRecordPos = getDbCellRecordPos(sheet); - if (dbCellRecordPos == 252) { - // The overt symptom of the bug - // DBCELL record pos is calculated wrong if VRA comes before RRA - throw new AssertionFailedError("Identified bug 45145"); - } + // The overt symptom of the bug + // DBCELL record pos is calculated wrong if VRA comes before RRA + assertNotEquals (252, dbCellRecordPos); // if (false) { // // make sure that RRA and VRA are in the right place @@ -575,17 +531,10 @@ public final class TestSheet { sheet.addRow(new RowRecord(0)); sheet.addRow(new RowRecord(1)); sheet.groupRowRange( 0, 1, true ); - sheet.toString(); + assertNotNull(sheet.toString()); List<RecordBase> recs = sheet.getRecords(); - int count=0; - for(int i=0; i< recs.size(); i++) { - if (recs.get(i) instanceof GutsRecord) { - count++; - } - } - if (count == 2) { - throw new AssertionFailedError("Identified bug 45640"); - } + long count = recs.stream().filter(r -> r instanceof GutsRecord).count(); + assertNotEquals(2, count); assertEquals(1, count); } @@ -600,7 +549,7 @@ public final class TestSheet { fail("Identified bug 45699"); } assertEquals("Informations", cell.getRichStringCellValue().getString()); - + wb.close(); } /** @@ -645,21 +594,12 @@ public final class TestSheet { inRecs.add(nr); inRecs.add(createWindow2Record()); inRecs.add(EOFRecord.instance); - InternalSheet sheet; - try { - sheet = createSheet(inRecs); - } catch (RuntimeException e) { - if ("DimensionsRecord was not found".equals(e.getMessage())) { - throw new AssertionFailedError("Identified bug 46206"); - } - throw e; - } + InternalSheet sheet = createSheet(inRecs); - RecordCollector rv = new RecordCollector(); - sheet.visitContainedRecords(rv, rowIx); - Record[] outRecs = rv.getRecords(); - assertEquals(8, outRecs.length); - DimensionsRecord dims = (DimensionsRecord) outRecs[5]; + List<Record> outRecs = new ArrayList<>(); + sheet.visitContainedRecords(outRecs::add, rowIx); + assertEquals(8, outRecs.size()); + DimensionsRecord dims = (DimensionsRecord) outRecs.get(5); assertEquals(rowIx, dims.getFirstRow()); assertEquals(rowIx, dims.getLastRow()); assertEquals(colIx, dims.getFirstCol()); @@ -683,9 +623,7 @@ public final class TestSheet { FormulaShifter shifter = FormulaShifter.createForRowShift(0, "", 0, 0, 1, SpreadsheetVersion.EXCEL97); sheet.updateFormulasAfterCellShift(shifter, 0); - if (sheetRecs.size() == 24 && sheetRecs.get(22) instanceof ConditionalFormattingTable) { - throw new AssertionFailedError("Identified bug 46547a"); - } + assertFalse(sheetRecs.size() == 24 && sheetRecs.get(22) instanceof ConditionalFormattingTable); assertEquals(23, sheetRecs.size()); } /** @@ -698,14 +636,8 @@ public final class TestSheet { InternalSheet sheet = InternalSheet.createSheet(); sheet.getOrCreateDataValidityTable(); - ConditionalFormattingTable cft; // attempt to add conditional formatting - try { - - cft = sheet.getConditionalFormattingTable(); // lazy getter - } catch (ClassCastException e) { - throw new AssertionFailedError("Identified bug 46547b"); - } + ConditionalFormattingTable cft = sheet.getConditionalFormattingTable(); assertNotNull(cft); } @@ -723,20 +655,12 @@ public final class TestSheet { InternalSheet sheet = createSheet(Arrays.asList(recs)); - InternalSheet sheet2; - try { - sheet2 = sheet.cloneSheet(); - } catch (RuntimeException e) { - if (e.getMessage().equals("The class org.apache.poi.hssf.record.MulBlankRecord needs to define a clone method")) { - throw new AssertionFailedError("Identified bug 46776"); - } - throw e; - } + InternalSheet sheet2 = sheet.cloneSheet(); - RecordCollector rc = new RecordCollector(); - sheet2.visitContainedRecords(rc, 0); - Record[] clonedRecs = rc.getRecords(); - assertEquals(recs.length+2, clonedRecs.length); // +2 for INDEX and DBCELL + List<Record> clonedRecs = new ArrayList<>(); + sheet2.visitContainedRecords(clonedRecs::add, 0); + // +2 for INDEX and DBCELL + assertEquals(recs.length+2, clonedRecs.size()); } @Test @@ -828,9 +752,10 @@ public final class TestSheet { } @Test - public void testSheetDimensions() throws IOException{ + public void testSheetDimensions() { InternalSheet sheet = InternalSheet.createSheet(); DimensionsRecord dimensions = (DimensionsRecord)sheet.findFirstRecordBySid(DimensionsRecord.sid); + assertNotNull(dimensions); assertEquals(0, dimensions.getFirstCol()); assertEquals(0, dimensions.getFirstRow()); assertEquals(1, dimensions.getLastCol()); // plus pne Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java Fri Dec 27 23:00:13 2019 @@ -17,15 +17,18 @@ package org.apache.poi.hssf.model; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; import org.apache.poi.hssf.record.ColumnInfoRecord; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public final class TestSheetAdditional { + @Rule + public ExpectedException thrown= ExpectedException.none(); -/** - * @author Tony Poppleton - */ -public final class TestSheetAdditional extends TestCase { - + @Test public void testGetCellWidth() { InternalSheet sheet = InternalSheet.createSheet(); ColumnInfoRecord nci = new ColumnInfoRecord(); @@ -55,14 +58,15 @@ public final class TestSheetAdditional e assertEquals(100,sheet.getColumnWidth(10)); } + @Test public void testMaxColumnWidth() { InternalSheet sheet = InternalSheet.createSheet(); - sheet.setColumnWidth(0, 255*256); //the limit - try { - sheet.setColumnWidth(0, 256*256); //the limit - fail("expected exception"); - } catch (IllegalArgumentException e){ - assertEquals(e.getMessage(), "The maximum column width for an individual cell is 255 characters."); - } + // the limit + sheet.setColumnWidth(0, 255*256); + + // over the limit + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("The maximum column width for an individual cell is 255 characters."); + sheet.setColumnWidth(0, 256*256); } } Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/TestArrayRecord.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/TestArrayRecord.java?rev=1872041&r1=1872040&r2=1872041&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/record/TestArrayRecord.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/record/TestArrayRecord.java Fri Dec 27 23:00:13 2019 @@ -17,7 +17,8 @@ package org.apache.poi.hssf.record; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.usermodel.HSSFSheet; @@ -30,9 +31,11 @@ import org.apache.poi.ss.formula.Formula import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.util.HexDump; import org.apache.poi.util.HexRead; +import org.junit.Test; -public final class TestArrayRecord extends TestCase { +public final class TestArrayRecord { + @Test public void testRead() { String hex = "21 02 25 00 01 00 01 00 01 01 00 00 00 00 00 00 " + @@ -58,6 +61,7 @@ public final class TestArrayRecord exten assertEquals(HexDump.toHex(data), HexDump.toHex(ser)); } + @Test public void testBug57231() { HSSFWorkbook wb = HSSFTestDataSamples .openSampleWorkbook("57231_MixedGasReport.xls"); --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org For additional commands, e-mail: commits-h...@poi.apache.org