001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp;
006:
007: import com.mockobjects.servlet.MockJspWriter;
008: import com.mockobjects.servlet.MockPageContext;
009: import com.opensymphony.webwork.ServletActionContext;
010: import com.opensymphony.webwork.WebWorkTestCase;
011: import com.opensymphony.webwork.WebWorkConstants;
012: import com.opensymphony.webwork.config.Configuration;
013: import com.opensymphony.xwork.ActionContext;
014: import com.opensymphony.xwork.util.OgnlValueStack;
015:
016: import javax.servlet.jsp.JspException;
017:
018: /**
019: * PropertyTag test case.
020: *
021: * @author $Author: plightbo $
022: * @version $Revision: 1927 $
023: */
024: public class PropertyTagTest extends WebWorkTestCase {
025:
026: WebWorkMockHttpServletRequest request = new WebWorkMockHttpServletRequest();
027: OgnlValueStack stack = new OgnlValueStack();
028:
029: public void testDefaultValue() {
030: PropertyTag tag = new PropertyTag();
031:
032: Foo foo = new Foo();
033:
034: stack.push(foo);
035:
036: MockJspWriter jspWriter = new MockJspWriter();
037: jspWriter.setExpectedData("TEST");
038:
039: MockPageContext pageContext = new MockPageContext();
040: pageContext.setJspWriter(jspWriter);
041: pageContext.setRequest(request);
042:
043: tag.setPageContext(pageContext);
044: tag.setValue("title");
045: tag.setDefault("TEST");
046:
047: try {
048: tag.doStartTag();
049: } catch (JspException e) {
050: e.printStackTrace();
051: fail();
052: }
053:
054: request.verify();
055: jspWriter.verify();
056: pageContext.verify();
057: }
058:
059: public void testNull() {
060: PropertyTag tag = new PropertyTag();
061:
062: Foo foo = new Foo();
063:
064: stack.push(foo);
065:
066: MockJspWriter jspWriter = new MockJspWriter();
067: jspWriter.setExpectedData("");
068:
069: MockPageContext pageContext = new MockPageContext();
070: pageContext.setJspWriter(jspWriter);
071: pageContext.setRequest(request);
072:
073: tag.setPageContext(pageContext);
074: tag.setValue("title");
075:
076: try {
077: tag.doStartTag();
078: } catch (JspException e) {
079: e.printStackTrace();
080: fail();
081: }
082:
083: request.verify();
084: jspWriter.verify();
085: pageContext.verify();
086: }
087:
088: public void testSimple() {
089: PropertyTag tag = new PropertyTag();
090:
091: Foo foo = new Foo();
092: foo.setTitle("test");
093:
094: stack.push(foo);
095:
096: MockJspWriter jspWriter = new MockJspWriter();
097: jspWriter.setExpectedData("test");
098:
099: MockPageContext pageContext = new MockPageContext();
100: pageContext.setJspWriter(jspWriter);
101: pageContext.setRequest(request);
102:
103: tag.setPageContext(pageContext);
104: tag.setValue("title");
105:
106: try {
107: tag.doStartTag();
108: } catch (JspException e) {
109: e.printStackTrace();
110: fail();
111: }
112:
113: request.verify();
114: jspWriter.verify();
115: pageContext.verify();
116: }
117:
118: public void testTopOfStack() {
119: PropertyTag tag = new PropertyTag();
120:
121: Foo foo = new Foo();
122: foo.setTitle("test");
123:
124: stack.push(foo);
125:
126: MockJspWriter jspWriter = new MockJspWriter();
127: jspWriter.setExpectedData("Foo is: test");
128:
129: MockPageContext pageContext = new MockPageContext();
130: pageContext.setJspWriter(jspWriter);
131: pageContext.setRequest(request);
132:
133: tag.setPageContext(pageContext);
134:
135: try {
136: tag.doStartTag();
137: } catch (JspException e) {
138: e.printStackTrace();
139: fail();
140: }
141:
142: request.verify();
143: jspWriter.verify();
144: pageContext.verify();
145: }
146:
147: public void testWithAltSyntax1() throws Exception {
148: // setups
149: Configuration.set(WebWorkConstants.WEBWORK_TAG_ALTSYNTAX,
150: "true");
151: assertEquals(Configuration
152: .get(WebWorkConstants.WEBWORK_TAG_ALTSYNTAX), "true");
153:
154: Foo foo = new Foo();
155: foo.setTitle("tm_jee");
156: stack.push(foo);
157:
158: MockJspWriter jspWriter = new MockJspWriter();
159: jspWriter.setExpectedData("Foo is: tm_jee");
160:
161: MockPageContext pageContext = new MockPageContext();
162: pageContext.setJspWriter(jspWriter);
163: pageContext.setRequest(request);
164:
165: // test
166: {
167: PropertyTag tag = new PropertyTag();
168: tag.setPageContext(pageContext);
169: tag.setValue("%{toString()}");
170: tag.doStartTag();
171: tag.doEndTag();
172: }
173:
174: // verify test
175: request.verify();
176: jspWriter.verify();
177: pageContext.verify();
178: }
179:
180: public void testWithAltSyntax2() throws Exception {
181: // setups
182: Configuration.set(WebWorkConstants.WEBWORK_TAG_ALTSYNTAX,
183: "true");
184: assertEquals(Configuration
185: .get(WebWorkConstants.WEBWORK_TAG_ALTSYNTAX), "true");
186:
187: Foo foo = new Foo();
188: foo.setTitle("tm_jee");
189: stack.push(foo);
190:
191: MockJspWriter jspWriter = new MockJspWriter();
192: jspWriter.setExpectedData("Foo is: tm_jee");
193:
194: MockPageContext pageContext = new MockPageContext();
195: pageContext.setJspWriter(jspWriter);
196: pageContext.setRequest(request);
197:
198: // test
199: {
200: PropertyTag tag = new PropertyTag();
201: tag.setPageContext(pageContext);
202: tag.setValue("toString()");
203: tag.doStartTag();
204: tag.doEndTag();
205: }
206:
207: // verify test
208: request.verify();
209: jspWriter.verify();
210: pageContext.verify();
211: }
212:
213: public void testWithoutAltSyntax1() throws Exception {
214: // setups
215: Configuration.set(WebWorkConstants.WEBWORK_TAG_ALTSYNTAX,
216: "false");
217: assertEquals(Configuration
218: .get(WebWorkConstants.WEBWORK_TAG_ALTSYNTAX), "false");
219:
220: Foo foo = new Foo();
221: foo.setTitle("tm_jee");
222: stack.push(foo);
223:
224: MockJspWriter jspWriter = new MockJspWriter();
225: jspWriter.setExpectedData("Foo is: tm_jee");
226:
227: MockPageContext pageContext = new MockPageContext();
228: pageContext.setJspWriter(jspWriter);
229: pageContext.setRequest(request);
230:
231: // test
232: {
233: PropertyTag tag = new PropertyTag();
234: tag.setPageContext(pageContext);
235: tag.setValue("toString()");
236: tag.doStartTag();
237: tag.doEndTag();
238: }
239:
240: // verify test
241: request.verify();
242: jspWriter.verify();
243: pageContext.verify();
244: }
245:
246: public void testWithoutAltSyntax2() throws Exception {
247: // setups
248: Configuration.set(WebWorkConstants.WEBWORK_TAG_ALTSYNTAX,
249: "false");
250: assertEquals(Configuration
251: .get(WebWorkConstants.WEBWORK_TAG_ALTSYNTAX), "false");
252:
253: Foo foo = new Foo();
254: foo.setTitle("tm_jee");
255: stack.push(foo);
256:
257: MockJspWriter jspWriter = new MockJspWriter();
258:
259: MockPageContext pageContext = new MockPageContext();
260: pageContext.setJspWriter(jspWriter);
261: pageContext.setRequest(request);
262:
263: // test
264: {
265: PropertyTag tag = new PropertyTag();
266: tag.setPageContext(pageContext);
267: tag.setValue("%{toString()}");
268: tag.doStartTag();
269: tag.doEndTag();
270: }
271:
272: // verify test
273: request.verify();
274: jspWriter.verify();
275: pageContext.verify();
276: }
277:
278: protected void setUp() throws Exception {
279: super .setUp();
280: ActionContext.getContext().setValueStack(stack);
281: request.setAttribute(
282: ServletActionContext.WEBWORK_VALUESTACK_KEY, stack);
283: }
284:
285: public class Foo {
286: private String title;
287:
288: public void setTitle(String title) {
289: this .title = title;
290: }
291:
292: public String getTitle() {
293: return title;
294: }
295:
296: public String toString() {
297: return "Foo is: " + title;
298: }
299: }
300: }
|