KickJava   Java API By Example, From Geeks To Geeks.

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


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 MDRTestInstancePreEvents 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 MDRTestInstancePreEvents(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 testCreateInstance() {
54         
55         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
56
57         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
58         
59         RefObject newRefObj = null;
60         Collection JavaDoc allMOFClasses = testMOFPackage.refAllClasses();
61         Iterator JavaDoc iterClasses = allMOFClasses.iterator();
62         while (iterClasses.hasNext()) {
63             RefClass clsProxy = (RefClass) iterClasses.next();
64             MofClass metaClass = (MofClass) clsProxy.refMetaObject();
65             if (!metaClass.isAbstract()) {
66                 log("Creating instance for class: " + metaClass.getName());
67                 eventsList.addPattern2(metaClass.getName(), EventsList._PLANNED | EventsList._CHANGED | EventsList._CREATE, System.currentTimeMillis());
68                 newRefObj = generator.generateInstance(metaClass);
69                 // !@#$%^& wait until event arives
70
for (int i = 0; i < 20; i++) {
71                     try { Thread.currentThread().sleep(100); }
72                         catch (InterruptedException JavaDoc ie) { }
73                 }
74                 assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._PLANNED | EventsList._CHANGED | EventsList._CREATE));
75                 eventsList.clear();
76             } else {
77                 log("Abstract class: " + metaClass.getName());
78             }
79         }
80
81     }
82     
83     public void testDeleteInstance() {
84         
85         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
86
87         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
88         
89         RefObject newRefObj = null;
90         Collection JavaDoc allMOFClasses = testMOFPackage.refAllClasses();
91         Iterator JavaDoc iterClasses = allMOFClasses.iterator();
92         while (iterClasses.hasNext()) {
93             RefClass clsProxy = (RefClass) iterClasses.next();
94             MofClass metaClass = (MofClass) clsProxy.refMetaObject();
95             if (!metaClass.isAbstract()) {
96                 log("Creating instance for class: " + metaClass.getName());
97                 eventsList.addPattern2(metaClass.getName(), EventsList._PLANNED | EventsList._CHANGED | EventsList._CREATE, System.currentTimeMillis());
98                 newRefObj = generator.generateInstance(metaClass);
99                 // !@#$%^& wait until event arives
100
for (int i = 0; i < 20; i++) {
101                     try { Thread.currentThread().sleep(100); }
102                         catch (InterruptedException JavaDoc ie) { }
103                 }
104                 assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._PLANNED | EventsList._CHANGED | EventsList._CREATE));
105                 eventsList.clear();
106
107                 log("Deleting instance of class: " + metaClass.getName());
108                 eventsList.addPattern2("DELETE", EventsList._PLANNED | EventsList._CHANGED | EventsList._CREATE, System.currentTimeMillis());
109                 newRefObj.refDelete();
110                 // !@#$%^& wait until event arives
111
for (int i = 0; i < 20; i++) {
112                     try { Thread.currentThread().sleep(100); }
113                         catch (InterruptedException JavaDoc ie) { }
114                 }
115                 assertTrue(eventsList.checkEvents("DELETE", EventsList._PLANNED | EventsList._CHANGED | EventsList._CREATE));
116                 eventsList.clear();
117             } else {
118                 log("Abstract class: " + metaClass.getName());
119             }
120         }
121         
122     }
123     
124     public void testCreateInstanceCanceled() {
125         
126         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
127
128         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
129         
130         RefObject newRefObj = null;
131         Collection JavaDoc allMOFClasses = testMOFPackage.refAllClasses();
132         Iterator JavaDoc iterClasses = allMOFClasses.iterator();
133         while (iterClasses.hasNext()) {
134             RefClass clsProxy = (RefClass) iterClasses.next();
135             MofClass metaClass = (MofClass) clsProxy.refMetaObject();
136             if (!metaClass.isAbstract()) {
137                 log("Creating instance for class: " + metaClass.getName());
138                 try {
139                     eventsList.addPattern2(metaClass.getName(), EventsList._PLANNED | EventsList._CANCELED | EventsList._CREATE, System.currentTimeMillis());
140                     newRefObj = generator.generateWrongInstance(metaClass);
141                     // !@#$%^& wait until event arives
142
for (int i = 0; i < 20; i++) {
143                         try { Thread.currentThread().sleep(100); }
144                             catch (InterruptedException JavaDoc ie) { }
145                     }
146                 } catch (JmiException jmiexc) {
147                     jmiexc.printStackTrace(getLog());
148                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._PLANNED | EventsList._CANCELED | EventsList._CREATE));
149                     eventsList.clear();
150                 }
151             } else {
152                 log("Abstract class: " + metaClass.getName());
153             }
154         }
155         
156     }
157     
158     // -------------------------------------------------------------------------
159

160     public void change(MDRChangeEvent e) {
161         assertEquals("Wrong order of events!", eventsQueue.getFirst(), e);
162         eventsQueue.removeFirst();
163         processEvent(e, CHANGED);
164     }
165     
166     public void changeCancelled(MDRChangeEvent e) {
167         assertEquals("Wrong order of events!", eventsQueue.getFirst(), e);
168         eventsQueue.removeFirst();
169         processEvent(e, CANCELED);
170     }
171     
172     public void plannedChange(MDRChangeEvent e) {
173         eventsQueue.add(e);
174         processEvent(e, PLANNED);
175     }
176     
177     private void processEvent(MDRChangeEvent e, int type) {
178         
179         if (e.isOfType(InstanceEvent.EVENTMASK_INSTANCE)) { // InstanceEvent
180

181             // EVENT_INSTANCE_CREATE
182
if (e.isOfType(InstanceEvent.EVENT_INSTANCE_CREATE) && (type == PLANNED)) {
183                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((InstanceEvent) e).getSource()).refMetaObject()).getName(),
184                                     e, System.currentTimeMillis(), EventsList._CREATE | EventsList._PLANNED);
185             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_CREATE) && (type == CHANGED)) {
186                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((InstanceEvent) e).getSource()).refMetaObject()).getName(),
187                                     e, System.currentTimeMillis(), EventsList._CREATE | EventsList._CHANGED);
188             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_CREATE) && (type == CANCELED)) {
189                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((InstanceEvent) e).getSource()).refMetaObject()).getName(),
190                                     e, System.currentTimeMillis(), EventsList._CREATE | EventsList._CANCELED);
191             }
192             // EVENT_INSTANCE_DELETE
193
else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_DELETE) && (type == PLANNED)) {
194                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((InstanceEvent) e).getSource()).refMetaObject()).getName(),
195                                     e, System.currentTimeMillis(), EventsList._DELETE | EventsList._PLANNED);
196             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_DELETE) && (type == CHANGED)) {
197                 eventsList.addEvent("DELETE", e, System.currentTimeMillis(), EventsList._DELETE | EventsList._CHANGED);
198             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_DELETE) && (type == CANCELED)) {
199                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((InstanceEvent) e).getSource()).refMetaObject()).getName(),
200                                     e, System.currentTimeMillis(), EventsList._DELETE | EventsList._CANCELED);
201             }
202             
203         }
204         
205     }
206     
207 }
208
Popular Tags