KickJava   Java API By Example, From Geeks To Geeks.

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


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 MDRTestInstanceEvents extends MDREventsTestCase implements MDRChangeListener {
32     
33     private static final String JavaDoc PKG_NAME = "TEST_MOF";
34     
35     private EventsList eventsList = new EventsList();
36     
37     public MDRTestInstanceEvents(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

54     public void testCreateInstance() {
55         
56         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
57         
58         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
59         
60         RefObject newRefObj = null;
61         Collection JavaDoc allMOFClasses = testMOFPackage.refAllClasses();
62         Iterator JavaDoc iterClasses = allMOFClasses.iterator();
63         while (iterClasses.hasNext()) {
64             RefClass clsProxy = (RefClass) iterClasses.next();
65             MofClass metaClass = (MofClass) clsProxy.refMetaObject();
66             if (!metaClass.isAbstract()) {
67                 log("Creating instance for class: " + metaClass.getName());
68                 eventsList.addPattern2(metaClass.getName(), EventsList._CREATE | EventsList._CHANGED, System.currentTimeMillis());
69                 newRefObj = generator.generateInstance(metaClass);
70                 // !@#$%^& wait until event arives
71
for (int i = 0; i < 20; i++) {
72                     try { Thread.currentThread().sleep(100); }
73                         catch (InterruptedException JavaDoc ie) { }
74                 }
75                 assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._CREATE | EventsList._CHANGED));
76                 eventsList.clear();
77             } else {
78                 log("Abstract class: " + metaClass.getName());
79             }
80         }
81         
82     }
83     
84     public void testDeleteInstance() {
85         
86         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
87         
88         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
89         
90         RefObject newRefObj = null;
91         Collection JavaDoc allMOFClasses = testMOFPackage.refAllClasses();
92         Iterator JavaDoc iterClasses = allMOFClasses.iterator();
93         while (iterClasses.hasNext()) {
94             RefClass clsProxy = (RefClass) iterClasses.next();
95             MofClass metaClass = (MofClass) clsProxy.refMetaObject();
96             if (!metaClass.isAbstract()) {
97                 log("Creating instance for class: " + metaClass.getName());
98                 eventsList.addPattern2(metaClass.getName(), EventsList._CREATE | EventsList._CHANGED, System.currentTimeMillis());
99                 newRefObj = generator.generateInstance(metaClass);
100                 // !@#$%^& wait until event arives
101
for (int i = 0; i < 20; i++) {
102                     try { Thread.currentThread().sleep(100); }
103                         catch (InterruptedException JavaDoc ie) { }
104                 }
105                 assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._CREATE | EventsList._CHANGED));
106                 eventsList.clear();
107
108                 log("Deleting instance of class: " + metaClass.getName());
109                 eventsList.addPattern2("DELETE", EventsList._DELETE | EventsList._CHANGED, System.currentTimeMillis());
110                 newRefObj.refDelete();
111                 // !@#$%^& wait until event arives
112
for (int i = 0; i < 20; i++) {
113                     try { Thread.currentThread().sleep(100); }
114                         catch (InterruptedException JavaDoc ie) { }
115                 }
116                 assertTrue(eventsList.checkEvents("DELETE", EventsList._DELETE | EventsList._CHANGED));
117                 eventsList.clear();
118             } else {
119                 log("Abstract class: " + metaClass.getName());
120             }
121         }
122         
123     }
124     
125     // -------------------------------------------------------------------------
126

127     public void change(MDRChangeEvent e) {
128         processEvent(e, CHANGED);
129     }
130     
131     private void processEvent(MDRChangeEvent e, int type) {
132         
133         if (e.isOfType(InstanceEvent.EVENTMASK_INSTANCE)) { // InstanceEvent
134

135             // EVENT_INSTANCE_CREATE
136
if (e.isOfType(InstanceEvent.EVENT_INSTANCE_CREATE) && (type == PLANNED)) {
137                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((InstanceEvent) e).getSource()).refMetaObject()).getName(),
138                                     e, System.currentTimeMillis(), EventsList._CREATE | EventsList._PLANNED);
139             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_CREATE) && (type == CHANGED)) {
140                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((InstanceEvent) e).getSource()).refMetaObject()).getName(),
141                                     e, System.currentTimeMillis(), EventsList._CREATE | EventsList._CHANGED);
142             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_CREATE) && (type == CANCELED)) {
143                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((InstanceEvent) e).getSource()).refMetaObject()).getName(),
144                                     e, System.currentTimeMillis(), EventsList._CREATE | EventsList._CANCELED);
145             }
146             // EVENT_INSTANCE_DELETE
147
else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_DELETE) && (type == PLANNED)) {
148                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((InstanceEvent) e).getSource()).refMetaObject()).getName(),
149                                     e, System.currentTimeMillis(), EventsList._DELETE | EventsList._PLANNED);
150             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_DELETE) && (type == CHANGED)) {
151                 eventsList.addEvent("DELETE", e, System.currentTimeMillis(), EventsList._DELETE | EventsList._CHANGED);
152             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_DELETE) && (type == CANCELED)) {
153                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((InstanceEvent) e).getSource()).refMetaObject()).getName(),
154                                     e, System.currentTimeMillis(), EventsList._DELETE | EventsList._CANCELED);
155             }
156             
157         }
158         
159     }
160     
161 }
162
Popular Tags