    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.util;
    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.assertFalse;
    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.HashMap;
    28  import java.util.Map;
    29
    30  import javax.servlet.http.HttpServletRequest;
    31  import javax.servlet.http.HttpServletResponse;
    32
    33  import org.junit.Test;
    34  import org.seasar.cubby.CubbyConstants;
    35  import org.seasar.cubby.action.Action;
    36  import org.seasar.cubby.action.ActionContext;
    37  import org.seasar.cubby.action.ActionErrors;
    38  import org.seasar.cubby.action.ActionResult;
    39  import org.seasar.cubby.internal.controller.ThreadContext;
    40  import org.seasar.cubby.internal.controller.ThreadContext.Command;
    41
    42  /**
    43   *
    44   * @author baba
    45   */
    46  public class ActionUtilsTest {
    47
    48  	@Test
    49  	public void actionContextFromThredLocal() throws Exception {
    50  		final ActionContext actionContext = createMock(ActionContext.class);
    51  		final HttpServletRequest request = createMock(HttpServletRequest.class);
    52  		expect(request.getAttribute(CubbyConstants.ATTR_ACTION_CONTEXT))
    53  				.andStubReturn(actionContext);
    54  		final HttpServletResponse response = createMock(HttpServletResponse.class);
    55  		replay(actionContext, request, response);
    56
    57  		ThreadContext.runInContext(request, response, new Command() {
    58
    59  			public void execute(final HttpServletRequest request,
    60  					final HttpServletResponse response) throws Exception {
    61  				assertSame(actionContext, ActionUtils.actionContext());
    62  			}
    63
    64  		});
    65
    66  		verify(actionContext, request, response);
    67  	}
    68
    69  	@Test
    70  	public void actionContextFromRequest() throws Exception {
    71  		final ActionContext actionContext = createMock(ActionContext.class);
    72  		final HttpServletRequest request = createMock(HttpServletRequest.class);
    73  		expect(request.getAttribute(CubbyConstants.ATTR_ACTION_CONTEXT))
    74  				.andStubReturn(actionContext);
    75  		final HttpServletResponse response = createMock(HttpServletResponse.class);
    76  		replay(actionContext, request, response);
    77
    78  		assertSame(actionContext, ActionUtils.actionContext(request));
    79
    80  		verify(actionContext, request, response);
    81  	}
    82
    83  	@Test
    84  	public void actionContextThrowsException() throws Exception {
    85  		final ActionContext actionContext = createMock(ActionContext.class);
    86  		final HttpServletRequest request = createMock(HttpServletRequest.class);
    87  		final HttpServletResponse response = createMock(HttpServletResponse.class);
    88  		replay(actionContext, request, response);
    89
    90  		try {
    91  			ActionUtils.actionContext();
    92  			fail();
    93  		} catch (IllegalStateException e) {
    94  			// ok
    95  		}
    96
    97  		verify(actionContext, request, response);
    98  	}
    99
    100 	@Test
    101 	public void errorsFromThrealLocal() throws Exception {
    102 		final ActionContext actionContext = createMock(ActionContext.class);
    103 		final ActionErrors actionErrors = createMock(ActionErrors.class);
    104 		final HttpServletRequest request = createMock(HttpServletRequest.class);
    105 		final HttpServletResponse response = createMock(HttpServletResponse.class);
    106 		replay(actionContext, actionErrors, request, response);
    107
    108 		try {
    109 			ActionUtils.actionContext().getActionErrors();
    110 			fail();
    111 		} catch (IllegalStateException e) {
    112 			// ok
    113 		}
    114
    115 		verify(actionContext, actionErrors, request, response);
    116 	}
    117
    118 	@Test
    119 	public void errorsFromRequest() throws Exception {
    120 		final ActionContext actionContext = createMock(ActionContext.class);
    121 		final ActionErrors actionErrors = createMock(ActionErrors.class);
    122 		expect(actionContext.getActionErrors()).andReturn(actionErrors);
    123 		final HttpServletRequest request = createMock(HttpServletRequest.class);
    124 		expect(request.getAttribute(CubbyConstants.ATTR_ACTION_CONTEXT))
    125 				.andReturn(actionContext);
    126 		final HttpServletResponse response = createMock(HttpServletResponse.class);
    127 		replay(actionContext, actionErrors, request, response);
    128
    129 		assertSame(actionErrors, ActionUtils.actionContext(request)
    130 				.getActionErrors());
    131
    132 		verify(actionContext, actionErrors, request, response);
    133 	}
    134
    135 	@Test
    136 	public void errorsThrowsException() throws Exception {
    137 		final ActionContext actionContext = createMock(ActionContext.class);
    138 		final ActionErrors actionErrors = createMock(ActionErrors.class);
    139 		final HttpServletRequest request = createMock(HttpServletRequest.class);
    140 		final HttpServletResponse response = createMock(HttpServletResponse.class);
    141 		replay(actionContext, actionErrors, request, response);
    142
    143 		try {
    144 			ActionUtils.actionContext().getActionErrors();
    145 			fail();
    146 		} catch (IllegalStateException e) {
    147 			// ok
    148 		}
    149
    150 		verify(actionContext, actionErrors, request, response);
    151 	}
    152
    153 	@Test
    154 	public void flashFromThreadLocal() throws Exception {
    155 		final ActionContext actionContext = createMock(ActionContext.class);
    156 		final Map<String, Object> flashMap = new HashMap<String, Object>();
    157 		expect(actionContext.getFlashMap()).andReturn(flashMap);
    158 		final HttpServletRequest request = createMock(HttpServletRequest.class);
    159 		expect(request.getAttribute(CubbyConstants.ATTR_ACTION_CONTEXT))
    160 				.andReturn(actionContext);
    161 		final HttpServletResponse response = createMock(HttpServletResponse.class);
    162 		replay(actionContext, request, response);
    163
    164 		ThreadContext.runInContext(request, response, new Command() {
    165
    166 			public void execute(final HttpServletRequest request,
    167 					final HttpServletResponse response) throws Exception {
    168 				assertSame(flashMap, ActionUtils.actionContext().getFlashMap());
    169 			}
    170 		});
    171
    172 		verify(actionContext, request, response);
    173 	}
    174
    175 	@Test
    176 	public void flashFromRequest() throws Exception {
    177 		final ActionContext actionContext = createMock(ActionContext.class);
    178 		final Map<String, Object> flashMap = new HashMap<String, Object>();
    179 		expect(actionContext.getFlashMap()).andReturn(flashMap);
    180 		final HttpServletRequest request = createMock(HttpServletRequest.class);
    181 		expect(request.getAttribute(CubbyConstants.ATTR_ACTION_CONTEXT))
    182 				.andReturn(actionContext);
    183 		final HttpServletResponse response = createMock(HttpServletResponse.class);
    184 		replay(actionContext, request, response);
    185
    186 		assertSame(flashMap, ActionUtils.actionContext(request).getFlashMap());
    187
    188 		verify(actionContext, request, response);
    189 	}
    190
    191 	@Test
    192 	public void flashThrowsException() throws Exception {
    193 		final ActionContext actionContext = createMock(ActionContext.class);
    194 		final HttpServletRequest request = createMock(HttpServletRequest.class);
    195 		final HttpServletResponse response = createMock(HttpServletResponse.class);
    196 		replay(actionContext, request, response);
    197
    198 		try {
    199 			ActionUtils.actionContext().getFlashMap();
    200 			fail();
    201 		} catch (IllegalStateException e) {
    202 			// ok
    203 		}
    204
    205 		verify(actionContext, request, response);
    206 	}
    207
    208 	@Test
    209 	public void isActionMethod() throws Exception {
    210 		assertTrue("親クラスのアクションメソッド", ActionUtils
    211 				.isActionMethod(ChildAction.class.getMethod("m1")));
    212 		assertTrue("オーバーライドした親クラスのアクションメソッド", ActionUtils
    213 				.isActionMethod(ChildAction.class.getMethod("m2")));
    214 		assertTrue("子クラスのアクションメソッド", ActionUtils
    215 				.isActionMethod(ChildAction.class.getMethod("m3")));
    216 		assertFalse("メソッドの引数が不正", ActionUtils.isActionMethod(ChildAction.class
    217 				.getMethod("m4", int.class)));
    218 		assertFalse("メソッドの戻り値が不正", ActionUtils.isActionMethod(ChildAction.class
    219 				.getMethod("m5")));
    220 	}
    221
    222 	public abstract class ParentAction extends Action {
    223 		public ActionResult m1() {
    224 			return null;
    225 		}
    226
    227 		public abstract ActionResult m2();
    228 	}
    229
    230 	public class ChildAction extends ParentAction {
    231 		@Override
    232 		public ActionResult m2() {
    233 			return null;
    234 		}
    235
    236 		public ActionResult m3() {
    237 			return null;
    238 		}
    239
    240 		public ActionResult m4(final int value) {
    241 			return null;
    242 		}
    243
    244 		public Object m5() {
    245 			return null;
    246 		}
    247 	}
    248
    249 }