1 19 20 package org.netbeans.modules.xml.axi; 21 22 import java.util.Iterator ; 23 import javax.swing.text.BadLocationException ; 24 import javax.swing.text.Document ; 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 38 public class RefactorRenameTest extends AbstractTestCase { 39 40 public static final String RENAME_ELEMENT_XSD = "resources/refactorRenameElement.xsd"; 41 public static final String RENAME_TYPE_XSD = "resources/refactorRenameType.xsd"; 42 public static final String GLOBAL_ELEMENT = "a"; 43 44 private Document doc = null; 45 46 public RefactorRenameTest(String testName) { 47 super(testName, RENAME_ELEMENT_XSD, GLOBAL_ELEMENT); 48 } 49 50 public static Test suite() { 51 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 ex) { 63 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 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 name) { 83 ContentModel aType = axiModel.getRoot().getContentModels().get(0); 84 ContentModel aType1 = axiModel.getRoot().getContentModels().get(1); 85 Iterator it = getSchemaModel().getSchema().getComplexTypes().iterator(); 86 GlobalComplexType gct = (GlobalComplexType) it.next(); 87 GlobalComplexType gct1 = (GlobalComplexType) it.next(); 88 Iterator 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 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 ex) { 111 assertTrue("Should not be here", false); 113 } 114 ContentModel aType = axiModel.getRoot().getContentModels().get(0); 115 Iterator it = getSchemaModel().getSchema().getComplexTypes().iterator(); 116 GlobalComplexType gct = (GlobalComplexType) it.next(); 117 Iterator 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 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 assertEquals("AXI global element b","a2",ge1.getName()); 148 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 ex) { 167 assertTrue("Should not be here", false); 169 } 170 } 171 172 private void renameElement(Element e, String name) { 173 axiModel.startTransaction(); 174 e.setName(name); 175 axiModel.endTransaction(); 176 } 177 178 private void renameElementRef(ElementRef eref, String name) { 179 axiModel.startTransaction(); 180 eref.setName(name); 181 axiModel.endTransaction(); 182 } 183 184 private void renameType(ContentModel aType, String name) { 185 axiModel.startTransaction(); 186 aType.setName(name); 187 axiModel.endTransaction(); 188 } 189 } 190 | Popular Tags |