001: package com.mockrunner.test.web;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005:
006: import javax.servlet.jsp.tagext.BodyTagSupport;
007: import javax.servlet.jsp.tagext.TagSupport;
008:
009: import com.mockrunner.base.BaseTestCase;
010: import com.mockrunner.mock.web.MockBodyContent;
011: import com.mockrunner.mock.web.MockJspWriter;
012: import com.mockrunner.tag.DynamicChild;
013: import com.mockrunner.tag.NestedStandardTag;
014: import com.mockrunner.tag.NestedTag;
015:
016: public class TagLifecycleTest extends BaseTestCase {
017: private NestedTag root;
018: private NestedTag level1child1;
019: private NestedTag level1child2;
020: private NestedTag level1child3;
021: private NestedTag level2child1;
022: private NestedTag level2child2;
023: private NestedTag level3child1;
024: private NestedTag level3child2;
025: private NestedTag level3child3;
026:
027: private TestTag rootTag;
028: private TestBodyTag level1child1Tag;
029: private TestTag level1child2Tag;
030: private TestBodyTag level1child3Tag;
031: private TestTag level2child1Tag;
032: private TestBodyTag level2child2Tag;
033: private TestTag level3child1Tag;
034: private TestTag level3child2Tag;
035: private TestSimpleTag level3child3Tag;
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039: Map testMap = new HashMap();
040: testMap.put("testString", "test");
041: root = new NestedStandardTag(new TestTag(),
042: getWebMockObjectFactory().getMockPageContext(), testMap);
043: level1child1 = root.addTagChild(TestBodyTag.class, testMap);
044: root.addTextChild("roottext");
045: level1child2 = root.addTagChild(TestTag.class, testMap);
046: level1child3 = root.addTagChild(TestBodyTag.class, testMap);
047: level1child3.addTextChild("level1textchild3");
048: level1child3.addDynamicChild(new TestDynamicChild(
049: "dynamicchild1"));
050: level2child1 = level1child2.addTagChild(TestTag.class, testMap);
051: level2child2 = level1child2.addTagChild(TestBodyTag.class,
052: testMap);
053: level1child2.addTextChild("level2textchild2");
054: level3child1 = level2child1.addTagChild(TestTag.class, testMap);
055: level3child2 = level2child2.addTagChild(TestTag.class, testMap);
056: level3child3 = level2child2.addTagChild(TestSimpleTag.class,
057: testMap);
058: level2child2.addDynamicChild(new TestDynamicChild(
059: "dynamicchild2"));
060:
061: rootTag = (TestTag) root.getTag();
062: level1child1Tag = (TestBodyTag) level1child1.getTag();
063: level1child2Tag = (TestTag) level1child2.getTag();
064: level1child3Tag = (TestBodyTag) level1child3.getTag();
065: level2child1Tag = (TestTag) level2child1.getTag();
066: level2child2Tag = (TestBodyTag) level2child2.getTag();
067: level3child1Tag = (TestTag) level3child1.getTag();
068: level3child2Tag = (TestTag) level3child2.getTag();
069: level3child3Tag = (TestSimpleTag) level3child3.getWrappedTag();
070: }
071:
072: public void testMethodsCalled() throws Exception {
073: root.setDoReleaseRecursive(true);
074: level1child1Tag
075: .setDoStartTagReturnValue(TagSupport.EVAL_BODY_INCLUDE);
076: level1child2Tag.setDoStartTagReturnValue(TagSupport.SKIP_BODY);
077: level1child3Tag
078: .setDoStartTagReturnValue(BodyTagSupport.EVAL_BODY_BUFFERED);
079: root.doLifecycle();
080: assertTrue(rootTag.wasDoStartTagCalled());
081: assertTrue(rootTag.wasDoAfterBodyCalled());
082: assertTrue(rootTag.wasDoEndTagCalled());
083: assertTrue(rootTag.wasReleaseCalled());
084: assertTrue(rootTag.wasReleaseCallLastMethodCall());
085:
086: assertTrue(level1child1Tag.wasDoStartTagCalled());
087: assertFalse(level1child1Tag.wasDoInitBodyCalled());
088: assertTrue(level1child1Tag.wasDoAfterBodyCalled());
089: assertTrue(level1child1Tag.wasDoEndTagCalled());
090: assertTrue(level1child1Tag.wasReleaseCalled());
091: assertTrue(level1child1Tag.wasReleaseCallLastMethodCall());
092:
093: assertTrue(level1child2Tag.wasDoStartTagCalled());
094: assertFalse(level1child2Tag.wasDoAfterBodyCalled());
095: assertTrue(level1child2Tag.wasDoEndTagCalled());
096: assertTrue(level1child2Tag.wasReleaseCalled());
097: assertTrue(level1child2Tag.wasReleaseCallLastMethodCall());
098:
099: assertTrue(level1child3Tag.wasDoStartTagCalled());
100: assertTrue(level1child3Tag.wasDoInitBodyCalled());
101: assertTrue(level1child3Tag.wasDoAfterBodyCalled());
102: assertTrue(level1child3Tag.wasDoEndTagCalled());
103: assertTrue(level1child3Tag.wasReleaseCalled());
104: assertTrue(level1child3Tag.wasReleaseCallLastMethodCall());
105:
106: assertFalse(level2child1Tag.wasDoStartTagCalled());
107: assertFalse(level2child1Tag.wasDoAfterBodyCalled());
108: assertFalse(level2child1Tag.wasDoEndTagCalled());
109: assertFalse(level2child1Tag.wasReleaseCalled());
110:
111: assertFalse(level2child2Tag.wasDoStartTagCalled());
112: assertFalse(level2child2Tag.wasDoInitBodyCalled());
113: assertFalse(level2child2Tag.wasDoAfterBodyCalled());
114: assertFalse(level2child2Tag.wasDoEndTagCalled());
115: assertFalse(level2child2Tag.wasReleaseCalled());
116:
117: assertFalse(level3child1Tag.wasDoStartTagCalled());
118: assertFalse(level3child1Tag.wasDoAfterBodyCalled());
119: assertFalse(level3child1Tag.wasDoEndTagCalled());
120: assertFalse(level3child1Tag.wasReleaseCalled());
121:
122: assertFalse(level3child2Tag.wasDoStartTagCalled());
123: assertFalse(level3child2Tag.wasDoAfterBodyCalled());
124: assertFalse(level3child2Tag.wasDoEndTagCalled());
125: assertFalse(level3child3Tag.wasDoTagCalled());
126:
127: level1child2Tag
128: .setDoStartTagReturnValue(TagSupport.EVAL_BODY_INCLUDE);
129: root.doLifecycle();
130:
131: assertTrue(level3child2Tag.wasDoStartTagCalled());
132: assertTrue(level3child2Tag.wasDoAfterBodyCalled());
133: assertTrue(level3child2Tag.wasDoEndTagCalled());
134: assertTrue(level3child3Tag.wasDoTagCalled());
135: }
136:
137: public void testPopulated() throws Exception {
138: level1child1Tag
139: .setDoStartTagReturnValue(TagSupport.EVAL_BODY_INCLUDE);
140: level1child2Tag.setDoStartTagReturnValue(TagSupport.SKIP_BODY);
141: level1child3Tag
142: .setDoStartTagReturnValue(BodyTagSupport.EVAL_BODY_BUFFERED);
143: root.doLifecycle();
144: assertEquals("test", rootTag.getTestString());
145: assertEquals("test", level1child1Tag.getTestString());
146: assertEquals("test", level1child2Tag.getTestString());
147: assertEquals("test", level1child2Tag.getTestString());
148: assertNull(level2child1Tag.getTestString());
149: assertNull(level2child2Tag.getTestString());
150: assertNull(level3child1Tag.getTestString());
151: assertNull(level3child2Tag.getTestString());
152: assertNull(level3child3Tag.getTestString());
153: level1child2Tag
154: .setDoStartTagReturnValue(TagSupport.EVAL_BODY_INCLUDE);
155: root.doLifecycle();
156: assertEquals("test", level2child1Tag.getTestString());
157: assertEquals("test", level2child2Tag.getTestString());
158: assertEquals("test", level3child1Tag.getTestString());
159: assertEquals("test", level3child2Tag.getTestString());
160: assertEquals("test", level3child3Tag.getTestString());
161: }
162:
163: public void testOutput() throws Exception {
164: level1child1Tag.setDoStartTagReturnValue(TagSupport.SKIP_BODY);
165: level1child2Tag.setDoStartTagReturnValue(TagSupport.SKIP_BODY);
166: level1child3Tag
167: .setDoStartTagReturnValue(BodyTagSupport.EVAL_BODY_BUFFERED);
168: root.doLifecycle();
169: assertEquals("TestTagTestBodyTagroottextTestTagTestBodyTag",
170: getTagOutput());
171: assertEquals("level1textchild3dynamicchild1",
172: ((MockBodyContent) level1child3Tag.getBufferedOut())
173: .getOutputAsString());
174: clearOutput();
175: level1child3Tag
176: .setDoStartTagReturnValue(BodyTagSupport.EVAL_BODY_INCLUDE);
177: root.doLifecycle();
178: assertEquals(
179: "TestTagTestBodyTagroottextTestTagTestBodyTaglevel1textchild3dynamicchild1",
180: getTagOutput());
181: clearOutput();
182: level1child3Tag
183: .setDoAfterBodyReturnValue(BodyTagSupport.EVAL_BODY_AGAIN);
184: root.doLifecycle();
185: assertEquals(
186: "TestTagTestBodyTagroottextTestTagTestBodyTaglevel1textchild3dynamicchild1level1textchild3dynamicchild1",
187: getTagOutput());
188: clearOutput();
189: level1child2Tag
190: .setDoStartTagReturnValue(TagSupport.EVAL_BODY_INCLUDE);
191: level1child3Tag
192: .setDoStartTagReturnValue(BodyTagSupport.SKIP_BODY);
193: root.doLifecycle();
194: assertEquals(
195: "TestTagTestBodyTagroottextTestTagTestTagTestTagTestBodyTagTestTagTestSimpleTagdynamicchild2level2textchild2TestBodyTag",
196: getTagOutput());
197: }
198:
199: private String getTagOutput() {
200: return ((MockJspWriter) (getWebMockObjectFactory()
201: .getMockPageContext().getOut())).getOutputAsString();
202: }
203:
204: private void clearOutput() throws Exception {
205: ((MockJspWriter) (getWebMockObjectFactory()
206: .getMockPageContext().getOut())).clearBuffer();
207: }
208:
209: private class TestDynamicChild implements DynamicChild {
210: private String value;
211:
212: public TestDynamicChild(String value) {
213: this .value = value;
214: }
215:
216: public Object evaluate() {
217: return value;
218: }
219: }
220: }
|