1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.seasar.cubby.internal.controller;
18
19 import static org.easymock.EasyMock.createMock;
20 import static org.easymock.EasyMock.replay;
21 import static org.easymock.EasyMock.verify;
22 import static org.junit.Assert.assertSame;
23 import static org.junit.Assert.fail;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.seasar.cubby.controller.MessagesBehaviour;
32 import org.seasar.cubby.controller.impl.DefaultMessagesBehaviour;
33 import org.seasar.cubby.mock.MockContainerProvider;
34 import org.seasar.cubby.plugin.PluginRegistry;
35 import org.seasar.cubby.plugins.BinderPlugin;
36 import org.seasar.cubby.spi.ContainerProvider;
37 import org.seasar.cubby.spi.container.Container;
38 import org.seasar.cubby.spi.container.LookupException;
39
40 public class ThreadContextTest {
41
42 private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
43
44 @Before
45 public void setup() {
46 final BinderPlugin binderPlugin = new BinderPlugin();
47 binderPlugin.bind(ContainerProvider.class).toInstance(
48 new MockContainerProvider(new Container() {
49
50 public <T> T lookup(final Class<T> type) {
51 if (MessagesBehaviour.class.equals(type)) {
52 return type.cast(new DefaultMessagesBehaviour());
53 }
54 throw new LookupException();
55 }
56
57 }));
58 pluginRegistry.register(binderPlugin);
59 }
60
61 @After
62 public void teardown() {
63 pluginRegistry.clear();
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 @Test
151 public void getRequest() throws Exception {
152 final HttpServletRequest request = createMock(HttpServletRequest.class);
153 final HttpServletResponse response = createMock(HttpServletResponse.class);
154 replay(request, response);
155
156 ThreadContext.enter(request, response);
157 try {
158 final ThreadContext currentContext = ThreadContext
159 .getCurrentContext();
160 assertSame("ThreadContext.getRequest()", request, currentContext
161 .getRequest());
162 assertSame("ThreadContext.getResponse()", response, currentContext
163 .getResponse());
164 } finally {
165 ThreadContext.exit();
166 }
167 ThreadContext.remove();
168 verify(request);
169 }
170
171 @Test
172 public void lifeCycle() throws Exception {
173 final HttpServletRequest request1 = createMock(HttpServletRequest.class);
174 final HttpServletResponse response1 = createMock(HttpServletResponse.class);
175 final HttpServletRequest request2 = createMock(HttpServletRequest.class);
176 final HttpServletResponse response2 = createMock(HttpServletResponse.class);
177 replay(request1, response1, request2, response2);
178
179 ThreadContext.enter(request1, response1);
180 try {
181 assertSame(request1, ThreadContext.getCurrentContext().getRequest());
182 assertSame(response1, ThreadContext.getCurrentContext()
183 .getResponse());
184
185 ThreadContext.enter(request2, response2);
186 try {
187 assertSame(request2, ThreadContext.getCurrentContext()
188 .getRequest());
189 assertSame(response2, ThreadContext.getCurrentContext()
190 .getResponse());
191 } finally {
192 ThreadContext.exit();
193 }
194 assertSame(request1, ThreadContext.getCurrentContext().getRequest());
195 assertSame(response1, ThreadContext.getCurrentContext()
196 .getResponse());
197
198 try {
199 ThreadContext.enter(request2, response2);
200 try {
201 assertSame(request2, ThreadContext.getCurrentContext()
202 .getRequest());
203 assertSame(response2, ThreadContext.getCurrentContext()
204 .getResponse());
205 throw new Exception();
206 } finally {
207 ThreadContext.exit();
208 }
209 } catch (Exception e) {
210 assertSame(request1, ThreadContext.getCurrentContext()
211 .getRequest());
212 assertSame(response1, ThreadContext.getCurrentContext()
213 .getResponse());
214 }
215 } finally {
216 ThreadContext.exit();
217 }
218 ThreadContext.remove();
219
220 try {
221 ThreadContext.getCurrentContext().getRequest();
222 fail();
223 } catch (IllegalStateException e) {
224
225 }
226 try {
227 ThreadContext.getCurrentContext().getResponse();
228 fail();
229 } catch (IllegalStateException e) {
230
231 }
232
233 try {
234 ThreadContext.enter(request1, response1);
235 try {
236 assertSame(request1, ThreadContext.getCurrentContext()
237 .getRequest());
238 assertSame(response1, ThreadContext.getCurrentContext()
239 .getResponse());
240 throw new Exception();
241 } finally {
242 ThreadContext.exit();
243 }
244 } catch (Exception e) {
245 assertSame(request1, ThreadContext.getCurrentContext().getRequest());
246 assertSame(response1, ThreadContext.getCurrentContext()
247 .getResponse());
248 }
249 ThreadContext.remove();
250
251 try {
252 ThreadContext.getCurrentContext().getRequest();
253 fail();
254 } catch (IllegalStateException e) {
255
256 }
257 try {
258 ThreadContext.getCurrentContext().getResponse();
259 fail();
260 } catch (IllegalStateException e) {
261
262 }
263
264 verify(request1, response1, request2, response2);
265 }
266 }