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.action.impl;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertSame;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.lang.reflect.Method;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.seasar.cubby.action.Action;
33  import org.seasar.cubby.action.ActionContext;
34  import org.seasar.cubby.action.ActionErrors;
35  import org.seasar.cubby.action.ActionException;
36  import org.seasar.cubby.action.ActionResult;
37  import org.seasar.cubby.action.Form;
38  import org.seasar.cubby.action.InitializeMethod;
39  import org.seasar.cubby.action.PostRenderMethod;
40  import org.seasar.cubby.action.PreRenderMethod;
41  import org.seasar.cubby.action.RequestParameterBindingType;
42  import org.seasar.cubby.plugin.PluginRegistry;
43  import org.seasar.cubby.plugins.BinderPlugin;
44  import org.seasar.cubby.spi.BeanDescProvider;
45  import org.seasar.cubby.spi.beans.PropertyNotFoundException;
46  import org.seasar.cubby.spi.beans.impl.DefaultBeanDescProvider;
47  
48  public class ActionContextImplTest {
49  
50  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
51  
52  	@Before
53  	public void setupProvider() {
54  		final BinderPlugin binderPlugin = new BinderPlugin();
55  		binderPlugin.bind(BeanDescProvider.class).toInstance(
56  				new DefaultBeanDescProvider());
57  		pluginRegistry.register(binderPlugin);
58  	}
59  
60  	@After
61  	public void teardownProvider() {
62  		pluginRegistry.clear();
63  	}
64  
65  	@Test
66  	public void constructWithNormalAction() throws Exception {
67  		final Action action = new NormalAction();
68  		final Class<?> actionClass = action.getClass();
69  		final Method method = action.getClass().getMethod("method1");
70  		final ActionErrors actionErrors = new ActionErrorsImpl();
71  		final Map<String, Object> flashMap = new HashMap<String, Object>();
72  		final ActionContext actionContext = new ActionContextImpl(action,
73  				actionClass, method, actionErrors, flashMap);
74  		assertSame(action, actionContext.getAction());
75  		assertEquals(actionClass, actionContext.getActionClass());
76  		assertEquals(method, actionContext.getActionMethod());
77  		assertSame(actionErrors, actionContext.getActionErrors());
78  		assertEquals(flashMap, actionContext.getFlashMap());
79  
80  		assertSame(action.getErrors(), actionContext.getActionErrors());
81  		assertSame(action.getFlash(), actionContext.getFlashMap());
82  
83  		System.out.println(actionContext);
84  	}
85  
86  	@Test
87  	public void invokeWithNormalAction() throws Exception {
88  		final NormalAction action = new NormalAction();
89  		final Class<?> actionClass = action.getClass();
90  		final Method method = action.getClass().getMethod("method1");
91  		final ActionErrors actionErrors = new ActionErrorsImpl();
92  		final Map<String, Object> flashMap = new HashMap<String, Object>();
93  		final ActionContext actionContext = new ActionContextImpl(action,
94  				actionClass, method, actionErrors, flashMap);
95  
96  		assertFalse(action.isInitialized());
97  		assertFalse(action.isPrerendered());
98  		assertFalse(action.isPostrendered());
99  
100 		actionContext.invokeInitializeMethod();
101 		assertTrue(action.isInitialized());
102 		assertFalse(action.isPrerendered());
103 		assertFalse(action.isPostrendered());
104 
105 		actionContext.invokePreRenderMethod();
106 		assertTrue(action.isInitialized());
107 		assertTrue(action.isPrerendered());
108 		assertFalse(action.isPostrendered());
109 
110 		actionContext.invokePostRenderMethod();
111 		assertTrue(action.isInitialized());
112 		assertTrue(action.isPrerendered());
113 		assertTrue(action.isPostrendered());
114 	}
115 
116 	@Test
117 	public void constructWithPojoAction() throws Exception {
118 		final PojoAction action = new PojoAction();
119 		final Class<?> actionClass = action.getClass();
120 		final Method method = action.getClass().getMethod("method1");
121 		final ActionErrors actionErrors = new ActionErrorsImpl();
122 		final Map<String, Object> flashMap = new HashMap<String, Object>();
123 		final ActionContext actionContext = new ActionContextImpl(action,
124 				actionClass, method, actionErrors, flashMap);
125 		assertSame(action, actionContext.getAction());
126 		assertEquals(actionClass, actionContext.getActionClass());
127 		assertEquals(method, actionContext.getActionMethod());
128 		assertSame(actionErrors, actionContext.getActionErrors());
129 		assertEquals(flashMap, actionContext.getFlashMap());
130 
131 		System.out.println(actionContext);
132 	}
133 
134 	@Test
135 	public void invokeWithPojoAction() throws Exception {
136 		final PojoAction action = new PojoAction();
137 		final Class<?> actionClass = action.getClass();
138 		final Method method = action.getClass().getMethod("method2");
139 		final ActionErrors actionErrors = new ActionErrorsImpl();
140 		final Map<String, Object> flashMap = new HashMap<String, Object>();
141 		final ActionContext actionContext = new ActionContextImpl(action,
142 				actionClass, method, actionErrors, flashMap);
143 
144 		assertFalse(action.isInitialized());
145 		assertFalse(action.isPrerendered());
146 		assertFalse(action.isPostrendered());
147 
148 		actionContext.invokeInitializeMethod();
149 		assertTrue(action.isInitialized());
150 		assertFalse(action.isPrerendered());
151 		assertFalse(action.isPostrendered());
152 
153 		actionContext.invokePreRenderMethod();
154 		assertTrue(action.isInitialized());
155 		assertTrue(action.isPrerendered());
156 		assertFalse(action.isPostrendered());
157 
158 		actionContext.invokePostRenderMethod();
159 		assertTrue(action.isInitialized());
160 		assertTrue(action.isPrerendered());
161 		assertTrue(action.isPostrendered());
162 	}
163 
164 	@Test
165 	public void getForm_noAnnotateMethod() throws Exception {
166 		final FormAction action = new FormAction();
167 		final Class<?> actionClass = action.getClass();
168 		final Method method = FormAction.class.getMethod("noAnnotate");
169 		final ActionErrors actionErrors = new ActionErrorsImpl();
170 		final Map<String, Object> flashMap = new HashMap<String, Object>();
171 		final ActionContext actionContext = new ActionContextImpl(action,
172 				actionClass, method, actionErrors, flashMap);
173 
174 		assertSame(action, actionContext.getFormBean());
175 		assertFalse(actionContext.isBindRequestParameterToAllProperties());
176 	}
177 
178 	@Test
179 	public void getForm_annotateValidFormName() throws Exception {
180 		final FormAction action = new FormAction();
181 		final Class<?> actionClass = action.getClass();
182 		final Method method = FormAction.class
183 				.getMethod("annotateValidFormName");
184 		final ActionErrors actionErrors = new ActionErrorsImpl();
185 		final Map<String, Object> flashMap = new HashMap<String, Object>();
186 		final ActionContext actionContext = new ActionContextImpl(action,
187 				actionClass, method, actionErrors, flashMap);
188 
189 		assertSame(action.getMyForm(), actionContext.getFormBean());
190 		assertTrue(actionContext.isBindRequestParameterToAllProperties());
191 	}
192 
193 	@Test
194 	public void getForm_annotateAllPropertiesBindingType() throws Exception {
195 		final FormAction action = new FormAction();
196 		final Class<?> actionClass = action.getClass();
197 		final Method method = FormAction.class
198 				.getMethod("annotateAllPropertiesBindingType");
199 		final ActionErrors actionErrors = new ActionErrorsImpl();
200 		final Map<String, Object> flashMap = new HashMap<String, Object>();
201 		final ActionContext actionContext = new ActionContextImpl(action,
202 				actionClass, method, actionErrors, flashMap);
203 
204 		assertSame(action, actionContext.getFormBean());
205 		assertTrue(actionContext.isBindRequestParameterToAllProperties());
206 	}
207 
208 	@Test
209 	public void getForm_annotateOnlySpecifiedPropertiesBindingType()
210 			throws Exception {
211 		final FormAction action = new FormAction();
212 		final Class<?> actionClass = action.getClass();
213 		final Method method = FormAction.class
214 				.getMethod("annotateOnlySpecifiedPropertiesBindingType");
215 		final ActionErrors actionErrors = new ActionErrorsImpl();
216 		final Map<String, Object> flashMap = new HashMap<String, Object>();
217 		final ActionContext actionContext = new ActionContextImpl(action,
218 				actionClass, method, actionErrors, flashMap);
219 
220 		assertSame(action.getMyForm(), actionContext.getFormBean());
221 		assertFalse(actionContext.isBindRequestParameterToAllProperties());
222 	}
223 
224 	@Test
225 	public void getForm_annotateNoneBindingType() throws Exception {
226 		final FormAction action = new FormAction();
227 		final Class<?> actionClass = action.getClass();
228 		final Method method = FormAction.class
229 				.getMethod("annotateNoneBindingType");
230 		final ActionErrors actionErrors = new ActionErrorsImpl();
231 		final Map<String, Object> flashMap = new HashMap<String, Object>();
232 		final ActionContext actionContext = new ActionContextImpl(action,
233 				actionClass, method, actionErrors, flashMap);
234 
235 		assertNull(actionContext.getFormBean());
236 		try {
237 			actionContext.isBindRequestParameterToAllProperties();
238 			fail();
239 		} catch (final IllegalStateException e) {
240 			// ok
241 		}
242 
243 	}
244 
245 	@Test
246 	public void getForm_annotateNullFormName() throws Exception {
247 		final FormAction action = new FormAction();
248 		final Class<?> actionClass = action.getClass();
249 		final Method method = FormAction.class
250 				.getMethod("annotateNullFormName");
251 		final ActionErrors actionErrors = new ActionErrorsImpl();
252 		final Map<String, Object> flashMap = new HashMap<String, Object>();
253 		final ActionContext actionContext = new ActionContextImpl(action,
254 				actionClass, method, actionErrors, flashMap);
255 
256 		try {
257 			assertNull(actionContext.getFormBean());
258 			fail();
259 		} catch (final ActionException e) {
260 			// ok
261 		}
262 	}
263 
264 	@Test
265 	public void getForm_annotateNotExistFormName() throws Exception {
266 		final FormAction action = new FormAction();
267 		final Class<?> actionClass = action.getClass();
268 		final Method method = FormAction.class
269 				.getMethod("annotateNotExistFormName");
270 		final ActionErrors actionErrors = new ActionErrorsImpl();
271 		final Map<String, Object> flashMap = new HashMap<String, Object>();
272 		final ActionContext actionContext = new ActionContextImpl(action,
273 				actionClass, method, actionErrors, flashMap);
274 
275 		try {
276 			assertNull(actionContext.getFormBean());
277 			fail();
278 		} catch (final PropertyNotFoundException e) {
279 			// ok
280 		}
281 	}
282 
283 	@Test
284 	public void getForm_annotateThisFormName() throws Exception {
285 		final FormAction action = new FormAction();
286 		final Class<?> actionClass = action.getClass();
287 		final Method method = FormAction.class
288 				.getMethod("annotateThisFormName");
289 		final ActionErrors actionErrors = new ActionErrorsImpl();
290 		final Map<String, Object> flashMap = new HashMap<String, Object>();
291 		final ActionContext actionContext = new ActionContextImpl(action,
292 				actionClass, method, actionErrors, flashMap);
293 
294 		assertSame(action, actionContext.getFormBean());
295 		assertTrue(actionContext.isBindRequestParameterToAllProperties());
296 	}
297 
298 	@Test
299 	public void clearFlash() throws Exception {
300 		final Action action = new NormalAction();
301 		final Class<?> actionClass = action.getClass();
302 		final Method method = action.getClass().getMethod("method1");
303 		final ActionErrors actionErrors = new ActionErrorsImpl();
304 		final Map<String, Object> flashMap = new HashMap<String, Object>();
305 		final ActionContext actionContext = new ActionContextImpl(action,
306 				actionClass, method, actionErrors, flashMap);
307 
308 		action.getFlash().put("key", "value");
309 		assertFalse(actionContext.getFlashMap().isEmpty());
310 		assertFalse(action.getFlash().isEmpty());
311 		actionContext.clearFlash();
312 		assertTrue(actionContext.getFlashMap().isEmpty());
313 		assertTrue(action.getFlash().isEmpty());
314 	}
315 
316 	public static class PojoAction {
317 		public ActionResult method1() {
318 			return null;
319 		}
320 
321 		@InitializeMethod("initialize")
322 		@PreRenderMethod("prerender")
323 		@PostRenderMethod("postrender")
324 		public ActionResult method2() {
325 			return null;
326 		}
327 
328 		private boolean initialized = false;
329 
330 		private boolean prerendered = false;
331 
332 		private boolean postrendered = false;
333 
334 		public void initialize() {
335 			initialized = true;
336 		}
337 
338 		public void prerender() {
339 			prerendered = true;
340 		}
341 
342 		public void postrender() {
343 			postrendered = true;
344 		}
345 
346 		public boolean isInitialized() {
347 			return initialized;
348 		}
349 
350 		public boolean isPrerendered() {
351 			return prerendered;
352 		}
353 
354 		public boolean isPostrendered() {
355 			return postrendered;
356 		}
357 
358 	}
359 
360 	public static class NormalAction extends Action {
361 		public ActionResult method1() {
362 			return null;
363 		}
364 
365 		private boolean initialized = false;
366 
367 		private boolean prerendered = false;
368 
369 		private boolean postrendered = false;
370 
371 		@Override
372 		public void initialize() {
373 			initialized = true;
374 		}
375 
376 		@Override
377 		public void prerender() {
378 			prerendered = true;
379 		}
380 
381 		@Override
382 		public void postrender() {
383 			postrendered = true;
384 		}
385 
386 		public boolean isInitialized() {
387 			return initialized;
388 		}
389 
390 		public boolean isPrerendered() {
391 			return prerendered;
392 		}
393 
394 		public boolean isPostrendered() {
395 			return postrendered;
396 		}
397 
398 	}
399 
400 	public static class FormAction {
401 
402 		private final Object myForm = new Object();
403 
404 		public Object getMyForm() {
405 			return myForm;
406 		}
407 
408 		public Object getNullForm() {
409 			return null;
410 		}
411 
412 		public ActionResult noAnnotate() {
413 			return null;
414 		}
415 
416 		@Form("myForm")
417 		public ActionResult annotateValidFormName() {
418 			return null;
419 		}
420 
421 		@Form(bindingType = RequestParameterBindingType.ALL_PROPERTIES)
422 		public ActionResult annotateAllPropertiesBindingType() {
423 			return null;
424 		}
425 
426 		@Form(value = "myForm", bindingType = RequestParameterBindingType.ONLY_SPECIFIED_PROPERTIES)
427 		public ActionResult annotateOnlySpecifiedPropertiesBindingType() {
428 			return null;
429 		}
430 
431 		@Form(bindingType = RequestParameterBindingType.NONE)
432 		public ActionResult annotateNoneBindingType() {
433 			return null;
434 		}
435 
436 		@Form("nullForm")
437 		public ActionResult annotateNullFormName() {
438 			return null;
439 		}
440 
441 		@Form("illegal")
442 		public ActionResult annotateNotExistFormName() {
443 			return null;
444 		}
445 
446 		@Form("this")
447 		public ActionResult annotateThisFormName() {
448 			return null;
449 		}
450 
451 	}
452 
453 }