View Javadoc
1   /**
2    *    Copyright 2009-2020 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    *       http://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 String resource = "resources/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   private void testEvalMethod(XPathParser parser) {
183     assertEquals((Long) 1970L, parser.evalLong("/employee/birth_date/year"));
184     assertEquals((short) 6, (short) parser.evalShort("/employee/birth_date/month"));
185     assertEquals((Integer) 15, parser.evalInteger("/employee/birth_date/day"));
186     assertEquals((Float) 5.8f, parser.evalFloat("/employee/height"));
187     assertEquals((Double) 5.8d, parser.evalDouble("/employee/height"));
188     assertEquals("${id_var}", parser.evalString("/employee/@id"));
189     assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active"));
190     assertEquals("<id>${id_var}</id>", parser.evalNode("/employee/@id").toString().trim());
191     assertEquals(7, parser.evalNodes("/employee/*").size());
192     XNode node = parser.evalNode("/employee/height");
193     assertEquals("employee/height", node.getPath());
194     assertEquals("employee[${id_var}]_height", node.getValueBasedIdentifier());
195   }
196 
197   @Test
198   public void formatXNodeToString() {
199     XPathParser parser = new XPathParser("<users><user><id>100</id><name>Tom</name><age>30</age><cars><car>BMW</car><car>Audi</car><car>Benz</car></cars></user></users>");
200     String usersNodeToString = parser.evalNode("/users").toString();
201     String userNodeToString = parser.evalNode("/users/user").toString();
202     String carsNodeToString = parser.evalNode("/users/user/cars").toString();
203 
204     String usersNodeToStringExpect =
205       "<users>\n" +
206       "    <user>\n" +
207       "        <id>100</id>\n" +
208       "        <name>Tom</name>\n" +
209       "        <age>30</age>\n" +
210       "        <cars>\n" +
211       "            <car>BMW</car>\n" +
212       "            <car>Audi</car>\n" +
213       "            <car>Benz</car>\n" +
214       "        </cars>\n" +
215       "    </user>\n" +
216       "</users>\n";
217 
218     String userNodeToStringExpect =
219       "<user>\n" +
220       "    <id>100</id>\n" +
221       "    <name>Tom</name>\n" +
222       "    <age>30</age>\n" +
223       "    <cars>\n" +
224       "        <car>BMW</car>\n" +
225       "        <car>Audi</car>\n" +
226       "        <car>Benz</car>\n" +
227       "    </cars>\n" +
228       "</user>\n";
229 
230   String carsNodeToStringExpect =
231       "<cars>\n" +
232       "    <car>BMW</car>\n" +
233       "    <car>Audi</car>\n" +
234       "    <car>Benz</car>\n" +
235       "</cars>\n";
236 
237     assertEquals(usersNodeToStringExpect, usersNodeToString);
238     assertEquals(userNodeToStringExpect, userNodeToString);
239     assertEquals(carsNodeToStringExpect, carsNodeToString);
240   }
241 
242 }