1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.parsing;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19
20 import java.util.Properties;
21
22 import org.junit.jupiter.api.Test;
23
24 class XNodeTest {
25
26 @Test
27 void formatXNodeToString() {
28 XPathParser parser = new XPathParser(
29 "<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>");
30 String usersNodeToString = parser.evalNode("/users").toString();
31 String userNodeToString = parser.evalNode("/users/user").toString();
32 String carsNodeToString = parser.evalNode("/users/user/cars").toString();
33
34
35 String usersNodeToStringExpect =
36 "<users>\n"
37 + " <user>\n"
38 + " <id>\n"
39 + " 100\n"
40 + " </id>\n"
41 + " <name>\n"
42 + " Tom\n"
43 + " </name>\n"
44 + " <age>\n"
45 + " 30\n"
46 + " </age>\n"
47 + " <cars>\n"
48 + " <car index=\"1\">\n"
49 + " BMW\n"
50 + " </car>\n"
51 + " <car index=\"2\">\n"
52 + " Audi\n"
53 + " </car>\n"
54 + " <car index=\"3\">\n"
55 + " Benz\n"
56 + " </car>\n"
57 + " </cars>\n"
58 + " </user>\n"
59 + "</users>\n";
60
61
62
63 String userNodeToStringExpect =
64 "<user>\n"
65 + " <id>\n"
66 + " 100\n"
67 + " </id>\n"
68 + " <name>\n"
69 + " Tom\n"
70 + " </name>\n"
71 + " <age>\n"
72 + " 30\n"
73 + " </age>\n"
74 + " <cars>\n"
75 + " <car index=\"1\">\n"
76 + " BMW\n"
77 + " </car>\n"
78 + " <car index=\"2\">\n"
79 + " Audi\n"
80 + " </car>\n"
81 + " <car index=\"3\">\n"
82 + " Benz\n"
83 + " </car>\n"
84 + " </cars>\n"
85 + "</user>\n";
86
87
88
89 String carsNodeToStringExpect =
90 "<cars>\n"
91 + " <car index=\"1\">\n"
92 + " BMW\n"
93 + " </car>\n"
94 + " <car index=\"2\">\n"
95 + " Audi\n"
96 + " </car>\n"
97 + " <car index=\"3\">\n"
98 + " Benz\n"
99 + " </car>\n"
100 + "</cars>\n";
101
102
103 assertEquals(usersNodeToStringExpect, usersNodeToString);
104 assertEquals(userNodeToStringExpect, userNodeToString);
105 assertEquals(carsNodeToStringExpect, carsNodeToString);
106 }
107
108 @Test
109 void xNodeToString() {
110
111 String xml = "<mapper>\n" +
112 " <select id='select' resultType='map'>\n" +
113 " select\n" +
114 " <var set='foo' value='bar' />\n" +
115 " ID,\n" +
116 " NAME\n" +
117 " from STUDENT\n" +
118 " <where>\n" +
119 " <if test=\"name != null\">\n" +
120 " NAME = #{name}\n" +
121 " </if>\n" +
122 " and DISABLED = false\n" +
123 " </where>\n" +
124 " order by ID\n" +
125 " <choose>\n" +
126 " <when test='limit10'>\n" +
127 " limit 10\n" +
128 " </when>\n" +
129 " <otherwise>limit 20</otherwise>\n" +
130 " </choose>\n" +
131 " </select>\n" +
132 "</mapper>";
133
134 String expected = "<select id=\"select\" resultType=\"map\">\n" +
135 " select\n" +
136 " <var set=\"foo\" value=\"bar\" />\n" +
137 " ID,\n" +
138
139 " NAME\n" +
140 " from STUDENT\n" +
141 " <where>\n" +
142 " <if test=\"name != null\">\n" +
143 " NAME = #{name}\n" +
144 " </if>\n" +
145 " and DISABLED = false\n" +
146 " </where>\n" +
147 " order by ID\n" +
148 " <choose>\n" +
149 " <when test=\"limit10\">\n" +
150 " limit 10\n" +
151 " </when>\n" +
152 " <otherwise>\n" +
153 " limit 20\n" +
154 " </otherwise>\n" +
155 " </choose>\n" +
156 "</select>\n";
157
158
159 XPathParser parser = new XPathParser(xml);
160 XNode selectNode = parser.evalNode("/mapper/select");
161 assertEquals(expected, selectNode.toString());
162 }
163
164 @Test
165 void testXnodeToStringVariables() throws Exception {
166 String src = "<root attr='${x}'>y = ${y}<sub attr='${y}'>x = ${x}</sub></root>";
167 String expected = "<root attr=\"foo\">\n y = bar\n <sub attr=\"bar\">\n x = foo\n </sub>\n</root>\n";
168 Properties vars = new Properties();
169 vars.put("x", "foo");
170 vars.put("y", "bar");
171 XPathParser parser = new XPathParser(src, false, vars);
172 XNode selectNode = parser.evalNode("/root");
173 assertEquals(expected, selectNode.toString());
174 }
175
176 }