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.schema.model.impl.xdm;
043:
044: import java.util.Iterator;
045: import javax.swing.text.Document;
046: import junit.framework.*;
047: import org.netbeans.modules.xml.schema.model.*;
048: import org.netbeans.modules.xml.schema.model.impl.GlobalElementImpl;
049: import org.netbeans.modules.xml.schema.model.impl.SchemaImpl;
050: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
051: import org.w3c.dom.Node;
052:
053: /**
054: *
055: * @author Ayub Khan
056: */
057: public class RenameTest extends TestCase {
058:
059: public RenameTest(String testName) {
060: super (testName);
061: }
062:
063: /**
064: * Test of rename operation
065: */
066: public void testRenameGlobalElement() throws Exception {
067: // add this code to make sure getDocument returns the same document
068: SchemaModel model = Util
069: .loadSchemaModel("resources/CutPasteTest_before.xsd");
070: SchemaImpl schema = (SchemaImpl) model.getSchema();
071: Node schemaNode = schema.getPeer();
072: GlobalElementImpl gei = (GlobalElementImpl) schema
073: .getElements().iterator().next();
074:
075: assertEquals("testRenameGlobalElement.schema", 1, schema
076: .getChildren().size());
077: assertEquals("testRenameGlobalElement.schema.node", 3,
078: schemaNode.getChildNodes().getLength());
079:
080: //Debug.log(Debug.LEVEL.ERROR, "Initial Document: ");
081: //Debug.logDocument(Debug.LEVEL.ERROR, schemaNode.getOwnerDocument());
082: //XDMModel.printChildren(Debug.LEVEL.ERROR, schemaNode);
083:
084: model.startTransaction();
085: gei.setName("NewName");
086: model.endTransaction();
087:
088: model.sync();
089:
090: SchemaImpl changedSchema = (SchemaImpl) model.getSchema();
091: GlobalElementImpl changedGei = (GlobalElementImpl) changedSchema
092: .getElements().iterator().next();
093: Node changedGeiNode = changedGei.getPeer();
094:
095: assertEquals("testRenameGlobalElement.firstRename.Gei",
096: "NewName", changedGei.getName());
097: assertEquals("testRenameGlobalElement.firstRename.GeiNode",
098: "NewName", changedGeiNode.getAttributes().item(0)
099: .getNodeValue());
100:
101: //TODO - fix, prints children from old document
102: //child nodes: Text@2, Element@3(xsd:element, OrgChart), Text@28
103: //XDMModel.printChildren(Debug.LEVEL.ERROR, changedSchemaNode);
104:
105: model.startTransaction();
106: changedGei.setName("NewName2");
107: model.endTransaction();
108:
109: model.sync();
110:
111: SchemaImpl changedSchema2 = (SchemaImpl) model.getSchema();
112: GlobalElementImpl changedGei2 = (GlobalElementImpl) changedSchema2
113: .getElements().iterator().next();
114: Node changedGeiNode2 = changedGei2.getPeer();
115:
116: assertEquals("testRenameGlobalElement.secondRename.Gei2",
117: "NewName2", changedGei2.getName());
118: assertEquals("testRenameGlobalElement.secondRename.Geinode2",
119: "NewName2", changedGeiNode2.getAttributes().item(0)
120: .getNodeValue());
121:
122: //TODO - fix, prints children from old document
123: //child nodes: Text@2, Element@3(xsd:element, OrgChart), Text@28
124: //XDMModel.printChildren(Debug.LEVEL.ERROR, changedSchemaNode2);
125: }
126:
127: public void testRenameGlobalElementAfterCopy() throws Exception {
128: // add this code to make sure getDocument returns the same document
129:
130: /* This simulates
131: *- create a new schema;
132: *- create a new element;
133: *- switch to Source view;
134: *- copy created element;
135: *- go to Schema and back to Source view;
136: */
137: SchemaModel model = Util
138: .loadSchemaModel("resources/RenameTestRename_before.xsd");
139:
140: SchemaImpl schema = (SchemaImpl) model.getSchema();
141: Document doc = AbstractDocumentModel.class.cast(model)
142: .getBaseDocument();
143:
144: Iterator it = schema.getElements().iterator();
145: GlobalElementImpl ge1 = (GlobalElementImpl) it.next();
146: GlobalElementImpl ge2 = (GlobalElementImpl) it.next();
147: assertEquals(
148: "testRenameGlobalElementAfterCopy.secondRename.ge1",
149: "OrgChart", ge1.getName());
150: assertEquals(
151: "testRenameGlobalElementAfterCopy.secondRename.ge2",
152: "OrgChart", ge2.getName());
153:
154: /* This simulates
155: *- rename both elements;
156: *- switch to Schema view.
157: */
158: Util.setDocumentContentTo(doc,
159: "resources/RenameTestRename_after.xsd");
160: model.sync();
161:
162: Iterator it1 = schema.getElements().iterator();
163: ge1 = (GlobalElementImpl) it1.next();
164: ge2 = (GlobalElementImpl) it1.next();
165: assertEquals(
166: "testRenameGlobalElementAfterCopy.secondRename.ge1",
167: "OrgChart1", ge1.getName());
168: assertEquals(
169: "testRenameGlobalElementAfterCopy.secondRename.ge2",
170: "OrgChart2", ge2.getName());
171: }
172:
173: protected void tearDown() throws Exception {
174: super.tearDown();
175: TestCatalogModel.getDefault().clearDocumentPool();
176: }
177:
178: }
|