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