Skip to content

Commit 523470e

Browse files
committed
Test for issue #98.
1 parent 6d4800a commit 523470e

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--
2+
-- Copyright 2009-2013 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+
17+
drop table users if exists;
18+
19+
create table users (
20+
id int,
21+
name varchar(20)
22+
);
23+
24+
insert into users (id, name) values(1, 'User1');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2009-2013 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE mapper
18+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
20+
21+
<mapper namespace="org.apache.ibatis.submitted.raw_sql_source.Mapper">
22+
23+
<select id="getUser1" resultType="org.apache.ibatis.submitted.raw_sql_source.User">
24+
select * from users where id = #{value}
25+
</select>
26+
27+
<select id="getUser2" resultType="org.apache.ibatis.submitted.raw_sql_source.User">
28+
select * from users where id = ${value}
29+
</select>
30+
31+
<select id="getUser3" resultType="org.apache.ibatis.submitted.raw_sql_source.User">
32+
<if test="true">
33+
select * from users where id = #{value}
34+
</if>
35+
</select>
36+
37+
</mapper>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2009-2013 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.submitted.raw_sql_source;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
21+
import org.apache.ibatis.io.Resources;
22+
import org.apache.ibatis.jdbc.ScriptRunner;
23+
import org.apache.ibatis.mapping.SqlSource;
24+
import org.apache.ibatis.scripting.defaults.RawSqlSource;
25+
import org.apache.ibatis.scripting.xmltags.DynamicSqlSource;
26+
import org.apache.ibatis.session.SqlSession;
27+
import org.apache.ibatis.session.SqlSessionFactory;
28+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29+
import org.junit.Assert;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class RawSqlSourceTest {
34+
35+
private static SqlSessionFactory sqlSessionFactory;
36+
37+
@BeforeClass
38+
public static void setUp() throws Exception {
39+
// create an SqlSessionFactory
40+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/raw_sql_source/mybatis-config.xml");
41+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42+
reader.close();
43+
44+
// populate in-memory database
45+
SqlSession session = sqlSessionFactory.openSession();
46+
Connection conn = session.getConnection();
47+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/raw_sql_source/CreateDB.sql");
48+
ScriptRunner runner = new ScriptRunner(conn);
49+
runner.setLogWriter(null);
50+
runner.runScript(reader);
51+
reader.close();
52+
session.close();
53+
}
54+
55+
@Test
56+
public void shouldUseRawSqlSourceForAnStaticStatement() {
57+
test("getUser1", RawSqlSource.class);
58+
}
59+
60+
@Test
61+
public void shouldUseDynamicSqlSourceForAnStatementWithInlineArguments() {
62+
test("getUser2", DynamicSqlSource.class);
63+
}
64+
65+
@Test
66+
public void shouldUseDynamicSqlSourceForAnStatementWithXmlTags() {
67+
test("getUser3", DynamicSqlSource.class);
68+
}
69+
70+
private void test(String statement, Class<? extends SqlSource> sqlSource) {
71+
SqlSession sqlSession = sqlSessionFactory.openSession();
72+
try {
73+
Assert.assertEquals(sqlSource, sqlSession.getConfiguration().getMappedStatement(statement).getSqlSource().getClass());
74+
User user = sqlSession.selectOne(statement, 1);
75+
Assert.assertEquals("User1", user.getName());
76+
} finally {
77+
sqlSession.close();
78+
}
79+
}
80+
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009-2013 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.submitted.raw_sql_source;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public void setId(Integer id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
Copyright 2009-2013 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE configuration
18+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
20+
21+
<configuration>
22+
23+
<environments default="development">
24+
<environment id="development">
25+
<transactionManager type="JDBC">
26+
<property name="" value="" />
27+
</transactionManager>
28+
<dataSource type="UNPOOLED">
29+
<property name="driver" value="org.hsqldb.jdbcDriver" />
30+
<property name="url" value="jdbc:hsqldb:mem:raw_sql_source" />
31+
<property name="username" value="sa" />
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
<mappers>
37+
<mapper resource="org/apache/ibatis/submitted/raw_sql_source/Mapper.xml" />
38+
</mappers>
39+
40+
</configuration>

0 commit comments

Comments
 (0)