-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEfxTestsBase.java
More file actions
132 lines (110 loc) · 4.81 KB
/
EfxTestsBase.java
File metadata and controls
132 lines (110 loc) · 4.81 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package eu.europa.ted.efx;
import static org.junit.jupiter.api.Assertions.assertEquals;
import eu.europa.ted.efx.interfaces.TranslatorOptions;
import eu.europa.ted.efx.mock.DependencyFactoryMock;
import eu.europa.ted.efx.model.DecimalFormat;
import net.sf.saxon.s9api.ExtensionFunction;
import net.sf.saxon.s9api.ItemType;
import net.sf.saxon.s9api.OccurrenceIndicator;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.SequenceType;
import net.sf.saxon.s9api.XPathCompiler;
import net.sf.saxon.s9api.XdmValue;
public abstract class EfxTestsBase {
protected static final TranslatorOptions DEFAULT_OPTIONS = new EfxTranslatorOptions("udf",
DecimalFormat.EFX_DEFAULT);
private static final String EFX_NAMESPACE = "http://ted.europa.eu/efx";
private static final XPathCompiler XPATH_COMPILER;
static {
Processor processor = new Processor(false);
// Register custom EFX extension functions so Saxon can validate XPath syntax.
// These are only needed for V1 expression tests, where multilingual field references
// are implicitly wrapped in efx:preferred-language-text() by XPathScriptGeneratorV1.
// V2 bans these functions in expression context (template-only).
processor.registerExtensionFunction(efxFunction("preferred-language"));
processor.registerExtensionFunction(efxFunction("preferred-language-text"));
XPATH_COMPILER = processor.newXPathCompiler();
XPATH_COMPILER.setLanguageVersion("3.1");
XPATH_COMPILER.declareNamespace("fn", "http://www.w3.org/2005/xpath-functions");
XPATH_COMPILER.declareNamespace("xs", "http://www.w3.org/2001/XMLSchema");
XPATH_COMPILER.declareNamespace("efx", EFX_NAMESPACE);
XPATH_COMPILER.declareVariable(new QName("urlPrefix"));
}
/**
* Creates a dummy extension function stub for XPath syntax validation.
* Accepts one argument (node) and returns a string.
*/
private static ExtensionFunction efxFunction(String localName) {
return new ExtensionFunction() {
@Override
public QName getName() {
return new QName(EFX_NAMESPACE, localName);
}
@Override
public SequenceType getResultType() {
return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
}
@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[] {
SequenceType.makeSequenceType(ItemType.ANY_ITEM, OccurrenceIndicator.ONE_OR_MORE)
};
}
@Override
public XdmValue call(XdmValue[] arguments) {
throw new UnsupportedOperationException("Stub for XPath validation only");
}
};
}
protected abstract String getSdkVersion();
protected void testExpressionTranslationWithContext(final String expectedTranslation,
final String context, final String expression) {
assertEquals(expectedTranslation, translateExpressionWithContext(context, expression));
}
protected void testExpressionTranslation(final String expectedTranslation,
final String expression, final String... params) {
assertEquals(expectedTranslation, translateExpression(expression, params));
}
protected String translateExpressionWithContext(final String context, final String expression) {
return translateExpression(String.format("{%s} ${%s}", context, expression));
}
protected void testComputeExpressionWithContext(final String expectedTranslation,
final String context, final String expression) {
assertEquals(expectedTranslation, translateComputeExpressionWithContext(context, expression));
}
protected String translateComputeExpressionWithContext(final String context,
final String expression) {
return translateExpression(String.format("WITH %s COMPUTE %s", context, expression));
}
protected String translateExpression(final String expression, final String... params) {
try {
String result = EfxTranslator.translateExpression(DependencyFactoryMock.INSTANCE,
getSdkVersion(), expression, DEFAULT_OPTIONS, params);
assertValidXPath(result);
return result;
} catch (InstantiationException e) {
throw new RuntimeException(e);
}
}
protected String translateTemplate(final String template) {
try {
return EfxTranslator.translateTemplate(DependencyFactoryMock.INSTANCE, getSdkVersion(),
template + "\n", DEFAULT_OPTIONS);
} catch (InstantiationException e) {
throw new RuntimeException(e);
}
}
protected static void assertValidXPath(final String xpath) {
try {
XPATH_COMPILER.compile(xpath);
} catch (SaxonApiException e) {
throw new AssertionError(
"Generated XPath is not valid: " + xpath + "\n" + e.getMessage(), e);
}
}
protected String lines(String... lines) {
return String.join("\n", lines);
}
}