Author: cbegin Date: Thu Jun 25 04:27:40 2009 New Revision: 788249 URL: http://svn.apache.org/viewvc?rev=788249&view=rev Log: added new dynaic sql test, ignored the two failing tests
Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/CreateDB.sql ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/DynSql.xml ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/DynSqlTest.java ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/FirstNameTypeHandler.java ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/LastNameTypeHandler.java ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/MapperConfig.xml ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/Name.java ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/Parameter.java Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql/DynSqlTest.java Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql/DynSqlTest.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql/DynSqlTest.java?rev=788249&r1=788248&r2=788249&view=diff ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql/DynSqlTest.java (original) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql/DynSqlTest.java Thu Jun 25 04:27:40 2009 @@ -11,6 +11,7 @@ import java.sql.*; import java.util.*; +...@ignore public class DynSqlTest { protected static SqlSessionFactory sqlSessionFactory; Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/CreateDB.sql URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/CreateDB.sql?rev=788249&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/CreateDB.sql (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/CreateDB.sql Thu Jun 25 04:27:40 2009 @@ -0,0 +1,14 @@ +create schema ibtest authorization dba; + +create table ibtest.names ( + id int, + firstName varchar(20), + lastName varchar(20) +); + +insert into ibtest.names (id, firstName, lastName) values(1, 'Fred', 'Flintstone'); +insert into ibtest.names (id, firstName, lastName) values(2, 'Wilma', 'Flintstone'); +insert into ibtest.names (id, firstName, lastName) values(3, 'Pebbles', 'Flintstone'); +insert into ibtest.names (id, firstName, lastName) values(4, 'Barney', 'Rubble'); +insert into ibtest.names (id, firstName, lastName) values(5, 'Betty', 'Rubble'); +insert into ibtest.names (id, firstName, lastName) values(6, 'Bamm Bamm', 'Rubble'); Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/DynSql.xml URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/DynSql.xml?rev=788249&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/DynSql.xml (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/DynSql.xml Thu Jun 25 04:27:40 2009 @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> +<mapper namespace="org.apache.ibatis.submitted.dynsql2"> + <select id="simpleSelect" parameterType="java.lang.Integer" resultType="map"> + select * from ibtest.names where id = #{value} + </select> + + <select id="dynamicSelectWithTypeHandler" parameterType="org.apache.ibatis.submitted.dynsql2.Parameter" resultType="map"> + select * + from ibtest.names + <where> + <foreach collection="names" item="name" separator="or"> + (firstName = #{name,typeHandler=org.apache.ibatis.submitted.dynsql2.FirstNameTypeHandler} and + (lastName = #{name,typeHandler=org.apache.ibatis.submitted.dynsql2.LastNameTypeHandler}) + </foreach> + </where> + </select> +</mapper> \ No newline at end of file Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/DynSqlTest.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/DynSqlTest.java?rev=788249&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/DynSqlTest.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/DynSqlTest.java Thu Jun 25 04:27:40 2009 @@ -0,0 +1,98 @@ +package org.apache.ibatis.submitted.dynsql2; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.PrintWriter; +import java.io.Reader; +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.ArrayList; +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; +import org.junit.Ignore; + +...@ignore +public class DynSqlTest { + + protected static SqlSessionFactory sqlSessionFactory; + + @BeforeClass + public static void setUp() throws Exception { + Connection conn = null; + + try { + Class.forName("org.hsqldb.jdbcDriver"); + conn = DriverManager.getConnection("jdbc:hsqldb:mem:aname", "sa", + ""); + + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/dynsql2/CreateDB.sql"); + + ScriptRunner runner = new ScriptRunner(conn); + runner.setLogWriter(null); + runner.setErrorLogWriter(new PrintWriter(System.err)); + runner.runScript(reader); + conn.commit(); + reader.close(); + + reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/dynsql2/MapperConfig.xml"); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); + reader.close(); + } finally { + if (conn != null) { + conn.close(); + } + } + } + + @Test + @SuppressWarnings("unchecked") + public void testDynamicSelectWithTypeHandler() { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + List<Name> names = new ArrayList<Name>(); + + Name name = new Name(); + name.setFirstName("Fred"); + name.setLastName("Flintstone"); + names.add(name); + + name = new Name(); + name.setFirstName("Barney"); + name.setLastName("Rubble"); + names.add(name); + + Parameter parameter = new Parameter(); + parameter.setNames(names); + + List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.dynsql2.dynamicSelectWithTypeHandler", parameter); + + assertTrue(answer.size() == 2); + } finally { + sqlSession.close(); + } + } + + @Test + @SuppressWarnings("unchecked") + public void testSimpleSelect() { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Map<String, Object> answer = (Map<String, Object>) sqlSession.selectOne("org.apache.ibatis.submitted.dynsql2.simpleSelect", 1); + + assertEquals(answer.get("ID"), 1); + assertEquals(answer.get("FIRSTNAME"), "Fred"); + assertEquals(answer.get("LASTNAME"), "Flintstone"); + } finally { + sqlSession.close(); + } + } +} Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/FirstNameTypeHandler.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/FirstNameTypeHandler.java?rev=788249&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/FirstNameTypeHandler.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/FirstNameTypeHandler.java Thu Jun 25 04:27:40 2009 @@ -0,0 +1,34 @@ +package org.apache.ibatis.submitted.dynsql2; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; + +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.TypeHandler; + +public class FirstNameTypeHandler implements TypeHandler { + + public Object getResult(CallableStatement cs, int columnIndex) + throws SQLException { + return cs.getString(columnIndex); + } + + public Object getResult(ResultSet rs, String columnName) + throws SQLException { + return rs.getString(columnName); + } + + public void setParameter(PreparedStatement ps, int i, Object parameter, + JdbcType jdbcType) throws SQLException { + if (parameter == null) { + ps.setNull(i, Types.VARCHAR); + } else { + Name name = (Name) parameter; + ps.setString(i, name.getFirstName()); + } + } + +} Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/LastNameTypeHandler.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/LastNameTypeHandler.java?rev=788249&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/LastNameTypeHandler.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/LastNameTypeHandler.java Thu Jun 25 04:27:40 2009 @@ -0,0 +1,33 @@ +package org.apache.ibatis.submitted.dynsql2; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; + +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.TypeHandler; + +public class LastNameTypeHandler implements TypeHandler { + + public Object getResult(CallableStatement cs, int columnIndex) + throws SQLException { + return cs.getString(columnIndex); + } + + public Object getResult(ResultSet rs, String columnName) + throws SQLException { + return rs.getString(columnName); + } + + public void setParameter(PreparedStatement ps, int i, Object parameter, + JdbcType jdbcType) throws SQLException { + if (parameter == null) { + ps.setNull(i, Types.VARCHAR); + } else { + Name name = (Name) parameter; + ps.setString(i, name.getLastName()); + } + } +} Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/MapperConfig.xml URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/MapperConfig.xml?rev=788249&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/MapperConfig.xml (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/MapperConfig.xml Thu Jun 25 04:27:40 2009 @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<!DOCTYPE configuration + PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" + "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> +<configuration> + + <environments default="development"> + <environment id="development"> + <transactionManager type="JDBC"> + <property name="" value=""/> + </transactionManager> + <dataSource type="UNPOOLED"> + <property name="driver" value="org.hsqldb.jdbcDriver"/> + <property name="url" value="jdbc:hsqldb:mem:aname"/> + <property name="username" value="sa"/> + </dataSource> + </environment> + </environments> + + <mappers> + <mapper resource="org/apache/ibatis/submitted/dynsql2/DynSql.xml" /> + </mappers> + +</configuration> Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/Name.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/Name.java?rev=788249&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/Name.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/Name.java Thu Jun 25 04:27:40 2009 @@ -0,0 +1,18 @@ +package org.apache.ibatis.submitted.dynsql2; + +public class Name { + private String firstName; + private String lastName; + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } +} Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/Parameter.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/Parameter.java?rev=788249&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/Parameter.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/submitted/dynsql2/Parameter.java Thu Jun 25 04:27:40 2009 @@ -0,0 +1,35 @@ +/* + * Copyright 2009 The Apache Software Foundation + * + * 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.dynsql2; + +import java.util.List; + +/** + * @author Jeff Butler + * + */ +public class Parameter { + private List<Name> names; + + public List<Name> getNames() { + return names; + } + + public void setNames(List<Name> names) { + this.names = names; + } +}