    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.TestCase;
    20
    21  /***
    22   * Test <code>Field</code> objects.
    23   *
    24   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
    25   */
    26  public class FieldTest extends TestCase {
    27
    28
    29      protected Field field;
    30
    31      /***
    32       * FieldTest constructor.
    33       */
    34      public FieldTest() {
    35          super();
    36      }
    37
    38      /***
    39       * FieldTest constructor.
    40       * @param name
    41       */
    42      public FieldTest(String name) {
    43          super(name);
    44      }
    45
    46      /***
    47       * Test setup
    48       */
    49      public void setUp() {
    50          field = new Field();
    51      }
    52
    53      /***
    54       * Test clean up
    55       */
    56      public void tearDown() {
    57          field = null;
    58      }
    59
    60      /***
    61       * test Field with no arguments
    62       */
    63      public void testEmptyArgs() {
    64
    65          assertEquals("Empty Args(1) ", 0, field.getArgs("required").length);
    66
    67      }
    68      /***
    69       * test Field with only 'default' arguments, no positions specified.
    70       */
    71      public void testDefaultPositionImplied() {
    72
    73          field.addArg(createArg("default-position-0"));
    74          field.addArg(createArg("default-position-1"));
    75          field.addArg(createArg("default-position-2"));
    76
    77          assertEquals("testDefaultPositionImplied(1) ", 3, field.getArgs("required").length);
    78          assertEquals("testDefaultPositionImplied(2) ", "default-position-0", field.getArg("required", 0).getKey());
    79          assertEquals("testDefaultPositionImplied(3) ", "default-position-1", field.getArg("required", 1).getKey());
    80          assertEquals("testDefaultPositionImplied(4) ", "default-position-2", field.getArg("required", 2).getKey());
    81
    82      }
    83
    84      /***
    85       * test Field with only 'default' arguments, positions specified.
    86       */
    87      public void testDefaultUsingPositions() {
    88
    89          field.addArg(createArg("default-position-1", 1));
    90          field.addArg(createArg("default-position-0", 0));
    91          field.addArg(createArg("default-position-2", 2));
    92
    93          assertEquals("testDefaultUsingPositions(1) ", 3, field.getArgs("required").length);
    94          assertEquals("testDefaultUsingPositions(2) ", "default-position-0", field.getArg("required", 0).getKey());
    95          assertEquals("testDefaultUsingPositions(3) ", "default-position-1", field.getArg("required", 1).getKey());
    96          assertEquals("testDefaultUsingPositions(4) ", "default-position-2", field.getArg("required", 2).getKey());
    97
    98      }
    99
    100     /***
    101      * test Field with only 'default' arguments, position specified for one argument
    102      */
    103     public void testDefaultOnePosition() {
    104
    105         field.addArg(createArg("default-position-0"));
    106         field.addArg(createArg("default-position-2", 2));
    107         field.addArg(createArg("default-position-3"));
    108
    109         assertEquals("testDefaultOnePosition(1) ", 4, field.getArgs("required").length);
    110         assertEquals("testDefaultOnePosition(2) ", "default-position-0", field.getArg("required", 0).getKey());
    111         assertNull("testDefaultOnePosition(3) ", field.getArg("required", 1));
    112         assertEquals("testDefaultOnePosition(4) ", "default-position-2", field.getArg("required", 2).getKey());
    113         assertEquals("testDefaultOnePosition(5) ", "default-position-3", field.getArg("required", 3).getKey());
    114
    115     }
    116
    117     /***
    118      * test Field with only 'default' arguments, some position specified.
    119      */
    120     public void testDefaultSomePositions() {
    121
    122         field.addArg(createArg("default-position-0"));
    123         field.addArg(createArg("default-position-2", 2));
    124         field.addArg(createArg("default-position-3"));
    125         field.addArg(createArg("default-position-1", 1));
    126
    127         assertEquals("testDefaultSomePositions(1) ", 4, field.getArgs("required").length);
    128         assertEquals("testDefaultSomePositions(2) ", "default-position-0", field.getArg("required", 0).getKey());
    129         assertEquals("testDefaultSomePositions(3) ", "default-position-1", field.getArg("required", 1).getKey());
    130         assertEquals("testDefaultSomePositions(4) ", "default-position-2", field.getArg("required", 2).getKey());
    131         assertEquals("testDefaultSomePositions(5) ", "default-position-3", field.getArg("required", 3).getKey());
    132
    133     }
    134
    135     /***
    136      * test Field with a 'default' argument overriden using 'position' property
    137      */
    138     public void testOverrideUsingPositionA() {
    139
    140         field.addArg(createArg("default-position-0"));
    141         field.addArg(createArg("default-position-1"));
    142         field.addArg(createArg("default-position-2"));
    143         field.addArg(createArg("required-position-1", "required", 1));
    144
    145         // use 'required' as name
    146         assertEquals("testOverrideUsingPositionA(1) ", 3, field.getArgs("required").length);
    147         assertEquals("testOverrideUsingPositionA(2) ", "required-position-1", field.getArg("required", 1).getKey());
    148
    149         // use 'mask' as name
    150         assertEquals("testOverrideUsingPositionA(3) ", 3, field.getArgs("mask").length);
    151         assertEquals("testOverrideUsingPositionA(4) ", "default-position-1", field.getArg("mask", 1).getKey());
    152
    153         // Get Default
    154         assertEquals("testOverrideUsingPositionA(5) ", "default-position-1", field.getArg(1).getKey());
    155
    156     }
    157
    158     /***
    159      * test Field with a 'default' argument overriden using 'position' property
    160      */
    161     public void testOverrideUsingPositionB() {
    162
    163         field.addArg(createArg("required-position-3", "required", 3));
    164         field.addArg(createArg("required-position-1", "required", 1));
    165         field.addArg(createArg("default-position-0"));
    166         field.addArg(createArg("default-position-1"));
    167         field.addArg(createArg("default-position-2"));
    168
    169         // use 'required' as name
    170         assertEquals("testOverrideUsingPositionB(1) ", 4, field.getArgs("required").length);
    171         assertEquals("testOverrideUsingPositionB(2) ", "default-position-0",  field.getArg("required", 0).getKey());
    172         assertEquals("testOverrideUsingPositionB(3) ", "required-position-1", field.getArg("required", 1).getKey());
    173         assertEquals("testOverrideUsingPositionB(4) ", "default-position-2",  field.getArg("required", 2).getKey());
    174         assertEquals("testOverrideUsingPositionB(5) ", "required-position-3", field.getArg("required", 3).getKey());
    175
    176         // use 'mask' as name
    177         assertEquals("testOverrideUsingPositionB(6) ", 4, field.getArgs("mask").length);
    178         assertEquals("testOverrideUsingPositionB(6) ", "default-position-0", field.getArg("mask", 0).getKey());
    179         assertEquals("testOverrideUsingPositionB(7) ", "default-position-1", field.getArg("mask", 1).getKey());
    180         assertEquals("testOverrideUsingPositionB(8) ", "default-position-2", field.getArg("mask", 2).getKey());
    181         assertNull("testOverrideUsingPositionB(9) ", field.getArg("mask", 3));
    182
    183     }
    184
    185     /***
    186      * test Field with a 'default' argument overriden without positions specified.
    187      */
    188     public void testOverridePositionImplied() {
    189
    190         field.addArg(createArg("default-position-0"));
    191         field.addArg(createArg("required-position-1", "required"));
    192         field.addArg(createArg("required-position-2", "required"));
    193         field.addArg(createArg("mask-position-1", "mask"));
    194
    195         // use 'required' as name
    196         assertEquals("testOverridePositionImplied(1) ", 3, field.getArgs("required").length);
    197         assertEquals("testOverridePositionImplied(2) ", "default-position-0", field.getArg("required", 0).getKey());
    198         assertEquals("testOverridePositionImplied(3) ", "required-position-1", field.getArg("required", 1).getKey());
    199         assertEquals("testOverridePositionImplied(4) ", "required-position-2", field.getArg("required", 2).getKey());
    200
    201         // use 'mask' as name
    202         assertEquals("testOverridePositionImplied(5) ", 3, field.getArgs("mask").length);
    203         assertEquals("testOverridePositionImplied(6) ", "default-position-0", field.getArg("mask", 0).getKey());
    204         assertEquals("testOverridePositionImplied(7) ", "mask-position-1", field.getArg("mask", 1).getKey());
    205         assertNull("testOverridePositionImplied(8) ", field.getArg("mask", 2));
    206
    207         // Get Defaults
    208         assertEquals("testOverridePositionImplied(9) ",  "default-position-0", field.getArg(0).getKey());
    209         assertNull("testOverridePositionImplied(10) ", field.getArg(1));
    210         assertNull("testOverridePositionImplied(11) ", field.getArg(2));
    211
    212     }
    213
    214     /***
    215      * test Field with a 'default' argument overriden with some positions specified
    216      */
    217     public void testOverrideSomePosition() {
    218
    219         field.addArg(createArg("default-position-0"));
    220         field.addArg(createArg("default-position-1"));
    221         field.addArg(createArg("default-position-2"));
    222         field.addArg(createArg("required-position-1", "required", 1));
    223         field.addArg(createArg("required-position-2", "required"));
    224         field.addArg(createArg("mask-position-3", "mask"));
    225
    226         // use 'required' as name
    227         assertEquals("testOverrideSomePosition(1) ", 4, field.getArgs("required").length);
    228         assertEquals("testOverrideSomePosition(2) ", "default-position-0", field.getArg("required", 0).getKey());
    229         assertEquals("testOverrideSomePosition(3) ", "required-position-1", field.getArg("required", 1).getKey());
    230         assertEquals("testOverrideSomePosition(4) ", "required-position-2", field.getArg("required", 2).getKey());
    231         assertNull("testOverrideSomePosition(5) ", field.getArg("required", 3));
    232
    233         // use 'mask' as name
    234         assertEquals("testOverrideSomePosition(6) ", 4, field.getArgs("mask").length);
    235         assertEquals("testOverrideSomePosition(7) ", "default-position-0", field.getArg("mask", 0).getKey());
    236         assertEquals("testOverrideSomePosition(8) ", "default-position-1", field.getArg("mask", 1).getKey());
    237         assertEquals("testOverrideSomePosition(9) ", "default-position-2", field.getArg("mask", 2).getKey());
    238         assertEquals("testOverrideSomePosition(10) ", "mask-position-3", field.getArg("mask", 3).getKey());
    239
    240         // Get Defaults
    241         assertEquals("testOverrideSomePosition(11) ",  "default-position-0", field.getArg(0).getKey());
    242         assertEquals("testOverrideSomePosition(12) ", "default-position-1", field.getArg(1).getKey());
    243         assertEquals("testOverrideSomePosition(13) ", "default-position-2", field.getArg(2).getKey());
    244         assertNull("testOverrideSomePosition(14) ", field.getArg(3));
    245
    246     }
    247
    248     /***
    249      * Convenience Method - create argument (no name or position specified)
    250      */
    251     private Arg createArg(String key) {
    252         Arg arg = new Arg();
    253         arg.setKey(key);
    254         return arg;
    255     }
    256
    257     /***
    258      * Convenience Method - create argument (no name, position specified)
    259      */
    260     private Arg createArg(String key, int position) {
    261         Arg arg = createArg(key);
    262         arg.setPosition(position);
    263         return arg;
    264     }
    265
    266     /***
    267      * Convenience Method - create argument (name specified, no position)
    268      */
    269     private Arg createArg(String key, String name) {
    270         Arg arg = createArg(key);
    271         arg.setName(name);
    272         return arg;
    273     }
    274
    275     /***
    276      * Convenience Method - create argument (name & position specified)
    277      */
    278     private Arg createArg(String key, String name, int position) {
    279         Arg arg = createArg(key, name);
    280         arg.setPosition(position);
    281         return arg;
    282     }
    283
    284 }