KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > DesignPatternTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.axi;
21
22 import javax.swing.text.BadLocationException JavaDoc;
23 import javax.swing.text.Document JavaDoc;
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 /**
35  *
36  * @author Ayub Khan
37  */

38 public class DesignPatternTest extends AbstractTestCase {
39     
40     public static final String JavaDoc LINE_RUSSIAN_DOLL_XSD = "resources/line_RussianDoll.xsd";
41     public static final String JavaDoc LINE_VENETIAN_BLIND_XSD = "resources/line_VenetianBlind.xsd";
42     public static final String JavaDoc LINE_SALAMI_SLICE_XSD = "resources/line_SalamiSlice.xsd";
43     public static final String JavaDoc LINE_GARDEN_OF_EDEN_XSD = "resources/line_GardenOfEden.xsd";
44     public static final String JavaDoc LOANAPP_XSD = "resources/LoanApplication.xsd";
45     public static final String JavaDoc EMPTY_XSD = "resources/empty.xsd";
46     public static final String JavaDoc ADDRESS3_XSD = "resources/address3.xsd";
47     public static final String JavaDoc GLOBAL_ELEMENT = "Line";
48     
49     private Document JavaDoc doc = null;
50     
51     public DesignPatternTest(String JavaDoc testName) {
52         super(testName, LINE_RUSSIAN_DOLL_XSD, GLOBAL_ELEMENT);
53     }
54     
55     public static Test suite() {
56 // TestSuite suite = new TestSuite(DesignPatternTest.class);
57
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("testLoanApp"));
63
suite.addTest(new DesignPatternTest("testEmpty"));
64         //suite.addTest(new DesignPatternTest("testChangeCompositor"));
65
return suite;
66     }
67     
68     public void testChangeCompositor() {
69         print("testChangeCompositor");
70         try {
71             loadModel(ADDRESS3_XSD);
72         } catch (Exception JavaDoc ex) {
73             //ex.printStackTrace();
74
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 JavaDoc ex) {
98             //ex.printStackTrace();
99
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 // printDocument();
109
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 JavaDoc ex) {
119             //ex.printStackTrace();
120
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 // printDocument();
130
assertEquals("global complex types",3,getSchemaModel().getSchema().getComplexTypes().size());
131         //FIXME should be 1 instead of 2
132
// assertEquals("global simple types",1,getSchemaModel().getSchema().getSimpleTypes().size());
133
assertEquals("global elements",2,getSchemaModel().getSchema().getElements().size());
134         
135         //FIXME gives 1 instead of 2
136
// assertEquals("AXI content models",2,axiModel.getRoot().getContentModels().size());
137
//FIXME gives 4 instead of 2
138
// assertEquals("AXI global elements",2,axiModel.getRoot().getElements().size());
139
}
140     
141     public void testLineSalamiSlice() {
142         print("testLineSalamiSlice");
143         try {
144             loadModel(LINE_SALAMI_SLICE_XSD);
145         } catch (Exception JavaDoc ex) {
146             //ex.printStackTrace();
147
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         //printDocument();
157
assertEquals("global complex types",0,getSchemaModel().getSchema().getComplexTypes().size());
158         assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size());
159         //FIXME gives 8 instead of 9
160
// assertEquals("global elements",9,getSchemaModel().getSchema().getElements().size());
161
}
162     
163     public void testLineGardenOfEden() {
164         print("testLineGardenOfEden");
165         try {
166             loadModel(LINE_GARDEN_OF_EDEN_XSD);
167         } catch (Exception JavaDoc ex) {
168             //ex.printStackTrace();
169
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 // printDocument();
179
assertEquals("global complex types",5,getSchemaModel().getSchema().getComplexTypes().size());
180         //FIXME should be 1 instead of 2
181
// assertEquals("global simple types",1,getSchemaModel().getSchema().getSimpleTypes().size());
182
//FIXME gives 8 instead of 9
183
// assertEquals("global elements",9,getSchemaModel().getSchema().getElements().size());
184

185         //FIXME gives 2 instead of 3
186
// assertEquals("AXI content models",3,axiModel.getRoot().getContentModels().size());
187
//FIXME gives 4 instead of 9
188
// assertEquals("AXI global elements",9,axiModel.getRoot().getElements().size());
189
}
190     
191     private void createLine() {
192         axiModel.startTransaction();
193         try {
194             //Add Line to root
195
Element element = axiModel.getComponentFactory().createElement();
196             element.setName("Line1");
197             axiModel.getRoot().addElement(element);
198             
199             //Add Sequence to Line
200
Compositor seq = axiModel.getComponentFactory().createSequence();
201             element.addCompositor(seq);
202             
203             //Create Point
204
Element point = axiModel.getComponentFactory().createElement();
205             point.setName("Point1");
206             
207             //Add Point to Line
208
seq.addElement(point);
209             
210             //Create and attribute x to Point
211
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             //Create and attribute y to Point
218
Attribute y = axiModel.getComponentFactory().createAttribute();
219             y.setName("y");
220             point.addAttribute(y);
221             
222             //Add Sequence to Point
223
Compositor seq2 = axiModel.getComponentFactory().createSequence();
224             point.addCompositor(seq2);
225             
226             //Add Choice to Sequence
227
Compositor c = axiModel.getComponentFactory().createChoice();
228             seq2.addCompositor(c);
229             
230             //Add Info1 to Choice
231
Element info1 = axiModel.getComponentFactory().createElement();
232             info1.setName("Info1");
233             c.addElement(info1);
234             //Add Info2 to Choice
235
Element info2 = axiModel.getComponentFactory().createElement();
236             info2.setName("Info2");
237             c.addElement(info2);
238             
239             //Add Info3 to Sequence
240
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             //Add Sequence to Info1
248
Compositor seq11 = axiModel.getComponentFactory().createSequence();
249             info1.addCompositor(seq11);
250             //Add Info11 to Sequence
251
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 JavaDoc ex) {
264             //ex.printStackTrace();
265
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 // printDocument();
275
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             //Add Line to root
284
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             //Create and attribute x to Point
291
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             //Add Sequence to year
306
Compositor seq = axiModel.getComponentFactory().createSequence();
307             vehicleYear.addCompositor(seq);
308             
309             //Create Gregorian
310
Element gregorian = axiModel.getComponentFactory().createElement();
311             gregorian.setName("Gregorian");
312             
313             //Add Gregorian to year
314
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             //Create Hijri
328
Element hijri = axiModel.getComponentFactory().createElement();
329             hijri.setName("Hijri");
330             
331             //Add Hijri to year
332
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 JavaDoc ex) {
362             //ex.printStackTrace();
363
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 // printDocument();
373
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 // printDocument();
381
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 // printDocument();
389
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 // printDocument();
397
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             //Add purchaseOrder to root
406
Element element = axiModel.getComponentFactory().createElement();
407             element.setName("purchaseOrder");
408             axiModel.getRoot().addElement(element);
409             
410             //Add Sequence to purchaseOrder
411
Compositor seq = axiModel.getComponentFactory().createSequence();
412             element.addCompositor(seq);
413             
414             //Create billTo
415
Element billTo = axiModel.getComponentFactory().createElement();
416             billTo.setName("billTo");
417             
418             //Add billTo to purchaseOrder
419
seq.addElement(billTo);
420             
421             //Add Sequence to purchaseOrder
422
Compositor seq1 = axiModel.getComponentFactory().createSequence();
423             billTo.addCompositor(seq1);
424             
425             //Create name
426
Element name = axiModel.getComponentFactory().createElement();
427             name.setName("name");
428             
429             //Add billTo to purchaseOrder
430
seq1.addElement(name);
431             
432             //Add Sequence to purchaseOrder
433
Compositor seq2 = axiModel.getComponentFactory().createSequence();
434             name.addCompositor(seq2);
435             
436             //Create firstName
437
Element firstName = axiModel.getComponentFactory().createElement();
438             firstName.setName("firstName");
439             
440             //Add billTo to purchaseOrder
441
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 JavaDoc ex) {
453             //ex.printStackTrace();
454
assertTrue("Should not be here", false);
455         }
456     }
457 }
458
Popular Tags