001: package org.concern.model.cpd;
002:
003: import java.beans.Introspector;
004: import java.beans.PropertyDescriptor;
005: import java.io.File;
006: import java.io.FileOutputStream;
007: import java.io.OutputStream;
008: import java.util.Collection;
009: import java.util.Iterator;
010:
011: import junit.framework.TestCase;
012:
013: import org.concern.model.Activity;
014: import org.concern.model.Actor;
015: import org.concern.model.AsynchronousActivity;
016: import org.concern.model.Collaborator;
017: import org.concern.model.Condition;
018: import org.concern.model.Event;
019: import org.concern.model.Interaction;
020: import org.concern.model.Listener;
021: import org.concern.model.Loader;
022: import org.concern.model.Process;
023: import org.concern.model.SynchronousActivity;
024:
025: public class CPDFilePersistenceTest extends TestCase {
026: private Process process;
027: private Actor actor;
028: private Collaborator collaborator;
029: private Condition condition_a;
030: private Condition condition_b;
031: private Activity activity_A;
032: private Activity activity_B;
033: private Activity activity_C;
034: private Event event_E;
035: private Event event_F;
036: private Listener listener;
037: private Loader loader;
038:
039: public CPDFilePersistenceTest(String name) {
040: super (name);
041: }
042:
043: protected void setUp() throws Exception {
044: actor = new Actor("W");
045: actor.setImplementation("test.Actor");
046: collaborator = new Collaborator("C");
047: collaborator.setCardinality(Collaborator.MANY_TO_ONE);
048:
049: condition_a = new Condition("a");
050: condition_a.setImplementation("test.Condition$A");
051: condition_b = new Condition("b");
052: condition_b.setImplementation("test.Condition$B");
053: condition_b.setTemporal(true);
054:
055: activity_A = new AsynchronousActivity("A");
056: activity_A.setImplementation("test.Activity$A");
057: activity_A.setPrecondition("!a");
058: activity_A.setPostcondition("a");
059: ((AsynchronousActivity) activity_A).setActor("W");
060: ((AsynchronousActivity) activity_A).setTimeout(3600);
061: activity_A.setInteraction(new Interaction(Interaction.IN,
062: "stop", "C"));
063:
064: activity_B = new AsynchronousActivity("B");
065: activity_B.setImplementation("test.Activity$B");
066: activity_B.setPrecondition("!a");
067: activity_B.setPostcondition("a");
068: ((AsynchronousActivity) activity_B).setOptional(true);
069: activity_B.setInteraction(new Interaction(Interaction.BOTH,
070: "communicate", "C"));
071:
072: activity_C = new SynchronousActivity("C");
073: activity_C.setImplementation("test.Activity$C");
074: activity_C.setPrecondition("a && !b");
075: activity_C.setPostcondition("b");
076: ((SynchronousActivity) activity_C).setRetryDelay(3600);
077: ((SynchronousActivity) activity_C).setTrials(3);
078: activity_C.setInteraction(new Interaction(Interaction.OUT,
079: "start", "C"));
080:
081: event_E = new Event("E");
082: event_E.setImplementation("test.Event$E");
083: event_E.setPostcondition("!a");
084: event_E.setInteraction(new Interaction(Interaction.IN,
085: "listen", "C"));
086:
087: event_F = new Event("E");
088: event_E.setImplementation("test.Event$F");
089: event_F.setPostcondition("!a");
090: event_F.setUser(true);
091:
092: listener = new Listener("L");
093: listener.setImplementation("test.Listener");
094: listener.setPrecondition("b");
095: listener.setInteraction(new Interaction(Interaction.OUT,
096: "notify", "C"));
097:
098: loader = new Loader("S");
099:
100: process = new Process("Test");
101: process.setSubject("test.Subject");
102: process.setLoader(loader);
103: process.addActor(actor);
104: process.addCollaborator(collaborator);
105: process.addCondition(condition_a);
106: process.addCondition(condition_b);
107: process.addActivity(activity_A);
108: process.addActivity(activity_B);
109: process.addActivity(activity_C);
110: process.addEvent(event_E);
111: process.addEvent(event_F);
112: process.addListener(listener);
113:
114: }
115:
116: public void testPersistence() throws Exception {
117: File file = File.createTempFile("dji-concern", ".cpd");
118: ProcessWriter.save(process, file);
119:
120: Process reloaded = ProcessLoader.load(file);
121:
122: /*
123: assertEquals(loader, reloaded.getInteraction());
124: assertEquals(actor.getName(), reloaded.getActor("W").getName());
125: assertEquals(collaboration, reloaded.getActor("C"));
126: */
127:
128: compareBeans("process", process, reloaded);
129: }
130:
131: public void compareBeans(String name, Object bean, Object with)
132: throws Exception {
133: String className = (bean != null) ? bean.getClass().getName()
134: : (with != null) ? with.getClass().getName() : "";
135: System.out.println(name + "[" + className + "]: " + bean + " "
136: + with);
137:
138: if (bean instanceof Collection) {
139: Collection beanCollection = (Collection) bean;
140: Collection withCollection = (Collection) with;
141: assertEquals(name, beanCollection.size(), withCollection
142: .size());
143:
144: Iterator beanIterator = beanCollection.iterator();
145: Iterator withIterator = withCollection.iterator();
146: int i = 0;
147: while (beanIterator.hasNext()) {
148: Object beanMember = beanIterator.next();
149: Object withMember = withIterator.next();
150: compareBeans(name + "[" + i++ + "]", beanMember,
151: withMember);
152: }
153: } else if (bean == null) {
154: assertNull(name, with);
155: } else {
156: PropertyDescriptor[] descriptors = Introspector
157: .getBeanInfo(bean.getClass(), Object.class)
158: .getPropertyDescriptors();
159: if (bean.getClass().getName().startsWith("java.lang")
160: || descriptors.length == 0) {
161: assertEquals(name, bean, with);
162: } else {
163: for (int i = 0; i < descriptors.length; i++) {
164: PropertyDescriptor descriptor = descriptors[i];
165: if (descriptor.getReadMethod() == null
166: || descriptor.getPropertyType().getName()
167: .startsWith("["))
168: continue;
169:
170: Object beanValue = descriptor.getReadMethod()
171: .invoke(bean, null);
172: try {
173: Object withValue = descriptor.getReadMethod()
174: .invoke(with, null);
175: compareBeans(name + "." + descriptor.getName(),
176: beanValue, withValue);
177: } catch (IllegalArgumentException ex) {
178: System.out.println();
179: System.out.println("dji ******************** "
180: + name + "[" + className + "]: " + bean
181: + " " + with);
182: System.out
183: .println("dji ******************** bean class: "
184: + bean.getClass().getName());
185: System.out
186: .println("dji ******************** bean descriptor: "
187: + descriptor.getName());
188: System.out
189: .println("dji ******************** bean value: "
190: + beanValue);
191: System.out
192: .println("dji ******************** with class: "
193: + with.getClass().getName());
194: System.out.println();
195: throw ex;
196: }
197: }
198: }
199: }
200: }
201: }
|