1 19 20 package org.netbeans.modules.xml.axi; 21 22 import javax.swing.text.BadLocationException ; 23 import javax.swing.text.Document ; 24 import junit.framework.*; 25 import org.netbeans.modules.xml.axi.Compositor.CompositorType; 26 import org.netbeans.modules.xml.axi.datatype.Base64BinaryType; 27 import org.netbeans.modules.xml.axi.datatype.DateType; 28 import org.netbeans.modules.xml.axi.datatype.IntegerType; 29 import org.netbeans.modules.xml.schema.model.Form; 30 import org.netbeans.modules.xml.schema.model.SchemaModel; 31 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 32 33 34 38 public class DesignPatternTest extends AbstractTestCase { 39 40 public static final String LINE_RUSSIAN_DOLL_XSD = "resources/line_RussianDoll.xsd"; 41 public static final String LINE_VENETIAN_BLIND_XSD = "resources/line_VenetianBlind.xsd"; 42 public static final String LINE_SALAMI_SLICE_XSD = "resources/line_SalamiSlice.xsd"; 43 public static final String LINE_GARDEN_OF_EDEN_XSD = "resources/line_GardenOfEden.xsd"; 44 public static final String LOANAPP_XSD = "resources/LoanApplication.xsd"; 45 public static final String EMPTY_XSD = "resources/empty.xsd"; 46 public static final String ADDRESS3_XSD = "resources/address3.xsd"; 47 public static final String GLOBAL_ELEMENT = "Line"; 48 49 private Document doc = null; 50 51 public DesignPatternTest(String testName) { 52 super(testName, LINE_RUSSIAN_DOLL_XSD, GLOBAL_ELEMENT); 53 } 54 55 public static Test suite() { 56 TestSuite suite = new TestSuite(); 58 suite.addTest(new DesignPatternTest("testLineGardenOfEden")); 59 suite.addTest(new DesignPatternTest("testLineSalamiSlice")); 60 suite.addTest(new DesignPatternTest("testLineVenetianBlind")); 61 suite.addTest(new DesignPatternTest("testLineRussianDoll")); 62 suite.addTest(new DesignPatternTest("testEmpty")); 64 return suite; 66 } 67 68 public void testChangeCompositor() { 69 print("testChangeCompositor"); 70 try { 71 loadModel(ADDRESS3_XSD); 72 } catch (Exception ex) { 73 assertTrue("Should not be here", false); 75 } 76 77 ContentModel cm = findContentModel("USAddress"); 78 assert(cm != null); 79 Compositor comp = (Compositor)cm.getChildren().get(0); 80 assert(comp != null); 81 assert(comp.getChildren().size() == 5); 82 Element e1 = (Element)comp.getChildren().get(0); 83 assert(e1.isReference()); 84 getAXIModel().startTransaction(); 85 comp.setType(CompositorType.ALL); 86 getAXIModel().endTransaction(); 87 assert(comp != null); 88 assert(comp.getChildren().size() == 5); 89 e1 = (Element)comp.getChildren().get(0); 90 assert(e1.isReference()); 91 } 92 93 public void testLineRussianDoll() { 94 print("testLineRussianDoll"); 95 try { 96 loadModel(LINE_RUSSIAN_DOLL_XSD); 97 } catch (Exception ex) { 98 assertTrue("Should not be here", false); 100 } 101 assertEquals("global complex types",0,getSchemaModel().getSchema().getComplexTypes().size()); 102 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 103 assertEquals("global elements",1,getSchemaModel().getSchema().getElements().size()); 104 105 axiModel.setSchemaDesignPattern(SchemaGenerator.Pattern.RUSSIAN_DOLL); 106 createLine(); 107 108 assertEquals("global complex types",0,getSchemaModel().getSchema().getComplexTypes().size()); 110 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 111 assertEquals("global elements",2,getSchemaModel().getSchema().getElements().size()); 112 } 113 114 public void testLineVenetianBlind() { 115 print("testLineVenetianBlind"); 116 try { 117 loadModel(LINE_VENETIAN_BLIND_XSD); 118 } catch (Exception ex) { 119 assertTrue("Should not be here", false); 121 } 122 assertEquals("global complex types",1,getSchemaModel().getSchema().getComplexTypes().size()); 123 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 124 assertEquals("global elements",1,getSchemaModel().getSchema().getElements().size()); 125 126 axiModel.setSchemaDesignPattern(SchemaGenerator.Pattern.VENITIAN_BLIND); 127 createLine(); 128 129 assertEquals("global complex types",3,getSchemaModel().getSchema().getComplexTypes().size()); 131 assertEquals("global elements",2,getSchemaModel().getSchema().getElements().size()); 134 135 } 140 141 public void testLineSalamiSlice() { 142 print("testLineSalamiSlice"); 143 try { 144 loadModel(LINE_SALAMI_SLICE_XSD); 145 } catch (Exception ex) { 146 assertTrue("Should not be here", false); 148 } 149 assertEquals("global complex types",0,getSchemaModel().getSchema().getComplexTypes().size()); 150 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 151 assertEquals("global elements",3,getSchemaModel().getSchema().getElements().size()); 152 153 axiModel.setSchemaDesignPattern(SchemaGenerator.Pattern.SALAMI_SLICE); 154 createLine(); 155 156 assertEquals("global complex types",0,getSchemaModel().getSchema().getComplexTypes().size()); 158 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 159 } 162 163 public void testLineGardenOfEden() { 164 print("testLineGardenOfEden"); 165 try { 166 loadModel(LINE_GARDEN_OF_EDEN_XSD); 167 } catch (Exception ex) { 168 assertTrue("Should not be here", false); 170 } 171 assertEquals("global complex types",2,getSchemaModel().getSchema().getComplexTypes().size()); 172 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 173 assertEquals("global elements",3,getSchemaModel().getSchema().getElements().size()); 174 175 axiModel.setSchemaDesignPattern(SchemaGenerator.Pattern.GARDEN_OF_EDEN); 176 createLine(); 177 178 assertEquals("global complex types",5,getSchemaModel().getSchema().getComplexTypes().size()); 180 185 } 190 191 private void createLine() { 192 axiModel.startTransaction(); 193 try { 194 Element element = axiModel.getComponentFactory().createElement(); 196 element.setName("Line1"); 197 axiModel.getRoot().addElement(element); 198 199 Compositor seq = axiModel.getComponentFactory().createSequence(); 201 element.addCompositor(seq); 202 203 Element point = axiModel.getComponentFactory().createElement(); 205 point.setName("Point1"); 206 207 seq.addElement(point); 209 210 Attribute x = axiModel.getComponentFactory().createAttribute(); 212 x.setName("x"); 213 point.addAttribute(x); 214 Base64BinaryType b = new Base64BinaryType(); 215 b.addEnumeration("XYZ"); 216 x.setType(b); 217 Attribute y = axiModel.getComponentFactory().createAttribute(); 219 y.setName("y"); 220 point.addAttribute(y); 221 222 Compositor seq2 = axiModel.getComponentFactory().createSequence(); 224 point.addCompositor(seq2); 225 226 Compositor c = axiModel.getComponentFactory().createChoice(); 228 seq2.addCompositor(c); 229 230 Element info1 = axiModel.getComponentFactory().createElement(); 232 info1.setName("Info1"); 233 c.addElement(info1); 234 Element info2 = axiModel.getComponentFactory().createElement(); 236 info2.setName("Info2"); 237 c.addElement(info2); 238 239 Element ce3 = axiModel.getComponentFactory().createElement(); 241 ce3.setName("Info3"); 242 DateType d = new DateType(); 243 d.addEnumeration("00:00:00"); 244 ce3.setType(d); 245 seq2.addElement(ce3); 246 247 Compositor seq11 = axiModel.getComponentFactory().createSequence(); 249 info1.addCompositor(seq11); 250 Element info11 = axiModel.getComponentFactory().createElement(); 252 info11.setName("Info11"); 253 seq11.addElement(info11); 254 } finally { 255 axiModel.endTransaction(); 256 } 257 } 258 259 public void testLoanApp() { 260 print("testLoanApp"); 261 try { 262 loadModel(LOANAPP_XSD); 263 } catch (Exception ex) { 264 assertTrue("Should not be here", false); 266 } 267 assertEquals("global complex types",7,getSchemaModel().getSchema().getComplexTypes().size()); 268 assertEquals("global simple types",2,getSchemaModel().getSchema().getSimpleTypes().size()); 269 assertEquals("global elements",1,getSchemaModel().getSchema().getElements().size()); 270 271 axiModel.setSchemaDesignPattern(SchemaGenerator.Pattern.VENITIAN_BLIND); 272 ModifyLoanApp(); 273 274 assertEquals("global complex types",9,getSchemaModel().getSchema().getComplexTypes().size()); 276 assertEquals("global simple types",6,getSchemaModel().getSchema().getSimpleTypes().size()); 277 assertEquals("global elements",1,getSchemaModel().getSchema().getElements().size()); 278 } 279 280 private void ModifyLoanApp() { 281 axiModel.startTransaction(); 282 try { 283 Element tradeIn = (Element) ((Compositor)axiModel.getRoot(). 285 getElements().get(0).getCompositor()). 286 getChildren().get(6); 287 Element vehicle = (Element)((Compositor)tradeIn.getCompositor()).getChildren().get(0); 288 Element vehicleYear = (Element) ((Compositor)vehicle.getCompositor()).getChildren().get(0); 289 290 Attribute x = axiModel.getComponentFactory().createAttribute(); 292 x.setName("attr1"); 293 vehicleYear.addAttribute(x); 294 Base64BinaryType b1 = new Base64BinaryType(); 295 b1.addEnumeration("XYZ"); 296 x.setType(b1); 297 298 Attribute y = axiModel.getComponentFactory().createAttribute(); 299 y.setName("attr2"); 300 vehicleYear.addAttribute(y); 301 Base64BinaryType b2 = new Base64BinaryType(); 302 b2.addEnumeration("ABC"); 303 y.setType(b2); 304 305 Compositor seq = axiModel.getComponentFactory().createSequence(); 307 vehicleYear.addCompositor(seq); 308 309 Element gregorian = axiModel.getComponentFactory().createElement(); 311 gregorian.setName("Gregorian"); 312 313 seq.addElement(gregorian); 315 316 gregorian.setAbstract(true); 317 gregorian.setBlock("testBlock"); 318 gregorian.setDefault("testDefault"); 319 gregorian.setFinal("testFinal"); 320 gregorian.setFixed("testFixed"); 321 gregorian.setForm(Form.QUALIFIED); 322 gregorian.setMinOccurs("3"); 323 gregorian.setMaxOccurs("unbounded"); 324 gregorian.setNillable(null); 325 gregorian.setType(new IntegerType()); 326 327 Element hijri = axiModel.getComponentFactory().createElement(); 329 hijri.setName("Hijri"); 330 331 seq.addElement(hijri); 333 334 hijri.setAbstract(true); 335 hijri.setBlock("testBlock"); 336 hijri.setDefault("testDefault"); 337 hijri.setFinal("testFinal"); 338 hijri.setFixed("testFixed"); 339 hijri.setForm(Form.UNQUALIFIED); 340 hijri.setMinOccurs("0"); 341 hijri.setMaxOccurs("1"); 342 hijri.setNillable(true); 343 344 ContentModel ct = axiModel.getComponentFactory().createComplexType(); 345 ct.setName("newType"); 346 axiModel.getRoot().addContentModel(ct); 347 Attribute a1 = axiModel.getComponentFactory().createAttribute(); 348 a1.setName("newAttr"); 349 ct.addAttribute(a1); 350 hijri.setType(ct); 351 } finally { 352 axiModel.endTransaction(); 353 } 354 } 355 356 357 public void testEmpty() { 358 print("testEmpty"); 359 try { 360 loadModel(EMPTY_XSD); 361 } catch (Exception ex) { 362 assertTrue("Should not be here", false); 364 } 365 assertEquals("global complex types",0,getSchemaModel().getSchema().getComplexTypes().size()); 366 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 367 assertEquals("global elements",0,getSchemaModel().getSchema().getElements().size()); 368 369 axiModel.setSchemaDesignPattern(SchemaGenerator.Pattern.GARDEN_OF_EDEN); 370 addGlobalElementAndChildElement(); 371 372 assertEquals("global complex types",3,getSchemaModel().getSchema().getComplexTypes().size()); 374 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 375 assertEquals("global elements",4,getSchemaModel().getSchema().getElements().size()); 376 377 axiModel.setSchemaDesignPattern(SchemaGenerator.Pattern.VENITIAN_BLIND); 378 addGlobalElementAndChildElement(); 379 380 assertEquals("global complex types",5,getSchemaModel().getSchema().getComplexTypes().size()); 382 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 383 assertEquals("global elements",5,getSchemaModel().getSchema().getElements().size()); 384 385 axiModel.setSchemaDesignPattern(SchemaGenerator.Pattern.SALAMI_SLICE); 386 addGlobalElementAndChildElement(); 387 388 assertEquals("global complex types",5,getSchemaModel().getSchema().getComplexTypes().size()); 390 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 391 assertEquals("global elements",9,getSchemaModel().getSchema().getElements().size()); 392 393 axiModel.setSchemaDesignPattern(SchemaGenerator.Pattern.RUSSIAN_DOLL); 394 addGlobalElementAndChildElement(); 395 396 assertEquals("global complex types",5,getSchemaModel().getSchema().getComplexTypes().size()); 398 assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size()); 399 assertEquals("global elements",10,getSchemaModel().getSchema().getElements().size()); 400 } 401 402 private void addGlobalElementAndChildElement() { 403 axiModel.startTransaction(); 404 try { 405 Element element = axiModel.getComponentFactory().createElement(); 407 element.setName("purchaseOrder"); 408 axiModel.getRoot().addElement(element); 409 410 Compositor seq = axiModel.getComponentFactory().createSequence(); 412 element.addCompositor(seq); 413 414 Element billTo = axiModel.getComponentFactory().createElement(); 416 billTo.setName("billTo"); 417 418 seq.addElement(billTo); 420 421 Compositor seq1 = axiModel.getComponentFactory().createSequence(); 423 billTo.addCompositor(seq1); 424 425 Element name = axiModel.getComponentFactory().createElement(); 427 name.setName("name"); 428 429 seq1.addElement(name); 431 432 Compositor seq2 = axiModel.getComponentFactory().createSequence(); 434 name.addCompositor(seq2); 435 436 Element firstName = axiModel.getComponentFactory().createElement(); 438 firstName.setName("firstName"); 439 440 seq2.addElement(firstName); 442 } finally { 443 axiModel.endTransaction(); 444 } 445 } 446 447 private void printDocument() { 448 try { 449 SchemaModel sm = getSchemaModel(); 450 doc = ((AbstractDocumentModel)sm).getBaseDocument(); 451 print("doc: "+doc.getText(0, doc.getLength())); 452 } catch (BadLocationException ex) { 453 assertTrue("Should not be here", false); 455 } 456 } 457 } 458 | Popular Tags |