001: package com.mockrunner.test.web;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.HashMap;
006: import java.util.Map;
007:
008: import javax.el.ValueExpression;
009: import javax.servlet.jsp.JspApplicationContext;
010: import javax.servlet.jsp.JspContext;
011: import javax.servlet.jsp.JspException;
012: import javax.servlet.jsp.JspFactory;
013: import javax.servlet.jsp.PageContext;
014: import javax.servlet.jsp.tagext.BodyContent;
015: import javax.servlet.jsp.tagext.BodyTag;
016: import javax.servlet.jsp.tagext.BodyTagSupport;
017: import javax.servlet.jsp.tagext.JspFragment;
018: import javax.servlet.jsp.tagext.SimpleTag;
019: import javax.servlet.jsp.tagext.Tag;
020: import javax.servlet.jsp.tagext.TagSupport;
021:
022: import com.mockrunner.base.BaseTestCase;
023: import com.mockrunner.mock.web.JasperJspFactory;
024: import com.mockrunner.mock.web.MockPageContext;
025: import com.mockrunner.tag.DynamicAttribute;
026: import com.mockrunner.tag.NestedBodyTag;
027: import com.mockrunner.tag.NestedSimpleTag;
028: import com.mockrunner.tag.NestedStandardTag;
029: import com.mockrunner.tag.NestedTag;
030: import com.mockrunner.tag.RuntimeAttribute;
031:
032: public class NestedTagTest extends BaseTestCase {
033: private NestedTag nestedTagRoot;
034: private MockPageContext context;
035: private Tag testTag;
036: private Tag testTag1;
037: private Tag testTag11;
038: private SimpleTag testTag111;
039: private NestedTag testTagChild1;
040: private NestedTag testTagChild11;
041: private NestedTag testTagChild111;
042: private SimpleTag rootSimpleTag;
043: private Map testMap;
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047: testMap = new HashMap();
048: testMap.put("testString", "test");
049: context = getWebMockObjectFactory().getMockPageContext();
050: }
051:
052: private void prepareStandardTagTest() {
053: testTag = new TestTag();
054: nestedTagRoot = new NestedStandardTag(testTag, context, testMap);
055: testTagChild1 = nestedTagRoot.addTagChild(new TestTag(),
056: testMap);
057: nestedTagRoot.addTextChild("test");
058: testTagChild11 = testTagChild1.addTagChild(TestTag.class,
059: testMap);
060: testTag1 = (TestTag) testTagChild1.getTag();
061: testTag11 = (TestTag) testTagChild11.getTag();
062: }
063:
064: private void prepareBodyTagTest() {
065: testTag = new TestBodyTag();
066: nestedTagRoot = new NestedBodyTag((BodyTagSupport) testTag,
067: context, testMap);
068: testTagChild1 = nestedTagRoot.addTagChild(TestTag.class,
069: testMap);
070: testTagChild11 = testTagChild1.addTagChild(new TestBodyTag(),
071: testMap);
072: testTagChild1.addTextChild("bodytest");
073: testTag1 = (TestTag) testTagChild1.getTag();
074: testTag11 = (TestBodyTag) testTagChild11.getTag();
075: }
076:
077: private void prepareSimpleTagTest() {
078: Map map = new HashMap();
079: map.put("stringProperty", "test");
080: map.put("floatProperty", "1");
081: rootSimpleTag = new TestSimpleTag();
082: nestedTagRoot = new NestedSimpleTag(rootSimpleTag, context, map);
083: testTagChild1 = nestedTagRoot.addTagChild(TestTag.class,
084: testMap);
085: testTagChild11 = testTagChild1.addTagChild(new TestBodyTag(),
086: testMap);
087: testTagChild1.addTextChild("simpletest");
088: testTag1 = (TestTag) testTagChild1.getTag();
089: testTag11 = (TestBodyTag) testTagChild11.getTag();
090: testTagChild111 = testTagChild11
091: .addTagChild(TestSimpleTag.class);
092: testTag111 = (SimpleTag) testTagChild111.getWrappedTag();
093: }
094:
095: public void testGetWrappedTag() {
096: BodyTag testBodyTag = new TestBodyTag();
097: Tag testStandardTag = new TestTag();
098: SimpleTag testSimpleTag = new TestSimpleTag();
099: NestedBodyTag nestedBodyTag = new NestedBodyTag(testBodyTag,
100: context);
101: NestedStandardTag nestedStandardTag = new NestedStandardTag(
102: testStandardTag, context);
103: NestedSimpleTag nestedSimpleTag = new NestedSimpleTag(
104: testSimpleTag, context);
105: assertSame(testBodyTag, nestedBodyTag.getTag());
106: assertSame(testBodyTag, nestedBodyTag.getWrappedTag());
107: assertSame(testStandardTag, nestedStandardTag.getTag());
108: assertSame(testStandardTag, nestedStandardTag.getWrappedTag());
109: assertSame(testSimpleTag, nestedSimpleTag.getWrappedTag());
110: try {
111: nestedSimpleTag.getTag();
112: fail();
113: } catch (RuntimeException exc) {
114: //should throw exception
115: }
116: }
117:
118: public void testAddTagChild() {
119: testTag = new TestBodyTag();
120: nestedTagRoot = new NestedBodyTag((BodyTagSupport) testTag,
121: context, testMap);
122: TestTag childTestTag = new TestTag();
123: nestedTagRoot.addTagChild(childTestTag);
124: nestedTagRoot.addTagChild(childTestTag);
125: nestedTagRoot.addTagChild(TestSimpleTag.class);
126: nestedTagRoot.addTagChild(TestBodyTag.class);
127: assertEquals(4, nestedTagRoot.getChilds().size());
128: assertSame(childTestTag,
129: ((NestedTag) nestedTagRoot.getChild(0)).getTag());
130: assertSame(childTestTag,
131: ((NestedTag) nestedTagRoot.getChild(1)).getTag());
132: assertNotSame(childTestTag, ((NestedTag) nestedTagRoot
133: .getChild(2)).getWrappedTag());
134: assertNotSame(childTestTag, ((NestedBodyTag) nestedTagRoot
135: .getChild(3)).getTag());
136: NestedBodyTag bodyTag = (NestedBodyTag) nestedTagRoot
137: .getChild(3);
138: bodyTag.addTagChild(TestTag.class);
139: bodyTag.addTagChild(new TestTag());
140: bodyTag.addTagChild(TestBodyTag.class);
141: bodyTag.addTagChild(new TestBodyTag());
142: assertEquals(4, bodyTag.getChilds().size());
143: assertTrue(((NestedTag) bodyTag.getChild(0)).getTag() instanceof TestTag);
144: assertTrue(((NestedTag) bodyTag.getChild(1)).getTag() instanceof TestTag);
145: assertTrue(((NestedTag) bodyTag.getChild(2)).getTag() instanceof TestBodyTag);
146: assertTrue(((NestedTag) bodyTag.getChild(3)).getTag() instanceof TestBodyTag);
147: NestedSimpleTag simpleTag = (NestedSimpleTag) nestedTagRoot
148: .getChild(2);
149: simpleTag.addTagChild(TestBodyTag.class);
150: simpleTag.addTagChild(new TestSimpleTag());
151: assertEquals(2, simpleTag.getChilds().size());
152: assertTrue(((NestedTag) simpleTag.getChild(0)).getWrappedTag() instanceof TestBodyTag);
153: assertTrue(((NestedTag) simpleTag.getChild(1)).getWrappedTag() instanceof TestSimpleTag);
154: NestedStandardTag standardtag = (NestedStandardTag) nestedTagRoot
155: .getChild(0);
156: standardtag.addTagChild(new TestSimpleTag());
157: assertEquals(1, standardtag.getChilds().size());
158: assertTrue(((NestedTag) standardtag.getChild(0))
159: .getWrappedTag() instanceof TestSimpleTag);
160: }
161:
162: public void testFindTag() {
163: AnotherTestTag anotherTestTag = new AnotherTestTag();
164: NestedStandardTag root = new NestedStandardTag(anotherTestTag,
165: context);
166: NestedTag child1 = root.addTagChild(TestTag.class, testMap);
167: NestedTag child11 = child1.addTagChild(TestTag.class, testMap);
168: Tag foundTag = TagSupport.findAncestorWithClass((Tag) child11
169: .getWrappedTag(), AnotherTestTag.class);
170: assertNotNull(foundTag);
171: }
172:
173: public void testPopulateAttributesStandard() {
174: prepareStandardTagTest();
175: nestedTagRoot.populateAttributes();
176: assertEquals("test", ((TestTag) testTag).getTestString());
177: assertNull(((TestTag) testTag1).getTestString());
178: assertNull(((TestTag) testTag11).getTestString());
179: }
180:
181: public void testPopulateAttributesBody() {
182: prepareBodyTagTest();
183: nestedTagRoot.populateAttributes();
184: assertEquals("test", ((TestBodyTag) testTag).getTestString());
185: assertNull(((TestTag) testTag1).getTestString());
186: assertNull(((TestBodyTag) testTag11).getTestString());
187: }
188:
189: public void testPopulateAttributesSimple() {
190: prepareSimpleTagTest();
191: nestedTagRoot.populateAttributes();
192: assertEquals("test", ((TestSimpleTag) rootSimpleTag)
193: .getStringProperty());
194: assertEquals(1, ((TestSimpleTag) rootSimpleTag)
195: .getFloatProperty(), 0.0);
196: }
197:
198: public void testPopulateDynamicAndRuntimeAttributes() {
199: Map map = new HashMap();
200: map.put("dynamicAttribute1", "test");
201: map.put("dynamicAttribute2", new Integer(1));
202: map.put("dynamicAttribute3", new TestRuntimeAttribute(new Long(
203: 3)));
204: map.put("dynamicAttribute4", new DynamicAttribute(null,
205: new TestRuntimeAttribute(new Byte((byte) 4))));
206: TestSimpleTag simpleTag = new TestSimpleTag();
207: NestedTag nestedTag = new NestedSimpleTag(simpleTag, context,
208: map);
209: nestedTag.populateAttributes();
210: assertEquals(4, simpleTag.getDynamicAttributesMap().size());
211: DynamicAttribute attribute1 = (DynamicAttribute) simpleTag
212: .getDynamicAttributesMap().get("dynamicAttribute1");
213: DynamicAttribute attribute2 = (DynamicAttribute) simpleTag
214: .getDynamicAttributesMap().get("dynamicAttribute2");
215: DynamicAttribute attribute3 = (DynamicAttribute) simpleTag
216: .getDynamicAttributesMap().get("dynamicAttribute3");
217: DynamicAttribute attribute4 = (DynamicAttribute) simpleTag
218: .getDynamicAttributesMap().get("dynamicAttribute4");
219: assertNull(attribute1.getUri());
220: assertEquals("test", attribute1.getValue());
221: assertNull(attribute2.getUri());
222: assertEquals(new Integer(1), attribute2.getValue());
223: assertNull(attribute3.getUri());
224: assertEquals(new Long(3), attribute3.getValue());
225: assertNull(attribute4.getUri());
226: assertEquals(new Byte((byte) 4), attribute4.getValue());
227: map = new HashMap();
228: map.put("dynamicAttribute1", "test");
229: map.put("dynamicAttribute2", new Integer(1));
230: map.put("dynamicAttribute3", new TestRuntimeAttribute(new Long(
231: 3)));
232: TestBodyTag bodyTag = new TestBodyTag();
233: nestedTag = new NestedBodyTag(bodyTag, context, map);
234: nestedTag.populateAttributes();
235: map.put("dynamicAttribute4", new DynamicAttribute("test",
236: "test"));
237: try {
238: nestedTag.populateAttributes();
239: fail();
240: } catch (IllegalArgumentException exc) {
241: //should throw exception
242: }
243: }
244:
245: public void testPopulateValueExpressionAttribute() throws Exception {
246: getWebMockObjectFactory().setDefaultJspFactory(
247: new JasperJspFactory()
248: .configure(getWebMockObjectFactory()));
249: JspApplicationContext applicationContext = JspFactory
250: .getDefaultFactory().getJspApplicationContext(
251: getWebMockObjectFactory().getMockPageContext()
252: .getServletContext());
253: ValueExpression valueExpression = applicationContext
254: .getExpressionFactory().createValueExpression(
255: getWebMockObjectFactory().getMockPageContext()
256: .getELContext(),
257: "#{sessionScope.test}", String.class);
258: Map map = new HashMap();
259: map.put("valueExpressionProperty", valueExpression);
260: TestSimpleTag simpleTag = new TestSimpleTag();
261: NestedTag nestedTag = new NestedSimpleTag(simpleTag, context,
262: map);
263: nestedTag.populateAttributes();
264: assertSame(valueExpression, simpleTag
265: .getValueExpressionProperty());
266: getWebMockObjectFactory().getMockSession().setAttribute("test",
267: "testValue");
268: nestedTag.doLifecycle();
269: assertEquals("testValue", simpleTag.getValueExpressionResult());
270: }
271:
272: public void testSetPageContextStandard() {
273: prepareStandardTagTest();
274: MockPageContext newContext = new MockPageContext(null, null,
275: null);
276: ((NestedStandardTag) nestedTagRoot).setPageContext(newContext);
277: assertTrue(((TestTag) testTag).getPageContext() == newContext);
278: assertTrue(((TestTag) testTag1).getPageContext() == newContext);
279: assertTrue(((TestTag) testTag11).getPageContext() == newContext);
280: }
281:
282: public void testSetPageContextBody() {
283: prepareBodyTagTest();
284: MockPageContext newContext = new MockPageContext(null, null,
285: null);
286: ((NestedBodyTag) nestedTagRoot).setPageContext(newContext);
287: assertTrue(((TestBodyTag) testTag).getPageContext() == newContext);
288: assertTrue(((TestTag) testTag1).getPageContext() == newContext);
289: assertTrue(((TestBodyTag) testTag11).getPageContext() == newContext);
290: }
291:
292: public void testSetJspContextSimple() {
293: prepareSimpleTagTest();
294: MockPageContext newContext = new MockPageContext(null, null,
295: null);
296: ((NestedSimpleTag) nestedTagRoot).setJspContext(newContext);
297: assertTrue(((TestSimpleTag) rootSimpleTag).getJspContext() == newContext);
298: assertTrue(((TestTag) testTag1).getPageContext() == newContext);
299: assertTrue(((TestBodyTag) testTag11).getPageContext() == newContext);
300: assertTrue(((TestSimpleTag) testTag111).getJspContext() == newContext);
301: }
302:
303: public void testSetDoReleaseStandard() throws Exception {
304: prepareStandardTagTest();
305: nestedTagRoot.setDoRelease(true);
306: nestedTagRoot.doLifecycle();
307: assertTrue(((TestTag) testTag).wasReleaseCalled());
308: assertFalse(((TestTag) testTag1).wasReleaseCalled());
309: assertFalse(((TestTag) testTag11).wasReleaseCalled());
310: nestedTagRoot.setDoReleaseRecursive(true);
311: nestedTagRoot.doLifecycle();
312: assertTrue(((TestTag) testTag).wasReleaseCalled());
313: assertTrue(((TestTag) testTag1).wasReleaseCalled());
314: assertTrue(((TestTag) testTag11).wasReleaseCalled());
315: }
316:
317: public void testSetDoReleaseBody() throws Exception {
318: prepareBodyTagTest();
319: nestedTagRoot.setDoRelease(true);
320: nestedTagRoot.doLifecycle();
321: assertTrue(((TestBodyTag) testTag).wasReleaseCalled());
322: assertFalse(((TestTag) testTag1).wasReleaseCalled());
323: assertFalse(((TestBodyTag) testTag11).wasReleaseCalled());
324: nestedTagRoot.setDoReleaseRecursive(true);
325: nestedTagRoot.doLifecycle();
326: assertTrue(((TestBodyTag) testTag).wasReleaseCalled());
327: assertTrue(((TestTag) testTag1).wasReleaseCalled());
328: assertTrue(((TestBodyTag) testTag11).wasReleaseCalled());
329: }
330:
331: public void testChildsWithCustomFragmentSimpleTag()
332: throws Exception {
333: TestSimpleTag testSimpleTag = new TestSimpleTag();
334: NestedSimpleTag nestedSimpleTag = new NestedSimpleTag(
335: testSimpleTag, context);
336: nestedSimpleTag.setJspBody(new TestJspFragment());
337: nestedSimpleTag.addTagChild(TestTag.class);
338: nestedSimpleTag.addTextChild("text");
339: assertNull(nestedSimpleTag.getChilds());
340: assertNull(nestedSimpleTag.getChild(0));
341: nestedSimpleTag.removeChilds();
342: assertNull(nestedSimpleTag.getChilds());
343: assertNull(nestedSimpleTag.getChild(0));
344: }
345:
346: public void testNotTagSupportInstanceStandard() throws Exception {
347: MyTestTag myTag = new MyTestTag();
348: NestedStandardTag tag = new NestedStandardTag(myTag, context);
349: assertSame(myTag, tag.getWrappedTag());
350: try {
351: tag.getTag();
352: fail();
353: } catch (RuntimeException exc) {
354: //should throw exception
355: }
356: try {
357: tag.getId();
358: fail();
359: } catch (RuntimeException exc) {
360: //should throw exception
361: }
362: try {
363: tag.getValue("");
364: fail();
365: } catch (RuntimeException exc) {
366: //should throw exception
367: }
368: try {
369: tag.removeValue("");
370: fail();
371: } catch (RuntimeException exc) {
372: //should throw exception
373: }
374: }
375:
376: public void testNotTagSupportInstanceBody() throws Exception {
377: MyTestTag myTag = new MyTestTag();
378: NestedBodyTag tag = new NestedBodyTag(myTag, context);
379: assertSame(myTag, tag.getWrappedTag());
380: try {
381: tag.getTag();
382: fail();
383: } catch (RuntimeException exc) {
384: //should throw exception
385: }
386: try {
387: tag.getId();
388: fail();
389: } catch (RuntimeException exc) {
390: //should throw exception
391: }
392: try {
393: tag.setValue("", "");
394: fail();
395: } catch (RuntimeException exc) {
396: //should throw exception
397: }
398: try {
399: tag.getPreviousOut();
400: fail();
401: } catch (RuntimeException exc) {
402: //should throw exception
403: }
404: try {
405: tag.getBodyContent();
406: fail();
407: } catch (RuntimeException exc) {
408: //should throw exception
409: }
410: }
411:
412: public void testNoIterationTag() throws Exception {
413: NestedStandardTag tag = new NestedStandardTag(
414: new NoIterationTag(), context);
415: assertEquals(Tag.EVAL_PAGE, tag.doLifecycle());
416: }
417:
418: public void testTryCatchFinallyHandlingStandardTag()
419: throws Exception {
420: ExceptionTestTag testTag = new ExceptionTestTag();
421: NestedStandardTag tag = new NestedStandardTag(testTag, context);
422: RuntimeException excToBeThrown = new RuntimeException();
423: tag.addTagChild(new FailureTestTag(excToBeThrown));
424: assertEquals(-1, tag.doLifecycle());
425: assertTrue(testTag.wasDoCatchCalled());
426: assertTrue(testTag.wasDoFinallyCalled());
427: assertSame(excToBeThrown, testTag.getCaughtException());
428: RuntimeException excToBeRethrown = new RuntimeException();
429: testTag = new ExceptionTestTag(excToBeRethrown);
430: tag = new NestedStandardTag(testTag, context);
431: excToBeThrown = new RuntimeException();
432: tag.addTagChild(new FailureTestTag(excToBeThrown));
433: try {
434: tag.doLifecycle();
435: } catch (Exception exc) {
436: assertTrue(testTag.wasDoCatchCalled());
437: assertTrue(testTag.wasDoFinallyCalled());
438: assertSame(excToBeThrown, testTag.getCaughtException());
439: assertSame(excToBeRethrown, exc);
440: }
441: }
442:
443: public void testTryCatchFinallyHandlingBodyTag() throws Exception {
444: ExceptionTestTag testTag = new ExceptionTestTag();
445: NestedBodyTag tag = new NestedBodyTag(testTag, context);
446: RuntimeException excToBeThrown = new RuntimeException();
447: tag.addTagChild(new FailureTestTag(excToBeThrown));
448: assertEquals(-1, tag.doLifecycle());
449: assertTrue(testTag.wasDoCatchCalled());
450: assertTrue(testTag.wasDoFinallyCalled());
451: assertSame(excToBeThrown, testTag.getCaughtException());
452: RuntimeException excToBeRethrown = new RuntimeException();
453: testTag = new ExceptionTestTag(excToBeRethrown);
454: tag = new NestedBodyTag(testTag, context);
455: excToBeThrown = new RuntimeException();
456: tag.addTagChild(new FailureTestTag(excToBeThrown));
457: try {
458: tag.doLifecycle();
459: } catch (Exception exc) {
460: assertTrue(testTag.wasDoCatchCalled());
461: assertTrue(testTag.wasDoFinallyCalled());
462: assertSame(excToBeThrown, testTag.getCaughtException());
463: assertSame(excToBeRethrown, exc);
464: }
465: }
466:
467: private class TestRuntimeAttribute implements RuntimeAttribute {
468: private Object value;
469:
470: public TestRuntimeAttribute(Object value) {
471: this .value = value;
472: }
473:
474: public Object evaluate() {
475: return value;
476: }
477: }
478:
479: private class FailureTestTag extends TagSupport {
480: private RuntimeException exc;
481:
482: public FailureTestTag(RuntimeException exc) {
483: this .exc = exc;
484: }
485:
486: public int doStartTag() throws JspException {
487: throw exc;
488: }
489: }
490:
491: private class TestJspFragment extends JspFragment {
492: public JspContext getJspContext() {
493: return null;
494: }
495:
496: public void invoke(Writer writer) throws JspException,
497: IOException {
498:
499: }
500: }
501:
502: private class AnotherTestTag extends TagSupport {
503:
504: }
505:
506: private class MyTestTag implements BodyTag {
507: public int doEndTag() throws JspException {
508: return 0;
509: }
510:
511: public int doStartTag() throws JspException {
512: return 0;
513: }
514:
515: public Tag getParent() {
516: return null;
517: }
518:
519: public void release() {
520:
521: }
522:
523: public void setPageContext(PageContext context) {
524:
525: }
526:
527: public void setParent(Tag parent) {
528:
529: }
530:
531: public void doInitBody() throws JspException {
532:
533: }
534:
535: public void setBodyContent(BodyContent content) {
536:
537: }
538:
539: public int doAfterBody() throws JspException {
540: return 0;
541: }
542: }
543:
544: private class NoIterationTag implements Tag {
545: public int doStartTag() throws JspException {
546: return Tag.EVAL_BODY_INCLUDE;
547: }
548:
549: public int doEndTag() throws JspException {
550: return Tag.EVAL_PAGE;
551: }
552:
553: public Tag getParent() {
554: return null;
555: }
556:
557: public void release() {
558:
559: }
560:
561: public void setPageContext(PageContext context) {
562:
563: }
564:
565: public void setParent(Tag parent) {
566:
567: }
568: }
569: }
|