    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.internal.controller.impl;
    17
    18  import static org.easymock.EasyMock.createMock;
    19  import static org.easymock.EasyMock.eq;
    20  import static org.easymock.EasyMock.expect;
    21  import static org.easymock.EasyMock.isA;
    22  import static org.easymock.EasyMock.replay;
    23  import static org.easymock.EasyMock.verify;
    24
    25  import java.util.ArrayList;
    26  import java.util.Collection;
    27  import java.util.HashMap;
    28  import java.util.List;
    29  import java.util.Map;
    30
    31  import javax.servlet.http.HttpServletRequest;
    32  import javax.servlet.http.HttpServletResponse;
    33
    34  import org.junit.After;
    35  import org.junit.Before;
    36  import org.junit.Test;
    37  import org.seasar.cubby.CubbyConstants;
    38  import org.seasar.cubby.action.ActionContext;
    39  import org.seasar.cubby.action.ActionErrors;
    40  import org.seasar.cubby.action.ActionResult;
    41  import org.seasar.cubby.action.Direct;
    42  import org.seasar.cubby.action.FlashMap;
    43  import org.seasar.cubby.action.impl.ActionContextImpl;
    44  import org.seasar.cubby.action.impl.ActionErrorsImpl;
    45  import org.seasar.cubby.action.impl.FlashMapImpl;
    46  import org.seasar.cubby.controller.RequestParser;
    47  import org.seasar.cubby.controller.impl.DefaultRequestParser;
    48  import org.seasar.cubby.internal.controller.RequestProcessor;
    49  import org.seasar.cubby.mock.MockContainerProvider;
    50  import org.seasar.cubby.plugin.PluginRegistry;
    51  import org.seasar.cubby.plugins.BinderPlugin;
    52  import org.seasar.cubby.routing.PathInfo;
    53  import org.seasar.cubby.routing.Routing;
    54  import org.seasar.cubby.spi.ContainerProvider;
    55  import org.seasar.cubby.spi.RequestParserProvider;
    56  import org.seasar.cubby.spi.container.Container;
    57  import org.seasar.cubby.spi.container.LookupException;
    58  import org.seasar.cubby.spi.impl.AbstractRequestParserProvider;
    59
    60  public class RequestProcessorImplTest {
    61
    62  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
    63
    64  	private RequestProcessor requestProcessor = new RequestProcessorImpl();
    65
    66  	private FlashMap flashMap;
    67
    68  	private ActionErrors actionErrors;
    69
    70  	private ActionContext actionContext;
    71
    72  	@Before
    73  	public void setupProvider() {
    74  		final BinderPlugin binderPlugin = new BinderPlugin();
    75
    76  		final List<RequestParser> requestParsers = new ArrayList<RequestParser>();
    77  		requestParsers.add(new DefaultRequestParser());
    78  		binderPlugin.bind(RequestParserProvider.class).toInstance(
    79  				new AbstractRequestParserProvider() {
    80
    81  					@Override
    82  					protected Collection<RequestParser> getRequestParsers() {
    83  						return requestParsers;
    84  					}
    85
    86  				});
    87  		binderPlugin.bind(ContainerProvider.class).toInstance(
    88  				new MockContainerProvider(new Container() {
    89
    90  					public <T> T lookup(Class<T> type) throws LookupException {
    91  						if (MockAction.class.equals(type)) {
    92  							return type.cast(new MockAction());
    93  						}
    94  						if (ActionErrors.class.equals(type)) {
    95  							return type.cast(actionErrors);
    96  						}
    97  						if (FlashMap.class.equals(type)) {
    98  							return type.cast(flashMap);
    99  						}
    100 						if (ActionContext.class.equals(type)) {
    101 							return type.cast(actionContext);
    102 						}
    103 						throw new LookupException();
    104 					}
    105
    106 				}));
    107
    108 		pluginRegistry.register(binderPlugin);
    109 	}
    110
    111 	@After
    112 	public void teardownProvider() {
    113 		pluginRegistry.clear();
    114 	}
    115
    116 	@Test
    117 	@SuppressWarnings("unchecked")
    118 	public void process() throws Exception {
    119 		Map<String, Object[]> parameterMap = new HashMap<String, Object[]>();
    120 		Routing routing = createMock(Routing.class);
    121 		expect(routing.getActionClass()).andReturn(
    122 				Class.class.cast(MockAction.class));
    123 		expect(routing.getActionMethod()).andReturn(
    124 				MockAction.class.getMethod("execute"));
    125 		HttpServletRequest request = createMock(HttpServletRequest.class);
    126 		expect(request.getParameterMap()).andReturn(parameterMap);
    127 		expect(request.getRequestURI()).andReturn("/context/mock/execute");
    128 		request.setAttribute(CubbyConstants.ATTR_PARAMS, parameterMap);
    129 		request.setAttribute(eq(CubbyConstants.ATTR_ERRORS),
    130 				isA(ActionErrors.class));
    131 		request.setAttribute(eq(CubbyConstants.ATTR_FLASH), isA(Map.class));
    132 		request.setAttribute(eq(CubbyConstants.ATTR_ACTION),
    133 				isA(MockAction.class));
    134 		request.setAttribute(eq(CubbyConstants.ATTR_ACTION_CONTEXT),
    135 				isA(ActionContext.class));
    136 		expect(request.getSession(false)).andReturn(null);
    137 		HttpServletResponse response = createMock(HttpServletResponse.class);
    138 		PathInfo pathInfo = createMock(PathInfo.class);
    139 		expect(pathInfo.dispatch(parameterMap)).andReturn(routing);
    140 		expect(pathInfo.getURIParameters()).andReturn(
    141 				new HashMap<String, String[]>());
    142 		replay(routing, request, response, pathInfo);
    143
    144 		this.actionErrors = new ActionErrorsImpl();
    145 		this.flashMap = new FlashMapImpl(request);
    146 		this.actionContext = new ActionContextImpl();
    147 		actionContext.initialize(null, null, null, actionErrors, flashMap);
    148
    149 		requestProcessor.process(request, response, pathInfo);
    150
    151 		verify(routing, request, response, pathInfo);
    152 	}
    153
    154 	public static class MockAction {
    155 		public ActionResult execute() {
    156 			return new Direct();
    157 		}
    158 	}
    159
    160 }