KickJava   Java API By Example, From Geeks To Geeks.

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


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.mapping.*;
35
36 import javax.jmi.reflect.*;
37 import javax.jmi.model.*;
38 import javax.jmi.xmi.*;
39
40 /**
41  * Abstract predecessor of MDR test cases handling set up and providing some "high level"
42  * functionality over repository (e.g. load model method).
43  */

44 public abstract class MDRTestCase extends TestCase {
45     
46     // default repository, it is inited by setUp method
47
protected MDRepository repository = null;
48     // default model package (created on booting repository)
49
protected ModelPackage modelPackage = null;
50     
51     public MDRTestCase(String JavaDoc testName) {
52         super (testName);
53     }
54     
55     /**
56      * Instantiates Model package and loads MOF model to it.
57      *
58      * @param docName XMI file name (should reside in 'data' directory)
59      * @param pkgName name of Model package extent to be instantiated
60      *
61      * @return instantiated model package
62      */

63     public ModelPackage loadMOFModel (String JavaDoc docName, String JavaDoc pkgName) {
64         Lookup lookup = Lookup.getDefault ();
65         XmiReader reader = (XmiReader) lookup.lookup (XmiReader.class);
66         URL url = this.getClass ().getResource ("data//" + docName);
67         if (url == null)
68             fail ("Resource not found: " + "data//" + docName);
69         ModelPackage pkg = (ModelPackage) createExtent (
70             findMofPackage (modelPackage, "Model"), pkgName
71         );
72         try {
73             repository.beginTrans (true);
74             reader.read (url.toExternalForm(), pkg);
75         } catch (Exception JavaDoc e) {
76             fail (e.getMessage ());
77         } finally {
78             repository.endTrans ();
79         }
80         return pkg;
81     }
82
83     /**
84      * Finds a package (MofPackage instance) given by a name in ModelPackage extent.
85      *
86      * @param modelPackage ModelPackage where the queried instance should reside
87      * @param name of the MofPackage instance to be found
88      *
89      * @return the desired MofPackage instance
90      */

91     public MofPackage findMofPackage (ModelPackage modelPackage, String JavaDoc name) {
92         MofPackageClass proxy = modelPackage.getMofPackage ();
93         Iterator iter = proxy.refAllOfType ().iterator ();
94         MofPackage thePackage = null;
95         while (iter.hasNext ()) {
96             MofPackage pkg = (MofPackage) iter.next ();
97             if (pkg.getName ().equals (name)) {
98                 thePackage = pkg;
99                 break;
100             } // if
101
} // while
102
if (thePackage == null)
103             fail ("Cannot find package " + name);
104         return thePackage;
105     }
106  
107     /**
108      * Creates extent according to given MofPackage and name.
109      */

110     public RefPackage createExtent (MofPackage pkg, String JavaDoc name) {
111         RefPackage extent = getExtent(name);
112         if (extent != null) {
113             extent.refDelete();
114         }
115         try {
116             return repository.createExtent (name, pkg);
117         } catch (CreationFailedException e) {
118             e.printStackTrace ();
119         }
120         fail ("Package instantiation failed: " + name);
121         return null;
122     }
123         
124     /**
125      * Returns extent in repository given by its name.
126      */

127     public ModelPackage getExtent (String JavaDoc name) {
128         ModelPackage vmp = null;
129         try {
130             vmp = (ModelPackage) repository.getExtent (name);
131         } catch (Exception JavaDoc e) {
132             fail ("Model not loaded, name not resolved.");
133         }
134         return vmp;
135     }
136    
137     protected void setUp() {
138         // properties will be set only if running outside of IDE
139
if (System.getProperty("org.openide.version") == null) {
140             System.setProperty("org.netbeans.mdr.persistence.Dir", System.getProperty("work.dir") + "/test_repository_1");
141             System.setProperty("org.netbeans.mdr.storagemodel.StorageFactoryClassName",
142             "org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeFactory");
143         }
144         
145         repository = ((MDRManager) Lookup.getDefault().lookup(MDRManager.class)).getDefaultRepository();
146         if (repository == null)
147             fail ("Repository manager not found.");
148         modelPackage = (ModelPackage) repository.getExtent ("MOF");
149         if (modelPackage == null)
150             fail ("MOF package not found.");
151     }
152     
153     protected static File getFile(String JavaDoc fileName) {
154         return new File(System.getProperty("work.dir") + '/' + fileName);
155     }
156     
157     protected void tearDown() {
158     }
159     
160     protected void log(String JavaDoc message) {
161         System.err.println(message);
162     }
163 }
164
Popular Tags