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