View Javadoc

1   /*
2    * $Id: ElseTagTest.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.JspException;
25  import javax.servlet.jsp.tagext.TagSupport;
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.ActionContext;
34  import com.opensymphony.xwork2.util.ValueStack;
35  import com.opensymphony.xwork2.util.ValueStackFactory;
36  
37  
38  /***
39   */
40  public class ElseTagTest extends StrutsTestCase {
41  
42      ElseTag elseTag;
43      MockPageContext pageContext;
44      ValueStack stack;
45  
46  
47      public void testTestFalse() {
48          stack.getContext().put(If.ANSWER, new Boolean(false));
49  
50          int result = 0;
51  
52          try {
53              elseTag.setPageContext(pageContext);
54              result = elseTag.doStartTag();
55              elseTag.doEndTag();
56          } catch (JspException e) {
57              e.printStackTrace();
58              fail();
59          }
60          assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
61      }
62  
63      public void testTestNull() {
64          elseTag.setPageContext(pageContext);
65  
66          int result = 0;
67  
68          try {
69              result = elseTag.doStartTag();
70          } catch (JspException e) {
71              e.printStackTrace();
72              fail();
73          }
74  
75          assertEquals(TagSupport.SKIP_BODY, result);
76      }
77  
78      public void testTestTrue() {
79          stack.getContext().put(If.ANSWER, new Boolean(true));
80          elseTag.setPageContext(pageContext);
81  
82          int result = 0;
83  
84          try {
85              result = elseTag.doStartTag();
86          } catch (JspException e) {
87              e.printStackTrace();
88              fail();
89          }
90  
91          assertEquals(TagSupport.SKIP_BODY, result);
92      }
93  
94      protected void setUp() throws Exception {
95          super.setUp();
96          // create the needed objects
97          elseTag = new ElseTag();
98          stack = ActionContext.getContext().getValueStack();
99  
100         // create the mock http servlet request
101         StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
102 
103         // NOTE: in Struts Tag library, TagUtil gets stack from request, which will be set
104         //       when request going through the FilterDispatcher --> DispatcherUtil etc. route
105         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
106 
107         StrutsMockServletContext servletContext = new StrutsMockServletContext();
108         servletContext.setServletInfo("not-weblogic");
109 
110         // create the mock page context
111         pageContext = new StrutsMockPageContext();
112         pageContext.setRequest(request);
113         pageContext.setServletContext(servletContext);
114         pageContext.setJspWriter(new MockJspWriter());
115     }
116 
117 
118     class Foo {
119         int num;
120 
121         public void setNum(int num) {
122             this.num = num;
123         }
124 
125         public int getNum() {
126             return num;
127         }
128     }
129 }