Skip to content

Commit 6079532

Browse files
committed
New test that causes an NPE due to unresolved type handler
1 parent 501b3b7 commit 6079532

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.apache.ibatis.submitted.blobtest;
2+
3+
import java.util.List;
4+
5+
public interface BlobMapper {
6+
int insert(BlobRecord blobRecord);
7+
List<BlobRecord> selectAll();
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3+
<mapper namespace="org.apache.ibatis.submitted.blobtest.BlobMapper">
4+
<resultMap type="org.apache.ibatis.submitted.blobtest.BlobRecord" id="blobRecordResult">
5+
<constructor>
6+
<idArg column="id" javaType="int"/>
7+
<arg column="blob" javaType="byte[]"/>
8+
</constructor>
9+
</resultMap>
10+
11+
<insert id="insert" parameterType="org.apache.ibatis.submitted.blobtest.BlobRecord">
12+
insert into blobtest.blobs values (#{id}, #{blob})
13+
</insert>
14+
15+
<select id="selectAll" resultMap="blobRecordResult">
16+
select * from blobtest.blobs
17+
</select>
18+
</mapper>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.apache.ibatis.submitted.blobtest;
2+
3+
public class BlobRecord {
4+
private int id;
5+
private byte[] blob;
6+
7+
public BlobRecord(int id, byte[] blob) {
8+
super();
9+
this.id = id;
10+
this.blob = blob;
11+
}
12+
13+
public int getId() {
14+
return id;
15+
}
16+
17+
public byte[] getBlob() {
18+
return blob;
19+
}
20+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.apache.ibatis.submitted.blobtest;
2+
3+
import static junit.framework.Assert.assertEquals;
4+
5+
import java.io.Reader;
6+
import java.sql.Connection;
7+
import java.sql.DriverManager;
8+
import java.util.List;
9+
10+
import org.apache.ibatis.io.Resources;
11+
import org.apache.ibatis.jdbc.ScriptRunner;
12+
import org.apache.ibatis.session.SqlSession;
13+
import org.apache.ibatis.session.SqlSessionFactory;
14+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
15+
import org.junit.BeforeClass;
16+
import org.junit.Ignore;
17+
import org.junit.Test;
18+
19+
public class BlobTest {
20+
private static SqlSessionFactory sqlSessionFactory;
21+
22+
@BeforeClass
23+
public static void initDatabase() throws Exception {
24+
Connection conn = null;
25+
26+
try {
27+
Class.forName("org.hsqldb.jdbcDriver");
28+
conn = DriverManager.getConnection("jdbc:hsqldb:mem:blobtest", "sa",
29+
"");
30+
31+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/blobtest/CreateDB.sql");
32+
33+
ScriptRunner runner = new ScriptRunner(conn);
34+
runner.setLogWriter(null);
35+
runner.setErrorLogWriter(null);
36+
runner.runScript(reader);
37+
conn.commit();
38+
reader.close();
39+
40+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/blobtest/MapperConfig.xml");
41+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42+
reader.close();
43+
} finally {
44+
if (conn != null) {
45+
conn.close();
46+
}
47+
}
48+
}
49+
50+
@Test
51+
@Ignore("Breaks the build currently due to NPE at selectAll")
52+
public void testInsertBlobThenSelectAll() {
53+
SqlSession sqlSession = sqlSessionFactory.openSession();
54+
try {
55+
BlobMapper blobMapper = sqlSession.getMapper(BlobMapper.class);
56+
57+
byte[] myblob = new byte[] {1, 2, 3, 4, 5};
58+
BlobRecord blobRecord = new BlobRecord(1, myblob);
59+
int rows = blobMapper.insert(blobRecord);
60+
assertEquals(1, rows);
61+
62+
// NPE here due to unresolved type handler
63+
List<BlobRecord> results = blobMapper.selectAll();
64+
65+
assertEquals(1, results.size());
66+
BlobRecord result = results.get(0);
67+
assertEquals (blobRecord.getId(), result.getId());
68+
assertEquals (blobRecord.getBlob(), result.getBlob());
69+
} finally {
70+
sqlSession.close();
71+
}
72+
}
73+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
drop table blobtest.blobs is exists;
2+
drop schema blobtest if exists;
3+
4+
create schema blobtest;
5+
6+
create table blobtest.blobs (
7+
id int not null,
8+
blob longvarbinary,
9+
primary key (id)
10+
);

0 commit comments

Comments
 (0)