KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > TextTagTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp;
6
7 import com.opensymphony.webwork.TestAction;
8 import com.opensymphony.webwork.ServletActionContext;
9 import com.opensymphony.webwork.views.jsp.AbstractTagTest;
10 import com.opensymphony.webwork.views.jsp.TextTag;
11 import com.opensymphony.webwork.views.jsp.ui.TestAction1;
12 import com.opensymphony.webwork.views.jsp.ui.WebWorkBodyContent;
13 import com.opensymphony.xwork.Action;
14 import com.opensymphony.xwork.ActionContext;
15 import com.opensymphony.xwork.util.OgnlValueStack;
16
17 import javax.servlet.jsp.JspException JavaDoc;
18 import javax.servlet.jsp.tagext.Tag JavaDoc;
19 import java.text.MessageFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24
25
26 /**
27  * TextTagTest
28  * User: jcarreira
29  * Created: Oct 17, 2003 2:15:36 PM
30  */

31 public class TextTagTest extends AbstractTagTest {
32     //~ Instance fields ////////////////////////////////////////////////////////
33

34     private String JavaDoc fooValue = "com.opensymphony.webwork.views.jsp.TextTagTest.fooValue";
35     private TextTag tag;
36
37     //~ Methods ////////////////////////////////////////////////////////////////
38

39     public Action getAction() {
40         TestAction action = new TestAction();
41         action.setFoo(fooValue);
42
43         return action;
44     }
45
46     public void testExpressionsEvaluated() throws Exception JavaDoc {
47         String JavaDoc key = "expressionKey";
48         String JavaDoc value = "Foo is " + fooValue;
49         tag.setName(key);
50         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
51         assertEquals(value, writer.toString());
52     }
53
54     public void testMessageFormatWorks() throws Exception JavaDoc {
55         String JavaDoc key = "messageFormatKey";
56         String JavaDoc pattern = "Params are {0} {1} {2}";
57         Object JavaDoc param1 = new Integer JavaDoc(12);
58         Object JavaDoc param2 = new Date JavaDoc();
59         Object JavaDoc param3 = "StringVal";
60         List JavaDoc params = new ArrayList JavaDoc();
61         params.add(param1);
62         params.add(param2);
63         params.add(param3);
64
65         String JavaDoc expected = MessageFormat.format(pattern, params.toArray());
66         tag.setName(key);
67         tag.addParameter(param1);
68         tag.addParameter(param2);
69         tag.addParameter(param3);
70         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
71         assertEquals(expected, writer.toString());
72     }
73
74     public void testSimpleKeyValueWorks() throws JspException JavaDoc {
75         String JavaDoc key = "simpleKey";
76         String JavaDoc value = "Simple Message";
77         tag.setName(key);
78         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
79         assertEquals(value, writer.toString());
80     }
81
82     public void testTextTagUsesValueStackInRequestNotActionContext() throws JspException JavaDoc {
83         String JavaDoc key = "simpleKey";
84         String JavaDoc value1 = "Simple Message";
85         String JavaDoc value2="This is TestBean1";
86         tag.setName(key);
87         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
88         assertEquals(value1, writer.toString());
89         final StringBuffer JavaDoc buffer = writer.getBuffer();
90         buffer.delete(0,buffer.length());
91         OgnlValueStack newStack = new OgnlValueStack();
92         newStack.getContext().put(ActionContext.LOCALE,Locale.US);
93         newStack.push(new TestAction1());
94         request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, newStack);
95         assertNotSame(ActionContext.getContext().getValueStack().peek(),newStack.peek());
96
97         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
98         assertEquals(value2, writer.toString());
99     }
100
101     public void testTextTagUsesLocaleFromValueStack() throws JspException JavaDoc {
102         stack.pop();
103         stack.push(new TestAction1());
104         ActionContext.getContext().setLocale(Locale.US);
105         String JavaDoc key = "simpleKey";
106         String JavaDoc value_en="This is TestBean1";
107         tag.setName(key);
108         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
109         assertEquals(value_en, writer.toString());
110         final StringBuffer JavaDoc buffer = writer.getBuffer();
111         buffer.delete(0,buffer.length());
112         String JavaDoc value_de="This is TestBean1 in German";
113         OgnlValueStack newStack = new OgnlValueStack(stack);
114         newStack.getContext().put(ActionContext.LOCALE,Locale.GERMANY);
115         assertNotSame(newStack.getContext().get(ActionContext.LOCALE),ActionContext.getContext().getLocale());
116         request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, newStack);
117         assertEquals(ActionContext.getContext().getValueStack().peek(),newStack.peek());
118         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
119         assertEquals(value_de, writer.toString());
120     }
121
122     public void testWithNoMessageAndBodyIsNotEmptyBodyIsReturned() throws Exception JavaDoc {
123         final String JavaDoc key = "key.does.not.exist";
124         final String JavaDoc bodyText = "body text";
125         tag.setName(key);
126
127         WebWorkBodyContent bodyContent = new WebWorkBodyContent(null);
128         bodyContent.print(bodyText);
129         tag.setBodyContent(bodyContent);
130         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
131         assertEquals(bodyText, writer.toString());
132     }
133
134     public void testWithNoMessageAndNoDefaultKeyReturned() throws JspException JavaDoc {
135         final String JavaDoc key = "key.does.not.exist";
136         tag.setName("'" + key + "'");
137         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
138         assertEquals(key, writer.toString());
139     }
140
141     /**
142      * todo remove ActionContext set after LocalizedTextUtil is fixed to not use ThreadLocal
143      *
144      * @throws Exception
145      */

146     protected void setUp() throws Exception JavaDoc {
147         super.setUp();
148         tag = new TextTag();
149         tag.setPageContext(pageContext);
150         ActionContext.setContext(new ActionContext(stack.getContext()));
151     }
152
153     protected void tearDown() throws Exception JavaDoc {
154         OgnlValueStack valueStack = new OgnlValueStack();
155         ActionContext.setContext(new ActionContext(valueStack.getContext()));
156     }
157 }
158
Popular Tags