1717
1818import static org .junit .jupiter .api .Assertions .assertEquals ;
1919
20- import javax .xml .parsers .DocumentBuilder ;
21- import javax .xml .parsers .DocumentBuilderFactory ;
2220import java .io .BufferedReader ;
2321import java .io .IOException ;
2422import java .io .InputStream ;
2523import java .io .Reader ;
2624
25+ import javax .xml .parsers .DocumentBuilder ;
26+ import javax .xml .parsers .DocumentBuilderFactory ;
27+
2728import org .apache .ibatis .builder .BuilderException ;
2829import org .apache .ibatis .io .Resources ;
2930import org .junit .jupiter .api .Test ;
3031import org .w3c .dom .Document ;
31- import org .xml .sax .* ;
32+ import org .xml .sax .InputSource ;
3233
3334class XPathParserTest {
3435 private String resource = "resources/nodelet_test.xml" ;
3536
36- //InputStream Source
37+ // InputStream Source
3738 @ Test
3839 void constructorWithInputStreamValidationVariablesEntityResolver () throws Exception {
3940
@@ -67,7 +68,7 @@ void constructorWithInputStream() throws IOException {
6768 }
6869 }
6970
70- //Reader Source
71+ // Reader Source
7172 @ Test
7273 void constructorWithReaderValidationVariablesEntityResolver () throws Exception {
7374
@@ -101,7 +102,7 @@ void constructorWithReader() throws IOException {
101102 }
102103 }
103104
104- //Xml String Source
105+ // Xml String Source
105106 @ Test
106107 void constructorWithStringValidationVariablesEntityResolver () throws Exception {
107108 XPathParser parser = new XPathParser (getXmlString (resource ), false , null , null );
@@ -126,7 +127,7 @@ void constructorWithString() throws IOException {
126127 testEvalMethod (parser );
127128 }
128129
129- //Document Source
130+ // Document Source
130131 @ Test
131132 void constructorWithDocumentValidationVariablesEntityResolver () {
132133 XPathParser parser = new XPathParser (getDocument (resource ), false , null , null );
@@ -161,7 +162,7 @@ private Document getDocument(String resource) {
161162 factory .setCoalescing (false );
162163 factory .setExpandEntityReferences (true );
163164 DocumentBuilder builder = factory .newDocumentBuilder ();
164- return builder .parse (inputSource );//already closed resource in builder.parse method
165+ return builder .parse (inputSource );// already closed resource in builder.parse method
165166 } catch (Exception e ) {
166167 throw new BuilderException ("Error creating document instance. Cause: " + e , e );
167168 }
@@ -193,4 +194,49 @@ private void testEvalMethod(XPathParser parser) {
193194 assertEquals ("employee[${id_var}]_height" , node .getValueBasedIdentifier ());
194195 }
195196
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+ }
0 commit comments