Skip to content

Commit 3822184

Browse files
committed
mybatis#331 Added tests for spec coverage.
1 parent d8f03c5 commit 3822184

File tree

7 files changed

+447
-0
lines changed

7 files changed

+447
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--
2+
-- Copyright 2009-2015 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 table1 if exists;
18+
19+
create table table1 (
20+
id int,
21+
a varchar(20),
22+
col_a varchar(20),
23+
col_b varchar(20),
24+
col_c varchar(20)
25+
);
26+
27+
insert into table1 (id, a, col_a, col_b, col_c) values(1, 'a value', 'col_a value', 'col_b value', 'col_c value');
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2009-2015 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.include_property;
17+
18+
public interface DuplicatedIncludePropertiesMapper {
19+
String select();
20+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2015 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<!DOCTYPE mapper
21+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
22+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
23+
24+
<mapper namespace="org.apache.ibatis.submitted.include_property.DuplicatedIncludePropertiesMapper">
25+
26+
<sql id="colsSuffix">
27+
col_${suffix}
28+
</sql>
29+
30+
<select id="select" resultType="string">
31+
select
32+
<include refid="colsSuffix">
33+
<property name="suffix" value="a" />
34+
<property name="suffix" value="b" />
35+
</include>
36+
from table1
37+
</select>
38+
39+
</mapper>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2009-2015 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.include_property;
17+
18+
import org.apache.ibatis.exceptions.PersistenceException;
19+
import org.apache.ibatis.session.Configuration;
20+
import org.junit.Test;
21+
22+
public class IncludePropertyErrorTest {
23+
24+
@Test(expected = PersistenceException.class)
25+
public void shouldFailForDuplicatedPropertyNames() throws Exception {
26+
Configuration configuration = new Configuration();
27+
configuration.addMapper(DuplicatedIncludePropertiesMapper.class);
28+
}
29+
30+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/**
2+
* Copyright 2009-2015 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.include_property;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.io.Reader;
21+
import java.sql.Connection;
22+
import java.util.HashMap;
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
import org.apache.ibatis.io.Resources;
27+
import org.apache.ibatis.jdbc.ScriptRunner;
28+
import org.apache.ibatis.session.SqlSession;
29+
import org.apache.ibatis.session.SqlSessionFactory;
30+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
34+
public class IncludePropertyTest {
35+
36+
private static SqlSessionFactory sqlSessionFactory;
37+
38+
@BeforeClass
39+
public static void setUp() throws Exception {
40+
// create an SqlSessionFactory
41+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/include_property/mybatis-config.xml");
42+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43+
reader.close();
44+
45+
// populate in-memory database
46+
SqlSession session = sqlSessionFactory.openSession();
47+
Connection conn = session.getConnection();
48+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/include_property/CreateDB.sql");
49+
ScriptRunner runner = new ScriptRunner(conn);
50+
runner.setLogWriter(null);
51+
runner.runScript(reader);
52+
reader.close();
53+
session.close();
54+
}
55+
56+
@Test
57+
public void testSimpleProperty() throws Exception {
58+
final SqlSession sqlSession = sqlSessionFactory.openSession();
59+
try {
60+
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectSimpleA");
61+
assertEquals("col_a value", results.get(0));
62+
results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectSimpleB");
63+
assertEquals("col_b value", results.get(0));
64+
} finally {
65+
sqlSession.close();
66+
}
67+
}
68+
69+
@Test
70+
public void testPropertyContext() throws Exception {
71+
final SqlSession sqlSession = sqlSessionFactory.openSession();
72+
try {
73+
List<Map<String, String>> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectPropertyContext");
74+
Map<String, String> map = results.get(0);
75+
assertEquals(2, map.size());
76+
assertEquals("col_a value", map.get("COL_A"));
77+
assertEquals("col_b value", map.get("COL_B"));
78+
} finally {
79+
sqlSession.close();
80+
}
81+
}
82+
83+
@Test
84+
public void testNestedDynamicValue() throws Exception {
85+
final SqlSession sqlSession = sqlSessionFactory.openSession();
86+
try {
87+
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectNestedDynamicValue");
88+
assertEquals("col_a value", results.get(0));
89+
} finally {
90+
sqlSession.close();
91+
}
92+
}
93+
94+
@Test
95+
public void testEmptyString() throws Exception {
96+
final SqlSession sqlSession = sqlSessionFactory.openSession();
97+
try {
98+
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectEmptyProperty");
99+
assertEquals("a value", results.get(0));
100+
} finally {
101+
sqlSession.close();
102+
}
103+
}
104+
105+
@Test
106+
public void testPropertyInRefid() throws Exception {
107+
final SqlSession sqlSession = sqlSessionFactory.openSession();
108+
try {
109+
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectPropertyInRefid");
110+
assertEquals("col_a value", results.get(0));
111+
} finally {
112+
sqlSession.close();
113+
}
114+
}
115+
116+
@Test
117+
public void testConfigVar() throws Exception {
118+
final SqlSession sqlSession = sqlSessionFactory.openSession();
119+
try {
120+
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectConfigVar");
121+
assertEquals("Property defined in the config file should be used.", "col_c value", results.get(0));
122+
} finally {
123+
sqlSession.close();
124+
}
125+
}
126+
127+
@Test
128+
public void testRuntimeVar() throws Exception {
129+
final SqlSession sqlSession = sqlSessionFactory.openSession();
130+
try {
131+
Map<String, String> params = new HashMap<String, String>();
132+
params.put("suffix", "b");
133+
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectRuntimeVar", params);
134+
assertEquals("col_b value", results.get(0));
135+
} finally {
136+
sqlSession.close();
137+
}
138+
}
139+
140+
@Test
141+
public void testNestedInclude() throws Exception {
142+
final SqlSession sqlSession = sqlSessionFactory.openSession();
143+
try {
144+
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectNestedInclude");
145+
assertEquals("a value", results.get(0));
146+
} finally {
147+
sqlSession.close();
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)