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
20
import java .io .BufferedReader ;
23
21
import java .io .IOException ;
24
22
import java .io .InputStream ;
25
23
import java .io .Reader ;
26
24
25
+ import javax .xml .parsers .DocumentBuilder ;
26
+ import javax .xml .parsers .DocumentBuilderFactory ;
27
+
27
28
import org .apache .ibatis .builder .BuilderException ;
28
29
import org .apache .ibatis .io .Resources ;
29
30
import org .junit .jupiter .api .Test ;
30
31
import org .w3c .dom .Document ;
31
- import org .xml .sax .* ;
32
+ import org .xml .sax .InputSource ;
32
33
33
34
class XPathParserTest {
34
35
private String resource = "resources/nodelet_test.xml" ;
35
36
36
- //InputStream Source
37
+ // InputStream Source
37
38
@ Test
38
39
void constructorWithInputStreamValidationVariablesEntityResolver () throws Exception {
39
40
@@ -67,7 +68,7 @@ void constructorWithInputStream() throws IOException {
67
68
}
68
69
}
69
70
70
- //Reader Source
71
+ // Reader Source
71
72
@ Test
72
73
void constructorWithReaderValidationVariablesEntityResolver () throws Exception {
73
74
@@ -101,7 +102,7 @@ void constructorWithReader() throws IOException {
101
102
}
102
103
}
103
104
104
- //Xml String Source
105
+ // Xml String Source
105
106
@ Test
106
107
void constructorWithStringValidationVariablesEntityResolver () throws Exception {
107
108
XPathParser parser = new XPathParser (getXmlString (resource ), false , null , null );
@@ -126,7 +127,7 @@ void constructorWithString() throws IOException {
126
127
testEvalMethod (parser );
127
128
}
128
129
129
- //Document Source
130
+ // Document Source
130
131
@ Test
131
132
void constructorWithDocumentValidationVariablesEntityResolver () {
132
133
XPathParser parser = new XPathParser (getDocument (resource ), false , null , null );
@@ -161,7 +162,7 @@ private Document getDocument(String resource) {
161
162
factory .setCoalescing (false );
162
163
factory .setExpandEntityReferences (true );
163
164
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
165
166
} catch (Exception e ) {
166
167
throw new BuilderException ("Error creating document instance. Cause: " + e , e );
167
168
}
@@ -193,4 +194,49 @@ private void testEvalMethod(XPathParser parser) {
193
194
assertEquals ("employee[${id_var}]_height" , node .getValueBasedIdentifier ());
194
195
}
195
196
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