2
* $Header: /home/cvs/jakarta-struts/contrib/struts-el/src/test/org/apache/strutsel/taglib/html/TestELErrorsTag.java,v 1.6 2004/06/24 01:27:31 jmitchell Exp $
4
* $Date: 2004/06/24 01:27:31 $
6
* Copyright 1999-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.strutsel.taglib.html;
23
import junit.framework.Test;
24
import junit.framework.TestSuite;
25
import org.apache.commons.logging.Log;
26
import org.apache.commons.logging.LogFactory;
27
import org.apache.strutsel.taglib.utils.JspTagTestCase;
30
public class TestELErrorsTag
31
extends JspTagTestCase {
32
protected static final String FOOTER_VALUE = "zzz";
33
protected static final String HEADER_VALUE = "aaa";
34
protected static final String PREFIX_VALUE = "[[[";
35
protected static final String PROPERTY_KEY = "property";
36
protected static final String REQUIRED_TEXT_VALUE_KEY =
38
protected static final String SUFFIX_VALUE = "]]]";
39
protected static final String XXX_VALUE = "xxx";
40
protected static final String YYY_VALUE = "yyy";
41
protected ELErrorsTag elErrorsTag = null;
42
private static Log log =
43
LogFactory.getLog(TestELErrorsTag.class);
45
public TestELErrorsTag(String theName) {
49
public static void main(String[] args) {
50
junit.awtui.TestRunner.main(
51
new String[] { TestELErrorsTag.class.getName() });
54
public static Test suite() {
55
return new TestSuite(TestELErrorsTag.class);
59
elErrorsTag = new ELErrorsTag();
60
elErrorsTag.setPageContext(pageContext);
63
public void testDummy()
68
// * Tests simple global error with all tag attributes set to default, and no
69
// * header, footer, prefix or suffix.
71
// public void testPlain()
72
// throws ServletException, JspException {
73
// HashMapMessageResources messageResources = new HashMapMessageResources(
75
// messageResources.addMessage("error.misc", XXX_VALUE);
76
// String bundle = elErrorsTag.getBundle();
77
// if (bundle == null)
78
// bundle = Action.MESSAGES_KEY;
79
// pageContext.setAttribute(bundle, messageResources,
80
// PageContext.APPLICATION_SCOPE);
82
// ActionErrors errors = new ActionErrors();
83
// ActionError error = new ActionError("error.misc");
84
// errors.add(ActionErrors.GLOBAL_ERROR, error);
85
// pageContext.setAttribute(elErrorsTag.getName(), errors,
86
// PageContext.REQUEST_SCOPE);
88
// HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
89
// String requiredText = XXX_VALUE;
90
// response.addHeader(REQUIRED_TEXT_VALUE_KEY, requiredText);
92
// System.out.println("pageContext[" + pageContext + "]");
93
// System.out.println("name[" + elErrorsTag.getName() + "]");
94
// System.out.println("request[" + pageContext.getRequest() + "]");
95
// System.out.println("session[" + pageContext.getSession() + "]");
96
// int startTagReturn = elErrorsTag.doStartTag();
97
// int afterBodyReturn = elErrorsTag.doAfterBody();
98
// int endTagReturn = elErrorsTag.doEndTag();
101
// public void endPlain(com.meterware.httpunit.WebResponse testResponse) {
103
// TestHelper.printResponse(testResponse);
105
// org.w3c.dom.Document document = testResponse.getDOM();
106
// DOMHelper.printNode(document.getDocumentElement());
108
// String nodeText = DOMHelper.getNodeText(document, "/html/body");
109
// log.debug("nodeText[" + nodeText + "]");
111
// String requiredTextValue = (String)testResponse.getHeaderField(
112
// REQUIRED_TEXT_VALUE_KEY);
114
// if (!nodeText.equals(requiredTextValue)) {
115
// fail("The <errors> tag instance should have resulted " +
116
// "in the text " + "\"" + requiredTextValue +
117
// "\", but instead had the value \"" + nodeText + "\".");
119
// } catch (Exception ex) {
120
// ex.printStackTrace();
126
// * Tests multiple errors put in errors list, with two different property
127
// * names, but referring to the same error, but still using the default
128
// * value of "property", which will cause both errors to be retrieved.
130
// public void testMultiProperties()
131
// throws ServletException, JspException {
132
// HashMapMessageResources messageResources = new HashMapMessageResources(
133
// null, null, true);
134
// messageResources.addMessage("error.misc", XXX_VALUE);
135
// String bundle = elErrorsTag.getBundle();
136
// if (bundle == null)
137
// bundle = Action.MESSAGES_KEY;
138
// pageContext.setAttribute(bundle, messageResources,
139
// PageContext.APPLICATION_SCOPE);
141
// ActionErrors errors = new ActionErrors();
142
// ActionError error = new ActionError("error.misc");
143
// errors.add(PROPERTY_KEY + "1", error);
144
// errors.add(PROPERTY_KEY + "2", error);
145
// pageContext.setAttribute(elErrorsTag.getName(), errors,
146
// PageContext.REQUEST_SCOPE);
148
// HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
149
// String requiredText = XXX_VALUE + " " + XXX_VALUE;
150
// response.addHeader(REQUIRED_TEXT_VALUE_KEY, requiredText);
152
// int startTagReturn = elErrorsTag.doStartTag();
153
// int afterBodyReturn = elErrorsTag.doAfterBody();
154
// int endTagReturn = elErrorsTag.doEndTag();
157
// public void endMultiProperties(com.meterware.httpunit.WebResponse testResponse) {
159
// TestHelper.printResponse(testResponse);
161
// org.w3c.dom.Document document = testResponse.getDOM();
162
// DOMHelper.printNode(document.getDocumentElement());
164
// String nodeText = DOMHelper.getNodeText(document, "/html/body");
165
// log.debug("nodeText[" + nodeText + "]");
167
// String requiredTextValue = (String)testResponse.getHeaderField(
168
// REQUIRED_TEXT_VALUE_KEY);
170
// if (!nodeText.equals(requiredTextValue)) {
171
// fail("The <errors> tag instance should have resulted " +
172
// "in the text " + "\"" + requiredTextValue +
173
// "\", but instead had the value \"" + nodeText + "\".");
175
// } catch (Exception ex) {
176
// ex.printStackTrace();
182
// * Tests putting in two errors in errors list, with two different property
183
// * names, and referring to two different error keys, and the "property"
184
// * attribute set to only one of them.
186
// public void testOnlyWantOneProperty()
187
// throws ServletException, JspException {
188
// elErrorsTag.setPropertyExpr(PROPERTY_KEY + "2");
190
// HashMapMessageResources messageResources = new HashMapMessageResources(
191
// null, null, true);
192
// messageResources.addMessage("error1.misc", XXX_VALUE);
193
// messageResources.addMessage("error2.misc", YYY_VALUE);
194
// String bundle = elErrorsTag.getBundle();
195
// if (bundle == null)
196
// bundle = Action.MESSAGES_KEY;
197
// pageContext.setAttribute(bundle, messageResources,
198
// PageContext.APPLICATION_SCOPE);
200
// ActionErrors errors = new ActionErrors();
201
// errors.add(PROPERTY_KEY + "1", new ActionError("error1.misc"));
202
// errors.add(PROPERTY_KEY + "2", new ActionError("error2.misc"));
203
// pageContext.setAttribute(elErrorsTag.getName(), errors,
204
// PageContext.REQUEST_SCOPE);
206
// HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
207
// String requiredText = YYY_VALUE;
208
// response.addHeader(REQUIRED_TEXT_VALUE_KEY, requiredText);
210
// int startTagReturn = elErrorsTag.doStartTag();
211
// int afterBodyReturn = elErrorsTag.doAfterBody();
212
// int endTagReturn = elErrorsTag.doEndTag();
215
// public void endOnlyWantOneProperty(com.meterware.httpunit.WebResponse testResponse) {
217
// TestHelper.printResponse(testResponse);
219
// org.w3c.dom.Document document = testResponse.getDOM();
220
// DOMHelper.printNode(document.getDocumentElement());
222
// String nodeText = DOMHelper.getNodeText(document, "/html/body");
223
// log.debug("nodeText[" + nodeText + "]");
225
// String requiredTextValue = (String)testResponse.getHeaderField(
226
// REQUIRED_TEXT_VALUE_KEY);
228
// if (!nodeText.equals(requiredTextValue)) {
229
// fail("The <errors> tag instance should have resulted " +
230
// "in the text " + "\"" + requiredTextValue +
231
// "\", but instead had the value \"" + nodeText + "\".");
233
// } catch (Exception ex) {
234
// ex.printStackTrace();
240
// * Tests one error on a specific property, with "property" attribute set to
243
// public void testProperty()
244
// throws ServletException, JspException {
245
// HashMapMessageResources messageResources = new HashMapMessageResources(
246
// null, null, true);
247
// messageResources.addMessage("error.misc", XXX_VALUE);
248
// String bundle = elErrorsTag.getBundle();
249
// if (bundle == null)
250
// bundle = Action.MESSAGES_KEY;
251
// pageContext.setAttribute(bundle, messageResources,
252
// PageContext.APPLICATION_SCOPE);
254
// ActionErrors errors = new ActionErrors();
255
// ActionError error = new ActionError("error.misc");
256
// errors.add(PROPERTY_KEY, error);
257
// pageContext.setAttribute(elErrorsTag.getName(), errors,
258
// PageContext.REQUEST_SCOPE);
260
// HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
261
// String requiredText = XXX_VALUE;
262
// response.addHeader(REQUIRED_TEXT_VALUE_KEY, requiredText);
264
// int startTagReturn = elErrorsTag.doStartTag();
265
// int afterBodyReturn = elErrorsTag.doAfterBody();
266
// int endTagReturn = elErrorsTag.doEndTag();
269
// public void endProperty(com.meterware.httpunit.WebResponse testResponse) {
271
// TestHelper.printResponse(testResponse);
273
// org.w3c.dom.Document document = testResponse.getDOM();
274
// DOMHelper.printNode(document.getDocumentElement());
276
// String nodeText = DOMHelper.getNodeText(document, "/html/body");
277
// log.debug("nodeText[" + nodeText + "]");
279
// String requiredTextValue = (String)testResponse.getHeaderField(
280
// REQUIRED_TEXT_VALUE_KEY);
282
// if (!nodeText.equals(requiredTextValue)) {
283
// fail("The <errors> tag instance should have resulted " +
284
// "in the text " + "\"" + requiredTextValue +
285
// "\", but instead had the value \"" + nodeText + "\".");
287
// } catch (Exception ex) {
288
// ex.printStackTrace();
294
// * Just like "testPropertySpecified", but the "property" value is an EL
295
// * expression, evaluating to the same value as in the other test.
297
// public void testPropertySpecifiedEL()
298
// throws ServletException, JspException {
299
// String varName = "targetVar";
300
// pageContext.setAttribute(varName, PROPERTY_KEY);
301
// elErrorsTag.setPropertyExpr("${" + varName + "}");
302
// testPropertySpecified();
305
// public void endPropertySpecifiedEL(com.meterware.httpunit.WebResponse testResponse) {
306
// endPropertySpecified(testResponse);
310
// * Tests one error in errors list, set to a property, and the "property"
311
// * attribute set to that property key.
313
// public void testPropertySpecified()
314
// throws ServletException, JspException {
315
// elErrorsTag.setPropertyExpr(PROPERTY_KEY);
317
// HashMapMessageResources messageResources = new HashMapMessageResources(
318
// null, null, true);
319
// messageResources.addMessage("error.misc", XXX_VALUE);
320
// String bundle = elErrorsTag.getBundle();
321
// if (bundle == null)
322
// bundle = Action.MESSAGES_KEY;
323
// pageContext.setAttribute(bundle, messageResources,
324
// PageContext.APPLICATION_SCOPE);
326
// ActionErrors errors = new ActionErrors();
327
// ActionError error = new ActionError("error.misc");
328
// errors.add(PROPERTY_KEY, error);
329
// pageContext.setAttribute(elErrorsTag.getName(), errors,
330
// PageContext.REQUEST_SCOPE);
332
// HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
333
// String requiredText = XXX_VALUE;
334
// response.addHeader(REQUIRED_TEXT_VALUE_KEY, requiredText);
336
// int startTagReturn = elErrorsTag.doStartTag();
337
// int afterBodyReturn = elErrorsTag.doAfterBody();
338
// int endTagReturn = elErrorsTag.doEndTag();
341
// public void endPropertySpecified(com.meterware.httpunit.WebResponse testResponse) {
343
// TestHelper.printResponse(testResponse);
345
// org.w3c.dom.Document document = testResponse.getDOM();
346
// DOMHelper.printNode(document.getDocumentElement());
348
// String nodeText = DOMHelper.getNodeText(document, "/html/body");
349
// log.debug("nodeText[" + nodeText + "]");
351
// String requiredTextValue = (String)testResponse.getHeaderField(
352
// REQUIRED_TEXT_VALUE_KEY);
354
// if (!nodeText.equals(requiredTextValue)) {
355
// fail("The <errors> tag instance should have resulted " +
356
// "in the text " + "\"" + requiredTextValue +
357
// "\", but instead had the value \"" + nodeText + "\".");
359
// } catch (Exception ex) {
360
// ex.printStackTrace();
366
// * Tests one error in the errors list, set to a property, and the
367
// * "property" attribute set to a different property key.
369
// public void testPropertySpecifiedWrong()
370
// throws ServletException, JspException {
371
// elErrorsTag.setPropertyExpr(PROPERTY_KEY + "x");
373
// HashMapMessageResources messageResources = new HashMapMessageResources(
374
// null, null, true);
375
// messageResources.addMessage("error.misc", XXX_VALUE);
376
// String bundle = elErrorsTag.getBundle();
377
// if (bundle == null)
378
// bundle = Action.MESSAGES_KEY;
379
// pageContext.setAttribute(bundle, messageResources,
380
// PageContext.APPLICATION_SCOPE);
382
// ActionErrors errors = new ActionErrors();
383
// ActionError error = new ActionError("error.misc");
384
// errors.add(PROPERTY_KEY, error);
385
// pageContext.setAttribute(elErrorsTag.getName(), errors,
386
// PageContext.REQUEST_SCOPE);
388
// HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
389
// String requiredText = "";
390
// response.addHeader(REQUIRED_TEXT_VALUE_KEY, requiredText);
392
// int startTagReturn = elErrorsTag.doStartTag();
393
// int afterBodyReturn = elErrorsTag.doAfterBody();
394
// int endTagReturn = elErrorsTag.doEndTag();
397
// public void endPropertySpecifiedWrong(com.meterware.httpunit.WebResponse testResponse) {
399
// TestHelper.printResponse(testResponse);
401
// org.w3c.dom.Document document = testResponse.getDOM();
402
// DOMHelper.printNode(document.getDocumentElement());
404
// String nodeText = DOMHelper.getNodeText(document, "/html/body");
405
// log.debug("nodeText[" + nodeText + "]");
407
// String requiredTextValue = (String)testResponse.getHeaderField(
408
// REQUIRED_TEXT_VALUE_KEY);
410
// if (!nodeText.equals(requiredTextValue)) {
411
// fail("The <errors> tag instance should have resulted " +
412
// "in the text " + "\"" + requiredTextValue +
413
// "\", but instead had the value \"" + nodeText + "\".");
415
// } catch (Exception ex) {
416
// ex.printStackTrace();
421
// public void tearDown() {
422
// elErrorsTag = null;
426
// * Tests a single error in errors list, with the "header" and "footer"
427
// * resource set to non-empty strings.
429
// public void testHeaderFooter()
430
// throws ServletException, JspException {
431
// HashMapMessageResources messageResources = new HashMapMessageResources(
432
// null, null, true);
433
// messageResources.addMessage("error.misc", XXX_VALUE);
434
// messageResources.addMessage("errors.header", HEADER_VALUE);
435
// messageResources.addMessage("errors.footer", FOOTER_VALUE);
436
// String bundle = elErrorsTag.getBundle();
437
// if (bundle == null)
438
// bundle = Action.MESSAGES_KEY;
439
// pageContext.setAttribute(bundle, messageResources,
440
// PageContext.APPLICATION_SCOPE);
442
// ActionErrors errors = new ActionErrors();
443
// ActionError error = new ActionError("error.misc");
444
// errors.add(ActionErrors.GLOBAL_ERROR, error);
445
// pageContext.setAttribute(elErrorsTag.getName(), errors,
446
// PageContext.REQUEST_SCOPE);
448
// HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
449
// String requiredText = HEADER_VALUE + " " + XXX_VALUE +
450
// " " + FOOTER_VALUE;
451
// response.addHeader(REQUIRED_TEXT_VALUE_KEY, requiredText);
453
// int startTagReturn = elErrorsTag.doStartTag();
454
// int afterBodyReturn = elErrorsTag.doAfterBody();
455
// int endTagReturn = elErrorsTag.doEndTag();
458
// public void endHeaderFooter(com.meterware.httpunit.WebResponse testResponse) {
460
// TestHelper.printResponse(testResponse);
462
// org.w3c.dom.Document document = testResponse.getDOM();
463
// DOMHelper.printNode(document.getDocumentElement());
465
// String nodeText = DOMHelper.getNodeText(document, "/html/body");
466
// log.debug("nodeText[" + nodeText + "]");
468
// String requiredTextValue = (String)testResponse.getHeaderField(
469
// REQUIRED_TEXT_VALUE_KEY);
471
// if (!nodeText.equals(requiredTextValue)) {
472
// fail("The <errors> tag instance should have resulted " +
473
// "in the text " + "\"" + requiredTextValue +
474
// "\", but instead had the value \"" + nodeText + "\".");
476
// } catch (Exception ex) {
477
// ex.printStackTrace();