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