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.validator;
19  
20  import java.util.Locale;
21  import java.util.ResourceBundle;
22  
23  import javax.servlet.ServletContextEvent;
24  
25  import org.apache.commons.validator.ValidatorAction;
26  import org.apache.commons.validator.ValidatorResources;
27  import org.apache.shale.test.base.AbstractJsfTestCase;
28  import org.apache.shale.validator.faces.ValidatorLifecycleListener;
29  import org.apache.shale.validator.util.AbstractUtilities;
30  import org.apache.shale.validator.util.ShaleValidatorAction;
31  
32  /***
33   * <p>Test case for the utility methods provided by
34   * <code>AbstractValidator</code>.  The protected <code>validate()</code>
35   * method is not tested here, as it is presumed that this will be exercised
36   * repeatedly by the test cases for the various concrete validators.</p>
37   */
38  public class AbstractValidatorTestCase extends AbstractJsfTestCase {
39  
40  
41      // ------------------------------------------------------------ Constructors
42  
43  
44      // Construct a new instance of this test case.
45      public AbstractValidatorTestCase(String name) {
46          super(name);
47      }
48  
49  
50      // ---------------------------------------------------- Overall Test Methods
51  
52  
53      // Set up instance variables required by this test case.
54      protected void setUp() throws Exception {
55  
56          super.setUp();
57          application.setMessageBundle("org.apache.shale.validator.TestBundle");
58          facesContext.getViewRoot().setLocale(Locale.US);
59  
60          listener = new ValidatorLifecycleListener();
61          listener.contextInitialized(new ServletContextEvent(servletContext));
62  
63          validator = new ConcreteValidator();
64  
65      }
66  
67  
68      // Tear down instance variables required by this test case.
69      protected void tearDown() throws Exception {
70  
71          validator = null;
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>Default resource bundle for error message lookup
86       */
87      protected static final ResourceBundle defaultBundle =
88        ResourceBundle.getBundle(AbstractUtilities.DEFAULT_RESOURCE_BUNDLE, Locale.US);
89  
90  
91      // ------------------------------------------------------ Instance Variables
92  
93  
94      /***
95       * <p>ValidatorLifecycleListener used to load configuration resources</p>
96       */
97      protected ValidatorLifecycleListener listener = null;
98  
99  
100     /***
101      * <p>Validator instance under test.</p>
102      */
103     protected ConcreteValidator validator = null;
104 
105 
106     // ------------------------------------------------- Individual Test Methods
107 
108 
109     // Test retrieving actions arrays
110     public void testActions() {
111 
112         ShaleValidatorAction actions[] = null;
113 
114         actions = validator.actions(facesContext, "double");
115         assertNotNull(actions);
116         assertEquals(1, actions.length);
117 
118         actions = validator.actions(facesContext, "doubleRange");
119         assertNotNull(actions);
120         assertEquals(2, actions.length);
121 
122         actions = validator.actions(facesContext, "intRange");
123         assertNotNull(actions);
124         assertEquals(2, actions.length);
125         assertEquals("integer", actions[0].getAction().getName());
126         assertEquals("intRange", actions[1].getAction().getName());
127 
128         actions = validator.actions(facesContext, "???undefined???");
129         assertNull(actions);
130 
131     }
132 
133 
134     // Test type conversions via registered converters
135     public void testConvert() {
136 
137         ; // FIXME - add some tests for conversions
138 
139     }
140 
141 
142     // Test retrieving messages
143     public void testMessage() {
144 
145         String message = null;
146 
147         // Check a message that should be overridden by the application
148         message = validator.message(facesContext, "errors.long");
149         assertNotNull(message);
150         assertEquals("Custom Long Error Message", message);
151 
152         // Check a message that should be grabbed from the default resources
153         message = validator.message(facesContext, "errors.integer");
154         assertNotNull(message);
155         assertEquals(defaultBundle.getString("errors.integer"), message);
156 
157         // Check a message that should be undefined
158         message = validator.message(facesContext, "???undefined???");
159         assertNull(message);
160 
161     }
162 
163 
164     // Test retrieving resources
165     public void testResources() {
166 
167         ValidatorAction action = null;
168 
169         ValidatorResources resources = validator.resources(facesContext);
170         assertNotNull(resources);
171 
172         action = resources.getValidatorAction("integer");
173         assertNotNull(action);
174 
175         action = resources.getValidatorAction("intRange");
176         assertNotNull(action);
177 
178         action = resources.getValidatorAction("???undefined");
179         assertNull(action);
180 
181     }
182 
183 
184 }