001: package org.conform;
002:
003: import junit.framework.TestCase;
004:
005: import java.util.*;
006: import java.beans.PropertyChangeListener;
007: import java.beans.PropertyChangeEvent;
008:
009: import org.conform.validator.StringLengthValidator;
010:
011: public class DataTest extends TestCase {
012: Beans beans = new Beans();
013:
014: protected void setUp() throws Exception {
015: beans.addBeanMeta(new BeanMeta(Person.class));
016: beans.addBeanMeta(new BeanMeta(Address.class));
017: }
018:
019: public void testInitialization() throws Exception {
020: BeanMeta personBeanMeta = beans.getBeanMeta(Person.class);
021: personBeanMeta.getProperty("addresses").setInitializer(
022: new AddressesInitializer());
023:
024: DefaultBeanData personBeanData = new DefaultBeanData(
025: personBeanMeta);
026:
027: personBeanData.setValue(new Person());
028: assertNull(((Person) personBeanData.getValue()).getAddresses());
029:
030: personBeanData.initialize();
031: assertNotNull(((Person) personBeanData.getValue())
032: .getAddresses());
033: }
034:
035: public void testPropertyChangeNotification() throws Exception {
036: BeanMeta personBeanMeta = beans.getBeanMeta(Person.class);
037: personBeanMeta.getProperty("addresses").setInitializer(
038: new AddressesInitializer());
039: BeanMeta addressBeanMeta = beans.getBeanMeta(Address.class);
040: addressBeanMeta.getProperty("postcode").addValidator(
041: new StringLengthValidator(5));
042:
043: DefaultBeanData personBeanData = new DefaultBeanData(
044: personBeanMeta);
045: DefaultBeanData addressBeanData = new DefaultBeanData(
046: addressBeanMeta);
047:
048: TestPropertyChangeListener personBeanChangeListener = new TestPropertyChangeListener();
049: personBeanData
050: .addPropertyChangeListener(personBeanChangeListener);
051:
052: TestPropertyChangeListener addressBeanChangeListener = new TestPropertyChangeListener();
053: addressBeanData
054: .addPropertyChangeListener(addressBeanChangeListener);
055:
056: TestPropertyChangeListener postcodePropertyChangeListener = new TestPropertyChangeListener();
057: addressBeanData.addPropertyChangeListener("postcode",
058: postcodePropertyChangeListener);
059:
060: personBeanData.setValue(new Person());
061: personBeanData.initialize();
062:
063: assertNotNull(personBeanChangeListener.lastEvent);
064: assertEquals("addresses", personBeanChangeListener.lastEvent
065: .getPropertyName());
066: assertEquals(null, personBeanChangeListener.lastEvent
067: .getOldValue());
068: assertEquals(LinkedList.class,
069: personBeanChangeListener.lastEvent.getNewValue()
070: .getClass());
071:
072: personBeanChangeListener.lastEvent = null;
073: Address address = new Address();
074: addressBeanData.setValue(address);
075: assertNotNull(postcodePropertyChangeListener.lastEvent);
076: assertNull(postcodePropertyChangeListener.lastEvent
077: .getNewValue());
078: assertNotNull(addressBeanChangeListener.lastEvent);
079: assertNull(addressBeanChangeListener.lastEvent.getNewValue());
080:
081: postcodePropertyChangeListener.lastEvent = null;
082: addressBeanData.getPropertyData(
083: addressBeanMeta.getProperty("postcode")).setValue(
084: "123456");
085: assertNull(postcodePropertyChangeListener.lastEvent);
086:
087: addressBeanData.getPropertyData(
088: addressBeanMeta.getProperty("postcode")).setValue(
089: "12345");
090: assertNotNull(postcodePropertyChangeListener.lastEvent);
091: assertEquals("postcode",
092: postcodePropertyChangeListener.lastEvent
093: .getPropertyName());
094: assertEquals(null, postcodePropertyChangeListener.lastEvent
095: .getOldValue());
096: assertEquals("12345", postcodePropertyChangeListener.lastEvent
097: .getNewValue());
098:
099: addressBeanData.getPropertyData(
100: addressBeanMeta.getProperty("city")).setValue(
101: "Neverland");
102: assertNotNull(addressBeanChangeListener.lastEvent);
103: assertEquals("city", addressBeanChangeListener.lastEvent
104: .getPropertyName());
105: assertEquals(null, addressBeanChangeListener.lastEvent
106: .getOldValue());
107: assertEquals("Neverland", addressBeanChangeListener.lastEvent
108: .getNewValue());
109: }
110:
111: public void testBinding() throws Exception {
112: BeanMeta personBeanMeta = beans.getBeanMeta(Person.class);
113: BeanMeta addressBeanMeta = beans.getBeanMeta(Address.class);
114: addressBeanMeta.getProperty("postcode").addValidator(
115: new StringLengthValidator(5));
116:
117: DefaultBeanData personBeanData = new DefaultBeanData(
118: personBeanMeta);
119: DefaultBeanData addressBeanData = new DefaultBeanData(
120: addressBeanMeta);
121:
122: Person person = new Person();
123: personBeanData.setValue(person);
124: Address address = new Address();
125: addressBeanData.setValue(address);
126:
127: personBeanData.getPropertyData(
128: personBeanMeta.getProperty("firstName")).setValue(
129: "Holger");
130: personBeanData.getPropertyData(
131: personBeanMeta.getProperty("lastName")).setValue(
132: "Engels");
133: addressBeanData.getPropertyData(
134: addressBeanMeta.getProperty("street")).setValue(
135: "Road to Nowhere");
136: addressBeanData.getPropertyData(
137: addressBeanMeta.getProperty("postcode")).setValue(
138: "123456");
139: addressBeanData.getPropertyData(
140: addressBeanMeta.getProperty("city")).setValue(
141: "Neverland");
142:
143: assertEquals("Holger", personBeanData.getPropertyData(
144: personBeanMeta.getProperty("firstName")).getValue());
145: assertEquals("Engels", personBeanData.getPropertyData(
146: personBeanMeta.getProperty("lastName")).getValue());
147: assertEquals("Road to Nowhere", addressBeanData
148: .getPropertyData(addressBeanMeta.getProperty("street"))
149: .getValue());
150: assertEquals("123456", addressBeanData.getPropertyData(
151: addressBeanMeta.getProperty("postcode")).getValue());
152: assertEquals("Neverland", addressBeanData.getPropertyData(
153: addressBeanMeta.getProperty("city")).getValue());
154:
155: assertEquals("Holger", person.getFirstName());
156: assertEquals("Engels", person.getLastName());
157: assertEquals("Road to Nowhere", address.getStreet());
158: assertEquals("123456", addressBeanData.getPropertyData(
159: addressBeanMeta.getProperty("postcode"))
160: .getInvalidValue());
161: assertNull(address.getPostcode());
162: assertEquals("Neverland", address.getCity());
163: }
164:
165: public void testValidation() throws Exception {
166: BeanMeta personBeanMeta = beans.getBeanMeta(Person.class);
167: personBeanMeta.getProperty("firstName").setMandatory(true);
168: personBeanMeta.getProperty("lastName").setMandatory(true);
169:
170: BeanMeta addressBeanMeta = beans.getBeanMeta(Address.class);
171: addressBeanMeta.getProperty("postcode").addValidator(
172: new StringLengthValidator(5));
173: addressBeanMeta.addValidator(new BeanValidator() {
174: public Object validate(Object value)
175: throws ValidationException {
176: Address address = (Address) value;
177: if ((address.getPostcode() != null) != (address
178: .getCity() != null))
179: throw new ValidationException(
180: new ValidationException.Message(
181: "Specify postcode and city or nothing at all"));
182: else
183: return address;
184: }
185:
186: public String[] getAffectedPropertyNames() {
187: return new String[] { "postcode", "city" };
188: }
189: });
190:
191: Set<Issue> issues = new HashSet<Issue>();
192: ValidationListener validations = new TestValidationListener(
193: issues);
194:
195: DefaultBeanData personBeanData = new DefaultBeanData(
196: personBeanMeta);
197: DefaultBeanData addressBeanData = new DefaultBeanData(
198: addressBeanMeta);
199: personBeanData.addValidationListener(validations);
200: addressBeanData.addValidationListener(validations);
201: assertEquals(0, issues.size());
202:
203: personBeanData.setValue(new Person());
204: addressBeanData.setValue(new Address());
205: assertEquals(0, issues.size());
206:
207: personBeanData.setIssuePublishingActive(true);
208: addressBeanData.setIssuePublishingActive(true);
209: assertEquals(2, issues.size());
210:
211: personBeanData.getPropertyData(
212: personBeanMeta.getProperty("firstName")).setValue(
213: "Holger");
214: personBeanData.getPropertyData(
215: personBeanMeta.getProperty("lastName")).setValue(
216: "Engels");
217: addressBeanData.getPropertyData(
218: addressBeanMeta.getProperty("postcode")).setValue(
219: "123456");
220: validate(personBeanData, addressBeanData, false);
221: System.out.println("issues = " + issues);
222: assertEquals(1, issues.size());
223:
224: personBeanData.setIssuePublishingActive(false);
225: addressBeanData.setIssuePublishingActive(false);
226: assertEquals(0, issues.size());
227:
228: addressBeanData.getPropertyData(
229: addressBeanMeta.getProperty("postcode")).setValue(
230: "12345");
231: validate(personBeanData, addressBeanData, true);
232:
233: personBeanData.setIssuePublishingActive(true);
234: addressBeanData.setIssuePublishingActive(true);
235: assertEquals("" + issues, 1, issues.size());
236:
237: addressBeanData.getPropertyData(
238: addressBeanMeta.getProperty("city")).setValue("Ulm");
239: validate(personBeanData, addressBeanData, false);
240: assertEquals(0, issues.size());
241:
242: personBeanData.getPropertyData(
243: personBeanMeta.getProperty("firstName")).setValue(null);
244: personBeanData.getPropertyData(
245: personBeanMeta.getProperty("lastName")).setValue(null);
246: assertEquals(2, issues.size());
247: }
248:
249: public void testValidationChanges() throws Exception {
250: BeanMeta personBeanMeta = beans.getBeanMeta(Person.class);
251: personBeanMeta.getProperty("firstName").setMandatory(true);
252: personBeanMeta.getProperty("lastName").setMandatory(true);
253:
254: BeanMeta addressBeanMeta = beans.getBeanMeta(Address.class);
255: addressBeanMeta.getProperty("postcode").addValidator(
256: new StringLengthValidator(5));
257: BeanValidator addressValidator = new BeanValidator() {
258: public Object validate(Object value)
259: throws ValidationException {
260: Address address = (Address) value;
261: if ((address.getPostcode() != null) != (address
262: .getCity() != null))
263: throw new ValidationException(
264: new ValidationException.Message(
265: "Specify postcode and city or nothing at all"));
266: else
267: return address;
268: }
269:
270: public String[] getAffectedPropertyNames() {
271: return new String[] { "postcode", "city" };
272: }
273: };
274: addressBeanMeta.addValidator(addressValidator);
275:
276: Set<Issue> issues = new HashSet<Issue>();
277: ValidationListener validations = new TestValidationListener(
278: issues);
279:
280: DefaultBeanData personBeanData = new DefaultBeanData(
281: personBeanMeta);
282: DefaultBeanData addressBeanData = new DefaultBeanData(
283: addressBeanMeta);
284: personBeanData.addValidationListener(validations);
285: addressBeanData.addValidationListener(validations);
286:
287: personBeanData.setValue(new Person());
288: addressBeanData.setValue(new Address());
289: addressBeanData.getPropertyData(
290: addressBeanMeta.getProperty("postcode")).setValue(
291: "12345");
292:
293: personBeanData.setIssuePublishingActive(true);
294: addressBeanData.setIssuePublishingActive(true);
295: validate(personBeanData, addressBeanData, true);
296: assertEquals(3, issues.size()); // firstName, lastName, Address(postcode, city)
297:
298: personBeanMeta.getProperty("firstName").setMandatory(false);
299: personBeanMeta.getProperty("lastName").setMandatory(false);
300: personBeanData.revalidateAll();
301: System.out.println("issues = " + issues);
302: assertEquals(1, issues.size()); // Address(postcode, city)
303:
304: addressBeanData.getPropertyData(
305: addressBeanMeta.getProperty("city")).setValue("Ulm");
306: addressBeanData.revalidateAll();
307: assertEquals(0, issues.size());
308: }
309:
310: private void validate(DefaultBeanData personBeanData,
311: DefaultBeanData addressBeanData, boolean expectException) {
312: boolean exception = false;
313: try {
314: personBeanData.validate(personBeanData.getValue());
315: } catch (ValidationException e) {
316: exception = true;
317: }
318: try {
319: addressBeanData.validate(addressBeanData.getValue());
320: } catch (ValidationException e) {
321: exception = true;
322: }
323: if (exception != expectException)
324: fail();
325: }
326:
327: private static class TestValidationListener implements
328: ValidationListener {
329: private final Set<Issue> issues;
330:
331: public TestValidationListener(Set<Issue> issues) {
332: this .issues = issues;
333: }
334:
335: public void addIssues(ValidationEvent event) {
336: issues.removeAll(event.getIssues());
337: issues.addAll(event.getIssues());
338: }
339:
340: public void removeIssues(ValidationEvent event) {
341: issues.removeAll(event.getIssues());
342: }
343:
344: public void clearIssues(Meta meta) {
345: for (Iterator<Issue> iterator = issues.iterator(); iterator
346: .hasNext();) {
347: Issue issue = iterator.next();
348: PropertyMeta[] issueMetas = issue.getMetas();
349: for (PropertyMeta issueMeta : issueMetas) {
350: if (issueMeta.getBeanMeta() == meta) {
351: iterator.remove();
352: break;
353: }
354: }
355: }
356: }
357: }
358:
359: private static class TestPropertyChangeListener implements
360: PropertyChangeListener {
361: public PropertyChangeEvent lastEvent;
362:
363: public void propertyChange(PropertyChangeEvent event) {
364: lastEvent = event;
365: }
366: }
367:
368: private static class AddressesInitializer implements Initializer {
369: public Object getValue() {
370: return new LinkedList<Address>();
371: }
372: }
373: }
|