code style improvements.
[utils] / test / enterprise / src / test / java / org / wamblee / support / ThreadSpecificProxyFactoryTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.support;
17
18 import static junit.framework.Assert.*;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
21
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25
26 public class ThreadSpecificProxyFactoryTest {
27
28     private static interface Service {
29         int execute(int aX) throws Exception;
30     }
31
32     private ThreadSpecificProxyFactory<Service> factory;
33     private Service proxy;
34
35     @Before
36     public void setUp() {
37         factory = new ThreadSpecificProxyFactory<Service>(Service.class);
38         proxy = factory.getProxy();
39     }
40
41     @After
42     public void tearDown() {
43         // Empty.
44     }
45
46     @Test(expected = NullPointerException.class)
47     public void testNoSvcDefined() throws Exception {
48         proxy.execute(10);
49     }
50
51     @Test
52     public void testInvokeThroughProxy() throws Exception {
53         Service svc = mock(Service.class);
54         when(svc.execute(anyInt())).thenReturn(50);
55         factory.set(svc);
56         assertEquals(50, proxy.execute(10));
57         verify(svc).execute(10);
58     }
59
60     @Test
61     public void testInvokeThroughProxyWithException() throws Exception {
62         Service svc = mock(Service.class);
63         try {
64             when(svc.execute(anyInt())).thenThrow(
65                 new RuntimeException("exception thrown"));
66             factory.set(svc);
67             svc.execute(10);
68             fail();
69         } catch (RuntimeException e) {
70             assertEquals("exception thrown", e.getMessage());
71         }
72     }
73
74     private int returnFromThread;
75
76     @Test
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);
82
83         factory.set(svc1);
84         assertEquals(10, svc1.execute(10));
85         Thread t = new Thread() {
86             public void run() {
87                 factory.set(svc2);
88                 try {
89                     returnFromThread = proxy.execute(100);
90                 } catch (Exception e) {
91                     returnFromThread = 100000;
92                 }
93             };
94         };
95         t.start();
96         t.join();
97         assertEquals(20, returnFromThread);
98         assertEquals(10, proxy.execute(100));
99
100     }
101
102     @Test(expected = IllegalArgumentException.class)
103     public void testNotAnInterface() {
104         ThreadSpecificProxyFactory f = new ThreadSpecificProxyFactory(String.class);
105     }
106 }