KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > sync > SyncElementTest


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.sync;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import junit.framework.*;
27 import org.netbeans.modules.xml.axi.Element;
28 import org.netbeans.modules.xml.schema.model.AttributeReference;
29 import org.netbeans.modules.xml.schema.model.ElementReference;
30 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
31 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
32 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
33 import org.netbeans.modules.xml.schema.model.GlobalElement;
34 import org.netbeans.modules.xml.schema.model.GlobalGroup;
35 import org.netbeans.modules.xml.schema.model.GlobalType;
36 import org.netbeans.modules.xml.schema.model.GroupReference;
37 import org.netbeans.modules.xml.schema.model.LocalAttribute;
38 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
39 import org.netbeans.modules.xml.schema.model.Sequence;
40
41
42 /**
43  * The unit test covers various use cases of sync on Element
44  * and ElementRef.
45  *
46  * @author Samaresh (Samaresh.Panda@Sun.Com)
47  */

48 public class SyncElementTest extends AbstractSyncTestCase {
49     
50     public static final String JavaDoc TEST_XSD = "resources/address.xsd";
51     public static final String JavaDoc GLOBAL_ELEMENT = "address";
52     
53     
54     /**
55      * SyncElementTest
56      */

57     public SyncElementTest(String JavaDoc testName) {
58     super(testName, TEST_XSD, GLOBAL_ELEMENT);
59     }
60     
61     public static Test suite() {
62     TestSuite suite = new TestSuite(SyncElementTest.class);
63     return suite;
64     }
65     
66     public void testElementType() throws Exception JavaDoc {
67     removeElementFromType();
68     removeAttributeFromAttrGroup();
69     changeType();
70     changeAttributeRef();
71     changeTypeContent();
72         changeNameOfElement();
73     //deleteGlobalType();
74
}
75     
76     public void testElement() throws Exception JavaDoc {
77     removeGlobalElement();
78     addGlobalElement();
79     changeElementRef();
80     }
81     
82     /**
83      * Removes an element from type "USAddress".
84      * Element count should be one less.
85      */

86     private void removeElementFromType() throws Exception JavaDoc {
87     Element address = findAXIGlobalElement("address");
88     int childCount = address.getChildElements().size();
89     assert(childCount == 3);
90     GlobalComplexType gct = findGlobalComplexType("USAddress");
91     getSchemaModel().startTransaction();
92     Sequence s = (Sequence)gct.getChildren().get(0);
93     s.removeContent(s.getContent().get(0));
94     getSchemaModel().endTransaction();
95     getAXIModel().sync();
96     childCount = address.getChildElements().size();
97     assert(childCount == 2);
98     }
99     
100     /**
101      * Removes an attribute from attribute group.
102      * child count should be one less.
103      */

104     private void removeAttributeFromAttrGroup() throws Exception JavaDoc {
105     Element address = findAXIGlobalElement("address");
106     int childCount = address.getChildren().size();
107     assert(childCount == 4);
108     GlobalAttributeGroup gag = findGlobalAttributeGroup("attr-group");
109     getSchemaModel().startTransaction();
110     LocalAttribute attr = (LocalAttribute)gag.getChildren().get(1);
111     gag.removeLocalAttribute(attr);
112     getSchemaModel().endTransaction();
113     getAXIModel().sync();
114     childCount = address.getChildren().size();
115     assert(childCount == 3);
116     }
117     
118     /**
119      * Change the type of element "address" from
120      * "USAddress" to "USAddress1".
121      */

122     private void changeType() throws Exception JavaDoc {
123     PropertyListener l = new PropertyListener();
124     getAXIModel().addPropertyChangeListener(l);
125     Element address = findAXIGlobalElement("address");
126     int childCount = address.getChildElements().size();
127     assert(childCount == 2);
128     GlobalElement ge = (GlobalElement)globalElement.getPeer();
129     getSchemaModel().startTransaction();
130     setType(ge, "USAddress1");
131     getSchemaModel().endTransaction();
132     getAXIModel().sync();
133     getAXIModel().removePropertyChangeListener(l);
134     childCount = address.getChildElements().size();
135     assert(childCount == 5);
136     }
137     
138     /**
139      * Change the content of "USAddress1".
140      */

141     private void changeTypeContent() throws Exception JavaDoc {
142     PropertyListener l = new PropertyListener();
143     getAXIModel().addPropertyChangeListener(l);
144     Element address = findAXIGlobalElement("address");
145     int childCount = address.getChildElements().size();
146     assert(childCount == 5);
147     getSchemaModel().startTransaction();
148     GlobalGroup gg = findGlobalGroup("group2");
149     GroupReference gr = (GroupReference)(findGlobalComplexType("USAddress1").getChildren().get(0));
150     NamedComponentReference ref = getSchemaModel().getFactory().
151         createGlobalReference(gg, GlobalGroup.class, gr);
152     gr.setRef(ref);
153     getSchemaModel().endTransaction();
154     getAXIModel().sync();
155     getAXIModel().removePropertyChangeListener(l);
156     childCount = address.getChildElements().size();
157     assert(childCount == 3);
158     }
159     
160     private void changeNameOfElement() throws Exception JavaDoc {
161     Element address = findAXIGlobalElement("address");
162     int childCount = address.getChildElements().size();
163     GlobalElement e = findGlobalElement("address");
164     getSchemaModel().startTransaction();
165     e.setName("NewAddress");
166     getSchemaModel().endTransaction();
167     getAXIModel().sync();
168     assert(childCount == address.getChildElements().size());
169         assert(address.getName().equals("NewAddress"));
170     }
171     
172     private void changeNameOfType() throws Exception JavaDoc {
173     Element address = findAXIGlobalElement("address");
174     int childCount = address.getChildElements().size();
175     GlobalComplexType type = findGlobalComplexType("USAddress1");
176     getSchemaModel().startTransaction();
177     type.setName("USAddress2");
178     getSchemaModel().endTransaction();
179     getAXIModel().sync();
180     assert(childCount == address.getChildElements().size());
181     }
182     
183     private void changeElementRef() throws Exception JavaDoc {
184     getSchemaModel().startTransaction();
185     GlobalGroup group = findGlobalGroup("group1");
186     ElementReference ref = (ElementReference)group.getChildren().get(0).getChildren().get(0);
187     GlobalElement ge = findGlobalElement("fullName");
188     NamedComponentReference ncr = getSchemaModel().getFactory().
189         createGlobalReference(ge, GlobalElement.class, ref);
190     ref.setRef(ncr);
191     getSchemaModel().endTransaction();
192     getAXIModel().sync();
193     }
194     
195     /**
196      * Remove a global element.
197      */

198     private void removeGlobalElement() throws Exception JavaDoc {
199     int elementCount = getAXIModel().getRoot().getElements().size();
200     Element address = findAXIGlobalElement("NewAddress");
201     GlobalElement ge = (GlobalElement)address.getPeer();
202     getSchemaModel().startTransaction();
203     getSchemaModel().getSchema().removeElement(ge);
204     getSchemaModel().endTransaction();
205     getAXIModel().sync();
206     int newCount = getAXIModel().getRoot().getElements().size();
207     assert( (elementCount-1) == newCount);
208     }
209     
210     private void addGlobalElement() throws Exception JavaDoc {
211     int elementCount = getAXIModel().getRoot().getElements().size();
212     getSchemaModel().startTransaction();
213     GlobalElement ge = getSchemaModel().getFactory().createGlobalElement();
214     ge.setName("address");
215     setType(ge, "USAddress1");
216     getSchemaModel().getSchema().addElement(ge);
217     getSchemaModel().endTransaction();
218     getAXIModel().sync();
219     int newCount = getAXIModel().getRoot().getElements().size();
220     assert( (elementCount+1) == newCount);
221     }
222     
223     private void setType(GlobalElement ge, String JavaDoc globalComplexType) {
224     for(GlobalComplexType type : getSchemaModel().getSchema().getComplexTypes()) {
225         if(type.getName().equals(globalComplexType)) {
226         NamedComponentReference ref = getSchemaModel().getFactory().
227             createGlobalReference(type, GlobalType.class, ge);
228         ge.setType(ref);
229         }
230     }
231     }
232     
233     private void renameGlobalElement() throws Exception JavaDoc {
234     getSchemaModel().startTransaction();
235     GlobalElement ge = (GlobalElement)globalElement.getPeer();
236     ge.setName("address1");
237     getSchemaModel().endTransaction();
238     getAXIModel().sync();
239     assert(globalElement.getName().equals("address1"));
240     }
241     
242     private void changeAttributeRef() throws Exception JavaDoc {
243     getSchemaModel().startTransaction();
244     GlobalAttributeGroup group = findGlobalAttributeGroup("attr-group");
245     AttributeReference ref = (AttributeReference)group.getChildren().get(0);
246     GlobalAttribute ga = findGlobalAttribute("countryString");
247     NamedComponentReference ncr = getSchemaModel().getFactory().
248         createGlobalReference(ga, GlobalAttribute.class, ref);
249     ref.setRef(ncr);
250     getSchemaModel().endTransaction();
251     getAXIModel().sync();
252     }
253
254     /**
255      * Remove a GCT "USAddress1", even tho it is being used by "address".
256      */

257     private void deleteGlobalType() throws Exception JavaDoc {
258     Element address = findAXIGlobalElement("NewAddress");
259     int childCount = address.getChildElements().size();
260     assert(childCount == 3);
261     GlobalComplexType gct = findGlobalComplexType("USAddress1");
262     getSchemaModel().startTransaction();
263         getSchemaModel().getSchema().removeComplexType(gct);
264     getSchemaModel().endTransaction();
265     getAXIModel().sync();
266     childCount = address.getChildElements().size();
267     assert(childCount == 0);
268     }
269     
270     static class PropertyListener implements PropertyChangeListener JavaDoc {
271     List JavaDoc<PropertyChangeEvent JavaDoc> events = new ArrayList JavaDoc<PropertyChangeEvent JavaDoc>();
272     
273     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
274         events.add(evt);
275     }
276     public List JavaDoc<PropertyChangeEvent JavaDoc> getEvents() {
277         return events;
278     }
279     public void clearEvents() { events.clear();}
280     }
281 }
282
Popular Tags