001: package com.mockrunner.test.web;
002:
003: import java.io.StringWriter;
004:
005: import javax.servlet.jsp.tagext.TagAdapter;
006:
007: import junit.framework.TestCase;
008:
009: import com.mockrunner.mock.web.MockJspFragment;
010: import com.mockrunner.mock.web.MockJspWriter;
011: import com.mockrunner.mock.web.MockPageContext;
012: import com.mockrunner.tag.NestedBodyTag;
013: import com.mockrunner.tag.NestedSimpleTag;
014: import com.mockrunner.tag.NestedStandardTag;
015:
016: public class MockJspFragmentTest extends TestCase {
017: private MockJspFragment fragment;
018: private MockPageContext context;
019:
020: protected void setUp() throws Exception {
021: super .setUp();
022: context = new MockPageContext();
023: fragment = new MockJspFragment(context);
024: }
025:
026: public void testTextChilds() {
027: assertEquals(0, fragment.getChilds().size());
028: fragment.addTextChild("atext");
029: fragment.addTextChild("anothertext");
030: assertEquals(2, fragment.getChilds().size());
031: assertEquals("atext", fragment.getChild(0));
032: assertEquals("anothertext", fragment.getChild(1));
033: try {
034: fragment.getChild(2);
035: fail();
036: } catch (IndexOutOfBoundsException exc) {
037: //should throw exception
038: }
039: fragment.removeChilds();
040: assertEquals(0, fragment.getChilds().size());
041: }
042:
043: public void testAddChildWithNullParent() {
044: assertNull(fragment.getParent());
045: NestedStandardTag standardTag = (NestedStandardTag) fragment
046: .addTagChild(TestTag.class);
047: NestedBodyTag bodyTag = (NestedBodyTag) fragment
048: .addTagChild(TestBodyTag.class);
049: NestedSimpleTag simpleTag = (NestedSimpleTag) fragment
050: .addTagChild(TestSimpleTag.class);
051: assertNotNull(standardTag);
052: assertNotNull(bodyTag);
053: assertNotNull(simpleTag);
054: assertNull(standardTag.getParent());
055: assertNull(bodyTag.getParent());
056: assertNull(simpleTag.getParent());
057: assertEquals(3, fragment.getChilds().size());
058: assertSame(standardTag, fragment.getChild(0));
059: assertSame(bodyTag, fragment.getChild(1));
060: assertSame(simpleTag, fragment.getChild(2));
061: }
062:
063: public void testAddChildWithSimpleTagParent() {
064: TestSimpleTag parent = new TestSimpleTag();
065: fragment.setParent(parent);
066: NestedStandardTag standardTag = (NestedStandardTag) fragment
067: .addTagChild(TestTag.class);
068: NestedBodyTag bodyTag = (NestedBodyTag) fragment
069: .addTagChild(TestBodyTag.class);
070: NestedSimpleTag simpleTag = (NestedSimpleTag) fragment
071: .addTagChild(TestSimpleTag.class);
072: assertNotNull(standardTag);
073: assertNotNull(bodyTag);
074: assertNotNull(simpleTag);
075: assertTrue(standardTag.getParent() instanceof TagAdapter);
076: assertSame(parent, ((TagAdapter) standardTag.getParent())
077: .getAdaptee());
078: assertTrue(bodyTag.getParent() instanceof TagAdapter);
079: assertSame(parent, ((TagAdapter) bodyTag.getParent())
080: .getAdaptee());
081: assertSame(parent, simpleTag.getParent());
082: assertEquals(3, fragment.getChilds().size());
083: assertSame(standardTag, fragment.getChild(0));
084: assertSame(bodyTag, fragment.getChild(1));
085: assertSame(simpleTag, fragment.getChild(2));
086: }
087:
088: public void testAddChildWithBodyTagParent() {
089: TestBodyTag parent = new TestBodyTag();
090: fragment.setParent(parent);
091: NestedStandardTag standardTag = (NestedStandardTag) fragment
092: .addTagChild(TestTag.class);
093: NestedBodyTag bodyTag = (NestedBodyTag) fragment
094: .addTagChild(TestBodyTag.class);
095: NestedSimpleTag simpleTag = (NestedSimpleTag) fragment
096: .addTagChild(TestSimpleTag.class);
097: assertNotNull(standardTag);
098: assertNotNull(bodyTag);
099: assertNotNull(simpleTag);
100: assertSame(parent, standardTag.getParent());
101: assertSame(parent, bodyTag.getParent());
102: assertSame(parent, simpleTag.getParent());
103: assertEquals(3, fragment.getChilds().size());
104: assertSame(standardTag, fragment.getChild(0));
105: assertSame(bodyTag, fragment.getChild(1));
106: assertSame(simpleTag, fragment.getChild(2));
107: }
108:
109: public void testInvoke() throws Exception {
110: fragment.addTextChild("text1");
111: fragment.addTagChild(TestTag.class);
112: fragment.addTagChild(TestBodyTag.class);
113: fragment.addTagChild(TestSimpleTag.class);
114: fragment.addTextChild("text2");
115: StringWriter writer = new StringWriter();
116: context.getOut().print("before");
117: fragment.invoke(writer);
118: context.getOut().print("after");
119: assertEquals("text1TestTagTestBodyTagTestSimpleTagtext2",
120: writer.toString());
121: String outText = ((MockJspWriter) context.getOut())
122: .getOutputAsString();
123: assertEquals("beforeafter", outText);
124: context.getOut().clearBuffer();
125: context.getOut().print("before");
126: fragment.invoke(null);
127: context.getOut().print("after");
128: outText = ((MockJspWriter) context.getOut())
129: .getOutputAsString();
130: assertEquals(
131: "beforetext1TestTagTestBodyTagTestSimpleTagtext2after",
132: outText);
133: }
134: }
|