2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.validator.faces;
19
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23 import javax.servlet.ServletContextEvent;
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import org.apache.commons.validator.ValidatorAction;
27 import org.apache.commons.validator.ValidatorResources;
28 import org.apache.shale.test.base.AbstractJsfTestCase;
29 import org.apache.shale.validator.Globals;
30 import org.apache.shale.validator.util.ShaleValidatorAction;
31
32 /***
33 * <p>Test case for the initializations performed by
34 * <code>ValidatorLifecycleListener</code> at application startup.</p>
35 */
36 public class ValidatorLifecycleListenerTestCase extends AbstractJsfTestCase {
37
38
39
40
41
42
43 public ValidatorLifecycleListenerTestCase(String name) {
44 super(name);
45 }
46
47
48
49
50
51
52 protected void setUp() throws Exception {
53
54 super.setUp();
55
56 listener = new ValidatorLifecycleListener();
57 listener.contextInitialized(new ServletContextEvent(servletContext));
58
59 }
60
61
62
63 public static Test suite() {
64
65 return (new TestSuite(ValidatorLifecycleListenerTestCase.class));
66
67 }
68
69
70
71 protected void tearDown() throws Exception {
72
73 listener.contextDestroyed(new ServletContextEvent(servletContext));
74 listener = null;
75
76 super.tearDown();
77
78 }
79
80
81
82
83
84 /***
85 * <p>The number of ShaleVariableAction entries that should be present
86 * for each logical type, keyed by the type name.</p>
87 *
88 * <p><strong>IMPLEMENTATION NOTE</strong> - The contents of this map
89 * will need to be adjusted whenever changes are made to the preregistered
90 * validators in <code>validator-rules.xml</code>.</p>
91 */
92 private static final Map ACTION_COUNTS = new HashMap();
93 static {
94 ACTION_COUNTS.put("byte", new Integer(1));
95 ACTION_COUNTS.put("creditCard", new Integer(1));
96 ACTION_COUNTS.put("date", new Integer(1));
97 ACTION_COUNTS.put("double", new Integer(1));
98 ACTION_COUNTS.put("doubleRange", new Integer(2));
99 ACTION_COUNTS.put("email", new Integer(1));
100 ACTION_COUNTS.put("float", new Integer(1));
101 ACTION_COUNTS.put("floatRange", new Integer(2));
102 ACTION_COUNTS.put("integer", new Integer(1));
103 ACTION_COUNTS.put("intRange", new Integer(2));
104 ACTION_COUNTS.put("long", new Integer(1));
105 ACTION_COUNTS.put("mask", new Integer(1));
106 ACTION_COUNTS.put("maxlength", new Integer(1));
107 ACTION_COUNTS.put("minlength", new Integer(1));
108 ACTION_COUNTS.put("required", new Integer(1));
109 ACTION_COUNTS.put("short", new Integer(1));
110 ACTION_COUNTS.put("url", new Integer(1));
111 }
112
113
114
115
116
117 /***
118 * <p>ValidatorLifecycleListener used to load configuration resources</p>
119 */
120 ValidatorLifecycleListener listener = null;
121
122
123
124
125
126
127
128 public void testActions() {
129
130
131 ValidatorResources resources = (ValidatorResources)
132 servletContext.getAttribute(Globals.VALIDATOR_RESOURCES);
133 Map vamap = resources.getValidatorActions();
134 Map actions = (Map)
135 servletContext.getAttribute(Globals.VALIDATOR_ACTIONS);
136
137
138
139 assertEquals(vamap.size() - 1, actions.size());
140
141
142
143
144 Iterator entries = ACTION_COUNTS.entrySet().iterator();
145 while (entries.hasNext()) {
146 Map.Entry entry = (Map.Entry) entries.next();
147 String key = (String) entry.getKey();
148 Integer value = (Integer) entry.getValue();
149 ValidatorAction va = resources.getValidatorAction(key);
150 assertTrue("ValidatorAction " + key + " is present", va != null);
151 ShaleValidatorAction[] action = (ShaleValidatorAction[])
152 actions.get(key);
153 assertTrue("ShaleValidatorAction[] " + key + " is present", action != null);
154 assertEquals("ShaleValidatorAction[] " + key + " has correct size",
155 value.intValue(), action.length);
156 if (action.length > 1) {
157 assertTrue("First two actions for " + key + " are not identical",
158 action[0] != action[1]);
159 }
160 }
161
162 }
163
164
165
166 public void testPristine() {
167
168 assertNotNull(listener);
169
170 ValidatorResources resources = (ValidatorResources)
171 servletContext.getAttribute(Globals.VALIDATOR_RESOURCES);
172 assertNotNull(resources);
173 Map vamap = resources.getValidatorActions();
174
175 Map actions = (Map)
176 servletContext.getAttribute(Globals.VALIDATOR_ACTIONS);
177 assertNotNull(actions);
178
179 }
180
181
182
183
184
185 }