Skip to content

Commit 9d07e69

Browse files
committed
Re-used test for issue #99.
1 parent 5244c0b commit 9d07e69

File tree

3 files changed

+50
-48
lines changed

3 files changed

+50
-48
lines changed

src/test/java/org/apache/ibatis/submitted/resultmapwithassociationstest/Mapper.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020

2121
<mapper namespace="org.apache.ibatis.submitted.resultmapwithassociationstest.Mapper">
2222
<resultMap id="personRM" type="org.apache.ibatis.submitted.resultmapwithassociationstest.Person" >
23-
<id property="id"/>
24-
<association property="address" column="id_address" javaType="org.apache.ibatis.submitted.resultmapwithassociationstest.Address">
23+
<id property="id" column="id"/>
24+
<collection property="addresses" ofType="org.apache.ibatis.submitted.resultmapwithassociationstest.Address">
2525
<id property="id" column="address_id" />
26-
</association>
26+
</collection>
2727
</resultMap>
2828

2929
<select id="findAll" parameterType="map" resultMap="personRM">
30-
SELECT p.id, id_address,
31-
a.id as address_id
30+
SELECT p.id, id_address, a.id as address_id
3231
FROM person p
33-
JOIN address a on a.id = p.id_address
32+
JOIN address a
33+
on a.id = p.id_address
3434
</select>
3535

3636
</mapper>

src/test/java/org/apache/ibatis/submitted/resultmapwithassociationstest/Person.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@
1515
*/
1616
package org.apache.ibatis.submitted.resultmapwithassociationstest;
1717

18+
import java.util.List;
19+
1820
public class Person {
19-
private int id;
20-
private Address address;
21+
private int id;
22+
private List<Address> addresses;
2123

22-
public int getId() {
23-
return id;
24-
}
24+
public int getId() {
25+
return id;
26+
}
2527

26-
public void setId(final int id) {
27-
this.id = id;
28-
}
28+
public void setId(final int id) {
29+
this.id = id;
30+
}
2931

30-
public Address getAddress() {
31-
return address;
32-
}
32+
public List<Address> getAddresses() {
33+
return addresses;
34+
}
3335

34-
public void setAddress(final Address address) {
35-
this.address = address;
36-
}
36+
public void setAddresses(final List<Address> addresses) {
37+
this.addresses = addresses;
38+
}
3739
}

src/test/java/org/apache/ibatis/submitted/resultmapwithassociationstest/ResultMapWithAssociationsTest.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,47 @@
1919
import java.sql.Connection;
2020
import java.util.List;
2121

22-
import org.apache.ibatis.exceptions.PersistenceException;
2322
import org.apache.ibatis.io.Resources;
2423
import org.apache.ibatis.jdbc.ScriptRunner;
2524
import org.apache.ibatis.session.SqlSession;
2625
import org.apache.ibatis.session.SqlSessionFactory;
2726
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
2827
import org.junit.Assert;
28+
import org.junit.BeforeClass;
2929
import org.junit.Test;
3030

3131
public class ResultMapWithAssociationsTest {
3232

33-
private static SqlSessionFactory sqlSessionFactory;
33+
private static SqlSessionFactory sqlSessionFactory;
3434

35-
@Test(expected=PersistenceException.class)
36-
public void testMissingColumn() throws Exception {
37-
// create a SqlSessionFactory
38-
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/resultmapwithassociationstest/mybatis-config.xml");
39-
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40-
reader.close();
35+
@BeforeClass
36+
public static void setUp() throws Exception {
37+
// create a SqlSessionFactory
38+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/resultmapwithassociationstest/mybatis-config.xml");
39+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40+
reader.close();
4141

42-
// populate in-memory database
43-
SqlSession session = sqlSessionFactory.openSession();
44-
Connection conn = session.getConnection();
45-
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/resultmapwithassociationstest/CreateDB.sql");
46-
ScriptRunner runner = new ScriptRunner(conn);
47-
runner.setLogWriter(null);
48-
runner.runScript(reader);
49-
reader.close();
50-
session.close();
51-
}
42+
// populate in-memory database
43+
SqlSession session = sqlSessionFactory.openSession();
44+
Connection conn = session.getConnection();
45+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/resultmapwithassociationstest/CreateDB.sql");
46+
ScriptRunner runner = new ScriptRunner(conn);
47+
runner.setLogWriter(null);
48+
runner.runScript(reader);
49+
reader.close();
50+
session.close();
51+
}
5252

53-
public void shouldFindAllPersonRecordsWithAssociatedAddressRecord() {
54-
SqlSession sqlSession = sqlSessionFactory.openSession();
55-
try {
56-
Mapper mapper = sqlSession.getMapper(Mapper.class);
57-
List<Person> resultList = mapper.findAll();
58-
Assert.assertEquals(3, resultList.size());
59-
}
60-
finally {
61-
sqlSession.close();
62-
}
53+
@Test
54+
public void shouldFindAllPersonRecordsWithAssociatedAddressRecord() {
55+
SqlSession sqlSession = sqlSessionFactory.openSession();
56+
try {
57+
Mapper mapper = sqlSession.getMapper(Mapper.class);
58+
List<Person> resultList = mapper.findAll();
59+
Assert.assertEquals(3, resultList.size());
60+
} finally {
61+
sqlSession.close();
6362
}
63+
}
6464

6565
}

0 commit comments

Comments
 (0)