fix eclipse warnings / close resources in tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1778958 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2017-01-16 01:18:46 +00:00
parent a7706b3fdf
commit 75c630f976
12 changed files with 447 additions and 351 deletions

View File

@ -349,6 +349,7 @@ public class TestEncryptor {
e.confirmPassword("AYBABTU");
docx.write(e.getDataStream(fs));
docx.close();
fs.close();
}

View File

@ -17,21 +17,24 @@
package org.apache.poi.xssf.extractor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import junit.framework.TestCase;
import org.apache.poi.POITextExtractor;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.junit.Test;
/**
* Tests for {@link XSSFEventBasedExcelExtractor}
*/
public class TestXSSFEventBasedExcelExtractor extends TestCase {
public class TestXSSFEventBasedExcelExtractor {
protected XSSFEventBasedExcelExtractor getExtractor(String sampleName) throws Exception {
return new XSSFEventBasedExcelExtractor(XSSFTestDataSamples.
openSamplePackage(sampleName));
@ -40,6 +43,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
/**
* Get text out of the simple file
*/
@Test
public void testGetSimpleText() throws Exception {
// a very simple file
XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsx");
@ -100,6 +104,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
extractor.close();
}
@Test
public void testGetComplexText() throws Exception {
// A fairly complex file
XSSFEventBasedExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx");
@ -117,6 +122,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
extractor.close();
}
@Test
public void testInlineStrings() throws Exception {
XSSFEventBasedExcelExtractor extractor = getExtractor("InlineStrings.xlsx");
extractor.setFormulasNotResults(true);
@ -146,6 +152,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
* ExcelExtractor does, when we're both passed
* the same file, just saved as xls and xlsx
*/
@Test
public void testComparedToOLE2() throws Exception {
// A fairly simple file - ooxml
XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("SampleSS.xlsx");
@ -171,6 +178,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
* Test text extraction from text box using getShapes()
* @throws Exception
*/
@Test
public void testShapes() throws Exception{
XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("WithTextBox.xlsx");
@ -189,6 +197,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
* Test that we return the same output for unstyled numbers as the
* non-event-based XSSFExcelExtractor.
*/
@Test
public void testUnstyledNumbersComparedToNonEventBasedExtractor()
throws Exception {
@ -216,6 +225,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
* Test that we return the same output headers and footers as the
* non-event-based XSSFExcelExtractor.
*/
@Test
public void testHeadersAndFootersComparedToNonEventBasedExtractor()
throws Exception {
@ -260,6 +270,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
* This test will need to be modified if these improvements are ported to
* XSSFExcelExtractor.
*/
@Test
public void testCommentsComparedToNonEventBasedExtractor()
throws Exception {
@ -315,6 +326,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
}
}
@Test
public void testFile56278_normal() throws Exception {
// first with normal Text Extractor
POIXMLTextExtractor extractor = new XSSFExcelExtractor(
@ -326,6 +338,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
}
}
@Test
public void testFile56278_event() throws Exception {
// then with event based one
POIXMLTextExtractor extractor = getExtractor("56278.xlsx");
@ -336,6 +349,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
}
}
@Test
public void test59021() throws Exception {
XSSFEventBasedExcelExtractor ex =
new XSSFEventBasedExcelExtractor(
@ -343,5 +357,6 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
String text = ex.getText();
assertTrue("can't find Abhkazia", text.contains("Abkhazia - Fixed"));
assertTrue("can't find 10/02/2016", text.contains("10/02/2016"));
ex.close();
}
}

View File

@ -18,6 +18,7 @@ package org.apache.poi.xssf.extractor;
import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.junit.After;
public class TestXSSFEventBasedExcelExtractorUsingFactory extends TestXSSFEventBasedExcelExtractor {
@ -27,7 +28,7 @@ public class TestXSSFEventBasedExcelExtractorUsingFactory extends TestXSSFEventB
return (XSSFEventBasedExcelExtractor) ExtractorFactory.createExtractor(HSSFTestDataSamples.openSampleFileStream(sampleName));
}
@Override
@After
public void tearDown() {
// reset setting to not affect other tests
ExtractorFactory.setAllThreadsPreferEventExtractors(null);

View File

@ -24,7 +24,6 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.poi.util.Units;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFRun.FontCharRange;
import org.junit.Test;
@ -82,10 +81,11 @@ public class TestXWPFBugs {
}
}
}
doc.close();
}
@Test
public void bug57495_getTableArrayInDoc() {
public void bug57495_getTableArrayInDoc() throws IOException {
XWPFDocument doc =new XWPFDocument();
//let's create a few tables for the test
for(int i=0;i<3;i++) {
@ -96,10 +96,11 @@ public class TestXWPFBugs {
//let's check also that returns the correct table
XWPFTable same = doc.getTables().get(0);
assertEquals(table, same);
doc.close();
}
@Test
public void bug57495_getParagraphArrayInTableCell() {
public void bug57495_getParagraphArrayInTableCell() throws IOException {
XWPFDocument doc =new XWPFDocument();
//let's create a table for the test
XWPFTable table = doc.createTable(2, 2);
@ -109,6 +110,7 @@ public class TestXWPFBugs {
//let's check also that returns the correct paragraph
XWPFParagraph same = table.getRow(0).getCell(0).getParagraphs().get(0);
assertEquals(p, same);
doc.close();
}
@Test
@ -120,16 +122,17 @@ public class TestXWPFBugs {
}
@Test
public void test56392() throws IOException, OpenXML4JException {
public void test56392() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("56392.docx");
assertNotNull(doc);
doc.close();
}
/**
* Removing a run needs to remove it from both Runs and IRuns
*/
@Test
public void test57829() throws Exception {
public void test57829() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
assertNotNull(doc);
assertEquals(3, doc.getParagraphs().size());
@ -138,13 +141,14 @@ public class TestXWPFBugs {
paragraph.removeRun(0);
assertNotNull(paragraph.getText());
}
doc.close();
}
/**
* Removing a run needs to take into account position of run if paragraph contains hyperlink runs
*/
@Test
public void test58618() throws Exception {
public void test58618() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("58618.docx");
XWPFParagraph para = (XWPFParagraph)doc.getBodyElements().get(0);
assertNotNull(para);
@ -154,10 +158,11 @@ public class TestXWPFBugs {
assertEquals("Some text some hyper links link link and some text.....New Text", para.getText());
para.removeRun(para.getRuns().size() -2);
assertEquals("Some text some hyper links link linkNew Text", para.getText());
doc.close();
}
@Test
public void test59378() throws Exception {
public void test59378() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("59378.docx");
ByteArrayOutputStream out = new ByteArrayOutputStream();
doc.write(out);

View File

@ -151,7 +151,7 @@ public final class TestXWPFDocument {
public void testAddPicture() throws IOException, InvalidFormatException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
byte[] jpeg = XWPFTestDataSamples.getImage("nature1.jpg");
String relationId = doc.addPictureData(jpeg, XWPFDocument.PICTURE_TYPE_JPEG);
String relationId = doc.addPictureData(jpeg, Document.PICTURE_TYPE_JPEG);
byte[] newJpeg = ((XWPFPictureData) doc.getRelationById(relationId)).getData();
assertEquals(newJpeg.length, jpeg.length);
@ -165,17 +165,17 @@ public final class TestXWPFDocument {
public void testAllPictureFormats() throws IOException, InvalidFormatException {
XWPFDocument doc = new XWPFDocument();
doc.addPictureData(new byte[10], XWPFDocument.PICTURE_TYPE_EMF);
doc.addPictureData(new byte[11], XWPFDocument.PICTURE_TYPE_WMF);
doc.addPictureData(new byte[12], XWPFDocument.PICTURE_TYPE_PICT);
doc.addPictureData(new byte[13], XWPFDocument.PICTURE_TYPE_JPEG);
doc.addPictureData(new byte[14], XWPFDocument.PICTURE_TYPE_PNG);
doc.addPictureData(new byte[15], XWPFDocument.PICTURE_TYPE_DIB);
doc.addPictureData(new byte[16], XWPFDocument.PICTURE_TYPE_GIF);
doc.addPictureData(new byte[17], XWPFDocument.PICTURE_TYPE_TIFF);
doc.addPictureData(new byte[18], XWPFDocument.PICTURE_TYPE_EPS);
doc.addPictureData(new byte[19], XWPFDocument.PICTURE_TYPE_BMP);
doc.addPictureData(new byte[20], XWPFDocument.PICTURE_TYPE_WPG);
doc.addPictureData(new byte[10], Document.PICTURE_TYPE_EMF);
doc.addPictureData(new byte[11], Document.PICTURE_TYPE_WMF);
doc.addPictureData(new byte[12], Document.PICTURE_TYPE_PICT);
doc.addPictureData(new byte[13], Document.PICTURE_TYPE_JPEG);
doc.addPictureData(new byte[14], Document.PICTURE_TYPE_PNG);
doc.addPictureData(new byte[15], Document.PICTURE_TYPE_DIB);
doc.addPictureData(new byte[16], Document.PICTURE_TYPE_GIF);
doc.addPictureData(new byte[17], Document.PICTURE_TYPE_TIFF);
doc.addPictureData(new byte[18], Document.PICTURE_TYPE_EPS);
doc.addPictureData(new byte[19], Document.PICTURE_TYPE_BMP);
doc.addPictureData(new byte[20], Document.PICTURE_TYPE_WPG);
assertEquals(11, doc.getAllPictures().size());
@ -442,8 +442,10 @@ public final class TestXWPFDocument {
String origText = ext.getText();
doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
ext.close();
ext = new XWPFWordExtractor(doc);
assertEquals(origText, ext.getText());
ext.close();
}
}

View File

@ -49,16 +49,17 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun
/**
* Tests for XWPF Run
*/
@SuppressWarnings("deprecation")
public class TestXWPFRun {
private CTR ctRun;
private XWPFParagraph p;
private IRunBody irb;
private XWPFDocument doc;
@Before
public void setUp() {
doc = new XWPFDocument();
p = doc.createParagraph();
irb = p;
this.ctRun = CTR.Factory.newInstance();
}
@ -75,7 +76,7 @@ public class TestXWPFRun {
ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(3, ctRun.sizeOfTArray());
XWPFRun run = new XWPFRun(ctRun, p);
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals("TEST2 STRING", run.getText(1));
@ -95,7 +96,7 @@ public class TestXWPFRun {
public void testCTOnOff() {
CTRPr rpr = ctRun.addNewRPr();
CTOnOff bold = rpr.addNewB();
XWPFRun run = new XWPFRun(ctRun, p);
XWPFRun run = new XWPFRun(ctRun, irb);
// True values: "true", "1", "on"
bold.setVal(STOnOff.TRUE);
@ -123,7 +124,7 @@ public class TestXWPFRun {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewB().setVal(STOnOff.TRUE);
XWPFRun run = new XWPFRun(ctRun, p);
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals(true, run.isBold());
run.setBold(false);
@ -137,7 +138,7 @@ public class TestXWPFRun {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewI().setVal(STOnOff.TRUE);
XWPFRun run = new XWPFRun(ctRun, p);
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals(true, run.isItalic());
run.setItalic(false);
@ -149,10 +150,10 @@ public class TestXWPFRun {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewStrike().setVal(STOnOff.TRUE);
XWPFRun run = new XWPFRun(ctRun, p);
assertEquals(true, run.isStrike());
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals(true, run.isStrikeThrough());
run.setStrike(false);
run.setStrikeThrough(false);
assertEquals(STOnOff.FALSE, rpr.getStrike().getVal());
}
@ -161,7 +162,7 @@ public class TestXWPFRun {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewU().setVal(STUnderline.DASH);
XWPFRun run = new XWPFRun(ctRun, p);
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals(UnderlinePatterns.DASH.getValue(), run.getUnderline()
.getValue());
@ -175,7 +176,7 @@ public class TestXWPFRun {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewVertAlign().setVal(STVerticalAlignRun.SUBSCRIPT);
XWPFRun run = new XWPFRun(ctRun, p);
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals(VerticalAlign.SUBSCRIPT, run.getSubscript());
run.setSubscript(VerticalAlign.BASELINE);
@ -187,7 +188,7 @@ public class TestXWPFRun {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewRFonts().setAscii("Times New Roman");
XWPFRun run = new XWPFRun(ctRun, p);
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals("Times New Roman", run.getFontFamily());
run.setFontFamily("Verdana");
@ -199,7 +200,7 @@ public class TestXWPFRun {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewSz().setVal(new BigInteger("14"));
XWPFRun run = new XWPFRun(ctRun, p);
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals(7, run.getFontSize());
run.setFontSize(24);
@ -211,7 +212,7 @@ public class TestXWPFRun {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewPosition().setVal(new BigInteger("4000"));
XWPFRun run = new XWPFRun(ctRun, p);
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals(4000, run.getTextPosition());
run.setTextPosition(2400);
@ -220,7 +221,7 @@ public class TestXWPFRun {
@Test
public void testSetGetColor() {
XWPFRun run = new XWPFRun(ctRun, p);
XWPFRun run = new XWPFRun(ctRun, irb);
run.setColor("0F0F0F");
String clr = run.getColor();
assertEquals("0F0F0F", clr);
@ -235,7 +236,7 @@ public class TestXWPFRun {
ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(2, ctRun.sizeOfCrArray());
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), p);
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
run.setText("T1");
run.addCarriageReturn();
run.addCarriageReturn();
@ -256,7 +257,7 @@ public class TestXWPFRun {
assertEquals(1, ctRun.sizeOfCrArray());
assertEquals(1, ctRun.sizeOfTabArray());
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), p);
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
run.setText("T1");
run.addCarriageReturn();
run.setText("T2");
@ -278,7 +279,7 @@ public class TestXWPFRun {
ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(2, ctRun.sizeOfBrArray());
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), p);
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
run.setText("TEXT1");
run.addBreak();
run.setText("TEXT2");
@ -308,14 +309,14 @@ public class TestXWPFRun {
assertEquals("This is a test document", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(1);
assertEquals(".", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
assertEquals(null, run.getCTR().getRPr());
@ -328,7 +329,7 @@ public class TestXWPFRun {
assertEquals("This bit is in bold and italic", run.toString());
assertEquals(true, run.isBold());
assertEquals(true, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
assertEquals(true, run.getCTR().getRPr().isSetB());
assertEquals(false, run.getCTR().getRPr().getB().isSetVal());
@ -342,7 +343,7 @@ public class TestXWPFRun {
assertEquals("Back to normal", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
assertEquals(null, run.getCTR().getRPr());
@ -355,73 +356,75 @@ public class TestXWPFRun {
assertEquals("This contains ", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(1);
assertEquals("BOLD", run.toString());
assertEquals(true, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
run = p.getRuns().get(2);
assertEquals(", ", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(3);
assertEquals("ITALIC", run.toString());
assertEquals(false, run.isBold());
assertEquals(true, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
run = p.getRuns().get(4);
assertEquals(" and ", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(5);
assertEquals("BOTH", run.toString());
assertEquals(true, run.isBold());
assertEquals(true, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
run = p.getRuns().get(6);
assertEquals(", as well as ", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(7);
assertEquals("RED", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
run = p.getRuns().get(8);
assertEquals(" and ", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
assertEquals(null, run.getCTR().getRPr());
run = p.getRuns().get(9);
assertEquals("YELLOW", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
run = p.getRuns().get(10);
assertEquals(" text.", run.toString());
assertEquals(false, run.isBold());
assertEquals(false, run.isItalic());
assertEquals(false, run.isStrike());
assertEquals(false, run.isStrikeThrough());
assertEquals(null, run.getCTR().getRPr());
doc.close();
}
@Test
@ -447,10 +450,11 @@ public class TestXWPFRun {
}
assertEquals(1, count);
sampleDoc.close();
}
@Test
public void testSetGetHighlight() throws Exception {
public void testSetGetHighlight() throws IOException {
XWPFRun run = p.createRun();
assertEquals(false, run.isHighlighted());
@ -483,15 +487,15 @@ public class TestXWPFRun {
assertEquals(1, docBack.getAllPictures().size());
assertEquals(1, rBack.getEmbeddedPictures().size());
docBack.close();
doc.close();
}
/**
* Bugzilla #58237 - Unable to add image to word document header
*
* @throws Exception
*/
@Test
public void testAddPictureInHeader() throws Exception {
public void testAddPictureInHeader() throws IOException, InvalidFormatException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
XWPFParagraph p = hdr.createParagraph();
@ -523,6 +527,8 @@ public class TestXWPFRun {
assertEquals(1, hdrBack.getAllPictures().size());
assertEquals(1, rBack.getEmbeddedPictures().size());
docBack.close();
doc.close();
}
/**
@ -530,7 +536,7 @@ public class TestXWPFRun {
* run mustn't NPE
*/
@Test
public void testSetFontFamily_52288() throws Exception {
public void testSetFontFamily_52288() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52288.docx");
final Iterator<XWPFParagraph> paragraphs = doc.getParagraphsIterator();
while (paragraphs.hasNext()) {
@ -544,6 +550,7 @@ public class TestXWPFRun {
}
}
}
doc.close();
}
@Test
@ -570,7 +577,7 @@ public class TestXWPFRun {
}
@Test
public void testBug58922() {
public void testBug58922() throws IOException {
XWPFDocument document = new XWPFDocument();
final XWPFRun run = document.createParagraph().createRun();
@ -625,5 +632,7 @@ public class TestXWPFRun {
run.setTextPosition(-1);
assertEquals(-1, run.getTextPosition());
document.close();
}
}

View File

@ -17,61 +17,65 @@
package org.apache.poi.hdgf.extractor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hdgf.HDGFDiagram;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.POIDataSamples;
import org.junit.Test;
public final class TestVisioExtractor extends TestCase {
public final class TestVisioExtractor {
private static POIDataSamples _dgTests = POIDataSamples.getDiagramInstance();
private String defFilename;
private int defTextChunks;
@Override
protected void setUp() {
defFilename = "Test_Visio-Some_Random_Text.vsd";
defTextChunks = 5;
}
private final String defFilename = "Test_Visio-Some_Random_Text.vsd";
private final int defTextChunks = 5;
/**
* Test the 3 different ways of creating one
*/
public void testCreation() throws Exception {
VisioTextExtractor extractor;
@Test
public void testCreation() throws IOException {
VisioTextExtractor extractor1 = openExtractor(defFilename);
assertNotNull(extractor1);
assertNotNull(extractor1.getAllText());
assertEquals(defTextChunks, extractor1.getAllText().length);
extractor1.close();
extractor = new VisioTextExtractor(_dgTests.openResourceAsStream(defFilename));
assertNotNull(extractor);
assertNotNull(extractor.getAllText());
assertEquals(defTextChunks, extractor.getAllText().length);
InputStream is2 = _dgTests.openResourceAsStream(defFilename);
POIFSFileSystem poifs2 = new POIFSFileSystem(is2);
is2.close();
VisioTextExtractor extractor2 = new VisioTextExtractor(poifs2);
assertNotNull(extractor2);
assertNotNull(extractor2.getAllText());
assertEquals(defTextChunks, extractor2.getAllText().length);
extractor2.close();
poifs2.close();
extractor = new VisioTextExtractor(
new POIFSFileSystem(
_dgTests.openResourceAsStream(defFilename)
)
);
assertNotNull(extractor);
assertNotNull(extractor.getAllText());
assertEquals(defTextChunks, extractor.getAllText().length);
InputStream is3 = _dgTests.openResourceAsStream(defFilename);
POIFSFileSystem poifs3 = new POIFSFileSystem(is3);
is3.close();
HDGFDiagram hdgf3 = new HDGFDiagram(poifs3);
extractor = new VisioTextExtractor(
new HDGFDiagram(
new POIFSFileSystem(
_dgTests.openResourceAsStream(defFilename)
)
)
);
assertNotNull(extractor);
assertNotNull(extractor.getAllText());
assertEquals(defTextChunks, extractor.getAllText().length);
VisioTextExtractor extractor3 = new VisioTextExtractor(hdgf3);
assertNotNull(extractor3);
assertNotNull(extractor3.getAllText());
assertEquals(defTextChunks, extractor3.getAllText().length);
extractor3.close();
hdgf3.close();
poifs3.close();
}
@Test
public void testExtraction() throws Exception {
VisioTextExtractor extractor =
new VisioTextExtractor(_dgTests.openResourceAsStream(defFilename));
VisioTextExtractor extractor = openExtractor(defFilename);
// Check the array fetch
String[] text = extractor.getAllText();
@ -87,8 +91,10 @@ public final class TestVisioExtractor extends TestCase {
// And the all-in fetch
String textS = extractor.getText();
assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page\n", textS);
extractor.close();
}
@Test
public void testProblemFiles() throws Exception {
String[] files = {
"44594.vsd", "44594-2.vsd",
@ -96,11 +102,13 @@ public final class TestVisioExtractor extends TestCase {
"NegativeChunkLength.vsd", "NegativeChunkLength2.vsd"
};
for(String file : files){
VisioTextExtractor ex = new VisioTextExtractor(_dgTests.openResourceAsStream(file));
VisioTextExtractor ex = openExtractor(file);
ex.getText();
ex.close();
}
}
@Test
public void testMain() throws Exception {
PrintStream oldOut = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@ -124,4 +132,13 @@ public final class TestVisioExtractor extends TestCase {
"Some random text, on a page\n"
));
}
private VisioTextExtractor openExtractor(String fileName) throws IOException {
InputStream is = _dgTests.openResourceAsStream(fileName);
try {
return new VisioTextExtractor(is);
} finally {
is.close();
}
}
}

View File

@ -17,24 +17,32 @@
package org.apache.poi.hpbf;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public final class TestHPBFDocument extends TestCase {
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples;
import org.junit.Test;
public final class TestHPBFDocument {
private static final POIDataSamples _samples = POIDataSamples.getPublisherInstance();
public void testOpen() throws Exception {
HPBFDocument doc = new HPBFDocument(
_samples.openResourceAsStream("Sample.pub")
);
@Test
public void testOpen() throws IOException {
InputStream is = _samples.openResourceAsStream("Sample.pub");
HPBFDocument doc = new HPBFDocument(is);
is.close();
assertNotNull(doc);
doc.close();
}
public void testBits() throws Exception {
HPBFDocument doc = new HPBFDocument(
_samples.openResourceAsStream("Sample.pub")
);
@Test
public void testBits() throws IOException {
InputStream is = _samples.openResourceAsStream("Sample.pub");
HPBFDocument doc = new HPBFDocument(is);
is.close();
assertNotNull(doc.getMainContents());
assertNotNull(doc.getQuillContents());
@ -45,6 +53,8 @@ public final class TestHPBFDocument extends TestCase {
assertTrue(doc.getQuillContents().getData().length > 0);
assertTrue(doc.getEscherStm().getData().length > 0);
assertTrue(doc.getEscherDelayStm().getData().length == 0);
doc.close();
}
// TODO

View File

@ -106,6 +106,7 @@ public final class TestOleEmbedding {
HWPFDocument doc = new HWPFDocument(data.getData());
String txt = doc.getRange().getParagraph(0).text();
assertEquals("OLE embedding is thoroughly unremarkable.\r", txt);
doc.close();
}
}

View File

@ -18,7 +18,14 @@
package org.apache.poi.hwpf.extractor;
import static org.apache.poi.POITestCase.assertContains;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.POITextExtractor;
@ -28,26 +35,25 @@ import org.apache.poi.hwpf.OldWordFileFormatException;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.StringUtil;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
/**
* Test the different routes to extracting text
*
* @author Nick Burch (nick at torchbox dot com)
*/
public final class TestWordExtractor extends TestCase {
public final class TestWordExtractor {
public static void assertEquals( String expected, String actual )
private static POIDataSamples docTests = POIDataSamples.getDocumentInstance();
public static void assertEqualsTrim( String expected, String actual )
{
String newExpected = expected.replaceAll( "\r\n", "\n" )
.replaceAll( "\r", "\n" ).trim();
String newActual = actual.replaceAll( "\r\n", "\n" )
.replaceAll( "\r", "\n" ).trim();
TestCase.assertEquals( newExpected, newActual );
assertEquals( newExpected, newActual );
}
private static void assertExtractedContains(String[] extracted, String needle) {
@ -71,68 +77,40 @@ public final class TestWordExtractor extends TestCase {
"\r\n",
"It is otherwise very very boring.\r\n"
};
private String p_text1_block = "";
// Well behaved document
private WordExtractor extractor;
// Slightly iffy document
private WordExtractor extractor2;
// A word doc embeded in an excel file
private String filename3;
// With header and footer
private String filename4;
// With unicode header and footer
private String filename5;
// With footnote
private String filename6;
@Override
protected void setUp() throws Exception {
String filename = "test2.doc";
String filename2 = "test.doc";
filename3 = "excel_with_embeded.xls";
filename4 = "ThreeColHeadFoot.doc";
filename5 = "HeaderFooterUnicode.doc";
filename6 = "footnote.doc";
POIDataSamples docTests = POIDataSamples.getDocumentInstance();
extractor = new WordExtractor(docTests.openResourceAsStream(filename));
extractor2 = new WordExtractor(docTests.openResourceAsStream(filename2));
// Build splat'd out text version
p_text1_block = StringUtil.join(p_text1, "");
}
@Override
protected void tearDown() throws Exception {
if (extractor != null) extractor.close();
if (extractor2 != null) extractor2.close();
}
private final String p_text1_block = StringUtil.join(p_text1, "");
/**
* Test paragraph based extraction
*/
public void testExtractFromParagraphs() {
@Test
public void testExtractFromParagraphs() throws IOException {
WordExtractor extractor = openExtractor("test2.doc");
String[] text = extractor.getParagraphText();
assertEquals(p_text1.length, text.length);
for (int i = 0; i < p_text1.length; i++) {
assertEquals(p_text1[i], text[i]);
}
extractor.close();
// Lots of paragraphs with only a few lines in them
WordExtractor extractor2 = openExtractor("test.doc");
assertEquals(24, extractor2.getParagraphText().length);
assertEquals("as d\r\n", extractor2.getParagraphText()[16]);
assertEquals("as d\r\n", extractor2.getParagraphText()[17]);
assertEquals("as d\r\n", extractor2.getParagraphText()[18]);
extractor2.close();
}
/**
* Test the paragraph -> flat extraction
*/
public void testGetText() {
assertEquals(p_text1_block, extractor.getText());
@Test
public void testGetText() throws IOException {
WordExtractor extractor = openExtractor("test2.doc");
assertEqualsTrim(p_text1_block, extractor.getText());
// For the 2nd, should give similar answers for
// the two methods, differing only in line endings
@ -141,26 +119,29 @@ public final class TestWordExtractor extends TestCase {
// assertEquals(
// extractor2.getTextFromPieces().replaceAll("[\\r\\n]", ""),
// extractor2.getText().replaceAll("[\\r\\n]", ""));
extractor.close();
}
/**
* Test textPieces based extraction
*/
public void testExtractFromTextPieces() {
@Test
public void testExtractFromTextPieces() throws IOException {
WordExtractor extractor = openExtractor("test2.doc");
String text = extractor.getTextFromPieces();
assertEquals(p_text1_block, text);
extractor.close();
}
/**
* Test that we can get data from two different
* embeded word documents
* @throws Exception
* Test that we can get data from two different embedded word documents
*/
public void testExtractFromEmbeded() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getSpreadSheetInstance().openResourceAsStream(filename3));
HWPFDocument doc;
WordExtractor extractor3;
@Test
public void testExtractFromEmbeded() throws IOException {
InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("excel_with_embeded.xls");
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
DirectoryNode dirA = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B7");
DirectoryNode dirB = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B2");
@ -173,101 +154,123 @@ public final class TestWordExtractor extends TestCase {
assertNotNull(dirB.getEntry("WordDocument"));
// Check each in turn
doc = new HWPFDocument(dirA);
extractor3 = new WordExtractor(doc);
HWPFDocument docA = new HWPFDocument(dirA);
WordExtractor extractorA = new WordExtractor(docA);
assertNotNull(extractor3.getText());
assertTrue(extractor3.getText().length() > 20);
assertEquals("I am a sample document\r\nNot much on me\r\nI am document 1\r\n", extractor3
.getText());
assertEquals("Sample Doc 1", extractor3.getSummaryInformation().getTitle());
assertEquals("Sample Test", extractor3.getSummaryInformation().getSubject());
assertNotNull(extractorA.getText());
assertTrue(extractorA.getText().length() > 20);
assertEqualsTrim("I am a sample document\r\nNot much on me\r\nI am document 1\r\n", extractorA.getText());
assertEquals("Sample Doc 1", extractorA.getSummaryInformation().getTitle());
assertEquals("Sample Test", extractorA.getSummaryInformation().getSubject());
doc = new HWPFDocument(dirB);
extractor3 = new WordExtractor(doc);
HWPFDocument docB = new HWPFDocument(dirB);
WordExtractor extractorB = new WordExtractor(docB);
assertNotNull(extractor3.getText());
assertTrue(extractor3.getText().length() > 20);
assertEquals("I am another sample document\r\nNot much on me\r\nI am document 2\r\n",
extractor3.getText());
assertEquals("Sample Doc 2", extractor3.getSummaryInformation().getTitle());
assertEquals("Another Sample Test", extractor3.getSummaryInformation().getSubject());
assertNotNull(extractorB.getText());
assertTrue(extractorB.getText().length() > 20);
assertEqualsTrim("I am another sample document\r\nNot much on me\r\nI am document 2\r\n", extractorB.getText());
assertEquals("Sample Doc 2", extractorB.getSummaryInformation().getTitle());
assertEquals("Another Sample Test", extractorB.getSummaryInformation().getSubject());
extractorA.close();
docA.close();
extractorB.close();
docB.close();
fs.close();
}
public void testWithHeader() {
@Test
public void testWithHeader() throws IOException {
// Non-unicode
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename4);
extractor = new WordExtractor(doc);
HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc");
WordExtractor extractor1 = new WordExtractor(doc1);
assertEquals("First header column!\tMid header Right header!\n", extractor.getHeaderText());
assertContains(extractor.getText(), "First header column!");
assertEquals("First header column!\tMid header Right header!\n", extractor1.getHeaderText());
assertContains(extractor1.getText(), "First header column!");
extractor1.close();
doc1.close();
// Unicode
doc = HWPFTestDataSamples.openSampleFile(filename5);
extractor = new WordExtractor(doc);
HWPFDocument doc2 = HWPFTestDataSamples.openSampleFile("HeaderFooterUnicode.doc");
WordExtractor extractor2 = new WordExtractor(doc2);
assertEquals("This is a simple header, with a \u20ac euro symbol in it.\n\n", extractor.getHeaderText());
assertContains(extractor.getText(), "This is a simple header");
assertEquals("This is a simple header, with a \u20ac euro symbol in it.\n\n", extractor2.getHeaderText());
assertContains(extractor2.getText(), "This is a simple header");
extractor2.close();
doc2.close();
}
public void testWithFooter() {
@Test
public void testWithFooter() throws IOException {
// Non-unicode
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename4);
extractor = new WordExtractor(doc);
HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc");
WordExtractor extractor1 = new WordExtractor(doc1);
assertEquals("Footer Left\tFooter Middle Footer Right\n", extractor.getFooterText());
assertContains(extractor.getText(), "Footer Left");
assertEquals("Footer Left\tFooter Middle Footer Right\n", extractor1.getFooterText());
assertContains(extractor1.getText(), "Footer Left");
extractor1.close();
doc1.close();
// Unicode
doc = HWPFTestDataSamples.openSampleFile(filename5);
extractor = new WordExtractor(doc);
HWPFDocument doc2 = HWPFTestDataSamples.openSampleFile("HeaderFooterUnicode.doc");
WordExtractor extractor2 = new WordExtractor(doc2);
assertEquals("The footer, with Moli\u00e8re, has Unicode in it.\n", extractor.getFooterText());
assertContains(extractor.getText(), "The footer, with");
assertEquals("The footer, with Moli\u00e8re, has Unicode in it.\n", extractor2.getFooterText());
assertContains(extractor2.getText(), "The footer, with");
extractor2.close();
doc2.close();
}
public void testFootnote() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename6);
extractor = new WordExtractor(doc);
@Test
public void testFootnote() throws IOException {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("footnote.doc");
WordExtractor extractor = new WordExtractor(doc);
assertExtractedContains(extractor.getFootnoteText(), "TestFootnote");
assertEquals(0x00, doc.getRange().getSection(0).getFootnoteNumberingFormat()); // msonfcArabic
assertEquals(0x00, doc.getRange().getSection(0).getFootnoteRestartQualifier()); // rncCont
assertEquals(0, doc.getRange().getSection(0).getFootnoteNumberingOffset());
assertEquals(1, doc.getFootnotes().getNotesCount());
extractor.close();
doc.close();
}
public void testEndnote() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename6);
extractor = new WordExtractor(doc);
@Test
public void testEndnote() throws IOException {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("footnote.doc");
WordExtractor extractor = new WordExtractor(doc);
assertExtractedContains(extractor.getEndnoteText(), "TestEndnote");
assertEquals(0x02, doc.getRange().getSection(0).getEndnoteNumberingFormat()); // msonfcLCRoman
assertEquals(0x00, doc.getRange().getSection(0).getEndnoteRestartQualifier()); // rncCont
assertEquals(0, doc.getRange().getSection(0).getEndnoteNumberingOffset());
assertEquals(1, doc.getEndnotes().getNotesCount());
extractor.close();
doc.close();
}
public void testComments() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename6);
extractor = new WordExtractor(doc);
@Test
public void testComments() throws IOException {
WordExtractor extractor = openExtractor("footnote.doc");
assertExtractedContains(extractor.getCommentsText(), "TestComment");
extractor.close();
}
public void testWord95() throws Exception {
@Test(expected=OldWordFileFormatException.class)
public void testWord95_WordExtractor() throws Exception {
// Too old for the default
try {
extractor = new WordExtractor(
POIDataSamples.getDocumentInstance().openResourceAsStream("Word95.doc")
);
fail();
} catch(OldWordFileFormatException e) {}
openExtractor("Word95.doc").close();
}
@Test
public void testWord95() throws Exception {
// Can work with the special one
Word6Extractor w6e = new Word6Extractor(
POIDataSamples.getDocumentInstance().openResourceAsStream("Word95.doc")
);
InputStream is = docTests.openResourceAsStream("Word95.doc");
Word6Extractor w6e = new Word6Extractor(is);
is.close();
String text = w6e.getText();
assertTrue(text.contains("The quick brown fox jumps over the lazy dog"));
@ -284,20 +287,20 @@ public final class TestWordExtractor extends TestCase {
assertEquals("Paragraph 3. Has some RED text and some BLUE BOLD text in it.\r\n", tp[4]);
assertEquals("\r\n", tp[5]);
assertEquals("Last (4th) paragraph.\r\n", tp[6]);
w6e.close();
}
public void testWord6() throws Exception {
@Test(expected=OldWordFileFormatException.class)
public void testWord6_WordExtractor() throws IOException {
// Too old for the default
try {
extractor = new WordExtractor(
POIDataSamples.getDocumentInstance().openResourceAsStream("Word6.doc")
);
fail();
} catch(OldWordFileFormatException e) {}
openExtractor("Word6.doc").close();
}
Word6Extractor w6e = new Word6Extractor(
POIDataSamples.getDocumentInstance().openResourceAsStream("Word6.doc")
);
@Test
public void testWord6() throws Exception {
InputStream is = docTests.openResourceAsStream("Word6.doc");
Word6Extractor w6e = new Word6Extractor(is);
is.close();
String text = w6e.getText();
assertTrue(text.contains("The quick brown fox jumps over the lazy dog"));
@ -305,12 +308,12 @@ public final class TestWordExtractor extends TestCase {
String[] tp = w6e.getParagraphText();
assertEquals(1, tp.length);
assertEquals("The quick brown fox jumps over the lazy dog\r\n", tp[0]);
w6e.close();
}
@Test
public void testFastSaved() throws Exception {
extractor = new WordExtractor(
POIDataSamples.getDocumentInstance().openResourceAsStream("rasp.doc")
);
WordExtractor extractor = openExtractor("rasp.doc");
String text = extractor.getText();
assertTrue(text.contains("\u0425\u0425\u0425\u0425\u0425"));
@ -319,57 +322,57 @@ public final class TestWordExtractor extends TestCase {
extractor.close();
}
@Test
public void testFirstParagraphFix() throws Exception {
extractor = new WordExtractor(
POIDataSamples.getDocumentInstance().openResourceAsStream("Bug48075.doc")
);
WordExtractor extractor = openExtractor("Bug48075.doc");
String text = extractor.getText();
assertTrue(text.startsWith("\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435"));
extractor.close();
}
/**
* Tests that we can work with both {@link POIFSFileSystem}
* and {@link NPOIFSFileSystem}
*/
@Test
public void testDifferentPOIFS() throws Exception {
POIDataSamples docTests = POIDataSamples.getDocumentInstance();
// Open the two filesystems
DirectoryNode[] files = new DirectoryNode[2];
POIFSFileSystem poifs = new POIFSFileSystem(docTests.openResourceAsStream("test2.doc"));
files[0] = poifs.getRoot();
NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(docTests.getFile("test2.doc"));
files[1] = npoifsFileSystem.getRoot();
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) {
WordExtractor extractor = new WordExtractor(dir);
assertEquals(p_text1_block, extractor.getText());
assertEqualsTrim(p_text1_block, extractor.getText());
}
// Open via a HWPFDocument
for(DirectoryNode dir : files) {
HWPFDocument doc = new HWPFDocument(dir);
WordExtractor extractor = new WordExtractor(doc);
assertEquals(p_text1_block, extractor.getText());
assertEqualsTrim(p_text1_block, extractor.getText());
extractor.close();
}
poifs.close();
npoifsFileSystem.close();
npoifs.close();
}
/**
* [RESOLVED FIXED] Bug 51686 - Update to POI 3.8 beta 4 causes
* ConcurrentModificationException in Tika's OfficeParser
*/
public void testBug51686() throws IOException
{
InputStream is = POIDataSamples.getDocumentInstance()
.openResourceAsStream( "Bug51686.doc" );
@Test
public void testBug51686() throws IOException {
InputStream is = docTests.openResourceAsStream( "Bug51686.doc" );
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
String text = null;
@ -385,11 +388,14 @@ public final class TestWordExtractor extends TestCase {
}
assertNotNull(text);
fs.close();
}
@Test
public void testExtractorFromWord6Extractor() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getHPSFInstance().openResourceAsStream("TestMickey.doc"));
InputStream is = POIDataSamples.getHPSFInstance().openResourceAsStream("TestMickey.doc");
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
Word6Extractor wExt = new Word6Extractor(fs);
try {
POITextExtractor ext = wExt.getMetadataTextExtractor();
@ -405,6 +411,17 @@ public final class TestWordExtractor extends TestCase {
}
} finally {
wExt.close();
fs.close();
}
}
private WordExtractor openExtractor(String fileName) throws IOException {
InputStream is = docTests.openResourceAsStream(fileName);
try {
return new WordExtractor(is);
} finally {
is.close();
}
}
}

View File

@ -17,26 +17,33 @@
package org.apache.poi.hpsf.basic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
import org.apache.poi.POIDocument;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument;
import org.apache.poi.hpsf.MarkUnsupportedException;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.junit.Test;
/**
* Tests various bugs have been fixed
*/
public final class TestHPSFBugs extends TestCase {
public final class TestHPSFBugs {
private static final POIDataSamples _samples = POIDataSamples.getHPSFInstance();
/**
@ -44,62 +51,59 @@ public final class TestHPSFBugs extends TestCase {
* then add some properties to it, save +
* reload, and still access & change them.
*/
public void test48832() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
@Test
public void test48832() throws IOException {
HSSFWorkbook wb1 = new HSSFWorkbook();
// Starts empty
assertNull(wb.getDocumentSummaryInformation());
assertNull(wb.getSummaryInformation());
assertNull(wb1.getDocumentSummaryInformation());
assertNull(wb1.getSummaryInformation());
// Add new properties
wb.createInformationProperties();
wb1.createInformationProperties();
assertNotNull(wb.getDocumentSummaryInformation());
assertNotNull(wb.getSummaryInformation());
assertNotNull(wb1.getDocumentSummaryInformation());
assertNotNull(wb1.getSummaryInformation());
// Set initial values
wb.getSummaryInformation().setAuthor("Apache POI");
wb.getSummaryInformation().setKeywords("Testing POI");
wb.getSummaryInformation().setCreateDateTime(new Date(12345));
wb1.getSummaryInformation().setAuthor("Apache POI");
wb1.getSummaryInformation().setKeywords("Testing POI");
wb1.getSummaryInformation().setCreateDateTime(new Date(12345));
wb.getDocumentSummaryInformation().setCompany("Apache");
wb1.getDocumentSummaryInformation().setCompany("Apache");
// Save and reload
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos);
ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
wb = new HSSFWorkbook(bais);
HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
// Ensure changes were taken
assertNotNull(wb.getDocumentSummaryInformation());
assertNotNull(wb.getSummaryInformation());
assertNotNull(wb2.getDocumentSummaryInformation());
assertNotNull(wb2.getSummaryInformation());
assertEquals("Apache POI", wb.getSummaryInformation().getAuthor());
assertEquals("Testing POI", wb.getSummaryInformation().getKeywords());
assertEquals(12345, wb.getSummaryInformation().getCreateDateTime().getTime());
assertEquals("Apache", wb.getDocumentSummaryInformation().getCompany());
assertEquals("Apache POI", wb2.getSummaryInformation().getAuthor());
assertEquals("Testing POI", wb2.getSummaryInformation().getKeywords());
assertEquals(12345, wb2.getSummaryInformation().getCreateDateTime().getTime());
assertEquals("Apache", wb2.getDocumentSummaryInformation().getCompany());
// Set some more, save + reload
wb.getSummaryInformation().setComments("Resaved");
wb2.getSummaryInformation().setComments("Resaved");
baos = new ByteArrayOutputStream();
wb.write(baos);
bais = new ByteArrayInputStream(baos.toByteArray());
wb = new HSSFWorkbook(bais);
HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
wb2.close();
// Check again
assertNotNull(wb.getDocumentSummaryInformation());
assertNotNull(wb.getSummaryInformation());
assertNotNull(wb3.getDocumentSummaryInformation());
assertNotNull(wb3.getSummaryInformation());
assertEquals("Apache POI", wb.getSummaryInformation().getAuthor());
assertEquals("Testing POI", wb.getSummaryInformation().getKeywords());
assertEquals("Resaved", wb.getSummaryInformation().getComments());
assertEquals(12345, wb.getSummaryInformation().getCreateDateTime().getTime());
assertEquals("Apache", wb.getDocumentSummaryInformation().getCompany());
assertEquals("Apache POI", wb3.getSummaryInformation().getAuthor());
assertEquals("Testing POI", wb3.getSummaryInformation().getKeywords());
assertEquals("Resaved", wb3.getSummaryInformation().getComments());
assertEquals(12345, wb3.getSummaryInformation().getCreateDateTime().getTime());
assertEquals("Apache", wb3.getDocumentSummaryInformation().getCompany());
wb3.close();
}
/**
@ -107,16 +111,16 @@ public final class TestHPSFBugs extends TestCase {
* and without that you'll hit an ArrayIndexOutOfBoundsException after
* reading junk
*/
public void test54233() throws Exception {
DocumentInputStream dis;
NPOIFSFileSystem fs =
new NPOIFSFileSystem(_samples.openResourceAsStream("TestNon4ByteBoundary.doc"));
@Test
public void test54233() throws IOException, NoPropertySetStreamException, MarkUnsupportedException {
InputStream is = _samples.openResourceAsStream("TestNon4ByteBoundary.doc");
NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
is.close();
dis = fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME);
SummaryInformation si = (SummaryInformation)PropertySetFactory.create(dis);
dis = fs.createDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
DocumentSummaryInformation dsi = (DocumentSummaryInformation)PropertySetFactory.create(dis);
SummaryInformation si = (SummaryInformation)
PropertySetFactory.create(fs.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
DocumentSummaryInformation dsi = (DocumentSummaryInformation)
PropertySetFactory.create(fs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
// Test
assertEquals("Microsoft Word 10.0", si.getApplicationName());
@ -137,21 +141,24 @@ public final class TestHPSFBugs extends TestCase {
assertEquals("", si.getTitle());
assertEquals("", si.getAuthor());
assertEquals("Cour de Justice", dsi.getCompany());
doc.close();
fs.close();
}
/**
* CodePage Strings can be zero length
*/
public void test56138() throws Exception {
DocumentInputStream dis;
NPOIFSFileSystem fs =
new NPOIFSFileSystem(_samples.openResourceAsStream("TestZeroLengthCodePage.mpp"));
@Test
public void test56138() throws IOException, NoPropertySetStreamException {
InputStream is = _samples.openResourceAsStream("TestZeroLengthCodePage.mpp");
NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
is.close();
dis = fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME);
SummaryInformation si = (SummaryInformation)PropertySetFactory.create(dis);
dis = fs.createDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
DocumentSummaryInformation dsi = (DocumentSummaryInformation)PropertySetFactory.create(dis);
SummaryInformation si = (SummaryInformation)
PropertySetFactory.create(fs.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
DocumentSummaryInformation dsi = (DocumentSummaryInformation)
PropertySetFactory.create(fs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
// Test
assertEquals("MSProject", si.getApplicationName());
@ -160,5 +167,7 @@ public final class TestHPSFBugs extends TestCase {
assertEquals("", dsi.getCompany());
assertEquals(2, dsi.getSectionCount());
fs.close();
}
}

View File

@ -18,22 +18,24 @@
package org.apache.poi.poifs.filesystem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import junit.framework.TestCase;
import org.junit.Test;
/**
* Class to test DocumentOutputStream functionality
*
* @author Marc Johnson
*/
public final class TestDocumentOutputStream extends TestCase {
public final class TestDocumentOutputStream {
/**
* test write(int) behavior
*/
@Test
public void testWrite1() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DocumentOutputStream dstream = new DocumentOutputStream(stream, 25);
@ -57,12 +59,14 @@ public final class TestDocumentOutputStream extends TestCase {
{
assertEquals(( byte ) j, output[ j ]);
}
dstream.close();
stream.close();
}
/**
* test write(byte[]) behavior
*/
@Test
public void testWrite2() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DocumentOutputStream dstream = new DocumentOutputStream(stream, 25);
@ -96,12 +100,14 @@ public final class TestDocumentOutputStream extends TestCase {
output[ (j * 4) + k ]);
}
}
dstream.close();
stream.close();
}
/**
* test write(byte[], int, int) behavior
*/
@Test
public void testWrite3() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DocumentOutputStream dstream = new DocumentOutputStream(stream, 25);
@ -127,12 +133,14 @@ public final class TestDocumentOutputStream extends TestCase {
{
assertEquals(( byte ) (j + 1), output[ j ]);
}
dstream.close();
stream.close();
}
/**
* test writeFiller()
*/
@Test
public void testWriteFiller() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DocumentOutputStream dstream = new DocumentOutputStream(stream, 25);
@ -161,6 +169,7 @@ public final class TestDocumentOutputStream extends TestCase {
{
assertEquals(String.valueOf(j), ( byte ) 0xff, output[ j ]);
}
dstream.close();
stream.close();
}
}