001: package com.mockrunner.example.tag;
002:
003: import java.io.BufferedReader;
004: import java.util.ArrayList;
005: import java.util.HashMap;
006: import java.util.Map;
007:
008: import org.apache.struts.taglib.bean.WriteTag;
009: import org.jdom.Element;
010:
011: import com.mockrunner.tag.BasicTagTestCaseAdapter;
012: import com.mockrunner.tag.NestedTag;
013: import com.mockrunner.util.web.XmlUtil;
014:
015: /**
016: * Example test for the {@link TableEnumTag}.
017: * Demonstrates the usage of {@link com.mockrunner.tag.TagTestModule}
018: * and {@link com.mockrunner.tag.BasicTagTestCaseAdapter} and
019: * {@link com.mockrunner.tag.NestedTag}.
020: * Tests the html output of {@link TableEnumTag} with static
021: * and dynamic body content (simulated by nesting the <code>WriteTag</code>).
022: * Demonstrates the testing of output data as string as well as parsed
023: * HTML data (<code>testStaticBodyAsXML</code>).
024: */
025: public class TableEnumTagTest extends BasicTagTestCaseAdapter {
026: private NestedTag nestedTag;
027:
028: protected void setUp() throws Exception {
029: super .setUp();
030: Map attributeMap = new HashMap();
031: attributeMap.put("label", "myLabel");
032: nestedTag = createNestedTag(TableEnumTag.class, attributeMap);
033: storeTestListInSession();
034: }
035:
036: private void storeTestListInSession() {
037: ArrayList list = new ArrayList();
038: list.add("Entry1");
039: list.add("Entry2");
040: list.add("Entry3");
041: getWebMockObjectFactory().getMockSession().setAttribute(
042: "currentCollection", list);
043: }
044:
045: public void testNoBody() throws Exception {
046: processTagLifecycle();
047: BufferedReader reader = getOutputAsBufferedReader();
048: assertEquals("<table>", reader.readLine().trim());
049: assertEquals("</table>", reader.readLine().trim());
050: }
051:
052: public void testStaticBody() throws Exception {
053: nestedTag.addTextChild("myStaticValue");
054: processTagLifecycle();
055: BufferedReader reader = getOutputAsBufferedReader();
056: assertEquals("<table>", reader.readLine().trim());
057: for (int ii = 0; ii < 3; ii++) {
058: assertEquals("<tr>", reader.readLine().trim());
059: assertEquals("<td>", reader.readLine().trim());
060: assertEquals("myLabel", reader.readLine().trim());
061: assertEquals("</td>", reader.readLine().trim());
062: assertEquals("<td>", reader.readLine().trim());
063: assertEquals("myStaticValue", reader.readLine().trim());
064: assertEquals("</td>", reader.readLine().trim());
065: assertEquals("</tr>", reader.readLine().trim());
066: }
067: assertEquals("</table>", reader.readLine().trim());
068: }
069:
070: public void testStaticBodyAsXML() throws Exception {
071: nestedTag.addTextChild("myStaticValue");
072: processTagLifecycle();
073: Element table = XmlUtil
074: .getBodyFragmentFromJDOMDocument(getOutputAsJDOMDocument());
075: Element tr = table.getChild("tr");
076: assertEquals("myLabel", tr.getChildTextTrim("td"));
077: Element secondTd = (Element) tr.getChildren().get(1);
078: assertEquals("myStaticValue", secondTd.getTextTrim());
079: }
080:
081: public void testDynamicBody() throws Exception {
082: Map attributeMap = new HashMap();
083: attributeMap.put("scope", "request");
084: attributeMap.put("name", "currentObject");
085: nestedTag.addTagChild(WriteTag.class, attributeMap);
086: processTagLifecycle();
087: BufferedReader reader = getOutputAsBufferedReader();
088: assertEquals("<table>", reader.readLine().trim());
089: for (int ii = 1; ii <= 3; ii++) {
090: assertEquals("<tr>", reader.readLine().trim());
091: assertEquals("<td>", reader.readLine().trim());
092: assertEquals("myLabel", reader.readLine().trim());
093: assertEquals("</td>", reader.readLine().trim());
094: assertEquals("<td>", reader.readLine().trim());
095: assertEquals("Entry" + ii, reader.readLine().trim());
096: assertEquals("</td>", reader.readLine().trim());
097: assertEquals("</tr>", reader.readLine().trim());
098: }
099: assertEquals("</table>", reader.readLine().trim());
100: }
101: }
|