use of org.apache.poi.poifs.filesystem.OPOIFSFileSystem in project poi by apache.
the class TestExtractorFactory method testOPOIFS.
@Test
public void testOPOIFS() throws Exception {
// Excel
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(xls))) instanceof ExcelExtractor);
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(xls))).getText().length() > 200);
// Word
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc))) instanceof WordExtractor);
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc))).getText().length() > 120);
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc6))) instanceof Word6Extractor);
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc6))).getText().length() > 20);
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc95))) instanceof Word6Extractor);
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc95))).getText().length() > 120);
// PowerPoint
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(ppt))) instanceof PowerPointExtractor);
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(ppt))).getText().length() > 120);
// Visio
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(vsd))) instanceof VisioTextExtractor);
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(vsd))).getText().length() > 50);
// Publisher
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(pub))) instanceof PublisherTextExtractor);
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(pub))).getText().length() > 50);
// Outlook msg
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(msg))) instanceof OutlookTextExtactor);
assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(msg))).getText().length() > 50);
// Text
try {
ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(txt)));
fail();
} catch (IOException e) {
// Good
}
}
use of org.apache.poi.poifs.filesystem.OPOIFSFileSystem in project poi by apache.
the class TestWordExtractor method testDifferentPOIFS.
/**
* Tests that we can work with both {@link POIFSFileSystem}
* and {@link NPOIFSFileSystem}
*/
@Test
public void testDifferentPOIFS() throws Exception {
// Open the two filesystems
File file = docTests.getFile("test2.doc");
InputStream is = new FileInputStream(file);
OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is);
is.close();
NPOIFSFileSystem npoifs = new NPOIFSFileSystem(file);
DirectoryNode[] files = { opoifs.getRoot(), npoifs.getRoot() };
// Open directly
for (DirectoryNode dir : files) {
@SuppressWarnings("resource") WordExtractor extractor = new WordExtractor(dir);
assertEqualsTrim(p_text1_block, extractor.getText());
// extractor.close();
}
// Open via a HWPFDocument
for (DirectoryNode dir : files) {
HWPFDocument doc = new HWPFDocument(dir);
WordExtractor extractor = new WordExtractor(doc);
assertEqualsTrim(p_text1_block, extractor.getText());
extractor.close();
}
npoifs.close();
}
use of org.apache.poi.poifs.filesystem.OPOIFSFileSystem in project poi by apache.
the class TestHSSFWorkbook method invalidInPlaceWrite.
@Test
public void invalidInPlaceWrite() throws Exception {
HSSFWorkbook wb;
// Can't work for new files
wb = new HSSFWorkbook();
try {
wb.write();
fail("Shouldn't work for new files");
} catch (IllegalStateException e) {
// expected here
}
wb.close();
// Can't work for InputStream opened files
wb = new HSSFWorkbook(POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls"));
try {
wb.write();
fail("Shouldn't work for InputStream");
} catch (IllegalStateException e) {
// expected here
}
wb.close();
// Can't work for OPOIFS
OPOIFSFileSystem ofs = new OPOIFSFileSystem(POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls"));
wb = new HSSFWorkbook(ofs.getRoot(), true);
try {
wb.write();
fail("Shouldn't work for OPOIFSFileSystem");
} catch (IllegalStateException e) {
// expected here
}
wb.close();
// Can't work for Read-Only files
NPOIFSFileSystem fs = new NPOIFSFileSystem(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xls"), true);
wb = new HSSFWorkbook(fs);
try {
wb.write();
fail("Shouldn't work for Read Only");
} catch (IllegalStateException e) {
// expected here
}
wb.close();
}
use of org.apache.poi.poifs.filesystem.OPOIFSFileSystem in project poi by apache.
the class TestBugs method bug46904.
/**
* java.io.IOException: block[ 0 ] already removed
* (is an excel 95 file though)
*/
@Test
public void bug46904() throws Exception {
try {
OPOIFSFileSystem fs = new OPOIFSFileSystem(HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
new HSSFWorkbook(fs.getRoot(), false).close();
fail("Should catch exception here");
} catch (OldExcelFormatException e) {
assertTrue(e.getMessage().startsWith("The supplied spreadsheet seems to be Excel"));
}
try {
NPOIFSFileSystem fs = new NPOIFSFileSystem(HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
try {
new HSSFWorkbook(fs.getRoot(), false).close();
fail("Should catch exception here");
} finally {
fs.close();
}
} catch (OldExcelFormatException e) {
assertTrue(e.getMessage().startsWith("The supplied spreadsheet seems to be Excel"));
}
}
use of org.apache.poi.poifs.filesystem.OPOIFSFileSystem in project poi by apache.
the class TestHWPFWrite method testInvalidInPlaceWriteOPOIFS.
@Test(expected = IllegalStateException.class)
public void testInvalidInPlaceWriteOPOIFS() throws Exception {
// Can't work for OPOIFS
OPOIFSFileSystem ofs = new OPOIFSFileSystem(SAMPLES.openResourceAsStream("SampleDoc.doc"));
HWPFDocument doc = new HWPFDocument(ofs.getRoot());
try {
doc.write();
} finally {
doc.close();
}
}
Aggregations