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