bbb395090f0a49351595d24a6284c208a9366be1
[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, callback);
69         proxy = factory.getProxy();
70         Service svc = factory.get();
71         
72         assertSame(initialService, svc); 
73         verify(callback).create();
74     }
75
76     @Test
77     public void testInvokeThroughProxyWithException() throws Exception {
78         Service svc = mock(Service.class);
79         try {
80             when(svc.execute(anyInt())).thenThrow(
81                 new RuntimeException("exception thrown"));
82             factory.set(svc);
83             svc.execute(10);
84             fail();
85         } catch (RuntimeException e) {
86             assertEquals("exception thrown", e.getMessage());
87         }
88     }
89
90     private int returnFromThread;
91
92     @Test
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);
98
99         factory.set(svc1);
100         assertEquals(10, svc1.execute(10));
101         Thread t = new Thread() {
102             public void run() {
103                 factory.set(svc2);
104                 try {
105                     returnFromThread = proxy.execute(100);
106                 } catch (Exception e) {
107                     returnFromThread = 100000;
108                 }
109             };
110         };
111         t.start();
112         t.join();
113         assertEquals(20, returnFromThread);
114         assertEquals(10, proxy.execute(100));
115
116     }
117
118     @Test(expected = IllegalArgumentException.class)
119     public void testNotAnInterface() {
120         ThreadSpecificProxyFactory f = new ThreadSpecificProxyFactory(String.class);
121     }
122     
123     @Test
124     public void testProxyToStringWhileUninitialized() { 
125         String val = proxy.toString();
126     }
127 }