Search in sources :

Example 1 with InternalSheet

use of org.apache.poi.hssf.model.InternalSheet in project poi by apache.

the class TestBugs method bug51675.

@Test
public void bug51675() throws Exception {
    final List<Short> list = new ArrayList<Short>();
    HSSFWorkbook wb = openSample("51675.xls");
    HSSFSheet sh = wb.getSheetAt(0);
    InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
    PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13);
    psb.visitContainedRecords(new RecordAggregate.RecordVisitor() {

        @Override
        public void visitRecord(Record r) {
            list.add(r.getSid());
        }
    });
    assertEquals(UnknownRecord.BITMAP_00E9, list.get(list.size() - 1).intValue());
    assertEquals(UnknownRecord.HEADER_FOOTER_089C, list.get(list.size() - 2).intValue());
    wb.close();
}
Also used : InternalSheet(org.apache.poi.hssf.model.InternalSheet) ArrayList(java.util.ArrayList) PageSettingsBlock(org.apache.poi.hssf.record.aggregates.PageSettingsBlock) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) Record(org.apache.poi.hssf.record.Record) EmbeddedObjectRefSubRecord(org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord) CommonObjectDataSubRecord(org.apache.poi.hssf.record.CommonObjectDataSubRecord) NameRecord(org.apache.poi.hssf.record.NameRecord) TabIdRecord(org.apache.poi.hssf.record.TabIdRecord) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate) RecordAggregate(org.apache.poi.hssf.record.aggregates.RecordAggregate) Test(org.junit.Test)

Example 2 with InternalSheet

use of org.apache.poi.hssf.model.InternalSheet in project poi by apache.

the class TestHSSFWorkbook method sheetSerializeSizeMismatch_bug45066.

/**
     * If Sheet.getSize() returns a different result to Sheet.serialize(), this will cause the BOF
     * records to be written with invalid offset indexes.  Excel does not like this, and such
     * errors are particularly hard to track down.  This test ensures that HSSFWorkbook throws
     * a specific exception as soon as the situation is detected. See bugzilla 45066
     * @throws IOException 
     */
@Test
public void sheetSerializeSizeMismatch_bug45066() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    InternalSheet sheet = wb.createSheet("Sheet1").getSheet();
    List<RecordBase> sheetRecords = sheet.getRecords();
    // one way (of many) to cause the discrepancy is with a badly behaved record:
    sheetRecords.add(new BadlyBehavedRecord());
    // There is also much logic inside Sheet that (if buggy) might also cause the discrepancy
    try {
        wb.getBytes();
        fail("Identified bug 45066 a");
    } catch (IllegalStateException e) {
        // Expected badly behaved sheet record to cause exception
        assertTrue(e.getMessage().startsWith("Actual serialized sheet size"));
    }
    wb.close();
}
Also used : InternalSheet(org.apache.poi.hssf.model.InternalSheet) RecordBase(org.apache.poi.hssf.record.RecordBase) Test(org.junit.Test)

Example 3 with InternalSheet

use of org.apache.poi.hssf.model.InternalSheet 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 InternalSheet

use of org.apache.poi.hssf.model.InternalSheet 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)

Example 5 with InternalSheet

use of org.apache.poi.hssf.model.InternalSheet in project poi by apache.

the class TestPageSettingsBlock method testDuplicateHeaderFooter_bug48026.

public void testDuplicateHeaderFooter_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), new UserSViewEnd(HexRead.readFromString("01 00")), // 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")), 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, outRecs.length);
    //expected order of records:
    Record[] expectedRecs = { //BOFRecord
    recs[0], //IndexRecord
    recs[1], //HeaderRecord
    recs[2], //FooterRecord
    recs[3], //HeaderFooterRecord
    recs[9], // DimensionsRecord
    recs[4], // WindowTwoRecord
    recs[5], // UserSViewBegin
    recs[6], // SelectionRecord
    recs[7], // HeaderFooterRecord
    recs[10], // UserSViewEnd
    recs[8], //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[4];
    //GUID is zero
    assertArrayEquals(new byte[16], hd1.getGuid());
    assertTrue(hd1.isCurrentSheet());
    UserSViewBegin svb = (UserSViewBegin) expectedRecs[7];
    HeaderFooterRecord hd2 = (HeaderFooterRecord) expectedRecs[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)

Aggregations

InternalSheet (org.apache.poi.hssf.model.InternalSheet)14 Test (org.junit.Test)6 AssertionFailedError (junit.framework.AssertionFailedError)5 RecordStream (org.apache.poi.hssf.model.RecordStream)5 RecordCollector (org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector)5 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)3 CommonObjectDataSubRecord (org.apache.poi.hssf.record.CommonObjectDataSubRecord)2 NameRecord (org.apache.poi.hssf.record.NameRecord)2 RecordBase (org.apache.poi.hssf.record.RecordBase)2 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)1 AutoFilterInfoRecord (org.apache.poi.hssf.record.AutoFilterInfoRecord)1 BOFRecord (org.apache.poi.hssf.record.BOFRecord)1 CellValueRecordInterface (org.apache.poi.hssf.record.CellValueRecordInterface)1 EOFRecord (org.apache.poi.hssf.record.EOFRecord)1 EmbeddedObjectRefSubRecord (org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord)1