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