forked from mybatis/mybatis-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mybatis#331 Added tests for spec coverage.
- Loading branch information
Showing
7 changed files
with
447 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/test/java/org/apache/ibatis/submitted/include_property/CreateDB.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-- | ||
-- Copyright 2009-2015 the original author or authors. | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
|
||
drop table table1 if exists; | ||
|
||
create table table1 ( | ||
id int, | ||
a varchar(20), | ||
col_a varchar(20), | ||
col_b varchar(20), | ||
col_c varchar(20) | ||
); | ||
|
||
insert into table1 (id, a, col_a, col_b, col_c) values(1, 'a value', 'col_a value', 'col_b value', 'col_c value'); |
20 changes: 20 additions & 0 deletions
20
.../java/org/apache/ibatis/submitted/include_property/DuplicatedIncludePropertiesMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright 2009-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.submitted.include_property; | ||
|
||
public interface DuplicatedIncludePropertiesMapper { | ||
String select(); | ||
} |
39 changes: 39 additions & 0 deletions
39
...t/java/org/apache/ibatis/submitted/include_property/DuplicatedIncludePropertiesMapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2009-2015 the original author or authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<!DOCTYPE mapper | ||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
|
||
<mapper namespace="org.apache.ibatis.submitted.include_property.DuplicatedIncludePropertiesMapper"> | ||
|
||
<sql id="colsSuffix"> | ||
col_${suffix} | ||
</sql> | ||
|
||
<select id="select" resultType="string"> | ||
select | ||
<include refid="colsSuffix"> | ||
<property name="suffix" value="a" /> | ||
<property name="suffix" value="b" /> | ||
</include> | ||
from table1 | ||
</select> | ||
|
||
</mapper> |
30 changes: 30 additions & 0 deletions
30
src/test/java/org/apache/ibatis/submitted/include_property/IncludePropertyErrorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright 2009-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.submitted.include_property; | ||
|
||
import org.apache.ibatis.exceptions.PersistenceException; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.junit.Test; | ||
|
||
public class IncludePropertyErrorTest { | ||
|
||
@Test(expected = PersistenceException.class) | ||
public void shouldFailForDuplicatedPropertyNames() throws Exception { | ||
Configuration configuration = new Configuration(); | ||
configuration.addMapper(DuplicatedIncludePropertiesMapper.class); | ||
} | ||
|
||
} |
150 changes: 150 additions & 0 deletions
150
src/test/java/org/apache/ibatis/submitted/include_property/IncludePropertyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/** | ||
* Copyright 2009-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.submitted.include_property; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.io.Reader; | ||
import java.sql.Connection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.jdbc.ScriptRunner; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class IncludePropertyTest { | ||
|
||
private static SqlSessionFactory sqlSessionFactory; | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
// create an SqlSessionFactory | ||
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/include_property/mybatis-config.xml"); | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||
reader.close(); | ||
|
||
// populate in-memory database | ||
SqlSession session = sqlSessionFactory.openSession(); | ||
Connection conn = session.getConnection(); | ||
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/include_property/CreateDB.sql"); | ||
ScriptRunner runner = new ScriptRunner(conn); | ||
runner.setLogWriter(null); | ||
runner.runScript(reader); | ||
reader.close(); | ||
session.close(); | ||
} | ||
|
||
@Test | ||
public void testSimpleProperty() throws Exception { | ||
final SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectSimpleA"); | ||
assertEquals("col_a value", results.get(0)); | ||
results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectSimpleB"); | ||
assertEquals("col_b value", results.get(0)); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPropertyContext() throws Exception { | ||
final SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
List<Map<String, String>> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectPropertyContext"); | ||
Map<String, String> map = results.get(0); | ||
assertEquals(2, map.size()); | ||
assertEquals("col_a value", map.get("COL_A")); | ||
assertEquals("col_b value", map.get("COL_B")); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNestedDynamicValue() throws Exception { | ||
final SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectNestedDynamicValue"); | ||
assertEquals("col_a value", results.get(0)); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testEmptyString() throws Exception { | ||
final SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectEmptyProperty"); | ||
assertEquals("a value", results.get(0)); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPropertyInRefid() throws Exception { | ||
final SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectPropertyInRefid"); | ||
assertEquals("col_a value", results.get(0)); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testConfigVar() throws Exception { | ||
final SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectConfigVar"); | ||
assertEquals("Property defined in the config file should be used.", "col_c value", results.get(0)); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRuntimeVar() throws Exception { | ||
final SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
Map<String, String> params = new HashMap<String, String>(); | ||
params.put("suffix", "b"); | ||
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectRuntimeVar", params); | ||
assertEquals("col_b value", results.get(0)); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNestedInclude() throws Exception { | ||
final SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectNestedInclude"); | ||
assertEquals("a value", results.get(0)); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.