KickJava   Java API By Example, From Geeks To Geeks.

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


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.BasicError;
14 import com.inversoft.error.PropertyError;
15 import com.inversoft.junit.JspTestCallback;
16 import com.inversoft.junit.JspTestCase;
17 import com.inversoft.verge.mvc.MVCException;
18 import com.inversoft.verge.mvc.model.ModelResolution;
19 import com.inversoft.verge.mvc.model.web.WebMetaData;
20 import com.inversoft.verge.mvc.view.jsp.error.ErrorsTag;
21 import com.inversoft.verge.mvc.view.jsp.model.ModelResolutionRegistry;
22 import com.inversoft.verge.util.RequestContext;
23 import com.inversoft.verge.util.ScopeConstants;
24
25
26 /**
27  * <p>
28  * Tests the ErrorsTag class
29  * </p>
30  *
31  * @author Brian Pontarelli
32  * @since 2.0
33  * @version 2.0
34  */

35 public class ErrorsTagTest extends JspTestCase {
36
37     private boolean called;
38
39     /**
40      * Constructs a new <code>ErrorsTagTest</code>
41      *
42      * @param name The name of the currently executing test
43      */

44     public ErrorsTagTest(String JavaDoc name) {
45         super(name);
46         setLocal(true);
47     }
48
49
50     /**
51      * Test that the tag iterates over the errors
52      */

53     public void testIteration() {
54         RequestContext context = new RequestContext(request);
55         context.addError(new BasicError("error1"));
56         context.addError(new BasicError("error2"));
57         context.addError(new BasicError("error3"));
58         context.addError(new BasicError("error4"));
59
60         ErrorsTag tag = new ErrorsTag();
61         tag.setVar("error");
62         tag.setPageContext(pageContext);
63
64         assertEquals("error", tag.getVar());
65
66         try {
67             assertEquals("Should return EVAL_PAGE", runTag(tag, 4,
68                 new JspTestCallback() {
69                     public void callback() {
70                         assertTrue("Should have error in pageContext",
71                             pageContext.getAttribute("error") != null);
72                     }
73                 }), Tag.EVAL_PAGE);
74         } catch (JspException JavaDoc jspe) {
75             fail(jspe.toString());
76         }
77     }
78
79     /**
80      * Test that the tag iterates over all the errors
81      */

82     public void testIterationAll() {
83         RequestContext context = new RequestContext(request);
84         context.addError(new BasicError("error1"));
85         context.addError(new BasicError("error2"));
86         context.addError(new BasicError("error3"));
87         context.addError(new BasicError("error4"));
88         context.addError(new PropertyError("perror1"));
89         context.addError(new PropertyError("perror2"));
90
91         ErrorsTag tag = new ErrorsTag();
92         tag.setVar("error");
93         tag.setPageContext(pageContext);
94
95         try {
96             assertEquals("Should return EVAL_PAGE", runTag(tag, 6,
97                 new JspTestCallback() {
98                     public void callback() {
99                         assertTrue("Should have error in pageContext",
100                             pageContext.getAttribute("error") != null);
101                     }
102                 }), Tag.EVAL_PAGE);
103         } catch (JspException JavaDoc jspe) {
104             fail(jspe.toString());
105         }
106     }
107
108     /**
109      * Test that PropertyErrors are excluded if the flag is set
110      */

111     public void testExcludeProperties() {
112         RequestContext context = new RequestContext(request);
113         context.addError(new BasicError("error1"));
114         context.addError(new BasicError("error2"));
115         context.addError(new BasicError("error3"));
116         context.addError(new BasicError("error4"));
117         context.addError(new PropertyError("perror1"));
118         context.addError(new PropertyError("perror2"));
119
120         ErrorsTag tag = new ErrorsTag();
121         tag.setVar("error");
122         tag.setExcludeProperties(true);
123         tag.setPageContext(pageContext);
124
125         assertTrue(tag.isExcludeProperties());
126
127         try {
128             assertEquals("Should return EVAL_PAGE", runTag(tag, 4,
129                 new JspTestCallback() {
130                     public void callback() {
131                         assertTrue("Should have error in pageContext",
132                             pageContext.getAttribute("error") != null);
133                     }
134                 }), Tag.EVAL_PAGE);
135         } catch (JspException JavaDoc jspe) {
136             fail(jspe.toString());
137         }
138     }
139
140     /**
141      * Test that the tag skips if there are no errors
142      */

143     public void testNoErrors() {
144         /*RequestContext context =*/ new RequestContext(request);
145
146         ErrorsTag tag = new ErrorsTag();
147         tag.setVar("error");
148         tag.setPageContext(pageContext);
149
150         try {
151             assertEquals("Should return SKIP_BODY", tag.doStartTag(), Tag.SKIP_BODY);
152         } catch (JspException JavaDoc jspe) {
153             fail(jspe.toString());
154         }
155     }
156
157     /**
158      * Tests that the tag iterates over a single PropertyError if setup to
159      */

160     public void testProperty() {
161         RequestContext context = new RequestContext(request);
162         context.addError(new BasicError("error1"));
163         context.addError(new BasicError("error2"));
164         context.addError(new BasicError("error3"));
165         context.addError(new BasicError("error4"));
166         context.addError(new PropertyError("perror1"));
167         context.addError(new PropertyError("perror2"));
168
169         ErrorsTag tag = new ErrorsTag();
170         tag.setVar("error");
171         tag.setProperty("perror1");
172         tag.setPageContext(pageContext);
173
174         assertEquals("perror1", tag.getProperty());
175
176         called = false;
177         JspTestCallback callback = new JspTestCallback() {
178             public void callback() {
179                 assertTrue("Should have error in pageContext",
180                     pageContext.getAttribute("error") != null);
181                 called = true;
182             }
183         };
184
185         try {
186             assertEquals("Should return EVAL_PAGE", runTag(tag, 1, callback),
187                 Tag.EVAL_PAGE);
188             assertTrue(called);
189         } catch (JspException JavaDoc jspe) {
190             fail(jspe.toString());
191         }
192     }
193
194     /**
195      * Tests that the tag does nothing on bad properties.
196      */

197     public void testBadProperty() {
198         RequestContext context = new RequestContext(request);
199         context.addError(new BasicError("error1"));
200         context.addError(new BasicError("error2"));
201         context.addError(new BasicError("error3"));
202         context.addError(new BasicError("error4"));
203         context.addError(new PropertyError("perror1"));
204         context.addError(new PropertyError("perror2"));
205
206         ErrorsTag tag = new ErrorsTag();
207         tag.setVar("error");
208         tag.setProperty("badProp");
209         tag.setPageContext(pageContext);
210
211         called = false;
212         JspTestCallback callback = new JspTestCallback() {
213             public void callback() {
214                 called = true;
215             }
216         };
217
218         try {
219             assertEquals("Should return EVAL_PAGE", runTag(tag, 0, callback),
220                 Tag.EVAL_PAGE);
221             assertFalse(called);
222         } catch (JspException JavaDoc jspe) {
223             fail(jspe.toString());
224         }
225     }
226
227     /**
228      * Tests that the tag correctly converts the name of the property
229      */

230     public void testNameConversion() {
231         WebMetaData wmd = null;
232         try {
233             wmd = new WebMetaData("webBean", "com.inversoft.verge.mvc.test.Customer",
234                 ScopeConstants.SESSION_INT);
235         } catch (MVCException mvce) {
236             fail(mvce.toString());
237         }
238
239         ModelResolution mr = new ModelResolution(new Object JavaDoc(), wmd);
240         ModelResolutionRegistry.store("pageBean", mr, pageContext);
241         RequestContext context = new RequestContext(request);
242         context.addError(new PropertyError("webBean.firstName"));
243
244         ErrorsTag tag = new ErrorsTag();
245         tag.setVar("error");
246         tag.setProperty("pageBean.firstName");
247         tag.setPageContext(pageContext);
248
249         try {
250             assertEquals("Should return EVAL_PAGE", runTag(tag, 1,
251                 new JspTestCallback() {
252                     public void callback() {
253                         assertTrue("Should have error in pageContext",
254                             pageContext.getAttribute("error") != null);
255                     }
256                 }), Tag.EVAL_PAGE);
257         } catch (JspException JavaDoc jspe) {
258             fail(jspe.toString());
259         }
260     }
261
262     /**
263      * Tests that the tag correctly converts the name of the property, but fails
264      * because the property name if bogus.
265      */

266     public void testNameConversionFailure() {
267         WebMetaData wmd = null;
268         try {
269             wmd = new WebMetaData("webBean", "com.inversoft.verge.mvc.test.Customer",
270                 ScopeConstants.SESSION_INT);
271         } catch (MVCException mvce) {
272             fail(mvce.toString());
273         }
274
275         ModelResolution mr = new ModelResolution(new Object JavaDoc(), wmd);
276         ModelResolutionRegistry.store("pageBean", mr, pageContext);
277         RequestContext context = new RequestContext(request);
278         context.addError(new PropertyError("webBean.firstName"));
279
280         ErrorsTag tag = new ErrorsTag();
281         tag.setVar("error");
282         tag.setProperty("badBean.firstName");
283         tag.setPageContext(pageContext);
284
285         called = false;
286         JspTestCallback callback = new JspTestCallback() {
287             public void callback() {
288                 called = true;
289             }
290         };
291
292         try {
293             assertEquals("Should return EVAL_PAGE", runTag(tag, 0, callback),
294                 Tag.EVAL_PAGE);
295         } catch (JspException JavaDoc jspe) {
296             fail(jspe.toString());
297         }
298     }
299
300     /**
301      * Tests that the tag correctly converts the name of the property, but fails
302      * because the model resolution is missing.
303      */

304     public void testNameConversionFailureMissingModel() {
305         RequestContext context = new RequestContext(request);
306         context.addError(new PropertyError("webBean.firstName"));
307
308         ErrorsTag tag = new ErrorsTag();
309         tag.setVar("error");
310         tag.setProperty("pageBean.firstName");
311         tag.setPageContext(pageContext);
312
313         called = false;
314         JspTestCallback callback = new JspTestCallback() {
315             public void callback() {
316                 called = true;
317             }
318         };
319
320         try {
321             assertEquals("Should return EVAL_PAGE", runTag(tag, 0, callback),
322                 Tag.EVAL_PAGE);
323         } catch (JspException JavaDoc jspe) {
324             fail(jspe.toString());
325         }
326     }
327
328     /**
329      * Tests that the tag correctly fails if the attributes are wrong.
330      */

331     public void testFailure() {
332         WebMetaData wmd = null;
333         try {
334             wmd = new WebMetaData("webBean", "com.inversoft.verge.mvc.test.Customer",
335                 ScopeConstants.SESSION_INT);
336         } catch (MVCException mvce) {
337             fail(mvce.toString());
338         }
339
340         ModelResolution mr = new ModelResolution(new Object JavaDoc(), wmd);
341         ModelResolutionRegistry.store("pageBean", mr, pageContext);
342         RequestContext context = new RequestContext(request);
343         context.addError(new PropertyError("webBean.firstName"));
344
345         ErrorsTag tag = new ErrorsTag();
346         tag.setVar("error");
347         tag.setProperty("pageBean.firstName");
348         tag.setExcludeProperties(true);
349         tag.setPageContext(pageContext);
350
351         try {
352             runTag(tag);
353             fail("Should have failed because of attributes");
354         } catch (JspException JavaDoc jspe) {
355             // Okay
356
}
357     }
358 }
Popular Tags