2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.general;
18 import static org.mockito.Mockito.*;
20 import org.apache.derby.impl.sql.execute.OnceResultSet;
21 import org.junit.Before;
22 import org.junit.Test;
24 public class SerializableProxyFactoryTest {
26 public static interface MyInterface {
30 private MyInterface intf;
34 intf = mock(MyInterface.class);
38 public void testProxyWorks() {
39 SerializableProxyFactory<MyInterface> factory = new SerializableProxyFactory<MyInterface>(
40 MyInterface.class, intf);
41 MyInterface proxy = factory.getProxy();
43 verify(intf).doSomething();
47 public void testProxiesAreUnique() {
48 SerializableProxyFactory<MyInterface> factory1 = new SerializableProxyFactory<MyInterface>(
49 MyInterface.class, intf);
50 MyInterface proxy1 = factory1.getProxy();
52 MyInterface intf2 = mock(MyInterface.class);
53 SerializableProxyFactory<MyInterface> factory2 = new SerializableProxyFactory<MyInterface>(
54 MyInterface.class, intf2);
55 MyInterface proxy2 = factory2.getProxy();
58 verify(intf, times(1)).doSomething();
59 verifyNoMoreInteractions(intf);
60 verifyNoMoreInteractions(intf2);
66 verify(intf2, times(1)).doSomething();
67 verifyNoMoreInteractions(intf);
68 verifyNoMoreInteractions(intf2);
72 public void testStillWorksAfterSerialization() throws Exception {
73 SerializableProxyFactory<MyInterface> factory = new SerializableProxyFactory<MyInterface>(
74 MyInterface.class, intf);
75 MyInterface proxy = factory.getProxy();
77 MyInterface deserialized = ObjectSerializationUtils.deserialize(
78 ObjectSerializationUtils.serialize(proxy), MyInterface.class);
79 deserialized.doSomething();
80 verify(intf).doSomething();
83 @Test(expected = IllegalArgumentException.class)
84 public void testWrongServiceType() {
85 SerializableProxyFactory factory = new SerializableProxyFactory(
86 MyInterface.class, "hello");
89 @Test(expected = IllegalArgumentException.class)
90 public void testNotAnInterface() {
91 SerializableProxyFactory factory = new SerializableProxyFactory(
92 String.class, "hello");