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.support;
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;
26 public class ThreadSpecificProxyFactoryTest {
28 private static interface Service {
29 int execute(int aX) throws Exception;
32 private ThreadSpecificProxyFactory<Service> factory;
33 private Service proxy;
37 factory = new ThreadSpecificProxyFactory<Service>(Service.class);
38 proxy = factory.getProxy();
42 public void tearDown() {
46 @Test(expected = NullPointerException.class)
47 public void testNoSvcDefined() throws Exception {
52 public void testInvokeThroughProxy() throws Exception {
53 Service svc = mock(Service.class);
54 when(svc.execute(anyInt())).thenReturn(50);
56 assertEquals(50, proxy.execute(10));
57 verify(svc).execute(10);
61 public void testInvokeThroughProxyWithException() throws Exception {
62 Service svc = mock(Service.class);
64 when(svc.execute(anyInt())).thenThrow(
65 new RuntimeException("exception thrown"));
69 } catch (RuntimeException e) {
70 assertEquals("exception thrown", e.getMessage());
74 private int returnFromThread;
77 public void testVerifyThreadSpecificUsingTwoThreads() throws Exception {
78 Service svc1 = mock(Service.class);
79 final Service svc2 = mock(Service.class);
80 when(svc1.execute(anyInt())).thenReturn(10);
81 when(svc2.execute(anyInt())).thenReturn(20);
84 assertEquals(10, svc1.execute(10));
85 Thread t = new Thread() {
89 returnFromThread = proxy.execute(100);
90 } catch (Exception e) {
91 returnFromThread = 100000;
97 assertEquals(20, returnFromThread);
98 assertEquals(10, proxy.execute(100));
102 @Test(expected = IllegalArgumentException.class)
103 public void testNotAnInterface() {
104 ThreadSpecificProxyFactory f = new ThreadSpecificProxyFactory(String.class);