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 javax.jmi.reflect.*; 38 import javax.jmi.model.*; 39 40 43 public class MultivaluedIndexTest extends MDRTestCase { 44 45 private static final int MAX = 2000; 47 48 private long counter = 0; 49 private Random random = new Random (0); 50 51 public MultivaluedIndexTest(String testName) { 52 super (testName); 53 } 54 55 public static void main (String [] args) { 56 junit.textui.TestRunner.run (suite ()); 57 } 58 59 public static Test suite () { 60 TestSuite suite = new TestSuite (); 61 suite.addTestSuite (MultivaluedIndexTest.class); 62 63 TestSetup setup = new TestSetup (suite) { 64 public void setUp () { 65 } 67 public void tearDown () { 68 } 69 }; 70 return setup; 71 } 72 73 public void test () { 74 MofClass classC = null, classN = null, classP = null; 75 MofException excF = null, excR = null; 76 Iterator iter = modelPackage.getMofClass ().refAllOfType ().iterator (); 77 while (iter.hasNext ()) { 78 MofClass mofClass = (MofClass) iter.next (); 79 String name = mofClass.getName (); 80 if (name.equals ("Namespace")) 81 classN = mofClass; 82 if (name.equals ("Class")) 83 classC = mofClass; 84 if (name.equals ("Package")) 85 classP = mofClass; 86 } 87 iter = modelPackage.getMofException ().refAllOfType ().iterator (); 88 while (iter.hasNext ()) { 89 MofException mofExc = (MofException) iter.next (); 90 String name = mofExc.getName (); 91 if (name.equals ("NameNotFound")) 92 excF = mofExc; 93 if (name.equals ("NameNotResolved")) 94 excR = mofExc; 95 } 96 97 iter = classC.getContents ().iterator (); 98 excF.setContainer (classP); 99 iter.next (); 100 excF.setContainer (classN); 101 while (iter.hasNext ()) 102 iter.next (); 103 104 try { 105 iter = classC.getContents ().iterator (); 106 excF.setContainer (classC); 107 excF.setContainer (classN); 108 iter.next (); 109 fail ("ConcurrentModificationException has not been thrown."); 111 } catch (ConcurrentModificationException e) { 112 } 113 114 iter = classC.getContents ().iterator (); 115 while (iter.hasNext ()) 116 iter.next (); 117 118 Contains assoc = modelPackage.getContains (); 119 List list = (List) assoc.refQuery ("container", classC); 120 list = null; 121 System.gc (); 122 list = (List) assoc.refQuery ("container", classC); 123 124 ListIterator listIter = list.listIterator (); 125 Object obj = listIter.next (); 126 listIter.remove (); 127 Object obj2 = listIter.next (); 128 listIter.add (obj); 129 Object obj3 = listIter.next (); 130 } 131 132 public void test2 () { 133 try { 134 repository.beginTrans (true); 135 int x; 136 MofClass main = generateClass (); 137 for (x = 0; x < MAX; x++) 138 generateClass ().setContainer (main); 139 Contains assoc = modelPackage.getContains (); 140 List list = (List) assoc.refQuery ("container", main); 141 ListIterator iter = list.listIterator (); 142 143 repository.endTrans (); 144 repository.beginTrans (true); 145 146 for (x = 0; x < MAX/10; x++) 148 iter.add (generateClass ()); 149 for (x = 0; x < MAX/10; x++) { 150 iter.next (); 151 iter.remove (); 152 } 153 154 repository.endTrans (); 155 repository.beginTrans (true); 156 157 iter = list.listIterator (); 158 Random random = new Random (); 159 int size = MAX; 160 for (x = 0; x < MAX/2; x++) 161 iter.next (); 162 for (x = 0; x < 4*MAX; x++) { 163 int opCode = random.nextInt (5); 164 switch (opCode) { 165 case 0: 166 if (iter.hasNext ()) 167 iter.next (); 168 break; 169 case 1: 170 if (iter.hasPrevious ()) 171 iter.previous (); 172 break; 173 case 2: 174 if (iter.hasNext ()) { 175 iter.next (); 176 iter.remove (); 177 size--; 178 } else if (iter.hasPrevious ()) { 179 iter.previous (); 180 iter.remove (); 181 size--; 182 } 183 break; 184 case 3: 185 if (iter.hasNext ()) { 186 iter.next (); 187 iter.set (generateClass ()); 188 } else if (iter.hasPrevious ()) { 189 iter.previous (); 190 iter.set (generateClass ()); 191 } 192 break; 193 case 4: 194 iter.add (generateClass ()); 195 size++; 196 break; 197 } } if (list.size () != size) 200 fail ("Counted list size does not match a value returned by size () method."); 201 } catch (Exception e) { 202 e.printStackTrace (); 203 fail (e.getMessage ()); 204 } finally { 205 repository.endTrans (); 206 } 207 } 208 209 public void test3 () { 210 try { 211 repository.beginTrans (true); 212 MofClass class_A = generateClass (); 213 MofClass class_B = generateClass (); 214 class_B.setContainer (class_A); 215 for (int x = 0; x < 3; x++) 216 generateClass ().setContainer (class_B); 217 Contains assoc = modelPackage.getContains (); 218 List list = (List) assoc.refQuery ("container", class_A); 219 printListElements ((List) assoc.refQuery ("container", class_B)); 220 printListElements ((List) assoc.refQuery ("containedElement", class_B)); 221 Iterator iter = list.iterator (); 222 iter.next (); 223 iter.remove (); 224 printListElements (list); 225 printListElements ((List) assoc.refQuery ("container", class_B)); 226 printListElements ((List) assoc.refQuery ("containedElement", class_B)); 227 228 MofClass class_C = generateClass (); 229 for (int x = 0; x < 100; x++) 230 generateClass().setContainer(class_C); 231 int index = -1; 232 ListIterator liter = class_C.getContents().listIterator(); 233 for (int x = 0; x < 90; x++) { 234 int val = random.nextInt (5); 235 switch (val) { 236 case 0: 237 case 1: 238 liter.add (generateClass()); 239 index ++; 240 break; 241 case 2: 242 case 3: 243 liter.next (); 244 index ++; 245 break; 246 case 4: 247 if (index > 0) { 248 liter.previous (); 249 index--; 250 } 251 } } if (liter.nextIndex() != (index + 1)) 254 fail (); 255 256 } catch (Exception e) { 257 e.printStackTrace (); 258 fail (e.getMessage ()); 259 } finally { 260 repository.endTrans (); 261 } 262 } 263 264 public void test4 () { 265 try { 266 repository.beginTrans (true); 267 MofClass class_A = generateClass (); 268 MofClass class_B = generateClass (); 269 MofClass class_C = generateClass (); 270 class_B.setContainer (class_A); 271 for (int x = 0; x < 3; x++) 272 generateClass ().setContainer (class_B); 273 class_C.setContainer (class_B); 274 275 Contains assoc = modelPackage.getContains (); 276 277 printListElements ((List) assoc.refQuery ("container", class_B)); 278 printListElements ((List) assoc.refQuery ("containedElement", class_B)); 279 280 class_B.refDelete (); 281 282 296 } catch (Exception e) { 297 System.out.println(e); 298 e.printStackTrace (); 299 fail (e.getMessage ()); 300 } finally { 301 repository.endTrans (); 302 } 303 } 304 305 public void test5 () { 306 ModelPackage pkg = (ModelPackage) createExtent(findMofPackage(modelPackage, "Model"), "Test"); 307 MofClassClass proxy = pkg.getMofClass (); 308 MofClass root = generateClass (proxy); 310 int num = proxy.refAllOfClass ().size (); 311 generateTree (root, 4, proxy); 312 deleteTree (root); 313 if (proxy.refAllOfClass ().size () != 1) 314 fail (); 315 generateTree2 (root, 4, proxy); 316 deleteTree (root); 317 if (proxy.refAllOfClass ().size () != 1) 318 fail (); 319 } 320 321 private void generateTree (MofClass elem, int depth, MofClassClass proxy) { 322 int sons = 1 + random.nextInt (7); 323 for (int x = 0; x < sons; x++) { 324 MofClass mofClass = generateClass (proxy); 325 mofClass.setContainer (elem); 326 if (depth > 0) 327 generateTree (mofClass, depth - 1, proxy); 328 } 329 } 330 331 private void generateTree2 (MofClass elem, int depth, MofClassClass proxy) { 332 int sons = 4 + random.nextInt (5); 333 ListIterator iter = elem.getContents ().listIterator (); 334 for (int x = 0; x < sons; x++) { 335 MofClass mofClass = generateClass (proxy); 336 iter.add (mofClass); 337 if (depth > 0) 338 generateTree (mofClass, depth - 1, proxy); 339 if (random.nextBoolean()) { 340 deleteTree (mofClass); 341 iter.previous (); 342 iter.remove (); 343 mofClass.refDelete (); 344 } 345 } 346 } 347 348 private void deleteTree (MofClass node) { 349 Collection subNodes = node.getContents(); 350 int size = subNodes.size(); 351 for (Iterator it = subNodes.iterator(); it.hasNext();) { 352 MofClass subNode = (MofClass)it.next(); 353 it.remove(); 354 deleteTree (subNode); 355 subNode.refDelete(); 356 } 357 } 358 359 private MofClass generateClass (MofClassClass proxy) { 360 return proxy.createMofClass ("name" + counter++, "", true, true, true, VisibilityKindEnum.PUBLIC_VIS, true); 361 } 362 363 private MofClass generateClass () { 364 MofClassClass proxy = modelPackage.getMofClass (); 365 return proxy.createMofClass ("name" + counter++, "", true, true, true, VisibilityKindEnum.PUBLIC_VIS, true); 366 } 367 368 private void printListElements (List list) { 369 378 } 379 380 } 381 | Popular Tags |