KickJava   Java API By Example, From Geeks To Geeks.

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


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.schema.model.AttributeGroupReference;
25 import org.netbeans.modules.xml.schema.model.Choice;
26 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
27 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
28 import org.netbeans.modules.xml.schema.model.GlobalElement;
29 import org.netbeans.modules.xml.schema.model.LocalAttribute;
30 import org.netbeans.modules.xml.schema.model.LocalComplexType;
31 import org.netbeans.modules.xml.schema.model.LocalElement;
32 import org.netbeans.modules.xml.schema.model.SchemaModel;
33 import org.netbeans.modules.xml.schema.model.SimpleExtension;
34 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
35
36
37 /**
38  *
39  * @author Ayub Khan
40  */

41 public class SchemaGeneratorPerfTest extends AbstractTestCase {
42     
43     public static final String JavaDoc TEST_XSD = "resources/OTA_TI_simple.xsd";//"resources/OTA_TravelItinerary.xsd";
44
public static final String JavaDoc GLOBAL_ELEMENT = "OTA_TravelItineraryRS";
45     
46     private Document JavaDoc doc = null;
47     
48     public SchemaGeneratorPerfTest(String JavaDoc testName) {
49         super(testName, TEST_XSD, GLOBAL_ELEMENT);
50     }
51     
52     protected void setUp() throws Exception JavaDoc {
53         super.setUp();
54     }
55     
56     public static Test suite() {
57         TestSuite suite = new TestSuite(SchemaGeneratorPerfTest.class);
58         
59         return suite;
60     }
61     
62     public void testGenerateSchema() throws Exception JavaDoc {
63         generateSchema();
64     }
65     
66     /**
67      * Test of createElement method, of class org.netbeans.modules.xml.axi.XAMFactory.
68      */

69     public void generateSchema() throws Exception JavaDoc {
70         Element element = globalElement;
71         assertNotNull(element);
72         SchemaModel sm = null;
73         sm = getSchemaModel();
74         doc = ((AbstractDocumentModel)sm).getBaseDocument();
75         //DefaultSchemaGenerator schemaGenerator = new DefaultSchemaGenerator(getAXIModel());
76

77         //global element name change
78
axiModel.startTransaction();
79         for(Element e:axiModel.getRoot().getElements())
80             if(e.getName().equals("CancellationStatus"))
81                 e.setName(e.getName()+"_");
82         long startTime = System.currentTimeMillis();
83         axiModel.endTransaction();
84         long endTime = System.currentTimeMillis();
85         print("Time taken to flush: "+(endTime-startTime)+" ms");
86 // for(GlobalElement ge:sm.getSchema().getElements()) {
87
// if(ge.getName().startsWith("CancellationStatus")) {
88
// assertEquals("updated schemamodel", ge.getName(), "CancellationStatus_");
89
// assertEquals("updated schemamodel type", ge.getType().getQName().getLocalPart(), "boolean");
90
// }
91
// }
92
//print("doc: "+doc.getText(0, doc.getLength()));
93

94         //Local element name change
95
/*axiModel.startTransaction();
96                     for(Element e:axiModel.getElements()) {
97                             if(e.getName().equals("OTA_TravelItineraryRS")) {
98                                     for(AXIComponent e2: e.getCompositor().getChildren()) {
99                                             if(e2 instanceof Element) {
100                                                     if(((Element)e2).getName().equals("Errors"))
101                                                             ((Element)e2).setName(((Element)e2).getName()+"_");
102                                             }
103                                     }
104                             }
105                     }
106                     axiModel.endTransaction();
107                      
108                     boolean found = false;
109                     for(GlobalElement ge:sm.getSchema().getElements()) {
110                             if(ge.getName().startsWith("OTA_TravelItineraryRS")) {
111                                     assertEquals("updated schemamodel",
112                                             ((LocalElement)ge.getChildren().get(0).getChildren().get(0).
113                                                     getChildren().get(1)).getName(), "Errors_");
114                                     found = true;
115                             }
116                     }
117                     assertTrue("Should have verified updated element", found);*/

118         
119         //check an attribute change is flushed to schema
120
axiModel.startTransaction();
121         for(Element e:axiModel.getRoot().getElements()) {
122             if(e.getName().equals(GLOBAL_ELEMENT)) {
123                 for(AXIComponent e2: e.getCompositor().getChildren()) {
124                     if(e2 instanceof Element) {
125                         if(((Element)e2).getName().equals("Errors")) {
126                             AbstractAttribute attr = ((Element)((Element)e2).getChildren().get(0).getChildren().get(0)).getAttributes().get(0);
127                             if(attr instanceof Attribute) {
128                                 ((Attribute)attr).setName("XYZ");
129                             }
130                         }
131                     }
132                 }
133             }
134         }
135         startTime = System.currentTimeMillis();
136         axiModel.endTransaction();
137         endTime = System.currentTimeMillis();
138         print("Time taken to flush: "+(endTime-startTime)+" ms");
139         
140         boolean found = false;
141         for(GlobalElement ge:sm.getSchema().getElements()) {
142             if(ge.getName().startsWith(GLOBAL_ELEMENT)) {
143                 LocalComplexType lct = (LocalComplexType) ge.getChildren().get(1);
144                 Choice choice = (Choice) lct.getChildren().get(0);
145                 LocalElement le = (LocalElement) choice.getChildren().get(1);
146                 GlobalComplexType gct = (GlobalComplexType) le.getType().get();
147                 le = (LocalElement) gct.getChildren().get(1).getChildren().get(0);
148                 gct = (GlobalComplexType) le.getType().get();
149                 SimpleExtension se = (SimpleExtension) gct.getChildren().get(1).getChildren().get(0);
150                 gct = (GlobalComplexType)se.getBase().get();
151                 AttributeGroupReference agr = (AttributeGroupReference)gct.getChildren().get(1).getChildren().get(0).getChildren().get(0);
152                 GlobalAttributeGroup gag = agr.getGroup().get();
153                 LocalAttribute la = (LocalAttribute)gag.getChildren().get(1);
154                 assertEquals("updated schemamodel", "XYZ", la.getName());
155                 found = true;
156             }
157         }
158         assertTrue("Should have verified updated element", found);
159         validateSchema(axiModel.getSchemaModel());
160     }
161     
162     public void testGenerateSchema2() {
163         assertEquals("global elements",SchemaGeneratorTest.GE_SIZE,getSchemaModel().getSchema().getElements().size());
164         Element element = axiModel.getComponentFactory().createElement();
165         element.setName("NewElement"+axiModel.getRoot().getElements().size());
166         
167         axiModel.startTransaction();
168         try {
169             axiModel.getRoot().addElement(element);
170         } finally {
171             axiModel.endTransaction();
172         }
173         assertEquals("global elements",SchemaGeneratorTest.GE_SIZE+1,getSchemaModel().getSchema().getElements().size());
174         
175 // try {
176
// SchemaModel sm = getSchemaModel();
177
// doc = ((AbstractDocumentModel)sm).getBaseDocument();
178
// print("doc: "+doc.getText(0, doc.getLength()));
179
// } catch (BadLocationException ex) {
180
// ex.printStackTrace();
181
// }
182

183         axiModel.startTransaction();
184         try {
185             axiModel.getRoot().removeElement(element);
186         } finally {
187             axiModel.endTransaction();
188         }
189         assertEquals("global elements",SchemaGeneratorTest.GE_SIZE,getSchemaModel().getSchema().getElements().size());
190         validateSchema(axiModel.getSchemaModel());
191 // try {
192
// SchemaModel sm = getSchemaModel();
193
// doc = ((AbstractDocumentModel)sm).getBaseDocument();
194
// print("doc: "+doc.getText(0, doc.getLength()));
195
// } catch (BadLocationException ex) {
196
// ex.printStackTrace();
197
// }
198
}
199     
200 }
201
Popular Tags