From: Andreas Beeker Date: Mon, 16 Jan 2017 01:18:46 +0000 (+0000) Subject: fix eclipse warnings / close resources in tests X-Git-Tag: REL_3_16_BETA2~35 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=96a6a9bc1fde998bf566423dbb556c51c14d2cc3;p=poi.git fix eclipse warnings / close resources in tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1778958 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java index 4a411cc3d9..cd2d532e34 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java @@ -349,6 +349,7 @@ public class TestEncryptor { e.confirmPassword("AYBABTU"); docx.write(e.getDataStream(fs)); + docx.close(); fs.close(); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractor.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractor.java index b223804e92..73d22aeebd 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractor.java @@ -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,7 +122,8 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase { extractor.close(); } - public void testInlineStrings() throws Exception { + @Test + public void testInlineStrings() throws Exception { XSSFEventBasedExcelExtractor extractor = getExtractor("InlineStrings.xlsx"); extractor.setFormulasNotResults(true); 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")); extractor.close(); - } + } /** * Test that we return pretty much the same as * ExcelExtractor does, when we're both passed * 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 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(); } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractorUsingFactory.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractorUsingFactory.java index 34478a909b..09fca4d855 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractorUsingFactory.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractorUsingFactory.java @@ -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); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java index 28980da911..8d26db2927 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java @@ -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); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java index 674eb9d45b..16959b4c96 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java @@ -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(); } } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java index fd237fb6da..a603ab276c 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -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 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(); } } diff --git a/src/scratchpad/testcases/org/apache/poi/hdgf/extractor/TestVisioExtractor.java b/src/scratchpad/testcases/org/apache/poi/hdgf/extractor/TestVisioExtractor.java index 48cc8a9166..69f61e29fb 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdgf/extractor/TestVisioExtractor.java +++ b/src/scratchpad/testcases/org/apache/poi/hdgf/extractor/TestVisioExtractor.java @@ -17,69 +17,73 @@ 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; - - extractor = new VisioTextExtractor(_dgTests.openResourceAsStream(defFilename)); - assertNotNull(extractor); - assertNotNull(extractor.getAllText()); - assertEquals(defTextChunks, extractor.getAllText().length); - - extractor = new VisioTextExtractor( - new POIFSFileSystem( - _dgTests.openResourceAsStream(defFilename) - ) - ); - assertNotNull(extractor); - assertNotNull(extractor.getAllText()); - assertEquals(defTextChunks, extractor.getAllText().length); - - extractor = new VisioTextExtractor( - new HDGFDiagram( - new POIFSFileSystem( - _dgTests.openResourceAsStream(defFilename) - ) - ) - ); - assertNotNull(extractor); - assertNotNull(extractor.getAllText()); - assertEquals(defTextChunks, extractor.getAllText().length); + @Test + public void testCreation() throws IOException { + VisioTextExtractor extractor1 = openExtractor(defFilename); + assertNotNull(extractor1); + assertNotNull(extractor1.getAllText()); + assertEquals(defTextChunks, extractor1.getAllText().length); + extractor1.close(); + + 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(); + + InputStream is3 = _dgTests.openResourceAsStream(defFilename); + POIFSFileSystem poifs3 = new POIFSFileSystem(is3); + is3.close(); + HDGFDiagram hdgf3 = new HDGFDiagram(poifs3); + + + 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(); assertNotNull(text); assertEquals(defTextChunks, text.length); - assertEquals("text\n", text[0]); - assertEquals("View\n", text[1]); + assertEquals("text\n", text[0]); + assertEquals("View\n", text[1]); assertEquals("Test View\n", text[2]); assertEquals("I am a test view\n", text[3]); 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 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(); + } + } } diff --git a/src/scratchpad/testcases/org/apache/poi/hpbf/TestHPBFDocument.java b/src/scratchpad/testcases/org/apache/poi/hpbf/TestHPBFDocument.java index f0a08a4039..ff290d711d 100644 --- a/src/scratchpad/testcases/org/apache/poi/hpbf/TestHPBFDocument.java +++ b/src/scratchpad/testcases/org/apache/poi/hpbf/TestHPBFDocument.java @@ -17,24 +17,32 @@ package org.apache.poi.hpbf; -import junit.framework.TestCase; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.InputStream; + import org.apache.poi.POIDataSamples; +import org.junit.Test; -public final class TestHPBFDocument extends TestCase { +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 diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestOleEmbedding.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestOleEmbedding.java index e3c739001b..d8c331b7fd 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestOleEmbedding.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestOleEmbedding.java @@ -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(); } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java b/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java index 0fed1e8337..eb621cb9b2 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java @@ -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(); - } + + // Build splat'd out text version + 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]); + 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); - - 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()); - - doc = new HWPFDocument(dirB); - extractor3 = new WordExtractor(doc); - - 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()); + HWPFDocument docA = new HWPFDocument(dirA); + WordExtractor extractorA = new WordExtractor(docA); + + 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()); + + HWPFDocument docB = new HWPFDocument(dirB); + WordExtractor extractorB = new WordExtractor(docB); + + 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(); } + @Test(expected=OldWordFileFormatException.class) + public void testWord95_WordExtractor() throws Exception { + // Too old for the default + openExtractor("Word95.doc").close(); + } + + @Test public void testWord95() throws Exception { - // Too old for the default - try { - extractor = new WordExtractor( - POIDataSamples.getDocumentInstance().openResourceAsStream("Word95.doc") - ); - fail(); - } catch(OldWordFileFormatException e) {} - // 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) {} - - Word6Extractor w6e = new Word6Extractor( - POIDataSamples.getDocumentInstance().openResourceAsStream("Word6.doc") - ); + openExtractor("Word6.doc").close(); + } + + @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(); } + } } diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestHPSFBugs.java b/src/testcases/org/apache/poi/hpsf/basic/TestHPSFBugs.java index cd58e159f4..0600ffea9b 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestHPSFBugs.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestHPSFBugs.java @@ -17,148 +17,157 @@ 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(); - + /** * Ensure that we can create a new HSSF Workbook, * 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(); - - assertNotNull(wb.getDocumentSummaryInformation()); - assertNotNull(wb.getSummaryInformation()); - + wb1.createInformationProperties(); + + 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)); - - wb.getDocumentSummaryInformation().setCompany("Apache"); - - + wb1.getSummaryInformation().setAuthor("Apache POI"); + wb1.getSummaryInformation().setKeywords("Testing POI"); + wb1.getSummaryInformation().setCreateDateTime(new Date(12345)); + + 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()); - - assertEquals("Apache POI", wb.getSummaryInformation().getAuthor()); - assertEquals("Testing POI", wb.getSummaryInformation().getKeywords()); - assertEquals(12345, wb.getSummaryInformation().getCreateDateTime().getTime()); - assertEquals("Apache", wb.getDocumentSummaryInformation().getCompany()); - - + assertNotNull(wb2.getDocumentSummaryInformation()); + assertNotNull(wb2.getSummaryInformation()); + + 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"); - - baos = new ByteArrayOutputStream(); - wb.write(baos); - bais = new ByteArrayInputStream(baos.toByteArray()); - wb = new HSSFWorkbook(bais); - + wb2.getSummaryInformation().setComments("Resaved"); + + HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); + wb2.close(); + // Check again - assertNotNull(wb.getDocumentSummaryInformation()); - assertNotNull(wb.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()); + assertNotNull(wb3.getDocumentSummaryInformation()); + assertNotNull(wb3.getSummaryInformation()); + + 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(); } - + /** * Some files seem to want the length and data to be on a 4-byte boundary, * 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")); - - 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); - + @Test + public void test54233() throws IOException, NoPropertySetStreamException, MarkUnsupportedException { + InputStream is = _samples.openResourceAsStream("TestNon4ByteBoundary.doc"); + NPOIFSFileSystem fs = new NPOIFSFileSystem(is); + is.close(); + + 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()); assertEquals("", si.getTitle()); assertEquals("", si.getAuthor()); assertEquals("Cour de Justice", dsi.getCompany()); - - + + // Write out and read back, should still be valid POIDocument doc = new HPSFPropertiesOnlyDocument(fs); ByteArrayOutputStream baos = new ByteArrayOutputStream(); doc.write(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); doc = new HPSFPropertiesOnlyDocument(new NPOIFSFileSystem(bais)); - + // Check properties are still there assertEquals("Microsoft Word 10.0", si.getApplicationName()); 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")); - - 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); - + @Test + public void test56138() throws IOException, NoPropertySetStreamException { + InputStream is = _samples.openResourceAsStream("TestZeroLengthCodePage.mpp"); + NPOIFSFileSystem fs = new NPOIFSFileSystem(is); + is.close(); + + 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()); assertEquals("project1", si.getTitle()); assertEquals("Jon Iles", si.getAuthor()); - + assertEquals("", dsi.getCompany()); assertEquals(2, dsi.getSectionCount()); + + fs.close(); } } diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java index 4218367d27..7404b240e9 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java @@ -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(); } }