X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=test%2Fenterprise%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fsupport%2FThreadSpecificProxyFactoryTest.java;fp=test%2Fenterprise%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fsupport%2FThreadSpecificProxyFactoryTest.java;h=cb03843a0a46702a31ca8f6094a1433b88424621;hb=4c4b45259c4af24c2551c40df72ad776dd037cbe;hp=0000000000000000000000000000000000000000;hpb=dd174318e1f8b2afa8a17ffc7e3775d65d0401ca;p=utils diff --git a/test/enterprise/src/test/java/org/wamblee/support/ThreadSpecificProxyFactoryTest.java b/test/enterprise/src/test/java/org/wamblee/support/ThreadSpecificProxyFactoryTest.java new file mode 100644 index 00000000..cb03843a --- /dev/null +++ b/test/enterprise/src/test/java/org/wamblee/support/ThreadSpecificProxyFactoryTest.java @@ -0,0 +1,106 @@ +/* + * 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.support; + +import javax.xml.ws.Holder; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static junit.framework.TestCase.*; +import static org.mockito.Mockito.*; + +public class ThreadSpecificProxyFactoryTest { + + private static interface Service { + int execute(int aX) throws Exception; + } + + private ThreadSpecificProxyFactory factory; + private Service proxy; + + @Before + public void setUp() { + factory = new ThreadSpecificProxyFactory(Service.class); + proxy = factory.getProxy(); + } + + @After + public void tearDown() { + // Empty. + } + + @Test(expected = NullPointerException.class) + public void testNoSvcDefined() throws Exception { + proxy.execute(10); + } + + @Test + public void testInvokeThroughProxy() throws Exception { + Service svc = mock(Service.class); + when(svc.execute(anyInt())).thenReturn(50); + factory.set(svc); + assertEquals(50, proxy.execute(10)); + verify(svc).execute(10); + } + + @Test + public void testInvokeThroughProxyWithException() throws Exception { + Service svc = mock(Service.class); + try { + when(svc.execute(anyInt())).thenThrow( + new RuntimeException("exception thrown")); + factory.set(svc); + svc.execute(10); + fail(); + } catch (RuntimeException e) { + assertEquals("exception thrown", e.getMessage()); + } + } + + private int returnFromThread; + + @Test + public void testVerifyThreadSpecificUsingTwoThreads() throws Exception { + Service svc1 = mock(Service.class); + final Service svc2 = mock(Service.class); + when(svc1.execute(anyInt())).thenReturn(10); + when(svc2.execute(anyInt())).thenReturn(20); + + factory.set(svc1); + assertEquals(10, svc1.execute(10)); + Thread t = new Thread() { + public void run() { + factory.set(svc2); + try { + returnFromThread = proxy.execute(100); + } catch (Exception e) { + returnFromThread = 100000; + } + }; + }; + t.start(); + t.join(); + assertEquals(20, returnFromThread); + assertEquals(10, proxy.execute(100)); + + } + + @Test(expected = IllegalArgumentException.class) + public void testNotAnInterface() { + ThreadSpecificProxyFactory f = new ThreadSpecificProxyFactory(String.class); + } +}