View Javadoc
1   /*
2    *    Copyright 2009-2024 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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      String usersNodeToStringExpect = """
35          <users>
36            <user>
37              <id>
38                100
39              </id>
40              <name>
41                Tom
42              </name>
43              <age>
44                30
45              </age>
46              <cars>
47                <car index="1">
48                  BMW
49                </car>
50                <car index="2">
51                  Audi
52                </car>
53                <car index="3">
54                  Benz
55                </car>
56              </cars>
57            </user>
58          </users>
59          """;
60  
61      String userNodeToStringExpect = """
62          <user>
63            <id>
64              100
65            </id>
66            <name>
67              Tom
68            </name>
69            <age>
70              30
71            </age>
72            <cars>
73              <car index="1">
74                BMW
75              </car>
76              <car index="2">
77                Audi
78              </car>
79              <car index="3">
80                Benz
81              </car>
82            </cars>
83          </user>
84          """;
85  
86      String carsNodeToStringExpect = """
87          <cars>
88            <car index="1">
89              BMW
90            </car>
91            <car index="2">
92              Audi
93            </car>
94            <car index="3">
95              Benz
96            </car>
97          </cars>
98          """;
99  
100     assertEquals(usersNodeToStringExpect, usersNodeToString);
101     assertEquals(userNodeToStringExpect, userNodeToString);
102     assertEquals(carsNodeToStringExpect, carsNodeToString);
103   }
104 
105   @Test
106   void xNodeToString() {
107     String xml = """
108         <mapper>
109           <select id='select' resultType='map'>
110             select
111             <var set='foo' value='bar' />
112               ID,
113               NAME
114             from STUDENT
115             <where>
116               <if test="name != null">
117                 NAME = #{name}
118               </if>
119               and DISABLED = false
120             </where>
121             order by ID
122             <choose>
123               <when test='limit10'>
124                 limit 10
125               </when>
126               <otherwise>limit 20</otherwise>
127             </choose>
128           </select>
129         </mapper>
130         """;
131 
132     // a little bit ugly with id/name break, but not a blocker
133     String expected = """
134         <select id="select" resultType="map">
135           select
136           <var set="foo" value="bar" />
137           ID,
138               NAME
139             from STUDENT
140           <where>
141             <if test="name != null">
142               NAME = #{name}
143             </if>
144             and DISABLED = false
145           </where>
146           order by ID
147           <choose>
148             <when test="limit10">
149               limit 10
150             </when>
151             <otherwise>
152               limit 20
153             </otherwise>
154           </choose>
155         </select>
156         """;
157 
158     XPathParser parser = new XPathParser(xml);
159     XNode selectNode = parser.evalNode("/mapper/select");
160     assertEquals(expected, selectNode.toString());
161   }
162 
163   @Test
164   void xnodeToStringVariables() throws Exception {
165     String src = "<root attr='${x}'>y = ${y}<sub attr='${y}'>x = ${x}</sub></root>";
166     String expected = "<root attr=\"foo\">\n  y = bar\n  <sub attr=\"bar\">\n    x = foo\n  </sub>\n</root>\n";
167     Properties vars = new Properties();
168     vars.put("x", "foo");
169     vars.put("y", "bar");
170     XPathParser parser = new XPathParser(src, false, vars);
171     XNode selectNode = parser.evalNode("/root");
172     assertEquals(expected, selectNode.toString());
173   }
174 
175 }