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;
26 import org.wamblee.general.ThreadSpecificProxyFactory.CreationCallback;
28 public class ThreadSpecificProxyFactoryTest {
30 private static interface Service {
31 int execute(int aX) throws Exception;
34 private ThreadSpecificProxyFactory<Service> factory;
35 private Service proxy;
39 factory = new ThreadSpecificProxyFactory<Service>(Service.class);
40 proxy = factory.getProxy();
44 public void tearDown() {
48 @Test(expected = NullPointerException.class)
49 public void testNoSvcDefined() throws Exception {
54 public void testInvokeThroughProxy() throws Exception {
55 Service svc = mock(Service.class);
56 when(svc.execute(anyInt())).thenReturn(50);
58 assertEquals(50, proxy.execute(10));
59 verify(svc).execute(10);
63 public void testCreationCallback() throws Exception {
64 CreationCallback callback = mock(CreationCallback.class);
65 Service initialService = mock(Service.class);
66 when(callback.create()).thenReturn(initialService);
68 factory = new ThreadSpecificProxyFactory<Service>(Service.class,
70 proxy = factory.getProxy();
71 Service svc = factory.get();
73 assertSame(initialService, svc);
74 verify(callback).create();
78 public void testInvokeThroughProxyWithException() throws Exception {
79 Service svc = mock(Service.class);
81 when(svc.execute(anyInt())).thenThrow(
82 new RuntimeException("exception thrown"));
86 } catch (RuntimeException e) {
87 assertEquals("exception thrown", e.getMessage());
91 private int returnFromThread;
94 public void testVerifyThreadSpecificUsingTwoThreads() throws Exception {
95 Service svc1 = mock(Service.class);
96 final Service svc2 = mock(Service.class);
97 when(svc1.execute(anyInt())).thenReturn(10);
98 when(svc2.execute(anyInt())).thenReturn(20);
101 assertEquals(10, svc1.execute(10));
102 Thread t = new Thread() {
106 returnFromThread = proxy.execute(100);
107 } catch (Exception e) {
108 returnFromThread = 100000;
114 assertEquals(20, returnFromThread);
115 assertEquals(10, proxy.execute(100));
119 @Test(expected = IllegalArgumentException.class)
120 public void testNotAnInterface() {
121 ThreadSpecificProxyFactory f = new ThreadSpecificProxyFactory(
126 public void testProxyToStringWhileUninitialized() {
127 String val = proxy.toString();