KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > impl > xdm > RenameTest


1 package org.netbeans.modules.xml.schema.model.impl.xdm;
2
3 import java.util.Iterator JavaDoc;
4 import javax.swing.text.Document JavaDoc;
5 import junit.framework.*;
6 import org.netbeans.modules.xml.schema.model.*;
7 import org.netbeans.modules.xml.schema.model.impl.GlobalElementImpl;
8 import org.netbeans.modules.xml.schema.model.impl.SchemaImpl;
9 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
10 import org.w3c.dom.Node JavaDoc;
11
12 /**
13  *
14  * @author Ayub Khan
15  */

16 public class RenameTest extends TestCase {
17     
18     public RenameTest(String JavaDoc testName) {
19         super(testName);
20     }
21     
22     
23     
24     /**
25      * Test of rename operation
26      */

27     public void testRenameGlobalElement() throws Exception JavaDoc {
28     // add this code to make sure getDocument returns the same document
29
SchemaModel model = Util.loadSchemaModel("resources/CutPasteTest_before.xsd");
30         SchemaImpl schema = (SchemaImpl) model.getSchema();
31         Node JavaDoc schemaNode = schema.getPeer();
32         GlobalElementImpl gei = (GlobalElementImpl) schema.getElements().iterator().next();
33         
34         assertEquals("testRenameGlobalElement.schema", 1, schema.getChildren().size());
35         assertEquals("testRenameGlobalElement.schema.node", 3, schemaNode.getChildNodes().getLength());
36         
37         //Debug.log(Debug.LEVEL.ERROR, "Initial Document: ");
38
//Debug.logDocument(Debug.LEVEL.ERROR, schemaNode.getOwnerDocument());
39
//XDMModel.printChildren(Debug.LEVEL.ERROR, schemaNode);
40

41         model.startTransaction();
42         gei.setName("NewName");
43         model.endTransaction();
44         
45         model.sync();
46         
47         SchemaImpl changedSchema = (SchemaImpl) model.getSchema();
48         GlobalElementImpl changedGei = (GlobalElementImpl) changedSchema.getElements().iterator().next();
49         Node JavaDoc changedGeiNode = changedGei.getPeer();
50                 
51         assertEquals("testRenameGlobalElement.firstRename.Gei", "NewName", changedGei.getName());
52         assertEquals("testRenameGlobalElement.firstRename.GeiNode", "NewName",
53                 changedGeiNode.getAttributes().item(0).getNodeValue());
54         
55         //TODO - fix, prints children from old document
56
//child nodes: Text@2, Element@3(xsd:element, OrgChart), Text@28
57
//XDMModel.printChildren(Debug.LEVEL.ERROR, changedSchemaNode);
58

59         model.startTransaction();
60         changedGei.setName("NewName2");
61         model.endTransaction();
62         
63         model.sync();
64         
65         SchemaImpl changedSchema2 = (SchemaImpl) model.getSchema();
66         GlobalElementImpl changedGei2 = (GlobalElementImpl) changedSchema2.getElements().iterator().next();
67         Node JavaDoc changedGeiNode2 = changedGei2.getPeer();
68         
69         assertEquals("testRenameGlobalElement.secondRename.Gei2", "NewName2", changedGei2.getName());
70         assertEquals("testRenameGlobalElement.secondRename.Geinode2", "NewName2",
71                 changedGeiNode2.getAttributes().item(0).getNodeValue());
72         
73         //TODO - fix, prints children from old document
74
//child nodes: Text@2, Element@3(xsd:element, OrgChart), Text@28
75
//XDMModel.printChildren(Debug.LEVEL.ERROR, changedSchemaNode2);
76
}
77
78     public void testRenameGlobalElementAfterCopy() throws Exception JavaDoc {
79         // add this code to make sure getDocument returns the same document
80

81         /* This simulates
82          *- create a new schema;
83          *- create a new element;
84          *- switch to Source view;
85          *- copy created element;
86          *- go to Schema and back to Source view;
87          */

88         SchemaModel model = Util.loadSchemaModel("resources/RenameTestRename_before.xsd");
89         
90         SchemaImpl schema = (SchemaImpl) model.getSchema();
91         Document JavaDoc doc = AbstractDocumentModel.class.cast(model).getBaseDocument();
92         
93         Iterator JavaDoc it = schema.getElements().iterator();
94         GlobalElementImpl ge1 = (GlobalElementImpl) it.next();
95         GlobalElementImpl ge2 = (GlobalElementImpl) it.next();
96         assertEquals("testRenameGlobalElementAfterCopy.secondRename.ge1",
97                 "OrgChart", ge1.getName());
98         assertEquals("testRenameGlobalElementAfterCopy.secondRename.ge2",
99                 "OrgChart", ge2.getName());
100         
101         /* This simulates
102          *- rename both elements;
103          *- switch to Schema view.
104          */

105         Util.setDocumentContentTo(doc, "resources/RenameTestRename_after.xsd");
106         model.sync();
107         
108         Iterator JavaDoc it1 = schema.getElements().iterator();
109         ge1 = (GlobalElementImpl) it1.next();
110         ge2 = (GlobalElementImpl) it1.next();
111         assertEquals("testRenameGlobalElementAfterCopy.secondRename.ge1",
112                 "OrgChart1", ge1.getName());
113         assertEquals("testRenameGlobalElementAfterCopy.secondRename.ge2",
114                 "OrgChart2", ge2.getName());
115     }
116         
117     protected void tearDown() throws Exception JavaDoc {
118         super.tearDown();
119         TestCatalogModel.getDefault().clearDocumentPool();
120     }
121     
122 }
123
Popular Tags