forked from thombergs/docx-stamper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomExpressionFunctionTest.java
More file actions
43 lines (34 loc) · 1.76 KB
/
CustomExpressionFunctionTest.java
File metadata and controls
43 lines (34 loc) · 1.76 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
package org.wickedsource.docxstamper;
import java.io.IOException;
import java.io.InputStream;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
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;
public class CustomExpressionFunctionTest extends AbstractDocx4jTest {
@Test
public void test() throws Docx4JException, IOException {
NameContext context = new NameContext();
context.setName("Homer Simpson");
InputStream template = getClass().getResourceAsStream("CustomExpressionFunction.docx");
DocxStamperConfiguration config = new DocxStamperConfiguration()
.exposeInterfaceToExpressionLanguage(UppercaseFunction.class, new UppercaseFunctionImpl());
WordprocessingMLPackage document = stampAndLoad(template, context, config);
P nameParagraph = (P) document.getMainDocumentPart().getContent().get(2);
Assert.assertEquals("In this paragraph, a custom expression function is used to uppercase a String: HOMER SIMPSON.", new ParagraphWrapper(nameParagraph).getText());
P commentedParagraph = (P) document.getMainDocumentPart().getContent().get(3);
Assert.assertEquals("To test that custom functions work together with comment expressions, we toggle visibility of this paragraph with a comment expression.", new ParagraphWrapper(commentedParagraph).getText());
}
public interface UppercaseFunction {
String toUppercase(String string);
}
public static class UppercaseFunctionImpl implements UppercaseFunction {
@Override
public String toUppercase(String string) {
return string.toUpperCase();
}
}
}