code style improvements.
[utils] / test / enterprise / src / test / java / org / wamblee / test / transactions / 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.test.transactions;
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 import org.wamblee.test.transactions.ThreadSpecificProxyFactory;
26
27 public class ThreadSpecificProxyFactoryTest {
28
29     private static interface Service {
30         int execute(int aX) throws Exception;
31     }
32
33     private ThreadSpecificProxyFactory<Service> factory;
34     private Service proxy;
35
36     @Before
37     public void setUp() {
38         factory = new ThreadSpecificProxyFactory<Service>(Service.class);
39         proxy = factory.getProxy();
40     }
41
42     @After
43     public void tearDown() {
44         // Empty.
45     }
46
47     @Test(expected = NullPointerException.class)
48     public void testNoSvcDefined() throws Exception {
49         proxy.execute(10);
50     }
51
52     @Test
53     public void testInvokeThroughProxy() throws Exception {
54         Service svc = mock(Service.class);
55         when(svc.execute(anyInt())).thenReturn(50);
56         factory.set(svc);
57         assertEquals(50, proxy.execute(10));
58         verify(svc).execute(10);
59     }
60
61     @Test
62     public void testInvokeThroughProxyWithException() throws Exception {
63         Service svc = mock(Service.class);
64         try {
65             when(svc.execute(anyInt())).thenThrow(
66                 new RuntimeException("exception thrown"));
67             factory.set(svc);
68             svc.execute(10);
69             fail();
70         } catch (RuntimeException e) {
71             assertEquals("exception thrown", e.getMessage());
72         }
73     }
74
75     private int returnFromThread;
76
77     @Test
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);
83
84         factory.set(svc1);
85         assertEquals(10, svc1.execute(10));
86         Thread t = new Thread() {
87             public void run() {
88                 factory.set(svc2);
89                 try {
90                     returnFromThread = proxy.execute(100);
91                 } catch (Exception e) {
92                     returnFromThread = 100000;
93                 }
94             };
95         };
96         t.start();
97         t.join();
98         assertEquals(20, returnFromThread);
99         assertEquals(10, proxy.execute(100));
100
101     }
102
103     @Test(expected = IllegalArgumentException.class)
104     public void testNotAnInterface() {
105         ThreadSpecificProxyFactory f = new ThreadSpecificProxyFactory(String.class);
106     }
107 }