forked from thombergs/docx-stamper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionReplacementInHeaderAndFooterTest.java
More file actions
74 lines (58 loc) · 3.43 KB
/
ExpressionReplacementInHeaderAndFooterTest.java
File metadata and controls
74 lines (58 loc) · 3.43 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
74
package org.wickedsource.docxstamper;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.P;
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 ExpressionReplacementInHeaderAndFooterTest extends AbstractDocx4jTest {
@Test
public void test() throws Docx4JException, IOException {
NameContext context = new NameContext();
context.setName("Homer Simpson");
InputStream template = getClass().getResourceAsStream("ExpressionReplacementInHeaderAndFooterTest.docx");
WordprocessingMLPackage document = stampAndLoad(template, context);
resolvedExpressionsAreReplacedInHeader(document);
resolvedExpressionsAreReplacedInFooter(document);
unresolvedExpressionsAreNotReplacedInHeader(document);
unresolvedExpressionsAreNotReplacedInFooter(document);
}
private void resolvedExpressionsAreReplacedInHeader(WordprocessingMLPackage document) {
HeaderPart headerPart = getHeaderPart(document);
P nameParagraph = (P) headerPart.getContent().get(1);
Assert.assertEquals("In this paragraph, the variable name should be resolved to the value Homer Simpson.", new ParagraphWrapper(nameParagraph).getText());
}
private void resolvedExpressionsAreReplacedInFooter(WordprocessingMLPackage document) {
FooterPart footerPart = getFooterPart(document);
P nameParagraph = (P) footerPart.getContent().get(1);
Assert.assertEquals("In this paragraph, the variable name should be resolved to the value Homer Simpson.", new ParagraphWrapper(nameParagraph).getText());
}
private void unresolvedExpressionsAreNotReplacedInHeader(WordprocessingMLPackage document) {
HeaderPart headerPart = getHeaderPart(document);
P fooParagraph = (P) headerPart.getContent().get(2);
Assert.assertEquals("In this paragraph, the variable foo should not be resolved: ${foo}.", new ParagraphWrapper(fooParagraph).getText());
}
private void unresolvedExpressionsAreNotReplacedInFooter(WordprocessingMLPackage document) {
FooterPart footerPart = getFooterPart(document);
P fooParagraph = (P) footerPart.getContent().get(2);
Assert.assertEquals("In this paragraph, the variable foo should not be resolved: ${foo}.", new ParagraphWrapper(fooParagraph).getText());
}
private HeaderPart getHeaderPart(WordprocessingMLPackage document) {
RelationshipsPart relPart = document.getMainDocumentPart().getRelationshipsPart();
Relationship rel = relPart.getRelationshipByType(Namespaces.HEADER);
return (HeaderPart) relPart.getPart(rel);
}
private FooterPart getFooterPart(WordprocessingMLPackage document) {
RelationshipsPart relPart = document.getMainDocumentPart().getRelationshipsPart();
Relationship rel = relPart.getRelationshipByType(Namespaces.FOOTER);
return (FooterPart) relPart.getPart(rel);
}
}