KickJava   Java API By Example, From Geeks To Geeks.

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


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 MDRTestAttributeEvents 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 MDRTestAttributeEvents(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 testAddRemoveAttribute() {
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._ADD | EventsList._CHANGED, System.currentTimeMillis());
69                 eventsList.addPattern2(metaClass.getName(), EventsList._REMOVE | EventsList._CHANGED, System.currentTimeMillis());
70                 boolean ws = generator.addAttribute(newRefObj, true, null, false);
71                 // !@#$%^& wait until event arives
72
for (int i = 0; i < 20; i++) {
73                     try { Thread.currentThread().sleep(100); }
74                         catch (InterruptedException JavaDoc ie) { }
75                 }
76                 if (ws) {
77                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._ADD | EventsList._CHANGED));
78                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._REMOVE | EventsList._CHANGED));
79                 }
80                 eventsList.clear();
81             } else {
82                 log("Abstract class: " + metaClass.getName());
83             }
84         }
85         
86     }
87     
88     public void testAddRemoveAttributeCL() {
89         
90         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
91
92         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
93         
94         MDRAttributeListener listener = new MDRAttributeListener();
95         listener.setRef (eventsList);
96         
97         RefObject newRefObj = null;
98         Collection JavaDoc allMOFClasses = testMOFPackage.refAllClasses();
99         Iterator JavaDoc iterClasses = allMOFClasses.iterator();
100         while (iterClasses.hasNext()) {
101             RefClass clsProxy = (RefClass) iterClasses.next();
102             MofClass metaClass = (MofClass) clsProxy.refMetaObject();
103             if (!metaClass.isAbstract()) {
104                 log("Creating instance for class: " + metaClass.getName());
105                 newRefObj = generator.generateInstance(metaClass);
106                 ((MDRChangeSource) newRefObj).addListener(listener);
107                 eventsList.addPattern2(metaClass.getName(), EventsList._ADD | EventsList._CHANGED, System.currentTimeMillis());
108                 eventsList.addPattern2(metaClass.getName(), EventsList._REMOVE | EventsList._CHANGED, System.currentTimeMillis());
109                 boolean ws = generator.addAttribute(newRefObj, true, null, false);
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                 if (ws) {
116                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._ADD | EventsList._CHANGED));
117                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._REMOVE | EventsList._CHANGED));
118                 }
119                 eventsList.clear();
120             } else {
121                 log("Abstract class: " + metaClass.getName());
122             }
123         }
124         
125     }
126     
127     public void testSetAttribute() {
128         
129         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
130
131         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
132         
133         RefObject newRefObj = null;
134         Collection JavaDoc allMOFClasses = testMOFPackage.refAllClasses();
135         Iterator JavaDoc iterClasses = allMOFClasses.iterator();
136         while (iterClasses.hasNext()) {
137             RefClass clsProxy = (RefClass) iterClasses.next();
138             MofClass metaClass = (MofClass) clsProxy.refMetaObject();
139             if (!metaClass.isAbstract()) {
140                 log("Creating instance for class: " + metaClass.getName());
141                 newRefObj = generator.generateInstance(metaClass);
142                 eventsList.addPattern2(metaClass.getName(), EventsList._SET | EventsList._CHANGED, System.currentTimeMillis());
143                 boolean ws = generator.setAttribute(newRefObj, null, false);
144                 // !@#$%^& wait until event arives
145
for (int i = 0; i < 20; i++) {
146                     try { Thread.currentThread().sleep(100); }
147                         catch (InterruptedException JavaDoc ie) { }
148                 }
149                 if (ws) {
150                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._SET | EventsList._CHANGED));
151                 }
152                 eventsList.clear();
153             } else {
154                 log("Abstract class: " + metaClass.getName());
155             }
156         }
157         
158     }
159     
160     public void testSetAttributeCL() {
161         
162         RefPackage testMOFPackage = createMOFExtent(PKG_NAME);
163
164         EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME));
165         
166         MDRAttributeListener listener = new MDRAttributeListener();
167         listener.setRef(eventsList);
168         
169         RefObject newRefObj = null;
170         Collection JavaDoc allMOFClasses = testMOFPackage.refAllClasses();
171         Iterator JavaDoc iterClasses = allMOFClasses.iterator();
172         while (iterClasses.hasNext()) {
173             RefClass clsProxy = (RefClass) iterClasses.next();
174             MofClass metaClass = (MofClass) clsProxy.refMetaObject();
175             if (!metaClass.isAbstract()) {
176                 log("Creating instance for class: " + metaClass.getName());
177                 newRefObj = generator.generateInstance(metaClass);
178                 // listening on class instance and repository too
179
((MDRChangeSource) newRefObj).addListener(listener);
180                 eventsList.addPattern2(metaClass.getName(), EventsList._SET | EventsList._CHANGED, System.currentTimeMillis());
181                 boolean ws = generator.setAttribute(newRefObj, null, false);
182                 // !@#$%^& wait until event arives
183
for (int i = 0; i < 20; i++) {
184                     try { Thread.currentThread().sleep(100); }
185                         catch (InterruptedException JavaDoc ie) { }
186                 }
187                 if (ws) {
188                     assertTrue(eventsList.checkEvents(metaClass.getName(), EventsList._SET | 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         processEvent(e, CHANGED);
202     }
203     
204     private void processEvent(MDRChangeEvent e, int type) {
205         
206         if (e.isOfType(AttributeEvent.EVENTMASK_ATTRIBUTE) || // AttributeEvent
207
e.isOfType(AttributeEvent.EVENTMASK_CLASSATTR)) {
208             
209             // EVENT_ATTRIBUTE_ADD
210
if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_ADD) && (type == PLANNED)) {
211                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
212                                     e, System.currentTimeMillis(), EventsList._ADD | EventsList._PLANNED);
213             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_ADD) && (type == CHANGED)) {
214                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
215                                     e, System.currentTimeMillis(), EventsList._ADD | EventsList._CHANGED);
216             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_ADD) && (type == CANCELED)) {
217                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
218                                     e, System.currentTimeMillis(), EventsList._ADD | EventsList._CANCELED);
219             }
220             // EVENT_ATTRIBUTE_SET
221
else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_SET) && (type == PLANNED)) {
222                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
223                                     e, System.currentTimeMillis(), EventsList._SET | EventsList._PLANNED);
224             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_SET) && (type == CHANGED)) {
225                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
226                                     e, System.currentTimeMillis(), EventsList._SET | EventsList._CHANGED);
227             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_SET) && (type == CANCELED)) {
228                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
229                                     e, System.currentTimeMillis(), EventsList._SET | EventsList._CANCELED);
230             }
231             // EVENT_ATTRIBUTE_REMOVE
232
else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_REMOVE) && (type == PLANNED)) {
233                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
234                                     e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._PLANNED);
235             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_REMOVE) && (type == CHANGED)) {
236                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
237                                     e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._CHANGED);
238             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_REMOVE) && (type == CANCELED)) {
239                 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AttributeEvent) e).getSource()).refMetaObject()).getName(),
240                                     e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._CANCELED);
241             }
242             
243         }
244         
245     }
246     
247 }
248
Popular Tags