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