View Javadoc

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