001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestValidationGroup.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.site;
009:
010: import com.uwyn.rife.site.exceptions.ValidationException;
011: import java.util.List;
012: import junit.framework.TestCase;
013:
014: public class TestValidationGroup extends TestCase {
015: public TestValidationGroup(String name) {
016: super (name);
017: }
018:
019: public void testInstantiation() {
020: Validation validation = new Validation();
021: ValidationGroup group1 = validation.addGroup("group1");
022: assertNotNull(group1);
023: ValidationGroup group2 = validation.addGroup("group2");
024: assertNotNull(group2);
025: ValidationGroup group3 = validation.addGroup("group1");
026: assertNotNull(group3);
027:
028: assertNotSame(group1, validation.getGroup("group1"));
029: assertSame(group3, validation.getGroup("group1"));
030: assertSame(group2, validation.getGroup("group2"));
031: assertSame(group3, validation.getGroup("group1"));
032: }
033:
034: public void testInitialstate() {
035: Validation validation = new Validation();
036: ValidationGroup group = validation.addGroup("mygroup");
037: assertSame(validation, group.getValidation());
038: assertEquals("mygroup", group.getName());
039: assertEquals(0, group.getSubjects().size());
040: }
041:
042: public void testAddSubject() {
043: Validation validation = new Validation();
044: ValidationGroup group = validation.addGroup("mygroup");
045: assertSame(group, group.addSubject("subject1"));
046: assertSame(group, group.addSubject("subject2"));
047: assertSame(group, group.addSubject("subject3"));
048: assertSame(group, group.addSubject("subject1"));
049: assertSame(group, group.addSubject("subject4"));
050:
051: List<String> subjects = group.getSubjects();
052: assertEquals(4, subjects.size());
053: assertEquals("subject1", subjects.get(0));
054: assertEquals("subject2", subjects.get(1));
055: assertEquals("subject3", subjects.get(2));
056: assertEquals("subject4", subjects.get(3));
057: }
058:
059: public void testReinitializeProperties() {
060: InitializedBeanImpl bean = new InitializedBeanImpl();
061: bean.setString("one");
062: bean.setInt(9);
063: bean.setInteger(12);
064: bean.setCharacter('Z');
065:
066: Validation validation = new Validation();
067: ValidationGroup group = validation.addGroup("mygroup");
068:
069: group.reinitializeProperties(bean);
070:
071: assertEquals("one", bean.getString());
072: assertNull(bean.getStringbuffer());
073: assertEquals(9, bean.getInt());
074: assertEquals(12, bean.getInteger().intValue());
075: assertEquals('i', bean.getChar());
076: assertEquals('Z', bean.getCharacter().charValue());
077:
078: group.addConstraint(new ConstrainedProperty("string"))
079: .addSubject("int").addSubject("integer").addSubject(
080: "char").addSubject("character");
081:
082: group.reinitializeProperties(null);
083:
084: assertEquals("one", bean.getString());
085: assertNull(bean.getStringbuffer());
086: assertEquals(9, bean.getInt());
087: assertEquals(12, bean.getInteger().intValue());
088: assertEquals('i', bean.getChar());
089: assertEquals('Z', bean.getCharacter().charValue());
090:
091: group.reinitializeProperties(bean);
092:
093: assertEquals("default", bean.getString());
094: assertNull(bean.getStringbuffer());
095: assertEquals(-1, bean.getInt());
096: assertNull(bean.getInteger());
097: assertEquals('i', bean.getChar());
098: assertEquals('k', bean.getCharacter().charValue());
099: }
100:
101: public void testReinitializePropertiesError() {
102: Validation validation = new Validation();
103: ValidationGroup group = validation.addGroup("mygroup");
104: group.addSubject("string");
105:
106: NonStaticBean bean = new NonStaticBean();
107: try {
108: group.reinitializeProperties(bean);
109: fail();
110: } catch (ValidationException e) {
111: assertTrue(e.getCause() instanceof InstantiationException);
112: }
113: }
114:
115: class NonStaticBean {
116: }
117:
118: public void testAddRule() {
119: Validation validation = new Validation();
120: ValidationGroup group = validation.addGroup("mygroup");
121: assertSame(group, group.addRule(new ValidationRuleNotNull(
122: "subject1")));
123: assertSame(group, group.addRule(new ValidationRuleNotEmpty(
124: "subject2")));
125: assertSame(group, group.addRule(new ValidationRuleUrl(
126: "subject3")));
127: assertSame(group, group.addRule(new ValidationRuleEmail(
128: "subject1")));
129: assertSame(group, group.addRule(new ValidationRuleNotEqual(
130: "subject4", "test")));
131:
132: List<String> subjects = group.getSubjects();
133: assertEquals(4, subjects.size());
134: assertEquals("subject1", subjects.get(0));
135: assertEquals("subject2", subjects.get(1));
136: assertEquals("subject3", subjects.get(2));
137: assertEquals("subject4", subjects.get(3));
138:
139: subjects = validation.getValidatedSubjects();
140: assertEquals(4, subjects.size());
141: assertEquals("subject1", subjects.get(0));
142: assertEquals("subject2", subjects.get(1));
143: assertEquals("subject3", subjects.get(2));
144: assertEquals("subject4", subjects.get(3));
145:
146: List<ValidationRule> rules = validation.getRules();
147: assertEquals(5, rules.size());
148: assertTrue(rules.get(0) instanceof ValidationRuleNotNull);
149: assertEquals("subject1", rules.get(0).getSubject());
150: assertTrue(rules.get(1) instanceof ValidationRuleNotEmpty);
151: assertEquals("subject2", rules.get(1).getSubject());
152: assertTrue(rules.get(2) instanceof ValidationRuleUrl);
153: assertEquals("subject3", rules.get(2).getSubject());
154: assertTrue(rules.get(3) instanceof ValidationRuleEmail);
155: assertEquals("subject1", rules.get(3).getSubject());
156: assertTrue(rules.get(4) instanceof ValidationRuleNotEqual);
157: assertEquals("subject4", rules.get(4).getSubject());
158: }
159:
160: public void testAddConstraint() {
161: Validation validation = new Validation();
162: ValidationGroup group = validation.addGroup("mygroup");
163:
164: ConstrainedProperty property1 = new ConstrainedProperty(
165: "subject1").notNull(true).notEmpty(true);
166: ConstrainedProperty property2 = new ConstrainedProperty(
167: "subject2").url(true);
168: ConstrainedProperty property3 = new ConstrainedProperty(
169: "subject3").email(true);
170: ConstrainedProperty property4 = new ConstrainedProperty(
171: "subject4").notEqual("test");
172: ConstrainedProperty property5 = new ConstrainedProperty(
173: "subject5");
174:
175: assertSame(group, group.addConstraint(property1));
176: assertSame(group, group.addConstraint(property2));
177: assertSame(group, group.addConstraint(property3));
178: assertSame(group, group.addConstraint(property4));
179: assertSame(group, group.addConstraint(property5));
180:
181: List<String> subjects = group.getSubjects();
182: assertEquals(4, subjects.size());
183: assertEquals("subject1", subjects.get(0));
184: assertEquals("subject2", subjects.get(1));
185: assertEquals("subject3", subjects.get(2));
186: assertEquals("subject4", subjects.get(3));
187:
188: subjects = validation.getValidatedSubjects();
189: assertEquals(4, subjects.size());
190: assertEquals("subject1", subjects.get(0));
191: assertEquals("subject2", subjects.get(1));
192: assertEquals("subject3", subjects.get(2));
193: assertEquals("subject4", subjects.get(3));
194:
195: List<ValidationRule> rules = validation.getRules();
196: assertEquals(5, rules.size());
197: assertTrue(rules.get(0) instanceof ValidationRuleNotNull);
198: assertEquals("subject1", rules.get(0).getSubject());
199: assertTrue(rules.get(1) instanceof ValidationRuleNotEmpty);
200: assertEquals("subject1", rules.get(1).getSubject());
201: assertTrue(rules.get(2) instanceof ValidationRuleUrl);
202: assertEquals("subject2", rules.get(2).getSubject());
203: assertTrue(rules.get(3) instanceof ValidationRuleEmail);
204: assertEquals("subject3", rules.get(3).getSubject());
205: assertTrue(rules.get(4) instanceof ValidationRuleNotEqual);
206: assertEquals("subject4", rules.get(4).getSubject());
207:
208: assertEquals(5, validation.getConstrainedProperties().size());
209: assertSame(property1, validation
210: .getConstrainedProperty("subject1"));
211: assertSame(property2, validation
212: .getConstrainedProperty("subject2"));
213: assertSame(property3, validation
214: .getConstrainedProperty("subject3"));
215: assertSame(property4, validation
216: .getConstrainedProperty("subject4"));
217: assertSame(property5, validation
218: .getConstrainedProperty("subject5"));
219: }
220:
221: public void testAddGroup() {
222: Validation validation = new Validation();
223: ValidationGroup group = validation.addGroup("mygroup");
224: ValidationGroup group2 = group.addGroup("group2");
225:
226: assertSame(group2, validation.getGroup("group2"));
227:
228: assertSame(group, group.addSubject("subject1"));
229: assertSame(group, group.addSubject("subject2"));
230: assertSame(group2, group2.addSubject("subject3"));
231: assertSame(group2, group2.addSubject("subject1"));
232: assertSame(group2, group2.addSubject("subject4"));
233:
234: List<String> subjects = null;
235: subjects = group.getSubjects();
236: assertEquals(4, subjects.size());
237: assertEquals("subject1", subjects.get(0));
238: assertEquals("subject2", subjects.get(1));
239: assertEquals("subject3", subjects.get(2));
240: assertEquals("subject4", subjects.get(3));
241:
242: subjects = group2.getSubjects();
243: assertEquals(3, subjects.size());
244: assertEquals("subject3", subjects.get(0));
245: assertEquals("subject1", subjects.get(1));
246: assertEquals("subject4", subjects.get(2));
247: }
248:
249: public void testAddGroupAddRule() {
250: Validation validation = new Validation();
251: ValidationGroup group = validation.addGroup("group").addGroup(
252: "mygroup");
253: assertSame(group, group.addRule(new ValidationRuleNotNull(
254: "subject1")));
255:
256: List<String> subjects = group.getSubjects();
257: assertEquals(1, subjects.size());
258: assertEquals("subject1", subjects.get(0));
259:
260: subjects = validation.getValidatedSubjects();
261: assertEquals(1, subjects.size());
262: assertEquals("subject1", subjects.get(0));
263:
264: List<ValidationRule> rules = validation.getRules();
265: assertEquals(1, rules.size());
266: assertTrue(rules.get(0) instanceof ValidationRuleNotNull);
267: assertEquals("subject1", rules.get(0).getSubject());
268: }
269:
270: public void testAddGroupAddConstraint() {
271: Validation validation = new Validation();
272: ValidationGroup group = validation.addGroup("group").addGroup(
273: "mygroup");
274:
275: ConstrainedProperty property1 = new ConstrainedProperty(
276: "subject1").notNull(true).notEmpty(true);
277:
278: assertSame(group, group.addConstraint(property1));
279:
280: List<String> subjects = group.getSubjects();
281: assertEquals(1, subjects.size());
282: assertEquals("subject1", subjects.get(0));
283:
284: subjects = validation.getValidatedSubjects();
285: assertEquals(1, subjects.size());
286: assertEquals("subject1", subjects.get(0));
287:
288: List<ValidationRule> rules = validation.getRules();
289: assertEquals(2, rules.size());
290: assertTrue(rules.get(0) instanceof ValidationRuleNotNull);
291: assertEquals("subject1", rules.get(0).getSubject());
292: assertTrue(rules.get(1) instanceof ValidationRuleNotEmpty);
293: assertEquals("subject1", rules.get(1).getSubject());
294:
295: assertEquals(1, validation.getConstrainedProperties().size());
296: assertSame(property1, validation
297: .getConstrainedProperty("subject1"));
298: }
299:
300: public void testClone() {
301: Validation validation = new Validation();
302: ValidationGroup group1 = validation.addGroup("mygroup");
303: group1.addSubject("subject1");
304: group1.addSubject("subject2");
305: group1.addSubject("subject3");
306: group1.addSubject("subject4");
307:
308: ValidationGroup group2 = (ValidationGroup) group1.clone();
309: assertNotSame(group2, group1);
310: assertSame(group2.getValidation(), group1.getValidation());
311: assertEquals(group2.getName(), group1.getName());
312:
313: List<String> subjects1 = group1.getSubjects();
314: List<String> subjects2 = group2.getSubjects();
315: assertEquals(subjects2.size(), subjects1.size());
316: assertEquals(subjects2.get(0), subjects1.get(0));
317: assertEquals(subjects2.get(1), subjects1.get(1));
318: assertEquals(subjects2.get(2), subjects1.get(2));
319: assertEquals(subjects2.get(3), subjects1.get(3));
320: }
321: }
|