001: /*
002: * Copyright (c) 2002-2007 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp;
006:
007: import javax.servlet.jsp.JspException;
008:
009: import com.mockobjects.servlet.MockBodyContent;
010:
011: /**
012: * @author plightbo
013: * @author tmjee
014: * @version $Date: 2007-03-23 19:33:24 +0100 (Fri, 23 Mar 2007) $ $Id: SetTagTest.java 2870 2007-03-23 18:33:24Z tm_jee $
015: */
016: public class SetTagTest extends AbstractUITagTest {
017:
018: Chewbacca chewie;
019: SetTag tag;
020:
021: public void testSetTagWithBodyContent() throws Exception {
022: MockBodyContent bodyContent = new MockBodyContent() {
023: public String getString() {
024: return "testing testing 123";
025: }
026: };
027: tag.setName("foo");
028: tag.doStartTag();
029: tag.setBodyContent(bodyContent);
030: tag.doInitBody();
031: tag.doAfterBody();
032: tag.doEndTag();
033:
034: assertEquals(stack.getContext().get("foo"),
035: "testing testing 123");
036: }
037:
038: public void testSetTagWithEmptyBody() throws Exception {
039: MockBodyContent bodyContent = new MockBodyContent() {
040: public String getString() {
041: return "";
042: }
043: };
044: tag.setValue("%{'Hello World'}");
045: tag.setName("foo");
046: tag.doStartTag();
047: tag.setBodyContent(bodyContent);
048: tag.doInitBody();
049: tag.doAfterBody();
050: tag.doEndTag();
051:
052: assertEquals(stack.getContext().get("foo"), "Hello World");
053: }
054:
055: public void testSetTagWithNullBody() throws Exception {
056: MockBodyContent bodyContent = new MockBodyContent() {
057: public String getString() {
058: return null;
059: }
060: };
061: tag.setValue("%{'Hello World'}");
062: tag.setName("foo");
063: tag.doStartTag();
064: tag.setBodyContent(bodyContent);
065: tag.doInitBody();
066: tag.doAfterBody();
067: tag.doEndTag();
068:
069: assertEquals(stack.getContext().get("foo"), "Hello World");
070: }
071:
072: public void testSetTagWithBodyAndScope() throws Exception {
073: MockBodyContent bodyContent = new MockBodyContent() {
074: public String getString() {
075: return "testing testing 123";
076: }
077: };
078: tag.setScope("request");
079: tag.setName("foo");
080: tag.doStartTag();
081: tag.setBodyContent(bodyContent);
082: tag.doInitBody();
083: tag.doAfterBody();
084: tag.doEndTag();
085:
086: assertEquals(request.getAttribute("foo"), "testing testing 123");
087: assertEquals(stack.getContext().get("foo"),
088: "testing testing 123");
089: }
090:
091: public void testApplicationScope() throws JspException {
092: tag.setName("foo");
093: tag.setValue("name");
094: tag.setScope("application");
095: tag.doStartTag();
096: tag.doEndTag();
097:
098: assertEquals("chewie", servletContext.getAttribute("foo"));
099: }
100:
101: public void testPageScope() throws JspException {
102: tag.setName("foo");
103: tag.setValue("name");
104: tag.setScope("page");
105: tag.doStartTag();
106: tag.doEndTag();
107:
108: assertEquals("chewie", pageContext.getAttribute("foo"));
109: }
110:
111: public void testRequestScope() throws JspException {
112: tag.setName("foo");
113: tag.setValue("name");
114: tag.setScope("request");
115: tag.doStartTag();
116: tag.doEndTag();
117: assertEquals("chewie", request.getAttribute("foo"));
118: }
119:
120: public void testSessionScope() throws JspException {
121: tag.setName("foo");
122: tag.setValue("name");
123: tag.setScope("session");
124: tag.doStartTag();
125: tag.doEndTag();
126:
127: assertEquals("chewie", session.get("foo"));
128: }
129:
130: public void testWebWorkScope() throws JspException {
131: tag.setName("foo");
132: tag.setValue("name");
133: tag.doStartTag();
134: tag.doEndTag();
135: assertEquals("chewie", context.get("foo"));
136: }
137:
138: public void testWebWorkScope2() throws JspException {
139: tag.setName("chewie");
140: tag.doStartTag();
141: tag.doEndTag();
142: assertEquals(chewie, context.get("chewie"));
143: }
144:
145: protected void setUp() throws Exception {
146: super .setUp();
147:
148: tag = new SetTag();
149: chewie = new Chewbacca("chewie", true);
150: stack.push(chewie);
151: tag.setPageContext(pageContext);
152: }
153:
154: public class Chewbacca {
155: String name;
156: boolean furry;
157:
158: public Chewbacca(String name, boolean furry) {
159: this .name = name;
160: this .furry = furry;
161: }
162:
163: public void setFurry(boolean furry) {
164: this .furry = furry;
165: }
166:
167: public boolean isFurry() {
168: return furry;
169: }
170:
171: public void setName(String name) {
172: this .name = name;
173: }
174:
175: public String getName() {
176: return name;
177: }
178: }
179: }
|