X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fgeneral%2FSerializableProxyFactoryTest.java;fp=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fgeneral%2FSerializableProxyFactoryTest.java;h=5ff747680b54c0da3cb30175b6b598ac98510980;hb=5ddf0de8b6ecb45f2420c9b2e68f82cb7c52dfbb;hp=0000000000000000000000000000000000000000;hpb=fb9750bc8475eb0c6c9525133e5e9b2aac18fadb;p=utils diff --git a/support/general/src/test/java/org/wamblee/general/SerializableProxyFactoryTest.java b/support/general/src/test/java/org/wamblee/general/SerializableProxyFactoryTest.java new file mode 100644 index 00000000..5ff74768 --- /dev/null +++ b/support/general/src/test/java/org/wamblee/general/SerializableProxyFactoryTest.java @@ -0,0 +1,94 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.general; + +import static org.mockito.Mockito.*; + +import org.apache.derby.impl.sql.execute.OnceResultSet; +import org.junit.Before; +import org.junit.Test; + +public class SerializableProxyFactoryTest { + + public static interface MyInterface { + void doSomething(); + } + + private MyInterface intf; + + @Before + public void setUp() { + intf = mock(MyInterface.class); + } + + @Test + public void testProxyWorks() { + SerializableProxyFactory factory = new SerializableProxyFactory( + MyInterface.class, intf); + MyInterface proxy = factory.getProxy(); + proxy.doSomething(); + verify(intf).doSomething(); + } + + @Test + public void testProxiesAreUnique() { + SerializableProxyFactory factory1 = new SerializableProxyFactory( + MyInterface.class, intf); + MyInterface proxy1 = factory1.getProxy(); + + MyInterface intf2 = mock(MyInterface.class); + SerializableProxyFactory factory2 = new SerializableProxyFactory( + MyInterface.class, intf2); + MyInterface proxy2 = factory2.getProxy(); + + proxy1.doSomething(); + verify(intf, times(1)).doSomething(); + verifyNoMoreInteractions(intf); + verifyNoMoreInteractions(intf2); + + reset(intf); + reset(intf2); + + proxy2.doSomething(); + verify(intf2, times(1)).doSomething(); + verifyNoMoreInteractions(intf); + verifyNoMoreInteractions(intf2); + } + + @Test + public void testStillWorksAfterSerialization() throws Exception { + SerializableProxyFactory factory = new SerializableProxyFactory( + MyInterface.class, intf); + MyInterface proxy = factory.getProxy(); + + MyInterface deserialized = ObjectSerializationUtils.deserialize( + ObjectSerializationUtils.serialize(proxy), MyInterface.class); + deserialized.doSomething(); + verify(intf).doSomething(); + } + + @Test(expected = IllegalArgumentException.class) + public void testWrongServiceType() { + SerializableProxyFactory factory = new SerializableProxyFactory( + MyInterface.class, "hello"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNotAnInterface() { + SerializableProxyFactory factory = new SerializableProxyFactory( + String.class, "hello"); + } +}