KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > error > test > HasErrorsTagTest


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.error.test;
8
9
10 import javax.servlet.jsp.JspException JavaDoc;
11 import javax.servlet.jsp.tagext.Tag JavaDoc;
12
13 import com.inversoft.error.PropertyError;
14 import com.inversoft.junit.JspTestCallback;
15 import com.inversoft.junit.JspTestCase;
16 import com.inversoft.verge.mvc.model.ModelResolution;
17 import com.inversoft.verge.mvc.model.form.FormMetaData;
18 import com.inversoft.verge.mvc.view.jsp.error.HasErrorsTag;
19 import com.inversoft.verge.mvc.view.jsp.model.ModelResolutionRegistry;
20 import com.inversoft.verge.util.RequestContext;
21
22
23 /**
24  * <p>
25  * This class tests the HasErrorsTag.
26  * </p>
27  *
28  * @author Brian Pontarelli
29  * @since 2.0
30  * @version 2.0
31  */

32 public class HasErrorsTagTest extends JspTestCase {
33
34     private boolean called;
35
36     /**
37      * Constructs a new <code>HasErrorsTagTest</code>
38      *
39      * @param name The name of the test case being run
40      */

41     public HasErrorsTagTest(String JavaDoc name) {
42         super(name);
43         setLocal(true);
44     }
45
46
47     /**
48      * Tests a complex situation
49      */

50     public void testComplex() {
51         HasErrorsTag tag = new HasErrorsTag();
52         tag.setProperty("formBean.${type}[${currentCount}]");
53         tag.setEquals(Boolean.TRUE);
54         tag.setPageContext(pageContext);
55
56         assertEquals("formBean.${type}[${currentCount}]", tag.getProperty());
57         assertEquals(Boolean.TRUE, tag.getEquals());
58
59         pageContext.setAttribute("type", "word");
60         pageContext.setAttribute("currentCount", "1");
61
62         // Add the property error
63
RequestContext context = new RequestContext(request);
64         PropertyError error = new PropertyError("webBean.word[1]", "message");
65         context.addError(error);
66
67         // Link the web name to the page name using the MetaData
68
FormMetaData fmd = new FormMetaData("webBean", null);
69         ModelResolution mr = new ModelResolution(new Object JavaDoc(), fmd);
70         ModelResolutionRegistry.store("formBean", mr, pageContext);
71
72         called = false;
73         JspTestCallback callback = new JspTestCallback() {
74             public void callback() {
75                 called = true;
76             }
77         };
78
79         try {
80             assertEquals("Should have returned EVAL_PAGE", runTag(tag, callback),
81                 Tag.EVAL_PAGE);
82             assertTrue("SHould have called body", called);
83         } catch (JspException JavaDoc jspe) {
84             fail(jspe.toString());
85         }
86     }
87
88     /**
89      * Tests no property definition
90      */

91     public void testNoProperty() {
92         HasErrorsTag tag = new HasErrorsTag();
93         tag.setEquals(Boolean.TRUE);
94         tag.setPageContext(pageContext);
95
96         // Add the property error
97
RequestContext context = new RequestContext(request);
98         PropertyError error = new PropertyError("webBean.word[1]", "message");
99         context.addError(error);
100
101         called = false;
102         JspTestCallback callback = new JspTestCallback() {
103             public void callback() {
104                 called = true;
105             }
106         };
107
108         try {
109             assertEquals("Should have returned EVAL_PAGE", runTag(tag, callback),
110                 Tag.EVAL_PAGE);
111             assertTrue("Should have called body", called);
112         } catch (JspException JavaDoc jspe) {
113             fail(jspe.toString());
114         }
115     }
116
117     /**
118      * Tests no errors
119      */

120     public void testNoErrors() {
121         HasErrorsTag tag = new HasErrorsTag();
122         tag.setEquals(Boolean.TRUE);
123         tag.setPageContext(pageContext);
124
125         called = false;
126         JspTestCallback callback = new JspTestCallback() {
127             public void callback() {
128                 called = true;
129             }
130         };
131
132         try {
133             assertEquals("Should have returned EVAL_PAGE", runTag(tag, callback),
134                 Tag.EVAL_PAGE);
135             assertFalse("Should have called body", called);
136         } catch (JspException JavaDoc jspe) {
137             fail(jspe.toString());
138         }
139     }
140
141     /**
142      * Tests a complex situation
143      */

144     public void testVariable() {
145         HasErrorsTag tag = new HasErrorsTag();
146         tag.setProperty("formBean.${type}[${currentCount}]");
147         tag.setEquals(Boolean.TRUE);
148         tag.setPageContext(pageContext);
149         tag.setVar("errorVariable");
150         tag.setValue("value");
151
152         assertEquals("errorVariable", tag.getVar());
153         assertEquals("value", tag.getValue());
154
155         pageContext.setAttribute("type", "word");
156         pageContext.setAttribute("currentCount", "1");
157
158         // Add the property error
159
RequestContext context = new RequestContext(request);
160         PropertyError error = new PropertyError("webBean.word[1]", "message");
161         context.addError(error);
162
163         // Link the web name to the page name using the MetaData
164
FormMetaData fmd = new FormMetaData("webBean", null);
165         ModelResolution mr = new ModelResolution(new Object JavaDoc(), fmd);
166         ModelResolutionRegistry.store("formBean", mr, pageContext);
167
168         called = false;
169         JspTestCallback callback = new JspTestCallback() {
170             public void callback() {
171                 called = true;
172             }
173         };
174
175         try {
176             assertEquals("Should have returned EVAL_PAGE", runTag(tag, callback),
177                 Tag.EVAL_PAGE);
178             assertTrue("SHould have called body", called);
179             assertEquals("value", pageContext.getAttribute("errorVariable"));
180         } catch (JspException JavaDoc jspe) {
181             fail(jspe.toString());
182         }
183     }
184 }
Popular Tags