View Javadoc

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