1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.lazy_deserialize;
17
18 import static org.junit.jupiter.api.Assertions.*;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.ObjectInputStream;
23 import java.io.ObjectOutputStream;
24 import java.io.Reader;
25
26 import org.apache.ibatis.BaseDataTest;
27 import org.apache.ibatis.executor.ExecutorException;
28 import org.apache.ibatis.io.Resources;
29 import org.apache.ibatis.session.Configuration;
30 import org.apache.ibatis.session.SqlSession;
31 import org.apache.ibatis.session.SqlSessionFactory;
32 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39
40
41 class LazyDeserializeTest {
42
43 private static final int FOO_ID = 1;
44 private static final int BAR_ID = 10;
45 private static SqlSessionFactory factory;
46
47 public static Configuration getConfiguration() {
48 return factory.getConfiguration();
49 }
50
51 @BeforeEach
52 void setupClass() throws Exception {
53 try (Reader reader = Resources
54 .getResourceAsReader("org/apache/ibatis/submitted/lazy_deserialize/ibatisConfig.xml")) {
55 factory = new SqlSessionFactoryBuilder().build(reader);
56 }
57
58 BaseDataTest.runScript(factory.getConfiguration().getEnvironment().getDataSource(),
59 "org/apache/ibatis/submitted/lazy_deserialize/CreateDB.sql");
60 }
61
62 @Test
63 void testLoadLazyDeserialize() throws Exception {
64 factory.getConfiguration().setConfigurationFactory(this.getClass());
65 try (SqlSession session = factory.openSession()) {
66 final Mapper mapper = session.getMapper(Mapper.class);
67 final LazyObjectFoo foo = mapper.loadFoo(FOO_ID);
68
69 final byte[] serializedFoo = this.serializeFoo(foo);
70 final LazyObjectFoo deserializedFoo = this.deserializeFoo(serializedFoo);
71
72 assertNotNull(deserializedFoo);
73 assertEquals(Integer.valueOf(FOO_ID), deserializedFoo.getId());
74 assertNotNull(deserializedFoo.getLazyObjectBar());
75 assertEquals(Integer.valueOf(BAR_ID), deserializedFoo.getLazyObjectBar().getId());
76 }
77 }
78
79 @Test
80 void testLoadLazyDeserializeWithoutConfigurationFactory() throws Exception {
81 try (SqlSession session = factory.openSession()) {
82 final Mapper mapper = session.getMapper(Mapper.class);
83 final LazyObjectFoo foo = mapper.loadFoo(FOO_ID);
84 final byte[] serializedFoo = this.serializeFoo(foo);
85 final LazyObjectFoo deserializedFoo = this.deserializeFoo(serializedFoo);
86 try {
87 deserializedFoo.getLazyObjectBar();
88 fail();
89 } catch (ExecutorException e) {
90 assertTrue(e.getMessage().contains("Cannot get Configuration as configuration factory was not set."));
91 }
92 }
93 }
94
95 private byte[] serializeFoo(final LazyObjectFoo foo) throws Exception {
96 try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
97 ObjectOutputStream oos = new ObjectOutputStream(bos)) {
98 oos.writeObject(foo);
99 return bos.toByteArray();
100 }
101 }
102
103 private LazyObjectFoo deserializeFoo(final byte[] serializedFoo) throws Exception {
104 try (ByteArrayInputStream bis = new ByteArrayInputStream(serializedFoo);
105 ObjectInputStream ios = new ObjectInputStream(bis)) {
106 return (LazyObjectFoo) ios.readObject();
107 }
108 }
109
110 }