Skip to content

Commit

Permalink
added tests for parsing package
Browse files Browse the repository at this point in the history
  • Loading branch information
tacoo committed Sep 20, 2020
1 parent bc90ee2 commit 774d031
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
37 changes: 27 additions & 10 deletions src/test/java/org/apache/ibatis/parsing/XPathParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,31 @@ private String getXmlString(String resource) throws IOException {
}
}

enum EnumTest {
YES, NO
}

private void testEvalMethod(XPathParser parser) {
assertEquals((Long) 1970L, parser.evalLong("/employee/birth_date/year"));
assertEquals((Long) 1970L, parser.evalNode("/employee/birth_date/year").getLongBody());
assertEquals((short) 6, (short) parser.evalShort("/employee/birth_date/month"));
assertEquals((Integer) 15, parser.evalInteger("/employee/birth_date/day"));
assertEquals((Integer) 15, parser.evalNode("/employee/birth_date/day").getIntBody());
assertEquals((Float) 5.8f, parser.evalFloat("/employee/height"));
assertEquals((Float) 5.8f, parser.evalNode("/employee/height").getFloatBody());
assertEquals((Double) 5.8d, parser.evalDouble("/employee/height"));
assertEquals((Double) 5.8d, parser.evalNode("/employee/height").getDoubleBody());
assertEquals((Double) 5.8d, parser.evalNode("/employee").evalDouble("height"));
assertEquals("${id_var}", parser.evalString("/employee/@id"));
assertEquals("${id_var}", parser.evalNode("/employee/@id").getStringBody());
assertEquals("${id_var}", parser.evalNode("/employee").evalString("@id"));
assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active"));
assertEquals(Boolean.TRUE, parser.evalNode("/employee/active").getBooleanBody());
assertEquals(Boolean.TRUE, parser.evalNode("/employee").evalBoolean("active"));
assertEquals(EnumTest.YES, parser.evalNode("/employee/active").getEnumAttribute(EnumTest.class, "bot"));
assertEquals((Float) 3.2f, parser.evalNode("/employee/active").getFloatAttribute("score"));
assertEquals((Double) 3.2d, parser.evalNode("/employee/active").getDoubleAttribute("score"));

assertEquals("<id>${id_var}</id>", parser.evalNode("/employee/@id").toString().trim());
assertEquals(7, parser.evalNodes("/employee/*").size());
XNode node = parser.evalNode("/employee/height");
Expand All @@ -196,7 +213,7 @@ private void testEvalMethod(XPathParser parser) {

@Test
void formatXNodeToString() {
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>");
XPathParser parser = new XPathParser("<users><user><id>100</id><name>Tom</name><age>30</age><cars><car index=\"1\">BMW</car><car index=\"2\">Audi</car><car index=\"3\">Benz</car></cars></user></users>");
String usersNodeToString = parser.evalNode("/users").toString();
String userNodeToString = parser.evalNode("/users/user").toString();
String carsNodeToString = parser.evalNode("/users/user/cars").toString();
Expand All @@ -208,9 +225,9 @@ void formatXNodeToString() {
" <name>Tom</name>\n" +
" <age>30</age>\n" +
" <cars>\n" +
" <car>BMW</car>\n" +
" <car>Audi</car>\n" +
" <car>Benz</car>\n" +
" <car index=\"1\">BMW</car>\n" +
" <car index=\"2\">Audi</car>\n" +
" <car index=\"3\">Benz</car>\n" +
" </cars>\n" +
" </user>\n" +
"</users>\n";
Expand All @@ -221,17 +238,17 @@ void formatXNodeToString() {
" <name>Tom</name>\n" +
" <age>30</age>\n" +
" <cars>\n" +
" <car>BMW</car>\n" +
" <car>Audi</car>\n" +
" <car>Benz</car>\n" +
" <car index=\"1\">BMW</car>\n" +
" <car index=\"2\">Audi</car>\n" +
" <car index=\"3\">Benz</car>\n" +
" </cars>\n" +
"</user>\n";

String carsNodeToStringExpect =
"<cars>\n" +
" <car>BMW</car>\n" +
" <car>Audi</car>\n" +
" <car>Benz</car>\n" +
" <car index=\"1\">BMW</car>\n" +
" <car index=\"2\">Audi</car>\n" +
" <car index=\"3\">Benz</car>\n" +
"</cars>\n";

assertEquals(usersNodeToStringExpect, usersNodeToString);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/resources/nodelet_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
</birth_date>
<height units="ft">5.8</height>
<weight units="lbs">200</weight>
<active>true</active>
</employee>
<active bot="YES" score="3.2">true</active>
</employee>

0 comments on commit 774d031

Please # to comment.