15
15
*/
16
16
package org .apache .ibatis .submitted .lazy_deserialize ;
17
17
18
+ import static org .hamcrest .core .Is .*;
19
+ import static org .junit .Assert .*;
20
+
21
+ import java .io .ByteArrayInputStream ;
22
+ import java .io .ByteArrayOutputStream ;
23
+ import java .io .ObjectInputStream ;
24
+ import java .io .ObjectOutputStream ;
18
25
import java .io .PrintWriter ;
19
26
import java .io .Reader ;
20
27
import java .sql .Connection ;
21
28
import java .sql .DriverManager ;
29
+
30
+ import org .apache .ibatis .executor .ExecutorException ;
22
31
import org .apache .ibatis .io .Resources ;
23
32
import org .apache .ibatis .jdbc .ScriptRunner ;
33
+ import org .apache .ibatis .session .Configuration ;
24
34
import org .apache .ibatis .session .SqlSession ;
25
35
import org .apache .ibatis .session .SqlSessionFactory ;
26
36
import org .apache .ibatis .session .SqlSessionFactoryBuilder ;
27
- import org .junit .BeforeClass ;
37
+ import org .junit .Before ;
38
+ import org .junit .Rule ;
28
39
import org .junit .Test ;
29
- import java .io .ByteArrayInputStream ;
30
- import java .io .ByteArrayOutputStream ;
31
- import java .io .ObjectInputStream ;
32
- import java .io .ObjectOutputStream ;
33
- import org .apache .ibatis .session .Configuration ;
34
- import static org .junit .Assert .*;
40
+ import org .junit .rules .ExpectedException ;
35
41
36
42
/**
37
43
*
40
46
*/
41
47
public final class LazyDeserializeTest {
42
48
49
+ @ Rule
50
+ public ExpectedException expectedException = ExpectedException .none ();
51
+
43
52
private static final int FOO_ID = 1 ;
44
53
private static final int BAR_ID = 10 ;
45
54
private static SqlSessionFactory factory ;
@@ -48,8 +57,8 @@ public static Configuration getConfiguration() {
48
57
return factory .getConfiguration ();
49
58
}
50
59
51
- @ BeforeClass
52
- public static void setupClass () throws Exception {
60
+ @ Before
61
+ public void setupClass () throws Exception {
53
62
Connection conn = null ;
54
63
55
64
try {
@@ -79,6 +88,7 @@ public static void setupClass() throws Exception {
79
88
80
89
@ Test
81
90
public void testLoadLazyDeserialize () throws Exception {
91
+ factory .getConfiguration ().setConfigurationFactory (this .getClass ());
82
92
final SqlSession session = factory .openSession ();
83
93
try {
84
94
final Mapper mapper = session .getMapper (Mapper .class );
@@ -96,6 +106,24 @@ public void testLoadLazyDeserialize() throws Exception {
96
106
}
97
107
}
98
108
109
+ @ Test
110
+ public void testLoadLazyDeserializeWithoutConfigurationFactory () throws Exception {
111
+ expectedException .expect (ExecutorException .class );
112
+ expectedException
113
+ .expectMessage (is ("Cannot get Configuration as configuration factory was not set." ));
114
+
115
+ final SqlSession session = factory .openSession ();
116
+ try {
117
+ final Mapper mapper = session .getMapper (Mapper .class );
118
+ final LazyObjectFoo foo = mapper .loadFoo (FOO_ID );
119
+ final byte [] serializedFoo = this .serializeFoo (foo );
120
+ final LazyObjectFoo deserializedFoo = this .deserializeFoo (serializedFoo );
121
+ deserializedFoo .getLazyObjectBar ();
122
+ } finally {
123
+ session .close ();
124
+ }
125
+ }
126
+
99
127
private byte [] serializeFoo (final LazyObjectFoo foo ) throws Exception {
100
128
final ByteArrayOutputStream bos = new ByteArrayOutputStream ();
101
129
final ObjectOutputStream oos = new ObjectOutputStream (bos );
0 commit comments