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.internal.controller.impl;
18  
19  import static org.easymock.EasyMock.createMock;
20  import static org.easymock.EasyMock.eq;
21  import static org.easymock.EasyMock.expect;
22  import static org.easymock.EasyMock.isA;
23  import static org.easymock.EasyMock.replay;
24  import static org.easymock.EasyMock.verify;
25  
26  import java.lang.reflect.Method;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletRequestWrapper;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.seasar.cubby.CubbyConstants;
35  import org.seasar.cubby.action.ActionClass;
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.action.FlashMap;
40  import org.seasar.cubby.internal.controller.ActionProcessor;
41  import org.seasar.cubby.internal.controller.ActionResultWrapper;
42  import org.seasar.cubby.mock.MockContainerProvider;
43  import org.seasar.cubby.plugin.PluginRegistry;
44  import org.seasar.cubby.plugins.BinderPlugin;
45  import org.seasar.cubby.routing.Routing;
46  import org.seasar.cubby.spi.ContainerProvider;
47  import org.seasar.cubby.spi.container.Container;
48  
49  public class ActionProcessorImplTest {
50  
51  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
52  
53  	FooAction action = new FooAction();
54  
55  	@Before
56  	public void setup() {
57  		BinderPlugin binderPlugin = new BinderPlugin();
58  		binderPlugin.bind(ContainerProvider.class).toInstance(
59  				new MockContainerProvider(new Container() {
60  
61  					public <T> T lookup(final Class<T> type) {
62  						if (FooAction.class.equals(type)) {
63  							return type.cast(action);
64  						}
65  
66  						return null;
67  					}
68  
69  				}));
70  		pluginRegistry.register(binderPlugin);
71  
72  	}
73  
74  	@Test
75  	@SuppressWarnings("unchecked")
76  	public void constructor() throws Throwable {
77  		Class<?> actionClass = action.getClass();
78  		Method actionMethod = actionClass.getMethod("foo");
79  
80  		HttpServletRequest request = createMock(HttpServletRequest.class);
81  		expect(request.getRequestURI()).andReturn("http://example.com/ctx/foo");
82  		request.setAttribute(eq(CubbyConstants.ATTR_ACTION), eq(action));
83  		request.setAttribute(eq(CubbyConstants.ATTR_ACTION_CONTEXT),
84  				isA(ActionContext.class));
85  		expect(request.getAttribute(CubbyConstants.ATTR_ERRORS))
86  				.andReturn(null);
87  		request.setAttribute(eq(CubbyConstants.ATTR_ERRORS),
88  				isA(ActionErrors.class));
89  		expect(request.getAttribute(CubbyConstants.ATTR_FLASH)).andReturn(null);
90  		request
91  				.setAttribute(eq(CubbyConstants.ATTR_FLASH),
92  						isA(FlashMap.class));
93  		request.setAttribute("assert", "ok");
94  		expect(request.getSession(false)).andReturn(null);
95  		HttpServletResponse response = createMock(HttpServletResponse.class);
96  		Routing routing = createMock(Routing.class);
97  		expect(routing.getActionClass()).andReturn((Class) actionClass);
98  		expect(routing.getActionMethod()).andReturn(actionMethod);
99  		replay(request, response, routing);
100 
101 		ActionProcessor actionProcessor = new ActionProcessorImpl();
102 		HttpServletRequest wrapper = new HttpServletRequestWrapper(request);
103 		ActionResultWrapper actionResultWrapper = actionProcessor.process(
104 				wrapper, response, routing);
105 		actionResultWrapper.execute(request, response);
106 
107 		verify(request, response, routing);
108 	}
109 
110 	@ActionClass
111 	public static class FooAction {
112 		public ActionResult foo() {
113 			return new ActionResult() {
114 
115 				public void execute(ActionContext actionContext,
116 						HttpServletRequest request, HttpServletResponse response)
117 						throws Exception {
118 					request.setAttribute("assert", "ok");
119 				}
120 			};
121 		}
122 	}
123 
124 }