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