Tidy up HSLF extractor tests, and add a bit more for bug #51803

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1173753 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-09-21 16:46:43 +00:00
parent 2c9a15dc3b
commit 59204cbc38

View File

@ -38,24 +38,33 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
* @author Nick Burch (nick at torchbox dot com) * @author Nick Burch (nick at torchbox dot com)
*/ */
public final class TestExtractor extends TestCase { public final class TestExtractor extends TestCase {
/** Extractor primed on the 2 page basic test data */ /** Extractor primed on the 2 page basic test data */
private PowerPointExtractor ppe; private PowerPointExtractor ppe;
private static final String expectText = "This is a test title\nThis is a test subtitle\nThis is on page 1\nThis is the title on page 2\nThis is page two\nIt has several blocks of text\nNone of them have formatting\n"; private static final String expectText = "This is a test title\nThis is a test subtitle\nThis is on page 1\nThis is the title on page 2\nThis is page two\nIt has several blocks of text\nNone of them have formatting\n";
/** Extractor primed on the 1 page but text-box'd test data */ /** Extractor primed on the 1 page but text-box'd test data */
private PowerPointExtractor ppe2; private PowerPointExtractor ppe2;
private static final String expectText2 = "Hello, World!!!\nI am just a poor boy\nThis is Times New Roman\nPlain Text \n"; private static final String expectText2 = "Hello, World!!!\nI am just a poor boy\nThis is Times New Roman\nPlain Text \n";
/** Where our embeded files live */
//private String pdirname;
private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
//private String pdirname;
/** Where our embeded files live */ protected void setUp() throws Exception {
//private String pdirname; ppe = new PowerPointExtractor(slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); ppe2 = new PowerPointExtractor(slTests.openResourceAsStream("with_textbox.ppt"));
//private String pdirname; }
protected void setUp() throws Exception { private static void assertContains(String haystack, String needle) {
ppe = new PowerPointExtractor(slTests.openResourceAsStream("basic_test_ppt_file.ppt")); assertContains(
ppe2 = new PowerPointExtractor(slTests.openResourceAsStream("with_textbox.ppt")); "Unable to find expected text '" + needle + "' in text:\n" + haystack,
} haystack, needle
);
}
private static void assertContains(String reason, String haystack, String needle) {
assertTrue(reason, haystack.contains(needle));
}
public void testReadSheetText() { public void testReadSheetText() {
// Basic 2 page example // Basic 2 page example
@ -202,78 +211,102 @@ public final class TestExtractor extends TestCase {
ppe = new PowerPointExtractor(slTests.openResourceAsStream("WithComments.ppt")); ppe = new PowerPointExtractor(slTests.openResourceAsStream("WithComments.ppt"));
String text = ppe.getText(); String text = ppe.getText();
assertFalse("Comments not in by default", contains(text, "This is a test comment")); assertFalse("Comments not in by default", text.contains("This is a test comment"));
ppe.setCommentsByDefault(true); ppe.setCommentsByDefault(true);
text = ppe.getText(); text = ppe.getText();
assertTrue("Unable to find expected word in text\n" + text, contains(text, "This is a test comment")); assertContains(text, "This is a test comment");
// And another file // And another file
ppe = new PowerPointExtractor(slTests.openResourceAsStream("45543.ppt")); ppe = new PowerPointExtractor(slTests.openResourceAsStream("45543.ppt"));
text = ppe.getText(); text = ppe.getText();
assertFalse("Comments not in by default", contains(text, "testdoc")); assertFalse("Comments not in by default", text.contains("testdoc"));
ppe.setCommentsByDefault(true); ppe.setCommentsByDefault(true);
text = ppe.getText(); text = ppe.getText();
assertTrue("Unable to find expected word in text\n" + text, contains(text, "testdoc")); assertContains(text, "testdoc");
} }
/** /**
* From bug #45537 * From bug #45537
*/ */
public void testHeaderFooter() throws Exception { public void testHeaderFooter() throws Exception {
String text; String text;
// With a header on the notes // With a header on the notes
HSLFSlideShow hslf = new HSLFSlideShow(slTests.openResourceAsStream("45537_Header.ppt")); HSLFSlideShow hslf = new HSLFSlideShow(slTests.openResourceAsStream("45537_Header.ppt"));
SlideShow ss = new SlideShow(hslf); SlideShow ss = new SlideShow(hslf);
assertNotNull(ss.getNotesHeadersFooters()); assertNotNull(ss.getNotesHeadersFooters());
assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getHeaderText()); assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getHeaderText());
ppe = new PowerPointExtractor(hslf); ppe = new PowerPointExtractor(hslf);
text = ppe.getText(); text = ppe.getText();
assertFalse("Unable to find expected word in text\n" + text, contains(text, "testdoc")); assertFalse("Header shouldn't be there by default\n" + text, text.contains("testdoc"));
assertFalse("Unable to find expected word in text\n" + text, contains(text, "test phrase")); assertFalse("Header shouldn't be there by default\n" + text, text.contains("test phrase"));
ppe.setNotesByDefault(true); ppe.setNotesByDefault(true);
text = ppe.getText(); text = ppe.getText();
assertTrue("Unable to find expected word in text\n" + text, contains(text, "testdoc")); assertContains(text, "testdoc");
assertTrue("Unable to find expected word in text\n" + text, contains(text, "test phrase")); assertContains(text, "test phrase");
// And with a footer, also on notes // And with a footer, also on notes
hslf = new HSLFSlideShow(slTests.openResourceAsStream("45537_Footer.ppt")); hslf = new HSLFSlideShow(slTests.openResourceAsStream("45537_Footer.ppt"));
ss = new SlideShow(hslf); ss = new SlideShow(hslf);
assertNotNull(ss.getNotesHeadersFooters()); assertNotNull(ss.getNotesHeadersFooters());
assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getFooterText()); assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getFooterText());
ppe = new PowerPointExtractor(slTests.openResourceAsStream("45537_Footer.ppt")); ppe = new PowerPointExtractor(slTests.openResourceAsStream("45537_Footer.ppt"));
text = ppe.getText(); text = ppe.getText();
assertFalse("Unable to find expected word in text\n" + text, contains(text, "testdoc")); assertFalse("Header shouldn't be there by default\n" + text, text.contains("testdoc"));
assertFalse("Unable to find expected word in text\n" + text, contains(text, "test phrase")); assertFalse("Header shouldn't be there by default\n" + text, text.contains("test phrase"));
ppe.setNotesByDefault(true); ppe.setNotesByDefault(true);
text = ppe.getText(); text = ppe.getText();
assertTrue("Unable to find expected word in text\n" + text, contains(text, "testdoc")); assertContains(text, "testdoc");
assertTrue("Unable to find expected word in text\n" + text, contains(text, "test phrase")); assertContains(text, "test phrase");
} }
private static boolean contains(String text, String searchString) { public void testSlideMasterText() throws Exception {
return text.indexOf(searchString) >=0; String masterText = "Master footer is here";
} HSLFSlideShow hslf = new HSLFSlideShow(slTests.openResourceAsStream("WithMaster.ppt"));
ppe = new PowerPointExtractor(hslf);
String text = ppe.getText();
assertContains(text, "Master");
assertContains(text, masterText);
}
public void testMasterText() throws Exception { public void testMasterText() throws Exception {
ppe = new PowerPointExtractor(slTests.openResourceAsStream("master_text.ppt")); ppe = new PowerPointExtractor(slTests.openResourceAsStream("master_text.ppt"));
ppe.setMasterByDefault(true);
String text = ppe.getText(); // Initially not there
assertTrue(text.contains("Master Header Text")); String text = ppe.getText();
assertFalse(text.contains("Master Header Text"));
// Enable, shows up
ppe.setMasterByDefault(true);
text = ppe.getText();
assertTrue(text.contains("Master Header Text"));
// Now with another file only containing master text
// Will always show up
String masterText = "Master footer is here";
HSLFSlideShow hslf = new HSLFSlideShow(slTests.openResourceAsStream("WithMaster.ppt"));
ppe = new PowerPointExtractor(hslf);
text = ppe.getText();
assertContains(text, "Master");
assertContains(text, masterText);
} }