001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp;
006:
007: import com.opensymphony.webwork.ServletActionContext;
008: import com.opensymphony.webwork.TestAction;
009: import com.opensymphony.webwork.components.Text;
010: import com.opensymphony.webwork.views.jsp.ui.TestAction1;
011: import com.opensymphony.webwork.views.jsp.ui.WebWorkBodyContent;
012:
013: import com.mockobjects.servlet.MockBodyContent;
014: import com.mockobjects.servlet.MockJspWriter;
015: import com.opensymphony.xwork.Action;
016: import com.opensymphony.xwork.ActionContext;
017: import com.opensymphony.xwork.util.OgnlValueStack;
018:
019: import javax.servlet.jsp.JspException;
020: import javax.servlet.jsp.tagext.BodyTag;
021: import javax.servlet.jsp.tagext.Tag;
022: import javax.servlet.jsp.tagext.TagSupport;
023:
024: import java.text.MessageFormat;
025: import java.util.ArrayList;
026: import java.util.Date;
027: import java.util.List;
028: import java.util.Locale;
029:
030: /**
031: * TextTagTest
032: *
033: * @author jcarreira
034: * @author Rainer Hermanns
035: */
036: public class TextTagTest extends AbstractTagTest {
037:
038: private String fooValue = "com.opensymphony.webwork.views.jsp.TextTagTest.fooValue";
039: private TextTag tag;
040:
041: public Action getAction() {
042: TestAction action = new TestAction();
043: action.setFoo(fooValue);
044:
045: return action;
046: }
047:
048: public void testDefaultMessageOk() throws Exception {
049: // NOTE:
050: // simulate the condition
051: // <saf:text name="some.invalid.key">My Default Message</saf:text>
052:
053: WebWorkMockBodyContent mockBodyContent = new WebWorkMockBodyContent(
054: new MockJspWriter());
055: mockBodyContent.setString("Sample Of Default Message");
056: tag.setBodyContent(mockBodyContent);
057: tag
058: .setName("some.invalid.key.so.we.should.get.the.default.message");
059: int startStatus = tag.doStartTag();
060: tag.doEndTag();
061:
062: assertEquals(startStatus, BodyTag.EVAL_BODY_BUFFERED);
063: assertEquals("Sample Of Default Message", writer.toString());
064: }
065:
066: public void testExpressionsEvaluated() throws Exception {
067: String key = "expressionKey";
068: String value = "Foo is " + fooValue;
069: tag.setName(key);
070: tag.doStartTag();
071: tag.doEndTag();
072: assertEquals(value, writer.toString());
073: }
074:
075: public void testCorrectI18NKey() throws Exception {
076: String key = "foo.bar.baz";
077: String value = "This should start with foo";
078: tag.setName(key);
079: tag.doStartTag();
080: tag.doEndTag();
081: assertEquals(value, writer.toString());
082: }
083:
084: public void testCorrectI18NKey2() throws Exception {
085: String key = "bar.baz";
086: String value = "No foo here";
087: tag.setName(key);
088: tag.doStartTag();
089: tag.doEndTag();
090: assertEquals(value, writer.toString());
091: }
092:
093: public void testMessageFormatWorks() throws Exception {
094: String key = "messageFormatKey";
095: String pattern = "Params are {0} {1} {2}";
096: Object param1 = new Integer(12);
097: Object param2 = new Date();
098: Object param3 = "StringVal";
099: List params = new ArrayList();
100: params.add(param1);
101: params.add(param2);
102: params.add(param3);
103:
104: String expected = MessageFormat.format(pattern, params
105: .toArray());
106: tag.setName(key);
107: tag.doStartTag();
108: ((Text) tag.component).addParameter(param1);
109: ((Text) tag.component).addParameter(param2);
110: ((Text) tag.component).addParameter(param3);
111: tag.doEndTag();
112: assertEquals(expected, writer.toString());
113: }
114:
115: public void testSimpleKeyValueWorks() throws JspException {
116: String key = "simpleKey";
117: String value = "Simple Message";
118: tag.setName(key);
119: tag.doStartTag();
120: tag.doEndTag();
121: assertEquals(value, writer.toString());
122: }
123:
124: private Locale getForeignLocale() {
125: if (Locale.getDefault().getLanguage().equals("de")) {
126: return Locale.FRANCE;
127: } else {
128: return Locale.GERMANY;
129: }
130: }
131:
132: private Locale getDefaultLocale() {
133: if (Locale.getDefault().getLanguage().equals("de")) {
134: return Locale.GERMANY;
135: } else if (Locale.getDefault().getLanguage().equals("fr")) {
136: return Locale.FRANCE;
137: } else {
138: return Locale.US;
139: }
140: }
141:
142: private String getLocalizedMessage(Locale locale) {
143: if (locale.getLanguage().equals("de")) {
144: return "This is TestBean1 in German";
145: } else if (locale.getLanguage().equals("fr")) {
146: return "This is TestBean1 in French";
147: } else {
148: return "This is TestBean1";
149: }
150: }
151:
152: public void testTextTagUsesValueStackInRequestNotActionContext()
153: throws JspException {
154: String key = "simpleKey";
155: String value1 = "Simple Message";
156: Locale foreignLocale = getForeignLocale();
157: String value2 = getLocalizedMessage(foreignLocale);
158: tag.setName(key);
159: tag.doStartTag();
160: tag.doEndTag();
161: assertEquals(value1, writer.toString());
162: final StringBuffer buffer = writer.getBuffer();
163: buffer.delete(0, buffer.length());
164: OgnlValueStack newStack = new OgnlValueStack();
165: newStack.getContext().put(ActionContext.LOCALE, foreignLocale);
166: newStack.push(new TestAction1());
167: request.setAttribute(
168: ServletActionContext.WEBWORK_VALUESTACK_KEY, newStack);
169: assertNotSame(
170: ActionContext.getContext().getValueStack().peek(),
171: newStack.peek());
172:
173: tag.doStartTag();
174: tag.doEndTag();
175: assertEquals(value2, writer.toString());
176: }
177:
178: public void testTextTagUsesLocaleFromValueStack()
179: throws JspException {
180: stack.pop();
181: stack.push(new TestAction1());
182:
183: Locale defaultLocale = getDefaultLocale();
184: Locale foreignLocale = getForeignLocale();
185: assertNotSame(defaultLocale, foreignLocale);
186:
187: ActionContext.getContext().setLocale(defaultLocale);
188: String key = "simpleKey";
189: String value_default = getLocalizedMessage(defaultLocale);
190: tag.setName(key);
191: tag.doStartTag();
192: tag.doEndTag();
193: assertEquals(value_default, writer.toString());
194:
195: final StringBuffer buffer = writer.getBuffer();
196: buffer.delete(0, buffer.length());
197: String value_int = getLocalizedMessage(foreignLocale);
198: assertFalse(value_default.equals(value_int));
199: OgnlValueStack newStack = new OgnlValueStack(stack);
200: newStack.getContext().put(ActionContext.LOCALE, foreignLocale);
201: assertNotSame(newStack.getContext().get(ActionContext.LOCALE),
202: ActionContext.getContext().getLocale());
203: request.setAttribute(
204: ServletActionContext.WEBWORK_VALUESTACK_KEY, newStack);
205: assertEquals(ActionContext.getContext().getValueStack().peek(),
206: newStack.peek());
207: tag.doStartTag();
208: tag.doEndTag();
209: assertEquals(value_int, writer.toString());
210: }
211:
212: public void testWithNoMessageAndBodyIsNotEmptyBodyIsReturned()
213: throws Exception {
214: final String key = "key.does.not.exist";
215: final String bodyText = "body text";
216: tag.setName(key);
217:
218: WebWorkBodyContent bodyContent = new WebWorkBodyContent(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()
227: throws JspException {
228: final String key = "key.does.not.exist";
229: tag.setName("'" + key + "'");
230: tag.doStartTag();
231: tag.doEndTag();
232: assertEquals(key, writer.toString());
233: }
234:
235: public void testNoNameDefined() throws Exception {
236: String msg = "tag 'text', field 'name': You must specify the i18n key. Example: welcome.header";
237: try {
238: tag.doStartTag();
239: tag.doEndTag();
240: fail("Should have thrown a RuntimeException");
241: } catch (RuntimeException e) {
242: assertEquals(msg, e.getMessage());
243: }
244: }
245:
246: public void testBlankNameDefined() throws Exception {
247: tag.setName("");
248: tag.doStartTag();
249: tag.doEndTag();
250: assertEquals("", writer.toString());
251: }
252:
253: public void testPutId() throws Exception {
254: assertEquals(null, stack.findString("myId")); // nothing in stack
255: tag.setId("myId");
256: tag.setName("bar.baz");
257: tag.doStartTag();
258: tag.doEndTag();
259: assertEquals("", writer.toString());
260: assertEquals("No foo here", stack.findString("myId")); // is in stack now
261: }
262:
263: /**
264: * todo remove ActionContext set after LocalizedTextUtil is fixed to not use ThreadLocal
265: *
266: * @throws Exception
267: */
268: protected void setUp() throws Exception {
269: super .setUp();
270: tag = new TextTag();
271: tag.setPageContext(pageContext);
272: ActionContext.setContext(new ActionContext(stack.getContext()));
273: }
274:
275: protected void tearDown() throws Exception {
276: OgnlValueStack valueStack = new OgnlValueStack();
277: ActionContext.setContext(new ActionContext(valueStack
278: .getContext()));
279: }
280: }
|