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  package org.apache.commons.validator;
18  
19  import java.io.IOException;
20  import java.util.Date;
21  import java.util.Iterator;
22  import java.util.Locale;
23  import java.util.Map;
24  
25  import org.xml.sax.SAXException;
26                                                            
27  /**                                                       
28   * Performs Validation Test for type validations.
29   *
30   * @version $Revision$
31   */
32  public class GenericTypeValidatorTest extends AbstractCommonTest {
33     
34     /**
35      * The key used to retrieve the set of validation 
36      * rules from the xml file.
37      */
38     protected static String FORM_KEY = "typeForm";   
39  
40     /**
41      * The key used to retrieve the validator action.
42      */
43     protected static String ACTION = "byte";
44  
45     public GenericTypeValidatorTest(String name) {                  
46         super(name);                                      
47     }                                                     
48  
49     /**
50      * Load <code>ValidatorResources</code> from 
51      * validator-type.xml.
52      */
53     @Override
54  protected void setUp() throws IOException, SAXException {
55        // Load resources
56        loadResources("GenericTypeValidatorTest-config.xml");
57     }
58  
59     @Override
60  protected void tearDown() {
61     }
62  
63     /**
64      * Tests the byte validation.
65      */
66     public void testType() throws ValidatorException {
67        // Create bean to run test on.
68        TypeBean/TypeBean.html#TypeBean">TypeBean info = new TypeBean();
69        info.setByte("12");
70        info.setShort("129");
71        info.setInteger("-144");
72        info.setLong("88000");
73        info.setFloat("12.1555f");
74        info.setDouble("129.1551511111d");
75        
76        // Construct validator based on the loaded resources 
77        // and the form key
78        Validator validator = new Validator(resources, FORM_KEY);
79        // add the name bean to the validator as a resource 
80        // for the validations to be performed on.
81        validator.setParameter(Validator.BEAN_PARAM, info);
82  
83        // Get results of the validation.
84        ValidatorResults results = null;
85        
86        // throws ValidatorException, 
87        // but we aren't catching for testing 
88        // since no validation methods we use 
89        // throw this
90        results = validator.validate();
91        
92        assertNotNull("Results are null.", results);
93        
94        Map<String, ?> hResultValues = results.getResultValueMap();
95  
96        assertTrue("Expecting byte result to be an instance of Byte.", (hResultValues.get("byte") instanceof Byte));
97        assertTrue("Expecting short result to be an instance of Short.", (hResultValues.get("short") instanceof Short));
98        assertTrue("Expecting integer result to be an instance of Integer.", (hResultValues.get("integer") instanceof Integer));
99        assertTrue("Expecting long result to be an instance of Long.", (hResultValues.get("long") instanceof Long));
100       assertTrue("Expecting float result to be an instance of Float.", (hResultValues.get("float") instanceof Float));
101       assertTrue("Expecting double result to be an instance of Double.", (hResultValues.get("double") instanceof Double));
102       
103       for (Iterator<String> i = hResultValues.keySet().iterator(); i.hasNext(); ) {
104          String key = i.next();
105          Object value = hResultValues.get(key);
106          
107          assertNotNull("value ValidatorResults.getResultValueMap() should not be null.", value);
108       }
109       
110       //ValidatorResult result = results.getValidatorResult("value");
111       
112       //assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
113       //assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION));
114       //assertTrue(ACTION + " value ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
115 
116    }
117    
118    /**
119     * Tests the us locale
120     */
121    public void testUSLocale() throws ValidatorException {
122       // Create bean to run test on.
123       TypeBean/TypeBean.html#TypeBean">TypeBean info = new TypeBean();
124       info.setByte("12");
125       info.setShort("129");
126       info.setInteger("-144");
127       info.setLong("88000");
128       info.setFloat("12.1555");
129       info.setDouble("129.1551511111");
130       info.setDate("12/21/2010");
131       localeTest(info, Locale.US);
132    }
133 
134    /**
135     * Tests the fr locale.
136     */
137    public void testFRLocale() throws ValidatorException {
138       // Create bean to run test on.
139       TypeBean/TypeBean.html#TypeBean">TypeBean info = new TypeBean();
140       info.setByte("12");
141       info.setShort("-129");
142       info.setInteger("1443");
143       info.setLong("88000");
144       info.setFloat("12,1555");
145       info.setDouble("129,1551511111");
146       info.setDate("21/12/2010");
147       Map<String, ?> map = localeTest(info, Locale.FRENCH);
148       assertTrue("float value not correct", ((Float)map.get("float")).intValue() == 12);
149       assertTrue("double value not correct", ((Double)map.get("double")).intValue() == 129);
150   }
151 
152   /**
153     * Tests the locale.
154     */
155    private Map<String, ?> localeTest(TypeBean info, Locale locale) throws ValidatorException {
156      
157       // Construct validator based on the loaded resources 
158       // and the form key
159       Validator validator = new Validator(resources, "typeLocaleForm");
160       // add the name bean to the validator as a resource 
161       // for the validations to be performed on.
162       validator.setParameter(Validator.BEAN_PARAM, info);
163       validator.setParameter("java.util.Locale", locale);
164 
165       // Get results of the validation.
166       ValidatorResults results = null;
167       
168       // throws ValidatorException, 
169       // but we aren't catching for testing 
170       // since no validation methods we use 
171       // throw this
172       results = validator.validate();
173       
174       assertNotNull("Results are null.", results);
175       
176       Map<String, ?> hResultValues = results.getResultValueMap();
177 
178       assertTrue("Expecting byte result to be an instance of Byte for locale: "+locale, (hResultValues.get("byte") instanceof Byte));
179       assertTrue("Expecting short result to be an instance of Short for locale: "+locale, (hResultValues.get("short") instanceof Short));
180       assertTrue("Expecting integer result to be an instance of Integer for locale: "+locale, (hResultValues.get("integer") instanceof Integer));
181       assertTrue("Expecting long result to be an instance of Long for locale: "+locale, (hResultValues.get("long") instanceof Long));
182       assertTrue("Expecting float result to be an instance of Float for locale: "+locale, (hResultValues.get("float") instanceof Float));
183       assertTrue("Expecting double result to be an instance of Double for locale: "+locale, (hResultValues.get("double") instanceof Double));
184       assertTrue("Expecting date result to be an instance of Date for locale: "+locale, (hResultValues.get("date") instanceof Date));
185       
186       for (Iterator<String> i = hResultValues.keySet().iterator(); i.hasNext(); ) {
187          String key = i.next();
188          Object value = hResultValues.get(key);
189          
190          assertNotNull("value ValidatorResults.getResultValueMap() should not be null for locale: "+locale, value);
191       }
192       return hResultValues;
193    }
194 
195 }