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