1 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 44 public abstract class MDRTestCase extends TestCase { 45 46 protected MDRepository repository = null; 48 protected ModelPackage modelPackage = null; 50 51 public MDRTestCase(String testName) { 52 super (testName); 53 } 54 55 63 public ModelPackage loadMOFModel (String docName, String 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 e) { 76 fail (e.getMessage ()); 77 } finally { 78 repository.endTrans (); 79 } 80 return pkg; 81 } 82 83 91 public MofPackage findMofPackage (ModelPackage modelPackage, String 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 (thePackage == null) 103 fail ("Cannot find package " + name); 104 return thePackage; 105 } 106 107 110 public RefPackage createExtent (MofPackage pkg, String 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 127 public ModelPackage getExtent (String name) { 128 ModelPackage vmp = null; 129 try { 130 vmp = (ModelPackage) repository.getExtent (name); 131 } catch (Exception e) { 132 fail ("Model not loaded, name not resolved."); 133 } 134 return vmp; 135 } 136 137 protected void setUp() { 138 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 fileName) { 154 return new File(System.getProperty("work.dir") + '/' + fileName); 155 } 156 157 protected void tearDown() { 158 } 159 160 protected void log(String message) { 161 System.err.println(message); 162 } 163 } 164 | Popular Tags |