(no commit message)
[utils] / support / general / src / test / java / org / wamblee / general / 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.general;
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.general.ThreadSpecificProxyFactory;
26 import org.wamblee.general.ThreadSpecificProxyFactory.CreationCallback;
27
28 public class ThreadSpecificProxyFactoryTest {
29
30     private static interface Service {
31         int execute(int aX) throws Exception;
32     }
33
34     private ThreadSpecificProxyFactory<Service> factory;
35     private Service proxy;
36
37     @Before
38     public void setUp() {
39         factory = new ThreadSpecificProxyFactory<Service>(Service.class);
40         proxy = factory.getProxy();
41     }
42
43     @After
44     public void tearDown() {
45         // Empty.
46     }
47
48     @Test(expected = NullPointerException.class)
49     public void testNoSvcDefined() throws Exception {
50         proxy.execute(10);
51     }
52
53     @Test
54     public void testInvokeThroughProxy() throws Exception {
55         Service svc = mock(Service.class);
56         when(svc.execute(anyInt())).thenReturn(50);
57         factory.set(svc);
58         assertEquals(50, proxy.execute(10));
59         verify(svc).execute(10);
60     }
61
62     @Test
63     public void testCreationCallback() throws Exception {
64         CreationCallback callback = mock(CreationCallback.class);
65         Service initialService = mock(Service.class);
66         when(callback.create()).thenReturn(initialService);
67
68         factory = new ThreadSpecificProxyFactory<Service>(Service.class,
69             callback);
70         proxy = factory.getProxy();
71         Service svc = factory.get();
72
73         assertSame(initialService, svc);
74         verify(callback).create();
75     }
76
77     @Test
78     public void testInvokeThroughProxyWithException() throws Exception {
79         Service svc = mock(Service.class);
80         try {
81             when(svc.execute(anyInt())).thenThrow(
82                 new RuntimeException("exception thrown"));
83             factory.set(svc);
84             svc.execute(10);
85             fail();
86         } catch (RuntimeException e) {
87             assertEquals("exception thrown", e.getMessage());
88         }
89     }
90
91     private int returnFromThread;
92
93     @Test
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);
99
100         factory.set(svc1);
101         assertEquals(10, svc1.execute(10));
102         Thread t = new Thread() {
103             public void run() {
104                 factory.set(svc2);
105                 try {
106                     returnFromThread = proxy.execute(100);
107                 } catch (Exception e) {
108                     returnFromThread = 100000;
109                 }
110             };
111         };
112         t.start();
113         t.join();
114         assertEquals(20, returnFromThread);
115         assertEquals(10, proxy.execute(100));
116
117     }
118
119     @Test(expected = IllegalArgumentException.class)
120     public void testNotAnInterface() {
121         ThreadSpecificProxyFactory f = new ThreadSpecificProxyFactory(
122             String.class);
123     }
124
125     @Test
126     public void testProxyToStringWhileUninitialized() {
127         String val = proxy.toString();
128     }
129 }