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.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.IOException;
26  import java.util.Locale;
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   * Test that the new Var attributes and the digester rule changes work.
35   */
36  public class VarTest 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 = "testForm";
42  
43      /**
44       * The key used to retrieve the validator action.
45       */
46      protected static String ACTION = "byte";
47  
48      /**
49       * Load <code>ValidatorResources</code> from validator-multipletest.xml.
50       */
51      @BeforeEach
52      protected void setUp() throws IOException, SAXException {
53          // Load resources
54          loadResources("VarTest-config.xml");
55      }
56  
57      @AfterEach
58      protected void tearDown() {
59      }
60  
61      /**
62       * With nothing provided, we should fail both because both are required.
63       */
64      @Test
65      public void testVars() {
66  
67          final Form form = resources.getForm(Locale.getDefault(), FORM_KEY);
68  
69          // Get field 1
70          final Field field1 = form.getField("field-1");
71          assertNotNull(field1, "field-1 is null.");
72          assertEquals(field1.getProperty(), "field-1", "field-1 property is wrong");
73  
74          // Get var-1-1
75          final Var var11 = field1.getVar("var-1-1");
76          assertNotNull(var11, "var-1-1 is null.");
77          assertEquals(var11.getName(), "var-1-1", "var-1-1 name is wrong");
78          assertEquals(var11.getValue(), "value-1-1", "var-1-1 value is wrong");
79          assertEquals(var11.getJsType(), "jstype-1-1", "var-1-1 jstype is wrong");
80          assertFalse(var11.isResource(), "var-1-1 resource is true");
81          assertNull(var11.getBundle(), "var-1-1 bundle is not null.");
82  
83          // Get field 2
84          final Field field2 = form.getField("field-2");
85          assertNotNull(field2, "field-2 is null.");
86          assertEquals(field2.getProperty(), "field-2", "field-2 property is wrong");
87  
88          // Get var-2-1
89          final Var var21 = field2.getVar("var-2-1");
90          assertNotNull(var21, "var-2-1 is null.");
91          assertEquals(var21.getName(), "var-2-1", "var-2-1 name is wrong");
92          assertEquals(var21.getValue(), "value-2-1", "var-2-1 value is wrong");
93          assertEquals("jstype-2-1", var21.getJsType(), "var-2-1 jstype is wrong");
94          assertTrue(var21.isResource(), "var-2-1 resource is false");
95          assertEquals(var21.getBundle(), "bundle-2-1", "var-2-1 bundle is wrong");
96  
97          // Get var-2-2
98          final Var var22 = field2.getVar("var-2-2");
99          assertNotNull(var22, "var-2-2 is null.");
100         assertEquals(var22.getName(), "var-2-2", "var-2-2 name is wrong");
101         assertEquals(var22.getValue(), "value-2-2", "var-2-2 value is wrong");
102         assertNull(var22.getJsType(), "var-2-2 jstype is not null");
103         assertFalse(var22.isResource(), "var-2-2 resource is true");
104         assertEquals(var22.getBundle(), "bundle-2-2", "var-2-2 bundle is wrong");
105 
106     }
107 
108 }