View Javadoc

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