View Javadoc

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