1 19 20 package org.netbeans.modules.xml.axi; 21 22 import javax.swing.text.Document ; 23 import junit.framework.*; 24 import org.netbeans.modules.xml.axi.datatype.BooleanType; 25 import org.netbeans.modules.xml.schema.model.AttributeGroupReference; 26 import org.netbeans.modules.xml.schema.model.Choice; 27 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup; 28 import org.netbeans.modules.xml.schema.model.GlobalComplexType; 29 import org.netbeans.modules.xml.schema.model.GlobalElement; 30 import org.netbeans.modules.xml.schema.model.GlobalSimpleType; 31 import org.netbeans.modules.xml.schema.model.LocalAttribute; 32 import org.netbeans.modules.xml.schema.model.LocalComplexType; 33 import org.netbeans.modules.xml.schema.model.LocalElement; 34 import org.netbeans.modules.xml.schema.model.SchemaModel; 35 import org.netbeans.modules.xml.schema.model.SimpleContent; 36 import org.netbeans.modules.xml.schema.model.SimpleExtension; 37 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 38 39 40 44 public class SchemaGeneratorTest extends AbstractTestCase { 45 46 public static final String TEST_XSD = "resources/OTA_TI_simple.xsd"; 47 public static final String GLOBAL_ELEMENT = "OTA_TravelItineraryRS"; 48 49 private Document doc = null; 50 51 public static final int GE_SIZE = 4; 52 53 public SchemaGeneratorTest(String testName) { 54 super(testName, TEST_XSD, GLOBAL_ELEMENT); 55 } 56 57 protected void setUp() throws Exception { 58 super.setUp(); 59 } 60 61 protected void tearDown() throws Exception { 62 axiModel.endTransaction(); 63 super.tearDown(); 64 } 65 66 public static Test suite() { 67 TestSuite suite = new TestSuite(SchemaGeneratorTest.class); 68 69 return suite; 70 } 71 72 public void testGenerateSchema() throws Exception { 73 generateSchema(); 74 } 75 76 79 public void generateSchema() throws Exception { 80 Element element = globalElement; 81 assertNotNull(element); 82 SchemaModel sm = null; 83 sm = getSchemaModel(); 84 doc = ((AbstractDocumentModel)sm).getBaseDocument(); 85 87 assertEquals("global elements",GE_SIZE,getSchemaModel().getSchema().getElements().size()); 88 axiModel.startTransaction(); 90 for(Element e:axiModel.getRoot().getElements()) 91 if(e.getName().equals("CancellationStatus")) 92 e.setName(e.getName()+"_"); 93 axiModel.endTransaction(); 94 assertEquals("global elements",GE_SIZE,getSchemaModel().getSchema().getElements().size()); 95 boolean found = false; 96 for(GlobalElement ge:sm.getSchema().getElements()) { 97 if(ge.getName().startsWith("CancellationStatus_")) { 98 found = true; 99 assertEquals("updated schemamodel", ge.getName(), "CancellationStatus_"); 100 assertEquals("updated schemamodel type", 101 ((LocalElement)ge.getChildren().get(0). getChildren().get(0). getChildren().get(0)). getType().getQName().getLocalPart(), "date"); 105 } 106 } 107 assertTrue("found CancellationStatus_", found); 108 110 axiModel.startTransaction(); 112 for(Element e:axiModel.getRoot().getElements()) { 113 if(e.getName().equals(GLOBAL_ELEMENT)) { 114 for(AXIComponent e2: e.getCompositor().getChildren()) { 115 if(e2 instanceof Element) { 116 if(((Element)e2).getName().equals("Errors")) { 117 AbstractAttribute attr = ((Element)((Element)e2).getChildren().get(0).getChildren().get(0)).getAttributes().get(0); 118 assertEquals("Language", attr.getName()); 119 if(attr instanceof Attribute) { 120 ((Attribute)attr).setName("XYZ"); 121 } 122 } 123 } 124 } 125 } 126 } 127 axiModel.endTransaction(); 128 assertEquals("global elements",GE_SIZE,getSchemaModel().getSchema().getElements().size()); 130 131 found = false; 132 for(GlobalElement ge:sm.getSchema().getElements()) { 133 if(ge.getName().startsWith(GLOBAL_ELEMENT)) { 134 LocalComplexType lct = (LocalComplexType) ge.getChildren().get(1); 135 Choice choice = (Choice) lct.getChildren().get(0); 136 LocalElement le = (LocalElement) choice.getChildren().get(1); 137 GlobalComplexType gct = (GlobalComplexType) le.getType().get(); 138 le = (LocalElement) gct.getChildren().get(1).getChildren().get(0); 139 gct = (GlobalComplexType) le.getType().get(); 140 SimpleExtension se = (SimpleExtension) gct.getChildren().get(1).getChildren().get(0); 141 gct = (GlobalComplexType)se.getBase().get(); 142 AttributeGroupReference agr = (AttributeGroupReference)gct.getChildren().get(1).getChildren().get(0).getChildren().get(0); 143 GlobalAttributeGroup gag = agr.getGroup().get(); 144 LocalAttribute la = (LocalAttribute)gag.getChildren().get(1); 145 assertEquals("updated schemamodel", "XYZ", la.getName()); 146 found = true; 147 } 148 } 149 assertTrue("Should have verified updated element", found); 150 validateSchema(sm); 151 } 152 153 public void testGenerateSchema2() { 154 assertEquals("global elements",GE_SIZE,getSchemaModel().getSchema().getElements().size()); 155 Element element = axiModel.getComponentFactory().createElement(); 156 element.setName("NewElement"+axiModel.getRoot().getElements().size()); 157 158 axiModel.startTransaction(); 159 try { 160 axiModel.getRoot().addElement(element); 161 } finally { 162 axiModel.endTransaction(); 163 } 164 assertEquals("global elements",GE_SIZE+1,getSchemaModel().getSchema().getElements().size()); 165 166 174 axiModel.startTransaction(); 175 try { 176 axiModel.getRoot().removeElement(element); 177 } finally { 178 axiModel.endTransaction(); 179 } 180 assertEquals("global elements",GE_SIZE,getSchemaModel().getSchema().getElements().size()); 181 validateSchema(axiModel.getSchemaModel()); 182 } 190 191 192 public void testDeleteExistingGlobalElement() { 193 assertEquals("global elements",GE_SIZE,getSchemaModel().getSchema().getElements().size()); 194 Element element = axiModel.getComponentFactory().createElement(); 195 element.setName("NewElement"+axiModel.getRoot().getElements().size()); 196 axiModel.startTransaction(); 198 try { 199 for(Element e:axiModel.getRoot().getElements()) 200 if(e.getName().equals("CancellationStatus2")) 201 axiModel.getRoot().removeElement(e); 202 } finally { 203 axiModel.endTransaction(); 204 } 205 assertEquals("global elements",GE_SIZE-1,getSchemaModel().getSchema().getElements().size()); 206 validateSchema(axiModel.getSchemaModel()); 207 } 215 216 public void testDeleteExistingLocalElement() { 217 assertEquals("global elements",GE_SIZE-1,getSchemaModel().getSchema().getElements().size()); 218 Element element = axiModel.getComponentFactory().createElement(); 219 element.setName("NewElement"+axiModel.getRoot().getElements().size()); 220 axiModel.startTransaction(); 221 for(Element e:axiModel.getRoot().getElements()) { 222 if(e.getName().equals(GLOBAL_ELEMENT)) { 223 Element del = null; 224 for(AXIComponent e2: e.getCompositor().getChildren()) { 225 if(e2 instanceof Element) { 226 if(((Element)e2).getName().equals("Errors_")) { 227 del = (Element) e2; 228 } 229 } 230 } 231 if(del != null) 232 e.getCompositor().removeElement(del); 233 } 234 } 235 axiModel.endTransaction(); 236 assertEquals("global elements",GE_SIZE-1,getSchemaModel().getSchema().getElements().size()); 237 validateSchema(axiModel.getSchemaModel()); 238 239 } 247 248 public void testSimpleToComplexContent() { 249 assertEquals("global elements",GE_SIZE-1,getSchemaModel().getSchema().getElements().size()); 250 Element newElem = axiModel.getComponentFactory().createElement(); 251 newElem.setName("City"); 252 Attribute newAttr = axiModel.getComponentFactory().createAttribute(); 253 newAttr.setName("a1"); 254 255 GlobalElement ge2 = null; 256 Element e2 = null; 257 for(Element e:axiModel.getRoot().getElements()) { 258 if(e.getName().equals("MyAddress")) { 259 e2 = e; 260 ge2 = (GlobalElement) e.getPeer(); 261 } 262 } 263 assertTrue("complexcontent", 264 ((GlobalComplexType)ge2.getType().get()).getDefinition() instanceof SimpleContent); 265 assertEquals("global Complex types",4,getSchemaModel().getSchema().getComplexTypes().size()); 266 267 axiModel.startTransaction(); 268 e2.addElement(newElem); 269 e2.addAttribute(newAttr); 270 axiModel.endTransaction(); 271 272 assertEquals("global elements",GE_SIZE-1,getSchemaModel().getSchema().getElements().size()); 273 assertEquals("global Complex types",4,getSchemaModel().getSchema().getComplexTypes().size()); 274 assertEquals("local element",1,e2.getChildElements().size()); 275 assertEquals("local attr",2,e2.getAttributes().size()); 276 assertNotNull("complexcontent", ge2.getType()); 277 org.netbeans.modules.xml.schema.model.Sequence seq = 278 (org.netbeans.modules.xml.schema.model.Sequence) 279 ((GlobalComplexType)ge2.getType().get()).getDefinition(); 280 assertNotNull("sequence", seq); 281 assertEquals("complexcontent attr size", 2, 282 ((GlobalComplexType)ge2.getType().get()).getLocalAttributes().size()); 283 validateSchema(axiModel.getSchemaModel()); 284 285 } 301 302 public void testCreateSimpleContent() { 303 assertEquals("global elements",GE_SIZE-1,getSchemaModel().getSchema().getElements().size()); 304 Element newElem = axiModel.getComponentFactory().createElement(); 305 newElem.setName("e1"); 306 307 Element newElem2 = axiModel.getComponentFactory().createElement(); 308 newElem2.setName("e2"); 309 310 axiModel.startTransaction(); 311 axiModel.getRoot().addElement(newElem); 312 axiModel.endTransaction(); 313 314 GlobalElement ge2 = (GlobalElement) newElem.getPeer(); 315 316 axiModel.startTransaction(); 317 newElem.setType(new BooleanType()); 318 axiModel.endTransaction(); 319 320 assertTrue("complexcontent",ge2.getType().get() instanceof GlobalSimpleType); 321 assertEquals("global elements",GE_SIZE,getSchemaModel().getSchema().getElements().size()); 322 validateSchema(axiModel.getSchemaModel()); 323 324 Attribute newAttr = axiModel.getComponentFactory().createAttribute(); 325 newAttr.setName("a1"); 326 327 axiModel.startTransaction(); 328 newElem.addAttribute(newAttr); 329 axiModel.endTransaction(); 330 assertNull("simplecontent", ge2.getType()); 331 SimpleContent newsc = (SimpleContent) ((LocalComplexType)ge2.getInlineType()).getDefinition(); 332 assertNotNull("simplecontent", newsc); 333 SimpleExtension se = (SimpleExtension) newsc.getLocalDefinition(); 334 assertNotNull("simplecontent", se); 335 assertNotNull("simplecontent", se.getBase().get() instanceof GlobalSimpleType); 336 assertEquals("simplecontent attr size", 1, se.getLocalAttributes().size()); 337 338 axiModel.startTransaction(); 339 newElem.removeAttribute(newAttr); 340 axiModel.endTransaction(); 341 assertNull("simplecontent", ge2.getType()); 342 newsc = (SimpleContent) ((LocalComplexType)ge2.getInlineType()).getDefinition(); 343 assertNotNull("simplecontent", newsc); 344 se = (SimpleExtension) newsc.getLocalDefinition(); 345 assertNotNull("simplecontent", se); 346 assertNotNull("simplecontent", se.getBase().get() instanceof GlobalSimpleType); 347 assertEquals("simplecontent attr size", 0, se.getLocalAttributes().size()); 348 } 349 } 350 | Popular Tags |