KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdr.test;
21
22 import java.io.*;
23 import java.util.*;
24 import java.net.*;
25
26 import junit.extensions.*;
27 import junit.framework.*;
28
29 import org.netbeans.api.mdr.*;
30 import org.openide.util.Lookup;
31
32 import org.netbeans.mdr.util.*;
33 import org.netbeans.mdr.NBMDRepositoryImpl;
34 import org.netbeans.lib.jmi.xmi.*;
35 import org.netbeans.lib.jmi.mapping.*;
36
37 import org.netbeans.api.mdr.events.*;
38
39 import javax.jmi.reflect.*;
40 import javax.jmi.model.*;
41 import javax.jmi.xmi.*;
42
43 /**
44  * Abstract predecessor of MDR test cases handling set up and providing some "high level"
45  * functionality over repository (e.g. load model method).
46  */

47 public abstract class MDREventsTestCase extends TestCase /*implements MDRPreChangeListener, MDRChangeListener*/ {
48     
49     // "type" of event
50
public static final int PLANNED = 2;
51     public static final int CHANGED = 4;
52     public static final int CANCELED = 8;
53     
54     // queue to test right order of events
55
protected LinkedList eventsQueue = new LinkedList();
56     
57     // default repository, it is inited by setUp method
58
protected MDRepository repository = null;
59     // default model package (created on booting repository)
60
protected ModelPackage modelPackage = null;
61     
62     public MDREventsTestCase(String JavaDoc testName) {
63         super (testName);
64     }
65     
66     /**
67      * Instantiates Model package and loads MOF model to it.
68      *
69      * @param docName XMI file name (should reside in 'data' directory)
70      * @param pkgName name of Model package extent to be instantiated
71      *
72      * @return instantiated model package
73      */

74     public ModelPackage loadMOFModel (String JavaDoc docName, String JavaDoc pkgName) {
75         
76         ModelPackage pkg = null;
77         
78         Lookup lookup = Lookup.getDefault ();
79         XmiReader reader = (XmiReader) lookup.lookup (XmiReader.class);
80         URL url = this.getClass ().getResource ("data" + File.separator + docName);
81         if (url == null) {
82             fail ("Resource not found.");
83         }
84         
85         try {
86             pkg = (ModelPackage) repository.createExtent(pkgName);
87         } catch (CreationFailedException cfe) {
88             fail ("Package instantiation failed: " + pkgName);
89         }
90         
91         try {
92             repository.beginTrans (true);
93             reader.read (url.toExternalForm(), pkg);
94         } catch (Exception JavaDoc e) {
95             fail (e.getMessage ());
96         } finally {
97             repository.endTrans ();
98         }
99         return pkg;
100         
101     }
102     
103     public MofPackage findMofPackage (ModelPackage modelPackage, String JavaDoc name) {
104         MofPackageClass proxy = modelPackage.getMofPackage ();
105         Iterator iter = proxy.refAllOfType ().iterator ();
106         MofPackage thePackage = null;
107         while (iter.hasNext ()) {
108             MofPackage pkg = (MofPackage) iter.next ();
109             if (pkg.getName ().equals (name)) {
110                 thePackage = pkg;
111                 break;
112             } // if
113
} // while
114
if (thePackage == null)
115             fail ("Cannot find package " + name);
116         return thePackage;
117     }
118     
119     /**
120      * Creates extent according to given MofPackage and name.
121      */

122     public RefPackage createExtent (MofPackage pkg, String JavaDoc name) {
123         try {
124             return repository.createExtent (name, pkg);
125         } catch (CreationFailedException e) {
126         }
127         fail ("Package instantiation failed: " + name);
128         return null;
129     }
130         
131     /**
132      * Returns extent in repository given by its name.
133      */

134     public ModelPackage getExtent (String JavaDoc name) {
135         ModelPackage vmp = null;
136         try {
137             vmp = (ModelPackage) repository.getExtent (name);
138         } catch (Exception JavaDoc e) {
139             fail ("Model not loaded, name not resolved.");
140         }
141         return vmp;
142     }
143     
144     public RefPackage createMOFExtent(String JavaDoc extentName) {
145         try {
146             return repository.createExtent(extentName);
147         } catch (CreationFailedException cfe) {
148             cfe.printStackTrace();
149         }
150         return null;
151     }
152     
153     // -------------------------------------------------------------------------
154

155     protected void setUp() {
156         // properties will be set only if running outside of IDE
157
if (System.getProperty("org.openide.version") == null) {
158             System.setProperty("org.netbeans.mdr.persistence.Dir", System.getProperty("work.dir") + "/TestEventsRepository_" + System.currentTimeMillis());
159             System.setProperty("org.netbeans.mdr.storagemodel.StorageFactoryClassName",
160                 "org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeFactory");
161             System.setProperty("org.netbeans.lib.jmi.Logger.fileName", "");
162         }
163         
164         repository = MDRManager.getDefault().getDefaultRepository();
165         if (repository == null) {
166             fail("Repository not found!");
167         } else {
168             // start up the repository
169
repository.getExtentNames();
170         }
171         
172     }
173     
174     protected void tearDown() {
175         
176     }
177     
178     // -------------------------------------------------------------------------
179
// MDRChangeListener, MDRPreChangeListener
180
// -------------------------------------------------------------------------
181
/*
182     public void change(MDRChangeEvent e) {
183         assertEquals("Wrong order of events!", eventsQueue.getFirst(), e);
184         eventsQueue.removeFirst();
185         processEvent(e, CHANGED);
186     }
187     
188     public void changeCancelled(MDRChangeEvent e) {
189         assertEquals("Wrong order of events!", eventsQueue.getFirst(), e);
190         eventsQueue.removeFirst();
191         processEvent(e, CANCELED);
192     }
193     
194     public void plannedChange(MDRChangeEvent e) {
195         eventsQueue.add(e);
196         processEvent(e, PLANNED);
197     }
198     */

199     /*
200     protected void processEvent(MDRChangeEvent e, int type, int eventType) throws IOException {
201         
202         if (e.isOfType(InstanceEvent.EVENTMASK_INSTANCE) && (eventType == InstanceEvent.EVENTMASK_INSTANCE)) { // InstanceEvent
203             
204             // EVENT_INSTANCE_CREATE
205             if (e.isOfType(InstanceEvent.EVENT_INSTANCE_CREATE) && (type == PLANNED)) {
206                 log("EVENT_INSTANCE_CREATE - PLANNED");
207             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_CREATE) && (type == CHANGED)) {
208                 log("EVENT_INSTANCE_CREATE - CHANGED: " +
209                     ((ModelElement) ((InstanceEvent) e).getInstance().refMetaObject()).getName());
210             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_CREATE) && (type == CANCELED)) {
211                 log("EVENT_INSTANCE_CREATE - CANCELED");
212             }
213             // EVENT_INSTANCE_DELETE
214             else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_DELETE) && (type == PLANNED)) {
215                 log("EVENT_INSTANCE_DELETE - PLANNED: " +
216                     ((ModelElement) ((InstanceEvent) e).getInstance().refMetaObject()).getName());
217             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_DELETE) && (type == CHANGED)) {
218                 log("EVENT_INSTANCE_DELETE - CHANGED");
219             } else if (e.isOfType(InstanceEvent.EVENT_INSTANCE_DELETE) && (type == CANCELED)) {
220                 log("EVENT_INSTANCE_DELETE - CANCELED: " +
221                     ((ModelElement) ((InstanceEvent) e).getInstance().refMetaObject()).getName());
222             }
223             
224         } else if (e.isOfType(ExtentEvent.EVENTMASK_EXTENT) && (eventType == ExtentEvent.EVENTMASK_EXTENT)) { // ExtentEvent
225             
226             // EVENT_EXTENT_CREATE
227             if (e.isOfType(ExtentEvent.EVENT_EXTENT_CREATE) && (type == PLANNED)) {
228                 ref("EVENT_EXTENT_CREATE - PLANNED: " + ((ExtentEvent) e).getExtentName());
229             } else if (e.isOfType(ExtentEvent.EVENT_EXTENT_CREATE) && (type == CHANGED)) {
230                 ref("EVENT_EXTENT_CREATE - CHANGED: " + ((ExtentEvent) e).getExtentName());
231             } else if (e.isOfType(ExtentEvent.EVENT_EXTENT_CREATE) && (type == CANCELED)) {
232                 ref("EVENT_EXTENT_CREATE - CANCELED: " + ((ExtentEvent) e).getExtentName());
233             }
234             // EVENT_EXTENT_DELETE
235             else if (e.isOfType(ExtentEvent.EVENT_EXTENT_DELETE) && (type == PLANNED)) {
236                 log("EVENT_EXTENT_DELETE - PLANNED: " + ((ExtentEvent) e).getExtentName());
237             } else if (e.isOfType(ExtentEvent.EVENT_EXTENT_DELETE) && (type == CHANGED)) {
238                 log("EVENT_EXTENT_DELETE - CHANGED: " + ((ExtentEvent) e).getExtentName());
239             } else if (e.isOfType(ExtentEvent.EVENT_EXTENT_DELETE) && (type == CANCELED)) {
240                 log("EVENT_EXTENT_DELETE - CANCELED: " + ((ExtentEvent) e).getExtentName());
241             }
242             
243         } else if ((e.isOfType(AttributeEvent.EVENTMASK_ATTRIBUTE) || // AttributeEvent
244                     e.isOfType(AttributeEvent.EVENTMASK_CLASSATTR)) &&
245                     
246                    ((eventType == AttributeEvent.EVENTMASK_ATTRIBUTE) ||
247                     (eventType == AttributeEvent.EVENTMASK_CLASSATTR))) {
248             
249             // EVENT_ATTRIBUTE_ADD
250             if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_ADD) && (type == PLANNED)) {
251                 log("EVENT_ATTRIBUTE_ADD - PLANNED");
252             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_ADD) && (type == CHANGED)) {
253                 log("EVENT_ATTRIBUTE_ADD - CHANGED: " + ((AttributeEvent) e).getAttributeName());
254             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_ADD) && (type == CANCELED)) {
255                 log("EVENT_ATTRIBUTE_ADD - CANCELED");
256             }
257             // EVENT_ATTRIBUTE_SET
258             else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_SET) && (type == PLANNED)) {
259                 log("EVENT_ATTRIBUTE_SET - PLANNED");
260             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_SET) && (type == CHANGED)) {
261                 log("EVENT_ATTRIBUTE_SET - CHANGED: " + ((AttributeEvent) e).getAttributeName());
262             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_SET) && (type == CANCELED)) {
263                 log("EVENT_ATTRIBUTE_SET - CANCELED");
264             }
265             // EVENT_ATTRIBUTE_REMOVE
266             else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_REMOVE) && (type == PLANNED)) {
267                 log("EVENT_ATTRIBUTE_REMOVE - PLANNED: " + ((AttributeEvent) e).getAttributeName());
268             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_REMOVE) && (type == CHANGED)) {
269                 log("EVENT_ATTRIBUTE_REMOVE - CHANGED");
270             } else if (e.isOfType(AttributeEvent.EVENT_ATTRIBUTE_REMOVE) && (type == CANCELED)) {
271                 log("EVENT_ATTRIBUTE_REMOVE - CANCELED");
272             }
273                        
274         } else if (e.isOfType(AssociationEvent.EVENTMASK_ASSOCIATION) && (eventType == AssociationEvent.EVENTMASK_ASSOCIATION)) { // AssociationEvent
275             
276             // EVENT_ASSOCIATION_ADD
277             if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_ADD) && (type == PLANNED)) {
278                 log("EVENT_ASSOCIATION_ADD - PLANNED");
279             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_ADD) && (type == CHANGED)) {
280                 log("EVENT_ASSOCIATION_ADD - CHANGED: " + ((AssociationEvent) e).getEndName());
281             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_ADD) && (type == CANCELED)) {
282                 log("EVENT_ASSOCIATION_ADD - CANCELED");
283             }
284             // EVENT_ASSOCIATION_SET
285             else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_SET) && (type == PLANNED)) {
286                 log("EVENT_ASSOCIATION_SET - PLANNED");
287             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_SET) && (type == CHANGED)) {
288                 log("EVENT_ASSOCIATION_SET - CHANGED: " + ((AssociationEvent) e).getEndName());
289             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_SET) && (type == CANCELED)) {
290                 log("EVENT_ASSOCIATION_SET - CANCELED");
291             }
292             // EVENT_ASSOCIATION_REMOVE
293             else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_REMOVE) && (type == PLANNED)) {
294                 log("EVENT_ASSOCIATION_REMOVE - PLANNED: " + ((AssociationEvent) e).getEndName());
295             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_REMOVE) && (type == CHANGED)) {
296                 log("EVENT_ASSOCIATION_REMOVE - CHANGED");
297             } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_REMOVE) && (type == CANCELED)) {
298                 log("EVENT_ASSOCIATION_REMOVE - CANCELED");
299             }
300             
301         }
302         
303     }*/

304     
305     protected void log(String JavaDoc message) {
306         getLog().println(message);
307     }
308     
309     protected PrintStream getLog() {
310         return System.err;
311     }
312 }
313
Popular Tags