Index: test/com/ibatis/sqlmap/WholePartTest.java =================================================================== --- test/com/ibatis/sqlmap/WholePartTest.java (revision 0) +++ test/com/ibatis/sqlmap/WholePartTest.java (revision 0) @@ -0,0 +1,76 @@ +package com.ibatis.sqlmap; + +import java.sql.SQLException; +import java.util.Collections; +import java.util.List; + +import testdomain.Part; +import testdomain.Whole; + +public class WholePartTest extends BaseSqlMapTest { + + // SETUP & TEARDOWN + + protected void setUp() throws Exception { + initSqlMap("com/ibatis/sqlmap/maps/WholePart-SqlMap.xml", null); + initScript("scripts/whole-part-relation-init.sql"); + } + + public void testWholePartRelationWithParameterObject() throws SQLException { + Whole whole = (Whole) sqlMap.queryForObject("select-whole-parameter-object", 1); + assertEquals("Whole 1", whole.getName()); + List parts = whole.getParts(); + assertEquals(2, parts.size()); + assertEquals("Part 1", parts.get(0).getName()); + assertEquals("Part 2", parts.get(1).getName()); + } + + public void testWholePartRelationEmptyWithParameterObject() throws SQLException { + Whole whole = (Whole) sqlMap.queryForObject("select-whole-parameter-object", 2); + assertEquals("Whole 2", whole.getName()); + List parts = whole.getParts(); + assertEquals(0, parts.size()); + } + + public void testPartWholeRelationWithParameterObject() throws SQLException { + Part part = (Part) sqlMap.queryForObject("select-part-parameter-object", 1); + assertEquals("Part 1", part.getName()); + assertEquals("Whole 1", part.getWhole().getName()); + } + + public void testPartWholeRelationEmptyWithParameterObject() throws SQLException { + Part part = (Part) sqlMap.queryForObject("select-part-parameter-object", 3); + assertEquals("Part 3", part.getName()); + assertNull(part.getWhole()); + } + + + public void testWholePartRelationWithParameterMap() throws SQLException { + Whole whole = (Whole) sqlMap.queryForObject("select-whole-parameter-map", Collections.singletonMap("id", 1)); + assertEquals("Whole 1", whole.getName()); + List parts = whole.getParts(); + assertEquals(2, parts.size()); + assertEquals("Part 1", parts.get(0).getName()); + assertEquals("Part 2", parts.get(1).getName()); + } + + public void testWholePartRelationEmptyWithParameterMap() throws SQLException { + Whole whole = (Whole) sqlMap.queryForObject("select-whole-parameter-map", Collections.singletonMap("id", 2)); + assertEquals("Whole 2", whole.getName()); + List parts = whole.getParts(); + assertEquals(0, parts.size()); + } + + public void testPartWholeRelationWithParameterMap() throws SQLException { + Part part = (Part) sqlMap.queryForObject("select-part-parameter-map", Collections.singletonMap("id", 1)); + assertEquals("Part 1", part.getName()); + assertEquals("Whole 1", part.getWhole().getName()); + } + + public void testPartWholeRelationEmptyWithParameterMap() throws SQLException { + Part part = (Part) sqlMap.queryForObject("select-part-parameter-map", Collections.singletonMap("id", 3)); + assertEquals("Part 3", part.getName()); + assertNull(part.getWhole()); + } + +} Index: test/com/ibatis/sqlmap/engine/mapping/result/loader/EnhancedLazyResultLoaderTest.java =================================================================== --- test/com/ibatis/sqlmap/engine/mapping/result/loader/EnhancedLazyResultLoaderTest.java (revision 0) +++ test/com/ibatis/sqlmap/engine/mapping/result/loader/EnhancedLazyResultLoaderTest.java (revision 0) @@ -0,0 +1,50 @@ +package com.ibatis.sqlmap.engine.mapping.result.loader; + +import java.sql.SQLException; +import java.util.Collections; + +import junit.framework.TestCase; + +import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl; +import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate; + +public class EnhancedLazyResultLoaderTest extends TestCase { + + public void testNullParameterObjectReturnsNullObject() throws SQLException { + SqlMapClientImpl client = setupMockSqlMapClientImpl(); + EnhancedLazyResultLoader loader = new EnhancedLazyResultLoader(client, "select", null, TestBean.class); + Object object = loader.loadResult(); + assertNull(object); + } + + public void testEmptyParameterMapReturnsNullObject() throws SQLException { + SqlMapClientImpl client = setupMockSqlMapClientImpl(); + EnhancedLazyResultLoader loader = new EnhancedLazyResultLoader(client, "select", Collections.emptyMap(), TestBean.class); + Object object = loader.loadResult(); + assertNull(object); + } + + /** + * Mock a {@link SqlMapClientImpl} which fails if {@link SqlMapClientImpl#queryForObject(String, Object) + * is called. + */ + private SqlMapClientImpl setupMockSqlMapClientImpl() { + SqlMapExecutorDelegate delegate = new SqlMapExecutorDelegate(); + SqlMapClientImpl client = new SqlMapClientImpl(delegate ) { + @Override + public Object queryForObject(String id, Object paramObject) throws SQLException { + // should not be called + fail(); + return null; + } + }; + return client; + } + + /** + * Test bean for {@link EnhancedLazyResultLoader}. + */ + public static class TestBean { + + } +} Index: test/com/ibatis/sqlmap/maps/WholePart-SqlMap.xml =================================================================== --- test/com/ibatis/sqlmap/maps/WholePart-SqlMap.xml (revision 0) +++ test/com/ibatis/sqlmap/maps/WholePart-SqlMap.xml (revision 0) @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Index: test/com/ibatis/sqlmap/maps/WholePartRelation.xml =================================================================== --- test/com/ibatis/sqlmap/maps/WholePartRelation.xml (revision 0) +++ test/com/ibatis/sqlmap/maps/WholePartRelation.xml (revision 0) @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file Index: test/scripts/whole-part-relation-init.sql =================================================================== --- test/scripts/whole-part-relation-init.sql (revision 0) +++ test/scripts/whole-part-relation-init.sql (revision 0) @@ -0,0 +1,31 @@ +-- HSQL DATABASE + +-- Dropping Tables + +drop table part; +drop table whole; + +-- Creating Tables + +create table part ( + id int not null, + whole_id int, + name char(50) not null, + primary key (id) +); + +create table whole ( + id int not null, + name char(50) not null, + primary key (id) +); + +-- Creating Test Data + +insert into whole values(1, 'Whole 1'); +insert into whole values(2, 'Whole 2'); + +insert into part values(1, 1, 'Part 1'); +insert into part values(2, 1, 'Part 2'); +insert into part values(3, null, 'Part 3'); + Index: test/testdomain/Part.java =================================================================== --- test/testdomain/Part.java (revision 0) +++ test/testdomain/Part.java (revision 0) @@ -0,0 +1,37 @@ +package testdomain; + +public class Part { + + private int id; + private String name; + private Whole whole; + + + public Part() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name.trim(); + } + + public void setName(String name) { + this.name = name; + } + + public Whole getWhole() { + return whole; + } + + public void setWhole(Whole whole) { + this.whole = whole; + } + +} Index: test/testdomain/Whole.java =================================================================== --- test/testdomain/Whole.java (revision 0) +++ test/testdomain/Whole.java (revision 0) @@ -0,0 +1,38 @@ +package testdomain; + +import java.util.List; + +public class Whole { + + private int id; + private String name; + private List parts; + + public Whole() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name.trim(); + } + + public void setName(String name) { + this.name = name; + } + + public List getParts() { + return parts; + } + + public void setParts(List parts) { + this.parts = parts; + } + +}