Skip to content

Commit

Permalink
mybatis#331 Added tests for spec coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata committed Mar 2, 2015
1 parent cdad451 commit e99abe6
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 0 deletions.
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');
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();
}
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>
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);
}

}
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();
}
}
}
Loading

0 comments on commit e99abe6

Please sign in to comment.