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 3cf6c40004
commit 96a6a9bc1f
12 changed files with 447 additions and 351 deletions

View File

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

View File

@ -17,21 +17,24 @@
package org.apache.poi.xssf.extractor; 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.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import junit.framework.TestCase;
import org.apache.poi.POITextExtractor; import org.apache.poi.POITextExtractor;
import org.apache.poi.POIXMLTextExtractor; import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.extractor.ExcelExtractor; import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.XSSFTestDataSamples;
import org.junit.Test;
/** /**
* Tests for {@link XSSFEventBasedExcelExtractor} * Tests for {@link XSSFEventBasedExcelExtractor}
*/ */
public class TestXSSFEventBasedExcelExtractor extends TestCase { public class TestXSSFEventBasedExcelExtractor {
protected XSSFEventBasedExcelExtractor getExtractor(String sampleName) throws Exception { protected XSSFEventBasedExcelExtractor getExtractor(String sampleName) throws Exception {
return new XSSFEventBasedExcelExtractor(XSSFTestDataSamples. return new XSSFEventBasedExcelExtractor(XSSFTestDataSamples.
openSamplePackage(sampleName)); openSamplePackage(sampleName));
@ -40,6 +43,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
/** /**
* Get text out of the simple file * Get text out of the simple file
*/ */
@Test
public void testGetSimpleText() throws Exception { public void testGetSimpleText() throws Exception {
// a very simple file // a very simple file
XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsx"); XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsx");
@ -100,6 +104,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
extractor.close(); extractor.close();
} }
@Test
public void testGetComplexText() throws Exception { public void testGetComplexText() throws Exception {
// A fairly complex file // A fairly complex file
XSSFEventBasedExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx"); XSSFEventBasedExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx");
@ -117,7 +122,8 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
extractor.close(); extractor.close();
} }
public void testInlineStrings() throws Exception { @Test
public void testInlineStrings() throws Exception {
XSSFEventBasedExcelExtractor extractor = getExtractor("InlineStrings.xlsx"); XSSFEventBasedExcelExtractor extractor = getExtractor("InlineStrings.xlsx");
extractor.setFormulasNotResults(true); extractor.setFormulasNotResults(true);
String text = extractor.getText(); String text = extractor.getText();
@ -139,14 +145,15 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
assertTrue("Unable to find expected word in text\n" + text, text.contains("A5-A$2")); assertTrue("Unable to find expected word in text\n" + text, text.contains("A5-A$2"));
extractor.close(); extractor.close();
} }
/** /**
* Test that we return pretty much the same as * Test that we return pretty much the same as
* ExcelExtractor does, when we're both passed * ExcelExtractor does, when we're both passed
* the same file, just saved as xls and xlsx * the same file, just saved as xls and xlsx
*/ */
public void testComparedToOLE2() throws Exception { @Test
public void testComparedToOLE2() throws Exception {
// A fairly simple file - ooxml // A fairly simple file - ooxml
XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("SampleSS.xlsx"); XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("SampleSS.xlsx");
@ -171,6 +178,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
* Test text extraction from text box using getShapes() * Test text extraction from text box using getShapes()
* @throws Exception * @throws Exception
*/ */
@Test
public void testShapes() throws Exception{ public void testShapes() throws Exception{
XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("WithTextBox.xlsx"); 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 * Test that we return the same output for unstyled numbers as the
* non-event-based XSSFExcelExtractor. * non-event-based XSSFExcelExtractor.
*/ */
@Test
public void testUnstyledNumbersComparedToNonEventBasedExtractor() public void testUnstyledNumbersComparedToNonEventBasedExtractor()
throws Exception { throws Exception {
@ -216,6 +225,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
* Test that we return the same output headers and footers as the * Test that we return the same output headers and footers as the
* non-event-based XSSFExcelExtractor. * non-event-based XSSFExcelExtractor.
*/ */
@Test
public void testHeadersAndFootersComparedToNonEventBasedExtractor() public void testHeadersAndFootersComparedToNonEventBasedExtractor()
throws Exception { throws Exception {
@ -260,6 +270,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
* This test will need to be modified if these improvements are ported to * This test will need to be modified if these improvements are ported to
* XSSFExcelExtractor. * XSSFExcelExtractor.
*/ */
@Test
public void testCommentsComparedToNonEventBasedExtractor() public void testCommentsComparedToNonEventBasedExtractor()
throws Exception { throws Exception {
@ -315,6 +326,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
} }
} }
@Test
public void testFile56278_normal() throws Exception { public void testFile56278_normal() throws Exception {
// first with normal Text Extractor // first with normal Text Extractor
POIXMLTextExtractor extractor = new XSSFExcelExtractor( POIXMLTextExtractor extractor = new XSSFExcelExtractor(
@ -326,6 +338,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
} }
} }
@Test
public void testFile56278_event() throws Exception { public void testFile56278_event() throws Exception {
// then with event based one // then with event based one
POIXMLTextExtractor extractor = getExtractor("56278.xlsx"); POIXMLTextExtractor extractor = getExtractor("56278.xlsx");
@ -336,6 +349,7 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
} }
} }
@Test
public void test59021() throws Exception { public void test59021() throws Exception {
XSSFEventBasedExcelExtractor ex = XSSFEventBasedExcelExtractor ex =
new XSSFEventBasedExcelExtractor( new XSSFEventBasedExcelExtractor(
@ -343,5 +357,6 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
String text = ex.getText(); String text = ex.getText();
assertTrue("can't find Abhkazia", text.contains("Abkhazia - Fixed")); assertTrue("can't find Abhkazia", text.contains("Abkhazia - Fixed"));
assertTrue("can't find 10/02/2016", text.contains("10/02/2016")); 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.extractor.ExtractorFactory;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.junit.After;
public class TestXSSFEventBasedExcelExtractorUsingFactory extends TestXSSFEventBasedExcelExtractor { public class TestXSSFEventBasedExcelExtractorUsingFactory extends TestXSSFEventBasedExcelExtractor {
@ -27,7 +28,7 @@ public class TestXSSFEventBasedExcelExtractorUsingFactory extends TestXSSFEventB
return (XSSFEventBasedExcelExtractor) ExtractorFactory.createExtractor(HSSFTestDataSamples.openSampleFileStream(sampleName)); return (XSSFEventBasedExcelExtractor) ExtractorFactory.createExtractor(HSSFTestDataSamples.openSampleFileStream(sampleName));
} }
@Override @After
public void tearDown() { public void tearDown() {
// reset setting to not affect other tests // reset setting to not affect other tests
ExtractorFactory.setAllThreadsPreferEventExtractors(null); ExtractorFactory.setAllThreadsPreferEventExtractors(null);

View File

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

View File

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

View File

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

View File

@ -17,69 +17,73 @@
package org.apache.poi.hdgf.extractor; 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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream; import java.io.PrintStream;
import junit.framework.TestCase; import org.apache.poi.POIDataSamples;
import org.apache.poi.hdgf.HDGFDiagram; import org.apache.poi.hdgf.HDGFDiagram;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; 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 static POIDataSamples _dgTests = POIDataSamples.getDiagramInstance();
private String defFilename; private final String defFilename = "Test_Visio-Some_Random_Text.vsd";
private int defTextChunks; private final int defTextChunks = 5;
@Override
protected void setUp() {
defFilename = "Test_Visio-Some_Random_Text.vsd";
defTextChunks = 5;
}
/** /**
* Test the 3 different ways of creating one * Test the 3 different ways of creating one
*/ */
public void testCreation() throws Exception { @Test
VisioTextExtractor extractor; 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)); InputStream is2 = _dgTests.openResourceAsStream(defFilename);
assertNotNull(extractor); POIFSFileSystem poifs2 = new POIFSFileSystem(is2);
assertNotNull(extractor.getAllText()); is2.close();
assertEquals(defTextChunks, extractor.getAllText().length); VisioTextExtractor extractor2 = new VisioTextExtractor(poifs2);
assertNotNull(extractor2);
assertNotNull(extractor2.getAllText());
assertEquals(defTextChunks, extractor2.getAllText().length);
extractor2.close();
poifs2.close();
extractor = new VisioTextExtractor( InputStream is3 = _dgTests.openResourceAsStream(defFilename);
new POIFSFileSystem( POIFSFileSystem poifs3 = new POIFSFileSystem(is3);
_dgTests.openResourceAsStream(defFilename) is3.close();
) HDGFDiagram hdgf3 = new HDGFDiagram(poifs3);
);
assertNotNull(extractor);
assertNotNull(extractor.getAllText());
assertEquals(defTextChunks, extractor.getAllText().length);
extractor = new VisioTextExtractor(
new HDGFDiagram( VisioTextExtractor extractor3 = new VisioTextExtractor(hdgf3);
new POIFSFileSystem( assertNotNull(extractor3);
_dgTests.openResourceAsStream(defFilename) assertNotNull(extractor3.getAllText());
) assertEquals(defTextChunks, extractor3.getAllText().length);
) extractor3.close();
); hdgf3.close();
assertNotNull(extractor); poifs3.close();
assertNotNull(extractor.getAllText());
assertEquals(defTextChunks, extractor.getAllText().length);
} }
@Test
public void testExtraction() throws Exception { public void testExtraction() throws Exception {
VisioTextExtractor extractor = VisioTextExtractor extractor = openExtractor(defFilename);
new VisioTextExtractor(_dgTests.openResourceAsStream(defFilename));
// Check the array fetch // Check the array fetch
String[] text = extractor.getAllText(); String[] text = extractor.getAllText();
assertNotNull(text); assertNotNull(text);
assertEquals(defTextChunks, text.length); assertEquals(defTextChunks, text.length);
assertEquals("text\n", text[0]); assertEquals("text\n", text[0]);
assertEquals("View\n", text[1]); assertEquals("View\n", text[1]);
assertEquals("Test View\n", text[2]); assertEquals("Test View\n", text[2]);
assertEquals("I am a test view\n", text[3]); assertEquals("I am a test view\n", text[3]);
assertEquals("Some random text, on a page\n", text[4]); assertEquals("Some random text, on a page\n", text[4]);
@ -87,8 +91,10 @@ public final class TestVisioExtractor extends TestCase {
// And the all-in fetch // And the all-in fetch
String textS = extractor.getText(); String textS = extractor.getText();
assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page\n", textS); 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 { public void testProblemFiles() throws Exception {
String[] files = { String[] files = {
"44594.vsd", "44594-2.vsd", "44594.vsd", "44594-2.vsd",
@ -96,11 +102,13 @@ public final class TestVisioExtractor extends TestCase {
"NegativeChunkLength.vsd", "NegativeChunkLength2.vsd" "NegativeChunkLength.vsd", "NegativeChunkLength2.vsd"
}; };
for(String file : files){ for(String file : files){
VisioTextExtractor ex = new VisioTextExtractor(_dgTests.openResourceAsStream(file)); VisioTextExtractor ex = openExtractor(file);
ex.getText(); ex.getText();
ex.close();
} }
} }
@Test
public void testMain() throws Exception { public void testMain() throws Exception {
PrintStream oldOut = System.out; PrintStream oldOut = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
@ -124,4 +132,13 @@ public final class TestVisioExtractor extends TestCase {
"Some random text, on a page\n" "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; package org.apache.poi.hpbf;
import junit.framework.TestCase; import static org.junit.Assert.assertNotNull;
import org.apache.poi.POIDataSamples; 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(); private static final POIDataSamples _samples = POIDataSamples.getPublisherInstance();
public void testOpen() throws Exception { @Test
HPBFDocument doc = new HPBFDocument( public void testOpen() throws IOException {
_samples.openResourceAsStream("Sample.pub") InputStream is = _samples.openResourceAsStream("Sample.pub");
); HPBFDocument doc = new HPBFDocument(is);
is.close();
assertNotNull(doc); assertNotNull(doc);
doc.close();
} }
public void testBits() throws Exception { @Test
HPBFDocument doc = new HPBFDocument( public void testBits() throws IOException {
_samples.openResourceAsStream("Sample.pub") InputStream is = _samples.openResourceAsStream("Sample.pub");
); HPBFDocument doc = new HPBFDocument(is);
is.close();
assertNotNull(doc.getMainContents()); assertNotNull(doc.getMainContents());
assertNotNull(doc.getQuillContents()); assertNotNull(doc.getQuillContents());
@ -45,6 +53,8 @@ public final class TestHPBFDocument extends TestCase {
assertTrue(doc.getQuillContents().getData().length > 0); assertTrue(doc.getQuillContents().getData().length > 0);
assertTrue(doc.getEscherStm().getData().length > 0); assertTrue(doc.getEscherStm().getData().length > 0);
assertTrue(doc.getEscherDelayStm().getData().length == 0); assertTrue(doc.getEscherDelayStm().getData().length == 0);
doc.close();
} }
// TODO // TODO

View File

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

View File

@ -18,7 +18,14 @@
package org.apache.poi.hwpf.extractor; package org.apache.poi.hwpf.extractor;
import static org.apache.poi.POITestCase.assertContains; 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.POIDataSamples;
import org.apache.poi.POITextExtractor; 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.DirectoryNode;
import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; 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.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
/** /**
* Test the different routes to extracting text * 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" ) String newExpected = expected.replaceAll( "\r\n", "\n" )
.replaceAll( "\r", "\n" ).trim(); .replaceAll( "\r", "\n" ).trim();
String newActual = actual.replaceAll( "\r\n", "\n" ) String newActual = actual.replaceAll( "\r\n", "\n" )
.replaceAll( "\r", "\n" ).trim(); .replaceAll( "\r", "\n" ).trim();
TestCase.assertEquals( newExpected, newActual ); assertEquals( newExpected, newActual );
} }
private static void assertExtractedContains(String[] extracted, String needle) { private static void assertExtractedContains(String[] extracted, String needle) {
@ -71,68 +77,40 @@ public final class TestWordExtractor extends TestCase {
"\r\n", "\r\n",
"It is otherwise very very boring.\r\n" "It is otherwise very very boring.\r\n"
}; };
private String p_text1_block = "";
// Well behaved document // Build splat'd out text version
private WordExtractor extractor; private final String p_text1_block = StringUtil.join(p_text1, "");
// 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();
}
/** /**
* Test paragraph based extraction * Test paragraph based extraction
*/ */
public void testExtractFromParagraphs() { @Test
public void testExtractFromParagraphs() throws IOException {
WordExtractor extractor = openExtractor("test2.doc");
String[] text = extractor.getParagraphText(); String[] text = extractor.getParagraphText();
assertEquals(p_text1.length, text.length); assertEquals(p_text1.length, text.length);
for (int i = 0; i < p_text1.length; i++) { for (int i = 0; i < p_text1.length; i++) {
assertEquals(p_text1[i], text[i]); assertEquals(p_text1[i], text[i]);
} }
extractor.close();
// Lots of paragraphs with only a few lines in them // Lots of paragraphs with only a few lines in them
WordExtractor extractor2 = openExtractor("test.doc");
assertEquals(24, extractor2.getParagraphText().length); assertEquals(24, extractor2.getParagraphText().length);
assertEquals("as d\r\n", extractor2.getParagraphText()[16]); assertEquals("as d\r\n", extractor2.getParagraphText()[16]);
assertEquals("as d\r\n", extractor2.getParagraphText()[17]); assertEquals("as d\r\n", extractor2.getParagraphText()[17]);
assertEquals("as d\r\n", extractor2.getParagraphText()[18]); assertEquals("as d\r\n", extractor2.getParagraphText()[18]);
extractor2.close();
} }
/** /**
* Test the paragraph -> flat extraction * Test the paragraph -> flat extraction
*/ */
public void testGetText() { @Test
assertEquals(p_text1_block, extractor.getText()); public void testGetText() throws IOException {
WordExtractor extractor = openExtractor("test2.doc");
assertEqualsTrim(p_text1_block, extractor.getText());
// For the 2nd, should give similar answers for // For the 2nd, should give similar answers for
// the two methods, differing only in line endings // the two methods, differing only in line endings
@ -141,26 +119,29 @@ public final class TestWordExtractor extends TestCase {
// assertEquals( // assertEquals(
// extractor2.getTextFromPieces().replaceAll("[\\r\\n]", ""), // extractor2.getTextFromPieces().replaceAll("[\\r\\n]", ""),
// extractor2.getText().replaceAll("[\\r\\n]", "")); // extractor2.getText().replaceAll("[\\r\\n]", ""));
extractor.close();
} }
/** /**
* Test textPieces based extraction * Test textPieces based extraction
*/ */
public void testExtractFromTextPieces() { @Test
public void testExtractFromTextPieces() throws IOException {
WordExtractor extractor = openExtractor("test2.doc");
String text = extractor.getTextFromPieces(); String text = extractor.getTextFromPieces();
assertEquals(p_text1_block, text); assertEquals(p_text1_block, text);
extractor.close();
} }
/** /**
* Test that we can get data from two different * Test that we can get data from two different embedded word documents
* embeded word documents
* @throws Exception
*/ */
public void testExtractFromEmbeded() throws Exception { @Test
POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getSpreadSheetInstance().openResourceAsStream(filename3)); public void testExtractFromEmbeded() throws IOException {
HWPFDocument doc; InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("excel_with_embeded.xls");
WordExtractor extractor3; POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
DirectoryNode dirA = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B7"); DirectoryNode dirA = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B7");
DirectoryNode dirB = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B2"); DirectoryNode dirB = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B2");
@ -173,101 +154,123 @@ public final class TestWordExtractor extends TestCase {
assertNotNull(dirB.getEntry("WordDocument")); assertNotNull(dirB.getEntry("WordDocument"));
// Check each in turn // Check each in turn
doc = new HWPFDocument(dirA); HWPFDocument docA = new HWPFDocument(dirA);
extractor3 = new WordExtractor(doc); WordExtractor extractorA = new WordExtractor(docA);
assertNotNull(extractor3.getText()); assertNotNull(extractorA.getText());
assertTrue(extractor3.getText().length() > 20); assertTrue(extractorA.getText().length() > 20);
assertEquals("I am a sample document\r\nNot much on me\r\nI am document 1\r\n", extractor3 assertEqualsTrim("I am a sample document\r\nNot much on me\r\nI am document 1\r\n", extractorA.getText());
.getText()); assertEquals("Sample Doc 1", extractorA.getSummaryInformation().getTitle());
assertEquals("Sample Doc 1", extractor3.getSummaryInformation().getTitle()); assertEquals("Sample Test", extractorA.getSummaryInformation().getSubject());
assertEquals("Sample Test", extractor3.getSummaryInformation().getSubject());
doc = new HWPFDocument(dirB); HWPFDocument docB = new HWPFDocument(dirB);
extractor3 = new WordExtractor(doc); WordExtractor extractorB = new WordExtractor(docB);
assertNotNull(extractor3.getText()); assertNotNull(extractorB.getText());
assertTrue(extractor3.getText().length() > 20); assertTrue(extractorB.getText().length() > 20);
assertEquals("I am another sample document\r\nNot much on me\r\nI am document 2\r\n", assertEqualsTrim("I am another sample document\r\nNot much on me\r\nI am document 2\r\n", extractorB.getText());
extractor3.getText()); assertEquals("Sample Doc 2", extractorB.getSummaryInformation().getTitle());
assertEquals("Sample Doc 2", extractor3.getSummaryInformation().getTitle()); assertEquals("Another Sample Test", extractorB.getSummaryInformation().getSubject());
assertEquals("Another Sample Test", extractor3.getSummaryInformation().getSubject());
extractorA.close();
docA.close();
extractorB.close();
docB.close();
fs.close();
} }
public void testWithHeader() { @Test
public void testWithHeader() throws IOException {
// Non-unicode // Non-unicode
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename4); HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc");
extractor = new WordExtractor(doc); WordExtractor extractor1 = new WordExtractor(doc1);
assertEquals("First header column!\tMid header Right header!\n", extractor.getHeaderText()); assertEquals("First header column!\tMid header Right header!\n", extractor1.getHeaderText());
assertContains(extractor.getText(), "First header column!"); assertContains(extractor1.getText(), "First header column!");
extractor1.close();
doc1.close();
// Unicode // Unicode
doc = HWPFTestDataSamples.openSampleFile(filename5); HWPFDocument doc2 = HWPFTestDataSamples.openSampleFile("HeaderFooterUnicode.doc");
extractor = new WordExtractor(doc); WordExtractor extractor2 = new WordExtractor(doc2);
assertEquals("This is a simple header, with a \u20ac euro symbol in it.\n\n", extractor.getHeaderText()); assertEquals("This is a simple header, with a \u20ac euro symbol in it.\n\n", extractor2.getHeaderText());
assertContains(extractor.getText(), "This is a simple header"); assertContains(extractor2.getText(), "This is a simple header");
extractor2.close();
doc2.close();
} }
public void testWithFooter() { @Test
public void testWithFooter() throws IOException {
// Non-unicode // Non-unicode
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename4); HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc");
extractor = new WordExtractor(doc); WordExtractor extractor1 = new WordExtractor(doc1);
assertEquals("Footer Left\tFooter Middle Footer Right\n", extractor.getFooterText()); assertEquals("Footer Left\tFooter Middle Footer Right\n", extractor1.getFooterText());
assertContains(extractor.getText(), "Footer Left"); assertContains(extractor1.getText(), "Footer Left");
extractor1.close();
doc1.close();
// Unicode // Unicode
doc = HWPFTestDataSamples.openSampleFile(filename5); HWPFDocument doc2 = HWPFTestDataSamples.openSampleFile("HeaderFooterUnicode.doc");
extractor = new WordExtractor(doc); WordExtractor extractor2 = new WordExtractor(doc2);
assertEquals("The footer, with Moli\u00e8re, has Unicode in it.\n", extractor.getFooterText()); assertEquals("The footer, with Moli\u00e8re, has Unicode in it.\n", extractor2.getFooterText());
assertContains(extractor.getText(), "The footer, with"); assertContains(extractor2.getText(), "The footer, with");
extractor2.close();
doc2.close();
} }
public void testFootnote() { @Test
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename6); public void testFootnote() throws IOException {
extractor = new WordExtractor(doc); HWPFDocument doc = HWPFTestDataSamples.openSampleFile("footnote.doc");
WordExtractor extractor = new WordExtractor(doc);
assertExtractedContains(extractor.getFootnoteText(), "TestFootnote"); assertExtractedContains(extractor.getFootnoteText(), "TestFootnote");
assertEquals(0x00, doc.getRange().getSection(0).getFootnoteNumberingFormat()); // msonfcArabic assertEquals(0x00, doc.getRange().getSection(0).getFootnoteNumberingFormat()); // msonfcArabic
assertEquals(0x00, doc.getRange().getSection(0).getFootnoteRestartQualifier()); // rncCont assertEquals(0x00, doc.getRange().getSection(0).getFootnoteRestartQualifier()); // rncCont
assertEquals(0, doc.getRange().getSection(0).getFootnoteNumberingOffset()); assertEquals(0, doc.getRange().getSection(0).getFootnoteNumberingOffset());
assertEquals(1, doc.getFootnotes().getNotesCount()); assertEquals(1, doc.getFootnotes().getNotesCount());
extractor.close();
doc.close();
} }
public void testEndnote() { @Test
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename6); public void testEndnote() throws IOException {
extractor = new WordExtractor(doc); HWPFDocument doc = HWPFTestDataSamples.openSampleFile("footnote.doc");
WordExtractor extractor = new WordExtractor(doc);
assertExtractedContains(extractor.getEndnoteText(), "TestEndnote"); assertExtractedContains(extractor.getEndnoteText(), "TestEndnote");
assertEquals(0x02, doc.getRange().getSection(0).getEndnoteNumberingFormat()); // msonfcLCRoman assertEquals(0x02, doc.getRange().getSection(0).getEndnoteNumberingFormat()); // msonfcLCRoman
assertEquals(0x00, doc.getRange().getSection(0).getEndnoteRestartQualifier()); // rncCont assertEquals(0x00, doc.getRange().getSection(0).getEndnoteRestartQualifier()); // rncCont
assertEquals(0, doc.getRange().getSection(0).getEndnoteNumberingOffset()); assertEquals(0, doc.getRange().getSection(0).getEndnoteNumberingOffset());
assertEquals(1, doc.getEndnotes().getNotesCount()); assertEquals(1, doc.getEndnotes().getNotesCount());
extractor.close();
doc.close();
} }
public void testComments() { @Test
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename6); public void testComments() throws IOException {
extractor = new WordExtractor(doc); WordExtractor extractor = openExtractor("footnote.doc");
assertExtractedContains(extractor.getCommentsText(), "TestComment"); assertExtractedContains(extractor.getCommentsText(), "TestComment");
extractor.close();
} }
public void testWord95() throws Exception { @Test(expected=OldWordFileFormatException.class)
// Too old for the default public void testWord95_WordExtractor() throws Exception {
try { // Too old for the default
extractor = new WordExtractor( openExtractor("Word95.doc").close();
POIDataSamples.getDocumentInstance().openResourceAsStream("Word95.doc") }
);
fail();
} catch(OldWordFileFormatException e) {}
@Test
public void testWord95() throws Exception {
// Can work with the special one // Can work with the special one
Word6Extractor w6e = new Word6Extractor( InputStream is = docTests.openResourceAsStream("Word95.doc");
POIDataSamples.getDocumentInstance().openResourceAsStream("Word95.doc") Word6Extractor w6e = new Word6Extractor(is);
); is.close();
String text = w6e.getText(); String text = w6e.getText();
assertTrue(text.contains("The quick brown fox jumps over the lazy dog")); 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("Paragraph 3. Has some RED text and some BLUE BOLD text in it.\r\n", tp[4]);
assertEquals("\r\n", tp[5]); assertEquals("\r\n", tp[5]);
assertEquals("Last (4th) paragraph.\r\n", tp[6]); 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 // Too old for the default
try { openExtractor("Word6.doc").close();
extractor = new WordExtractor( }
POIDataSamples.getDocumentInstance().openResourceAsStream("Word6.doc")
);
fail();
} catch(OldWordFileFormatException e) {}
Word6Extractor w6e = new Word6Extractor( @Test
POIDataSamples.getDocumentInstance().openResourceAsStream("Word6.doc") public void testWord6() throws Exception {
); InputStream is = docTests.openResourceAsStream("Word6.doc");
Word6Extractor w6e = new Word6Extractor(is);
is.close();
String text = w6e.getText(); String text = w6e.getText();
assertTrue(text.contains("The quick brown fox jumps over the lazy dog")); 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(); String[] tp = w6e.getParagraphText();
assertEquals(1, tp.length); assertEquals(1, tp.length);
assertEquals("The quick brown fox jumps over the lazy dog\r\n", tp[0]); assertEquals("The quick brown fox jumps over the lazy dog\r\n", tp[0]);
w6e.close();
} }
@Test
public void testFastSaved() throws Exception { public void testFastSaved() throws Exception {
extractor = new WordExtractor( WordExtractor extractor = openExtractor("rasp.doc");
POIDataSamples.getDocumentInstance().openResourceAsStream("rasp.doc")
);
String text = extractor.getText(); String text = extractor.getText();
assertTrue(text.contains("\u0425\u0425\u0425\u0425\u0425")); assertTrue(text.contains("\u0425\u0425\u0425\u0425\u0425"));
@ -319,57 +322,57 @@ public final class TestWordExtractor extends TestCase {
extractor.close(); extractor.close();
} }
@Test
public void testFirstParagraphFix() throws Exception { public void testFirstParagraphFix() throws Exception {
extractor = new WordExtractor( WordExtractor extractor = openExtractor("Bug48075.doc");
POIDataSamples.getDocumentInstance().openResourceAsStream("Bug48075.doc")
);
String text = extractor.getText(); String text = extractor.getText();
assertTrue(text.startsWith("\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435")); assertTrue(text.startsWith("\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435"));
extractor.close();
} }
/** /**
* Tests that we can work with both {@link POIFSFileSystem} * Tests that we can work with both {@link POIFSFileSystem}
* and {@link NPOIFSFileSystem} * and {@link NPOIFSFileSystem}
*/ */
@Test
public void testDifferentPOIFS() throws Exception { public void testDifferentPOIFS() throws Exception {
POIDataSamples docTests = POIDataSamples.getDocumentInstance();
// Open the two filesystems // Open the two filesystems
DirectoryNode[] files = new DirectoryNode[2]; File file = docTests.getFile("test2.doc");
POIFSFileSystem poifs = new POIFSFileSystem(docTests.openResourceAsStream("test2.doc")); InputStream is = new FileInputStream(file);
files[0] = poifs.getRoot(); OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is);
NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(docTests.getFile("test2.doc")); is.close();
files[1] = npoifsFileSystem.getRoot(); NPOIFSFileSystem npoifs = new NPOIFSFileSystem(file);
DirectoryNode[] files = { opoifs.getRoot(), npoifs.getRoot() };
// Open directly // Open directly
for(DirectoryNode dir : files) { for(DirectoryNode dir : files) {
WordExtractor extractor = new WordExtractor(dir); WordExtractor extractor = new WordExtractor(dir);
assertEquals(p_text1_block, extractor.getText()); assertEqualsTrim(p_text1_block, extractor.getText());
} }
// Open via a HWPFDocument // Open via a HWPFDocument
for(DirectoryNode dir : files) { for(DirectoryNode dir : files) {
HWPFDocument doc = new HWPFDocument(dir); HWPFDocument doc = new HWPFDocument(dir);
WordExtractor extractor = new WordExtractor(doc); WordExtractor extractor = new WordExtractor(doc);
assertEquals(p_text1_block, extractor.getText()); assertEqualsTrim(p_text1_block, extractor.getText());
extractor.close();
} }
poifs.close(); npoifs.close();
npoifsFileSystem.close();
} }
/** /**
* [RESOLVED FIXED] Bug 51686 - Update to POI 3.8 beta 4 causes * [RESOLVED FIXED] Bug 51686 - Update to POI 3.8 beta 4 causes
* ConcurrentModificationException in Tika's OfficeParser * ConcurrentModificationException in Tika's OfficeParser
*/ */
public void testBug51686() throws IOException @Test
{ public void testBug51686() throws IOException {
InputStream is = POIDataSamples.getDocumentInstance() InputStream is = docTests.openResourceAsStream( "Bug51686.doc" );
.openResourceAsStream( "Bug51686.doc" );
POIFSFileSystem fs = new POIFSFileSystem(is); POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
String text = null; String text = null;
@ -385,11 +388,14 @@ public final class TestWordExtractor extends TestCase {
} }
assertNotNull(text); assertNotNull(text);
fs.close();
} }
@Test
public void testExtractorFromWord6Extractor() throws Exception { 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); Word6Extractor wExt = new Word6Extractor(fs);
try { try {
POITextExtractor ext = wExt.getMetadataTextExtractor(); POITextExtractor ext = wExt.getMetadataTextExtractor();
@ -405,6 +411,17 @@ public final class TestWordExtractor extends TestCase {
} }
} finally { } finally {
wExt.close(); 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; 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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date; import java.util.Date;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.POIDocument; import org.apache.poi.POIDocument;
import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument; 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.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.junit.Test;
/** /**
* Tests various bugs have been fixed * Tests various bugs have been fixed
*/ */
public final class TestHPSFBugs extends TestCase { public final class TestHPSFBugs {
private static final POIDataSamples _samples = POIDataSamples.getHPSFInstance(); private static final POIDataSamples _samples = POIDataSamples.getHPSFInstance();
/** /**
@ -44,62 +51,59 @@ public final class TestHPSFBugs extends TestCase {
* then add some properties to it, save + * then add some properties to it, save +
* reload, and still access & change them. * reload, and still access & change them.
*/ */
public void test48832() throws Exception { @Test
HSSFWorkbook wb = new HSSFWorkbook(); public void test48832() throws IOException {
HSSFWorkbook wb1 = new HSSFWorkbook();
// Starts empty // Starts empty
assertNull(wb.getDocumentSummaryInformation()); assertNull(wb1.getDocumentSummaryInformation());
assertNull(wb.getSummaryInformation()); assertNull(wb1.getSummaryInformation());
// Add new properties // Add new properties
wb.createInformationProperties(); wb1.createInformationProperties();
assertNotNull(wb.getDocumentSummaryInformation()); assertNotNull(wb1.getDocumentSummaryInformation());
assertNotNull(wb.getSummaryInformation()); assertNotNull(wb1.getSummaryInformation());
// Set initial values // Set initial values
wb.getSummaryInformation().setAuthor("Apache POI"); wb1.getSummaryInformation().setAuthor("Apache POI");
wb.getSummaryInformation().setKeywords("Testing POI"); wb1.getSummaryInformation().setKeywords("Testing POI");
wb.getSummaryInformation().setCreateDateTime(new Date(12345)); wb1.getSummaryInformation().setCreateDateTime(new Date(12345));
wb.getDocumentSummaryInformation().setCompany("Apache"); wb1.getDocumentSummaryInformation().setCompany("Apache");
// Save and reload // Save and reload
ByteArrayOutputStream baos = new ByteArrayOutputStream(); HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
wb.write(baos); wb1.close();
ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
wb = new HSSFWorkbook(bais);
// Ensure changes were taken // Ensure changes were taken
assertNotNull(wb.getDocumentSummaryInformation()); assertNotNull(wb2.getDocumentSummaryInformation());
assertNotNull(wb.getSummaryInformation()); assertNotNull(wb2.getSummaryInformation());
assertEquals("Apache POI", wb.getSummaryInformation().getAuthor()); assertEquals("Apache POI", wb2.getSummaryInformation().getAuthor());
assertEquals("Testing POI", wb.getSummaryInformation().getKeywords()); assertEquals("Testing POI", wb2.getSummaryInformation().getKeywords());
assertEquals(12345, wb.getSummaryInformation().getCreateDateTime().getTime()); assertEquals(12345, wb2.getSummaryInformation().getCreateDateTime().getTime());
assertEquals("Apache", wb.getDocumentSummaryInformation().getCompany()); assertEquals("Apache", wb2.getDocumentSummaryInformation().getCompany());
// Set some more, save + reload // Set some more, save + reload
wb.getSummaryInformation().setComments("Resaved"); wb2.getSummaryInformation().setComments("Resaved");
baos = new ByteArrayOutputStream(); HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
wb.write(baos); wb2.close();
bais = new ByteArrayInputStream(baos.toByteArray());
wb = new HSSFWorkbook(bais);
// Check again // Check again
assertNotNull(wb.getDocumentSummaryInformation()); assertNotNull(wb3.getDocumentSummaryInformation());
assertNotNull(wb.getSummaryInformation()); assertNotNull(wb3.getSummaryInformation());
assertEquals("Apache POI", wb.getSummaryInformation().getAuthor()); assertEquals("Apache POI", wb3.getSummaryInformation().getAuthor());
assertEquals("Testing POI", wb.getSummaryInformation().getKeywords()); assertEquals("Testing POI", wb3.getSummaryInformation().getKeywords());
assertEquals("Resaved", wb.getSummaryInformation().getComments()); assertEquals("Resaved", wb3.getSummaryInformation().getComments());
assertEquals(12345, wb.getSummaryInformation().getCreateDateTime().getTime()); assertEquals(12345, wb3.getSummaryInformation().getCreateDateTime().getTime());
assertEquals("Apache", wb.getDocumentSummaryInformation().getCompany()); 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 * and without that you'll hit an ArrayIndexOutOfBoundsException after
* reading junk * reading junk
*/ */
public void test54233() throws Exception { @Test
DocumentInputStream dis; public void test54233() throws IOException, NoPropertySetStreamException, MarkUnsupportedException {
NPOIFSFileSystem fs = InputStream is = _samples.openResourceAsStream("TestNon4ByteBoundary.doc");
new NPOIFSFileSystem(_samples.openResourceAsStream("TestNon4ByteBoundary.doc")); NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
is.close();
dis = fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME); SummaryInformation si = (SummaryInformation)
SummaryInformation si = (SummaryInformation)PropertySetFactory.create(dis); PropertySetFactory.create(fs.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
DocumentSummaryInformation dsi = (DocumentSummaryInformation)
dis = fs.createDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME); PropertySetFactory.create(fs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
DocumentSummaryInformation dsi = (DocumentSummaryInformation)PropertySetFactory.create(dis);
// Test // Test
assertEquals("Microsoft Word 10.0", si.getApplicationName()); assertEquals("Microsoft Word 10.0", si.getApplicationName());
@ -137,21 +141,24 @@ public final class TestHPSFBugs extends TestCase {
assertEquals("", si.getTitle()); assertEquals("", si.getTitle());
assertEquals("", si.getAuthor()); assertEquals("", si.getAuthor());
assertEquals("Cour de Justice", dsi.getCompany()); assertEquals("Cour de Justice", dsi.getCompany());
doc.close();
fs.close();
} }
/** /**
* CodePage Strings can be zero length * CodePage Strings can be zero length
*/ */
public void test56138() throws Exception { @Test
DocumentInputStream dis; public void test56138() throws IOException, NoPropertySetStreamException {
NPOIFSFileSystem fs = InputStream is = _samples.openResourceAsStream("TestZeroLengthCodePage.mpp");
new NPOIFSFileSystem(_samples.openResourceAsStream("TestZeroLengthCodePage.mpp")); NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
is.close();
dis = fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME); SummaryInformation si = (SummaryInformation)
SummaryInformation si = (SummaryInformation)PropertySetFactory.create(dis); PropertySetFactory.create(fs.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
DocumentSummaryInformation dsi = (DocumentSummaryInformation)
dis = fs.createDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME); PropertySetFactory.create(fs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
DocumentSummaryInformation dsi = (DocumentSummaryInformation)PropertySetFactory.create(dis);
// Test // Test
assertEquals("MSProject", si.getApplicationName()); assertEquals("MSProject", si.getApplicationName());
@ -160,5 +167,7 @@ public final class TestHPSFBugs extends TestCase {
assertEquals("", dsi.getCompany()); assertEquals("", dsi.getCompany());
assertEquals(2, dsi.getSectionCount()); assertEquals(2, dsi.getSectionCount());
fs.close();
} }
} }

View File

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