View Javadoc

1   /*
2    * $Id: ElseIfTagTest.java 474191 2006-11-13 08:30:40Z mrdon $
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  package org.apache.struts2.views.jsp;
22  
23  import javax.servlet.jsp.tagext.TagSupport;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.struts2.ServletActionContext;
28  import org.apache.struts2.StrutsTestCase;
29  import org.apache.struts2.components.If;
30  
31  import com.mockobjects.servlet.MockJspWriter;
32  import com.mockobjects.servlet.MockPageContext;
33  import com.opensymphony.xwork2.util.ValueStack;
34  import com.opensymphony.xwork2.util.ValueStackFactory;
35  
36  /***
37   *
38   */
39  public class ElseIfTagTest extends StrutsTestCase {
40  
41      protected MockPageContext pageContext;
42      protected MockJspWriter jspWriter;
43      protected ValueStack stack;
44  
45  
46      public void testIfIsFalseElseIfIsTrue() throws Exception {
47          stack.getContext().put(If.ANSWER, Boolean.FALSE);
48  
49          ElseIfTag tag = new ElseIfTag();
50          tag.setPageContext(pageContext);
51          tag.setTest("true");
52  
53          int result = tag.doStartTag();
54          tag.doEndTag();
55  
56          assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
57      }
58  
59      public void testIfIsFalseElseIfIsFalse() throws Exception {
60          stack.getContext().put(If.ANSWER, Boolean.FALSE);
61  
62          ElseIfTag tag = new ElseIfTag();
63          tag.setPageContext(pageContext);
64          tag.setTest("false");
65  
66          int result = tag.doStartTag();
67          tag.doEndTag();
68  
69          assertEquals(result, TagSupport.SKIP_BODY);
70      }
71  
72      public void testIfIsTrueElseIfIsTrue() throws Exception {
73          stack.getContext().put(If.ANSWER, Boolean.TRUE);
74  
75          ElseIfTag tag = new ElseIfTag();
76          tag.setPageContext(pageContext);
77          tag.setTest("true");
78  
79          int result = tag.doStartTag();
80          tag.doEndTag();
81  
82          assertEquals(result, TagSupport.SKIP_BODY);
83      }
84  
85      public void testIfIsTrueElseIfIsFalse() throws Exception {
86          stack.getContext().put(If.ANSWER, Boolean.TRUE);
87  
88          ElseIfTag tag = new ElseIfTag();
89          tag.setPageContext(pageContext);
90          tag.setTest("false");
91  
92          int result = tag.doStartTag();
93          tag.doEndTag();
94  
95          assertEquals(result, TagSupport.SKIP_BODY);
96      }
97  
98  
99      protected void setUp() throws Exception {
100         super.setUp();
101         stack = ValueStackFactory.getFactory().createValueStack();
102 
103         jspWriter = new MockJspWriter();
104 
105         StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
106 
107         StrutsMockServletContext servletContext = new StrutsMockServletContext();
108         servletContext.setServletInfo("not-weblogic");
109 
110         pageContext = new MockPageContext();
111         pageContext.setJspWriter(jspWriter);
112         pageContext.setRequest(request);
113         pageContext.setServletContext(servletContext);
114 
115         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
116     }
117 
118 
119 }