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 TransactionTest extends MDRTestCase { 44 45 private static final int MAX = 500; 46 private static int counter = 0; 47 private Random random = new Random (0); 48 49 public TransactionTest(String testName) { 50 super (testName); 51 } 52 53 public static void main (String [] args) { 54 junit.textui.TestRunner.run (suite ()); 55 } 56 57 public static Test suite () { 58 TestSuite suite = new TestSuite (); 59 suite.addTestSuite (TransactionTest.class); 60 61 TestSetup setup = new TestSetup (suite) { 62 public void setUp () { 63 } 65 public void tearDown () { 66 } 67 }; 68 return setup; 69 } 70 71 public void test () { 72 int x; 73 VisibilityKindEnum visKind = null; 74 Contains assoc = modelPackage.getContains (); 75 MofClass mainClass = generateClass (); 76 AttributeClass attribClass = modelPackage.getAttribute (); 77 78 int classes_incr, links_incr; 79 int classes_num = modelPackage.getMofClass ().refAllOfType ().size (); 80 int links_num = assoc.refQuery ("container", mainClass).size (); 81 82 try { 84 repository.beginTrans (true); 85 } catch (Exception e) { 86 e.printStackTrace (); 87 fail (e.getMessage ()); 88 } finally { 89 repository.endTrans (true); 90 } 91 92 final int VIS_NUM = 50; 94 Attribute [] attrib = new Attribute [VIS_NUM]; 95 VisibilityKind [] vis = new VisibilityKind [VIS_NUM]; 96 97 for (x = 0; x < VIS_NUM; x++) { 98 attrib [x] = attribClass.createAttribute (); 99 int rnd = random.nextInt (3); 100 switch (rnd) { 101 case 0: visKind = VisibilityKindEnum.PUBLIC_VIS; break; 102 case 1: visKind = VisibilityKindEnum.PROTECTED_VIS; break; 103 case 2: visKind = VisibilityKindEnum.PRIVATE_VIS; break; 104 } 105 attrib [x].setVisibility (visKind); 106 vis [x] = visKind; 107 } 108 try { 109 repository.beginTrans (true); 110 for (x = 0; x < VIS_NUM; x++) { 111 int rnd = random.nextInt (3); 112 switch (rnd) { 113 case 0: visKind = VisibilityKindEnum.PUBLIC_VIS; break; 114 case 1: visKind = VisibilityKindEnum.PROTECTED_VIS; break; 115 case 2: visKind = VisibilityKindEnum.PRIVATE_VIS; break; 116 } 117 attrib [x].setVisibility (visKind); 118 } 119 } catch (Exception e) { 120 e.printStackTrace (); 121 fail (e.getMessage ()); 122 } finally { 123 repository.endTrans (true); 124 } 125 for (x = 0; x < VIS_NUM; x++) { 127 if (attrib [x].getVisibility () != vis[x]) 128 fail (); 129 } 130 131 for (int y = 0; y < 100; y++) { 133 classes_incr = links_incr = 0; 134 boolean rollback = (random.nextInt (3) == 2); 135 try { 136 repository.beginTrans (true); 137 int top = 10 + random.nextInt (MAX); 138 for (x = 0; x < top; x++) { 139 MofClass obj = generateClass (); 140 classes_incr++; 141 if (random.nextInt (3) == 1) { 142 obj.refDelete (); 143 classes_incr--; 144 } else if (random.nextInt (6) == 1) { 145 obj.setContainer (mainClass); 146 links_incr++; 147 } 148 } } catch (Exception e) { 150 e.printStackTrace (); 151 fail (e.getMessage ()); 152 } finally { 153 repository.endTrans (rollback); 154 } 155 156 if (!rollback) { 157 classes_num += classes_incr; 158 links_num += links_incr; 159 } 160 161 if (classes_num != modelPackage.getMofClass ().refAllOfType ().size ()) { 162 fail ("number of instances does not match: " + classes_num); 163 } 164 if (links_num != assoc.refQuery ("container", mainClass).size ()) { 165 fail ("number of links does not match: " + links_num); 166 } 167 168 } 170 } 171 172 public void testMemoryStorage () { 173 System.setProperty("org.netbeans.mdr.persistence.memoryimpl.persistent", "false"); 174 System.setProperty("org.netbeans.mdr.storagemodel.StorageFactoryClassName", 175 "org.netbeans.mdr.persistence.memoryimpl.StorageFactoryImpl"); 176 test (); 177 } 178 179 private MofClass generateClass () { 180 MofClassClass proxy = modelPackage.getMofClass (); 181 return proxy.createMofClass ("name" + counter++, "", true, true, true, VisibilityKindEnum.PUBLIC_VIS, true); 182 } 183 184 private void printListElements (List list) { 185 Iterator iter = list.iterator (); 186 System.out.println("list size: " + list.size () + " " + list.getClass ().getName ()); 187 while (iter.hasNext ()) { 188 ModelElement element = (ModelElement) iter.next (); 189 System.out.println((element != null ? element.getName () : "null") + " "); 190 } 191 System.out.println(""); 192 } 193 194 } | Popular Tags |