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 junit.framework.Assert.*;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.wamblee.general.ThreadSpecificProxyFactory;
27 public class ThreadSpecificProxyFactoryTest {
29 private static interface Service {
30 int execute(int aX) throws Exception;
33 private ThreadSpecificProxyFactory<Service> factory;
34 private Service proxy;
38 factory = new ThreadSpecificProxyFactory<Service>(Service.class);
39 proxy = factory.getProxy();
43 public void tearDown() {
47 @Test(expected = NullPointerException.class)
48 public void testNoSvcDefined() throws Exception {
53 public void testInvokeThroughProxy() throws Exception {
54 Service svc = mock(Service.class);
55 when(svc.execute(anyInt())).thenReturn(50);
57 assertEquals(50, proxy.execute(10));
58 verify(svc).execute(10);
62 public void testInvokeThroughProxyWithException() throws Exception {
63 Service svc = mock(Service.class);
65 when(svc.execute(anyInt())).thenThrow(
66 new RuntimeException("exception thrown"));
70 } catch (RuntimeException e) {
71 assertEquals("exception thrown", e.getMessage());
75 private int returnFromThread;
78 public void testVerifyThreadSpecificUsingTwoThreads() throws Exception {
79 Service svc1 = mock(Service.class);
80 final Service svc2 = mock(Service.class);
81 when(svc1.execute(anyInt())).thenReturn(10);
82 when(svc2.execute(anyInt())).thenReturn(20);
85 assertEquals(10, svc1.execute(10));
86 Thread t = new Thread() {
90 returnFromThread = proxy.execute(100);
91 } catch (Exception e) {
92 returnFromThread = 100000;
98 assertEquals(20, returnFromThread);
99 assertEquals(10, proxy.execute(100));
103 @Test(expected = IllegalArgumentException.class)
104 public void testNotAnInterface() {
105 ThreadSpecificProxyFactory f = new ThreadSpecificProxyFactory(String.class);