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.ArrayList ; 25 import java.util.List ; 26 import junit.framework.*; 27 import org.netbeans.modules.xml.axi.Element; 28 import org.netbeans.modules.xml.schema.model.AttributeReference; 29 import org.netbeans.modules.xml.schema.model.ElementReference; 30 import org.netbeans.modules.xml.schema.model.GlobalAttribute; 31 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup; 32 import org.netbeans.modules.xml.schema.model.GlobalComplexType; 33 import org.netbeans.modules.xml.schema.model.GlobalElement; 34 import org.netbeans.modules.xml.schema.model.GlobalGroup; 35 import org.netbeans.modules.xml.schema.model.GlobalType; 36 import org.netbeans.modules.xml.schema.model.GroupReference; 37 import org.netbeans.modules.xml.schema.model.LocalAttribute; 38 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 39 import org.netbeans.modules.xml.schema.model.Sequence; 40 41 42 48 public class SyncElementTest extends AbstractSyncTestCase { 49 50 public static final String TEST_XSD = "resources/address.xsd"; 51 public static final String GLOBAL_ELEMENT = "address"; 52 53 54 57 public SyncElementTest(String testName) { 58 super(testName, TEST_XSD, GLOBAL_ELEMENT); 59 } 60 61 public static Test suite() { 62 TestSuite suite = new TestSuite(SyncElementTest.class); 63 return suite; 64 } 65 66 public void testElementType() throws Exception { 67 removeElementFromType(); 68 removeAttributeFromAttrGroup(); 69 changeType(); 70 changeAttributeRef(); 71 changeTypeContent(); 72 changeNameOfElement(); 73 } 75 76 public void testElement() throws Exception { 77 removeGlobalElement(); 78 addGlobalElement(); 79 changeElementRef(); 80 } 81 82 86 private void removeElementFromType() throws Exception { 87 Element address = findAXIGlobalElement("address"); 88 int childCount = address.getChildElements().size(); 89 assert(childCount == 3); 90 GlobalComplexType gct = findGlobalComplexType("USAddress"); 91 getSchemaModel().startTransaction(); 92 Sequence s = (Sequence)gct.getChildren().get(0); 93 s.removeContent(s.getContent().get(0)); 94 getSchemaModel().endTransaction(); 95 getAXIModel().sync(); 96 childCount = address.getChildElements().size(); 97 assert(childCount == 2); 98 } 99 100 104 private void removeAttributeFromAttrGroup() throws Exception { 105 Element address = findAXIGlobalElement("address"); 106 int childCount = address.getChildren().size(); 107 assert(childCount == 4); 108 GlobalAttributeGroup gag = findGlobalAttributeGroup("attr-group"); 109 getSchemaModel().startTransaction(); 110 LocalAttribute attr = (LocalAttribute)gag.getChildren().get(1); 111 gag.removeLocalAttribute(attr); 112 getSchemaModel().endTransaction(); 113 getAXIModel().sync(); 114 childCount = address.getChildren().size(); 115 assert(childCount == 3); 116 } 117 118 122 private void changeType() throws Exception { 123 PropertyListener l = new PropertyListener(); 124 getAXIModel().addPropertyChangeListener(l); 125 Element address = findAXIGlobalElement("address"); 126 int childCount = address.getChildElements().size(); 127 assert(childCount == 2); 128 GlobalElement ge = (GlobalElement)globalElement.getPeer(); 129 getSchemaModel().startTransaction(); 130 setType(ge, "USAddress1"); 131 getSchemaModel().endTransaction(); 132 getAXIModel().sync(); 133 getAXIModel().removePropertyChangeListener(l); 134 childCount = address.getChildElements().size(); 135 assert(childCount == 5); 136 } 137 138 141 private void changeTypeContent() throws Exception { 142 PropertyListener l = new PropertyListener(); 143 getAXIModel().addPropertyChangeListener(l); 144 Element address = findAXIGlobalElement("address"); 145 int childCount = address.getChildElements().size(); 146 assert(childCount == 5); 147 getSchemaModel().startTransaction(); 148 GlobalGroup gg = findGlobalGroup("group2"); 149 GroupReference gr = (GroupReference)(findGlobalComplexType("USAddress1").getChildren().get(0)); 150 NamedComponentReference ref = getSchemaModel().getFactory(). 151 createGlobalReference(gg, GlobalGroup.class, gr); 152 gr.setRef(ref); 153 getSchemaModel().endTransaction(); 154 getAXIModel().sync(); 155 getAXIModel().removePropertyChangeListener(l); 156 childCount = address.getChildElements().size(); 157 assert(childCount == 3); 158 } 159 160 private void changeNameOfElement() throws Exception { 161 Element address = findAXIGlobalElement("address"); 162 int childCount = address.getChildElements().size(); 163 GlobalElement e = findGlobalElement("address"); 164 getSchemaModel().startTransaction(); 165 e.setName("NewAddress"); 166 getSchemaModel().endTransaction(); 167 getAXIModel().sync(); 168 assert(childCount == address.getChildElements().size()); 169 assert(address.getName().equals("NewAddress")); 170 } 171 172 private void changeNameOfType() throws Exception { 173 Element address = findAXIGlobalElement("address"); 174 int childCount = address.getChildElements().size(); 175 GlobalComplexType type = findGlobalComplexType("USAddress1"); 176 getSchemaModel().startTransaction(); 177 type.setName("USAddress2"); 178 getSchemaModel().endTransaction(); 179 getAXIModel().sync(); 180 assert(childCount == address.getChildElements().size()); 181 } 182 183 private void changeElementRef() throws Exception { 184 getSchemaModel().startTransaction(); 185 GlobalGroup group = findGlobalGroup("group1"); 186 ElementReference ref = (ElementReference)group.getChildren().get(0).getChildren().get(0); 187 GlobalElement ge = findGlobalElement("fullName"); 188 NamedComponentReference ncr = getSchemaModel().getFactory(). 189 createGlobalReference(ge, GlobalElement.class, ref); 190 ref.setRef(ncr); 191 getSchemaModel().endTransaction(); 192 getAXIModel().sync(); 193 } 194 195 198 private void removeGlobalElement() throws Exception { 199 int elementCount = getAXIModel().getRoot().getElements().size(); 200 Element address = findAXIGlobalElement("NewAddress"); 201 GlobalElement ge = (GlobalElement)address.getPeer(); 202 getSchemaModel().startTransaction(); 203 getSchemaModel().getSchema().removeElement(ge); 204 getSchemaModel().endTransaction(); 205 getAXIModel().sync(); 206 int newCount = getAXIModel().getRoot().getElements().size(); 207 assert( (elementCount-1) == newCount); 208 } 209 210 private void addGlobalElement() throws Exception { 211 int elementCount = getAXIModel().getRoot().getElements().size(); 212 getSchemaModel().startTransaction(); 213 GlobalElement ge = getSchemaModel().getFactory().createGlobalElement(); 214 ge.setName("address"); 215 setType(ge, "USAddress1"); 216 getSchemaModel().getSchema().addElement(ge); 217 getSchemaModel().endTransaction(); 218 getAXIModel().sync(); 219 int newCount = getAXIModel().getRoot().getElements().size(); 220 assert( (elementCount+1) == newCount); 221 } 222 223 private void setType(GlobalElement ge, String globalComplexType) { 224 for(GlobalComplexType type : getSchemaModel().getSchema().getComplexTypes()) { 225 if(type.getName().equals(globalComplexType)) { 226 NamedComponentReference ref = getSchemaModel().getFactory(). 227 createGlobalReference(type, GlobalType.class, ge); 228 ge.setType(ref); 229 } 230 } 231 } 232 233 private void renameGlobalElement() throws Exception { 234 getSchemaModel().startTransaction(); 235 GlobalElement ge = (GlobalElement)globalElement.getPeer(); 236 ge.setName("address1"); 237 getSchemaModel().endTransaction(); 238 getAXIModel().sync(); 239 assert(globalElement.getName().equals("address1")); 240 } 241 242 private void changeAttributeRef() throws Exception { 243 getSchemaModel().startTransaction(); 244 GlobalAttributeGroup group = findGlobalAttributeGroup("attr-group"); 245 AttributeReference ref = (AttributeReference)group.getChildren().get(0); 246 GlobalAttribute ga = findGlobalAttribute("countryString"); 247 NamedComponentReference ncr = getSchemaModel().getFactory(). 248 createGlobalReference(ga, GlobalAttribute.class, ref); 249 ref.setRef(ncr); 250 getSchemaModel().endTransaction(); 251 getAXIModel().sync(); 252 } 253 254 257 private void deleteGlobalType() throws Exception { 258 Element address = findAXIGlobalElement("NewAddress"); 259 int childCount = address.getChildElements().size(); 260 assert(childCount == 3); 261 GlobalComplexType gct = findGlobalComplexType("USAddress1"); 262 getSchemaModel().startTransaction(); 263 getSchemaModel().getSchema().removeComplexType(gct); 264 getSchemaModel().endTransaction(); 265 getAXIModel().sync(); 266 childCount = address.getChildElements().size(); 267 assert(childCount == 0); 268 } 269 270 static class PropertyListener implements PropertyChangeListener { 271 List <PropertyChangeEvent > events = new ArrayList <PropertyChangeEvent >(); 272 273 public void propertyChange(PropertyChangeEvent evt) { 274 events.add(evt); 275 } 276 public List <PropertyChangeEvent > getEvents() { 277 return events; 278 } 279 public void clearEvents() { events.clear();} 280 } 281 } 282 | Popular Tags |