View Javadoc

1   /*
2    * $Id: ElseIfTagTest.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.views.jsp;
19  
20  import javax.servlet.jsp.tagext.TagSupport;
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.struts2.ServletActionContext;
25  import org.apache.struts2.components.If;
26  
27  import com.mockobjects.servlet.MockJspWriter;
28  import com.mockobjects.servlet.MockPageContext;
29  import com.opensymphony.xwork2.util.ValueStack;
30  import com.opensymphony.xwork2.util.ValueStackFactory;
31  
32  /***
33   * 
34   */
35  public class ElseIfTagTest extends TestCase {
36  
37  	protected MockPageContext pageContext;
38  	protected MockJspWriter jspWriter;
39  	protected ValueStack stack;
40  	
41  	
42  	public void testIfIsFalseElseIfIsTrue() throws Exception {
43  		stack.getContext().put(If.ANSWER, Boolean.FALSE);
44  		
45  		ElseIfTag tag = new ElseIfTag();
46  		tag.setPageContext(pageContext);
47  		tag.setTest("true");
48  		
49  		int result = tag.doStartTag();
50  		tag.doEndTag();
51  		
52  		assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
53  	}
54  	
55  	public void testIfIsFalseElseIfIsFalse() throws Exception {
56  		stack.getContext().put(If.ANSWER, Boolean.FALSE);
57  		
58  		ElseIfTag tag = new ElseIfTag();
59  		tag.setPageContext(pageContext);
60  		tag.setTest("false");
61  		
62  		int result = tag.doStartTag();
63  		tag.doEndTag();
64  		
65  		assertEquals(result, TagSupport.SKIP_BODY);
66  	}
67  	
68  	public void testIfIsTrueElseIfIsTrue() throws Exception {
69  		stack.getContext().put(If.ANSWER, Boolean.TRUE);
70  		
71  		ElseIfTag tag = new ElseIfTag();
72  		tag.setPageContext(pageContext);
73  		tag.setTest("true");
74  		
75  		int result = tag.doStartTag();
76  		tag.doEndTag();
77  		
78  		assertEquals(result, TagSupport.SKIP_BODY);
79  	}
80  	
81  	public void testIfIsTrueElseIfIsFalse() throws Exception {
82  		stack.getContext().put(If.ANSWER, Boolean.TRUE);
83  		
84  		ElseIfTag tag = new ElseIfTag();
85  		tag.setPageContext(pageContext);
86  		tag.setTest("false");
87  		
88  		int result = tag.doStartTag();
89  		tag.doEndTag();
90  		
91  		assertEquals(result, TagSupport.SKIP_BODY);
92  	}
93  	
94  	
95  	protected void setUp() throws Exception {
96  		stack = ValueStackFactory.getFactory().createValueStack();
97  		
98  		jspWriter = new MockJspWriter();
99  		
100 		StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
101 		
102 		StrutsMockServletContext servletContext = new StrutsMockServletContext();
103 		servletContext.setServletInfo("not-weblogic");
104 		
105 		pageContext = new MockPageContext();
106 		pageContext.setJspWriter(jspWriter);
107 		pageContext.setRequest(request);
108 		pageContext.setServletContext(servletContext);
109 		
110 		request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
111 	}
112 	
113 	
114 }