Skip to content

Commit

Permalink
Merged test for discussion at issue #39 (Thanks @eyalfa)
Browse files Browse the repository at this point in the history
  • Loading branch information
emacarron committed Dec 23, 2015
1 parent 3d1fdb1 commit 256d7f0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2015 the original author or authors.
/*
* Copyright 2009-2012 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.
Expand All @@ -19,6 +19,16 @@ public class Item {
private Integer id;
private String name;

public String toString(){
return new StringBuilder()
.append("Item(")
.append(id)
.append(", ")
.append(name)
.append(" )")
.toString();
}

public Integer getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2015 the original author or authors.
/*
* Copyright 2009-2012 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.
Expand All @@ -20,4 +20,5 @@
public interface Mapper {
List<Person> getPersons();
List<Person> getPersonsWithItemsOrdered();
List<PersonItemPair> getPersonItemPairs();
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,23 @@
where p.id = i.owner
order by i.name
</select>

<select id="getPersonItemPairs" resultMap="personItemPairResult">
select p.id as person_id, p.name as person_name, i.id as item_id, i.name as item_name
from persons p, items i
where p.id = i.owner
order by p.id, i.id
</select>

<resultMap id="personItemPairResult" type="org.apache.ibatis.submitted.nestedresulthandler.PersonItemPair">
<association property="person" javaType="org.apache.ibatis.submitted.nestedresulthandler.Person">
<id property="id" column="person_id" />
<result property="name" column="person_name"/>
</association>
<association property="item" javaType="org.apache.ibatis.submitted.nestedresulthandler.Item">
<id property="id" column="item_id"/>
<result property="name" column="item_name"/>
</association>
</resultMap>

</mapper>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2015 the original author or authors.
/*
* Copyright 2009-2012 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.
Expand Down Expand Up @@ -29,6 +29,7 @@
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class NestedResultHandlerTest {
Expand Down Expand Up @@ -87,7 +88,6 @@ public void testGetPersonWithHandler() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
sqlSession.select("getPersons", new ResultHandler() {
@Override
public void handleResult(ResultContext context) {
Person person = (Person) context.getResultObject();
if ("grandma".equals(person.getName())) {
Expand All @@ -105,7 +105,6 @@ public void testUnorderedGetPersonWithHandler() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
sqlSession.select("getPersonsWithItemsOrdered", new ResultHandler() {
@Override
public void handleResult(ResultContext context) {
Person person = (Person) context.getResultObject();
if ("grandma".equals(person.getName())) {
Expand Down Expand Up @@ -152,4 +151,25 @@ public void testGetPersonOrderedByItem() {
}
}

@Ignore
@Test //reopen issue 39? (not a bug?)
public void testGetPersonItemPairs(){
SqlSession sqlSession = sqlSessionFactory.openSession();
try{
Mapper mapper = sqlSession.getMapper(Mapper.class);
List<PersonItemPair> pairs = mapper.getPersonItemPairs();

Assert.assertNotNull( pairs );
// System.out.println( new StringBuilder().append("selected pairs: ").append(pairs) );

Assert.assertEquals(5, pairs.size() );
Assert.assertNotNull(pairs.get(0).getPerson());
Assert.assertEquals(pairs.get(0).getPerson().getId(), Integer.valueOf(1));
Assert.assertNotNull(pairs.get(0).getItem());
Assert.assertEquals( pairs.get(0).getItem().getId(), Integer.valueOf(1));
} finally{
sqlSession.close();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2015 the original author or authors.
/*
* Copyright 2009-2012 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.
Expand All @@ -22,7 +22,19 @@
public class Person {
private Integer id;
private String name;
private List<Item> items=new ArrayList<Item>();
private List<Item> items=new ArrayList<Item>();

public String toString(){
return new StringBuilder()
.append("Person(")
.append(id)
.append(", ")
.append(name)
.append(", ")
.append(items)
.append(" )")
.toString();
}

public Integer getId() {
return id;
Expand Down

0 comments on commit 256d7f0

Please sign in to comment.