    1   /*
    2    * $Id: FieldTest.java 155434 2005-02-26 13:16:41Z dirkv $
    3    * $Rev: 155434 $
    4    * $Date: 2005-02-26 13:16:41 +0000 (Sat, 26 Feb 2005) $
    5    *
    6    * ====================================================================
    7    * Copyright 2001-2005 The Apache Software Foundation
    8    *
    9    * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *     http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   */
    21
    22  package org.apache.commons.validator;
    23
    24  import junit.framework.TestCase;
    25
    26  /***
    27   * Test <code>Field</code> objects.
    28   */
    29  public class FieldTest extends TestCase {
    30
    31
    32      protected Field field;
    33
    34      /***
    35       * FieldTest constructor.
    36       */
    37      public FieldTest() {
    38          super();
    39      }
    40
    41      /***
    42       * FieldTest constructor.
    43       * @param name
    44       */
    45      public FieldTest(String name) {
    46          super(name);
    47      }
    48
    49      /***
    50       * Test setup
    51       */
    52      public void setUp() {
    53          field = new Field();
    54      }
    55
    56      /***
    57       * Test clean up
    58       */
    59      public void tearDown() {
    60          field = null;
    61      }
    62
    63      /***
    64       * test Field with no arguments
    65       */
    66      public void testEmptyArgs() {
    67
    68          assertEquals("Empty Args(1) ", 0, field.getArgs("required").length);
    69
    70      }
    71      /***
    72       * test Field with only 'default' arguments, no positions specified.
    73       */
    74      public void testDefaultPositionImplied() {
    75
    76          field.addArg(createArg("default-position-0"));
    77          field.addArg(createArg("default-position-1"));
    78          field.addArg(createArg("default-position-2"));
    79
    80          assertEquals("testDefaultPositionImplied(1) ", 3, field.getArgs("required").length);
    81          assertEquals("testDefaultPositionImplied(2) ", "default-position-0", field.getArg("required", 0).getKey());
    82          assertEquals("testDefaultPositionImplied(3) ", "default-position-1", field.getArg("required", 1).getKey());
    83          assertEquals("testDefaultPositionImplied(4) ", "default-position-2", field.getArg("required", 2).getKey());
    84
    85      }
    86
    87      /***
    88       * test Field with only 'default' arguments, positions specified.
    89       */
    90      public void testDefaultUsingPositions() {
    91
    92          field.addArg(createArg("default-position-1", 1));
    93          field.addArg(createArg("default-position-0", 0));
    94          field.addArg(createArg("default-position-2", 2));
    95
    96          assertEquals("testDefaultUsingPositions(1) ", 3, field.getArgs("required").length);
    97          assertEquals("testDefaultUsingPositions(2) ", "default-position-0", field.getArg("required", 0).getKey());
    98          assertEquals("testDefaultUsingPositions(3) ", "default-position-1", field.getArg("required", 1).getKey());
    99          assertEquals("testDefaultUsingPositions(4) ", "default-position-2", field.getArg("required", 2).getKey());
    100
    101     }
    102
    103     /***
    104      * test Field with only 'default' arguments, position specified for one argument
    105      */
    106     public void testDefaultOnePosition() {
    107
    108         field.addArg(createArg("default-position-0"));
    109         field.addArg(createArg("default-position-2", 2));
    110         field.addArg(createArg("default-position-3"));
    111
    112         assertEquals("testDefaultOnePosition(1) ", 4, field.getArgs("required").length);
    113         assertEquals("testDefaultOnePosition(2) ", "default-position-0", field.getArg("required", 0).getKey());
    114         assertNull("testDefaultOnePosition(3) ", field.getArg("required", 1));
    115         assertEquals("testDefaultOnePosition(4) ", "default-position-2", field.getArg("required", 2).getKey());
    116         assertEquals("testDefaultOnePosition(5) ", "default-position-3", field.getArg("required", 3).getKey());
    117
    118     }
    119
    120     /***
    121      * test Field with only 'default' arguments, some position specified.
    122      */
    123     public void testDefaultSomePositions() {
    124
    125         field.addArg(createArg("default-position-0"));
    126         field.addArg(createArg("default-position-2", 2));
    127         field.addArg(createArg("default-position-3"));
    128         field.addArg(createArg("default-position-1", 1));
    129
    130         assertEquals("testDefaultSomePositions(1) ", 4, field.getArgs("required").length);
    131         assertEquals("testDefaultSomePositions(2) ", "default-position-0", field.getArg("required", 0).getKey());
    132         assertEquals("testDefaultSomePositions(3) ", "default-position-1", field.getArg("required", 1).getKey());
    133         assertEquals("testDefaultSomePositions(4) ", "default-position-2", field.getArg("required", 2).getKey());
    134         assertEquals("testDefaultSomePositions(5) ", "default-position-3", field.getArg("required", 3).getKey());
    135
    136     }
    137
    138     /***
    139      * test Field with a 'default' argument overriden using 'position' property
    140      */
    141     public void testOverrideUsingPositionA() {
    142
    143         field.addArg(createArg("default-position-0"));
    144         field.addArg(createArg("default-position-1"));
    145         field.addArg(createArg("default-position-2"));
    146         field.addArg(createArg("required-position-1", "required", 1));
    147
    148         // use 'required' as name
    149         assertEquals("testOverrideUsingPositionA(1) ", 3, field.getArgs("required").length);
    150         assertEquals("testOverrideUsingPositionA(2) ", "required-position-1", field.getArg("required", 1).getKey());
    151
    152         // use 'mask' as name
    153         assertEquals("testOverrideUsingPositionA(3) ", 3, field.getArgs("mask").length);
    154         assertEquals("testOverrideUsingPositionA(4) ", "default-position-1", field.getArg("mask", 1).getKey());
    155
    156         // Get Default
    157         assertEquals("testOverrideUsingPositionA(5) ", "default-position-1", field.getArg(1).getKey());
    158
    159     }
    160
    161     /***
    162      * test Field with a 'default' argument overriden using 'position' property
    163      */
    164     public void testOverrideUsingPositionB() {
    165
    166         field.addArg(createArg("required-position-3", "required", 3));
    167         field.addArg(createArg("required-position-1", "required", 1));
    168         field.addArg(createArg("default-position-0"));
    169         field.addArg(createArg("default-position-1"));
    170         field.addArg(createArg("default-position-2"));
    171
    172         // use 'required' as name
    173         assertEquals("testOverrideUsingPositionB(1) ", 4, field.getArgs("required").length);
    174         assertEquals("testOverrideUsingPositionB(2) ", "default-position-0",  field.getArg("required", 0).getKey());
    175         assertEquals("testOverrideUsingPositionB(3) ", "required-position-1", field.getArg("required", 1).getKey());
    176         assertEquals("testOverrideUsingPositionB(4) ", "default-position-2",  field.getArg("required", 2).getKey());
    177         assertEquals("testOverrideUsingPositionB(5) ", "required-position-3", field.getArg("required", 3).getKey());
    178
    179         // use 'mask' as name
    180         assertEquals("testOverrideUsingPositionB(6) ", 4, field.getArgs("mask").length);
    181         assertEquals("testOverrideUsingPositionB(6) ", "default-position-0", field.getArg("mask", 0).getKey());
    182         assertEquals("testOverrideUsingPositionB(7) ", "default-position-1", field.getArg("mask", 1).getKey());
    183         assertEquals("testOverrideUsingPositionB(8) ", "default-position-2", field.getArg("mask", 2).getKey());
    184         assertNull("testOverrideUsingPositionB(9) ", field.getArg("mask", 3));
    185
    186     }
    187
    188     /***
    189      * test Field with a 'default' argument overriden without positions specified.
    190      */
    191     public void testOverridePositionImplied() {
    192
    193         field.addArg(createArg("default-position-0"));
    194         field.addArg(createArg("required-position-1", "required"));
    195         field.addArg(createArg("required-position-2", "required"));
    196         field.addArg(createArg("mask-position-1", "mask"));
    197
    198         // use 'required' as name
    199         assertEquals("testOverridePositionImplied(1) ", 3, field.getArgs("required").length);
    200         assertEquals("testOverridePositionImplied(2) ", "default-position-0", field.getArg("required", 0).getKey());
    201         assertEquals("testOverridePositionImplied(3) ", "required-position-1", field.getArg("required", 1).getKey());
    202         assertEquals("testOverridePositionImplied(4) ", "required-position-2", field.getArg("required", 2).getKey());
    203
    204         // use 'mask' as name
    205         assertEquals("testOverridePositionImplied(5) ", 3, field.getArgs("mask").length);
    206         assertEquals("testOverridePositionImplied(6) ", "default-position-0", field.getArg("mask", 0).getKey());
    207         assertEquals("testOverridePositionImplied(7) ", "mask-position-1", field.getArg("mask", 1).getKey());
    208         assertNull("testOverridePositionImplied(8) ", field.getArg("mask", 2));
    209
    210         // Get Defaults
    211         assertEquals("testOverridePositionImplied(9) ",  "default-position-0", field.getArg(0).getKey());
    212         assertNull("testOverridePositionImplied(10) ", field.getArg(1));
    213         assertNull("testOverridePositionImplied(11) ", field.getArg(2));
    214
    215     }
    216
    217     /***
    218      * test Field with a 'default' argument overriden with some positions specified
    219      */
    220     public void testOverrideSomePosition() {
    221
    222         field.addArg(createArg("default-position-0"));
    223         field.addArg(createArg("default-position-1"));
    224         field.addArg(createArg("default-position-2"));
    225         field.addArg(createArg("required-position-1", "required", 1));
    226         field.addArg(createArg("required-position-2", "required"));
    227         field.addArg(createArg("mask-position-3", "mask"));
    228
    229         // use 'required' as name
    230         assertEquals("testOverrideSomePosition(1) ", 4, field.getArgs("required").length);
    231         assertEquals("testOverrideSomePosition(2) ", "default-position-0", field.getArg("required", 0).getKey());
    232         assertEquals("testOverrideSomePosition(3) ", "required-position-1", field.getArg("required", 1).getKey());
    233         assertEquals("testOverrideSomePosition(4) ", "required-position-2", field.getArg("required", 2).getKey());
    234         assertNull("testOverrideSomePosition(5) ", field.getArg("required", 3));
    235
    236         // use 'mask' as name
    237         assertEquals("testOverrideSomePosition(6) ", 4, field.getArgs("mask").length);
    238         assertEquals("testOverrideSomePosition(7) ", "default-position-0", field.getArg("mask", 0).getKey());
    239         assertEquals("testOverrideSomePosition(8) ", "default-position-1", field.getArg("mask", 1).getKey());
    240         assertEquals("testOverrideSomePosition(9) ", "default-position-2", field.getArg("mask", 2).getKey());
    241         assertEquals("testOverrideSomePosition(10) ", "mask-position-3", field.getArg("mask", 3).getKey());
    242
    243         // Get Defaults
    244         assertEquals("testOverrideSomePosition(11) ",  "default-position-0", field.getArg(0).getKey());
    245         assertEquals("testOverrideSomePosition(12) ", "default-position-1", field.getArg(1).getKey());
    246         assertEquals("testOverrideSomePosition(13) ", "default-position-2", field.getArg(2).getKey());
    247         assertNull("testOverrideSomePosition(14) ", field.getArg(3));
    248
    249     }
    250
    251     /***
    252      * Convenience Method - create argument (no name or position specified)
    253      */
    254     private Arg createArg(String key) {
    255         Arg arg = new Arg();
    256         arg.setKey(key);
    257         return arg;
    258     }
    259
    260     /***
    261      * Convenience Method - create argument (no name, position specified)
    262      */
    263     private Arg createArg(String key, int position) {
    264         Arg arg = createArg(key);
    265         arg.setPosition(position);
    266         return arg;
    267     }
    268
    269     /***
    270      * Convenience Method - create argument (name specified, no position)
    271      */
    272     private Arg createArg(String key, String name) {
    273         Arg arg = createArg(key);
    274         arg.setName(name);
    275         return arg;
    276     }
    277
    278     /***
    279      * Convenience Method - create argument (name & position specified)
    280      */
    281     private Arg createArg(String key, String name, int position) {
    282         Arg arg = createArg(key, name);
    283         arg.setPosition(position);
    284         return arg;
    285     }
    286
    287 }