View Javadoc

1   /*
2    * $Id: TextTagTest.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 java.text.MessageFormat;
25  import java.util.ArrayList;
26  import java.util.Date;
27  import java.util.List;
28  import java.util.Locale;
29  
30  import javax.servlet.jsp.JspException;
31  import javax.servlet.jsp.tagext.BodyTag;
32  
33  import org.apache.struts2.ServletActionContext;
34  import org.apache.struts2.StrutsException;
35  import org.apache.struts2.TestAction;
36  import org.apache.struts2.components.Text;
37  import org.apache.struts2.views.jsp.ui.StrutsBodyContent;
38  import org.apache.struts2.views.jsp.ui.TestAction1;
39  
40  import com.mockobjects.servlet.MockJspWriter;
41  import com.opensymphony.xwork2.Action;
42  import com.opensymphony.xwork2.ActionContext;
43  import com.opensymphony.xwork2.util.ValueStack;
44  import com.opensymphony.xwork2.util.ValueStackFactory;
45  
46  
47  /***
48   * TextTagTest
49   *
50   */
51  public class TextTagTest extends AbstractTagTest {
52  
53      private String fooValue = "org.apache.struts2.views.jsp.TextTagTest.fooValue";
54      private TextTag tag;
55  
56  
57      public Action getAction() {
58          TestAction action = new TestAction();
59          action.setFoo(fooValue);
60  
61          return action;
62      }
63  
64      public void testDefaultMessageOk() throws Exception {
65          // NOTE:
66          // simulate the condition
67          // <s:text name="some.invalid.key">My Default Message</s:text>
68  
69          StrutsMockBodyContent mockBodyContent = new StrutsMockBodyContent(new MockJspWriter());
70          mockBodyContent.setString("Sample Of Default Message");
71          tag.setBodyContent(mockBodyContent);
72          tag.setName("some.invalid.key.so.we.should.get.the.default.message");
73          int startStatus = tag.doStartTag();
74          tag.doEndTag();
75  
76          assertEquals(startStatus, BodyTag.EVAL_BODY_BUFFERED);
77          assertEquals("Sample Of Default Message", writer.toString());
78      }
79  
80      public void testExpressionsEvaluated() throws Exception {
81          String key = "expressionKey";
82          String value = "Foo is " + fooValue;
83          tag.setName(key);
84          tag.doStartTag();
85          tag.doEndTag();
86          assertEquals(value, writer.toString());
87      }
88  
89      public void testCorrectI18NKey() throws Exception {
90          String key = "foo.bar.baz";
91          String value = "This should start with foo";
92          tag.setName(key);
93          tag.doStartTag();
94          tag.doEndTag();
95          assertEquals(value, writer.toString());
96      }
97  
98      public void testCorrectI18NKey2() throws Exception {
99          String key = "bar.baz";
100         String value = "No foo here";
101         tag.setName(key);
102         tag.doStartTag();
103         tag.doEndTag();
104         assertEquals(value, writer.toString());
105     }
106 
107     public void testMessageFormatWorks() throws Exception {
108         String key = "messageFormatKey";
109         String pattern = "Params are {0} {1} {2}";
110         Object param1 = new Integer(12);
111         Object param2 = new Date();
112         Object param3 = "StringVal";
113         List params = new ArrayList();
114         params.add(param1);
115         params.add(param2);
116         params.add(param3);
117 
118         String expected = MessageFormat.format(pattern, params.toArray());
119         tag.setName(key);
120         tag.doStartTag();
121         ((Text) tag.component).addParameter(param1);
122         ((Text) tag.component).addParameter(param2);
123         ((Text) tag.component).addParameter(param3);
124         tag.doEndTag();
125         assertEquals(expected, writer.toString());
126     }
127 
128     public void testSimpleKeyValueWorks() throws JspException {
129         String key = "simpleKey";
130         String value = "Simple Message";
131         tag.setName(key);
132         tag.doStartTag();
133         tag.doEndTag();
134         assertEquals(value, writer.toString());
135     }
136 
137     private Locale getForeignLocale() {
138         if (Locale.getDefault().getLanguage().equals("de")) {
139             return Locale.FRANCE;
140         } else {
141             return Locale.GERMANY;
142         }
143     }
144 
145     private Locale getDefaultLocale() {
146         if (Locale.getDefault().getLanguage().equals("de")) {
147             return Locale.GERMANY;
148         } else if (Locale.getDefault().getLanguage().equals("fr")) {
149             return Locale.FRANCE;
150         } else {
151             return Locale.US;
152         }
153     }
154 
155     private String getLocalizedMessage(Locale locale) {
156         if (locale.getLanguage().equals("de")) {
157             return "This is TestBean1 in German";
158         } else if (locale.getLanguage().equals("fr")) {
159             return "This is TestBean1 in French";
160         } else {
161             return "This is TestBean1";
162         }
163     }
164 
165     public void testTextTagUsesValueStackInRequestNotActionContext() throws JspException {
166         String key = "simpleKey";
167         String value1 = "Simple Message";
168         Locale foreignLocale = getForeignLocale();
169         String value2 = getLocalizedMessage(foreignLocale);
170         tag.setName(key);
171         tag.doStartTag();
172         tag.doEndTag();
173         assertEquals(value1, writer.toString());
174         final StringBuffer buffer = writer.getBuffer();
175         buffer.delete(0, buffer.length());
176         ValueStack newStack = container.getInstance(ValueStackFactory.class).createValueStack();
177         newStack.getContext().put(ActionContext.LOCALE, foreignLocale);
178         newStack.getContext().put(ActionContext.CONTAINER, container);
179         newStack.push(new TestAction1());
180         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, newStack);
181         assertNotSame(ActionContext.getContext().getValueStack().peek(), newStack.peek());
182 
183         tag.doStartTag();
184         tag.doEndTag();
185         assertEquals(value2, writer.toString());
186     }
187 
188     public void testTextTagUsesLocaleFromValueStack() throws JspException {
189         stack.pop();
190         stack.push(new TestAction1());
191 
192         Locale defaultLocale = getDefaultLocale();
193         Locale foreignLocale = getForeignLocale();
194         assertNotSame(defaultLocale, foreignLocale);
195 
196         ActionContext.getContext().setLocale(defaultLocale);
197         String key = "simpleKey";
198         String value_default = getLocalizedMessage(defaultLocale);
199         tag.setName(key);
200         tag.doStartTag();
201         tag.doEndTag();
202         assertEquals(value_default, writer.toString());
203 
204         final StringBuffer buffer = writer.getBuffer();
205         buffer.delete(0, buffer.length());
206         String value_int = getLocalizedMessage(foreignLocale);
207         assertFalse(value_default.equals(value_int));
208         ValueStack newStack = container.getInstance(ValueStackFactory.class).createValueStack(stack);
209         newStack.getContext().put(ActionContext.LOCALE, foreignLocale);
210         newStack.getContext().put(ActionContext.CONTAINER, container);
211         assertNotSame(newStack.getContext().get(ActionContext.LOCALE), ActionContext.getContext().getLocale());
212         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, newStack);
213         assertEquals(ActionContext.getContext().getValueStack().peek(), newStack.peek());
214         tag.doStartTag();
215         tag.doEndTag();
216         assertEquals(value_int, writer.toString());
217     }
218 
219     public void testWithNoMessageAndBodyIsNotEmptyBodyIsReturned() throws Exception {
220         final String key = "key.does.not.exist";
221         final String bodyText = "body text";
222         tag.setName(key);
223 
224         StrutsBodyContent bodyContent = new StrutsBodyContent(null);
225         bodyContent.print(bodyText);
226         tag.setBodyContent(bodyContent);
227         tag.doStartTag();
228         tag.doEndTag();
229         assertEquals(bodyText, writer.toString());
230     }
231 
232     public void testWithNoMessageAndNoDefaultKeyReturned() throws JspException {
233         final String key = "key.does.not.exist";
234         tag.setName("'" + key + "'");
235         tag.doStartTag();
236         tag.doEndTag();
237         assertEquals(key, writer.toString());
238     }
239 
240     public void testNoNameDefined() throws Exception {
241         String msg = "tag 'text', field 'name': You must specify the i18n key. Example: welcome.header";
242         try {
243             tag.doStartTag();
244             tag.doEndTag();
245             fail("Should have thrown a RuntimeException");
246         } catch (StrutsException e) {
247             assertEquals(msg, e.getMessage());
248         }
249     }
250 
251     public void testBlankNameDefined() throws Exception {
252         tag.setName("");
253         tag.doStartTag();
254         tag.doEndTag();
255         assertEquals("", writer.toString());
256     }
257 
258     public void testPutId() throws Exception {
259         assertEquals(null, stack.findString("myId")); // nothing in stack
260         tag.setId("myId");
261         tag.setName("bar.baz");
262         tag.doStartTag();
263         tag.doEndTag();
264         assertEquals("", writer.toString());
265         assertEquals("No foo here", stack.findString("myId")); // is in stack now
266     }
267 
268     /***
269      * todo remove ActionContext set after LocalizedTextUtil is fixed to not use ThreadLocal
270      *
271      * @throws Exception
272      */
273     protected void setUp() throws Exception {
274         super.setUp();
275         tag = new TextTag();
276         tag.setPageContext(pageContext);
277         ActionContext.setContext(new ActionContext(stack.getContext()));
278     }
279 
280     protected void tearDown() throws Exception {
281         super.tearDown();
282     }
283 }