View Javadoc

1   /*
2    * $Id: TextTagTest.java 727558 2008-12-17 22:38:40Z musachy $
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 testTextTagSearchesStackByDefault() throws JspException {
220         String key = "result";
221 
222         tag.setName(key);
223         final StringBuffer buffer = writer.getBuffer();
224         buffer.delete(0, buffer.length());
225         ValueStack newStack = container.getInstance(ValueStackFactory.class).createValueStack();
226         newStack.getContext().put(ActionContext.CONTAINER, container);
227         TestAction testAction = new TestAction();
228         testAction.setResult("bar");
229         newStack.push(testAction);
230         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, newStack);
231 
232 
233         tag.doStartTag();
234         tag.doEndTag();
235         assertEquals("bar", writer.toString());
236     }
237 
238     public void testTextTagDoNotSearchStack() throws JspException {
239         String key = "result";
240 
241         tag.setName(key);
242         tag.setSearchValueStack("false");
243         final StringBuffer buffer = writer.getBuffer();
244         buffer.delete(0, buffer.length());
245         ValueStack newStack = container.getInstance(ValueStackFactory.class).createValueStack();
246         newStack.getContext().put(ActionContext.CONTAINER, container);
247         TestAction testAction = new TestAction();
248         testAction.setResult("bar");
249         newStack.push(testAction);
250         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, newStack);
251 
252 
253         tag.doStartTag();
254         tag.doEndTag();
255         assertEquals("result", writer.toString());
256     }
257 
258     public void testWithNoMessageAndBodyIsNotEmptyBodyIsReturned() throws Exception {
259         final String key = "key.does.not.exist";
260         final String bodyText = "body text";
261         tag.setName(key);
262 
263         StrutsBodyContent bodyContent = new StrutsBodyContent(null);
264         bodyContent.print(bodyText);
265         tag.setBodyContent(bodyContent);
266         tag.doStartTag();
267         tag.doEndTag();
268         assertEquals(bodyText, writer.toString());
269     }
270 
271     public void testWithNoMessageAndNoDefaultKeyReturned() throws JspException {
272         final String key = "key.does.not.exist";
273         tag.setName("'" + key + "'");
274         tag.doStartTag();
275         tag.doEndTag();
276         assertEquals(key, writer.toString());
277     }
278 
279     public void testNoNameDefined() throws Exception {
280         String msg = "tag 'text', field 'name': You must specify the i18n key. Example: welcome.header";
281         try {
282             tag.doStartTag();
283             tag.doEndTag();
284             fail("Should have thrown a RuntimeException");
285         } catch (StrutsException e) {
286             assertEquals(msg, e.getMessage());
287         }
288     }
289 
290     public void testBlankNameDefined() throws Exception {
291         tag.setName("");
292         tag.doStartTag();
293         tag.doEndTag();
294         assertEquals("", writer.toString());
295     }
296 
297     public void testPutId() throws Exception {
298         assertEquals(null, stack.findString("myId")); // nothing in stack
299         tag.setId("myId");
300         tag.setName("bar.baz");
301         tag.doStartTag();
302         tag.doEndTag();
303         assertEquals("", writer.toString());
304         assertEquals("No foo here", stack.findString("myId")); // is in stack now
305     }
306 
307     /***
308      * todo remove ActionContext set after LocalizedTextUtil is fixed to not use ThreadLocal
309      *
310      * @throws Exception
311      */
312     protected void setUp() throws Exception {
313         super.setUp();
314         tag = new TextTag();
315         tag.setPageContext(pageContext);
316         ActionContext.setContext(new ActionContext(stack.getContext()));
317     }
318 
319     protected void tearDown() throws Exception {
320         super.tearDown();
321     }
322 }