KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > ValidatorTestCase


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: ValidatorTestCase.java,v 1.12 2004/02/01 05:16:33 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.w3c.dom.*;
26 import junit.framework.*;
27
28 import org.enhydra.barracuda.core.forms.*;
29 import org.apache.log4j.*;
30 import org.enhydra.barracuda.testbed.*;
31 import org.enhydra.barracuda.plankton.data.StateMap;
32
33
34 /**
35  * This test verifies that BLink works correctly.
36  */

37 public abstract class ValidatorTestCase extends DefaultTestCase {
38
39     /**
40      * Public Constructor
41      */

42     public ValidatorTestCase(String JavaDoc name) {
43         super(name);
44     }
45     
46     //to fully test a Validator, we need to verify its behavior against
47
//all supported types. Consequently, validator tests should implement
48
//these methods
49
public abstract void testString();
50     public abstract void testBoolean();
51     public abstract void testInteger();
52     public abstract void testDate();
53     public abstract void testLong();
54     public abstract void testShort();
55     public abstract void testDouble();
56     public abstract void testFloat();
57
58     /**
59      * Validate a validator against valid cases (shouldn't generate any errors)
60      */

61     public void assertValid(String JavaDoc msg, DefaultFormValidator v, DefaultFormElement el, Object JavaDoc o) {
62         try {
63             v.validateFormElement(o, el, false);
64             //good, there was no error
65
} catch (ValidationException ve) {
66             fail(msg + " - " + ve);
67         }
68     }
69
70     /**
71      * Validate a validator against invalid cases (we expect errors)
72      */

73     public void assertInvalid(String JavaDoc msg, DefaultFormValidator v, DefaultFormElement el, Object JavaDoc o) {
74         try {
75             v.validateFormElement(o, el, false);
76             fail(msg);
77         } catch (ValidationException ve) {
78             //error is expected
79
}
80     }
81
82     public void assertAllValid( DefaultFormValidator v, DefaultFormElement el, Object JavaDoc[] objs ) {
83         for ( int i = 0; i < objs.length; i++ ) {
84             assertValid( objs[i] + " should be valid", v, el, objs[i] );
85         }
86
87     }
88
89     public void assertAllInvalid( DefaultFormValidator v, DefaultFormElement el, Object JavaDoc[] objs ) {
90         for ( int i = 0; i < objs.length; i++ ) {
91             assertInvalid( objs[i] + " should be invalid", v, el, objs[i] );
92         }
93
94     }
95
96
97     // ilc_02202.1_start
98
// send all the validation through a formmap.
99

100     public void assertAllInvalid(FormValidator fv, StateMap sm, FormType ft){
101       this.assertAll(fv, sm, ft, false);
102     }
103
104     public void assertAllValid(FormValidator fv, StateMap sm, FormType ft){
105       this.assertAll(fv, sm, ft, true);
106     }
107
108     public void assertAll(FormValidator fv, StateMap sm, FormType ft, boolean assertValid) {
109         DefaultFormMap fm = new DefaultFormMap();
110         Iterator it = sm.getStateKeys().iterator();
111         while (it.hasNext()) {
112             String JavaDoc key = (String JavaDoc) it.next();
113             fm.defineElement(new DefaultFormElement(key, ft, null, fv));
114         }
115
116         //mow map the StateMap into the FormMap
117
fm.map(sm);
118
119         it = sm.getStateKeys().iterator();
120         while (it.hasNext()) {
121             String JavaDoc key = (String JavaDoc) it.next();
122             DefaultFormElement el = (DefaultFormElement) fm.getElement(key);
123             DefaultFormValidator v = (DefaultFormValidator) el.getValidator();
124             if (assertValid)
125                 assertValid("Error validating element for locale:"+Locale.getDefault()+" key:"+key+" value: "+el.getOrigVal(), v, el, el.getOrigVal());
126             else
127                 assertInvalid("Error invalidating element for locale:"+Locale.getDefault()+" key:"+key+" value: "+el.getOrigVal(), v, el, el.getOrigVal());
128         }
129
130     }
131
132     // ilc_02202.1_end
133
}
134
Popular Tags