KickJava   Java API By Example, From Geeks To Geeks.

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


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 MDRTestAttributePreEvents 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 MDRTestAttributePreEvents(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 testSetAttribute() {
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                 newRefObj = generator.generateInstance(metaClass);
68                 eventsList.addPattern2(metaClass.getName(), EventsList._PLANNED | EventsList._SET | EventsList._CHANGED, System.currentTimeMillis());
69                 boolean ws = generator.setAttribute(newRefObj, null, true);
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                 if (ws) {
76                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._PLANNED | EventsList._SET | EventsList._CHANGED));
77                 }
78                 eventsList.clear();
79             } else {
80                 log("Abstract class: " + metaClass.getName());
81             }
82         }
83         
84     }
85     
86     public void testSetAttributeCL() {
87         
88         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
89         
90         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
91                 
92         MDRAttributePreListener listener = new MDRAttributePreListener("test");
93         listener.setRef(eventsList);
94         
95         RefObject newRefObj = null;
96         Collection JavaDoc allMOFClasses = testMOFPackage.refAllClasses();
97         Iterator JavaDoc iterClasses = allMOFClasses.iterator();
98         while (iterClasses.hasNext()) {
99             RefClass clsProxy = (RefClass) iterClasses.next();
100             MofClass metaClass = (MofClass) clsProxy.refMetaObject();
101             if (!metaClass.isAbstract()) {
102                 log("Creating instance for class: " + metaClass.getName());
103                 newRefObj = generator.generateInstance(metaClass);
104                 // listening on class instance and repository too
105
((MDRChangeSource) newRefObj).addListener(listener);
106                 eventsList.addPattern2(metaClass.getName(), EventsList._PLANNED | EventsList._SET | EventsList._CHANGED, System.currentTimeMillis());
107                 boolean ws = generator.setAttribute(newRefObj, null, true);
108                 // !@#$%^& wait until event arives
109
for (int i = 0; i < 20; i++) {
110                     try { Thread.currentThread().sleep(100); }
111                         catch (InterruptedException JavaDoc ie) { }
112                 }
113                 if (ws) {
114                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._PLANNED | EventsList._SET | EventsList._CHANGED));
115                 }
116                 eventsList.clear();
117             } else {
118                 log("Abstract class: " + metaClass.getName());
119             }
120         }
121         
122     }
123     
124     public void testAddRemoveAttribute() {
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                 newRefObj = generator.generateInstance(metaClass);
139                 eventsList.addPattern2(metaClass.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED, System.currentTimeMillis());
140                 eventsList.addPattern2(metaClass.getName(), EventsList._PLANNED | EventsList._REMOVE | EventsList._CHANGED, System.currentTimeMillis());
141                 boolean ws = generator.addAttribute(newRefObj, true, null, true);
142                 // !@#$%^& wait until event arives
143
for (int i = 0; i < 20; i++) {
144                     try { Thread.currentThread().sleep(100); }
145                         catch (InterruptedException JavaDoc ie) { }
146                 }
147                 if (ws) {
148                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED));
149                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._PLANNED | EventsList._REMOVE | EventsList._CHANGED));
150                 }
151                 eventsList.clear();
152             } else {
153                 log("Abstract class: " + metaClass.getName());
154             }
155         }
156         
157     }
158     
159     public void testAddRemoveAttributeCL() {
160         
161         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
162         
163         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
164         
165         MDRAttributePreListener listener = new MDRAttributePreListener("test");
166         listener.setRef(eventsList);
167         
168         RefObject newRefObj = null;
169         Collection JavaDoc allMOFClasses = testMOFPackage.refAllClasses();
170         Iterator JavaDoc iterClasses = allMOFClasses.iterator();
171         while (iterClasses.hasNext()) {
172             RefClass clsProxy = (RefClass) iterClasses.next();
173             MofClass metaClass = (MofClass) clsProxy.refMetaObject();
174             if (!metaClass.isAbstract()) {
175                 log("Creating instance for class: " + metaClass.getName());
176                 newRefObj = generator.generateInstance(metaClass);
177                 ((MDRChangeSource) newRefObj).addListener(listener);
178                 eventsList.addPattern2(metaClass.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED, System.currentTimeMillis());
179                 eventsList.addPattern2(metaClass.getName(), EventsList._PLANNED | EventsList._REMOVE | EventsList._CHANGED, System.currentTimeMillis());
180                 boolean ws = generator.addAttribute(newRefObj, true, null, true);
181                 // !@#$%^& wait until event arives
182
for (int i = 0; i < 20; i++) {
183                     try { Thread.currentThread().sleep(100); }
184                         catch (InterruptedException JavaDoc ie) { }
185                 }
186                 if (ws) {
187                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED));
188                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._PLANNED | EventsList._REMOVE | EventsList._CHANGED));
189                 }
190                 eventsList.clear();
191             } else {
192                 log("Abstract class: " + metaClass.getName());
193             }
194         }
195
196     }
197     
198     // -------------------------------------------------------------------------
199

200     public void change(MDRChangeEvent e) {
201         assertEquals("Wrong order of events!", eventsQueue.getFirst(), e);
202         eventsQueue.removeFirst();
203         processEvent(e, CHANGED);
204     }
205     
206     public void changeCancelled(MDRChangeEvent e) {
207         assertEquals("Wrong order of events!", eventsQueue.getFirst(), e);
208         eventsQueue.removeFirst();
209         processEvent(e, CANCELED);
210     }
211     
212     public void plannedChange(MDRChangeEvent e) {
213         eventsQueue.add(e);
214         processEvent(e, PLANNED);
215     }
216     
217     private void processEvent(MDRChangeEvent e, int type) {
218         
219         if (e.isOfType(AttributeEvent.EVENTMASK_ATTRIBUTE) || // AttributeEvent
220
e.isOfType(AttributeEvent.EVENTMASK_CLASSATTR)) {
221             
222             // EVENT_ATTRIBUTE_ADD
223
if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_ADD) && (type == PLANNED)) {
224                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
225                                     e, System.currentTimeMillis(), EventsList._ADD | EventsList._PLANNED);
226             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_ADD) && (type == CHANGED)) {
227                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
228                                     e, System.currentTimeMillis(), EventsList._ADD | EventsList._CHANGED);
229             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_ADD) && (type == CANCELED)) {
230                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
231                                     e, System.currentTimeMillis(), EventsList._ADD | EventsList._CANCELED);
232             }
233             // EVENT_ATTRIBUTE_SET
234
else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_SET) && (type == PLANNED)) {
235                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
236                                     e, System.currentTimeMillis(), EventsList._SET | EventsList._PLANNED);
237             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_SET) && (type == CHANGED)) {
238                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
239                                     e, System.currentTimeMillis(), EventsList._SET | EventsList._CHANGED);
240             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_SET) && (type == CANCELED)) {
241                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
242                                     e, System.currentTimeMillis(), EventsList._SET | EventsList._CANCELED);
243             }
244             // EVENT_ATTRIBUTE_REMOVE
245
else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_REMOVE) && (type == PLANNED)) {
246                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
247                                     e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._PLANNED);
248             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_REMOVE) && (type == CHANGED)) {
249                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
250                                     e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._CHANGED);
251             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_REMOVE) && (type == CANCELED)) {
252                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
253                                     e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._CANCELED);
254             }
255             
256         }
257         
258     }
259     
260 }
261
Popular Tags