1 7 8 package org.netbeans.mdr.test; 9 10 import java.util.Iterator ; 11 import java.util.Collection ; 12 13 import junit.framework.*; 14 15 import org.netbeans.mdr.handlers.BaseObjectHandler; 16 import org.netbeans.mdr.NBMDRepositoryImpl; 17 18 import org.netbeans.api.mdr.MDRManager; 19 import org.netbeans.api.mdr.MDRepository; 20 import org.netbeans.api.mdr.CreationFailedException; 21 22 import org.netbeans.api.mdr.events.*; 23 24 import javax.jmi.reflect.*; 25 import javax.jmi.model.*; 26 27 31 public class MDRTestAssociationPreEvents extends MDREventsTestCase implements MDRPreChangeListener { 32 33 private static final String PKG_NAME = "TEST_MOF"; 34 35 private EventsList eventsList = new EventsList(); 36 37 public MDRTestAssociationPreEvents(java.lang.String testName) { 38 super(testName); 39 } 40 41 protected void setUp() { 42 super.setUp(); 43 ((MDRChangeSource) repository).addListener(this); 44 } 45 46 protected void tearDown() { 47 super.tearDown(); 48 ((MDRChangeSource) repository).removeListener(this); 49 } 50 51 53 public void testAddAssociation() { 54 55 RefPackage testMOFPackage = createMOFExtent(PKG_NAME); 56 57 EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME)); 58 59 Collection allMOFAssociations = testMOFPackage.refAllAssociations(); 60 Iterator iterAssoc = allMOFAssociations.iterator(); 61 while (iterAssoc.hasNext()) { 62 RefAssociation assocProxy = (RefAssociation) iterAssoc.next(); 63 Association metaAssoc = (Association) assocProxy.refMetaObject(); 64 eventsList.addPattern2(metaAssoc.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED, System.currentTimeMillis()); 65 java.util.List ends = generator.generateAssociation(assocProxy, metaAssoc, null, true); 66 for (int i = 0; i < 20; i++) { 68 try { Thread.currentThread().sleep(100); } 69 catch (InterruptedException ie) { } 70 } 71 if (ends != null) { 72 assertTrue(eventsList.checkEvents(metaAssoc.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED)); 73 } 74 eventsList.clear(); 75 } 76 77 } 78 79 public void testRemoveAssociation() { 80 81 RefPackage testMOFPackage = createMOFExtent(PKG_NAME); 82 83 EventsDataGenerator generator = new EventsDataGenerator(repository.getExtent(PKG_NAME)); 84 85 Collection allMOFAssociations = testMOFPackage.refAllAssociations(); 86 Iterator iterAssoc = allMOFAssociations.iterator(); 87 while (iterAssoc.hasNext()) { 88 RefAssociation assocProxy = (RefAssociation) iterAssoc.next(); 89 Association metaAssoc = (Association) assocProxy.refMetaObject(); 90 eventsList.addPattern2(metaAssoc.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED, System.currentTimeMillis()); 91 java.util.List ends = generator.generateAssociation(assocProxy, metaAssoc, null, true); 92 for (int i = 0; i < 20; i++) { 94 try { Thread.currentThread().sleep(100); } 95 catch (InterruptedException ie) { } 96 } 97 if (ends != null) { 98 assertTrue(eventsList.checkEvents(metaAssoc.getName(), EventsList._PLANNED | EventsList._ADD | EventsList._CHANGED)); 99 } 100 eventsList.clear(); 101 102 if (ends != null) { 103 Collection links = assocProxy.refAllLinks(); 104 Object [] linksArray = links.toArray(); 105 if (linksArray.length >= 1) { 106 RefAssociationLink link = (RefAssociationLink) linksArray[0]; 107 eventsList.addPattern2(metaAssoc.getName(), EventsList._PLANNED | EventsList._REMOVE | EventsList._CHANGED, System.currentTimeMillis()); 108 assocProxy.refRemoveLink(link.refFirstEnd(), link.refSecondEnd()); 109 for (int i = 0; i < 20; i++) { 111 try { Thread.currentThread().sleep(100); } 112 catch (InterruptedException ie) { } 113 } 114 assertTrue(eventsList.checkEvents(metaAssoc.getName(), EventsList._PLANNED | EventsList._REMOVE | EventsList._CHANGED)); 115 eventsList.clear(); 116 } else { 117 fail("Association contained no link!"); 118 } 119 } 120 } 121 122 } 123 124 126 public void change(MDRChangeEvent e) { 127 assertEquals("Wrong order of events!", eventsQueue.getFirst(), e); 128 eventsQueue.removeFirst(); 129 processEvent(e, CHANGED); 130 } 131 132 public void changeCancelled(MDRChangeEvent e) { 133 assertEquals("Wrong order of events!", eventsQueue.getFirst(), e); 134 eventsQueue.removeFirst(); 135 processEvent(e, CANCELED); 136 } 137 138 public void plannedChange(MDRChangeEvent e) { 139 eventsQueue.add(e); 140 processEvent(e, PLANNED); 141 } 142 143 private void processEvent(MDRChangeEvent e, int type) { 144 145 if (e.isOfType(AssociationEvent.EVENTMASK_ASSOCIATION)) { 147 if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_ADD) && (type == PLANNED)) { 149 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(), 150 e, System.currentTimeMillis(), EventsList._ADD | EventsList._PLANNED); 151 } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_ADD) && (type == CHANGED)) { 152 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(), 153 e, System.currentTimeMillis(), EventsList._ADD | EventsList._CHANGED); 154 } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_ADD) && (type == CANCELED)) { 155 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(), 156 e, System.currentTimeMillis(), EventsList._ADD | EventsList._CANCELED); 157 } 158 else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_SET) && (type == PLANNED)) { 160 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(), 161 e, System.currentTimeMillis(), EventsList._SET | EventsList._PLANNED); 162 } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_SET) && (type == CHANGED)) { 163 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(), 164 e, System.currentTimeMillis(), EventsList._SET | EventsList._CHANGED); 165 } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_SET) && (type == CANCELED)) { 166 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(), 167 e, System.currentTimeMillis(), EventsList._SET | EventsList._CANCELED); 168 } 169 else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_REMOVE) && (type == PLANNED)) { 171 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(), 172 e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._PLANNED); 173 } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_REMOVE) && (type == CHANGED)) { 174 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(), 175 e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._CHANGED); 176 } else if (e.isOfType(AssociationEvent.EVENT_ASSOCIATION_REMOVE) && (type == CANCELED)) { 177 eventsList.addEvent(((ModelElement) ((RefBaseObject)((AssociationEvent) e).getSource()).refMetaObject()).getName(), 178 e, System.currentTimeMillis(), EventsList._REMOVE | EventsList._CANCELED); 179 } 180 181 } 182 183 } 184 185 } 186 | Popular Tags |