forked from thombergs/docx-stamper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomEvaluationContextConfigurerTest.java
More file actions
82 lines (64 loc) · 2.62 KB
/
CustomEvaluationContextConfigurerTest.java
File metadata and controls
82 lines (64 loc) · 2.62 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
75
76
77
78
79
80
81
82
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.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.wickedsource.docxstamper.api.EvaluationContextConfigurer;
import org.wickedsource.docxstamper.util.ParagraphWrapper;
public class CustomEvaluationContextConfigurerTest extends AbstractDocx4jTest {
@Test
public void customEvaluationContextConfigurerIsHonored() throws Docx4JException, IOException {
DocxStamperConfiguration config = new DocxStamperConfiguration();
config.setEvaluationContextConfigurer(new EvaluationContextConfigurer() {
@Override
public void configureEvaluationContext(StandardEvaluationContext context) {
context.addPropertyAccessor(new SimpleGetter("foo", "bar"));
}
});
InputStream template = getClass().getResourceAsStream("CustomEvaluationContextConfigurerTest.docx");
WordprocessingMLPackage document = stampAndLoad(template, new EmptyContext(), config);
P p2 = (P) document.getMainDocumentPart().getContent().get(2);
Assert.assertEquals("The variable foo has the value bar.", new ParagraphWrapper(p2).getText());
}
static class EmptyContext {
}
static class SimpleGetter implements PropertyAccessor {
private final String fieldName;
private final Object value;
public SimpleGetter(String fieldName, Object value) {
this.fieldName = fieldName;
this.value = value;
}
@Override
public Class<?>[] getSpecificTargetClasses() {
return null;
}
@Override
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
return true;
}
@Override
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
if (name.equals(this.fieldName)) {
return new TypedValue(value);
} else {
return null;
}
}
@Override
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
return false;
}
@Override
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
}
}
}