1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.xml;
17
18 import net.sf.saxon.lib.FeatureKeys;
19 import org.apache.commons.io.IOUtils;
20 import org.custommonkey.xmlunit.XMLUnit;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.xml.sax.SAXException;
25
26 import javax.xml.transform.Source;
27 import javax.xml.transform.stream.StreamSource;
28 import javax.xml.xquery.XQException;
29 import java.io.IOException;
30 import java.io.StringReader;
31 import java.nio.charset.Charset;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import static org.junit.Assert.assertTrue;
36
37 public class XqjTransmogrifierTest {
38
39 private XqjTransmogrifier<String> transformer;
40
41 @BeforeClass
42 public static void setUpClass() {
43 XMLUnit.setCompareUnmatched(true);
44 XMLUnit.setIgnoreAttributeOrder(true);
45 XMLUnit.setIgnoreComments(true);
46 XMLUnit.setIgnoreWhitespace(true);
47 }
48
49 @Before
50 public void setUp() throws Exception {
51 transformer = new XqjTransmogrifier<>(String.class);
52 }
53
54 @Test
55 public void zapSimple() throws IOException, SAXException {
56 String zapResult = transformer.zap(source("/xquery/string.xml"), "/xquery/string-q1.xq");
57 assertTrue(XMLUnit.compareXML(result("/xquery/string-q1.xml"), zapResult).similar());
58 }
59
60 @Test
61 public void zapStringParameter() throws IOException, SAXException {
62 Map<String, Object> dynamicParams = new HashMap<>();
63 dynamicParams.put("language", "English");
64 Object[] params = new Object[] { "/xquery/string-q2.xq", dynamicParams };
65 String zapResult1 = transformer.zap(source("/xquery/string.xml"), params);
66 assertTrue(XMLUnit.compareXML(result("/xquery/string-q2.xml"), zapResult1).similar());
67 dynamicParams.put("language", "German");
68 String zapResult2 = transformer.zap(source("/xquery/string.xml"), params);
69 assertTrue(XMLUnit.compareXML(result("/xquery/string-q2g.xml"), zapResult2).similar());
70 }
71
72 @Test(expected = RuntimeException.class)
73 public void zapMissingParameter() throws IOException, SAXException {
74 transformer.zap(source("/xquery/string.xml"), "/xquery/string-q2.xq");
75 }
76
77 @Test
78 public void zapLocalFunction() throws IOException, SAXException {
79 Map<String, Object> dynamicEvalParams = new HashMap<>();
80 dynamicEvalParams.put("language", "Bulgarian");
81 dynamicEvalParams.put("author_name", "John");
82 Object[] params = new Object[] { "/xquery/string-q3.xq", dynamicEvalParams };
83 String zapResult = transformer.zap(source("/xquery/string.xml"), params);
84 assertTrue(XMLUnit.compareXML(result("/xquery/string-q3.xml"), zapResult).similar());
85 }
86
87 @Test
88 public void zapMainFunction() throws IOException, SAXException {
89 String zapResult = transformer.zap(source("/xquery/string.xml"), "/xquery/string-q4.xq");
90 assertTrue(XMLUnit.compareXML(result("/xquery/string.xml"), zapResult).similar());
91 }
92
93 @Test
94 public void zapClasspathResolver() throws IOException, SAXException {
95 String zapResult = transformer.zap(source("/xquery/string.xml"), "/xquery/string-q5.xq");
96 assertTrue(XMLUnit.compareXML(result("/xquery/string.xml"), zapResult).similar());
97 }
98
99 @Test
100 public void zapParametrisedConstructor() throws IOException, SAXException, XQException {
101 Map<String, Object> configParams = new HashMap<>();
102 configParams.put(FeatureKeys.PRE_EVALUATE_DOC_FUNCTION, Boolean.TRUE);
103 XqjTransmogrifier<String> localTransformer = new XqjTransmogrifier<>(String.class, configParams);
104 String zapResult = localTransformer.zap(source("/xquery/string.xml"), "/xquery/string-q5.xq" );
105 assertTrue(XMLUnit.compareXML(result("/xquery/string.xml"), zapResult).similar());
106 }
107
108 @Test
109 public void zapParametrisedConstructorNoParams() throws IOException, SAXException, XQException {
110 XqjTransmogrifier<String> localTransformer = new XqjTransmogrifier<>(String.class);
111 String zapResult = localTransformer.zap(source("/xquery/string.xml"), "/xquery/string-q5.xq");
112 assertTrue(XMLUnit.compareXML(result("/xquery/string.xml"), zapResult).similar());
113 }
114
115 @Test
116 public void zapWithNamespaces() throws IOException, SAXException {
117 String zapResult = transformer.zap(source("/xquery/ns.xml"), "/xquery/ns-q1.xq");
118 assertTrue(XMLUnit.compareXML(result("/xquery/ns-q1.xml"), zapResult).similar());
119 }
120
121 @Test
122 public void zapRCase() throws IOException, SAXException {
123 String zapResult = transformer.zap(new StreamSource(new StringReader("<empty/>")), "/xquery/r-q1.xq");
124 assertTrue(XMLUnit.compareXML(result("/xquery/r-q1.xml"), zapResult).similar());
125 }
126
127 private Source source(String path) {
128 return new StreamSource(getClass().getResourceAsStream(path));
129 }
130
131 private String result(String path) throws IOException{
132 return IOUtils.toString(getClass().getResourceAsStream(path), Charset.defaultCharset());
133 }
134 }