1 21 22 package org.apache.commons.validator; 23 24 import junit.framework.TestCase; 25 26 29 public class FieldTest extends TestCase { 30 31 32 protected Field field; 33 34 37 public FieldTest() { 38 super(); 39 } 40 41 45 public FieldTest(String name) { 46 super(name); 47 } 48 49 52 public void setUp() { 53 field = new Field(); 54 } 55 56 59 public void tearDown() { 60 field = null; 61 } 62 63 66 public void testEmptyArgs() { 67 68 assertEquals("Empty Args(1) ", 0, field.getArgs("required").length); 69 70 } 71 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 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 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 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 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 assertEquals("testOverrideUsingPositionA(1) ", 3, field.getArgs("required").length); 150 assertEquals("testOverrideUsingPositionA(2) ", "required-position-1", field.getArg("required", 1).getKey()); 151 152 assertEquals("testOverrideUsingPositionA(3) ", 3, field.getArgs("mask").length); 154 assertEquals("testOverrideUsingPositionA(4) ", "default-position-1", field.getArg("mask", 1).getKey()); 155 156 assertEquals("testOverrideUsingPositionA(5) ", "default-position-1", field.getArg(1).getKey()); 158 159 } 160 161 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 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 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 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 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 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 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 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 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 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 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 254 private Arg createArg(String key) { 255 Arg arg = new Arg(); 256 arg.setKey(key); 257 return arg; 258 } 259 260 263 private Arg createArg(String key, int position) { 264 Arg arg = createArg(key); 265 arg.setPosition(position); 266 return arg; 267 } 268 269 272 private Arg createArg(String key, String name) { 273 Arg arg = createArg(key); 274 arg.setName(name); 275 return arg; 276 } 277 278 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 } 288 | Popular Tags |