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: /*
043: * SchemaTest.java
044: * JUnit based test
045: *
046: * Created on October 5, 2005, 12:49 PM
047: */
048:
049: package org.netbeans.modules.xml.schema.model;
050:
051: import java.io.File;
052: import java.io.IOException;
053: import java.util.ArrayList;
054: import java.util.Set;
055: import javax.swing.undo.UndoManager;
056: import junit.framework.*;
057: import org.netbeans.modules.xml.schema.model.Schema.Block;
058: import org.netbeans.modules.xml.schema.model.Schema.Final;
059: import org.netbeans.modules.xml.schema.model.impl.SchemaModelImpl;
060: import org.netbeans.modules.xml.xam.AbstractModel;
061: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
062: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
063:
064: /**
065: *
066: * @author rico
067: */
068: public class SchemaTest extends TestCase {
069:
070: public SchemaTest(String testName) {
071: super (testName);
072: }
073:
074: public static final String TEST_XSD = "resources/PurchaseOrder.xsd";
075: Schema schema = null;
076: SchemaModel model;
077:
078: protected void setUp() throws Exception {
079: model = Util.loadSchemaModel(TEST_XSD);
080: schema = model.getSchema();
081: }
082:
083: protected void tearDown() throws Exception {
084: TestCatalogModel.getDefault().clearDocumentPool();
085: }
086:
087: public static Test suite() {
088: TestSuite suite = new TestSuite(SchemaTest.class);
089: return suite;
090: }
091:
092: public void testReadElements() throws Exception {
093: ArrayList<GlobalElement> elements = new ArrayList(schema
094: .getElements());
095: assertEquals("Schema.getElements", 2, elements.size());
096: assertEquals("Schema.getElements(1)", "comment", elements
097: .get(1).getName());
098: }
099:
100: /**
101: * Test of getAttributeFormDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
102: */
103: public void testGetAttributeFormDefault() {
104: Form f = schema.getAttributeFormDefault();
105: assertEquals("getAttributeFormDefault", Form.UNQUALIFIED, f);
106: }
107:
108: /**
109: * Test of setAttributeFormDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
110: */
111: public void testSetAttributeFormDefault() throws Exception {
112: SchemaModel model = Util.loadSchemaModel(TEST_XSD);
113: Schema myschema = model.getSchema();
114: model.startTransaction();
115: myschema.setAttributeFormDefault(Form.QUALIFIED);
116: model.endTransaction();
117:
118: myschema = Util.dumpAndReloadModel(model).getSchema();
119: assertEquals("setAttributeFormDefault", Form.QUALIFIED,
120: myschema.getAttributeFormDefault());
121: }
122:
123: /**
124: * Test of getBlockDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
125: */
126: public void testGetBlockDefault() {
127: Set<Block> result = schema.getBlockDefault();
128: assertNull("testGetBlockDefault", result);
129: assertEquals("testGetBlockDefaultDefault", "", schema
130: .getBlockDefaultDefault().toString());
131: }
132:
133: public void testGetBlockDefaultEffective() throws IOException {
134: Set<Block> d1 = schema.getBlockDefaultDefault();
135: d1.clear();
136: d1.add(Block.ALL);
137: model.startTransaction();
138: schema.setBlockDefault(d1);
139: model.endTransaction();
140:
141: assertEquals("testGetBlockDefaultEffective.0", "#all", schema
142: .getBlockDefaultEffective().toString());
143:
144: ArrayList<GlobalComplexType> types = new ArrayList(schema
145: .getComplexTypes());
146: GlobalComplexType typePurchaseOrder = null;
147: if (types.get(0).getName().equals("PurchaseOrderType")) {
148: typePurchaseOrder = types.get(0);
149: }
150:
151: Set<GlobalComplexType.Block> d2 = typePurchaseOrder.getBlock();
152: assertNull("testGetBlockEffective.2", d2);
153: d2 = typePurchaseOrder.getBlockEffective();
154: assertEquals("testGetBlockDefaultEffective.3", "#all", d2
155: .toString());
156: }
157:
158: /**
159: * Test of setBlockDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
160: */
161: public void testSetBlockDefault() throws IOException {
162: Set<Block> d = schema.getBlockDefaultDefault();
163: d.add(Block.EXTENSION);
164: model.startTransaction();
165: schema.setBlockDefault(d);
166: model.endTransaction();
167: assertEquals("testSetBlockDefault.1", "extension", schema
168: .getBlockDefault().toString());
169: assertEquals("testSetBlockDefault.2", d, schema
170: .getBlockDefault());
171: }
172:
173: /**
174: * Test of getElementFormDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
175: */
176: public void testGetElementFormDefault() {
177: Form result = schema.getElementFormDefault();
178: assertEquals("getElementFormDefault", Form.UNQUALIFIED, result);
179: }
180:
181: /**
182: * Test of setElementFormDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
183: */
184: public void testSetElementFormDefault() throws Exception {
185: SchemaModel model = Util.loadSchemaModel(TEST_XSD);
186: Schema myschema = model.getSchema();
187: Form old = myschema.getElementFormDefault();
188: assertEquals("getElementFormDefault", Form.UNQUALIFIED, old);
189: model.startTransaction();
190: myschema.setElementFormDefault(Form.QUALIFIED);
191: model.endTransaction();
192: Form now = myschema.getElementFormDefault();
193: assertEquals("setElementFormDefault: notchanged",
194: Form.QUALIFIED, now);
195:
196: myschema = Util.dumpAndReloadModel(model).getSchema();
197: assertEquals("setElementFormDefault", Form.QUALIFIED, myschema
198: .getElementFormDefault());
199: }
200:
201: /**
202: * Test of getFinalDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
203: */
204: public void testGetFinalDefault() {
205: Set<Final> result = schema.getFinalDefault();
206: assertNull("getFinalDefault", result);
207: Set<Final> def = schema.getFinalDefaultDefault();
208: assertEquals("getFinalDefaultDefault", def.toString(), "");
209: result = schema.getFinalDefaultEffective();
210: assertEquals("getFinalDefaultEffective", def.toString(), result
211: .toString());
212: }
213:
214: /**
215: * Test of setFinalDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
216: */
217: public void testSetFinalDefault() throws Exception {
218: SchemaModel model = Util.loadSchemaModel(TEST_XSD);
219: Schema myschema = model.getSchema();
220: Set<Final> f = myschema.getFinalDefaultDefault();
221: f.add(Final.ALL);
222: model.startTransaction();
223: myschema.setFinalDefault(f);
224: model.endTransaction();
225:
226: myschema = Util.dumpAndReloadModel(model).getSchema();
227: assertTrue("setFinalDefault flush failed", f.equals(myschema
228: .getFinalDefault()));
229: }
230:
231: /**
232: * Test of getTargetNamespace method, of class org.netbeans.modules.xml.schema.model.api.Schema.
233: */
234: public void testGetTargetNamespace() throws Exception {
235: String url = schema.getTargetNamespace();
236: assertEquals("getTargetNamespace",
237: "http://www.example.com/PO1", url);
238: }
239:
240: public void testGetComplexTypes() {
241: ArrayList<GlobalComplexType> types = new ArrayList(schema
242: .getComplexTypes());
243: GlobalComplexType typePurchaseOrder = null;
244: if (types.get(0).getName().equals("PurchaseOrderType")) {
245: typePurchaseOrder = types.get(0);
246: }
247: assertNotNull("getComplexTypes", typePurchaseOrder);
248:
249: ComplexTypeDefinition ctd = typePurchaseOrder.getDefinition();
250: assertTrue("getComplexTypes:sequence", ctd instanceof Sequence);
251:
252: Sequence seq = (Sequence) ctd;
253: assertEquals("getComplexTypes:PurchaseOrder:sequence count", 3,
254: seq.getChildren().size());
255: ArrayList<SchemaComponent> elements = new ArrayList(seq
256: .getChildren());
257: assertTrue(
258: "getComplexTypes:PurchaseOrder:sequence.element(0).name",
259: elements.get(0) instanceof LocalElement);
260: LocalElement e = (LocalElement) elements.get(1);
261: assertNotNull("getComplexTypes:PurchaseOrder:billTo type null",
262: e.getType());
263: assertTrue("getComplexTypes:PurchaseOrder:billTo type", e
264: .getType() instanceof NamedComponentReference);
265: NamedComponentReference<? extends GlobalType> ref = e.getType();
266: GlobalComplexType gct = (GlobalComplexType) ref.get();
267: assertEquals("getComplexTypes:PurchaseOrder:billTo type",
268: "USAddress", gct.getName());
269: }
270:
271: public void testAddSchemaReferences() throws Exception {
272: Import extref = schema.getModel().getFactory().createImport();
273: extref.setNamespace("foor");
274: extref.setSchemaLocation("foor");
275: model.startTransaction();
276: schema.addExternalReference(extref);
277: model.endTransaction();
278: int index = ((AbstractDocumentModel) model).getAccess()
279: .getElementIndexOf(schema.getPeer(), extref.getPeer());
280: assertEquals(0, index);
281:
282: // now add new element it should be added after import
283: GlobalElement ge = schema.getModel().getFactory()
284: .createGlobalElement();
285: ge.setName("newElement");
286: model.startTransaction();
287: schema.addElement(ge);
288: model.endTransaction();
289: index = ((AbstractDocumentModel) model).getAccess()
290: .getElementIndexOf(schema.getPeer(), extref.getPeer());
291: assertEquals(
292: "import should still be the first child component", 0,
293: index);
294: index = ((AbstractDocumentModel) model).getAccess()
295: .getElementIndexOf(schema.getPeer(), ge.getPeer());
296: assertEquals(
297: "globalelement should be appended as last component",
298: schema.getChildren().size() - 1, index);
299: }
300:
301: public void testRollback() throws Exception {
302: UndoManager um = new UndoManager();
303: schema.getModel().addUndoableEditListener(um);
304:
305: GlobalElement stick = schema.getModel().getFactory()
306: .createGlobalElement();
307: stick.setName("stickAfterRollbackElement");
308: model.startTransaction();
309: schema.addElement(stick);
310: model.endTransaction();
311:
312: GlobalElement ge = schema.getModel().getFactory()
313: .createGlobalElement();
314: ge.setName("newElement");
315: int initialCount = schema.getElements().size();
316: model.startTransaction();
317: schema.addElement(ge);
318: assertEquals(initialCount + 1, schema.getElements().size());
319: String text = ((AbstractDocumentModel) model).getAccess()
320: .getCurrentDocumentText();
321: assertTrue(text.indexOf("newElement") > 0);
322: ((AbstractModel) model).rollbackTransaction();
323: text = ((AbstractDocumentModel) model).getAccess()
324: .getCurrentDocumentText();
325: assertTrue(text.indexOf("newElement") == -1);
326: assertEquals(initialCount, schema.getElements().size());
327: assertTrue(text.indexOf("stickAfterRollbackElement") > 0);
328:
329: um.undo();
330: text = ((AbstractDocumentModel) model).getAccess()
331: .getCurrentDocumentText();
332: assertTrue(text.indexOf("stickAfterRollbackElement") == -1);
333:
334: um.redo();
335: text = ((AbstractDocumentModel) model).getAccess()
336: .getCurrentDocumentText();
337: assertTrue(text.indexOf("stickAfterRollbackElement") > 0);
338: }
339:
340: public void testDeleteRollback() throws Exception {
341: UndoManager um = new UndoManager();
342: schema.getModel().addUndoableEditListener(um);
343:
344: GlobalElement stick = schema.getModel().getFactory()
345: .createGlobalElement();
346: stick.setName("stickAfterRollbackElement");
347: model.startTransaction();
348: schema.addElement(stick);
349: model.endTransaction();
350:
351: model.startTransaction();
352: ArrayList<GlobalComplexType> types = new ArrayList(schema
353: .getComplexTypes());
354: ArrayList<GlobalElement> elements = new ArrayList(schema
355: .getElements());
356: GlobalElement element = elements.get(0);
357:
358: if (element.getName().equals("purchaseOrder")) {
359: schema.removeElement(element);
360:
361: String text = ((AbstractDocumentModel) model).getAccess()
362: .getCurrentDocumentText();
363: assertTrue(text.indexOf("purchaseOrder") == -1);
364: ((AbstractModel) model).rollbackTransaction();
365: text = ((AbstractDocumentModel) model).getAccess()
366: .getCurrentDocumentText();
367: assertTrue(text.indexOf("purchaseOrder") > 0);
368: assertTrue(text.indexOf("stickAfterRollbackElement") > 0);
369:
370: um.undo();
371: text = ((AbstractDocumentModel) model).getAccess()
372: .getCurrentDocumentText();
373: assertTrue(text.indexOf("stickAfterRollbackElement") == -1);
374:
375: um.redo();
376: text = ((AbstractDocumentModel) model).getAccess()
377: .getCurrentDocumentText();
378: assertTrue(text.indexOf("stickAfterRollbackElement") > 0);
379:
380: }
381:
382: }
383:
384: public void testRenameRollback() throws Exception {
385: UndoManager um = new UndoManager();
386: schema.getModel().addUndoableEditListener(um);
387:
388: GlobalElement stick = schema.getModel().getFactory()
389: .createGlobalElement();
390: stick.setName("stickAfterRollbackElement");
391: model.startTransaction();
392: schema.addElement(stick);
393: model.endTransaction();
394:
395: int initialCount = schema.getElements().size();
396: model.startTransaction();
397: ArrayList<GlobalComplexType> types = new ArrayList(schema
398: .getComplexTypes());
399: ArrayList<GlobalElement> elements = new ArrayList(schema
400: .getElements());
401: GlobalElement element = elements.get(0);
402:
403: if (element.getName().equals("purchaseOrder")) {
404: element.setName("TestPurchaseOrder");
405:
406: String text = ((AbstractDocumentModel) model).getAccess()
407: .getCurrentDocumentText();
408: assertTrue(text.indexOf("purchaseOrder") == -1);
409: assertTrue(text.indexOf("TestPurchaseOrder") > 0);
410: assertEquals(initialCount, schema.getElements().size());
411: ((AbstractModel) model).rollbackTransaction();
412: text = ((AbstractDocumentModel) model).getAccess()
413: .getCurrentDocumentText();
414: assertTrue(text.indexOf("purchaseOrder") > 0);
415: assertTrue(text.indexOf("TestPurchaseOrder") == -1);
416: assertTrue(text.indexOf("stickAfterRollbackElement") > 0);
417: assertEquals(initialCount, schema.getElements().size());
418: assertEquals("purchaseOrder", element.getName());
419:
420: um.undo();
421: text = ((AbstractDocumentModel) model).getAccess()
422: .getCurrentDocumentText();
423: assertTrue(text.indexOf("stickAfterRollbackElement") == -1);
424:
425: um.redo();
426: text = ((AbstractDocumentModel) model).getAccess()
427: .getCurrentDocumentText();
428: assertTrue(text.indexOf("stickAfterRollbackElement") > 0);
429:
430: }
431:
432: }
433: }
|