KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.*;
11
12 import org.netbeans.mdr.handlers.BaseObjectHandler;
13 import org.netbeans.mdr.NBMDRepositoryImpl;
14
15 import org.netbeans.api.mdr.MDRManager;
16 import org.netbeans.api.mdr.MDRepository;
17 import org.netbeans.api.mdr.CreationFailedException;
18
19 import org.netbeans.api.mdr.events.*;
20
21 import javax.jmi.reflect.*;
22 import javax.jmi.model.*;
23
24 /**
25  *
26  * @author mk97936
27  */

28 public class MDRTestExtentPreEvents extends MDREventsTestCase implements MDRPreChangeListener {
29     
30     private static final String JavaDoc PKG_NAME_MOF = "MOF_EXTENT";
31     private static final String JavaDoc PKG_NAME_JAVA = "JAVA_EXTENT";
32     private static final String JavaDoc MDL_NAME_JAVA = "JAVA_MODEL";
33     private static final String JavaDoc PKG_NAME_TEXT = "TEXT_EXTENT";
34     private static final String JavaDoc MDL_NAME_TEXT = "TEXT_MODEL";
35     
36     private java.io.PrintStream JavaDoc goldenRef = null;
37     private EventsList eventsList = new EventsList();
38     
39     public MDRTestExtentPreEvents(java.lang.String JavaDoc testName) {
40         super(testName);
41     }
42     
43     protected void setUp() {
44         super.setUp();
45         ((MDRChangeSource) repository).addListener(this);
46     }
47     
48     protected void tearDown() {
49         super.tearDown();
50         ((MDRChangeSource) repository).removeListener(this);
51     }
52     
53     // -------------------------------------------------------------------------
54

55     // Instantiate MOF
56
public void testCreateMOFExtent() {
57         RefPackage mofExtent = null;
58         try {
59             eventsList.addPattern2(PKG_NAME_MOF, EventsList._CREATE | EventsList._PLANNED | EventsList._CHANGED, System.currentTimeMillis());
60             mofExtent = repository.createExtent(PKG_NAME_MOF);
61             for (int i = 0; i < 10; i++) {
62                try { Thread.currentThread().sleep(100); }
63                     catch (InterruptedException JavaDoc ie) { }
64             }
65             assertTrue(eventsList.checkEvents(PKG_NAME_MOF, EventsList._CREATE | EventsList._PLANNED | EventsList._CHANGED));
66             eventsList.clear();
67         } catch (CreationFailedException cfe) {
68             fail(cfe.toString());
69         }
70         
71     }
72     
73     // Load Java model and instantiate it
74
public void testCreateJavaExtent() {
75         
76         RefPackage javaExtent = null;
77         eventsList.addPattern2(MDL_NAME_JAVA, EventsList._CREATE | EventsList._PLANNED | EventsList._CHANGED, System.currentTimeMillis());
78         ModelPackage javaMOFModel = loadMOFModel("Java.xml", MDL_NAME_JAVA);
79         assertTrue(eventsList.checkEvents(MDL_NAME_JAVA, EventsList._CREATE | EventsList._PLANNED | EventsList._CHANGED));
80         eventsList.clear();
81         try {
82             eventsList.addPattern2(PKG_NAME_JAVA, EventsList._CREATE | EventsList._PLANNED | EventsList._CHANGED, System.currentTimeMillis());
83             javaExtent = repository.createExtent(PKG_NAME_JAVA, findMofPackage(javaMOFModel, "Core"));
84             for (int i = 0; i < 10; i++) {
85                try { Thread.currentThread().sleep(100); }
86                     catch (InterruptedException JavaDoc ie) { }
87             }
88             assertTrue(eventsList.checkEvents(PKG_NAME_JAVA, EventsList._CREATE | EventsList._PLANNED | EventsList._CHANGED));
89             eventsList.clear();
90         } catch (CreationFailedException cfe) {
91             fail(cfe.toString());
92         }
93         
94     }
95     
96     // Load Text model and instantiate it - negative test
97
public void testCreateTextExtent() {
98         
99         RefPackage textExtent = null;
100         eventsList.addPattern2(MDL_NAME_TEXT, EventsList._CREATE | EventsList._PLANNED | EventsList._CHANGED, System.currentTimeMillis());
101         ModelPackage textMOFModel = loadMOFModel("TestText.xml", MDL_NAME_TEXT);
102         assertTrue(eventsList.checkEvents(MDL_NAME_TEXT, EventsList._CREATE | EventsList._PLANNED | EventsList._CHANGED));
103         eventsList.clear();
104         try {
105             eventsList.addPattern2(PKG_NAME_TEXT, EventsList._CREATE | EventsList._PLANNED | EventsList._CHANGED, System.currentTimeMillis());
106             textExtent = repository.createExtent(PKG_NAME_TEXT, findMofPackage(textMOFModel, "TestText"));
107         } catch (CreationFailedException cfe) {
108             // fail(cfe.toString()); // this must fail
109
assertTrue(eventsList.checkEvents(PKG_NAME_TEXT, EventsList._CREATE | EventsList._PLANNED | EventsList._CANCELED));
110             eventsList.clear();
111         }
112         
113     }
114     
115     public void testDeleteMOFExtent() {
116         
117         RefPackage mofExtent = repository.getExtent(PKG_NAME_MOF);
118         eventsList.addPattern2(PKG_NAME_MOF, EventsList._DELETE | EventsList._PLANNED | EventsList._CHANGED, System.currentTimeMillis());
119         mofExtent.refDelete();
120         for (int i = 0; i < 10; i++) {
121             try { Thread.currentThread().sleep(100); }
122                 catch (InterruptedException JavaDoc ie) { }
123         }
124         assertTrue(eventsList.checkEvents(PKG_NAME_MOF, EventsList._DELETE | EventsList._PLANNED | EventsList._CHANGED));
125         eventsList.clear();
126         
127     }
128     
129     public void testDeleteJavaExtent() {
130         
131         RefPackage javaExtent = repository.getExtent(PKG_NAME_JAVA);
132         eventsList.addPattern2(PKG_NAME_JAVA, EventsList._DELETE | EventsList._PLANNED | EventsList._CHANGED, System.currentTimeMillis());
133         javaExtent.refDelete();
134         for (int i = 0; i < 10; i++) {
135             try { Thread.currentThread().sleep(100); }
136                 catch (InterruptedException JavaDoc ie) { }
137         }
138         assertTrue(eventsList.checkEvents(PKG_NAME_JAVA, EventsList._DELETE | EventsList._PLANNED | EventsList._CHANGED));
139         eventsList.clear();
140         
141     }
142     
143     // -------------------------------------------------------------------------
144

145     public void change(MDRChangeEvent e) {
146         assertEquals("Wrong order of events!", eventsQueue.getFirst(), e);
147         eventsQueue.removeFirst();
148         processEvent(e, CHANGED);
149     }
150     
151     public void changeCancelled(MDRChangeEvent e) {
152         assertEquals("Wrong order of events!", eventsQueue.getFirst(), e);
153         eventsQueue.removeFirst();
154         processEvent(e, CANCELED);
155     }
156     
157     public void plannedChange(MDRChangeEvent e) {
158         eventsQueue.add(e);
159         processEvent(e, PLANNED);
160     }
161     
162     private void processEvent(MDRChangeEvent e, int type) {
163         
164         if (e.isOfType(ExtentEvent.EVENTMASK_EXTENT)) { // ExtentEvent
165

166             // EVENT_EXTENT_CREATE
167
if (e.isOfType(ExtentEvent.EVENT_EXTENT_CREATE) && (type == PLANNED)) {
168                 eventsList.addEvent(((ExtentEvent) e).getExtentName(), e, System.currentTimeMillis(),
169                                     EventsList._CREATE | EventsList._PLANNED);
170             } else if (e.isOfType(ExtentEvent.EVENT_EXTENT_CREATE) && (type == CHANGED)) {
171                 eventsList.addEvent(((ExtentEvent) e).getExtentName(), e, System.currentTimeMillis(),
172                                     EventsList._CREATE | EventsList._CHANGED);
173             } else if (e.isOfType(ExtentEvent.EVENT_EXTENT_CREATE) && (type == CANCELED)) {
174                 eventsList.addEvent(((ExtentEvent) e).getExtentName(), e, System.currentTimeMillis(),
175                                     EventsList._CREATE | EventsList._CANCELED);
176             }
177             // EVENT_EXTENT_DELETE
178
else if (e.isOfType(ExtentEvent.EVENT_EXTENT_DELETE) && (type == PLANNED)) {
179                 eventsList.addEvent(((ExtentEvent) e).getExtentName(), e, System.currentTimeMillis(),
180                                     EventsList._DELETE | EventsList._PLANNED);
181             } else if (e.isOfType(ExtentEvent.EVENT_EXTENT_DELETE) && (type == CHANGED)) {
182                 eventsList.addEvent(((ExtentEvent) e).getExtentName(), e, System.currentTimeMillis(),
183                                     EventsList._DELETE | EventsList._CHANGED);
184             } else if (e.isOfType(ExtentEvent.EVENT_EXTENT_DELETE) && (type == CANCELED)) {
185                 eventsList.addEvent(((ExtentEvent) e).getExtentName(), e, System.currentTimeMillis(),
186                                     EventsList._DELETE | EventsList._CANCELED);
187             }
188             
189         }
190         
191     }
192     
193 }
194
Popular Tags