diff options
author | Yegor Kozlov <yegor@apache.org> | 2009-08-19 18:51:44 +0000 |
---|---|---|
committer | Yegor Kozlov <yegor@apache.org> | 2009-08-19 18:51:44 +0000 |
commit | 8b283b14b10c01f6b7247555ac0098258d7f0164 (patch) | |
tree | c5186589a87b7e5c49b3394a6d750a8c09e1b686 /src/ooxml/testcases | |
parent | bb7b42c1265efe711303c4f38b25bc0a1eb92247 (diff) | |
download | poi-8b283b14b10c01f6b7247555ac0098258d7f0164.tar.gz poi-8b283b14b10c01f6b7247555ac0098258d7f0164.zip |
Centralize logic for finding/opening sample files
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@805928 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/testcases')
15 files changed, 132 insertions, 273 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/TestDetectAsOOXML.java b/src/ooxml/testcases/org/apache/poi/TestDetectAsOOXML.java index dca593c3c7..2461f463dd 100644 --- a/src/ooxml/testcases/org/apache/poi/TestDetectAsOOXML.java +++ b/src/ooxml/testcases/org/apache/poi/TestDetectAsOOXML.java @@ -22,23 +22,18 @@ package org.apache.poi; import junit.framework.TestCase; import java.io.*; +import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.openxml4j.opc.OPCPackage; + /** * Class to test that HXF correctly detects OOXML * documents */ public class TestDetectAsOOXML extends TestCase { - public String dirname; - - public void setUp() { - dirname = System.getProperty("HSSF.testdata.path"); - } - public void testOpensProperly() throws Exception { - File f = new File(dirname + "/sample.xlsx"); - - POIXMLDocument.openPackage(f.toString()); + OPCPackage.open(HSSFTestDataSamples.openSampleFileStream("sample.xlsx")); } public void testDetectAsPOIFS() throws Exception { @@ -46,19 +41,19 @@ public class TestDetectAsOOXML extends TestCase // ooxml file is in = new PushbackInputStream( - new FileInputStream(dirname + "/SampleSS.xlsx"), 10 + HSSFTestDataSamples.openSampleFileStream("SampleSS.xlsx"), 10 ); assertTrue(POIXMLDocument.hasOOXMLHeader(in)); // xls file isn't in = new PushbackInputStream( - new FileInputStream(dirname + "/SampleSS.xls"), 10 + HSSFTestDataSamples.openSampleFileStream("SampleSS.xls"), 10 ); assertFalse(POIXMLDocument.hasOOXMLHeader(in)); // text file isn't in = new PushbackInputStream( - new FileInputStream(dirname + "/SampleSS.txt"), 10 + HSSFTestDataSamples.openSampleFileStream("SampleSS.txt"), 10 ); assertFalse(POIXMLDocument.hasOOXMLHeader(in)); } diff --git a/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java b/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java index 7d0128618b..dd9eeeb939 100755 --- a/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java +++ b/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java @@ -175,7 +175,7 @@ public final class TestPOIXMLProperties extends TestCase { public void testGetSetRevision() { String revision = _coreProperties.getRevision(); - assertTrue("Revision number is 1", new Integer(_coreProperties.getRevision()).intValue() > 1); + assertTrue("Revision number is 1", new Integer(revision)> 1); _coreProperties.setRevision("20"); assertEquals("20", _coreProperties.getRevision()); _coreProperties.setRevision("20xx"); diff --git a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java index 0cebd76410..6858e93acb 100644 --- a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java +++ b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java @@ -17,10 +17,8 @@ package org.apache.poi.ss; -import java.io.File; -import java.io.FileInputStream; - import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; @@ -30,26 +28,14 @@ import org.apache.poi.openxml4j.opc.OPCPackage; import junit.framework.TestCase; public final class TestWorkbookFactory extends TestCase { - private File xls; - private File xlsx; - private File txt; + private String xls; + private String xlsx; + private String txt; protected void setUp() { - xls = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "SampleSS.xls" - ); - xlsx = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "SampleSS.xlsx" - ); - txt = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "SampleSS.txt" - ); - assertTrue(xls.exists()); - assertTrue(xlsx.exists()); - assertTrue(txt.exists()); + xls = "SampleSS.xls"; + xlsx = "SampleSS.xlsx"; + txt = "SampleSS.txt"; } public void testCreateNative() throws Exception { @@ -57,14 +43,15 @@ public final class TestWorkbookFactory extends TestCase { // POIFS -> hssf wb = WorkbookFactory.create( - new POIFSFileSystem(new FileInputStream(xls)) + new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream(xls)) ); assertNotNull(wb); assertTrue(wb instanceof HSSFWorkbook); // Package -> xssf wb = WorkbookFactory.create( - OPCPackage.open(xlsx.toString()) + OPCPackage.open( + HSSFTestDataSamples.openSampleFileStream(xlsx)) ); assertNotNull(wb); assertTrue(wb instanceof XSSFWorkbook); @@ -80,20 +67,20 @@ public final class TestWorkbookFactory extends TestCase { // InputStream -> either wb = WorkbookFactory.create( - new FileInputStream(xls) + HSSFTestDataSamples.openSampleFileStream(xls) ); assertNotNull(wb); assertTrue(wb instanceof HSSFWorkbook); wb = WorkbookFactory.create( - new FileInputStream(xlsx) + HSSFTestDataSamples.openSampleFileStream(xlsx) ); assertNotNull(wb); assertTrue(wb instanceof XSSFWorkbook); try { wb = WorkbookFactory.create( - new FileInputStream(txt) + HSSFTestDataSamples.openSampleFileStream(txt) ); fail(); } catch(IllegalArgumentException e) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java b/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java index 95b8d71b14..94df0da632 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java @@ -38,6 +38,14 @@ import org.apache.poi.util.TempFile; * @author Josh Micich */ public class XSSFTestDataSamples { + + public static InputStream openSampleFileStream(String sampleFileName) { + return HSSFTestDataSamples.openSampleFileStream(sampleFileName); + } + public static byte[] getTestDataFileContent(String fileName) { + return HSSFTestDataSamples.getTestDataFileContent(fileName); + } + public static final XSSFWorkbook openSampleWorkbook(String sampleName) { InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleName); try { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java index f02cfb4e47..7e6281e757 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java @@ -25,31 +25,16 @@ import junit.framework.TestCase; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.openxml4j.opc.OPCPackage; /** * Tests for {@link XSSFReader} */ public final class TestXSSFReader extends TestCase { - private String dirName; - - @Override - protected void setUp() { - - dirName = System.getProperty("HSSF.testdata.path"); - assertNotNull(dirName); - assertTrue( (new File(dirName)).exists() ); - - // Use system out logger - System.setProperty( - "org.apache.poi.util.POILogger", - "org.apache.poi.util.SystemOutLogger" - ); - } public void testGetBits() throws Exception { - File f = new File(dirName, "SampleSS.xlsx"); - OPCPackage pkg = OPCPackage.open(f.toString()); + OPCPackage pkg = OPCPackage.open(XSSFTestDataSamples.openSampleFileStream("SampleSS.xlsx")); XSSFReader r = new XSSFReader(pkg); @@ -62,8 +47,7 @@ public final class TestXSSFReader extends TestCase { } public void testStyles() throws Exception { - File f = new File(dirName, "SampleSS.xlsx"); - OPCPackage pkg = OPCPackage.open(f.toString()); + OPCPackage pkg = OPCPackage.open(XSSFTestDataSamples.openSampleFileStream("SampleSS.xlsx")); XSSFReader r = new XSSFReader(pkg); @@ -72,8 +56,7 @@ public final class TestXSSFReader extends TestCase { } public void testStrings() throws Exception { - File f = new File(dirName, "SampleSS.xlsx"); - OPCPackage pkg = OPCPackage.open(f.toString()); + OPCPackage pkg = OPCPackage.open(XSSFTestDataSamples.openSampleFileStream("SampleSS.xlsx")); XSSFReader r = new XSSFReader(pkg); @@ -82,8 +65,7 @@ public final class TestXSSFReader extends TestCase { } public void testSheets() throws Exception { - File f = new File(dirName, "SampleSS.xlsx"); - OPCPackage pkg = OPCPackage.open(f.toString()); + OPCPackage pkg = OPCPackage.open(XSSFTestDataSamples.openSampleFileStream("SampleSS.xlsx")); XSSFReader r = new XSSFReader(pkg); byte[] data = new byte[4096]; @@ -115,8 +97,7 @@ public final class TestXSSFReader extends TestCase { * (as they are defined in the workbook.xml) */ public void testOrderOfSheets() throws Exception { - File f = new File(dirName, "reordered_sheets.xlsx"); - OPCPackage pkg = OPCPackage.open(f.toString()); + OPCPackage pkg = OPCPackage.open(XSSFTestDataSamples.openSampleFileStream("reordered_sheets.xlsx")); XSSFReader r = new XSSFReader(pkg); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java index 07eaa4b8fc..14dde090e2 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java @@ -19,7 +19,6 @@ package org.apache.poi.xssf.model; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.File; import java.util.List; import org.apache.poi.ss.usermodel.Cell; @@ -120,14 +119,7 @@ public class TestCommentsTable extends TestCase { } public void testDontLoostNewLines() throws Exception { - File xml = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "WithVariousData.xlsx" - ); - assertTrue(xml.exists()); - - OPCPackage pkg = OPCPackage.open(xml.toString()); - XSSFWorkbook wb = new XSSFWorkbook(pkg); + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx"); List<POIXMLDocumentPart> rels = wb.getSheetAt(0).getRelations(); CommentsTable ct = null; for(POIXMLDocumentPart p : rels) { @@ -171,13 +163,7 @@ public class TestCommentsTable extends TestCase { } public void testExisting() throws Exception { - File xml = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "WithVariousData.xlsx" - ); - assertTrue(xml.exists()); - - XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx"); Sheet sheet1 = workbook.getSheetAt(0); Sheet sheet2 = workbook.getSheetAt(1); @@ -207,13 +193,7 @@ public class TestCommentsTable extends TestCase { } public void testWriteRead() throws Exception { - File xml = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "WithVariousData.xlsx" - ); - assertTrue(xml.exists()); - - XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx"); XSSFSheet sheet1 = workbook.getSheetAt(0); XSSFSheet sheet2 = workbook.getSheetAt(1); @@ -260,13 +240,7 @@ public class TestCommentsTable extends TestCase { } public void testReadWriteMultipleAuthors() throws Exception { - File xml = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "WithMoreVariousData.xlsx" - ); - assertTrue(xml.exists()); - - XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx"); XSSFSheet sheet1 = workbook.getSheetAt(0); XSSFSheet sheet2 = workbook.getSheetAt(1); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java index 46b0fd802c..7adfdd0f5c 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java @@ -17,8 +17,6 @@ package org.apache.poi.xssf.model; -import java.io.File; - import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.XSSFTestDataSamples; @@ -26,15 +24,7 @@ import org.apache.poi.xssf.XSSFTestDataSamples; import junit.framework.TestCase; public final class TestStylesTable extends TestCase { - private File xml; - - protected void setUp() { - xml = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "Formatting.xlsx" - ); - assertTrue(xml.exists()); - } + private String testFile = "Formatting.xlsx"; public void testCreateNew() { StylesTable st = new StylesTable(); @@ -64,7 +54,7 @@ public final class TestStylesTable extends TestCase { } public void testLoadExisting() throws Exception { - XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile); assertNotNull(workbook.getStylesSource()); StylesTable st = workbook.getStylesSource(); @@ -72,7 +62,7 @@ public final class TestStylesTable extends TestCase { doTestExisting(st); } public void testLoadSaveLoad() throws Exception { - XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile); assertNotNull(workbook.getStylesSource()); StylesTable st = workbook.getStylesSource(); @@ -136,7 +126,7 @@ public final class TestStylesTable extends TestCase { } public void testPopulateExisting() throws Exception { - XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile); assertNotNull(workbook.getStylesSource()); StylesTable st = workbook.getStylesSource(); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java index 0a7b08347e..e6d6e97952 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java @@ -39,13 +39,7 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink { } public void testLoadExisting() throws Exception { - File xml = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "WithMoreVariousData.xlsx" - ); - assertTrue(xml.exists()); - - XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx"); assertEquals(3, workbook.getNumberOfSheets()); XSSFSheet sheet = workbook.getSheetAt(0); @@ -56,13 +50,7 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink { } public void testLoadSave() throws Exception { - File xml = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "WithMoreVariousData.xlsx" - ); - assertTrue(xml.exists()); - - XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx"); CreationHelper createHelper = workbook.getCreationHelper(); assertEquals(3, workbook.getNumberOfSheets()); XSSFSheet sheet = workbook.getSheetAt(0); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index bebfe30826..fa3d42d387 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -17,8 +17,6 @@ package org.apache.poi.xssf.usermodel; -import java.io.File; - import org.apache.poi.ss.usermodel.BaseTestSheet; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Workbook; @@ -54,13 +52,7 @@ public class TestXSSFSheet extends BaseTestSheet { } public void testExistingHeaderFooter() throws Exception { - File xml = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "45540_classic_Header.xlsx" - ); - assertTrue(xml.exists()); - - XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("45540_classic_Header.xlsx"); XSSFOddHeader hdr; XSSFOddFooter ftr; diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java b/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java index e31e42f753..f55e7f5081 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java @@ -29,27 +29,10 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFRelation; public final class TestXWPFDocument extends TestCase { - private File sampleFile; - private File complexFile; - - protected void setUp() throws Exception { - super.setUp(); - - sampleFile = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "sample.docx" - ); - complexFile = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "IllustrativeCases.docx" - ); - - assertTrue(sampleFile.exists()); - assertTrue(complexFile.exists()); - } public void testContainsMainContentType() throws Exception { - OPCPackage pack = POIXMLDocument.openPackage(sampleFile.toString()); + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); + OPCPackage pack = doc.getPackage(); boolean found = false; for(PackagePart part : pack.getParts()) { @@ -62,40 +45,24 @@ public final class TestXWPFDocument extends TestCase { } public void testOpen() throws Exception { - POIXMLDocument.openPackage(sampleFile.toString()); - POIXMLDocument.openPackage(complexFile.toString()); - - new XWPFDocument( - POIXMLDocument.openPackage(sampleFile.toString()) - ); - new XWPFDocument( - POIXMLDocument.openPackage(complexFile.toString()) - ); - XWPFDocument xml; // Simple file - xml = new XWPFDocument( - POIXMLDocument.openPackage(sampleFile.toString()) - ); + xml = XWPFTestDataSamples.openSampleDocument("sample.docx"); // Check it has key parts assertNotNull(xml.getDocument()); assertNotNull(xml.getDocument().getBody()); assertNotNull(xml.getStyle()); // Complex file - xml = new XWPFDocument( - POIXMLDocument.openPackage(complexFile.toString()) - ); + xml = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); assertNotNull(xml.getDocument()); assertNotNull(xml.getDocument().getBody()); assertNotNull(xml.getStyle()); } public void testMetadataBasics() throws Exception { - XWPFDocument xml = new XWPFDocument( - POIXMLDocument.openPackage(sampleFile.toString()) - ); + XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("sample.docx"); assertNotNull(xml.getProperties().getCoreProperties()); assertNotNull(xml.getProperties().getExtendedProperties()); @@ -108,9 +75,7 @@ public final class TestXWPFDocument extends TestCase { } public void testMetadataComplex() throws Exception { - XWPFDocument xml = new XWPFDocument( - POIXMLDocument.openPackage(complexFile.toString()) - ); + XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); assertNotNull(xml.getProperties().getCoreProperties()); assertNotNull(xml.getProperties().getExtendedProperties()); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java b/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java new file mode 100755 index 0000000000..3255d62f10 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java @@ -0,0 +1,54 @@ +/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xwpf;
+
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.apache.poi.hwpf.HWPFTestDataSamples;
+import org.apache.poi.POIDataSamples;
+
+import java.io.*;
+
+/**
+ * @author Yegor Kozlov
+ */
+public class XWPFTestDataSamples {
+
+ public static XWPFDocument openSampleDocument(String sampleName) {
+ InputStream is = HWPFTestDataSamples.openSampleFileStream(sampleName);
+ try {
+ return new XWPFDocument(is);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static XWPFDocument writeOutAndReadBack(XWPFDocument doc) {
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
+ doc.write(baos);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ return new XWPFDocument(bais);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static POIDataSamples getInstance(){
+ return HWPFTestDataSamples.getInstance();
+ }
+
+}
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java b/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java index fe80baff10..cffdaec9ab 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java @@ -21,6 +21,7 @@ import java.io.IOException; import org.apache.poi.POIXMLDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.XWPFTestDataSamples; import junit.framework.TestCase; @@ -33,7 +34,7 @@ public class TestXWPFWordExtractor extends TestCase { * Get text out of the simple file */ public void testGetSimpleText() throws Exception { - XWPFDocument doc = open("sample.docx"); + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); String text = extractor.getText(); @@ -62,7 +63,7 @@ public class TestXWPFWordExtractor extends TestCase { * Tests getting the text out of a complex file */ public void testGetComplexText() throws Exception { - XWPFDocument doc = open("IllustrativeCases.docx"); + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); String text = extractor.getText(); @@ -94,7 +95,7 @@ public class TestXWPFWordExtractor extends TestCase { } public void testGetWithHyperlinks() throws Exception { - XWPFDocument doc = open("TestDocument.docx"); + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); // Now check contents @@ -119,7 +120,7 @@ public class TestXWPFWordExtractor extends TestCase { } public void testHeadersFooters() throws Exception { - XWPFDocument doc = open("ThreeColHeadFoot.docx"); + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("ThreeColHeadFoot.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); assertEquals( @@ -138,7 +139,7 @@ public class TestXWPFWordExtractor extends TestCase { // Now another file, expect multiple headers // and multiple footers - doc = open("DiffFirstPageHeadFoot.docx"); + doc = XWPFTestDataSamples.openSampleDocument("DiffFirstPageHeadFoot.docx"); extractor = new XWPFWordExtractor(doc); extractor = new XWPFWordExtractor(doc); @@ -162,7 +163,7 @@ public class TestXWPFWordExtractor extends TestCase { } public void testFootnotes() throws Exception { - XWPFDocument doc = open("footnotes.docx"); + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("footnotes.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); assertTrue(extractor.getText().contains("snoska")); @@ -170,14 +171,14 @@ public class TestXWPFWordExtractor extends TestCase { public void testTableFootnotes() throws Exception { - XWPFDocument doc = open("table_footnotes.docx"); + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("table_footnotes.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); assertTrue(extractor.getText().contains("snoska")); } public void testFormFootnotes() throws Exception { - XWPFDocument doc = open("form_footnotes.docx"); + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("form_footnotes.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); String text = extractor.getText(); @@ -186,33 +187,18 @@ public class TestXWPFWordExtractor extends TestCase { } public void testEndnotes() throws Exception { - XWPFDocument doc = open("endnotes.docx"); + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("endnotes.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); assertTrue(extractor.getText().contains("XXX")); } public void testInsertedDeletedText() throws Exception { - XWPFDocument doc = open("delins.docx"); + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("delins.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); assertTrue(extractor.getText().contains("pendant worn")); assertTrue(extractor.getText().contains("extremely well")); } - //TODO use the same logic for opening test files as in HSSFTestDataSamples - private XWPFDocument open(String sampleFileName) throws IOException { - File file = new File( - System.getProperty("HWPF.testdata.path"), sampleFileName); - - try { - if(!sampleFileName.equals(file.getCanonicalFile().getName())){ - throw new RuntimeException("File name is case-sensitive: requested '" + sampleFileName - + "' but actual file is '" + file.getCanonicalFile().getName() + "'"); - } - } catch (IOException e){ - throw new RuntimeException(e); - } - return new XWPFDocument(POIXMLDocument.openPackage(file.getPath())); - } } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java b/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java index ef5d5868a0..13c04b0193 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java @@ -20,6 +20,7 @@ import java.io.File; import org.apache.poi.POIXMLDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.XWPFTestDataSamples; import junit.framework.TestCase; @@ -36,49 +37,13 @@ public class TestXWPFHeaderFooterPolicy extends TestCase { protected void setUp() throws Exception { super.setUp(); - File file; - file = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "NoHeadFoot.docx" - ); - assertTrue(file.exists()); - noHeader = new XWPFDocument(POIXMLDocument.openPackage(file.toString())); - - file = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "ThreeColHead.docx" - ); - assertTrue(file.exists()); - header = new XWPFDocument(POIXMLDocument.openPackage(file.toString())); - - file = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "SimpleHeadThreeColFoot.docx" - ); - assertTrue(file.exists()); - headerFooter = new XWPFDocument(POIXMLDocument.openPackage(file.toString())); - - file = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "FancyFoot.docx" - ); - assertTrue(file.exists()); - footer = new XWPFDocument(POIXMLDocument.openPackage(file.toString())); - - file = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "PageSpecificHeadFoot.docx" - ); - assertTrue(file.exists()); - oddEven = new XWPFDocument(POIXMLDocument.openPackage(file.toString())); - - file = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "DiffFirstPageHeadFoot.docx" - ); - assertTrue(file.exists()); - diffFirst = new XWPFDocument(POIXMLDocument.openPackage(file.toString())); + noHeader = XWPFTestDataSamples.openSampleDocument("NoHeadFoot.docx"); + header = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx"); + headerFooter = XWPFTestDataSamples.openSampleDocument("SimpleHeadThreeColFoot.docx"); + footer = XWPFTestDataSamples.openSampleDocument("FancyFoot.docx"); + oddEven = XWPFTestDataSamples.openSampleDocument("PageSpecificHeadFoot.docx"); + diffFirst = XWPFTestDataSamples.openSampleDocument("DiffFirstPageHeadFoot.docx"); } public void testPolicy() { diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java index 84a655ed65..a893ab1183 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java @@ -23,6 +23,7 @@ import junit.framework.TestCase; import org.apache.poi.POIXMLDocument; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; +import org.apache.poi.xwpf.XWPFTestDataSamples; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; @@ -31,16 +32,8 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; public class TestXWPFHeader extends TestCase { public void testSimpleHeader() throws IOException { - File sampleFile = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "headerFooter.docx" - ); - assertTrue(sampleFile.exists()); - XWPFDocument sampleDoc; - sampleDoc = new XWPFDocument( - POIXMLDocument.openPackage(sampleFile.toString()) - ); - + XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerFooter.docx"); + XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); @@ -55,15 +48,7 @@ public class TestXWPFHeader extends TestCase { } public void testSetHeader() throws IOException { - File sampleFile = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "SampleDoc.docx" - ); - assertTrue(sampleFile.exists()); - XWPFDocument sampleDoc; - sampleDoc = new XWPFDocument( - POIXMLDocument.openPackage(sampleFile.toString()) - ); + XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx"); // no header is set (yet) XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); assertNull(policy.getDefaultHeader()); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java index f02135afac..12405fb4a7 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java @@ -22,6 +22,7 @@ import java.math.BigInteger; import junit.framework.TestCase; import org.apache.poi.POIXMLDocument; +import org.apache.poi.xwpf.XWPFTestDataSamples; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTInd; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc; @@ -41,26 +42,13 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTextAlignment; * Tests for XWPF Paragraphs */ public final class TestXWPFParagraph extends TestCase { - /** - * A simple file - */ - private XWPFDocument xml; - - protected void setUp() throws Exception { - super.setUp(); - - File file = new File( - System.getProperty("HWPF.testdata.path") + - File.separator + "ThreeColHead.docx" - ); - assertTrue(file.exists()); - xml = new XWPFDocument(POIXMLDocument.openPackage(file.toString())); - } /** * Check that we get the right paragraph from the header */ public void disabled_testHeaderParagraph() { + XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx"); + XWPFHeader hdr = xml.getHeaderFooterPolicy().getDefaultHeader(); assertNotNull(hdr); @@ -77,6 +65,7 @@ public final class TestXWPFParagraph extends TestCase { * Check that we get the right paragraphs from the document */ public void disabled_testDocumentParagraph() { + XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx"); XWPFParagraph[] ps = xml.getParagraphs(); assertEquals(10, ps.length); |