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: TestMetaData.java 3682 2007-03-07 16:22:08Z gbevin $
007: */
008: package com.uwyn.rife.site;
009:
010: import com.uwyn.rife.database.Datasources;
011: import com.uwyn.rife.database.querymanagers.generic.GenericQueryManager;
012: import com.uwyn.rife.database.querymanagers.generic.GenericQueryManagerFactory;
013: import com.uwyn.rife.tools.ObjectUtils;
014: import com.uwyn.rifetestmodels.Person;
015: import com.uwyn.rifetestmodels.PersonCallbacks;
016: import com.uwyn.rifetestmodels.PersonCloneable;
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.List;
020: import java.util.Set;
021: import junit.framework.TestCase;
022:
023: public class TestMetaData extends TestCase {
024: public TestMetaData(String name) {
025: super (name);
026: }
027:
028: public void testConstraintsValidation() {
029: Person person = new Person();
030:
031: Constrained constrained = (Constrained) person;
032: assertNotNull(constrained);
033:
034: Set<ValidationError> errors;
035:
036: Validated validated = (Validated) person;
037: assertFalse(validated.validate());
038: assertFalse(validated.isSubjectValid("firstname"));
039: assertTrue(validated.isSubjectValid("lastname"));
040: errors = validated.getValidationErrors();
041: assertEquals(1, errors.size());
042: assertEquals(ValidationError.IDENTIFIER_MANDATORY, errors
043: .iterator().next().getIdentifier());
044:
045: validated.resetValidation();
046: person.setFirstname("John");
047: person.setLastname("Smith");
048: assertTrue(validated.validate());
049:
050: validated.resetValidation();
051: person.setFirstname("John");
052: person.setLastname("Wayne");
053: assertFalse(validated.validate());
054: assertTrue(validated.isSubjectValid("firstname"));
055: assertFalse(validated.isSubjectValid("lastname"));
056: errors = validated.getValidationErrors();
057: assertEquals(1, errors.size());
058: assertEquals(ValidationError.IDENTIFIER_INVALID, errors
059: .iterator().next().getIdentifier());
060:
061: validated.resetValidation();
062: person.setFirstname("Dean Marie Alson");
063: person.setLastname("Jones");
064: assertFalse(validated.validate());
065: assertFalse(validated.isSubjectValid("firstname"));
066: assertTrue(validated.isSubjectValid("lastname"));
067: errors = validated.getValidationErrors();
068: assertEquals(1, errors.size());
069: assertEquals(ValidationError.IDENTIFIER_WRONGLENGTH, errors
070: .iterator().next().getIdentifier());
071: }
072:
073: public void testCloningNoMethod() {
074: Person person = new Person();
075:
076: person.setFirstname("John");
077: person.setLastname("Smith");
078: assertTrue(((Validated) person).validate());
079:
080: Person person_clone = ObjectUtils.genericClone(person);
081: assertTrue(((Validated) person_clone).validate());
082:
083: person_clone.setFirstname("Jeremy Jackson James");
084: person_clone.setLastname("Dillinger");
085: assertFalse(((Validated) person_clone).validate());
086: assertEquals(2, ((Validated) person_clone)
087: .countValidationErrors());
088:
089: assertEquals(0, ((Validated) person).countValidationErrors());
090: assertTrue(((Validated) person).validate());
091: ((Validated) person).resetValidation();
092:
093: assertEquals(2, ((Validated) person_clone)
094: .countValidationErrors());
095: }
096:
097: public void testCloningExistingMethod() {
098: PersonCloneable person = new PersonCloneable();
099: assertNull(person.getFirstname());
100: assertNull(person.getLastname());
101:
102: PersonCloneable person_clone;
103:
104: // check if the original clone methods it still working
105: person_clone = ObjectUtils.genericClone(person);
106: assertNotNull(person_clone);
107: assertEquals("autofirst", person_clone.getFirstname());
108: assertNull(person_clone.getLastname());
109:
110: // check that the instance properties as really seperated
111: assertNull(person.getFirstname());
112: assertNull(person.getLastname());
113:
114: // check that the validation features are really seperated
115: assertFalse(((Validated) person).validate());
116: assertEquals(1, ((Validated) person).countValidationErrors());
117:
118: assertEquals(0, ((Validated) person_clone)
119: .countValidationErrors());
120: assertTrue(((Validated) person_clone).validate());
121: assertEquals(0, ((Validated) person_clone)
122: .countValidationErrors());
123:
124: // try another path in the default clone method
125: // also test that the original validation errors are cloned correctly
126: ((Validated) person).resetValidation();
127: assertEquals(0, ((Validated) person).countValidationErrors());
128: person.setFirstname("this name is too long");
129: assertFalse(((Validated) person).validate());
130: assertEquals(1, ((Validated) person).countValidationErrors());
131:
132: person_clone = ObjectUtils.genericClone(person);
133: assertNotNull(person_clone);
134: assertEquals(1, ((Validated) person_clone)
135: .countValidationErrors());
136: assertEquals("this name is too long", person_clone
137: .getFirstname());
138: assertEquals("autolast", person_clone.getLastname());
139:
140: // check that the error resetting is really seperated
141: ((Validated) person).resetValidation();
142: assertEquals(0, ((Validated) person).countValidationErrors());
143: assertEquals(1, ((Validated) person_clone)
144: .countValidationErrors());
145:
146: ((Validated) person_clone).resetValidation();
147: assertEquals(0, ((Validated) person_clone)
148: .countValidationErrors());
149:
150: // check that the constrained properties are seperated
151: ConstrainedProperty prop = ((Constrained) person_clone)
152: .getConstrainedProperty("lastname");
153: assertNotNull(prop);
154: String[] list_array = prop.getInList();
155: List<String> list_list = new ArrayList<String>(Arrays
156: .asList(list_array));
157: list_list.add("Chronno");
158: prop.inList(list_list);
159: ((Constrained) person_clone).addConstraint(prop);
160: person_clone.setFirstname("Jeremy");
161: person_clone.setLastname("Chronno");
162: assertTrue(((Validated) person_clone).validate());
163: assertEquals(0, ((Validated) person_clone)
164: .countValidationErrors());
165:
166: person.setFirstname("Jeremy");
167: person.setLastname("Chronno");
168: assertFalse(((Validated) person).validate());
169: assertEquals(1, ((Validated) person).countValidationErrors());
170: }
171:
172: public void testCallbacks() {
173: PersonCallbacks person = new PersonCallbacks();
174:
175: person.setFirstname("John");
176: person.setLastname("Smith");
177:
178: GenericQueryManager<PersonCallbacks> manager = GenericQueryManagerFactory
179: .getInstance(Datasources.getRepInstance()
180: .getDatasource("unittestsderby"),
181: PersonCallbacks.class);
182: manager.install();
183: try {
184: int id = manager.save(person);
185: assertTrue(id >= 0);
186:
187: PersonCallbacks restored_person = manager.restore(id);
188: assertNotNull(restored_person);
189:
190: assertEquals("beforeSave", restored_person.getFirstname());
191: assertEquals("Smith", restored_person.getLastname());
192: } finally {
193: manager.remove();
194: }
195: }
196: }
|