use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestHSSFEventFactory method testWithDifferentWorkbookName.
public void testWithDifferentWorkbookName() throws Exception {
HSSFRequest req = new HSSFRequest();
MockHSSFListener mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
POIFSFileSystem fs = new POIFSFileSystem(openSample("BOOK_in_capitals.xls"));
HSSFEventFactory factory = new HSSFEventFactory();
factory.processWorkbookEvents(req, fs);
fs = new POIFSFileSystem(openSample("WORKBOOK_in_capitals.xls"));
factory = new HSSFEventFactory();
factory.processWorkbookEvents(req, fs);
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestHSSFEventFactory method testWithMissingRecords.
public void testWithMissingRecords() throws Exception {
HSSFRequest req = new HSSFRequest();
MockHSSFListener mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
POIFSFileSystem fs = new POIFSFileSystem(openSample("SimpleWithSkip.xls"));
HSSFEventFactory factory = new HSSFEventFactory();
factory.processWorkbookEvents(req, fs);
Record[] recs = mockListen.getRecords();
// Check we got the records
assertTrue(recs.length > 100);
// Check that the last few records are as we expect
// (Makes sure we don't accidently skip the end ones)
int numRec = recs.length;
assertEquals(WindowTwoRecord.class, recs[numRec - 3].getClass());
assertEquals(SelectionRecord.class, recs[numRec - 2].getClass());
assertEquals(EOFRecord.class, recs[numRec - 1].getClass());
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestHSSFEventFactory method testWithPasswordProtectedWorkbooks.
public void testWithPasswordProtectedWorkbooks() throws Exception {
HSSFRequest req = new HSSFRequest();
MockHSSFListener mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
// Without a password, can't be read
Biff8EncryptionKey.setCurrentUserPassword(null);
POIFSFileSystem fs = new POIFSFileSystem(openSample("xor-encryption-abc.xls"));
HSSFEventFactory factory = new HSSFEventFactory();
try {
factory.processWorkbookEvents(req, fs);
fail("Shouldn't be able to process protected workbook without the password");
} catch (EncryptedDocumentException e) {
}
// With the password, is properly processed
Biff8EncryptionKey.setCurrentUserPassword("abc");
req = new HSSFRequest();
mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
factory.processWorkbookEvents(req, fs);
// Check we got the sheet and the contents
Record[] recs = mockListen.getRecords();
assertTrue(recs.length > 50);
// Has one sheet, with values 1,2,3 in column A rows 1-3
boolean hasSheet = false, hasA1 = false, hasA2 = false, hasA3 = false;
for (Record r : recs) {
if (r instanceof BoundSheetRecord) {
BoundSheetRecord bsr = (BoundSheetRecord) r;
assertEquals("Sheet1", bsr.getSheetname());
hasSheet = true;
}
if (r instanceof NumberRecord) {
NumberRecord nr = (NumberRecord) r;
if (nr.getColumn() == 0 && nr.getRow() == 0) {
assertEquals(1, (int) nr.getValue());
hasA1 = true;
}
if (nr.getColumn() == 0 && nr.getRow() == 1) {
assertEquals(2, (int) nr.getValue());
hasA2 = true;
}
if (nr.getColumn() == 0 && nr.getRow() == 2) {
assertEquals(3, (int) nr.getValue());
hasA3 = true;
}
}
}
assertTrue("Sheet record not found", hasSheet);
assertTrue("Numeric record for A1 not found", hasA1);
assertTrue("Numeric record for A2 not found", hasA2);
assertTrue("Numeric record for A3 not found", hasA3);
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestRecordFactory method testNonZeroPadding_bug46987.
@Test
public void testNonZeroPadding_bug46987() throws IOException {
Record[] recs = { new BOFRecord(), // need *something* between BOF and EOF
new WriteAccessRecord(), EOFRecord.instance, BOFRecord.createSheetBOF(), EOFRecord.instance };
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (Record rec : recs) {
try {
baos.write(rec.serialize());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//simulate the bad padding at the end of the workbook stream in attachment 23483 of bug 46987
baos.write(0x00);
baos.write(0x11);
baos.write(0x00);
baos.write(0x02);
for (int i = 0; i < 192; i++) {
baos.write(0x00);
}
POIFSFileSystem fs = new POIFSFileSystem();
fs.createDocument(new ByteArrayInputStream(baos.toByteArray()), "dummy");
InputStream is = fs.getRoot().createDocumentInputStream("dummy");
List<Record> outRecs = RecordFactory.createRecords(is);
// Buffer underrun - requested 512 bytes but 192 was available
assertEquals(5, outRecs.size());
fs.close();
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestOLE2Embeding method getSamplePPT.
static POIFSFileSystem getSamplePPT() throws IOException {
// scratchpad classes are not available, so we use something pre-cooked
InputStream is = POIDataSamples.getSlideShowInstance().openResourceAsStream("with_textbox.ppt");
POIFSFileSystem poifs = new POIFSFileSystem(is);
is.close();
return poifs;
}
Aggregations