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