1   /*
2    * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TypeTest.java,v 1.16 2004/02/21 17:10:30 rleland Exp $
3    * $Revision: 1.16 $
4    * $Date: 2004/02/21 17:10:30 $
5    *
6    * ====================================================================
7    * Copyright 2001-2004 The Apache Software Foundation
8    *
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *     http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  
22  package org.apache.commons.validator;
23  
24  import java.io.IOException;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.xml.sax.SAXException;
32                                                            
33  /***                                                       
34   * Performs Validation Test for type validations.
35   */                                                       
36  public class TypeTest extends TestCommon {
37     
38     /***
39      * The key used to retrieve the set of validation 
40      * rules from the xml file.
41      */
42     protected static String FORM_KEY = "typeForm";   
43  
44     /***
45      * The key used to retrieve the validator action.
46      */
47     protected static String ACTION = "byte";
48  
49     public TypeTest(String name) {                  
50         super(name);                                      
51     }                                                     
52  
53     /***
54      * Start the tests.
55      *
56      * @param theArgs the arguments. Not used
57      */
58     public static void main(String[] theArgs) {
59         junit.awtui.TestRunner.main(new String[] {TypeTest.class.getName()});
60     }
61  
62     /***
63      * @return a test suite (<code>TestSuite</code>) that includes all methods
64      *         starting with "test"
65      */
66     public static Test suite() {
67         // All methods starting with "test" will be executed in the test suite.
68         return new TestSuite(TypeTest.class);
69     }
70  
71     /***
72      * Load <code>ValidatorResources</code> from 
73      * validator-type.xml.
74      */
75     protected void setUp() throws IOException, SAXException {
76        // Load resources
77        loadResources("validator-type.xml");
78     }
79  
80     protected void tearDown() {
81     }
82  
83     /***
84      * Tests the byte validation.
85      */
86     public void testType() throws ValidatorException {
87        // Create bean to run test on.
88        TypeBean info = new TypeBean();
89        info.setByte("12");
90        info.setShort("129");
91        info.setInteger("-144");
92        info.setLong("88000");
93        info.setFloat("12.1555f");
94        info.setDouble("129.1551511111d");
95        
96        // Construct validator based on the loaded resources 
97        // and the form key
98        Validator validator = new Validator(resources, FORM_KEY);
99        // add the name bean to the validator as a resource 
100       // for the validations to be performed on.
101       validator.setParameter(Validator.BEAN_PARAM, info);
102 
103       // Get results of the validation.
104       ValidatorResults results = null;
105       
106       // throws ValidatorException, 
107       // but we aren't catching for testing 
108       // since no validation methods we use 
109       // throw this
110       results = validator.validate();
111       
112       assertNotNull("Results are null.", results);
113       
114       Map hResultValues = results.getResultValueMap();
115 
116       assertTrue("Expecting byte result to be an instance of Byte.", (hResultValues.get("byte") instanceof Byte));
117       assertTrue("Expecting short result to be an instance of Short.", (hResultValues.get("short") instanceof Short));
118       assertTrue("Expecting integer result to be an instance of Integer.", (hResultValues.get("integer") instanceof Integer));
119       assertTrue("Expecting long result to be an instance of Long.", (hResultValues.get("long") instanceof Long));
120       assertTrue("Expecting float result to be an instance of Float.", (hResultValues.get("float") instanceof Float));
121       assertTrue("Expecting double result to be an instance of Double.", (hResultValues.get("double") instanceof Double));
122       
123       for (Iterator i = hResultValues.keySet().iterator(); i.hasNext(); ) {
124          String key = (String)i.next();
125          Object value = hResultValues.get(key);
126          
127          assertNotNull("value ValidatorResults.getResultValueMap() should not be null.", value);
128       }
129       
130       //ValidatorResult result = results.getValidatorResult("value");
131       
132       //assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
133       //assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION));
134       //assertTrue(ACTION + " value ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
135 
136    }
137 
138 }