001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import junit.framework.TestCase;
020:
021: /**
022: * Test <code>Field</code> objects.
023: *
024: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
025: */
026: public class FieldTest extends TestCase {
027:
028: protected Field field;
029:
030: /**
031: * FieldTest constructor.
032: */
033: public FieldTest() {
034: super ();
035: }
036:
037: /**
038: * FieldTest constructor.
039: * @param name
040: */
041: public FieldTest(String name) {
042: super (name);
043: }
044:
045: /**
046: * Test setup
047: */
048: public void setUp() {
049: field = new Field();
050: }
051:
052: /**
053: * Test clean up
054: */
055: public void tearDown() {
056: field = null;
057: }
058:
059: /**
060: * test Field with no arguments
061: */
062: public void testEmptyArgs() {
063:
064: assertEquals("Empty Args(1) ", 0,
065: field.getArgs("required").length);
066:
067: }
068:
069: /**
070: * test Field with only 'default' arguments, no positions specified.
071: */
072: public void testDefaultPositionImplied() {
073:
074: field.addArg(createArg("default-position-0"));
075: field.addArg(createArg("default-position-1"));
076: field.addArg(createArg("default-position-2"));
077:
078: assertEquals("testDefaultPositionImplied(1) ", 3, field
079: .getArgs("required").length);
080: assertEquals("testDefaultPositionImplied(2) ",
081: "default-position-0", field.getArg("required", 0)
082: .getKey());
083: assertEquals("testDefaultPositionImplied(3) ",
084: "default-position-1", field.getArg("required", 1)
085: .getKey());
086: assertEquals("testDefaultPositionImplied(4) ",
087: "default-position-2", field.getArg("required", 2)
088: .getKey());
089:
090: }
091:
092: /**
093: * test Field with only 'default' arguments, positions specified.
094: */
095: public void testDefaultUsingPositions() {
096:
097: field.addArg(createArg("default-position-1", 1));
098: field.addArg(createArg("default-position-0", 0));
099: field.addArg(createArg("default-position-2", 2));
100:
101: assertEquals("testDefaultUsingPositions(1) ", 3, field
102: .getArgs("required").length);
103: assertEquals("testDefaultUsingPositions(2) ",
104: "default-position-0", field.getArg("required", 0)
105: .getKey());
106: assertEquals("testDefaultUsingPositions(3) ",
107: "default-position-1", field.getArg("required", 1)
108: .getKey());
109: assertEquals("testDefaultUsingPositions(4) ",
110: "default-position-2", field.getArg("required", 2)
111: .getKey());
112:
113: }
114:
115: /**
116: * test Field with only 'default' arguments, position specified for one argument
117: */
118: public void testDefaultOnePosition() {
119:
120: field.addArg(createArg("default-position-0"));
121: field.addArg(createArg("default-position-2", 2));
122: field.addArg(createArg("default-position-3"));
123:
124: assertEquals("testDefaultOnePosition(1) ", 4, field
125: .getArgs("required").length);
126: assertEquals("testDefaultOnePosition(2) ",
127: "default-position-0", field.getArg("required", 0)
128: .getKey());
129: assertNull("testDefaultOnePosition(3) ", field.getArg(
130: "required", 1));
131: assertEquals("testDefaultOnePosition(4) ",
132: "default-position-2", field.getArg("required", 2)
133: .getKey());
134: assertEquals("testDefaultOnePosition(5) ",
135: "default-position-3", field.getArg("required", 3)
136: .getKey());
137:
138: }
139:
140: /**
141: * test Field with only 'default' arguments, some position specified.
142: */
143: public void testDefaultSomePositions() {
144:
145: field.addArg(createArg("default-position-0"));
146: field.addArg(createArg("default-position-2", 2));
147: field.addArg(createArg("default-position-3"));
148: field.addArg(createArg("default-position-1", 1));
149:
150: assertEquals("testDefaultSomePositions(1) ", 4, field
151: .getArgs("required").length);
152: assertEquals("testDefaultSomePositions(2) ",
153: "default-position-0", field.getArg("required", 0)
154: .getKey());
155: assertEquals("testDefaultSomePositions(3) ",
156: "default-position-1", field.getArg("required", 1)
157: .getKey());
158: assertEquals("testDefaultSomePositions(4) ",
159: "default-position-2", field.getArg("required", 2)
160: .getKey());
161: assertEquals("testDefaultSomePositions(5) ",
162: "default-position-3", field.getArg("required", 3)
163: .getKey());
164:
165: }
166:
167: /**
168: * test Field with a 'default' argument overriden using 'position' property
169: */
170: public void testOverrideUsingPositionA() {
171:
172: field.addArg(createArg("default-position-0"));
173: field.addArg(createArg("default-position-1"));
174: field.addArg(createArg("default-position-2"));
175: field.addArg(createArg("required-position-1", "required", 1));
176:
177: // use 'required' as name
178: assertEquals("testOverrideUsingPositionA(1) ", 3, field
179: .getArgs("required").length);
180: assertEquals("testOverrideUsingPositionA(2) ",
181: "required-position-1", field.getArg("required", 1)
182: .getKey());
183:
184: // use 'mask' as name
185: assertEquals("testOverrideUsingPositionA(3) ", 3, field
186: .getArgs("mask").length);
187: assertEquals("testOverrideUsingPositionA(4) ",
188: "default-position-1", field.getArg("mask", 1).getKey());
189:
190: // Get Default
191: assertEquals("testOverrideUsingPositionA(5) ",
192: "default-position-1", field.getArg(1).getKey());
193:
194: }
195:
196: /**
197: * test Field with a 'default' argument overriden using 'position' property
198: */
199: public void testOverrideUsingPositionB() {
200:
201: field.addArg(createArg("required-position-3", "required", 3));
202: field.addArg(createArg("required-position-1", "required", 1));
203: field.addArg(createArg("default-position-0"));
204: field.addArg(createArg("default-position-1"));
205: field.addArg(createArg("default-position-2"));
206:
207: // use 'required' as name
208: assertEquals("testOverrideUsingPositionB(1) ", 4, field
209: .getArgs("required").length);
210: assertEquals("testOverrideUsingPositionB(2) ",
211: "default-position-0", field.getArg("required", 0)
212: .getKey());
213: assertEquals("testOverrideUsingPositionB(3) ",
214: "required-position-1", field.getArg("required", 1)
215: .getKey());
216: assertEquals("testOverrideUsingPositionB(4) ",
217: "default-position-2", field.getArg("required", 2)
218: .getKey());
219: assertEquals("testOverrideUsingPositionB(5) ",
220: "required-position-3", field.getArg("required", 3)
221: .getKey());
222:
223: // use 'mask' as name
224: assertEquals("testOverrideUsingPositionB(6) ", 4, field
225: .getArgs("mask").length);
226: assertEquals("testOverrideUsingPositionB(6) ",
227: "default-position-0", field.getArg("mask", 0).getKey());
228: assertEquals("testOverrideUsingPositionB(7) ",
229: "default-position-1", field.getArg("mask", 1).getKey());
230: assertEquals("testOverrideUsingPositionB(8) ",
231: "default-position-2", field.getArg("mask", 2).getKey());
232: assertNull("testOverrideUsingPositionB(9) ", field.getArg(
233: "mask", 3));
234:
235: }
236:
237: /**
238: * test Field with a 'default' argument overriden without positions specified.
239: */
240: public void testOverridePositionImplied() {
241:
242: field.addArg(createArg("default-position-0"));
243: field.addArg(createArg("required-position-1", "required"));
244: field.addArg(createArg("required-position-2", "required"));
245: field.addArg(createArg("mask-position-1", "mask"));
246:
247: // use 'required' as name
248: assertEquals("testOverridePositionImplied(1) ", 3, field
249: .getArgs("required").length);
250: assertEquals("testOverridePositionImplied(2) ",
251: "default-position-0", field.getArg("required", 0)
252: .getKey());
253: assertEquals("testOverridePositionImplied(3) ",
254: "required-position-1", field.getArg("required", 1)
255: .getKey());
256: assertEquals("testOverridePositionImplied(4) ",
257: "required-position-2", field.getArg("required", 2)
258: .getKey());
259:
260: // use 'mask' as name
261: assertEquals("testOverridePositionImplied(5) ", 3, field
262: .getArgs("mask").length);
263: assertEquals("testOverridePositionImplied(6) ",
264: "default-position-0", field.getArg("mask", 0).getKey());
265: assertEquals("testOverridePositionImplied(7) ",
266: "mask-position-1", field.getArg("mask", 1).getKey());
267: assertNull("testOverridePositionImplied(8) ", field.getArg(
268: "mask", 2));
269:
270: // Get Defaults
271: assertEquals("testOverridePositionImplied(9) ",
272: "default-position-0", field.getArg(0).getKey());
273: assertNull("testOverridePositionImplied(10) ", field.getArg(1));
274: assertNull("testOverridePositionImplied(11) ", field.getArg(2));
275:
276: }
277:
278: /**
279: * test Field with a 'default' argument overriden with some positions specified
280: */
281: public void testOverrideSomePosition() {
282:
283: field.addArg(createArg("default-position-0"));
284: field.addArg(createArg("default-position-1"));
285: field.addArg(createArg("default-position-2"));
286: field.addArg(createArg("required-position-1", "required", 1));
287: field.addArg(createArg("required-position-2", "required"));
288: field.addArg(createArg("mask-position-3", "mask"));
289:
290: // use 'required' as name
291: assertEquals("testOverrideSomePosition(1) ", 4, field
292: .getArgs("required").length);
293: assertEquals("testOverrideSomePosition(2) ",
294: "default-position-0", field.getArg("required", 0)
295: .getKey());
296: assertEquals("testOverrideSomePosition(3) ",
297: "required-position-1", field.getArg("required", 1)
298: .getKey());
299: assertEquals("testOverrideSomePosition(4) ",
300: "required-position-2", field.getArg("required", 2)
301: .getKey());
302: assertNull("testOverrideSomePosition(5) ", field.getArg(
303: "required", 3));
304:
305: // use 'mask' as name
306: assertEquals("testOverrideSomePosition(6) ", 4, field
307: .getArgs("mask").length);
308: assertEquals("testOverrideSomePosition(7) ",
309: "default-position-0", field.getArg("mask", 0).getKey());
310: assertEquals("testOverrideSomePosition(8) ",
311: "default-position-1", field.getArg("mask", 1).getKey());
312: assertEquals("testOverrideSomePosition(9) ",
313: "default-position-2", field.getArg("mask", 2).getKey());
314: assertEquals("testOverrideSomePosition(10) ",
315: "mask-position-3", field.getArg("mask", 3).getKey());
316:
317: // Get Defaults
318: assertEquals("testOverrideSomePosition(11) ",
319: "default-position-0", field.getArg(0).getKey());
320: assertEquals("testOverrideSomePosition(12) ",
321: "default-position-1", field.getArg(1).getKey());
322: assertEquals("testOverrideSomePosition(13) ",
323: "default-position-2", field.getArg(2).getKey());
324: assertNull("testOverrideSomePosition(14) ", field.getArg(3));
325:
326: }
327:
328: /**
329: * Convenience Method - create argument (no name or position specified)
330: */
331: private Arg createArg(String key) {
332: Arg arg = new Arg();
333: arg.setKey(key);
334: return arg;
335: }
336:
337: /**
338: * Convenience Method - create argument (no name, position specified)
339: */
340: private Arg createArg(String key, int position) {
341: Arg arg = createArg(key);
342: arg.setPosition(position);
343: return arg;
344: }
345:
346: /**
347: * Convenience Method - create argument (name specified, no position)
348: */
349: private Arg createArg(String key, String name) {
350: Arg arg = createArg(key);
351: arg.setName(name);
352: return arg;
353: }
354:
355: /**
356: * Convenience Method - create argument (name & position specified)
357: */
358: private Arg createArg(String key, String name, int position) {
359: Arg arg = createArg(key, name);
360: arg.setPosition(position);
361: return arg;
362: }
363:
364: }
|