|
17 | 17 |
|
18 | 18 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
19 | 19 |
|
| 20 | +import javax.xml.parsers.DocumentBuilder; |
| 21 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 22 | +import java.io.BufferedReader; |
| 23 | +import java.io.IOException; |
20 | 24 | import java.io.InputStream;
|
| 25 | +import java.io.Reader; |
21 | 26 |
|
| 27 | +import org.apache.ibatis.builder.BuilderException; |
22 | 28 | import org.apache.ibatis.io.Resources;
|
23 | 29 | import org.junit.jupiter.api.Test;
|
| 30 | +import org.w3c.dom.Document; |
| 31 | +import org.xml.sax.*; |
24 | 32 |
|
25 | 33 | class XPathParserTest {
|
| 34 | + private String resource = "resources/nodelet_test.xml"; |
26 | 35 |
|
| 36 | + //InputStream Source |
27 | 37 | @Test
|
28 |
| - void shouldTestXPathParserMethods() throws Exception { |
29 |
| - String resource = "resources/nodelet_test.xml"; |
| 38 | + void constructorWithInputStreamValidationVariablesEntityResolver() throws Exception { |
| 39 | + |
30 | 40 | try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
|
31 | 41 | XPathParser parser = new XPathParser(inputStream, false, null, null);
|
32 |
| - assertEquals((Long) 1970L, parser.evalLong("/employee/birth_date/year")); |
33 |
| - assertEquals((short) 6, (short) parser.evalShort("/employee/birth_date/month")); |
34 |
| - assertEquals((Integer) 15, parser.evalInteger("/employee/birth_date/day")); |
35 |
| - assertEquals((Float) 5.8f, parser.evalFloat("/employee/height")); |
36 |
| - assertEquals((Double) 5.8d, parser.evalDouble("/employee/height")); |
37 |
| - assertEquals("${id_var}", parser.evalString("/employee/@id")); |
38 |
| - assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active")); |
39 |
| - assertEquals("<id>${id_var}</id>", parser.evalNode("/employee/@id").toString().trim()); |
40 |
| - assertEquals(7, parser.evalNodes("/employee/*").size()); |
41 |
| - XNode node = parser.evalNode("/employee/height"); |
42 |
| - assertEquals("employee/height", node.getPath()); |
43 |
| - assertEquals("employee[${id_var}]_height", node.getValueBasedIdentifier()); |
| 42 | + testEvalMethod(parser); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void constructorWithInputStreamValidationVariables() throws IOException { |
| 48 | + try (InputStream inputStream = Resources.getResourceAsStream(resource)) { |
| 49 | + XPathParser parser = new XPathParser(inputStream, false, null); |
| 50 | + testEvalMethod(parser); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void constructorWithInputStreamValidation() throws IOException { |
| 56 | + try (InputStream inputStream = Resources.getResourceAsStream(resource)) { |
| 57 | + XPathParser parser = new XPathParser(inputStream, false); |
| 58 | + testEvalMethod(parser); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void constructorWithInputStream() throws IOException { |
| 64 | + try (InputStream inputStream = Resources.getResourceAsStream(resource)) { |
| 65 | + XPathParser parser = new XPathParser(inputStream); |
| 66 | + testEvalMethod(parser); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + //Reader Source |
| 71 | + @Test |
| 72 | + void constructorWithReaderValidationVariablesEntityResolver() throws Exception { |
| 73 | + |
| 74 | + try (Reader reader = Resources.getResourceAsReader(resource)) { |
| 75 | + XPathParser parser = new XPathParser(reader, false, null, null); |
| 76 | + testEvalMethod(parser); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void constructorWithReaderValidationVariables() throws IOException { |
| 82 | + try (Reader reader = Resources.getResourceAsReader(resource)) { |
| 83 | + XPathParser parser = new XPathParser(reader, false, null); |
| 84 | + testEvalMethod(parser); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void constructorWithReaderValidation() throws IOException { |
| 90 | + try (Reader reader = Resources.getResourceAsReader(resource)) { |
| 91 | + XPathParser parser = new XPathParser(reader, false); |
| 92 | + testEvalMethod(parser); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void constructorWithReader() throws IOException { |
| 98 | + try (Reader reader = Resources.getResourceAsReader(resource)) { |
| 99 | + XPathParser parser = new XPathParser(reader); |
| 100 | + testEvalMethod(parser); |
44 | 101 | }
|
45 | 102 | }
|
46 | 103 |
|
| 104 | + //Xml String Source |
| 105 | + @Test |
| 106 | + void constructorWithStringValidationVariablesEntityResolver() throws Exception { |
| 107 | + XPathParser parser = new XPathParser(getXmlString(resource), false, null, null); |
| 108 | + testEvalMethod(parser); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void constructorWithStringValidationVariables() throws IOException { |
| 113 | + XPathParser parser = new XPathParser(getXmlString(resource), false, null); |
| 114 | + testEvalMethod(parser); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void constructorWithStringValidation() throws IOException { |
| 119 | + XPathParser parser = new XPathParser(getXmlString(resource), false); |
| 120 | + testEvalMethod(parser); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void constructorWithString() throws IOException { |
| 125 | + XPathParser parser = new XPathParser(getXmlString(resource)); |
| 126 | + testEvalMethod(parser); |
| 127 | + } |
| 128 | + |
| 129 | + //Document Source |
| 130 | + @Test |
| 131 | + void constructorWithDocumentValidationVariablesEntityResolver() { |
| 132 | + XPathParser parser = new XPathParser(getDocument(resource), false, null, null); |
| 133 | + testEvalMethod(parser); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void constructorWithDocumentValidationVariables() { |
| 138 | + XPathParser parser = new XPathParser(getDocument(resource), false, null); |
| 139 | + testEvalMethod(parser); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void constructorWithDocumentValidation() { |
| 144 | + XPathParser parser = new XPathParser(getDocument(resource), false); |
| 145 | + testEvalMethod(parser); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void constructorWithDocument() { |
| 150 | + XPathParser parser = new XPathParser(getDocument(resource)); |
| 151 | + testEvalMethod(parser); |
| 152 | + } |
| 153 | + |
| 154 | + private Document getDocument(String resource) { |
| 155 | + try { |
| 156 | + InputSource inputSource = new InputSource(Resources.getResourceAsReader(resource)); |
| 157 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 158 | + factory.setNamespaceAware(false); |
| 159 | + factory.setIgnoringComments(true); |
| 160 | + factory.setIgnoringElementContentWhitespace(false); |
| 161 | + factory.setCoalescing(false); |
| 162 | + factory.setExpandEntityReferences(true); |
| 163 | + DocumentBuilder builder = factory.newDocumentBuilder(); |
| 164 | + return builder.parse(inputSource);//already closed resource in builder.parse method |
| 165 | + } catch (Exception e) { |
| 166 | + throw new BuilderException("Error creating document instance. Cause: " + e, e); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private String getXmlString(String resource) throws IOException { |
| 171 | + try (BufferedReader bufferedReader = new BufferedReader(Resources.getResourceAsReader(resource))) { |
| 172 | + StringBuilder sb = new StringBuilder(); |
| 173 | + String temp; |
| 174 | + while ((temp = bufferedReader.readLine()) != null) { |
| 175 | + sb.append(temp); |
| 176 | + } |
| 177 | + return sb.toString(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private void testEvalMethod(XPathParser parser) { |
| 182 | + assertEquals((Long) 1970L, parser.evalLong("/employee/birth_date/year")); |
| 183 | + assertEquals((short) 6, (short) parser.evalShort("/employee/birth_date/month")); |
| 184 | + assertEquals((Integer) 15, parser.evalInteger("/employee/birth_date/day")); |
| 185 | + assertEquals((Float) 5.8f, parser.evalFloat("/employee/height")); |
| 186 | + assertEquals((Double) 5.8d, parser.evalDouble("/employee/height")); |
| 187 | + assertEquals("${id_var}", parser.evalString("/employee/@id")); |
| 188 | + assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active")); |
| 189 | + assertEquals("<id>${id_var}</id>", parser.evalNode("/employee/@id").toString().trim()); |
| 190 | + assertEquals(7, parser.evalNodes("/employee/*").size()); |
| 191 | + XNode node = parser.evalNode("/employee/height"); |
| 192 | + assertEquals("employee/height", node.getPath()); |
| 193 | + assertEquals("employee[${id_var}]_height", node.getValueBasedIdentifier()); |
| 194 | + } |
| 195 | + |
47 | 196 | }
|
0 commit comments