KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > test > MDRTestAssociationPreEvents


1 /*
2  * TestListeners.java
3  * NetBeans JUnit based test
4  *
5  * Created on 5. duben 2002, 17:00
6  */

7
8 package org.netbeans.mdr.test;
9
10 import java.util.Iterator JavaDoc;
11 import java.util.Collection JavaDoc;
12
13 import junit.framework.*;
14
15 import org.netbeans.mdr.handlers.BaseObjectHandler;
16 import org.netbeans.mdr.NBMDRepositoryImpl;
17
18 import org.netbeans.api.mdr.MDRManager;
19 import org.netbeans.api.mdr.MDRepository;
20 import org.netbeans.api.mdr.CreationFailedException;
21
22 import org.netbeans.api.mdr.events.*;
23
24 import javax.jmi.reflect.*;
25 import javax.jmi.model.*;
26
27 /**
28  *
29  * @author mk97936
30  */

31 public class MDRTestAssociationPreEvents extends MDREventsTestCase implements MDRPreChangeListener {
32     
33     private static final String JavaDoc PKG_NAME = "TEST_MOF";
34
35     private EventsList eventsList = new EventsList();
36     
37     public MDRTestAssociationPreEvents(java.lang.String JavaDoc testName) {
38         super(testName);
39     }
40     
41     protected void setUp() {
42         super.setUp();
43         ((MDRChangeSource) repository).addListener(this);
44     }
45     
46     protected void tearDown() {
47         super.tearDown();
48         ((MDRChangeSource) repository).removeListener(this);
49     }
50     
51     // -------------------------------------------------------------------------
52

53     public void testAddAssociation() {
54         
55         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
56
57         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
58         
59         Collection JavaDoc allMOFAssociations = testMOFPackage.refAllAssociations();
60         Iterator JavaDoc iterAssoc = allMOFAssociations.iterator();
61         while (iterAssoc.hasNext()) {
62             RefAssociation assocProxy = (RefAssociation) iterAssoc.next();
63             Association metaAssoc = (Association) assocProxy.refMetaObject();
64             eventsList.addPattern2(metaAssoc.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED, System.currentTimeMillis());
65             java.util.List JavaDoc ends = generator.generateAssociation(assocProxy, metaAssoc, null, true);
66             // !@#$%^& wait until event arives
67
for (int i = 0; i < 20; i++) {
68                 try { Thread.currentThread().sleep(100); }
69                     catch (InterruptedException JavaDoc ie) { }
70             }
71             if (ends != null) {
72                 assertTrue(eventsList.checkEvents(metaAssoc.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED));
73             }
74             eventsList.clear();
75         }
76         
77     }
78     
79     public void testRemoveAssociation() {
80         
81         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
82
83         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
84         
85         Collection JavaDoc allMOFAssociations = testMOFPackage.refAllAssociations();
86         Iterator JavaDoc iterAssoc = allMOFAssociations.iterator();
87         while (iterAssoc.hasNext()) {
88             RefAssociation assocProxy = (RefAssociation) iterAssoc.next();
89             Association metaAssoc = (Association) assocProxy.refMetaObject();
90             eventsList.addPattern2(metaAssoc.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED, System.currentTimeMillis());
91             java.util.List JavaDoc ends = generator.generateAssociation(assocProxy, metaAssoc, null, true);
92             // !@#$%^& wait until event arives
93
for (int i = 0; i < 20; i++) {
94                 try { Thread.currentThread().sleep(100); }
95                     catch (InterruptedException JavaDoc ie) { }
96             }
97             if (ends != null) {
98                 assertTrue(eventsList.checkEvents(metaAssoc.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED));
99             }
100             eventsList.clear();
101             
102             if (ends != null) {
103                 Collection JavaDoc links = assocProxy.refAllLinks();
104                 Object JavaDoc [] linksArray = links.toArray();
105                 if (linksArray.length >= 1) {
106                     RefAssociationLink link = (RefAssociationLink) linksArray[0];
107                     eventsList.addPattern2(metaAssoc.getName(), EventsList._PLANNED | EventsList._REMOVE | EventsList._CHANGED, System.currentTimeMillis());
108                     assocProxy.refRemoveLink(link.refFirstEnd(), link.refSecondEnd());
109                     // !@#$%^& wait until event arives
110
for (int i = 0; i < 20; i++) {
111                         try { Thread.currentThread().sleep(100); }
112                             catch (InterruptedException JavaDoc ie) { }
113                     }
114                     assertTrue(eventsList.checkEvents(metaAssoc.getName(), EventsList._PLANNED | EventsList._REMOVE | EventsList._CHANGED));
115                     eventsList.clear();
116                 } else {
117                     fail("Association contained no link!");
118                 }
119             }
120         }
121         
122     }
123     
124     // -------------------------------------------------------------------------
125

126     public void change(MDRChangeEvent e) {
127         assertEquals("Wrong order of events!", eventsQueue.getFirst(), e);
128         eventsQueue.removeFirst();
129         processEvent(e, CHANGED);
130     }
131     
132     public void changeCancelled(MDRChangeEvent e) {
133         assertEquals("Wrong order of events!", eventsQueue.getFirst(), e);
134         eventsQueue.removeFirst();
135         processEvent(e, CANCELED);
136     }
137     
138     public void plannedChange(MDRChangeEvent e) {
139         eventsQueue.add(e);
140         processEvent(e, PLANNED);
141     }
142     
143     private void processEvent(MDRChangeEvent e, int type) {
144         
145         if (e.isOfType(AssociationEvent.EVENTMASK_ASSOCIATION)) { // AssociationEvent
146

147             // EVENT_ASSOCIATION_ADD
148
if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_ADD) && (type == PLANNED)) {
149                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(),
150                                     e, System.currentTimeMillis(), EventsList._ADD | EventsList._PLANNED);
151             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_ADD) && (type == CHANGED)) {
152                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(),
153                                     e, System.currentTimeMillis(), EventsList._ADD | EventsList._CHANGED);
154             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_ADD) && (type == CANCELED)) {
155                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(),
156                                     e, System.currentTimeMillis(), EventsList._ADD | EventsList._CANCELED);
157             }
158             // EVENT_ASSOCIATION_SET
159
else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_SET) && (type == PLANNED)) {
160                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(),
161                                     e, System.currentTimeMillis(), EventsList._SET | EventsList._PLANNED);
162             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_SET) && (type == CHANGED)) {
163                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(),
164                                     e, System.currentTimeMillis(), EventsList._SET | EventsList._CHANGED);
165             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_SET) && (type == CANCELED)) {
166                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(),
167                                     e, System.currentTimeMillis(), EventsList._SET | EventsList._CANCELED);
168             }
169             // EVENT_ASSOCIATION_REMOVE
170
else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_REMOVE) && (type == PLANNED)) {
171                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(),
172                                     e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._PLANNED);
173             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_REMOVE) && (type == CHANGED)) {
174                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(),
175                                     e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._CHANGED);
176             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_REMOVE) && (type == CANCELED)) {
177                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(),
178                                     e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._CANCELED);
179             }
180             
181         }
182         
183     }
184     
185 }
186
Popular Tags