KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > form > test > ATagTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.form.test;
8
9
10 import javax.servlet.ServletException JavaDoc;
11 import javax.servlet.jsp.JspException JavaDoc;
12 import javax.servlet.jsp.tagext.Tag JavaDoc;
13
14 import com.inversoft.junit.JspTestCase;
15 import com.inversoft.junit.internal.http.MockHttpServletRequest;
16 import com.inversoft.verge.config.VergeConfigConstants;
17 import com.inversoft.verge.config.servlet.ConfigServlet;
18 import com.inversoft.verge.mvc.controller.form.FormURLTools;
19 import com.inversoft.verge.mvc.controller.form.config.test.FormMVCBuilderTest2;
20 import com.inversoft.verge.mvc.model.ModelResolution;
21 import com.inversoft.verge.mvc.view.jsp.form.ATag;
22 import com.inversoft.verge.mvc.view.jsp.model.ModelResolutionRegistry;
23
24
25 /**
26  * <p>
27  * This class has the tests for the image tag
28  * </p>
29  *
30  * @author Brian Pontarelli
31  * @since 2.0
32  * @version 2.0
33  */

34 public class ATagTest extends JspTestCase {
35
36     // This should only be called once, because of the ThreadLocal stuff in the
37
// ConfigRegistry for the Form Basec MVC
38
static {
39         // Build the config to test
40
FormMVCBuilderTest2 test = new FormMVCBuilderTest2("testValidate");
41         test.setRequest(new MockHttpServletRequest(null)); // For ConfigRegistry stuff
42
test.testValidate();
43     }
44
45     /**
46      * Constructor for ImageTagTest.
47      * @param name
48      */

49     public ATagTest(String JavaDoc name) {
50         super(name);
51         setLocal(true);
52     }
53
54
55     /**
56      * Tests a simple anchor tag usage
57      */

58     public void testSimple() {
59         ATag tag = new ATag();
60         tag.setPageContext(pageContext);
61         tag.setAction("testAction");
62         tag.setForm("form1");
63         tag.setBodyContent(createBodyContent("link"));
64
65         try {
66             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
67             String JavaDoc tagStr = getPageContext().getMockOut().getText();
68
69             String JavaDoc expected = "<a HREF=\"" +
70                 FormURLTools.generateURL("form1", "testAction") + "\">link</a>";
71             System.out.println("Expected: " + expected);
72             System.out.println("Actual: " + tagStr);
73             assertEquals("Should be the a tag", expected, tagStr);
74         } catch (JspException JavaDoc e) {
75             fail(e.toString());
76         }
77     }
78
79     /**
80      * Tests a form bean usage scenario
81      */

82     public void testFormBeans() {
83         if (isLocal()) {
84             getContext().setInitParameter(VergeConfigConstants.CONTEXT_PARAM,
85                 "src/com/inversoft/verge/mvc/view/jsp/form/test/test-config.xml");
86         }
87
88         ConfigServlet cs = new ConfigServlet();
89         try {
90             cs.init(createConfig("ConfigServlet"));
91         } catch (ServletException JavaDoc se) {
92             fail(se.toString());
93         }
94
95         ATag tag = new ATag();
96         tag.setPageContext(pageContext);
97         tag.setAction("testAction");
98         tag.setForm("testForm");
99         tag.setBodyContent(createBodyContent("link"));
100
101         try {
102             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
103             String JavaDoc tagStr = getPageContext().getMockOut().getText();
104
105             String JavaDoc expected = "<a HREF=\"" +
106                 FormURLTools.generateURL("testForm", "testAction") + "\">link</a>";
107             System.out.println("Expected: " + expected);
108             System.out.println("Actual: " + tagStr);
109             assertEquals("Should be the a tag", expected, tagStr);
110
111             ModelResolution mr = ModelResolutionRegistry.lookup("User", pageContext);
112             assertNotNull(mr);
113         } catch (JspException JavaDoc e) {
114             fail(e.toString());
115         }
116     }
117
118     /**
119      * Tests a form bean usage scenario
120      */

121     public void testWithoutForm() {
122         if (isLocal()) {
123             getContext().setInitParameter(VergeConfigConstants.CONTEXT_PARAM,
124                 "src/com/inversoft/verge/mvc/view/jsp/form/test/test-config.xml");
125         }
126
127         ConfigServlet cs = new ConfigServlet();
128         try {
129             cs.init(createConfig("ConfigServlet"));
130         } catch (ServletException JavaDoc se) {
131             fail(se.toString());
132         }
133
134         ATag tag = new ATag();
135         tag.setPageContext(pageContext);
136         tag.setAction("testAction");
137         tag.setBodyContent(createBodyContent("link"));
138
139         try {
140             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
141             String JavaDoc tagStr = getPageContext().getMockOut().getText();
142
143             String JavaDoc expected = "<a HREF=\"" +
144                 FormURLTools.generateURL(null, "testAction") + "\">link</a>";
145             System.out.println("Expected: " + expected);
146             System.out.println("Actual: " + tagStr);
147             assertEquals("Should be the a tag", expected, tagStr);
148         } catch (JspException JavaDoc e) {
149             fail(e.toString());
150         }
151     }
152 }
Popular Tags