001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.axi.sync;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.util.ArrayList;
047: import java.util.List;
048: import junit.framework.*;
049: import org.netbeans.modules.xml.axi.Element;
050: import org.netbeans.modules.xml.schema.model.AttributeReference;
051: import org.netbeans.modules.xml.schema.model.ElementReference;
052: import org.netbeans.modules.xml.schema.model.GlobalAttribute;
053: import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
054: import org.netbeans.modules.xml.schema.model.GlobalComplexType;
055: import org.netbeans.modules.xml.schema.model.GlobalElement;
056: import org.netbeans.modules.xml.schema.model.GlobalGroup;
057: import org.netbeans.modules.xml.schema.model.GlobalType;
058: import org.netbeans.modules.xml.schema.model.GroupReference;
059: import org.netbeans.modules.xml.schema.model.LocalAttribute;
060: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
061: import org.netbeans.modules.xml.schema.model.Sequence;
062:
063: /**
064: * The unit test covers various use cases of sync on Element
065: * and ElementRef.
066: *
067: * @author Samaresh (Samaresh.Panda@Sun.Com)
068: */
069: public class SyncElementTest extends AbstractSyncTestCase {
070:
071: public static final String TEST_XSD = "resources/address.xsd";
072: public static final String GLOBAL_ELEMENT = "address";
073:
074: /**
075: * SyncElementTest
076: */
077: public SyncElementTest(String testName) {
078: super (testName, TEST_XSD, GLOBAL_ELEMENT);
079: }
080:
081: public static Test suite() {
082: TestSuite suite = new TestSuite(SyncElementTest.class);
083: return suite;
084: }
085:
086: public void testElementType() throws Exception {
087: removeElementFromType();
088: removeAttributeFromAttrGroup();
089: changeType();
090: changeAttributeRef();
091: changeTypeContent();
092: changeNameOfElement();
093: //deleteGlobalType();
094: }
095:
096: public void testElement() throws Exception {
097: removeGlobalElement();
098: addGlobalElement();
099: changeElementRef();
100: }
101:
102: /**
103: * Removes an element from type "USAddress".
104: * Element count should be one less.
105: */
106: private void removeElementFromType() throws Exception {
107: Element address = findAXIGlobalElement("address");
108: int childCount = address.getChildElements().size();
109: assert (childCount == 3);
110: GlobalComplexType gct = findGlobalComplexType("USAddress");
111: getSchemaModel().startTransaction();
112: Sequence s = (Sequence) gct.getChildren().get(0);
113: s.removeContent(s.getContent().get(0));
114: getSchemaModel().endTransaction();
115: getAXIModel().sync();
116: childCount = address.getChildElements().size();
117: assert (childCount == 2);
118: }
119:
120: /**
121: * Removes an attribute from attribute group.
122: * child count should be one less.
123: */
124: private void removeAttributeFromAttrGroup() throws Exception {
125: Element address = findAXIGlobalElement("address");
126: int childCount = address.getChildren().size();
127: assert (childCount == 4);
128: GlobalAttributeGroup gag = findGlobalAttributeGroup("attr-group");
129: getSchemaModel().startTransaction();
130: LocalAttribute attr = (LocalAttribute) gag.getChildren().get(1);
131: gag.removeLocalAttribute(attr);
132: getSchemaModel().endTransaction();
133: getAXIModel().sync();
134: childCount = address.getChildren().size();
135: assert (childCount == 3);
136: }
137:
138: /**
139: * Change the type of element "address" from
140: * "USAddress" to "USAddress1".
141: */
142: private void changeType() throws Exception {
143: PropertyListener l = new PropertyListener();
144: getAXIModel().addPropertyChangeListener(l);
145: Element address = findAXIGlobalElement("address");
146: int childCount = address.getChildElements().size();
147: assert (childCount == 2);
148: GlobalElement ge = (GlobalElement) globalElement.getPeer();
149: getSchemaModel().startTransaction();
150: setType(ge, "USAddress1");
151: getSchemaModel().endTransaction();
152: getAXIModel().sync();
153: getAXIModel().removePropertyChangeListener(l);
154: childCount = address.getChildElements().size();
155: assert (childCount == 5);
156: }
157:
158: /**
159: * Change the content of "USAddress1".
160: */
161: private void changeTypeContent() throws Exception {
162: PropertyListener l = new PropertyListener();
163: getAXIModel().addPropertyChangeListener(l);
164: Element address = findAXIGlobalElement("address");
165: int childCount = address.getChildElements().size();
166: assert (childCount == 5);
167: getSchemaModel().startTransaction();
168: GlobalGroup gg = findGlobalGroup("group2");
169: GroupReference gr = (GroupReference) (findGlobalComplexType(
170: "USAddress1").getChildren().get(0));
171: NamedComponentReference ref = getSchemaModel().getFactory()
172: .createGlobalReference(gg, GlobalGroup.class, gr);
173: gr.setRef(ref);
174: getSchemaModel().endTransaction();
175: getAXIModel().sync();
176: getAXIModel().removePropertyChangeListener(l);
177: childCount = address.getChildElements().size();
178: assert (childCount == 3);
179: }
180:
181: private void changeNameOfElement() throws Exception {
182: Element address = findAXIGlobalElement("address");
183: int childCount = address.getChildElements().size();
184: GlobalElement e = findGlobalElement("address");
185: getSchemaModel().startTransaction();
186: e.setName("NewAddress");
187: getSchemaModel().endTransaction();
188: getAXIModel().sync();
189: assert (childCount == address.getChildElements().size());
190: assert (address.getName().equals("NewAddress"));
191: }
192:
193: private void changeNameOfType() throws Exception {
194: Element address = findAXIGlobalElement("address");
195: int childCount = address.getChildElements().size();
196: GlobalComplexType type = findGlobalComplexType("USAddress1");
197: getSchemaModel().startTransaction();
198: type.setName("USAddress2");
199: getSchemaModel().endTransaction();
200: getAXIModel().sync();
201: assert (childCount == address.getChildElements().size());
202: }
203:
204: private void changeElementRef() throws Exception {
205: getSchemaModel().startTransaction();
206: GlobalGroup group = findGlobalGroup("group1");
207: ElementReference ref = (ElementReference) group.getChildren()
208: .get(0).getChildren().get(0);
209: GlobalElement ge = findGlobalElement("fullName");
210: NamedComponentReference ncr = getSchemaModel().getFactory()
211: .createGlobalReference(ge, GlobalElement.class, ref);
212: ref.setRef(ncr);
213: getSchemaModel().endTransaction();
214: getAXIModel().sync();
215: }
216:
217: /**
218: * Remove a global element.
219: */
220: private void removeGlobalElement() throws Exception {
221: int elementCount = getAXIModel().getRoot().getElements().size();
222: Element address = findAXIGlobalElement("NewAddress");
223: GlobalElement ge = (GlobalElement) address.getPeer();
224: getSchemaModel().startTransaction();
225: getSchemaModel().getSchema().removeElement(ge);
226: getSchemaModel().endTransaction();
227: getAXIModel().sync();
228: int newCount = getAXIModel().getRoot().getElements().size();
229: assert ((elementCount - 1) == newCount);
230: }
231:
232: private void addGlobalElement() throws Exception {
233: int elementCount = getAXIModel().getRoot().getElements().size();
234: getSchemaModel().startTransaction();
235: GlobalElement ge = getSchemaModel().getFactory()
236: .createGlobalElement();
237: ge.setName("address");
238: setType(ge, "USAddress1");
239: getSchemaModel().getSchema().addElement(ge);
240: getSchemaModel().endTransaction();
241: getAXIModel().sync();
242: int newCount = getAXIModel().getRoot().getElements().size();
243: assert ((elementCount + 1) == newCount);
244: }
245:
246: private void setType(GlobalElement ge, String globalComplexType) {
247: for (GlobalComplexType type : getSchemaModel().getSchema()
248: .getComplexTypes()) {
249: if (type.getName().equals(globalComplexType)) {
250: NamedComponentReference ref = getSchemaModel()
251: .getFactory().createGlobalReference(type,
252: GlobalType.class, ge);
253: ge.setType(ref);
254: }
255: }
256: }
257:
258: private void renameGlobalElement() throws Exception {
259: getSchemaModel().startTransaction();
260: GlobalElement ge = (GlobalElement) globalElement.getPeer();
261: ge.setName("address1");
262: getSchemaModel().endTransaction();
263: getAXIModel().sync();
264: assert (globalElement.getName().equals("address1"));
265: }
266:
267: private void changeAttributeRef() throws Exception {
268: getSchemaModel().startTransaction();
269: GlobalAttributeGroup group = findGlobalAttributeGroup("attr-group");
270: AttributeReference ref = (AttributeReference) group
271: .getChildren().get(0);
272: GlobalAttribute ga = findGlobalAttribute("countryString");
273: NamedComponentReference ncr = getSchemaModel().getFactory()
274: .createGlobalReference(ga, GlobalAttribute.class, ref);
275: ref.setRef(ncr);
276: getSchemaModel().endTransaction();
277: getAXIModel().sync();
278: }
279:
280: /**
281: * Remove a GCT "USAddress1", even tho it is being used by "address".
282: */
283: private void deleteGlobalType() throws Exception {
284: Element address = findAXIGlobalElement("NewAddress");
285: int childCount = address.getChildElements().size();
286: assert (childCount == 3);
287: GlobalComplexType gct = findGlobalComplexType("USAddress1");
288: getSchemaModel().startTransaction();
289: getSchemaModel().getSchema().removeComplexType(gct);
290: getSchemaModel().endTransaction();
291: getAXIModel().sync();
292: childCount = address.getChildElements().size();
293: assert (childCount == 0);
294: }
295:
296: static class PropertyListener implements PropertyChangeListener {
297: List<PropertyChangeEvent> events = new ArrayList<PropertyChangeEvent>();
298:
299: public void propertyChange(PropertyChangeEvent evt) {
300: events.add(evt);
301: }
302:
303: public List<PropertyChangeEvent> getEvents() {
304: return events;
305: }
306:
307: public void clearEvents() {
308: events.clear();
309: }
310: }
311: }
|