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.errors();
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.errors(request));
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.errors();
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.runInContext(request, response, new Command() {
164 
165 			public void execute(final HttpServletRequest request,
166 					final HttpServletResponse response) throws Exception {
167 				assertSame(flashMap, ActionUtils.flash());
168 			}
169 		});
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.flash(request));
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.flash();
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 }