diff options
author | Sayi <sayi@apache.org> | 2021-03-20 16:21:41 +0000 |
---|---|---|
committer | Sayi <sayi@apache.org> | 2021-03-20 16:21:41 +0000 |
commit | 0efa53456a11291cc4bdd1d93f135bb065db61cd (patch) | |
tree | 4fba57857c43070fd68bb4960c11592b8ad866d1 /src/ooxml/testcases/org/apache/poi | |
parent | 6167f3416fefc30997e1c8742d04ae83dbb09338 (diff) | |
download | poi-0efa53456a11291cc4bdd1d93f135bb065db61cd.tar.gz poi-0efa53456a11291cc4bdd1d93f135bb065db61cd.zip |
Create, get, modify and remove comments, support operating paragraphs, pictures and tables in comments
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1887867 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/testcases/org/apache/poi')
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFComment.java | 86 | ||||
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFComments.java | 62 |
2 files changed, 147 insertions, 1 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFComment.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFComment.java index 5f0d8995da..f1ba77714a 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFComment.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFComment.java @@ -16,14 +16,19 @@ ==================================================================== */ package org.apache.poi.xwpf.usermodel; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xwpf.XWPFTestDataSamples; import org.junit.jupiter.api.Test; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.math.BigInteger; +import java.util.Calendar; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; class TestXWPFComment { + @Test void testText() throws IOException { try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("comment.docx")) { @@ -34,4 +39,83 @@ class TestXWPFComment { assertEquals("This is the first line\n\nThis is the second line", comment.getText()); } } + + @Test + public void testAddComment() throws IOException { + BigInteger cId = BigInteger.valueOf(0); + Calendar date = Calendar.getInstance(); + try (XWPFDocument docOut = new XWPFDocument()) { + assertNull(docOut.getDocComments()); + + XWPFComments comments = docOut.createComments(); + XWPFComment comment = comments.createComment(cId); + comment.setAuthor("Author"); + comment.setInitials("s"); + comment.setDate(date); + + XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut); + assertEquals(1, docIn.getComments().length); + comment = docIn.getCommentByID(cId.toString()); + assertNotNull(comment); + assertEquals("Author", comment.getAuthor()); + assertEquals("s", comment.getInitials()); + assertEquals(date.getTimeInMillis(), comment.getDate().getTimeInMillis()); + } + } + + @Test + void testRemoveComment() throws IOException { + try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("comment.docx")) { + assertEquals(1, doc.getComments().length); + + doc.getDocComments().removeComment(0); + + XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(doc); + assertEquals(0, docIn.getComments().length); + } + } + + @Test + void testCreateParagraph() throws IOException { + try (XWPFDocument doc = new XWPFDocument()) { + XWPFComments comments = doc.createComments(); + XWPFComment comment = comments.createComment(BigInteger.ONE); + XWPFParagraph paragraph = comment.createParagraph(); + paragraph.createRun().setText("comment paragraph text"); + + XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(doc); + XWPFComment xwpfComment = docIn.getCommentByID("1"); + assertEquals(1, xwpfComment.getParagraphs().size()); + String text = xwpfComment.getParagraphArray(0).getText(); + assertEquals("comment paragraph text", text); + } + } + + @Test + void testAddPicture() throws IOException, InvalidFormatException { + try (XWPFDocument doc = new XWPFDocument()) { + XWPFComments comments = doc.createComments(); + XWPFComment comment = comments.createComment(BigInteger.ONE); + XWPFParagraph paragraph = comment.createParagraph(); + XWPFRun r = paragraph.createRun(); + r.addPicture(new ByteArrayInputStream(new byte[0]), + Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32); + + assertEquals(1, comments.getAllPictures().size()); + assertEquals(1, doc.getAllPackagePictures().size()); + } + } + + @Test + void testCreateTable() throws IOException { + try (XWPFDocument doc = new XWPFDocument()) { + XWPFComments comments = doc.createComments(); + XWPFComment comment = comments.createComment(BigInteger.ONE); + comment.createTable(1, 1); + + XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(doc); + XWPFComment xwpfComment = docIn.getCommentByID("1"); + assertEquals(1, xwpfComment.getTables().size()); + } + } } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFComments.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFComments.java new file mode 100644 index 0000000000..dad5ab877b --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFComments.java @@ -0,0 +1,62 @@ +/* ==================================================================== + 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.usermodel; + +import org.apache.poi.xwpf.XWPFTestDataSamples; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.math.BigInteger; + +import static org.junit.jupiter.api.Assertions.*; + +class TestXWPFComments { + + @Test + void testAddCommentsToDoc() throws IOException { + BigInteger cId = BigInteger.ZERO; + try (XWPFDocument docOut = new XWPFDocument()) { + assertNull(docOut.getDocComments()); + + // create comments + XWPFComments comments = docOut.createComments(); + assertNotNull(comments); + assertSame(comments, docOut.createComments()); + + // create comment + XWPFComment comment = comments.createComment(cId); + comment.setAuthor("Author"); + comment.createParagraph().createRun().setText("comment paragraph"); + + // apply comment to run text + XWPFParagraph paragraph = docOut.createParagraph(); + paragraph.getCTP().addNewCommentRangeStart().setId(cId); + paragraph.getCTP().addNewR().addNewT().setStringValue("HelloWorld"); + paragraph.getCTP().addNewCommentRangeEnd().setId(cId); + paragraph.getCTP().addNewR().addNewCommentReference().setId(cId); + + // check + XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut); + assertNotNull(docIn.getDocComments()); + assertEquals(1, docIn.getComments().length); + comment = docIn.getCommentByID("0"); + assertTrue(null != comment); + assertEquals("Author", comment.getAuthor()); + } + } + +} |