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.assertNotNull;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  import static org.junit.jupiter.api.Assertions.fail;
22  
23  import java.io.IOException;
24  import java.util.Locale;
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   * This TestCase is a confirmation of the parameter of the validator's method.
33   */
34  public class ParameterTest extends AbstractCommonTest {
35  
36      private static final String FORM_KEY = "nameForm";
37  
38      private String firstName;
39  
40      private String middleName;
41  
42      private String lastName;
43  
44      private void assertParameterValue(final Validator validator, final String name, final Class<?> type) {
45          final Object value = validator.getParameterValue(name);
46          assertNotNull(value, () -> "Expected '" + type.getName() + "' but was null");
47          assertTrue(type.isInstance(value), () -> "Expected '" + type.getName() + "' but was '" + value.getClass().getName() + "'");
48      }
49  
50      /**
51       * Create a NameBean.
52       */
53      private NameBean createNameBean() {
54          final NameBean name = new NameBean();
55          name.setFirstName(firstName);
56          name.setMiddleName(middleName);
57          name.setLastName(lastName);
58          return name;
59      }
60  
61      /**
62       * Load <code>ValidatorResources</code> from ValidatorResultsTest-config.xml.
63       */
64      @BeforeEach
65      protected void setUp() throws IOException, SAXException {
66          // Load resources
67          loadResources("ParameterTest-config.xml");
68  
69          // initialize values
70          firstName = "foo";
71          middleName = "123";
72          lastName = "456";
73  
74      }
75  
76      @AfterEach
77      protected void tearDown() {
78      }
79  
80      /**
81       * Test all validations ran and passed.
82       */
83      @Test
84      public void testAllValid() {
85  
86          // Create bean to run test on.
87          final NameBean bean = createNameBean();
88  
89          final Validator validator = new Validator(resources, FORM_KEY);
90  
91          // add the name bean to the validator as a resource
92          // for the validations to be performed on.
93          validator.setParameter(Validator.BEAN_PARAM, bean);
94          validator.setParameter(Validator.LOCALE_PARAM, Locale.getDefault());
95  
96          // Get results of the validation.
97          try {
98              validator.validate();
99          } catch (final Exception e) {
100             fail("Validator.validate() threw " + e);
101         }
102         assertParameterValue(validator, Validator.BEAN_PARAM, Object.class);
103         assertParameterValue(validator, Validator.FIELD_PARAM, Field.class);
104         assertParameterValue(validator, Validator.FORM_PARAM, Form.class);
105         assertParameterValue(validator, Validator.LOCALE_PARAM, Locale.class);
106         assertParameterValue(validator, Validator.VALIDATOR_ACTION_PARAM, ValidatorAction.class);
107         assertParameterValue(validator, Validator.VALIDATOR_PARAM, Validator.class);
108         assertParameterValue(validator, Validator.VALIDATOR_RESULTS_PARAM, ValidatorResults.class);
109     }
110 }