+/*
+ * 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<MyInterface> factory = new SerializableProxyFactory<MyInterface>(
+ MyInterface.class, intf);
+ MyInterface proxy = factory.getProxy();
+ proxy.doSomething();
+ verify(intf).doSomething();
+ }
+
+ @Test
+ public void testProxiesAreUnique() {
+ SerializableProxyFactory<MyInterface> factory1 = new SerializableProxyFactory<MyInterface>(
+ MyInterface.class, intf);
+ MyInterface proxy1 = factory1.getProxy();
+
+ MyInterface intf2 = mock(MyInterface.class);
+ SerializableProxyFactory<MyInterface> factory2 = new SerializableProxyFactory<MyInterface>(
+ 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<MyInterface> factory = new SerializableProxyFactory<MyInterface>(
+ 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");
+ }
+}