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