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