    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.tags;
    17
    18  import static org.easymock.EasyMock.anyObject;
    19  import static org.easymock.EasyMock.createMock;
    20  import static org.easymock.EasyMock.expect;
    21  import static org.easymock.EasyMock.expectLastCall;
    22  import static org.easymock.EasyMock.getCurrentArguments;
    23  import static org.easymock.EasyMock.isA;
    24  import static org.easymock.EasyMock.replay;
    25
    26  import java.io.IOException;
    27  import java.io.StringReader;
    28  import java.util.Hashtable;
    29
    30  import javax.servlet.ServletContext;
    31  import javax.servlet.http.HttpServletRequest;
    32  import javax.servlet.http.HttpServletResponse;
    33  import javax.servlet.http.HttpSession;
    34  import javax.servlet.jsp.JspContext;
    35  import javax.servlet.jsp.PageContext;
    36
    37  import org.easymock.IAnswer;
    38  import org.jdom.Document;
    39  import org.jdom.Element;
    40  import org.jdom.JDOMException;
    41  import org.jdom.input.SAXBuilder;
    42  import org.junit.After;
    43  import org.junit.Before;
    44  import org.seasar.cubby.action.impl.ActionErrorsImpl;
    45  import org.seasar.cubby.mock.MockContainerProvider;
    46  import org.seasar.cubby.plugin.PluginRegistry;
    47  import org.seasar.cubby.plugins.BinderPlugin;
    48  import org.seasar.cubby.spi.BeanDescProvider;
    49  import org.seasar.cubby.spi.ContainerProvider;
    50  import org.seasar.cubby.spi.beans.impl.DefaultBeanDescProvider;
    51  import org.seasar.cubby.spi.container.Container;
    52  import org.seasar.cubby.spi.container.LookupException;
    53
    54  public abstract class AbstractTagTestCase {
    55
    56  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
    57
    58  	protected MockJspFragment jspBody;
    59
    60  	protected MockJspContext context;
    61
    62  	private ServletContext servletContext;
    63
    64  	private HttpServletRequest request;
    65
    66  	private HttpServletResponse response;
    67
    68  	private HttpSession session;
    69
    70  	public AbstractTagTestCase() {
    71  		super();
    72  	}
    73
    74  	@Before
    75  	public void setupMocks() throws Exception {
    76  		servletContext = createMock(ServletContext.class);
    77  		response = createMock(HttpServletResponse.class);
    78  		final Hashtable<String, Object> attributes = new Hashtable<String, Object>();
    79  		session = createMock(HttpSession.class);
    80  		expect(session.getAttribute(isA(String.class))).andStubAnswer(
    81  				new IAnswer<Object>() {
    82
    83  					public Object answer() throws Throwable {
    84  						return attributes.get(getCurrentArguments()[0]);
    85  					}
    86
    87  				});
    88  		session.setAttribute(isA(String.class), anyObject());
    89  		expectLastCall().andStubAnswer(new IAnswer<Object>() {
    90
    91  			public Object answer() throws Throwable {
    92  				return attributes.put(String.class
    93  						.cast(getCurrentArguments()[0]),
    94  						getCurrentArguments()[1]);
    95  			}
    96
    97  		});
    98
    99  		request = createMock(HttpServletRequest.class);
    100 		expect(request.getCharacterEncoding()).andStubReturn("UTF-8");
    101 		expect(request.getSession()).andStubReturn(session);
    102 		expect(response.encodeURL(isA(String.class))).andStubAnswer(
    103 				new IAnswer<String>() {
    104
    105 					public String answer() throws Throwable {
    106 						return String.class.cast(getCurrentArguments()[0]);
    107 					}
    108
    109 				});
    110 		expect(request.getRequestURL()).andStubReturn(
    111 				new StringBuffer("http://localhost:8080/test/test"));
    112 		replay(request, response, session, servletContext);
    113
    114 		context = new MockJspContext(servletContext, request, response);
    115 		jspBody = new MockJspFragment();
    116 		jspBody.setJspContext(context);
    117 	}
    118
    119 	@Before
    120 	public void setupContainer() {
    121 		final BinderPlugin binderPlugin = new BinderPlugin();
    122 		binderPlugin.bind(ContainerProvider.class).toInstance(
    123 				new MockContainerProvider(new Container() {
    124
    125 					public <T> T lookup(Class<T> type) throws LookupException {
    126 						return null;
    127 					}
    128
    129 				}));
    130 		binderPlugin.bind(BeanDescProvider.class).toInstance(
    131 				new DefaultBeanDescProvider());
    132 		pluginRegistry.register(binderPlugin);
    133 	}
    134
    135 	@After
    136 	public void teardownPluginRegistry() {
    137 		pluginRegistry.clear();
    138 	}
    139
    140 	protected Element getResultAsElementFromContext() throws JDOMException,
    141 			IOException {
    142 		String result = context.getResult();
    143 		Document document = new SAXBuilder().build(new StringReader(result));
    144 		Element element = document.getRootElement();
    145 		return element;
    146 	}
    147
    148 	protected void setupErrors(JspContext context) {
    149 		ActionErrorsImpl errors = new ActionErrorsImpl();
    150 		context.setAttribute("errors", errors, PageContext.REQUEST_SCOPE);
    151 	}
    152
    153 }