01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: TestValidationRuleLimitedLength.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.site;
09:
10: import junit.framework.TestCase;
11:
12: public class TestValidationRuleLimitedLength extends TestCase {
13: public TestValidationRuleLimitedLength(String name) {
14: super (name);
15: }
16:
17: public void testInstantiation() {
18: Bean bean = new Bean("123456");
19: ValidationRuleLimitedLength rule = new ValidationRuleLimitedLength(
20: "property", 1, 6).setBean(bean);
21: assertNotNull(rule);
22: }
23:
24: public void testValid() {
25: Bean bean = new Bean("123456");
26: ValidationRuleLimitedLength rule = new ValidationRuleLimitedLength(
27: "property", 1, 6).setBean(bean);
28: assertTrue(rule.validate());
29: }
30:
31: public void testValidArray() {
32: Bean bean = new Bean(new String[] { "123456", "FDF3", "9J" });
33: ValidationRuleLimitedLength rule = new ValidationRuleLimitedLength(
34: "arrayProperty", 1, 6).setBean(bean);
35: assertTrue(rule.validate());
36: }
37:
38: public void testInvalid() {
39: Bean bean = new Bean("123456");
40: ValidationRuleLimitedLength rule = new ValidationRuleLimitedLength(
41: "property", 1, 4).setBean(bean);
42: assertFalse(rule.validate());
43: }
44:
45: public void testInvalidArray() {
46: Bean bean = new Bean(new String[] { "123456", "FDF3", "9J" });
47: ValidationRuleLimitedLength rule = new ValidationRuleLimitedLength(
48: "arrayProperty", 3, 6).setBean(bean);
49: assertFalse(rule.validate());
50: }
51:
52: public void testUnknownProperty() {
53: Bean bean = new Bean("123456");
54: ValidationRuleLimitedLength rule = new ValidationRuleLimitedLength(
55: "unknown_property", 1, 6).setBean(bean);
56: assertTrue(rule.validate());
57: }
58:
59: public void testGetError() {
60: Bean bean = new Bean("123456");
61: ValidationRuleLimitedLength rule = new ValidationRuleLimitedLength(
62: "property", 1, 4).setBean(bean);
63: ValidationError error = rule.getError();
64: assertEquals(ValidationError.IDENTIFIER_WRONGLENGTH, error
65: .getIdentifier());
66: assertEquals("property", error.getSubject());
67: assertEquals(rule.getSubject(), error.getSubject());
68: }
69:
70: public class Bean {
71: private String mProperty = null;
72: private String[] mArrayProperty = null;
73:
74: public Bean(String property) {
75: mProperty = property;
76: }
77:
78: public Bean(String[] arrayProperty) {
79: mArrayProperty = arrayProperty;
80: }
81:
82: public void setArrayProperty(String[] arrayProperty) {
83: mArrayProperty = arrayProperty;
84: }
85:
86: public String[] getArrayProperty() {
87: return mArrayProperty;
88: }
89:
90: public void setProperty(String property) {
91: mProperty = property;
92: }
93:
94: public String getProperty() {
95: return mProperty;
96: }
97: }
98: }
|