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  
21  import org.xml.sax.SAXException;
22  
23  /***
24   * Abstracts number unit tests methods.
25   *
26   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
27   */
28  abstract public class TestNumber extends TestCommon {
29      
30      /***
31       * The key used to retrieve the set of validation
32       * rules from the xml file.
33       */
34      protected String FORM_KEY;
35      
36      /***
37       * The key used to retrieve the validator action.
38       */
39      protected String ACTION;
40  
41  
42      public TestNumber(String name) {
43          super(name);
44      }
45  
46      /***
47       * Load <code>ValidatorResources</code> from 
48       * validator-numeric.xml.
49       */
50      protected void setUp() throws IOException, SAXException {
51          // Load resources
52          loadResources("TestNumber-config.xml");
53      }
54  
55      protected void tearDown() {
56      }
57  
58      /***
59       * Tests the number validation.
60       */
61      public void testNumber() throws ValidatorException {
62          // Create bean to run test on.
63          ValueBean info = new ValueBean();
64          info.setValue("0");
65          valueTest(info, true);
66      }
67  
68      /***
69       * Tests the float validation failure.
70       */
71      public void testNumberFailure() throws ValidatorException {
72          // Create bean to run test on.
73          ValueBean info = new ValueBean();
74          valueTest(info, false);
75      }
76  
77      /***
78       * Utlity class to run a test on a value.
79       *
80       * @param	info	Value to run test on.
81       * @param	passed	Whether or not the test is expected to pass.
82       */
83      protected void valueTest(Object info, boolean passed) throws ValidatorException {
84          // Construct validator based on the loaded resources
85          // and the form key
86          Validator validator = new Validator(resources, FORM_KEY);
87          // add the name bean to the validator as a resource
88          // for the validations to be performed on.
89          validator.setParameter(Validator.BEAN_PARAM, info);
90  
91          // Get results of the validation.
92          ValidatorResults results = null;
93  
94          // throws ValidatorException,
95          // but we aren't catching for testing
96          // since no validation methods we use
97          // throw this
98          results = validator.validate();
99  
100         assertNotNull("Results are null.", results);
101 
102         ValidatorResult result = results.getValidatorResult("value");
103 
104         assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
105         assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.", result.containsAction(ACTION));
106         assertTrue(ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
107     }
108 
109 
110 }