View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * @since 2011-04-06T10:58:55+0200
41   *
42   * @author Franta Mejta
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 }