View Javadoc
1   /*
2    *    Copyright 2009-2023 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.type;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNull;
20  import static org.mockito.Mockito.verify;
21  import static org.mockito.Mockito.when;
22  
23  import java.sql.Connection;
24  import java.sql.SQLXML;
25  
26  import org.apache.ibatis.BaseDataTest;
27  import org.apache.ibatis.annotations.Insert;
28  import org.apache.ibatis.annotations.Select;
29  import org.apache.ibatis.mapping.Environment;
30  import org.apache.ibatis.session.Configuration;
31  import org.apache.ibatis.session.SqlSession;
32  import org.apache.ibatis.session.SqlSessionFactory;
33  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
34  import org.apache.ibatis.testcontainers.PgContainer;
35  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
36  import org.junit.jupiter.api.BeforeAll;
37  import org.junit.jupiter.api.Tag;
38  import org.junit.jupiter.api.Test;
39  import org.mockito.Mock;
40  
41  @Tag("TestcontainersTests")
42  class SqlxmlTypeHandlerTest extends BaseTypeHandlerTest {
43    private static final TypeHandler<String> TYPE_HANDLER = new SqlxmlTypeHandler();
44  
45    private static SqlSessionFactory sqlSessionFactory;
46  
47    @Mock
48    private SQLXML sqlxml;
49  
50    @Mock
51    private Connection connection;
52  
53    @BeforeAll
54    static void setUp() throws Exception {
55      Configuration configuration = new Configuration();
56      Environment environment = new Environment("development", new JdbcTransactionFactory(),
57          PgContainer.getUnpooledDataSource());
58      configuration.setEnvironment(environment);
59      configuration.addMapper(Mapper.class);
60      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
61  
62      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
63          "org/apache/ibatis/type/SqlxmlTypeHandlerTest.sql");
64    }
65  
66    @Override
67    @Test
68    public void shouldSetParameter() throws Exception {
69      when(connection.createSQLXML()).thenReturn(sqlxml);
70      when(ps.getConnection()).thenReturn(connection);
71      String xml = "<message>test</message>";
72      TYPE_HANDLER.setParameter(ps, 1, xml, null);
73      verify(ps).setSQLXML(1, sqlxml);
74      verify(sqlxml).setString(xml);
75      verify(sqlxml).free();
76    }
77  
78    @Override
79    @Test
80    public void shouldGetResultFromResultSetByName() throws Exception {
81      String xml = "<message>test</message>";
82      when(sqlxml.getString()).thenReturn(xml);
83      when(rs.getSQLXML("column")).thenReturn(sqlxml);
84      assertEquals(xml, TYPE_HANDLER.getResult(rs, "column"));
85      verify(sqlxml).free();
86    }
87  
88    @Override
89    @Test
90    public void shouldGetResultNullFromResultSetByName() throws Exception {
91      when(rs.getSQLXML("column")).thenReturn(null);
92      assertNull(TYPE_HANDLER.getResult(rs, "column"));
93    }
94  
95    @Override
96    @Test
97    public void shouldGetResultFromResultSetByPosition() throws Exception {
98      String xml = "<message>test</message>";
99      when(sqlxml.getString()).thenReturn(xml);
100     when(rs.getSQLXML(1)).thenReturn(sqlxml);
101     assertEquals(xml, TYPE_HANDLER.getResult(rs, 1));
102     verify(sqlxml).free();
103   }
104 
105   @Override
106   @Test
107   public void shouldGetResultNullFromResultSetByPosition() throws Exception {
108     when(rs.getSQLXML(1)).thenReturn(null);
109     assertNull(TYPE_HANDLER.getResult(rs, 1));
110   }
111 
112   @Override
113   @Test
114   public void shouldGetResultFromCallableStatement() throws Exception {
115     String xml = "<message>test</message>";
116     when(sqlxml.getString()).thenReturn(xml);
117     when(cs.getSQLXML(1)).thenReturn(sqlxml);
118     assertEquals(xml, TYPE_HANDLER.getResult(cs, 1));
119     verify(sqlxml).free();
120   }
121 
122   @Override
123   @Test
124   public void shouldGetResultNullFromCallableStatement() throws Exception {
125     when(cs.getSQLXML(1)).thenReturn(null);
126     assertNull(TYPE_HANDLER.getResult(cs, 1));
127   }
128 
129   @Test
130   void shouldReturnXmlAsString() {
131     try (SqlSession session = sqlSessionFactory.openSession()) {
132       Mapper mapper = session.getMapper(Mapper.class);
133       XmlBean bean = mapper.select(1);
134       assertEquals("<title>XML data</title>", bean.getContent());
135     }
136   }
137 
138   @Test
139   void shouldReturnNull() {
140     try (SqlSession session = sqlSessionFactory.openSession()) {
141       Mapper mapper = session.getMapper(Mapper.class);
142       XmlBean bean = mapper.select(2);
143       assertNull(bean.getContent());
144     }
145   }
146 
147   @Test
148   void shouldInsertXmlString() {
149     final Integer id = 100;
150     final String content = "<books><book><title>Save XML</title></book><book><title>Get XML</title></book></books>";
151     // Insert
152     try (SqlSession session = sqlSessionFactory.openSession()) {
153       Mapper mapper = session.getMapper(Mapper.class);
154       XmlBean bean = new XmlBean();
155       bean.setId(id);
156       bean.setContent(content);
157       mapper.insert(bean);
158       session.commit();
159     }
160     // Select to verify
161     try (SqlSession session = sqlSessionFactory.openSession()) {
162       Mapper mapper = session.getMapper(Mapper.class);
163       XmlBean bean = mapper.select(id);
164       assertEquals(content, bean.getContent());
165     }
166   }
167 
168   interface Mapper {
169     @Select("select id, content from mbtest.test_sqlxml where id = #{id}")
170     XmlBean select(Integer id);
171 
172     @Insert("insert into mbtest.test_sqlxml (id, content) values (#{id}, #{content,jdbcType=SQLXML})")
173     void insert(XmlBean bean);
174   }
175 
176   public static class XmlBean {
177     private Integer id;
178 
179     private String content;
180 
181     public Integer getId() {
182       return id;
183     }
184 
185     public void setId(Integer id) {
186       this.id = id;
187     }
188 
189     public String getContent() {
190       return content;
191     }
192 
193     public void setContent(String content) {
194       this.content = content;
195     }
196   }
197 }