Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions src/test/java/resources/nodelet_test.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--

Copyright 2009-2016 the original author or authors.
Copyright 2009-2020 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -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>