KickJava   Java API By Example, From Geeks To Geeks.

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


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.Document JavaDoc;
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 /**
41  *
42  * @author Ayub Khan
43  */

44 public class SchemaGeneratorTest extends AbstractTestCase {
45     
46     public static final String JavaDoc TEST_XSD = "resources/OTA_TI_simple.xsd";
47     public static final String JavaDoc GLOBAL_ELEMENT = "OTA_TravelItineraryRS";
48     
49     private Document JavaDoc doc = null;
50     
51     public static final int GE_SIZE = 4;
52     
53     public SchemaGeneratorTest(String JavaDoc testName) {
54         super(testName, TEST_XSD, GLOBAL_ELEMENT);
55     }
56     
57     protected void setUp() throws Exception JavaDoc {
58         super.setUp();
59     }
60     
61     protected void tearDown() throws Exception JavaDoc {
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 JavaDoc {
73         generateSchema();
74     }
75     
76     /**
77      * Test of createElement method, of class org.netbeans.modules.xml.axi.XAMFactory.
78      */

79     public void generateSchema() throws Exception JavaDoc {
80         Element element = globalElement;
81         assertNotNull(element);
82         SchemaModel sm = null;
83         sm = getSchemaModel();
84         doc = ((AbstractDocumentModel)sm).getBaseDocument();
85         //DefaultSchemaGenerator schemaGenerator = new DefaultSchemaGenerator(getAXIModel());
86

87         assertEquals("global elements",GE_SIZE,getSchemaModel().getSchema().getElements().size());
88         //global element name change
89
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).//complexType
102
getChildren().get(0).//all
103
getChildren().get(0)).//element[date]
104
getType().getQName().getLocalPart(), "date");
105             }
106         }
107         assertTrue("found CancellationStatus_", found);
108         //System.out.println("doc: "+doc.getText(0, doc.getLength()));
109

110         //check an attribute change is flushed to schema
111
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         //System.out.println("doc: "+doc.getText(0, doc.getLength()));
129
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 // try {
167
// SchemaModel sm = getSchemaModel();
168
// doc = ((AbstractDocumentModel)sm).getBaseDocument();
169
// System.out.println("doc: "+doc.getText(0, doc.getLength()));
170
// } catch (BadLocationException ex) {
171
// ex.printStackTrace();
172
// }
173

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 // try {
183
// SchemaModel sm = getSchemaModel();
184
// doc = ((AbstractDocumentModel)sm).getBaseDocument();
185
// System.out.println("doc: "+doc.getText(0, doc.getLength()));
186
// } catch (BadLocationException ex) {
187
// ex.printStackTrace();
188
// }
189
}
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         //global element name change
197
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 // try {
208
// SchemaModel sm = getSchemaModel();
209
// doc = ((AbstractDocumentModel)sm).getBaseDocument();
210
// System.out.println("doc: "+doc.getText(0, doc.getLength()));
211
// } catch (BadLocationException ex) {
212
// ex.printStackTrace();
213
// }
214
}
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 // try {
240
// SchemaModel sm = getSchemaModel();
241
// doc = ((AbstractDocumentModel)sm).getBaseDocument();
242
// System.out.println("doc: "+doc.getText(0, doc.getLength()));
243
// } catch (BadLocationException ex) {
244
// ex.printStackTrace();
245
// }
246
}
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         //FIXME: Exception on AXIComponent.removeChild()
286
// axiModel.startTransaction();
287
// e2.removeElement(newElem);
288
// e2.removeAttribute(newAttr);
289
// axiModel.endTransaction();
290
//
291
// assertEquals("global elements",GE_SIZE-1,getSchemaModel().getSchema().getElements().size());
292
// assertNotNull("complexcontent", ge2.getType());
293
// newcc = (ComplexContent) ((GlobalComplexType)ge2.getType().get()).getDefinition();
294
// assertNotNull("simplecontent", newcc);
295
// ccr = (ComplexContentRestriction) newcc.getLocalDefinition();
296
// assertNotNull("complexcontent", ccr);
297
// assertNotNull("complexcontent", ccr.getBase().get() instanceof GlobalSimpleType);
298
// assertEquals("complexcontent attr size", 1, ccr.getLocalAttributes().size());
299
// validateSchema(axiModel.getSchemaModel());
300
}
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