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, callback);
69 proxy = factory.getProxy();
70 Service svc = factory.get();
72 assertSame(initialService, svc);
73 verify(callback).create();
77 public void testInvokeThroughProxyWithException() throws Exception {
78 Service svc = mock(Service.class);
80 when(svc.execute(anyInt())).thenThrow(
81 new RuntimeException("exception thrown"));
85 } catch (RuntimeException e) {
86 assertEquals("exception thrown", e.getMessage());
90 private int returnFromThread;
93 public void testVerifyThreadSpecificUsingTwoThreads() throws Exception {
94 Service svc1 = mock(Service.class);
95 final Service svc2 = mock(Service.class);
96 when(svc1.execute(anyInt())).thenReturn(10);
97 when(svc2.execute(anyInt())).thenReturn(20);
100 assertEquals(10, svc1.execute(10));
101 Thread t = new Thread() {
105 returnFromThread = proxy.execute(100);
106 } catch (Exception e) {
107 returnFromThread = 100000;
113 assertEquals(20, returnFromThread);
114 assertEquals(10, proxy.execute(100));
118 @Test(expected = IllegalArgumentException.class)
119 public void testNotAnInterface() {
120 ThreadSpecificProxyFactory f = new ThreadSpecificProxyFactory(String.class);
124 public void testProxyToStringWhileUninitialized() {
125 String val = proxy.toString();