    1   /*
    2    * Copyright 2004-2009 the Seasar Foundation and the Others.
    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,
    13   * either express or implied. See the License for the specific language
    14   * governing permissions and limitations under the License.
    15   */
    16  package org.seasar.cubby.internal.controller;
    17
    18  import static org.easymock.EasyMock.createMock;
    19  import static org.easymock.EasyMock.expect;
    20  import static org.easymock.EasyMock.replay;
    21  import static org.easymock.EasyMock.verify;
    22  import static org.junit.Assert.assertEquals;
    23  import static org.junit.Assert.assertSame;
    24  import static org.junit.Assert.assertTrue;
    25  import static org.junit.Assert.fail;
    26
    27  import java.util.Locale;
    28  import java.util.Map;
    29  import java.util.PropertyResourceBundle;
    30
    31  import javax.servlet.http.HttpServletRequest;
    32  import javax.servlet.http.HttpServletResponse;
    33
    34  import org.junit.After;
    35  import org.junit.Before;
    36  import org.junit.Test;
    37  import org.seasar.cubby.controller.MessagesBehaviour;
    38  import org.seasar.cubby.controller.impl.DefaultMessagesBehaviour;
    39  import org.seasar.cubby.internal.controller.ThreadContext.Command;
    40  import org.seasar.cubby.mock.MockContainerProvider;
    41  import org.seasar.cubby.plugin.PluginRegistry;
    42  import org.seasar.cubby.plugins.BinderPlugin;
    43  import org.seasar.cubby.spi.ContainerProvider;
    44  import org.seasar.cubby.spi.container.Container;
    45  import org.seasar.cubby.spi.container.LookupException;
    46
    47  public class ThreadContextTest {
    48
    49  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
    50
    51  	@Before
    52  	public void setup() {
    53  		final BinderPlugin binderPlugin = new BinderPlugin();
    54  		binderPlugin.bind(ContainerProvider.class).toInstance(
    55  				new MockContainerProvider(new Container() {
    56
    57  					public <T> T lookup(final Class<T> type) {
    58  						if (MessagesBehaviour.class.equals(type)) {
    59  							return type.cast(new DefaultMessagesBehaviour());
    60  						}
    61  						throw new LookupException();
    62  					}
    63
    64  				}));
    65  		pluginRegistry.register(binderPlugin);
    66  	}
    67
    68  	@After
    69  	public void teardown() {
    70  		pluginRegistry.clear();
    71  	}
    72
    73  	@Test
    74  	public void getMessagesMap_ja() throws Exception {
    75  		final HttpServletRequest request = createMock(HttpServletRequest.class);
    76  		expect(request.getLocale()).andStubReturn(Locale.JAPANESE);
    77  		final HttpServletResponse response = createMock(HttpServletResponse.class);
    78  		replay(request, response);
    79
    80  		ThreadContext.runInContext(request, response, new Command() {
    81
    82  			public void execute(final HttpServletRequest request,
    83  					final HttpServletResponse response) throws Exception {
    84  				final Map<?, ?> result = ThreadContext.getMessagesMap();
    85  				assertEquals("result.size()", 16, result.size());
    86  				assertEquals("(HashMap) result.get(\"valid.arrayMaxSize\")",
    87  						"{0}は{1}以下選択してください。", result.get("valid.arrayMaxSize"));
    88  			}
    89
    90  		});
    91  	}
    92
    93  	@Test
    94  	public void getMessagesMap_en() throws Exception {
    95  		final HttpServletRequest request = createMock(HttpServletRequest.class);
    96  		expect(request.getLocale()).andStubReturn(Locale.ENGLISH);
    97  		final HttpServletResponse response = createMock(HttpServletResponse.class);
    98  		replay(request, response);
    99
    100 		ThreadContext.runInContext(request, response, new Command() {
    101
    102 			public void execute(final HttpServletRequest request,
    103 					final HttpServletResponse response) throws Exception {
    104 				final Map<?, ?> result = ThreadContext.getMessagesMap();
    105 				assertEquals("result.size()", 16, result.size());
    106 				assertEquals("(HashMap) result.get(\"valid.arrayMaxSize\")",
    107 						"{0} : selects <= {1}.", result
    108 								.get("valid.arrayMaxSize"));
    109 			}
    110 		});
    111 	}
    112
    113 	@Test
    114 	public void getMessagesResourceBundle_ja() throws Exception {
    115 		final HttpServletRequest request = createMock(HttpServletRequest.class);
    116 		expect(request.getLocale()).andStubReturn(Locale.JAPANESE);
    117 		final HttpServletResponse response = createMock(HttpServletResponse.class);
    118 		replay(request, response);
    119
    120 		ThreadContext.runInContext(request, response, new Command() {
    121
    122 			public void execute(final HttpServletRequest request,
    123 					final HttpServletResponse response) throws Exception {
    124 				final PropertyResourceBundle result = (PropertyResourceBundle) ThreadContext
    125 						.getMessagesResourceBundle();
    126 				assertTrue("result.getKeys().hasMoreElements()", result
    127 						.getKeys().hasMoreElements());
    128 			}
    129 		});
    130 	}
    131
    132 	@Test
    133 	public void getMessagesResourceBundle_en() throws Exception {
    134 		final HttpServletRequest request = createMock(HttpServletRequest.class);
    135 		expect(request.getLocale()).andStubReturn(Locale.ENGLISH);
    136 		final HttpServletResponse response = createMock(HttpServletResponse.class);
    137 		replay(request, response);
    138
    139 		ThreadContext.runInContext(request, response, new Command() {
    140
    141 			public void execute(final HttpServletRequest request,
    142 					final HttpServletResponse response) throws Exception {
    143 				final PropertyResourceBundle result = (PropertyResourceBundle) ThreadContext
    144 						.getMessagesResourceBundle();
    145 				assertTrue("result.getKeys().hasMoreElements()", result
    146 						.getKeys().hasMoreElements());
    147 			}
    148 		});
    149 	}
    150
    151 	@Test
    152 	public void getRequest() throws Exception {
    153 		final HttpServletRequest request = createMock(HttpServletRequest.class);
    154 		final HttpServletResponse response = createMock(HttpServletResponse.class);
    155 		replay(request, response);
    156
    157 		ThreadContext.runInContext(request, response, new Command() {
    158 			public void execute(final HttpServletRequest request,
    159 					final HttpServletResponse response) throws Exception {
    160 				assertSame("ThreadContext.getRequest()", request, ThreadContext
    161 						.getRequest());
    162 				assertSame("ThreadContext.getResponse()", response,
    163 						ThreadContext.getResponse());
    164 			}
    165 		});
    166 		verify(request);
    167 	}
    168
    169 	@Test
    170 	public void lifeCycle() throws Exception {
    171 		final HttpServletRequest request1 = createMock(HttpServletRequest.class);
    172 		final HttpServletResponse response1 = createMock(HttpServletResponse.class);
    173 		final HttpServletRequest request2 = createMock(HttpServletRequest.class);
    174 		final HttpServletResponse response2 = createMock(HttpServletResponse.class);
    175 		replay(request1, response1, request2, response2);
    176
    177 		ThreadContext.runInContext(request1, response1, new Command() {
    178
    179 			public void execute(final HttpServletRequest request,
    180 					final HttpServletResponse response) throws Exception {
    181 				assertSame(request, ThreadContext.getRequest());
    182 				assertSame(response, ThreadContext.getResponse());
    183
    184 				ThreadContext.runInContext(request2, response2, new Command() {
    185
    186 					public void execute(final HttpServletRequest request,
    187 							final HttpServletResponse response)
    188 							throws Exception {
    189 						assertSame(request, ThreadContext.getRequest());
    190 						assertSame(response, ThreadContext.getResponse());
    191 					}
    192
    193 				});
    194 				assertSame(request1, ThreadContext.getRequest());
    195 				assertSame(response1, ThreadContext.getResponse());
    196
    197 				try {
    198 					ThreadContext.runInContext(request2, response2,
    199 							new Command() {
    200
    201 								public void execute(
    202 										final HttpServletRequest request,
    203 										final HttpServletResponse response)
    204 										throws Exception {
    205 									assertSame(request, ThreadContext
    206 											.getRequest());
    207 									assertSame(response, ThreadContext
    208 											.getResponse());
    209 									throw new Exception();
    210 								}
    211
    212 							});
    213 				} catch (Exception e) {
    214 					assertSame(request1, ThreadContext.getRequest());
    215 					assertSame(response1, ThreadContext.getResponse());
    216 				}
    217 			}
    218 		});
    219
    220 		try {
    221 			ThreadContext.getRequest();
    222 			fail();
    223 		} catch (final IllegalStateException e) {
    224 			// ok
    225 		}
    226
    227 		try {
    228 			ThreadContext.getResponse();
    229 			fail();
    230 		} catch (final IllegalStateException e) {
    231 			// ok
    232 		}
    233
    234 		try {
    235 			ThreadContext.runInContext(request1, response1, new Command() {
    236
    237 				public void execute(final HttpServletRequest request,
    238 						final HttpServletResponse response) throws Exception {
    239 					assertSame(request, ThreadContext.getRequest());
    240 					assertSame(response, ThreadContext.getResponse());
    241 					throw new Exception();
    242 				}
    243
    244 			});
    245 		} catch (Exception e) {
    246 			try {
    247 				ThreadContext.getRequest();
    248 				fail();
    249 			} catch (final IllegalStateException e1) {
    250 				// ok
    251 			}
    252
    253 			try {
    254 				ThreadContext.getResponse();
    255 				fail();
    256 			} catch (final IllegalStateException e1) {
    257 				// ok
    258 			}
    259 		}
    260
    261 		verify(request1, response1, request2, response2);
    262 	}
    263 }