Home | History | Annotate | Download | only in serialization
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 
      6 package org.mockitousage.serialization;
      7 
      8 import org.junit.Before;
      9 import org.junit.Test;
     10 import org.mockito.Mockito;
     11 import org.mockito.mock.SerializableMode;
     12 import org.mockitousage.IMethods;
     13 import org.mockitoutil.SimplePerRealmReloadingClassLoader;
     14 import org.mockitoutil.SimpleSerializationUtil;
     15 
     16 import java.io.ByteArrayInputStream;
     17 import java.util.Collections;
     18 import java.util.List;
     19 import java.util.concurrent.Callable;
     20 
     21 
     22 public class AcrossClassLoaderSerializationTest {
     23 
     24     public IMethods mock;
     25 
     26     @Before
     27     public void reproduce_CCE_by_creating_a_mock_with_IMethods_before() throws Exception {
     28         mock = Mockito.mock(IMethods.class);
     29     }
     30 
     31     @Test
     32     public void check_that_mock_can_be_serialized_in_a_classloader_and_deserialized_in_another() throws Exception {
     33         byte[] bytes = create_mock_and_serialize_it_in_class_loader_A();
     34 
     35         Object the_deserialized_mock = read_stream_and_deserialize_it_in_class_loader_B(bytes);
     36     }
     37 
     38     private Object read_stream_and_deserialize_it_in_class_loader_B(byte[] bytes) throws Exception {
     39         return new SimplePerRealmReloadingClassLoader(this.getClass().getClassLoader(), isolating_test_classes())
     40                 .doInRealm(
     41                         "org.mockitousage.serialization.AcrossClassLoaderSerializationTest$ReadStreamAndDeserializeIt",
     42                         new Class<?>[]{ byte[].class },
     43                         new Object[]{ bytes }
     44                 );
     45     }
     46 
     47     private byte[] create_mock_and_serialize_it_in_class_loader_A() throws Exception {
     48         return (byte[]) new SimplePerRealmReloadingClassLoader(this.getClass().getClassLoader(), isolating_test_classes())
     49                 .doInRealm("org.mockitousage.serialization.AcrossClassLoaderSerializationTest$CreateMockAndSerializeIt");
     50     }
     51 
     52 
     53     private SimplePerRealmReloadingClassLoader.ReloadClassPredicate isolating_test_classes() {
     54         return new SimplePerRealmReloadingClassLoader.ReloadClassPredicate() {
     55             public boolean acceptReloadOf(String qualifiedName) {
     56                 return qualifiedName.contains("org.mockitousage")
     57                         || qualifiedName.contains("org.mockitoutil")
     58                         ;
     59             }
     60         };
     61     }
     62 
     63 
     64     // see create_mock_and_serialize_it_in_class_loader_A
     65     public static class CreateMockAndSerializeIt implements Callable<byte[]> {
     66         public byte[] call() throws Exception {
     67             AClassToBeMockedInThisTestOnlyAndInCallablesOnly mock = Mockito.mock(
     68                     AClassToBeMockedInThisTestOnlyAndInCallablesOnly.class,
     69                     Mockito.withSettings().serializable(SerializableMode.ACROSS_CLASSLOADERS)
     70             );
     71             // use MethodProxy before
     72             mock.returningSomething();
     73 
     74             return SimpleSerializationUtil.serializeMock(mock).toByteArray();
     75         }
     76     }
     77 
     78     // see read_stream_and_deserialize_it_in_class_loader_B
     79     public static class ReadStreamAndDeserializeIt implements Callable<Object> {
     80         private byte[] bytes;
     81 
     82         public ReadStreamAndDeserializeIt(byte[] bytes) {
     83             this.bytes = bytes;
     84         }
     85 
     86         public Object call() throws Exception {
     87             ByteArrayInputStream to_unserialize = new ByteArrayInputStream(bytes);
     88             return SimpleSerializationUtil.deserializeMock(
     89                     to_unserialize,
     90                     AClassToBeMockedInThisTestOnlyAndInCallablesOnly.class
     91             );
     92         }
     93     }
     94 
     95 
     96     public static class AClassToBeMockedInThisTestOnlyAndInCallablesOnly {
     97         List returningSomething() { return Collections.emptyList(); }
     98     }
     99 }
    100