KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
23 import javax.swing.text.BadLocationException JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import junit.framework.*;
26 import org.netbeans.modules.xml.axi.impl.ElementRef;
27 import org.netbeans.modules.xml.schema.model.ElementReference;
28 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
29 import org.netbeans.modules.xml.schema.model.GlobalElement;
30 import org.netbeans.modules.xml.schema.model.SchemaModel;
31 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
32
33
34 /**
35  *
36  * @author Ayub Khan
37  */

38 public class RefactorRenameTest extends AbstractTestCase {
39     
40     public static final String JavaDoc RENAME_ELEMENT_XSD = "resources/refactorRenameElement.xsd";
41     public static final String JavaDoc RENAME_TYPE_XSD = "resources/refactorRenameType.xsd";
42     public static final String JavaDoc GLOBAL_ELEMENT = "a";
43     
44     private Document JavaDoc doc = null;
45     
46     public RefactorRenameTest(String JavaDoc testName) {
47         super(testName, RENAME_ELEMENT_XSD, GLOBAL_ELEMENT);
48     }
49     
50     public static Test suite() {
51 // TestSuite suite = new TestSuite(DesignPatternTest.class);
52
TestSuite suite = new TestSuite();
53         suite.addTest(new RefactorRenameTest("testRenameElement"));
54         suite.addTest(new RefactorRenameTest("testRenameType"));
55         return suite;
56     }
57     
58     public void testRenameElement() {
59         print("testRenameElement");
60         try {
61             loadModel(RENAME_ELEMENT_XSD);
62         } catch (Exception JavaDoc ex) {
63             //ex.printStackTrace();
64
assertTrue("Should not be here", false);
65         }
66         ContentModel aType = axiModel.getRoot().getContentModels().get(0);
67         assertElementNames(axiModel, "b");
68         
69         renameElementRef((ElementRef) aType.getChildElements().get(0), "newName");
70         
71         assertElementNames(axiModel, "newName");
72         
73         Iterator JavaDoc it3 = axiModel.getRoot().getElements().iterator();
74         Element e1 = (Element) it3.next();
75         if(!e1.getName().equals("b"))
76             e1 = (Element) it3.next();
77         renameElement(e1, "newName2");
78         
79         assertElementNames(axiModel, "newName2");
80     }
81     
82     private void assertElementNames(final AXIModel axiModel, String JavaDoc name) {
83         ContentModel aType = axiModel.getRoot().getContentModels().get(0);
84         ContentModel aType1 = axiModel.getRoot().getContentModels().get(1);
85         Iterator JavaDoc it = getSchemaModel().getSchema().getComplexTypes().iterator();
86         GlobalComplexType gct = (GlobalComplexType) it.next();
87         GlobalComplexType gct1 = (GlobalComplexType) it.next();
88         Iterator JavaDoc it1 = getSchemaModel().getSchema().getElements().iterator();
89         GlobalElement ge = (GlobalElement) it1.next();
90         GlobalElement ge1 = (GlobalElement) it1.next();
91         assertEquals("global complex types",2,getSchemaModel().getSchema().getComplexTypes().size());
92         assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size());
93         assertEquals("global elements",2,getSchemaModel().getSchema().getElements().size());
94         assertEquals("AXI global element b",name,ge1.getName());
95         assertEquals("AXI global element b",name,((ElementReference)gct.getDefinition().getChildren().get(0)).getRef().get().getName());
96         assertEquals("AXI global element b",name,((ElementReference)gct1.getDefinition().getChildren().get(0)).getRef().get().getName());
97         
98         assertEquals("AXI content models",2,axiModel.getRoot().getContentModels().size());
99         assertEquals("AXI global elements",2,axiModel.getRoot().getElements().size());
100 // assertEquals("AXI global element a","a",axiModel.getRoot().getElements().get(0).getName());
101
assertEquals("AXI global element b",name,axiModel.getRoot().getElements().get(1).getName());
102         assertEquals("AXI global element b",name,((ElementRef) aType.getChildElements().get(0)).getName());
103         assertEquals("AXI global element b",name,((ElementRef) aType1.getChildElements().get(0)).getName());
104     }
105     
106     public void testRenameType() {
107         print("testRenameType");
108         try {
109             loadModel(RENAME_TYPE_XSD);
110         } catch (Exception JavaDoc ex) {
111             //ex.printStackTrace();
112
assertTrue("Should not be here", false);
113         }
114         ContentModel aType = axiModel.getRoot().getContentModels().get(0);
115         Iterator JavaDoc it = getSchemaModel().getSchema().getComplexTypes().iterator();
116         GlobalComplexType gct = (GlobalComplexType) it.next();
117         Iterator JavaDoc it1 = getSchemaModel().getSchema().getElements().iterator();
118         GlobalElement ge = (GlobalElement) it1.next();
119         GlobalElement ge1 = (GlobalElement) it1.next();
120         GlobalElement ge2 = (GlobalElement) it1.next();
121         assertEquals("global complex types",1,getSchemaModel().getSchema().getComplexTypes().size());
122         assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size());
123         assertEquals("global elements",3,getSchemaModel().getSchema().getElements().size());
124         assertEquals("AXI global element b","a1",ge.getName());
125         assertEquals("AXI global element b","aType",ge.getType().get().getName());
126         assertEquals("AXI global element b","a2",ge1.getName());
127         assertEquals("AXI global element b","aType",ge1.getType().get().getName());
128         assertEquals("AXI global element b","aType",gct.getName());
129         assertEquals("AXI global element b","b",((ElementReference)gct.getDefinition().getChildren().get(0)).getRef().get().getName());
130         
131         assertEquals("AXI content models",1,axiModel.getRoot().getContentModels().size());
132         assertEquals("AXI global elements",3,axiModel.getRoot().getElements().size());
133         assertEquals("AXI global element a","a1",axiModel.getRoot().getElements().get(0).getName());
134         assertEquals("AXI global element b","a2",axiModel.getRoot().getElements().get(1).getName());
135         assertEquals("AXI global element b","b",axiModel.getRoot().getElements().get(2).getName());
136         assertEquals("AXI global element b","b",((ElementRef) aType.getChildElements().get(0)).getName());
137         
138         renameType(aType, "newName");
139         
140 // printDocument();
141
assertEquals("global complex types",1,getSchemaModel().getSchema().getComplexTypes().size());
142         assertEquals("global simple types",0,getSchemaModel().getSchema().getSimpleTypes().size());
143         assertEquals("global elements",3,getSchemaModel().getSchema().getElements().size());
144         assertEquals("AXI global element b","a1",ge.getName());
145         //FIXME - is null
146
// assertEquals("AXI global element b","aType",ge.getType().get().getName());
147
assertEquals("AXI global element b","a2",ge1.getName());
148         //FIXME - is null
149
// assertEquals("AXI global element b","aType",ge1.getType().get().getName());
150
assertEquals("AXI global element b","newName",gct.getName());
151         assertEquals("AXI global element b","b",((ElementReference)gct.getDefinition().getChildren().get(0)).getRef().get().getName());
152         
153         assertEquals("AXI content models",1,axiModel.getRoot().getContentModels().size());
154         assertEquals("AXI global elements",3,axiModel.getRoot().getElements().size());
155         assertEquals("AXI global element a","a1",axiModel.getRoot().getElements().get(0).getName());
156         assertEquals("AXI global element b","a2",axiModel.getRoot().getElements().get(1).getName());
157         assertEquals("AXI global element b","b",axiModel.getRoot().getElements().get(2).getName());
158         assertEquals("AXI global element b","b",((ElementRef) aType.getChildElements().get(0)).getName());
159     }
160     
161     private void printDocument() {
162         try {
163             SchemaModel sm = getSchemaModel();
164             doc = ((AbstractDocumentModel)sm).getBaseDocument();
165             print("doc: "+doc.getText(0, doc.getLength()));
166         } catch (BadLocationException JavaDoc ex) {
167             //ex.printStackTrace();
168
assertTrue("Should not be here", false);
169         }
170     }
171     
172     private void renameElement(Element e, String JavaDoc name) {
173         axiModel.startTransaction();
174         e.setName(name);
175         axiModel.endTransaction();
176     }
177     
178     private void renameElementRef(ElementRef eref, String JavaDoc name) {
179         axiModel.startTransaction();
180         eref.setName(name);
181         axiModel.endTransaction();
182     }
183     
184     private void renameType(ContentModel aType, String JavaDoc name) {
185         axiModel.startTransaction();
186         aType.setName(name);
187         axiModel.endTransaction();
188     }
189 }
190
Popular Tags