1   /*
2    * Copyright 2004-2008 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 java.io.IOException;
19  import java.io.StringReader;
20  
21  import javax.servlet.jsp.JspContext;
22  import javax.servlet.jsp.PageContext;
23  import javax.servlet.jsp.tagext.BodyTag;
24  import javax.servlet.jsp.tagext.SimpleTag;
25  
26  import org.jdom.Document;
27  import org.jdom.Element;
28  import org.jdom.JDOMException;
29  import org.jdom.input.SAXBuilder;
30  import org.seasar.cubby.action.impl.ActionErrorsImpl;
31  import org.seasar.extension.unit.S2TestCase;
32  
33  abstract public class JspTagTestCase extends S2TestCase {
34  	protected MockJspFragment jspBody;
35  	protected MockJspContext context;
36  
37  	@Override
38  	protected void setUp() throws Exception {
39  		super.setUp();
40  		jspBody = new MockJspFragment();
41  		context = new MockJspContext();
42  		jspBody.setJspContext(context);
43  	}
44  
45  	
46  	protected void setupSimpleTag(SimpleTag tag) {
47  		tag.setJspBody(jspBody);
48  		tag.setJspContext(context);
49  	}
50  
51  	protected void setupBodyTag(BodyTag tag) {
52  		tag.setPageContext(context);
53  	}
54  
55  	protected Element getResultAsElementFromContext() throws JDOMException,
56  			IOException {
57  		String result = context.getResult();
58  		Document document = new SAXBuilder().build(new StringReader(result));
59  		Element element = document.getRootElement();
60  		return element;
61  	}
62  
63  	public void setupErrors(JspContext context) {
64  		ActionErrorsImpl errors = new ActionErrorsImpl();
65  		context.setAttribute("errors", errors, PageContext.REQUEST_SCOPE);
66  	}
67  
68  }