forked from thombergs/docx-stamper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalDisplayOfTablesTest.java
More file actions
49 lines (40 loc) · 2.16 KB
/
ConditionalDisplayOfTablesTest.java
File metadata and controls
49 lines (40 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.wickedsource.docxstamper;
import jakarta.xml.bind.JAXBElement;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.P;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.Tc;
import org.docx4j.wml.Tr;
import org.junit.Assert;
import org.junit.Test;
import org.wickedsource.docxstamper.context.NameContext;
import org.wickedsource.docxstamper.util.ParagraphWrapper;
import java.io.IOException;
import java.io.InputStream;
public class ConditionalDisplayOfTablesTest extends AbstractDocx4jTest {
@Test
public void test() throws Docx4JException, IOException {
NameContext context = new NameContext();
context.setName("Homer");
InputStream template = getClass().getResourceAsStream("ConditionalDisplayOfTablesTest.docx");
WordprocessingMLPackage document = stampAndLoad(template, context);
globalTablesAreRemoved(document);
nestedTablesAreRemoved(document);
}
private void globalTablesAreRemoved(WordprocessingMLPackage document) {
P p1 = (P) document.getMainDocumentPart().getContent().get(1);
Tbl table2 = (Tbl) ((JAXBElement) document.getMainDocumentPart().getContent().get(3)).getValue();
Tbl table3 = (Tbl) ((JAXBElement) document.getMainDocumentPart().getContent().get(5)).getValue();
P p4 = (P) document.getMainDocumentPart().getContent().get(7);
Assert.assertEquals("This paragraph stays untouched.", new ParagraphWrapper(p1).getText());
Assert.assertNotNull(table2);
Assert.assertNotNull(table3);
Assert.assertEquals("This paragraph stays untouched.", new ParagraphWrapper(p4).getText());
}
private void nestedTablesAreRemoved(WordprocessingMLPackage document) {
Tbl outerTable = (Tbl) ((JAXBElement) document.getMainDocumentPart().getContent().get(3)).getValue();
Tc cell = (Tc) ((JAXBElement) ((Tr) outerTable.getContent().get(1)).getContent().get(1)).getValue();
Assert.assertEquals("", new ParagraphWrapper((P) cell.getContent().get(0)).getText()); // empty paragraph, since the last element inside the cell was removed
}
}