Author: cbegin Date: Fri Aug 15 20:57:49 2008 New Revision: 686445 URL: http://svn.apache.org/viewvc?rev=686445&view=rev Log: added test submitted by Jeff on the mailing list
Added: ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/ ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/ ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/Address.java ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/PeerListResultTest.java ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/Person.java ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/PhoneNumber.java ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/TestSqlMap.xml ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/TestSqlMapConfig.xml Added: ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/Address.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/Address.java?rev=686445&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/Address.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/Address.java Fri Aug 15 20:57:49 2008 @@ -0,0 +1,21 @@ +package submitted.resultmap; + +public class Address { + private Integer id; + private String address; + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + public String getAddress() { + return address; + } + public void setAddress(String address) { + this.address = address; + } + public String toString() { + return address; + } +} Added: ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/PeerListResultTest.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/PeerListResultTest.java?rev=686445&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/PeerListResultTest.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/PeerListResultTest.java Fri Aug 15 20:57:49 2008 @@ -0,0 +1,120 @@ +/* + * Copyright 2006 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 submitted.resultmap; + +import java.io.IOException; +import java.io.Reader; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; + +import junit.framework.TestCase; + +import com.ibatis.common.resources.Resources; +import com.ibatis.sqlmap.client.SqlMapClient; +import com.ibatis.sqlmap.client.SqlMapClientBuilder; + +/** + * @author Jeff Butler + */ +public class PeerListResultTest extends TestCase { + private SqlMapClient sqlMapClient = null; + + protected void setUp() throws Exception { + Connection conn = null; + Statement st = null; + boolean dbCreated = true; + + try { + Class.forName("org.hsqldb.jdbcDriver"); + conn = DriverManager.getConnection("jdbc:hsqldb:mem:aname", "sa", ""); + st = conn.createStatement(); + st.execute("drop table Person if exists"); + st.execute("create table Person(personId int not null, name varchar(50), primary key (personId))"); + + st.execute("drop table PhoneNumber if exists"); + st.execute("create table PhoneNumber(personId int not null, phoneId int not null, phoneNumber varchar(50), primary key (personId, phoneId))"); + + st.execute("drop table Address if exists"); + st.execute("create table Address(personId int not null, addressId int not null, address varchar(50), primary key (personId, addressId))"); + + // setup Fred + st.execute("insert into Person (personId, name) values(1, 'Fred')"); + st.execute("insert into PhoneNumber (personId, phoneId, phoneNumber) values(1, 1, '111-2222')"); + st.execute("insert into PhoneNumber (personId, phoneId, phoneNumber) values(1, 2, '333-4444')"); + st.execute("insert into Address (personId, addressId, address) values(1, 1, 'Main Street')"); + + // setup Wilma + st.execute("insert into Person (personId, name) values(2, 'Wilma')"); + st.execute("insert into PhoneNumber (personId, phoneId, phoneNumber) values(2, 1, '555-6666')"); + st.execute("insert into Address (personId, addressId, address) values(2, 1, 'Elm Street')"); + st.execute("insert into Address (personId, addressId, address) values(2, 2, 'Maple Lane')"); + + } catch (Exception e) { + fail(e.getMessage()); + dbCreated = false; + } finally { + try { + if (st != null) { + st.close(); + } + + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + // ignore + ; + } + } + + if (dbCreated) { + String resource = "submitted/resultmap/TestSqlMapConfig.xml"; + try { + Reader reader = Resources.getResourceAsReader(resource); + sqlMapClient = SqlMapClientBuilder + .buildSqlMapClient(reader); + } catch (IOException e) { + fail(e.getMessage()); + sqlMapClient = null; + } + } + } + + public void test01() { + + try { + List list = sqlMapClient.queryForList("TestSqlMap.test01", null); + + System.out.println(list); + + assertEquals(2, list.size()); + Person person = (Person) list.get(0); + assertEquals("Fred", person.getName()); + assertEquals(person.getAddresses().size(), 1); + assertEquals(person.getPhoneNumbers().size(), 2); + + person = (Person) list.get(1); + assertEquals("Wilma", person.getName()); + assertEquals(person.getAddresses().size(), 2); + assertEquals(person.getPhoneNumbers().size(), 1); + } catch (Exception e) { + fail(e.getMessage()); + } + } +} Added: ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/Person.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/Person.java?rev=686445&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/Person.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/Person.java Fri Aug 15 20:57:49 2008 @@ -0,0 +1,67 @@ +package submitted.resultmap; + +import java.util.List; + +public class Person { + private Integer id; + private String name; + private List phoneNumbers; + private List addresses; + + public List getAddresses() { + return addresses; + } + public void setAddresses(List addresses) { + this.addresses = addresses; + } + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public List getPhoneNumbers() { + return phoneNumbers; + } + public void setPhoneNumbers(List phoneNumbers) { + this.phoneNumbers = phoneNumbers; + } + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("Name: "); + sb.append(name); + + if (phoneNumbers != null && phoneNumbers.size() > 0) { + sb.append(" Phone Numbers: <"); + + for (int i = 0; i < phoneNumbers.size(); i++) { + if (i > 0) { + sb.append(" "); + } + sb.append(phoneNumbers.get(i)); + } + sb.append(">"); + } + + if (addresses != null && addresses.size() > 0) { + sb.append(" Addresses: <"); + + for (int i = 0; i < addresses.size(); i++) { + if (i > 0) { + sb.append(" "); + } + sb.append(addresses.get(i)); + } + sb.append(">"); + } + + return sb.toString(); + } + +} Added: ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/PhoneNumber.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/PhoneNumber.java?rev=686445&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/PhoneNumber.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/PhoneNumber.java Fri Aug 15 20:57:49 2008 @@ -0,0 +1,21 @@ +package submitted.resultmap; + +public class PhoneNumber { + private Integer id; + private String number; + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + public String getNumber() { + return number; + } + public void setNumber(String number) { + this.number = number; + } + public String toString() { + return number; + } +} Added: ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/TestSqlMap.xml URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/TestSqlMap.xml?rev=686445&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/TestSqlMap.xml (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/TestSqlMap.xml Fri Aug 15 20:57:49 2008 @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE sqlMap + PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" + "http://ibatis.apache.org/dtd/sql-map-2.dtd"> +<sqlMap namespace="TestSqlMap"> + + <resultMap id="addressResult" class="submitted.resultmap.Address"> + <result property="id" column="addressId"/> + <result property="address" column="address"/> + </resultMap> + + <resultMap id="phoneResult" class="submitted.resultmap.PhoneNumber"> + <result property="id" column="phoneId"/> + <result property="number" column="phoneNumber"/> + </resultMap> + + <resultMap id="personResult" class="submitted.resultmap.Person" groupBy="id"> + <result property="id" column="personId"/> + <result property="name" column="name"/> + <result property="addresses" resultMap="TestSqlMap.addressResult"/> + <result property="phoneNumbers" resultMap="TestSqlMap.phoneResult"/> + </resultMap> + + <select id="test01" resultMap="personResult" > + select personId, name, addressId, address, phoneId, phoneNumber + from Person join PhoneNumber on Person.personId = PhoneNumber.personId + join Address on Person.personId = Address.personId + order by personId + </select> + +</sqlMap> Added: ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/TestSqlMapConfig.xml URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/TestSqlMapConfig.xml?rev=686445&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/TestSqlMapConfig.xml (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-compat/src/test/java/submitted/resultmap/TestSqlMapConfig.xml Fri Aug 15 20:57:49 2008 @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE sqlMapConfig + PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" + "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> + +<sqlMapConfig> + + <settings useStatementNamespaces="true" + cacheModelsEnabled="false" + enhancementEnabled="false" + lazyLoadingEnabled="false"/> + + <transactionManager type="JDBC"> + <dataSource type="SIMPLE"> + <property name="JDBC.Driver" value="org.hsqldb.jdbcDriver"/> + <property name="JDBC.ConnectionURL" value="jdbc:hsqldb:mem:aname"/> + <property name="JDBC.Username" value="sa"/> + <property name="JDBC.Password" value=""/> + <property name="JDBC.DefaultAutoCommit" value="true"/> + + <property value="15" name="Pool.MaximumActiveConnections"/> + <property value="15" name="Pool.MaximumIdleConnections"/> + <property value="1000" name="Pool.MaximumWait"/> + + <property name="useUnicode" value="true"/> + <property name="characterEncoding" value="UTF-8"/> + + <!-- ping query --> + <property name="Pool.PingQuery" value="select 1 from contact2"/> + <property name="Pool.PingEnabled" value="true"/> + <property name="Pool.PingConnectionsNotUsedFor" value="60000"/> + + </dataSource> + </transactionManager> + + <sqlMap resource="submitted/resultmap/TestSqlMap.xml" /> + +</sqlMapConfig>