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.xmi.*; 35 import org.netbeans.lib.jmi.mapping.*; 36 37 import org.xml.sax.*; 38 39 import javax.jmi.reflect.*; 40 import javax.jmi.model.*; 41 import javax.jmi.xmi.*; 42 43 46 public class MDRExportImportTest extends MDRTestCase { 47 48 public MDRExportImportTest (String testName) { 49 super (testName); 50 } 51 52 public static void main (String [] args) { 53 junit.textui.TestRunner.run (suite ()); 54 } 55 56 public static Test suite () { 57 TestSuite suite = new TestSuite (); 58 suite.addTestSuite (MDRExportImportTest.class); 59 60 TestSetup setup = new TestSetup (suite) { 61 public void setUp () { 62 } 63 public void tearDown () { 64 } 65 }; 66 return setup; 67 } 68 69 71 public void test () { 72 RefPackage pkg = instantiateModel ("Java.xml", "Core", "Core"); 73 new RandomDataGenerator ().generate (pkg, 0, 5); 74 75 Lookup lookup = Lookup.getDefault (); 76 XmiReader reader = (XmiReader) lookup.lookup (XmiReader.class); 77 XmiWriter writer = (XmiWriter) lookup.lookup (XmiWriter.class); 78 79 File file = getFile ("temp.xml"); 80 try { 81 OutputStream os = new FileOutputStream (file); 82 repository.beginTrans (true); 83 writer.write (os, pkg, null); 84 os.flush (); 85 os.close (); 86 } catch (Exception e) { 87 fail (e.getMessage ()); 88 } finally { 89 repository.endTrans (); 90 } 91 try { 92 repository.beginTrans (true); 93 reader.read (file.toURL ().toExternalForm(), pkg); 94 } catch (Exception e) { 95 fail (e.getMessage ()); 96 } finally { 97 repository.endTrans (); 98 } 99 deleteModel (pkg); 100 } 101 102 106 public void testTextModel () { 107 readWrite ("Text.xml"); 108 } 109 110 public void testCollectionType () { 111 RefPackage pkg = instantiateModel ("Coll.xml", "Main", "Collections_test"); 112 RefPackage pkg2 = instantiateModel ("Coll.xml", "Main", "Collections_test_2"); 113 new RandomDataGenerator ().generate (pkg, 0, 5); 114 115 org.netbeans.api.xmi.XMIWriter writer = 116 org.netbeans.api.xmi.XMIWriterFactory.getDefault ().createXMIWriter (); 117 org.netbeans.api.xmi.XMIReader reader = 118 org.netbeans.api.xmi.XMIReaderFactory.getDefault ().createXMIReader (); 119 120 File file_1 = getFile ("out.xml"); 121 File file_2 = getFile ("out_2.xml"); 122 123 try { 124 repository.beginTrans (false); 125 OutputStream os = new FileOutputStream (file_1); 126 writer.write (os, pkg, null); 127 } catch (Exception e) { 128 System.out.println(e); 129 e.printStackTrace (); 130 } finally { 131 repository.endTrans (false); 132 } 133 134 boolean failed = true; 135 try { 136 repository.beginTrans (true); 137 reader.read (file_1.toURL ().toExternalForm(), pkg2); 138 failed = false; 139 } catch (Exception e) { 140 fail (e.getMessage ()); 141 } finally { 142 repository.endTrans (failed); 143 } 144 145 try { 146 repository.beginTrans (false); 147 OutputStream os = new FileOutputStream (file_2); 148 writer.write (os, pkg2, null); 149 } catch (Exception e) { 150 fail (e.getMessage ()); 151 } finally { 152 repository.endTrans (false); 153 } 154 155 deleteModel (pkg); 156 deleteModel (pkg2); 157 } 158 159 public void testCollectionWritting () { 160 RefPackage pkg = instantiateModel ("Text.xml", "Text", "text_1"); 161 RefPackage pkg2 = instantiateModel ("Text.xml", "Text", "text_2"); 162 new RandomDataGenerator ().generate (pkg, 0, 10); 163 164 Lookup lookup = Lookup.getDefault (); 165 XmiReader reader = (XmiReader) lookup.lookup (XmiReader.class); 166 XmiWriter writer = (XmiWriter) lookup.lookup (XmiWriter.class); 167 168 RefClass proxyWord = pkg.refClass ("Word"); 169 RefClass proxySentence = pkg.refClass ("Sentence"); 170 int sentenceSize = proxySentence.refAllOfType ().size (); 171 int wordSize = proxyWord.refAllOfType ().size (); 172 List list = new LinkedList (); 173 174 int wordCount = 0, sentenceCount = 0; 175 Iterator iter = proxySentence.refAllOfType ().iterator (); 176 for (int x = 0; x < sentenceSize; x++) { 177 if (x > x / 3) { 178 list.add (iter.next ()); 179 sentenceCount++; 180 } 181 } 182 iter = proxyWord.refAllOfType ().iterator (); 183 for (int x = 0; x < wordSize; x++) { 184 if (x > x / 4) { 185 list.add (iter.next ()); 186 wordCount++; 187 } 188 } 189 190 File file = getFile ("temp.xml"); 191 try { 192 OutputStream os = new FileOutputStream (file); 193 repository.beginTrans (true); 194 writer.write (os, list, null); 195 os.flush (); 196 os.close (); 197 } catch (Exception e) { 198 e.printStackTrace (); 199 fail (e.getMessage ()); 200 } finally { 201 repository.endTrans (); 202 } 203 try { 204 repository.beginTrans (true); 205 reader.read (file.toURL ().toExternalForm(), pkg2); 206 } catch (Exception e) { 207 fail (e.getMessage ()); 208 } finally { 209 repository.endTrans (); 210 } 211 212 if (pkg2.refClass ("Sentence").refAllOfType().size() != sentenceCount) 214 fail ("Number of deserialized Classes does not match."); 215 if (pkg2.refClass ("Word").refAllOfType().size() != wordCount) 216 fail ("Number of deserialized Methods does not match."); 217 218 deleteModel (pkg); 219 deleteModel (pkg2); 220 } 221 222 224 228 private void compareData (RefPackage pkg_1, RefPackage pkg_2, Set checkedPackages) { 229 Set set = new HashSet (); 230 checkedPackages.add (pkg_1); 231 Iterator iter = pkg_1.refAllClasses ().iterator (); 232 while (iter.hasNext ()) { 233 RefClass proxy = (RefClass) iter.next (); 234 ModelElement meta = (ModelElement) proxy.refMetaObject (); 235 int count = proxy.refAllOfType ().size (); 236 int count2 = pkg_2.refClass (meta).refAllOfType ().size (); 237 if (count != count2) 238 fail ("Number of instances does not match: " + meta.getName () + " " + count + " " + count2); 239 } 240 iter = pkg_1.refAllAssociations ().iterator (); 241 while (iter.hasNext ()) { 242 RefAssociation proxy = (RefAssociation) iter.next (); 243 ModelElement meta = (ModelElement) proxy.refMetaObject (); 244 int count = proxy.refAllLinks ().size (); 245 int count2 = pkg_2.refAssociation (meta).refAllLinks ().size (); 246 if (count != count2) 247 fail ("Number of links does not match: " + meta.getName () + " " + count + " " + count2); 248 } 249 iter = pkg_1.refAllPackages ().iterator (); 250 while (iter.hasNext ()) { 251 RefPackage p1 = (RefPackage) iter.next (); 252 ModelElement meta = (ModelElement) p1.refMetaObject (); 253 RefPackage p2 = pkg_2.refPackage (meta); 254 compareData (p1, p2, checkedPackages); 255 } 256 } 257 258 265 public void readWrite (String docName) { 266 Lookup lookup = Lookup.getDefault (); 267 XmiReader reader = (XmiReader) lookup.lookup (XmiReader.class); 268 XmiWriter writer = (XmiWriter) lookup.lookup (XmiWriter.class); 269 URL url = this.getClass ().getResource ("data" + File.separator + docName); 270 if (url == null) 271 fail ("Resource not found."); 272 RefPackage pkg = createExtent ( 273 findMofPackage (modelPackage, "Model"), "PureMOF_test" + docName 274 ); 275 try { 276 repository.beginTrans (true); 277 reader.read (url.toExternalForm(), pkg); 278 } catch (Exception e) { 279 fail (e.getMessage ()); 280 } finally { 281 repository.endTrans (); 282 } 283 File file = getFile ("temp.xml"); 284 try { 285 OutputStream os = new FileOutputStream (file); 286 repository.beginTrans (true); 287 writer.write (os, pkg, null); 288 os.flush (); 289 os.close (); 290 } catch (Exception e) { 291 e.printStackTrace (); 292 fail (e.getMessage ()); 293 } finally { 294 repository.endTrans (); 295 } 296 XMIComparator comp = new XMIComparator (); 297 InputSource is = new InputSource (url.toString ()); 298 InputSource stream = null; 299 try { 300 stream = new InputSource (new FileInputStream (file)); 301 } catch (Exception e) { 302 fail (e.getMessage ()); 303 } 304 String result = comp.compareDocuments (is, stream); 305 if (result != null) 306 fail (result); 307 assertTrue (true); 308 try { 309 pkg.refDelete (); 310 } catch (Exception e) { } 312 } 313 314 private RefPackage instantiateModel (String docName, String mainPackageName, String extentName) { 315 ModelPackage pkg = loadMOFModel (docName, extentName + "_meta"); 316 RefPackage result = createExtent ( 317 findMofPackage (pkg, mainPackageName), 318 extentName 319 ); 320 return result; 321 } 322 323 private void deleteModel (RefPackage pkg) { 324 RefPackage meta = null; 325 try { 326 if (pkg.refMetaObject () != null) 327 meta = pkg.refMetaObject ().refOutermostPackage (); 328 } catch (javax.jmi.reflect.InvalidObjectException e) { 329 } 330 try { 331 pkg.refDelete (); 332 } catch (Exception e) { 333 } 334 try { 335 if (meta != null) 336 meta.refDelete (); 337 } catch (Exception e) { 338 } 339 } 340 } 341 | Popular Tags |