(no commit message)
[utils] / support / general / src / test / java / org / wamblee / general / SerializableProxyFactoryTest.java
1 /*
2  * Copyright 2005-2010 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  *      http://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.wamblee.general;
17
18 import static org.mockito.Mockito.*;
19
20 import org.apache.derby.impl.sql.execute.OnceResultSet;
21 import org.junit.Before;
22 import org.junit.Test;
23
24 public class SerializableProxyFactoryTest {
25
26     public static interface MyInterface {
27         void doSomething();
28     }
29
30     private MyInterface intf;
31
32     @Before
33     public void setUp() {
34         intf = mock(MyInterface.class);
35     }
36
37     @Test
38     public void testProxyWorks() {
39         SerializableProxyFactory<MyInterface> factory = new SerializableProxyFactory<MyInterface>(
40             MyInterface.class, intf);
41         MyInterface proxy = factory.getProxy();
42         proxy.doSomething();
43         verify(intf).doSomething();
44     }
45
46     @Test
47     public void testProxiesAreUnique() {
48         SerializableProxyFactory<MyInterface> factory1 = new SerializableProxyFactory<MyInterface>(
49             MyInterface.class, intf);
50         MyInterface proxy1 = factory1.getProxy();
51
52         MyInterface intf2 = mock(MyInterface.class);
53         SerializableProxyFactory<MyInterface> factory2 = new SerializableProxyFactory<MyInterface>(
54             MyInterface.class, intf2);
55         MyInterface proxy2 = factory2.getProxy();
56
57         proxy1.doSomething();
58         verify(intf, times(1)).doSomething();
59         verifyNoMoreInteractions(intf);
60         verifyNoMoreInteractions(intf2);
61
62         reset(intf);
63         reset(intf2);
64
65         proxy2.doSomething();
66         verify(intf2, times(1)).doSomething();
67         verifyNoMoreInteractions(intf);
68         verifyNoMoreInteractions(intf2);
69     }
70
71     @Test
72     public void testStillWorksAfterSerialization() throws Exception {
73         SerializableProxyFactory<MyInterface> factory = new SerializableProxyFactory<MyInterface>(
74             MyInterface.class, intf);
75         MyInterface proxy = factory.getProxy();
76
77         MyInterface deserialized = ObjectSerializationUtils.deserialize(
78             ObjectSerializationUtils.serialize(proxy), MyInterface.class);
79         deserialized.doSomething();
80         verify(intf).doSomething();
81     }
82
83     @Test(expected = IllegalArgumentException.class)
84     public void testWrongServiceType() {
85         SerializableProxyFactory factory = new SerializableProxyFactory(
86             MyInterface.class, "hello");
87     }
88
89     @Test(expected = IllegalArgumentException.class)
90     public void testNotAnInterface() {
91         SerializableProxyFactory factory = new SerializableProxyFactory(
92             String.class, "hello");
93     }
94 }