2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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      // ------------------------------------------------------------ Constructors
40  
41  
42      // Construct a new instance of this test case.
43      public ValidatorLifecycleListenerTestCase(String name) {
44          super(name);
45      }
46  
47  
48      // ---------------------------------------------------- Overall Test Methods
49  
50  
51      // Set up instance variables required by this test case.
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      // Return the tests included in this test case.
63      public static Test suite() {
64  
65          return (new TestSuite(ValidatorLifecycleListenerTestCase.class));
66  
67      }
68  
69  
70      // Tear down instance variables required by this test case.
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      // -------------------------------------------------------- Static Variables
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     // ------------------------------------------------------ Instance Variables
115 
116 
117     /***
118      * <p>ValidatorLifecycleListener used to load configuration resources</p>
119      */
120     ValidatorLifecycleListener listener = null;
121 
122 
123     // ------------------------------------------------- Individual Test Methods
124 
125 
126     // Test whether the precalculated validator actions includes the data
127     // that it should
128     public void testActions() {
129 
130         // Acquire the configuration data we will need
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         // The precalculated map will not include an entry for
138         // "includeJavaScriptUtilities" because it is not a real validation type
139         assertEquals(vamap.size() - 1, actions.size());
140 
141         // Verify the number of actions for each precalculated validator
142         // This also indirectly checks that all entries that should be
143         // precalculated are actually present
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     // Test a pristine instance of the listener
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     // --------------------------------------------------------- Private Methods
183 
184 
185 }