forked from thombergs/docx-stamper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionReplacementInTablesTest.java
More file actions
73 lines (61 loc) · 3.55 KB
/
ExpressionReplacementInTablesTest.java
File metadata and controls
73 lines (61 loc) · 3.55 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 ExpressionReplacementInTablesTest extends AbstractDocx4jTest {
@Test
public void test() throws Docx4JException, IOException {
NameContext context = new NameContext();
context.setName("Bart Simpson");
InputStream template = getClass().getResourceAsStream("ExpressionReplacementInTablesTest.docx");
WordprocessingMLPackage document = stampAndLoad(template, context);
resolvedExpressionsAreReplacedInFirstLevelTable(document);
unresolvedExpressionsAreNotReplacedInFirstLevelTable(document);
resolvedExpressionsAreReplacedInNestedTable(document);
unresolvedExpressionsAreNotReplacedInNestedTable(document);
}
private void resolvedExpressionsAreReplacedInFirstLevelTable(WordprocessingMLPackage document) {
Tbl table = (Tbl) ((JAXBElement) document.getMainDocumentPart().getContent().get(1)).getValue();
Tr row = (Tr) table.getContent().get(0);
Tc cell = (Tc) ((JAXBElement) row.getContent().get(1)).getValue();
P nameParagraph = (P) cell.getContent().get(0);
Assert.assertEquals("Bart Simpson", new ParagraphWrapper(nameParagraph).getText());
}
private void unresolvedExpressionsAreNotReplacedInFirstLevelTable(WordprocessingMLPackage document) {
Tbl table = (Tbl) ((JAXBElement) document.getMainDocumentPart().getContent().get(1)).getValue();
Tr row = (Tr) table.getContent().get(1);
Tc cell = (Tc) ((JAXBElement) row.getContent().get(1)).getValue();
P nameParagraph = (P) cell.getContent().get(0);
Assert.assertEquals("${foo}", new ParagraphWrapper(nameParagraph).getText());
}
private void resolvedExpressionsAreReplacedInNestedTable(WordprocessingMLPackage document) {
Tbl table = (Tbl) ((JAXBElement) document.getMainDocumentPart().getContent().get(1)).getValue();
Tr row = (Tr) table.getContent().get(2);
Tc cell = (Tc) ((JAXBElement) row.getContent().get(0)).getValue();
Tbl nestedTable = (Tbl) ((JAXBElement) cell.getContent().get(1)).getValue();
Tr nestedRow = (Tr) nestedTable.getContent().get(0);
Tc nestedCell = (Tc) ((JAXBElement) nestedRow.getContent().get(1)).getValue();
P nameParagraph = (P) nestedCell.getContent().get(0);
Assert.assertEquals("Bart Simpson", new ParagraphWrapper(nameParagraph).getText());
}
private void unresolvedExpressionsAreNotReplacedInNestedTable(WordprocessingMLPackage document) {
Tbl table = (Tbl) ((JAXBElement) document.getMainDocumentPart().getContent().get(1)).getValue();
Tr row = (Tr) table.getContent().get(2);
Tc cell = (Tc) ((JAXBElement) row.getContent().get(0)).getValue();
Tbl nestedTable = (Tbl) ((JAXBElement) cell.getContent().get(1)).getValue();
Tr nestedRow = (Tr) nestedTable.getContent().get(1);
Tc nestedCell = (Tc) ((JAXBElement) nestedRow.getContent().get(1)).getValue();
P nameParagraph = (P) nestedCell.getContent().get(0);
Assert.assertEquals("${foo}", new ParagraphWrapper(nameParagraph).getText());
}
}