View Javadoc

1   /*
2    * $Id$
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 org.apache.struts2.TestAction;
25  import org.apache.struts2.StrutsTestCase;
26  import org.apache.struts2.ServletActionContext;
27  import org.apache.struts2.StrutsException;
28  import com.mockobjects.servlet.MockPageContext;
29  import com.mockobjects.servlet.MockJspWriter;
30  import com.opensymphony.xwork2.util.ValueStack;
31  import com.opensymphony.xwork2.ActionContext;
32  
33  import javax.servlet.jsp.JspException;
34  import javax.servlet.jsp.tagext.TagSupport;
35  
36  public class I18nTagTest extends StrutsTestCase {
37  
38      I18nTag tag;
39      MockPageContext pageContext;
40      ValueStack stack;
41  
42      protected void setUp() throws Exception {
43          super.setUp();
44          // create the needed objects
45          tag = new I18nTag();
46          stack = ActionContext.getContext().getValueStack();
47  
48          // create the mock http servlet request
49          StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
50          ActionContext.getContext().setValueStack(stack);
51          request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
52  
53          // create the mock page context
54          pageContext = new MockPageContext();
55          pageContext.setRequest(request);
56          pageContext.setJspWriter(new MockJspWriter());
57  
58          // associate the tag with the mock page request
59          tag.setPageContext(pageContext);
60      }
61  
62      public void testSimple() throws Exception {
63  
64          // set the resource bundle
65          tag.setName("testmessages");
66  
67          int result = 0;
68  
69          try {
70              result = tag.doStartTag();
71          } catch (JspException e) {
72              e.printStackTrace();
73              fail();
74          }
75  
76          assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
77  
78          try {
79              result = tag.doEndTag();
80          } catch (JspException e) {
81              e.printStackTrace();
82              fail();
83          }
84      }
85  
86      /***
87       * Asserts that an exception is thrown when something unexpected is popped off the stack by the closing tag
88       *
89       * @throws Exception
90       */
91      public void testUnexpectedPop() throws Exception {
92  
93           // set the resource bundle
94          tag.setName("testmessages");
95  
96          int result = 0;
97  
98          try {
99              result = tag.doStartTag();
100         } catch (JspException e) {
101             e.printStackTrace();
102             fail();
103         }
104 
105         stack.push("An new object on top of the stack");
106 
107         assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
108 
109         try {
110             result = tag.doEndTag();
111             fail();
112         } catch (JspException e) {
113             e.printStackTrace();
114             fail();
115         } catch (StrutsException e) {
116             e.printStackTrace();
117             // pass
118         }
119 
120     }
121 }