Search in sources :

Example 1 with RecordCollector

use of org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector in project poi by apache.

the class TestSheet method testCloneMulBlank_bug46776.

@Test
public void testCloneMulBlank_bug46776() {
    Record[] recs = { InternalSheet.createBOF(), new DimensionsRecord(), new RowRecord(1), new MulBlankRecord(1, 3, new short[] { 0x0F, 0x0F, 0x0F }), new RowRecord(2), createWindow2Record(), EOFRecord.instance };
    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;
    }
    RecordCollector rc = new RecordCollector();
    sheet2.visitContainedRecords(rc, 0);
    Record[] clonedRecs = rc.getRecords();
    // +2 for INDEX and DBCELL
    assertEquals(recs.length + 2, clonedRecs.length);
}
Also used : MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) RowRecord(org.apache.poi.hssf.record.RowRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) GutsRecord(org.apache.poi.hssf.record.GutsRecord) MergeCellsRecord(org.apache.poi.hssf.record.MergeCellsRecord) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) Record(org.apache.poi.hssf.record.Record) ObjRecord(org.apache.poi.hssf.record.ObjRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) ColumnInfoRecord(org.apache.poi.hssf.record.ColumnInfoRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) EscherDggRecord(org.apache.poi.ddf.EscherDggRecord) TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) IndexRecord(org.apache.poi.hssf.record.IndexRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) UncalcedRecord(org.apache.poi.hssf.record.UncalcedRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) AssertionFailedError(junit.framework.AssertionFailedError) Test(org.junit.Test)

Example 2 with RecordCollector

use of org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector in project poi by apache.

the class TestSheet method getSheetRecords.

private static Record[] getSheetRecords(InternalSheet s, int offset) {
    RecordCollector rc = new RecordCollector();
    s.visitContainedRecords(rc, offset);
    return rc.getRecords();
}
Also used : RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector)

Example 3 with RecordCollector

use of org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector in project poi by apache.

the class TestPageSettingsBlock method testDuplicateHeaderFooterInside_bug48026.

public void testDuplicateHeaderFooterInside_bug48026() {
    Record[] recs = { BOFRecord.createSheetBOF(), new IndexRecord(), //PageSettingsBlock
    new HeaderRecord("&LDecember"), new FooterRecord("&LJanuary"), new DimensionsRecord(), new WindowTwoRecord(), //CustomViewSettingsRecordAggregate
    new UserSViewBegin(HexRead.readFromString("53 CE BD CC DE 38 44 45 97 C1 5C 89 F9 37 32 1B 01 00 00 00 64 00 00 00 40 00 00 00 03 00 00 00 7D 00 00 20 00 00 34 00 00 00 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF FF FF FF")), new SelectionRecord(0, 0), // the other is matched with a CustomViewSettingsRecordAggregate having UserSViewBegin with the same GUID
    new HeaderFooterRecord(HexRead.readFromString("9C 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 34 33 00 00 00 00 00 00 00 00")), new HeaderFooterRecord(HexRead.readFromString("9C 08 00 00 00 00 00 00 00 00 00 00 53 CE BD CC DE 38 44 45 97 C1 5C 89 F9 37 32 1B 34 33 00 00 00 00 00 00 00 00")), new UserSViewEnd(HexRead.readFromString("01 00")), EOFRecord.instance };
    RecordStream rs = new RecordStream(Arrays.asList(recs), 0);
    InternalSheet sheet;
    try {
        sheet = InternalSheet.createSheet(rs);
    } catch (RuntimeException e) {
        if (e.getMessage().equals("Duplicate PageSettingsBlock record (sid=0x89c)")) {
            throw new AssertionFailedError("Identified bug 48026");
        }
        throw e;
    }
    RecordCollector rv = new RecordCollector();
    sheet.visitContainedRecords(rv, 0);
    Record[] outRecs = rv.getRecords();
    assertEquals(recs.length + 1, outRecs.length);
    //expected order of records:
    Record[] expectedRecs = { //BOFRecord
    recs[0], //IndexRecord
    recs[1], //HeaderRecord
    recs[2], //FooterRecord
    recs[3], // DimensionsRecord
    recs[4], // WindowTwoRecord
    recs[5], // UserSViewBegin
    recs[6], // SelectionRecord
    recs[7], //HeaderRecord
    recs[2], //FooterRecord
    recs[3], // HeaderFooterRecord
    recs[8], // UserSViewEnd
    recs[10], //EOFRecord
    recs[11] };
    for (int i = 0; i < expectedRecs.length; i++) {
        assertEquals("Record mismatch at index " + i, expectedRecs[i].getClass(), outRecs[i].getClass());
    }
    HeaderFooterRecord hd1 = (HeaderFooterRecord) expectedRecs[10];
    //GUID is zero
    assertArrayEquals(new byte[16], hd1.getGuid());
    assertTrue(hd1.isCurrentSheet());
    UserSViewBegin svb = (UserSViewBegin) expectedRecs[6];
    HeaderFooterRecord hd2 = (HeaderFooterRecord) recs[9];
    assertFalse(hd2.isCurrentSheet());
    //GUIDs of HeaderFooterRecord and UserSViewBegin must be the same
    assertArrayEquals(svb.getGuid(), hd2.getGuid());
}
Also used : RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) RecordStream(org.apache.poi.hssf.model.RecordStream) InternalSheet(org.apache.poi.hssf.model.InternalSheet) AssertionFailedError(junit.framework.AssertionFailedError)

Example 4 with RecordCollector

use of org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector in project poi by apache.

the class TestPageSettingsBlock method testMissingHeaderFooter.

/**
	 * Excel tolerates missing header / footer records, but adds them (empty) in when re-saving.
	 * This is not critical functionality but it has been decided to keep POI consistent with
	 * Excel in this regard.
	 */
public void testMissingHeaderFooter() {
    // initialise PSB with some records, but not the header / footer
    Record[] recs = { new HCenterRecord(), new VCenterRecord() };
    RecordStream rs = new RecordStream(Arrays.asList(recs), 0);
    PageSettingsBlock psb = new PageSettingsBlock(rs);
    // serialize the PSB to see what records come out
    RecordCollector rc = new RecordCollector();
    psb.visitContainedRecords(rc);
    Record[] outRecs = rc.getRecords();
    if (outRecs.length == 2) {
        throw new AssertionFailedError("PageSettingsBlock didn't add missing header/footer records");
    }
    assertEquals(4, outRecs.length);
    assertEquals(HeaderRecord.class, outRecs[0].getClass());
    assertEquals(FooterRecord.class, outRecs[1].getClass());
    assertEquals(HCenterRecord.class, outRecs[2].getClass());
    assertEquals(VCenterRecord.class, outRecs[3].getClass());
    // make sure the added header / footer records are empty
    HeaderRecord hr = (HeaderRecord) outRecs[0];
    assertEquals("", hr.getText());
    FooterRecord fr = (FooterRecord) outRecs[1];
    assertEquals("", fr.getText());
}
Also used : RecordStream(org.apache.poi.hssf.model.RecordStream) RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) AssertionFailedError(junit.framework.AssertionFailedError)

Example 5 with RecordCollector

use of org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector in project poi by apache.

the class TestPageSettingsBlock method testLateHeaderFooter_bug46953.

/**
	 * Bug 46953 occurred because POI didn't handle late PSB records properly.
	 */
public void testLateHeaderFooter_bug46953() {
    int rowIx = 5;
    int colIx = 6;
    NumberRecord nr = new NumberRecord();
    nr.setRow(rowIx);
    nr.setColumn((short) colIx);
    nr.setValue(3.0);
    Record[] recs = { BOFRecord.createSheetBOF(), new HeaderRecord("&LSales Figures"), new FooterRecord("&LJanuary"), new DimensionsRecord(), new WindowTwoRecord(), new HeaderFooterRecord(HexRead.readFromString("9C 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 C4 60 00 00 00 00 00 00 00 00")), EOFRecord.instance };
    RecordStream rs = new RecordStream(Arrays.asList(recs), 0);
    InternalSheet sheet = InternalSheet.createSheet(rs);
    RecordCollector rv = new RecordCollector();
    sheet.visitContainedRecords(rv, 0);
    Record[] outRecs = rv.getRecords();
    if (outRecs[4] == EOFRecord.instance) {
        throw new AssertionFailedError("Identified bug 46953 - EOF incorrectly appended to PSB");
    }
    // +1 for index record
    assertEquals(recs.length + 1, outRecs.length);
    assertEquals(BOFRecord.class, outRecs[0].getClass());
    assertEquals(IndexRecord.class, outRecs[1].getClass());
    assertEquals(HeaderRecord.class, outRecs[2].getClass());
    assertEquals(FooterRecord.class, outRecs[3].getClass());
    assertEquals(HeaderFooterRecord.class, outRecs[4].getClass());
    assertEquals(DimensionsRecord.class, outRecs[5].getClass());
    assertEquals(WindowTwoRecord.class, outRecs[6].getClass());
    assertEquals(EOFRecord.instance, outRecs[7]);
}
Also used : RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) RecordStream(org.apache.poi.hssf.model.RecordStream) InternalSheet(org.apache.poi.hssf.model.InternalSheet) AssertionFailedError(junit.framework.AssertionFailedError)

Aggregations

RecordCollector (org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector)14 AssertionFailedError (junit.framework.AssertionFailedError)10 RecordStream (org.apache.poi.hssf.model.RecordStream)8 InternalSheet (org.apache.poi.hssf.model.InternalSheet)5 Record (org.apache.poi.hssf.record.Record)5 Test (org.junit.Test)5 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)4 DimensionsRecord (org.apache.poi.hssf.record.DimensionsRecord)3 NumberRecord (org.apache.poi.hssf.record.NumberRecord)3 ObjRecord (org.apache.poi.hssf.record.ObjRecord)3 RowRecord (org.apache.poi.hssf.record.RowRecord)3 StringRecord (org.apache.poi.hssf.record.StringRecord)3 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)3 EscherDggRecord (org.apache.poi.ddf.EscherDggRecord)2 BOFRecord (org.apache.poi.hssf.record.BOFRecord)2 BlankRecord (org.apache.poi.hssf.record.BlankRecord)2 ColumnInfoRecord (org.apache.poi.hssf.record.ColumnInfoRecord)2 DrawingRecord (org.apache.poi.hssf.record.DrawingRecord)2 EOFRecord (org.apache.poi.hssf.record.EOFRecord)2 GutsRecord (org.apache.poi.hssf.record.GutsRecord)2