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 java.io.IOException;
20  import java.util.List;
21  
22  import javax.servlet.jsp.JspException;
23  import javax.servlet.jsp.tagext.BodyContent;
24  import javax.servlet.jsp.tagext.BodyTag;
25  import javax.servlet.jsp.tagext.JspTag;
26  import javax.servlet.jsp.tagext.SimpleTag;
27  import javax.servlet.jsp.tagext.Tag;
28  
29  abstract class AbstractStandardTagTestCase extends AbstractTagTestCase {
30  
31  	protected void setupBodyTag(BodyTag tag) {
32  		tag.setPageContext(context);
33  	}
34  
35  	protected void doLifecycle(Tag tag) throws JspException, IOException {
36  		doLifecycle(tag, null);
37  	}
38  
39  	protected void doLifecycle(Tag tag, ChildrenFactory childrenFactory)
40  			throws JspException, IOException {
41  		int reuslt = tag.doStartTag();
42  		if (tag instanceof BodyTag) {
43  			BodyTag bodyTag = (BodyTag) tag;
44  			if (reuslt == BodyTag.EVAL_BODY_BUFFERED) {
45  				BodyContent bodyContent = context.pushBody();
46  				bodyTag.setBodyContent(bodyContent);
47  			}
48  			bodyTag.doInitBody();
49  			if (childrenFactory != null) {
50  				List<JspTag> children = childrenFactory.create();
51  				if (children != null) {
52  					for (JspTag child : children) {
53  						if (child instanceof SimpleTag) {
54  							SimpleTag simpleTag = (SimpleTag) child;
55  							simpleTag.setJspBody(jspBody);
56  							simpleTag.setJspContext(context);
57  							simpleTag.setParent(tag);
58  						} else {
59  							throw new UnsupportedOperationException();
60  						}
61  						jspBody.addChild(child);
62  					}
63  				}
64  			}
65  			jspBody.invoke(context.getOut());
66  			bodyTag.doAfterBody();
67  			if (reuslt == BodyTag.EVAL_BODY_BUFFERED) {
68  				context.popBody();
69  			}
70  		}
71  		tag.doEndTag();
72  	}
73  
74  	interface ChildrenFactory {
75  		List<JspTag> create();
76  	}
77  
78  }