001: /*
002: * $Id: TextTagTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.jsp;
022:
023: import java.text.MessageFormat;
024: import java.util.ArrayList;
025: import java.util.Date;
026: import java.util.List;
027: import java.util.Locale;
028:
029: import javax.servlet.jsp.JspException;
030: import javax.servlet.jsp.tagext.BodyTag;
031:
032: import org.apache.struts2.ServletActionContext;
033: import org.apache.struts2.StrutsException;
034: import org.apache.struts2.TestAction;
035: import org.apache.struts2.components.Text;
036: import org.apache.struts2.views.jsp.ui.StrutsBodyContent;
037: import org.apache.struts2.views.jsp.ui.TestAction1;
038:
039: import com.mockobjects.servlet.MockJspWriter;
040: import com.opensymphony.xwork2.Action;
041: import com.opensymphony.xwork2.ActionContext;
042: import com.opensymphony.xwork2.util.ValueStack;
043: import com.opensymphony.xwork2.util.ValueStackFactory;
044:
045: /**
046: * TextTagTest
047: *
048: */
049: public class TextTagTest extends AbstractTagTest {
050:
051: private String fooValue = "org.apache.struts2.views.jsp.TextTagTest.fooValue";
052: private TextTag tag;
053:
054: public Action getAction() {
055: TestAction action = new TestAction();
056: action.setFoo(fooValue);
057:
058: return action;
059: }
060:
061: public void testDefaultMessageOk() throws Exception {
062: // NOTE:
063: // simulate the condition
064: // <s:text name="some.invalid.key">My Default Message</s:text>
065:
066: StrutsMockBodyContent mockBodyContent = new StrutsMockBodyContent(
067: new MockJspWriter());
068: mockBodyContent.setString("Sample Of Default Message");
069: tag.setBodyContent(mockBodyContent);
070: tag
071: .setName("some.invalid.key.so.we.should.get.the.default.message");
072: int startStatus = tag.doStartTag();
073: tag.doEndTag();
074:
075: assertEquals(startStatus, BodyTag.EVAL_BODY_BUFFERED);
076: assertEquals("Sample Of Default Message", writer.toString());
077: }
078:
079: public void testExpressionsEvaluated() throws Exception {
080: String key = "expressionKey";
081: String value = "Foo is " + fooValue;
082: tag.setName(key);
083: tag.doStartTag();
084: tag.doEndTag();
085: assertEquals(value, writer.toString());
086: }
087:
088: public void testCorrectI18NKey() throws Exception {
089: String key = "foo.bar.baz";
090: String value = "This should start with foo";
091: tag.setName(key);
092: tag.doStartTag();
093: tag.doEndTag();
094: assertEquals(value, writer.toString());
095: }
096:
097: public void testCorrectI18NKey2() throws Exception {
098: String key = "bar.baz";
099: 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
118: .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()
166: throws JspException {
167: String key = "simpleKey";
168: String value1 = "Simple Message";
169: Locale foreignLocale = getForeignLocale();
170: String value2 = getLocalizedMessage(foreignLocale);
171: tag.setName(key);
172: tag.doStartTag();
173: tag.doEndTag();
174: assertEquals(value1, writer.toString());
175: final StringBuffer buffer = writer.getBuffer();
176: buffer.delete(0, buffer.length());
177: ValueStack newStack = ValueStackFactory.getFactory()
178: .createValueStack();
179: newStack.getContext().put(ActionContext.LOCALE, foreignLocale);
180: newStack.push(new TestAction1());
181: request.setAttribute(
182: ServletActionContext.STRUTS_VALUESTACK_KEY, newStack);
183: assertNotSame(
184: ActionContext.getContext().getValueStack().peek(),
185: newStack.peek());
186:
187: tag.doStartTag();
188: tag.doEndTag();
189: assertEquals(value2, writer.toString());
190: }
191:
192: public void testTextTagUsesLocaleFromValueStack()
193: throws JspException {
194: stack.pop();
195: stack.push(new TestAction1());
196:
197: Locale defaultLocale = getDefaultLocale();
198: Locale foreignLocale = getForeignLocale();
199: assertNotSame(defaultLocale, foreignLocale);
200:
201: ActionContext.getContext().setLocale(defaultLocale);
202: String key = "simpleKey";
203: String value_default = getLocalizedMessage(defaultLocale);
204: tag.setName(key);
205: tag.doStartTag();
206: tag.doEndTag();
207: assertEquals(value_default, writer.toString());
208:
209: final StringBuffer buffer = writer.getBuffer();
210: buffer.delete(0, buffer.length());
211: String value_int = getLocalizedMessage(foreignLocale);
212: assertFalse(value_default.equals(value_int));
213: ValueStack newStack = ValueStackFactory.getFactory()
214: .createValueStack(stack);
215: newStack.getContext().put(ActionContext.LOCALE, foreignLocale);
216: assertNotSame(newStack.getContext().get(ActionContext.LOCALE),
217: ActionContext.getContext().getLocale());
218: request.setAttribute(
219: ServletActionContext.STRUTS_VALUESTACK_KEY, newStack);
220: assertEquals(ActionContext.getContext().getValueStack().peek(),
221: newStack.peek());
222: tag.doStartTag();
223: tag.doEndTag();
224: assertEquals(value_int, writer.toString());
225: }
226:
227: public void testWithNoMessageAndBodyIsNotEmptyBodyIsReturned()
228: throws Exception {
229: final String key = "key.does.not.exist";
230: final String bodyText = "body text";
231: tag.setName(key);
232:
233: StrutsBodyContent bodyContent = new StrutsBodyContent(null);
234: bodyContent.print(bodyText);
235: tag.setBodyContent(bodyContent);
236: tag.doStartTag();
237: tag.doEndTag();
238: assertEquals(bodyText, writer.toString());
239: }
240:
241: public void testWithNoMessageAndNoDefaultKeyReturned()
242: throws JspException {
243: final String key = "key.does.not.exist";
244: tag.setName("'" + key + "'");
245: tag.doStartTag();
246: tag.doEndTag();
247: assertEquals(key, writer.toString());
248: }
249:
250: public void testNoNameDefined() throws Exception {
251: String msg = "tag 'text', field 'name': You must specify the i18n key. Example: welcome.header";
252: try {
253: tag.doStartTag();
254: tag.doEndTag();
255: fail("Should have thrown a RuntimeException");
256: } catch (StrutsException e) {
257: assertEquals(msg, e.getMessage());
258: }
259: }
260:
261: public void testBlankNameDefined() throws Exception {
262: tag.setName("");
263: tag.doStartTag();
264: tag.doEndTag();
265: assertEquals("", writer.toString());
266: }
267:
268: public void testPutId() throws Exception {
269: assertEquals(null, stack.findString("myId")); // nothing in stack
270: tag.setId("myId");
271: tag.setName("bar.baz");
272: tag.doStartTag();
273: tag.doEndTag();
274: assertEquals("", writer.toString());
275: assertEquals("No foo here", stack.findString("myId")); // is in stack now
276: }
277:
278: /**
279: * todo remove ActionContext set after LocalizedTextUtil is fixed to not use ThreadLocal
280: *
281: * @throws Exception
282: */
283: protected void setUp() throws Exception {
284: super .setUp();
285: tag = new TextTag();
286: tag.setPageContext(pageContext);
287: ActionContext.setContext(new ActionContext(stack.getContext()));
288: }
289:
290: protected void tearDown() throws Exception {
291: ValueStack valueStack = ValueStackFactory.getFactory()
292: .createValueStack();
293: ActionContext.setContext(new ActionContext(valueStack
294: .getContext()));
295: }
296: }
|