View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.parsing;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.Reader;
24  
25  import javax.xml.parsers.DocumentBuilder;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  
28  import org.apache.ibatis.builder.BuilderException;
29  import org.apache.ibatis.io.Resources;
30  import org.junit.jupiter.api.Test;
31  import org.w3c.dom.Document;
32  import org.xml.sax.InputSource;
33  
34  class XPathParserTest {
35    private final String resource = "nodelet_test.xml";
36  
37    // InputStream Source
38    @Test
39    void constructorWithInputStreamValidationVariablesEntityResolver() throws Exception {
40  
41      try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
42        XPathParser parser = new XPathParser(inputStream, false, null, null);
43        testEvalMethod(parser);
44      }
45    }
46  
47    @Test
48    void constructorWithInputStreamValidationVariables() throws IOException {
49      try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
50        XPathParser parser = new XPathParser(inputStream, false, null);
51        testEvalMethod(parser);
52      }
53    }
54  
55    @Test
56    void constructorWithInputStreamValidation() throws IOException {
57      try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
58        XPathParser parser = new XPathParser(inputStream, false);
59        testEvalMethod(parser);
60      }
61    }
62  
63    @Test
64    void constructorWithInputStream() throws IOException {
65      try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
66        XPathParser parser = new XPathParser(inputStream);
67        testEvalMethod(parser);
68      }
69    }
70  
71    // Reader Source
72    @Test
73    void constructorWithReaderValidationVariablesEntityResolver() throws Exception {
74  
75      try (Reader reader = Resources.getResourceAsReader(resource)) {
76        XPathParser parser = new XPathParser(reader, false, null, null);
77        testEvalMethod(parser);
78      }
79    }
80  
81    @Test
82    void constructorWithReaderValidationVariables() throws IOException {
83      try (Reader reader = Resources.getResourceAsReader(resource)) {
84        XPathParser parser = new XPathParser(reader, false, null);
85        testEvalMethod(parser);
86      }
87    }
88  
89    @Test
90    void constructorWithReaderValidation() throws IOException {
91      try (Reader reader = Resources.getResourceAsReader(resource)) {
92        XPathParser parser = new XPathParser(reader, false);
93        testEvalMethod(parser);
94      }
95    }
96  
97    @Test
98    void constructorWithReader() throws IOException {
99      try (Reader reader = Resources.getResourceAsReader(resource)) {
100       XPathParser parser = new XPathParser(reader);
101       testEvalMethod(parser);
102     }
103   }
104 
105   // Xml String Source
106   @Test
107   void constructorWithStringValidationVariablesEntityResolver() throws Exception {
108     XPathParser parser = new XPathParser(getXmlString(resource), false, null, null);
109     testEvalMethod(parser);
110   }
111 
112   @Test
113   void constructorWithStringValidationVariables() throws IOException {
114     XPathParser parser = new XPathParser(getXmlString(resource), false, null);
115     testEvalMethod(parser);
116   }
117 
118   @Test
119   void constructorWithStringValidation() throws IOException {
120     XPathParser parser = new XPathParser(getXmlString(resource), false);
121     testEvalMethod(parser);
122   }
123 
124   @Test
125   void constructorWithString() throws IOException {
126     XPathParser parser = new XPathParser(getXmlString(resource));
127     testEvalMethod(parser);
128   }
129 
130   // Document Source
131   @Test
132   void constructorWithDocumentValidationVariablesEntityResolver() {
133     XPathParser parser = new XPathParser(getDocument(resource), false, null, null);
134     testEvalMethod(parser);
135   }
136 
137   @Test
138   void constructorWithDocumentValidationVariables() {
139     XPathParser parser = new XPathParser(getDocument(resource), false, null);
140     testEvalMethod(parser);
141   }
142 
143   @Test
144   void constructorWithDocumentValidation() {
145     XPathParser parser = new XPathParser(getDocument(resource), false);
146     testEvalMethod(parser);
147   }
148 
149   @Test
150   void constructorWithDocument() {
151     XPathParser parser = new XPathParser(getDocument(resource));
152     testEvalMethod(parser);
153   }
154 
155   private Document getDocument(String resource) {
156     try {
157       InputSource inputSource = new InputSource(Resources.getResourceAsReader(resource));
158       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
159       factory.setNamespaceAware(false);
160       factory.setIgnoringComments(true);
161       factory.setIgnoringElementContentWhitespace(false);
162       factory.setCoalescing(false);
163       factory.setExpandEntityReferences(true);
164       DocumentBuilder builder = factory.newDocumentBuilder();
165       return builder.parse(inputSource);// already closed resource in builder.parse method
166     } catch (Exception e) {
167       throw new BuilderException("Error creating document instance.  Cause: " + e, e);
168     }
169   }
170 
171   private String getXmlString(String resource) throws IOException {
172     try (BufferedReader bufferedReader = new BufferedReader(Resources.getResourceAsReader(resource))) {
173       StringBuilder sb = new StringBuilder();
174       String temp;
175       while ((temp = bufferedReader.readLine()) != null) {
176         sb.append(temp);
177       }
178       return sb.toString();
179     }
180   }
181 
182   enum EnumTest {
183     YES, NO
184   }
185 
186   private void testEvalMethod(XPathParser parser) {
187     assertEquals((Long) 1970L, parser.evalLong("/employee/birth_date/year"));
188     assertEquals((Long) 1970L, parser.evalNode("/employee/birth_date/year").getLongBody());
189     assertEquals((short) 6, (short) parser.evalShort("/employee/birth_date/month"));
190     assertEquals((Integer) 15, parser.evalInteger("/employee/birth_date/day"));
191     assertEquals((Integer) 15, parser.evalNode("/employee/birth_date/day").getIntBody());
192     assertEquals((Float) 5.8f, parser.evalFloat("/employee/height"));
193     assertEquals((Float) 5.8f, parser.evalNode("/employee/height").getFloatBody());
194     assertEquals((Double) 5.8d, parser.evalDouble("/employee/height"));
195     assertEquals((Double) 5.8d, parser.evalNode("/employee/height").getDoubleBody());
196     assertEquals((Double) 5.8d, parser.evalNode("/employee").evalDouble("height"));
197     assertEquals("${id_var}", parser.evalString("/employee/@id"));
198     assertEquals("${id_var}", parser.evalNode("/employee/@id").getStringBody());
199     assertEquals("${id_var}", parser.evalNode("/employee").evalString("@id"));
200     assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active"));
201     assertEquals(Boolean.TRUE, parser.evalNode("/employee/active").getBooleanBody());
202     assertEquals(Boolean.TRUE, parser.evalNode("/employee").evalBoolean("active"));
203     assertEquals(EnumTest.YES, parser.evalNode("/employee/active").getEnumAttribute(EnumTest.class, "bot"));
204     assertEquals((Float) 3.2f, parser.evalNode("/employee/active").getFloatAttribute("score"));
205     assertEquals((Double) 3.2d, parser.evalNode("/employee/active").getDoubleAttribute("score"));
206 
207     assertEquals("<id>\n  ${id_var}\n</id>", parser.evalNode("/employee/@id").toString().trim());
208     assertEquals(7, parser.evalNodes("/employee/*").size());
209     XNode node = parser.evalNode("/employee/height");
210     assertEquals("employee/height", node.getPath());
211     assertEquals("employee[${id_var}]_height", node.getValueBasedIdentifier());
212   }
213 
214 }