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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.IOException;
25  
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  import org.xml.sax.SAXException;
30  
31  /**
32   * Test ValidatorResults.
33   */
34  public class ValidatorResultsTest extends AbstractCommonTest {
35  
36      private static final String FORM_KEY = "nameForm";
37      private static final String firstNameField = "firstName";
38      private static final String middleNameField = "middleName";
39      private static final String lastNameField = "lastName";
40  
41      private String firstName;
42      private String middleName;
43      private String lastName;
44  
45      /**
46       * Check a validator has not been run for a field and the result.
47       */
48      private void checkNotRun(final ValidatorResults results, final String field, final String action) {
49          final ValidatorResult result = results.getValidatorResult(field);
50          assertNotNull(result, () -> field + " result");
51          assertFalse(result.containsAction(action), () -> field + "[" + action + "] run");
52          // System.out.println(field + "[" + action + "] not run");
53      }
54  
55      /**
56       * Check a validator has run for a field and the result.
57       */
58      private void checkValidatorResult(final ValidatorResults results, final String field, final String action, final boolean expected) {
59          final ValidatorResult result = results.getValidatorResult(field);
60          // System.out.println(field + "[" + action + "]=" + result.isValid(action));
61          assertNotNull(result, () -> field + " result");
62          assertTrue(result.containsAction(action), () -> field + "[" + action + "] not run");
63          assertEquals(expected, result.isValid(action), () -> field + "[" + action + "] result");
64      }
65  
66      /**
67       * Create a NameBean.
68       */
69      private NameBean createNameBean() {
70          final NameBean name = new NameBean();
71          name.setFirstName(firstName);
72          name.setMiddleName(middleName);
73          name.setLastName(lastName);
74          return name;
75      }
76  
77      /**
78       * Load <code>ValidatorResources</code> from ValidatorResultsTest-config.xml.
79       */
80      @BeforeEach
81      protected void setUp() throws IOException, SAXException {
82          // Load resources
83          loadResources("ValidatorResultsTest-config.xml");
84  
85          // initialize values
86          firstName = "foo";
87          middleName = "123";
88          lastName = "456";
89  
90      }
91  
92      @AfterEach
93      protected void tearDown() {
94      }
95  
96      /**
97       * Test all validations ran and passed.
98       */
99      @Test
100     public void testAllValid() throws ValidatorException {
101 
102         // Create bean to run test on.
103         final NameBean bean = createNameBean();
104 
105         // Validate.
106         final ValidatorResults results = validate(bean);
107 
108         // Check results
109         checkValidatorResult(results, firstNameField, "required", true);
110         checkValidatorResult(results, middleNameField, "required", true);
111         checkValidatorResult(results, middleNameField, "int", true);
112         checkValidatorResult(results, middleNameField, "positive", true);
113         checkValidatorResult(results, lastNameField, "required", true);
114         checkValidatorResult(results, lastNameField, "int", true);
115 
116     }
117 
118     /**
119      * Test some validations failed and some didn't run.
120      */
121     @Test
122     public void testErrors() throws ValidatorException {
123 
124         middleName = "XXX";
125         lastName = null;
126 
127         // Create bean to run test on.
128         final NameBean bean = createNameBean();
129 
130         // Validate.
131         final ValidatorResults results = validate(bean);
132 
133         // Check results
134         checkValidatorResult(results, firstNameField, "required", true);
135         checkValidatorResult(results, middleNameField, "required", true);
136         checkValidatorResult(results, middleNameField, "int", false);
137         checkNotRun(results, middleNameField, "positive");
138         checkValidatorResult(results, lastNameField, "required", false);
139         checkNotRun(results, lastNameField, "int");
140 
141     }
142 
143     /**
144      * Validate results.
145      */
146     private ValidatorResults validate(final Object bean) throws ValidatorException {
147 
148         // Construct validator based on the loaded resources
149         // and the form key
150         final Validator validator = new Validator(resources, FORM_KEY);
151 
152         // add the name bean to the validator as a resource
153         // for the validations to be performed on.
154         validator.setParameter(Validator.BEAN_PARAM, bean);
155 
156         // Get results of the validation.
157         final ValidatorResults results = validator.validate();
158 
159         return results;
160 
161     }
162 
163 }