    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 junit.framework.Test;
    20  import junit.framework.TestSuite;
    21  import java.util.Locale;
    22  import java.io.IOException;
    23  import org.xml.sax.SAXException;
    24
    25  /***
    26   * Test that the new Var attributes and the
    27   * digester rule changes work.
    28   *
    29   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
    30   */
    31  public class VarTest extends TestCommon {
    32
    33     /***
    34      * The key used to retrieve the set of validation
    35      * rules from the xml file.
    36      */
    37     protected static String FORM_KEY = "testForm";
    38
    39     /***
    40      * The key used to retrieve the validator action.
    41      */
    42     protected static String ACTION = "byte";
    43
    44
    45
    46     public VarTest(String name) {
    47         super(name);
    48     }
    49
    50     /***
    51      * Start the tests.
    52      *
    53      * @param theArgs the arguments. Not used
    54      */
    55     public static void main(String[] theArgs) {
    56         junit.awtui.TestRunner.main(new String[] {VarTest.class.getName()});
    57     }
    58
    59     /***
    60      * @return a test suite (<code>TestSuite</code>) that includes all methods
    61      *         starting with "test"
    62      */
    63     public static Test suite() {
    64         // All methods starting with "test" will be executed in the test suite.
    65         return new TestSuite(VarTest.class);
    66     }
    67
    68     /***
    69      * Load <code>ValidatorResources</code> from
    70      * validator-multipletest.xml.
    71      */
    72     protected void setUp() throws IOException, SAXException {
    73        // Load resources
    74        loadResources("VarTest-config.xml");
    75     }
    76
    77     protected void tearDown() {
    78     }
    79
    80     /***
    81      * With nothing provided, we should fail both because both are required.
    82      */
    83     public void testVars() throws ValidatorException {
    84
    85         Form form = resources.getForm(Locale.getDefault(), FORM_KEY);
    86
    87         // Get field 1
    88         Field field1 = form.getField("field-1");
    89         assertNotNull("field-1 is null.", field1);
    90         assertEquals("field-1 property is wrong", "field-1", field1.getProperty());
    91
    92         // Get var-1-1
    93         Var var11 = field1.getVar("var-1-1");
    94         assertNotNull("var-1-1 is null.", var11);
    95         assertEquals("var-1-1 name is wrong", "var-1-1", var11.getName());
    96         assertEquals("var-1-1 value is wrong", "value-1-1", var11.getValue());
    97         assertEquals("var-1-1 jstype is wrong", "jstype-1-1", var11.getJsType());
    98         assertFalse("var-1-1 resource is true", var11.isResource());
    99         assertNull("var-1-1 bundle is not null.", var11.getBundle());
    100
    101        // Get field 2
    102        Field field2 = form.getField("field-2");
    103        assertNotNull("field-2 is null.", field2);
    104        assertEquals("field-2 property is wrong", "field-2", field2.getProperty());
    105
    106        // Get var-2-1
    107        Var var21 = field2.getVar("var-2-1");
    108        assertNotNull("var-2-1 is null.", var21);
    109        assertEquals("var-2-1 name is wrong", "var-2-1", var21.getName());
    110        assertEquals("var-2-1 value is wrong", "value-2-1", var21.getValue());
    111        assertEquals("var-2-1 jstype is wrong", "jstype-2-1", var21.getJsType());
    112        assertTrue("var-2-1 resource is false", var21.isResource());
    113        assertEquals("var-2-1 bundle is wrong", "bundle-2-1", var21.getBundle());
    114
    115        // Get var-2-2
    116        Var var22 = field2.getVar("var-2-2");
    117        assertNotNull("var-2-2 is null.", var22);
    118        assertEquals("var-2-2 name is wrong", "var-2-2", var22.getName());
    119        assertEquals("var-2-2 value is wrong", "value-2-2", var22.getValue());
    120        assertNull("var-2-2 jstype is not null", var22.getJsType());
    121        assertFalse("var-2-2 resource is true", var22.isResource());
    122        assertEquals("var-2-2 bundle is wrong", "bundle-2-2", var22.getBundle());
    123
    124    }
    125
    126 }