001: package com.mockrunner.test.web;
002:
003: import java.io.StringWriter;
004: import java.util.HashMap;
005:
006: import javax.servlet.jsp.JspException;
007: import javax.servlet.jsp.PageContext;
008: import javax.servlet.jsp.tagext.BodyContent;
009: import javax.servlet.jsp.tagext.BodyTag;
010: import javax.servlet.jsp.tagext.Tag;
011:
012: import com.mockrunner.base.BaseTestCase;
013: import com.mockrunner.base.NestedApplicationException;
014: import com.mockrunner.base.VerifyFailedException;
015: import com.mockrunner.mock.web.MockJspFragment;
016: import com.mockrunner.tag.DynamicAttribute;
017: import com.mockrunner.tag.NestedBodyTag;
018: import com.mockrunner.tag.NestedSimpleTag;
019: import com.mockrunner.tag.NestedStandardTag;
020: import com.mockrunner.tag.NestedTag;
021: import com.mockrunner.tag.RuntimeAttribute;
022: import com.mockrunner.tag.TagTestModule;
023:
024: public class TagTestModuleTest extends BaseTestCase {
025: private TagTestModule module;
026:
027: protected void setUp() throws Exception {
028: super .setUp();
029: module = new TagTestModule(getWebMockObjectFactory());
030: }
031:
032: public void testCreateAndSetTag() {
033: module.createTag(TestTag.class);
034: assertTrue(module.getTag() instanceof TestTag);
035: assertTrue(module.getNestedTag() instanceof NestedStandardTag);
036: module.createWrappedTag(TestBodyTag.class);
037: assertTrue(module.getTag() instanceof TestBodyTag);
038: assertTrue(module.getNestedTag() instanceof NestedBodyTag);
039: module.createWrappedTag(TestSimpleTag.class);
040: assertTrue(module.getWrappedTag() instanceof TestSimpleTag);
041: assertTrue(module.getNestedTag() instanceof NestedSimpleTag);
042: TestTag testTag = new TestTag();
043: module.setTag(testTag);
044: assertSame(testTag, module.getTag());
045: TestBodyTag testBodyTag = new TestBodyTag();
046: module.setTag(testBodyTag);
047: assertSame(testBodyTag, module.getTag());
048: TestSimpleTag testSimpleTag = new TestSimpleTag();
049: module.setTag(testSimpleTag);
050: assertSame(testSimpleTag, module.getWrappedTag());
051: }
052:
053: public void testCreateTagWithAttributes() {
054: HashMap testMap = new HashMap();
055: testMap.put("testString", "test");
056: testMap.put("dynamicAttribute", "dynamicAttributeValue");
057: testMap.put("stringProperty", new TestRuntimeAttribute(
058: "stringPropertyValue"));
059: module.createWrappedTag(TestSimpleTag.class, testMap);
060: TestSimpleTag simpleTag = (TestSimpleTag) module
061: .getWrappedTag();
062: assertNull(simpleTag.getTestString());
063: assertEquals(0, simpleTag.getDynamicAttributesMap().size());
064: module.populateAttributes();
065: assertEquals("test", simpleTag.getTestString());
066: assertEquals(1, simpleTag.getDynamicAttributesMap().size());
067: assertEquals("dynamicAttributeValue",
068: ((DynamicAttribute) simpleTag.getDynamicAttributesMap()
069: .get("dynamicAttribute")).getValue());
070: assertNull(((DynamicAttribute) simpleTag
071: .getDynamicAttributesMap().get("dynamicAttribute"))
072: .getUri());
073: assertEquals("stringPropertyValue", simpleTag
074: .getStringProperty());
075: module.createTag(TestTag.class, testMap);
076: module.populateAttributes();
077: TestTag tag = (TestTag) module.getWrappedTag();
078: assertEquals("test", tag.getTestString());
079: module.createTag(TestBodyTag.class, testMap);
080: module.populateAttributes();
081: TestBodyTag bodyTag = (TestBodyTag) module.getWrappedTag();
082: assertEquals("test", bodyTag.getTestString());
083: }
084:
085: public void testSetTagWithAttributes() {
086: HashMap testMap = new HashMap();
087: testMap.put("testString", "test");
088: TestTag testTag = new TestTag();
089: testTag.setTestInteger(new Integer(3));
090: module.setTag(testTag, testMap);
091: module.populateAttributes();
092: assertEquals(new Integer(3), testTag.getTestInteger());
093: assertEquals("test", testTag.getTestString());
094: testMap.put("testInteger", new Integer(5));
095: module.setTag(testTag, testMap);
096: module.populateAttributes();
097: assertEquals(new Integer(5), testTag.getTestInteger());
098: assertEquals("test", testTag.getTestString());
099: testMap.put("testString", new TestRuntimeAttribute(
100: "anothervalue"));
101: TestSimpleTag simpleTag = new TestSimpleTag();
102: module.setTag(simpleTag, testMap);
103: module.populateAttributes();
104: assertEquals("anothervalue", simpleTag.getTestString());
105: assertEquals(1, simpleTag.getDynamicAttributesMap().size());
106: assertEquals(new Integer(5), ((DynamicAttribute) simpleTag
107: .getDynamicAttributesMap().get("testInteger"))
108: .getValue());
109: }
110:
111: public void testTagWithJspFragmentAttribute() {
112: HashMap testMap = new HashMap();
113: MockJspFragment fragment = new MockJspFragment(
114: getWebMockObjectFactory().getMockPageContext());
115: fragment.addTextChild("AFragmentText");
116: testMap.put("testFragment", fragment);
117: module.createWrappedTag(TestFragmentTag.class, testMap);
118: module.processTagLifecycle();
119: module.verifyOutput("AFragmentText");
120: }
121:
122: public void testVerifyOutput() {
123: module.createNestedTag(TestTag.class);
124: module.processTagLifecycle();
125: try {
126: module.verifyOutput("testtag");
127: fail();
128: } catch (VerifyFailedException exc) {
129: //should throw exception
130: }
131: module.setCaseSensitive(false);
132: module.verifyOutput("testtag");
133: module.verifyOutputContains("ES");
134: module.verifyOutputRegularExpression("[abT].*");
135: module.setCaseSensitive(true);
136: try {
137: module.verifyOutputContains("ES");
138: fail();
139: } catch (VerifyFailedException exc) {
140: //should throw exception
141: }
142: try {
143: module.verifyOutputRegularExpression("tesT.*");
144: fail();
145: } catch (VerifyFailedException exc) {
146: //should throw exception
147: }
148: }
149:
150: public void testSetDoReleaseCalled() {
151: try {
152: module.setDoRelease(true);
153: fail();
154: } catch (RuntimeException exc) {
155: //should throw exception
156: }
157: try {
158: module.setDoReleaseRecursive(true);
159: fail();
160: } catch (RuntimeException exc) {
161: //should throw exception
162: }
163: TestTag tag = (TestTag) module.createTag(TestTag.class);
164: module.setDoRelease(true);
165: module.processTagLifecycle();
166: assertTrue(tag.wasReleaseCalled());
167: tag = (TestTag) module.createTag(TestTag.class);
168: module.setDoReleaseRecursive(true);
169: module.processTagLifecycle();
170: assertTrue(tag.wasReleaseCalled());
171: }
172:
173: public void testSetBody() throws Exception {
174: try {
175: module.setBody("body");
176: fail();
177: } catch (RuntimeException exc) {
178: //should throw exception
179: }
180: NestedTag tag = module.createNestedTag(TestTag.class);
181: tag.addTagChild(TestTag.class);
182: assertTrue(tag.getChild(0) instanceof NestedTag);
183: module.setBody("body");
184: assertTrue(tag.getChild(0) instanceof String);
185: TestSimpleTag simpleTag = new TestSimpleTag();
186: tag = module.setTag(simpleTag);
187: module.setBody("body");
188: assertTrue(tag.getChild(0) instanceof String);
189: MockJspFragment fragment = (MockJspFragment) ((NestedSimpleTag) tag)
190: .getJspBody();
191: assertTrue(fragment.getChild(0) instanceof String);
192: StringWriter writer = new StringWriter();
193: fragment.invoke(writer);
194: assertEquals("body", writer.toString());
195: }
196:
197: public void testNullTag() {
198: try {
199: module.populateAttributes();
200: fail();
201: } catch (RuntimeException exc) {
202: //should throw exception
203: }
204: try {
205: module.doInitBody();
206: fail();
207: } catch (RuntimeException exc) {
208: //should throw exception
209: }
210: try {
211: module.doStartTag();
212: fail();
213: } catch (RuntimeException exc) {
214: //should throw exception
215: }
216: try {
217: module.doEndTag();
218: fail();
219: } catch (RuntimeException exc) {
220: //should throw exception
221: }
222: try {
223: module.doAfterBody();
224: fail();
225: } catch (RuntimeException exc) {
226: //should throw exception
227: }
228: try {
229: module.processTagLifecycle();
230: fail();
231: } catch (RuntimeException exc) {
232: //should throw exception
233: }
234: }
235:
236: public void testDoMethodsCalled() {
237: TestBodyTag bodyTag = (TestBodyTag) module
238: .createTag(TestBodyTag.class);
239: module.doInitBody();
240: assertTrue(bodyTag.wasDoInitBodyCalled());
241: module.doStartTag();
242: assertTrue(bodyTag.wasDoStartTagCalled());
243: module.doEndTag();
244: assertTrue(bodyTag.wasDoEndTagCalled());
245: module.doAfterBody();
246: assertTrue(bodyTag.wasDoAfterBodyCalled());
247: bodyTag = (TestBodyTag) module.createTag(TestBodyTag.class);
248: module.processTagLifecycle();
249: assertTrue(bodyTag.wasDoStartTagCalled());
250: TestSimpleTag simpleTag = (TestSimpleTag) module
251: .createWrappedTag(TestSimpleTag.class);
252: module.doTag();
253: assertTrue(simpleTag.wasDoTagCalled());
254: simpleTag = (TestSimpleTag) module
255: .createWrappedTag(TestSimpleTag.class);
256: module.processTagLifecycle();
257: assertTrue(simpleTag.wasDoTagCalled());
258: }
259:
260: public void testCreateInvalidParameter() {
261: try {
262: module.createTag(String.class);
263: fail();
264: } catch (IllegalArgumentException exc) {
265: //should throw exception
266: }
267: try {
268: module.createTag(TestSimpleTag.class, new HashMap());
269: fail();
270: } catch (IllegalArgumentException exc) {
271: //should throw exception
272: }
273: try {
274: module.createTag(AnotherTag.class);
275: fail();
276: } catch (IllegalArgumentException exc) {
277: //should throw exception
278: }
279: module.createWrappedTag(AnotherTag.class, new HashMap());
280: module.createWrappedTag(TestSimpleTag.class);
281: try {
282: module.createWrappedTag(String.class);
283: fail();
284: } catch (IllegalArgumentException exc) {
285: //should throw exception
286: }
287: try {
288: module.createNestedTag(String.class);
289: fail();
290: } catch (IllegalArgumentException exc) {
291: //should throw exception
292: }
293: module.createNestedTag(TestSimpleTag.class, new HashMap());
294: }
295:
296: public void testGetTag() {
297: assertNull(module.getTag());
298: assertNull(module.getWrappedTag());
299: module.createWrappedTag(TestSimpleTag.class, new HashMap());
300: try {
301: module.getTag();
302: fail();
303: } catch (RuntimeException exc) {
304: //should throw exception
305: }
306: assertNotNull(module.getWrappedTag());
307: }
308:
309: public void testDoTagInvalidParameter() {
310: module.createWrappedTag(TestSimpleTag.class);
311: module.doTag();
312: try {
313: module.doStartTag();
314: fail();
315: } catch (RuntimeException exc) {
316: //should throw exception
317: }
318: try {
319: module.doEndTag();
320: fail();
321: } catch (RuntimeException exc) {
322: //should throw exception
323: }
324: try {
325: module.doInitBody();
326: fail();
327: } catch (RuntimeException exc) {
328: //should throw exception
329: }
330: try {
331: module.doAfterBody();
332: fail();
333: } catch (RuntimeException exc) {
334: //should throw exception
335: }
336: module.createWrappedTag(TestTag.class);
337: try {
338: module.doTag();
339: fail();
340: } catch (RuntimeException exc) {
341: //should throw exception
342: }
343: try {
344: module.doInitBody();
345: fail();
346: } catch (RuntimeException exc) {
347: //should throw exception
348: }
349: module.doStartTag();
350: module.doEndTag();
351: module.doAfterBody();
352: module.createWrappedTag(TestBodyTag.class);
353: try {
354: module.doTag();
355: fail();
356: } catch (RuntimeException exc) {
357: //should throw exception
358: }
359: module.doStartTag();
360: module.doEndTag();
361: module.doAfterBody();
362: module.doInitBody();
363: module.createWrappedTag(AnotherTag.class);
364: try {
365: module.doTag();
366: fail();
367: } catch (RuntimeException exc) {
368: //should throw exception
369: }
370: try {
371: module.doInitBody();
372: fail();
373: } catch (RuntimeException exc) {
374: //should throw exception
375: }
376: try {
377: module.doAfterBody();
378: fail();
379: } catch (RuntimeException exc) {
380: //should throw exception
381: }
382: module.doStartTag();
383: module.doEndTag();
384: }
385:
386: public void testDoTagThrowsException() {
387: module.createWrappedTag(ErrorTag.class);
388: try {
389: module.doStartTag();
390: fail();
391: } catch (NestedApplicationException exc) {
392: //should throw exception
393: }
394: try {
395: module.doEndTag();
396: fail();
397: } catch (NestedApplicationException exc) {
398: //should throw exception
399: }
400: try {
401: module.doInitBody();
402: fail();
403: } catch (NestedApplicationException exc) {
404: //should throw exception
405: }
406: try {
407: module.doAfterBody();
408: fail();
409: } catch (NestedApplicationException exc) {
410: //should throw exception
411: }
412: try {
413: module.processTagLifecycle();
414: fail();
415: } catch (NestedApplicationException exc) {
416: //should throw exception
417: }
418: }
419:
420: public void testRelease() {
421: module.createWrappedTag(TestSimpleTag.class);
422: try {
423: module.release();
424: fail();
425: } catch (RuntimeException exc) {
426: //should throw exception
427: }
428: TestTag tag = (TestTag) module.createWrappedTag(TestTag.class);
429: module.release();
430: assertTrue(tag.wasReleaseCalled());
431: }
432:
433: private class TestRuntimeAttribute implements RuntimeAttribute {
434: private Object value;
435:
436: public TestRuntimeAttribute(Object value) {
437: this .value = value;
438: }
439:
440: public Object evaluate() {
441: return value;
442: }
443: }
444:
445: public static class AnotherTag implements Tag {
446: public int doEndTag() throws JspException {
447: return 0;
448: }
449:
450: public int doStartTag() throws JspException {
451: return 0;
452: }
453:
454: public Tag getParent() {
455: return null;
456: }
457:
458: public void release() {
459:
460: }
461:
462: public void setPageContext(PageContext pageContext) {
463:
464: }
465:
466: public void setParent(Tag parent) {
467:
468: }
469: }
470:
471: public static class ErrorTag implements BodyTag {
472: public void doInitBody() throws JspException {
473: throw new JspException("ErrorTag doInitBody");
474: }
475:
476: public void setBodyContent(BodyContent arg0) {
477:
478: }
479:
480: public int doAfterBody() throws JspException {
481: throw new JspException("ErrorTag doAfterBody");
482: }
483:
484: public int doEndTag() throws JspException {
485: throw new JspException("ErrorTag doEndTag");
486: }
487:
488: public int doStartTag() throws JspException {
489: throw new JspException("ErrorTag doStartTag");
490: }
491:
492: public Tag getParent() {
493: return null;
494: }
495:
496: public void release() {
497:
498: }
499:
500: public void setPageContext(PageContext pageContext) {
501:
502: }
503:
504: public void setParent(Tag tag) {
505:
506: }
507: }
508: }
|