1 19 20 package org.netbeans.modules.xml.axi.sync; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.Timer ; 25 import java.util.TimerTask ; 26 import junit.framework.*; 27 import org.netbeans.modules.xml.axi.AXIComponent; 28 import org.netbeans.modules.xml.axi.AXIComponent.ComponentType; 29 import org.netbeans.modules.xml.axi.AXIDocument; 30 import org.netbeans.modules.xml.axi.AXIModel; 31 import org.netbeans.modules.xml.axi.Compositor; 32 import org.netbeans.modules.xml.axi.ContentModel; 33 import org.netbeans.modules.xml.axi.Element; 34 import org.netbeans.modules.xml.axi.impl.AXIModelImpl; 35 import org.netbeans.modules.xml.axi.impl.ElementProxy; 36 import org.netbeans.modules.xml.axi.visitor.DeepAXITreeVisitor; 37 import org.netbeans.modules.xml.schema.model.GlobalGroup; 38 import org.netbeans.modules.xml.schema.model.LocalElement; 39 40 41 46 public class MultiFileSyncTest extends AbstractSyncTestCase { 47 48 public static final String TEST_XSD = "resources/employee.xsd"; 49 public static final String IMPORTED_XSD = "resources/address1.xsd"; 50 public static final String GLOBAL_ELEMENT = "employee"; 51 private ContentModel usAddress; 52 private PropertyListener pcl; 53 private boolean sync1 = false; 54 private boolean sync2 = false; 55 private boolean fromAXIModel = false; 56 57 public MultiFileSyncTest(String testName) { 58 super(testName, TEST_XSD, GLOBAL_ELEMENT); 59 } 60 61 public static Test suite() { 62 TestSuite suite = new TestSuite(MultiFileSyncTest.class); 63 return suite; 64 } 65 66 public void testUpdateImportedSchema() throws Exception { 67 AXIModel iModel = getModel(IMPORTED_XSD); 69 usAddress = findContentModel(iModel, "group"); 70 assert(usAddress != null); 71 72 AXIModelImpl model = (AXIModelImpl)getAXIModel(); 74 AXIDocument document = model.getRoot(); 75 DeepVisitor visitor = new DeepVisitor(); 76 visitor.traverse(document); 77 78 updateAddressInAXIModel(); 79 Thread.sleep(10000); 80 assert(sync1); 81 82 updateAddressInSchemaModel(); 84 Thread.sleep(10000); 85 assert(sync2); 86 } 87 88 private void updateAddressInAXIModel() throws Exception { 89 fromAXIModel = true; 90 usAddress.getModel().startTransaction(); 91 Element name = (Element)usAddress.getChildren().get(0).getChildren().get(0); 92 assert(name.getName().equals("name")); 93 name.setName("nameUpdatedFromDV"); 94 usAddress.getModel().endTransaction(); 95 } 96 97 private void updateAddressInSchemaModel() throws Exception { 98 fromAXIModel = false; 99 GlobalGroup type = (GlobalGroup)usAddress.getPeer(); 100 type.getModel().startTransaction(); 101 LocalElement name = (LocalElement)type.getChildren().get(0).getChildren().get(0); 102 assert(name.getName().equals("nameUpdatedFromDV")); 103 name.setName("nameUpdatedFromSV"); 104 type.getModel().endTransaction(); 105 } 106 107 private ContentModel findContentModel(AXIModel model, String name) { 108 for(ContentModel cm : model.getRoot().getContentModels()) { 109 if(cm.getName().equals(name)) { 110 return cm; 111 } 112 } 113 114 return null; 115 } 116 117 private class DeepVisitor extends DeepAXITreeVisitor { 118 AXIModel model; 119 public void traverse(AXIDocument document) { 120 model = document.getModel(); 121 document.accept(this); 122 } 123 124 public void visit(Compositor compositor) { 125 if(compositor.getComponentType() != ComponentType.PROXY) { 126 visitChildren(compositor); 127 return; 128 } 129 130 Element e = (Element)usAddress.getChildren().get(0).getChildren().get(0); 131 ElementProxy eP = (ElementProxy)compositor.getChildren().get(0); 132 assert(eP.getModel() == model); 133 assert(eP.getOriginal().getModel() != model); 134 assert(eP.getOriginal().getModel() == usAddress.getModel()); 135 assert(eP.getName().equals("name")); 136 pcl = new PropertyListener(eP); 137 eP.addPropertyChangeListener(pcl); 138 visitChildren(compositor); 139 } 140 } 141 142 private class PropertyListener implements PropertyChangeListener { 143 private AXIComponent source; 144 145 public PropertyListener (AXIComponent source) { 146 this.source = source; 147 } 148 149 public void propertyChange(PropertyChangeEvent evt) { 150 if(evt.getSource() == source) { 151 String name = (String )evt.getNewValue(); 152 if(fromAXIModel) { 153 assert(name.equals("nameUpdatedFromDV")); 154 sync1 = true; 155 } else { 156 assert(name.equals("nameUpdatedFromSV")); 157 sync2 = true; 158 } 159 } 160 } 161 } 162 } 163 | Popular Tags |