diff options
Diffstat (limited to 'src/ooxml/testcases/org/apache/poi/xwpf')
14 files changed, 323 insertions, 203 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java b/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java index b0d9513be6..b1a9983f75 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java @@ -22,6 +22,7 @@ import junit.framework.TestSuite; import org.apache.poi.xwpf.extractor.TestXWPFWordExtractor; import org.apache.poi.xwpf.model.TestXWPFHeaderFooterPolicy; +import org.apache.poi.xwpf.usermodel.TestXWPFDocument; import org.apache.poi.xwpf.usermodel.TestXWPFHeader; import org.apache.poi.xwpf.usermodel.TestXWPFHeadings; import org.apache.poi.xwpf.usermodel.TestXWPFNumbering; diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/TestAllExtendedProperties.java b/src/ooxml/testcases/org/apache/poi/xwpf/TestAllExtendedProperties.java index 8b7b08b7c9..c04f73c934 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/TestAllExtendedProperties.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/TestAllExtendedProperties.java @@ -17,6 +17,8 @@ package org.apache.poi.xwpf; +import java.io.IOException; + import junit.framework.TestCase; import org.apache.poi.POIXMLProperties.CoreProperties; @@ -41,7 +43,7 @@ import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTVect * */ public final class TestAllExtendedProperties extends TestCase { - public void testGetAllExtendedProperties() { + public void testGetAllExtendedProperties() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestPoiXMLDocumentCorePropertiesGetKeywords.docx"); CTProperties ctProps = doc.getProperties().getExtendedProperties().getUnderlyingProperties(); assertEquals("Microsoft Office Word",ctProps.getApplication()); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/TestPackageCorePropertiesGetKeywords.java b/src/ooxml/testcases/org/apache/poi/xwpf/TestPackageCorePropertiesGetKeywords.java index 5c150bdf1d..4b37623df1 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/TestPackageCorePropertiesGetKeywords.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/TestPackageCorePropertiesGetKeywords.java @@ -17,6 +17,8 @@ package org.apache.poi.xwpf; +import java.io.IOException; + import junit.framework.TestCase; import org.apache.poi.POIXMLProperties.CoreProperties; @@ -37,7 +39,7 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument; * */ public final class TestPackageCorePropertiesGetKeywords extends TestCase { - public void testGetSetKeywords() { + public void testGetSetKeywords() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestPoiXMLDocumentCorePropertiesGetKeywords.docx"); String keywords = doc.getProperties().getCoreProperties().getKeywords(); assertEquals("extractor, test, rdf", keywords); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java b/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java index d88c029cdb..a6028eb49a 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java @@ -22,6 +22,8 @@ import java.io.IOException; import java.io.InputStream;
import org.apache.poi.POIDataSamples;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.PackageHelper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
/**
@@ -29,23 +31,21 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument; */
public class XWPFTestDataSamples {
- public static XWPFDocument openSampleDocument(String sampleName) {
+ public static XWPFDocument openSampleDocument(String sampleName) throws IOException {
InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream(sampleName);
- try {
- return new XWPFDocument(is);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
+ return new XWPFDocument(is);
}
- 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 XWPFDocument writeOutAndReadBack(XWPFDocument doc) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
+ doc.write(baos);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ return new XWPFDocument(bais);
+ }
+
+ public static byte[] getImage(String filename) throws IOException {
+ InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream(filename);
+ byte[] result = IOUtils.toByteArray(is);
+ return result;
}
}
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 d1cc2eea53..57a67eab6b 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java @@ -17,6 +17,8 @@ package org.apache.poi.xwpf.extractor; +import java.io.IOException; + import junit.framework.TestCase; import org.apache.poi.xwpf.XWPFTestDataSamples; @@ -29,8 +31,9 @@ public class TestXWPFWordExtractor extends TestCase { /** * Get text out of the simple file + * @throws IOException */ - public void testGetSimpleText() { + public void testGetSimpleText() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); @@ -58,8 +61,9 @@ public class TestXWPFWordExtractor extends TestCase { /** * Tests getting the text out of a complex file + * @throws IOException */ - public void testGetComplexText() { + public void testGetComplexText() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); @@ -91,7 +95,7 @@ public class TestXWPFWordExtractor extends TestCase { assertEquals(134, ps); } - public void testGetWithHyperlinks() { + public void testGetWithHyperlinks() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); @@ -116,7 +120,7 @@ public class TestXWPFWordExtractor extends TestCase { ); } - public void testHeadersFooters() { + public void testHeadersFooters() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("ThreeColHeadFoot.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); @@ -159,7 +163,7 @@ public class TestXWPFWordExtractor extends TestCase { ); } - public void testFootnotes() { + public void testFootnotes() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("footnotes.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); @@ -167,14 +171,14 @@ public class TestXWPFWordExtractor extends TestCase { } - public void testTableFootnotes() { + public void testTableFootnotes() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("table_footnotes.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); assertTrue(extractor.getText().contains("snoska")); } - public void testFormFootnotes() { + public void testFormFootnotes() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("form_footnotes.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); @@ -183,14 +187,14 @@ public class TestXWPFWordExtractor extends TestCase { assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase")); } - public void testEndnotes() { + public void testEndnotes() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("endnotes.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); assertTrue(extractor.getText().contains("XXX")); } - public void testInsertedDeletedText() { + public void testInsertedDeletedText() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("delins.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); @@ -198,7 +202,7 @@ public class TestXWPFWordExtractor extends TestCase { assertTrue(extractor.getText().contains("extremely well")); } - public void testParagraphHeader() { + public void testParagraphHeader() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Headers.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); @@ -210,8 +214,9 @@ public class TestXWPFWordExtractor extends TestCase { /** * Test that we can open and process .docm * (macro enabled) docx files (bug #45690) + * @throws IOException */ - public void testDOCMFiles() { + public void testDOCMFiles() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("45690.docm"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); @@ -224,8 +229,9 @@ public class TestXWPFWordExtractor extends TestCase { * Test that we handle things like tabs and * carriage returns properly in the text that * we're extracting (bug #49189) + * @throws IOException */ - public void testDocTabs() { + public void testDocTabs() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("WithTabs.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); @@ -241,8 +247,9 @@ public class TestXWPFWordExtractor extends TestCase { /** * The output should not contain field codes, e.g. those specified in the * w:instrText tag (spec sec. 17.16.23) + * @throws IOException */ - public void testNoFieldCodes() { + public void testNoFieldCodes() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("FieldCodes.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); String text = extractor.getText(); @@ -254,8 +261,9 @@ public class TestXWPFWordExtractor extends TestCase { /** * The output should contain the values of simple fields, those specified * with the fldSimple element (spec sec. 17.16.19) + * @throws IOException */ - public void testFldSimpleContent() { + public void testFldSimpleContent() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("FldSimple.docx"); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); String text = extractor.getText(); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFDecorators.java b/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFDecorators.java index 3f51403898..7c96a94660 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFDecorators.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFDecorators.java @@ -17,6 +17,8 @@ package org.apache.poi.xwpf.model; +import java.io.IOException; + import junit.framework.TestCase; import org.apache.poi.xwpf.XWPFTestDataSamples; @@ -32,10 +34,10 @@ public class TestXWPFDecorators extends TestCase { private XWPFDocument hyperlink; private XWPFDocument comments; - protected void setUp() { - simple = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx"); - hyperlink = XWPFTestDataSamples.openSampleDocument("TestDocument.docx"); - comments = XWPFTestDataSamples.openSampleDocument("WordWithAttachments.docx"); + protected void setUp() throws IOException { + simple = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx"); + hyperlink = XWPFTestDataSamples.openSampleDocument("TestDocument.docx"); + comments = XWPFTestDataSamples.openSampleDocument("WordWithAttachments.docx"); } public void testHyperlink() { 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 86b22466bc..7d70866e89 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java @@ -17,6 +17,8 @@ package org.apache.poi.xwpf.model; +import java.io.IOException; + import junit.framework.TestCase; import org.apache.poi.xwpf.XWPFTestDataSamples; @@ -33,9 +35,9 @@ public class TestXWPFHeaderFooterPolicy extends TestCase { private XWPFDocument oddEven; private XWPFDocument diffFirst; - protected void setUp() { + protected void setUp() throws IOException { - noHeader = XWPFTestDataSamples.openSampleDocument("NoHeadFoot.docx"); + noHeader = XWPFTestDataSamples.openSampleDocument("NoHeadFoot.docx"); header = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx"); headerFooter = XWPFTestDataSamples.openSampleDocument("SimpleHeadThreeColFoot.docx"); footer = XWPFTestDataSamples.openSampleDocument("FancyFoot.docx"); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java index 7b6af4ee94..9828af1bb8 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java @@ -15,9 +15,11 @@ limitations under the License. ==================================================================== */ -package org.apache.poi.xwpf; +package org.apache.poi.xwpf.usermodel; -import java.util.ArrayList; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Arrays; import java.util.List; import junit.framework.TestCase; @@ -27,10 +29,11 @@ import org.apache.poi.POIXMLProperties; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; -import org.apache.poi.xwpf.usermodel.XWPFDocument; -import org.apache.poi.xwpf.usermodel.XWPFParagraph; -import org.apache.poi.xwpf.usermodel.XWPFPictureData; -import org.apache.poi.xwpf.usermodel.XWPFRelation; +import org.apache.poi.openxml4j.opc.PackagePartName; +import org.apache.poi.openxml4j.opc.PackageRelationship; +import org.apache.poi.openxml4j.opc.PackagingURIHelper; +import org.apache.poi.openxml4j.opc.TargetMode; +import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.xmlbeans.XmlCursor; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; @@ -70,7 +73,7 @@ public final class TestXWPFDocument extends TestCase { assertNotNull(xml.getStyle()); } - public void testMetadataBasics() { + public void testMetadataBasics() throws IOException { XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("sample.docx"); assertNotNull(xml.getProperties().getCoreProperties()); assertNotNull(xml.getProperties().getExtendedProperties()); @@ -83,7 +86,7 @@ public final class TestXWPFDocument extends TestCase { assertEquals(null, xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().getValue()); } - public void testMetadataComplex() { + public void testMetadataComplex() throws IOException { XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); assertNotNull(xml.getProperties().getCoreProperties()); assertNotNull(xml.getProperties().getExtendedProperties()); @@ -103,7 +106,7 @@ public final class TestXWPFDocument extends TestCase { assertEquals("Apache POI", props.getExtendedProperties().getUnderlyingProperties().getApplication()); } - public void testAddParagraph(){ + public void testAddParagraph() throws IOException{ XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); assertEquals(3, doc.getParagraphs().size()); @@ -122,24 +125,22 @@ public final class TestXWPFDocument extends TestCase { assertSame(cP, doc.getParagraphs().get(0)); assertEquals(5, doc.getParagraphs().size()); } - - public void testAddPicture(){ - XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); - byte[] jpeg = "This is a jpeg".getBytes(); - try { - int jpegNum = doc.addPicture(jpeg, XWPFDocument.PICTURE_TYPE_JPEG); - byte[] newJpeg = doc.getAllPictures().get(jpegNum).getData(); - assertEquals(newJpeg.length, jpeg.length); - for(int i = 0 ; i < jpeg.length; i++){ - assertEquals(newJpeg[i], jpeg[i]); - } - } catch (InvalidFormatException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public void testRemoveBodyElement() { + + 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); + + byte[] newJpeg = ((XWPFPictureData) doc.getRelationById(relationId)).getData(); + assertEquals(newJpeg.length,jpeg.length); + for (int i = 0 ; i < jpeg.length ; i++) + { + assertEquals(newJpeg[i],jpeg[i]); + } + } + + public void testRemoveBodyElement() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); assertEquals(3, doc.getParagraphs().size()); assertEquals(3, doc.getBodyElements().size()); @@ -200,46 +201,119 @@ public final class TestXWPFDocument extends TestCase { assertEquals(p3, doc.getParagraphs().get(0)); } - public void testSettings() throws Exception { - XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("WithGIF.docx"); - assertEquals(120, doc.getZoomPercent()); - assertEquals(false, doc.isEnforcedCommentsProtection()); - assertEquals(false, doc.isEnforcedFillingFormsProtection()); - assertEquals(false, doc.isEnforcedReadonlyProtection()); - assertEquals(false, doc.isEnforcedTrackedChangesProtection()); - - doc.setZoomPercent(124); - - // Only one enforcement allowed, last one wins! - doc.enforceFillingFormsProtection(); - doc.enforceReadonlyProtection(); - - doc = XWPFTestDataSamples.writeOutAndReadBack(doc); - - assertEquals(124, doc.getZoomPercent()); - assertEquals(false, doc.isEnforcedCommentsProtection()); - assertEquals(false, doc.isEnforcedFillingFormsProtection()); - assertEquals(true, doc.isEnforcedReadonlyProtection()); - assertEquals(false, doc.isEnforcedTrackedChangesProtection()); + public void testRegisterPackagePictureData() throws IOException, InvalidFormatException { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx"); + + /* manually assemble a new image package part*/ + OPCPackage opcPckg = doc.getPackage(); + XWPFRelation jpgRelation = XWPFRelation.IMAGE_JPEG; + PackagePartName partName = PackagingURIHelper.createPartName(jpgRelation.getDefaultFileName().replace('#', '2')); + PackagePart newImagePart = opcPckg.createPart(partName, jpgRelation.getContentType()); + byte[] nature1 = XWPFTestDataSamples.getImage("abstract4.jpg"); + OutputStream os = newImagePart.getOutputStream(); + os.write(nature1); + os.close(); + XWPFHeader xwpfHeader = doc.getHeaderList().get(0); + PackageRelationship relationship = xwpfHeader.getPackagePart().addRelationship(partName, TargetMode.INTERNAL, jpgRelation.getRelation()); + XWPFPictureData newPicData = new XWPFPictureData(newImagePart,relationship); + /* new part is now ready to rumble */ + + assertFalse(xwpfHeader.getAllPictures().contains(newPicData)); + assertFalse(doc.getAllPictures().contains(newPicData)); + assertFalse(doc.getAllPackagePictures().contains(newPicData)); + + doc.registerPackagePictureData(newPicData); + + assertFalse(xwpfHeader.getAllPictures().contains(newPicData)); + assertFalse(doc.getAllPictures().contains(newPicData)); + assertTrue(doc.getAllPackagePictures().contains(newPicData)); + + doc.getPackage().revert(); + } + + public void testFindPackagePictureData() throws IOException { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx"); + byte[] nature1 = XWPFTestDataSamples.getImage("nature1.gif"); + XWPFPictureData part = doc.findPackagePictureData(nature1, Document.PICTURE_TYPE_GIF); + assertNotNull(part); + assertTrue(doc.getAllPictures().contains(part)); + assertTrue(doc.getAllPackagePictures().contains(part)); + doc.getPackage().revert(); } - public void testGIFSupport() throws Exception { - XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("WithGIF.docx"); - ArrayList<PackagePart> gifParts = doc.getPackage().getPartsByContentType(XWPFRelation.IMAGE_GIF.getContentType()); - assertEquals("Expected exactly one GIF part in package.",1,gifParts.size()); - PackagePart gifPart = gifParts.get(0); + public void testGetAllPictures() throws IOException { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx"); + List<XWPFPictureData> allPictures = doc.getAllPictures(); + List<XWPFPictureData> allPackagePictures = doc.getAllPackagePictures(); - List<POIXMLDocumentPart> relations = doc.getRelations(); - POIXMLDocumentPart gifDocPart = null; - for (POIXMLDocumentPart docPart : relations) - { - if (gifPart == docPart.getPackagePart()) - { - assertNull("More than one POIXMLDocumentPart for GIF PackagePart.",gifDocPart); - gifDocPart = docPart; - } + assertNotNull(allPictures); + assertEquals(3,allPictures.size()); + for (XWPFPictureData xwpfPictureData : allPictures) { + assertTrue(allPackagePictures.contains(xwpfPictureData)); + } + + try { + allPictures.add(allPictures.get(0)); + fail("This list must be unmodifiable!"); + } catch (UnsupportedOperationException e) { + // all ok } - assertNotNull("GIF part not related to document.xml PackagePart",gifDocPart); - assertTrue("XWPFRelation for GIF image was not recognized properly, as the POIXMLDocumentPart created was of a wrong type.",XWPFRelation.IMAGE_GIF.getRelationClass().isInstance(gifDocPart)); + + doc.getPackage().revert(); + } + + public void testGetAllPackagePictures() throws IOException { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx"); + List<XWPFPictureData> allPackagePictures = doc.getAllPackagePictures(); + + assertNotNull(allPackagePictures); + assertEquals(5,allPackagePictures.size()); + + try { + allPackagePictures.add(allPackagePictures.get(0)); + fail("This list must be unmodifiable!"); + } catch (UnsupportedOperationException e) { + // all ok + } + + doc.getPackage().revert(); + } + + public void testPictureHandlingSimpleFile() throws IOException, InvalidFormatException { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx"); + assertEquals(1,doc.getAllPackagePictures().size()); + byte[] newPic = XWPFTestDataSamples.getImage("abstract4.jpg"); + String id1 = doc.addPictureData(newPic, Document.PICTURE_TYPE_JPEG); + assertEquals(2,doc.getAllPackagePictures().size()); + /* copy data, to avoid instance-equality */ + byte[] newPicCopy = Arrays.copyOf(newPic, newPic.length); + String id2 = doc.addPictureData(newPicCopy, Document.PICTURE_TYPE_JPEG); + assertEquals(id1,id2); + doc.getPackage().revert(); + } + + public void testPictureHandlingHeaderDocumentImages() throws IOException { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_2.docx"); + assertEquals(1,doc.getAllPictures().size()); + assertEquals(1,doc.getAllPackagePictures().size()); + assertEquals(1,doc.getHeaderList().get(0).getAllPictures().size()); + doc.getPackage().revert(); + } + + public void testPictureHandlingComplex() throws IOException, InvalidFormatException { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx"); + XWPFHeader xwpfHeader = doc.getHeaderList().get(0); + + assertEquals(3,doc.getAllPictures().size()); + assertEquals(3,xwpfHeader.getAllPictures().size()); + assertEquals(5,doc.getAllPackagePictures().size()); + + byte[] nature1 = XWPFTestDataSamples.getImage("nature1.jpg"); + String id = doc.addPictureData(nature1, Document.PICTURE_TYPE_JPEG); + POIXMLDocumentPart part1 = xwpfHeader.getRelationById("rId1"); + XWPFPictureData part2 = (XWPFPictureData) doc.getRelationById(id); + assertSame(part1,part2); + + doc.getPackage().revert(); } } 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 738ca7af13..2a65e8e560 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java @@ -29,7 +29,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; public final class TestXWPFHeader extends TestCase { - public void testSimpleHeader() { + public void testSimpleHeader() throws IOException { XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerFooter.docx"); XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); @@ -40,7 +40,7 @@ public final class TestXWPFHeader extends TestCase { assertNotNull(footer); } - public void testImageInHeader() { + public void testImageInHeader() throws IOException { XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx"); XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); @@ -110,7 +110,7 @@ public final class TestXWPFHeader extends TestCase { // make sure that it contains two paragraphs of text and that // both do hold what is expected. footer = policy.getDefaultFooter(); - + XWPFParagraph[] paras = new XWPFParagraph[footer.getParagraphs().size()]; int i=0; for(XWPFParagraph p : footer.getParagraphs()) { @@ -122,7 +122,7 @@ public final class TestXWPFHeader extends TestCase { assertEquals("Second paragraph for the footer", paras[1].getText()); } - public void testSetWatermark() { + public void testSetWatermark() throws IOException { XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx"); // no header is set (yet) XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); @@ -136,4 +136,20 @@ public final class TestXWPFHeader extends TestCase { assertNotNull(policy.getFirstPageHeader()); assertNotNull(policy.getEvenPageHeader()); } + + public void testAddPictureData() { + + } + + public void testGetAllPictures() { + + } + + public void testGetAllPackagePictures() { + + } + + public void testGetPictureDataById() { + + } } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFNumbering.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFNumbering.java index 7990cd70d6..7ee5637394 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFNumbering.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFNumbering.java @@ -17,6 +17,7 @@ package org.apache.poi.xwpf.usermodel; +import java.io.IOException; import java.math.BigInteger; import junit.framework.TestCase; @@ -25,7 +26,7 @@ import org.apache.poi.xwpf.XWPFTestDataSamples; public class TestXWPFNumbering extends TestCase { - public void testCompareAbstractNum(){ + public void testCompareAbstractNum() throws IOException{ XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx"); XWPFNumbering numbering = doc.getNumbering(); BigInteger numId = BigInteger.valueOf(1); 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 bf1c430f25..cb414168fb 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java @@ -17,6 +17,7 @@ package org.apache.poi.xwpf.usermodel; +import java.io.IOException; import java.math.BigInteger; import java.util.List; @@ -49,8 +50,9 @@ public final class TestXWPFParagraph extends TestCase { /** * Check that we get the right paragraph from the header + * @throws IOException */ - public void disabled_testHeaderParagraph() { + public void disabled_testHeaderParagraph() throws IOException { XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx"); XWPFHeader hdr = xml.getHeaderFooterPolicy().getDefaultHeader(); @@ -67,8 +69,9 @@ public final class TestXWPFParagraph extends TestCase { /** * Check that we get the right paragraphs from the document + * @throws IOException */ - public void disabled_testDocumentParagraph() { + public void disabled_testDocumentParagraph() throws IOException { XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx"); List<XWPFParagraph> ps = xml.getParagraphs(); assertEquals(10, ps.size()); @@ -231,7 +234,7 @@ public final class TestXWPFParagraph extends TestCase { assertEquals(STOnOff.TRUE, ppr.getPageBreakBefore().getVal()); } - public void testBookmarks() { + public void testBookmarks() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bookmarks.docx"); XWPFParagraph paragraph = doc.getParagraphs().get(0); assertEquals("Sample Word Document", paragraph.getText()); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java index c05e6201e1..72aa4c115e 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java @@ -17,6 +17,7 @@ package org.apache.poi.xwpf.usermodel; +import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -29,98 +30,103 @@ import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; public class TestXWPFPictureData extends TestCase { - public void testRead(){ - XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx"); - List<XWPFPictureData> pictures = sampleDoc.getAllPictures(); - assertSame(pictures, sampleDoc.getAllPictures()); - - assertEquals(5, pictures.size()); - String[] ext = {"wmf", "png", "emf", "emf", "jpeg"}; - for (int i = 0; i < pictures.size(); i++) { - assertEquals(ext[i], pictures.get(i).suggestFileExtension()); - } - - int num = pictures.size(); - - byte[] pictureData = {0xA, 0xB, 0XC, 0xD, 0xE, 0xF}; - - int idx; - try { - idx = sampleDoc.addPicture(pictureData, XWPFDocument.PICTURE_TYPE_JPEG); - assertEquals(num + 1, pictures.size()); - //idx is 0-based index in the #pictures array - assertEquals(pictures.size() - 1, idx); - XWPFPictureData pict = pictures.get(idx); - assertEquals("jpeg", pict.suggestFileExtension()); - assertTrue(Arrays.equals(pictureData, pict.getData())); - } catch (InvalidFormatException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public void testPictureInHeader() { - XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx"); - XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); - - XWPFHeader header = policy.getDefaultHeader(); - - List<XWPFPictureData> pictures = header.getAllPictures(); - assertEquals(1, pictures.size()); + + public void testRead() throws InvalidFormatException, IOException + { + XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx"); + List<XWPFPictureData> pictures = sampleDoc.getAllPictures(); + + assertEquals(5,pictures.size()); + String[] ext = {"wmf","png","emf","emf","jpeg"}; + for (int i = 0 ; i < pictures.size() ; i++) + { + assertEquals(ext[i],pictures.get(i).suggestFileExtension()); } - public void testNew() throws Exception { - XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx"); - byte[] jpegData = "test jpeg data".getBytes(); - byte[] wmfData = "test wmf data".getBytes(); - byte[] pngData = "test png data".getBytes(); - - List<XWPFPictureData> pictures = doc.getAllPictures(); - assertEquals(0, pictures.size()); - - // Document shouldn't have any image relationships - assertEquals(13, doc.getPackagePart().getRelationships().size()); - for(PackageRelationship rel : doc.getPackagePart().getRelationships()) { - if(rel.getRelationshipType().equals(XSSFRelation.IMAGE_JPEG.getRelation())) { - fail("Shouldn't have JPEG yet"); - } - } - - // Add the image - int jpegIdx; - - jpegIdx = doc.addPicture(jpegData, XWPFDocument.PICTURE_TYPE_JPEG); - assertEquals(1, pictures.size()); - assertEquals("jpeg", pictures.get(jpegIdx).suggestFileExtension()); - assertTrue(Arrays.equals(jpegData, pictures.get(jpegIdx).getData())); - - // Ensure it now has one - assertEquals(14, doc.getPackagePart().getRelationships().size()); - PackageRelationship jpegRel = null; - for(PackageRelationship rel : doc.getPackagePart().getRelationships()) { - if(rel.getRelationshipType().equals(XWPFRelation.IMAGE_JPEG.getRelation())) { - if(jpegRel != null) + int num = pictures.size(); + + byte[] pictureData = XWPFTestDataSamples.getImage("nature1.jpg"); + + String relationId = sampleDoc.addPictureData(pictureData,XWPFDocument.PICTURE_TYPE_JPEG); + // picture list was updated + assertEquals(num + 1,pictures.size()); + XWPFPictureData pict = (XWPFPictureData) sampleDoc.getRelationById(relationId); + assertEquals("jpeg",pict.suggestFileExtension()); + assertTrue(Arrays.equals(pictureData,pict.getData())); + } + + public void testPictureInHeader() throws IOException + { + XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx"); + XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); + + XWPFHeader header = policy.getDefaultHeader(); + + List<XWPFPictureData> pictures = header.getAllPictures(); + assertEquals(1,pictures.size()); + } + + public void testNew() throws InvalidFormatException, IOException + { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx"); + byte[] jpegData = XWPFTestDataSamples.getImage("nature1.jpg"); + byte[] gifData = XWPFTestDataSamples.getImage("nature1.gif"); + byte[] pngData = XWPFTestDataSamples.getImage("nature1.png"); + + List<XWPFPictureData> pictures = doc.getAllPictures(); + assertEquals(0,pictures.size()); + + // Document shouldn't have any image relationships + assertEquals(13,doc.getPackagePart().getRelationships().size()); + for (PackageRelationship rel : doc.getPackagePart().getRelationships()) + { + if (rel.getRelationshipType().equals(XSSFRelation.IMAGE_JPEG.getRelation())) + { + fail("Shouldn't have JPEG yet"); + } + } + + // Add the image + String relationId = doc.addPictureData(jpegData,XWPFDocument.PICTURE_TYPE_JPEG); + assertEquals(1,pictures.size()); + XWPFPictureData jpgPicData = (XWPFPictureData) doc.getRelationById(relationId); + assertEquals("jpeg",jpgPicData.suggestFileExtension()); + assertTrue(Arrays.equals(jpegData,jpgPicData.getData())); + + // Ensure it now has one + assertEquals(14,doc.getPackagePart().getRelationships().size()); + PackageRelationship jpegRel = null; + for (PackageRelationship rel : doc.getPackagePart().getRelationships()) + { + if (rel.getRelationshipType().equals(XWPFRelation.IMAGE_JPEG.getRelation())) + { + if (jpegRel != null) fail("Found 2 jpegs!"); - jpegRel = rel; - } - } - assertNotNull("JPEG Relationship not found", jpegRel); - - // Check the details - assertEquals(XWPFRelation.IMAGE_JPEG.getRelation(), jpegRel.getRelationshipType()); - assertEquals("/word/document.xml", jpegRel.getSource().getPartName().toString()); - assertEquals("/word/media/image1.jpeg", jpegRel.getTargetURI().getPath()); - - XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId()); - byte [] newJPEGData = pictureDataByID.getData(); - assertEquals(newJPEGData.length, jpegData.length); - for(int i = 0; i < newJPEGData.length; i++){ - assertEquals(newJPEGData[i], jpegData[i]); - } - - // Save an re-load, check it appears - doc = XWPFTestDataSamples.writeOutAndReadBack(doc); - assertEquals(1, doc.getAllPictures().size()); - assertEquals(1, doc.getAllPackagePictures().size()); - } + jpegRel = rel; + } + } + assertNotNull("JPEG Relationship not found",jpegRel); + + // Check the details + assertEquals(XWPFRelation.IMAGE_JPEG.getRelation(),jpegRel.getRelationshipType()); + assertEquals("/word/document.xml",jpegRel.getSource().getPartName().toString()); + assertEquals("/word/media/image1.jpeg",jpegRel.getTargetURI().getPath()); + + XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId()); + byte[] newJPEGData = pictureDataByID.getData(); + assertEquals(newJPEGData.length,jpegData.length); + for (int i = 0 ; i < newJPEGData.length ; i++) + { + assertEquals(newJPEGData[i],jpegData[i]); + } + + // Save an re-load, check it appears + doc = XWPFTestDataSamples.writeOutAndReadBack(doc); + assertEquals(1,doc.getAllPictures().size()); + assertEquals(1,doc.getAllPackagePictures().size()); + } + + public void testGetChecksum() { + + } } 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 b12932c2ec..88b44975f6 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -17,6 +17,7 @@ package org.apache.poi.xwpf.usermodel; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.math.BigInteger; import java.util.List; @@ -198,8 +199,9 @@ public class TestXWPFRun extends TestCase { /** * Test that on an existing document, we do the * right thing with it + * @throws IOException */ - public void testExisting() { + public void testExisting() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx"); XWPFParagraph p; XWPFRun run; @@ -330,7 +332,7 @@ public class TestXWPFRun extends TestCase { assertEquals(null, run.getCTR().getRPr()); } - public void testPictureInHeader() { + public void testPictureInHeader() throws IOException { XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx"); XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java index 646103f905..704b02afc5 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java @@ -17,6 +17,7 @@ package org.apache.poi.xwpf.usermodel; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -30,7 +31,7 @@ public class TestXWPFStyles extends TestCase { // super.setUp(); // } - public void testGetUsedStyles(){ + public void testGetUsedStyles() throws IOException{ XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("Styles.docx"); List<XWPFStyle> testUsedStyleList = new ArrayList<XWPFStyle>(); XWPFStyles styles = sampleDoc.getStyles(); |