1 7 8 package org.netbeans.modules.xml.schema.model.impl; 9 10 import java.io.IOException ; 11 import java.util.Collections ; 12 import junit.framework.*; 13 import org.netbeans.modules.xml.schema.model.Util; 14 import org.netbeans.modules.xml.schema.model.*; 15 import org.netbeans.modules.xml.schema.model.visitor.*; 16 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 17 18 22 public class RefactorVisitorTest extends TestCase { 23 24 public static final String TEST_XSD = "resources/PurchaseOrder.xsd"; 25 26 private Schema schema = null; 27 private GlobalElement global_element = null; 28 private GlobalType global_type = null; 29 private GlobalAttribute global_attribute = null; 30 private SchemaModel model; 31 32 public RefactorVisitorTest(String testName) { 33 super(testName); 34 } 35 36 protected void setUp() throws Exception { 37 model = Util.loadSchemaModel(TEST_XSD); 38 schema = model.getSchema(); 39 40 for(GlobalType type : schema.getComplexTypes()) { 41 if(type.getName().endsWith("USAddress")) { 42 this.global_type = type; 43 } 44 } 45 46 for(GlobalElement e : schema.getElements()) { 47 if(e.getName().endsWith("comment")) { 48 this.global_element = e; 49 } 50 } 51 } 52 53 protected void tearDown() throws Exception { 54 } 55 56 public static Test suite() { 57 TestSuite suite = new TestSuite(RefactorVisitorTest.class); 58 return suite; 59 } 60 61 public void testRenameGlobalType() throws IOException { 62 String oldVal = global_type.getName(); 63 String newVal = "MyAddress"; 64 FindUsageVisitor usage = new FindUsageVisitor(); 65 Preview preview_before = usage.findUsages(Collections.singletonList(schema), global_type); 66 System.out.println(preview_before.getUsages().size() + " occurances of " + oldVal + " found!!!"); 67 68 RefactorVisitor visitor = new RefactorVisitor(); 69 model.startTransaction(); 70 String oldName = global_type.getName(); 71 global_type.setName(newVal); 72 model.endTransaction(); 73 visitor.setRenamedElement(global_type, oldName); 74 model.startTransaction(); 75 visitor.rename(preview_before); 76 model.endTransaction(); 77 78 usage = new FindUsageVisitor(); 79 Preview preview_after = usage.findUsages(Collections.singletonList(schema), global_type); 80 System.out.println(preview_after.getUsages().size() + " occurances of " + newVal + " found!!!"); 81 this.assertEquals(preview_before.getUsages().size(), preview_after.getUsages().size()); 82 } 83 84 public void testRenameGlobalElement() throws IOException { 85 System.out.println("Renaming global element comment to xcomment..."); 86 String oldVal = global_element.getName(); 87 String newVal = "xcomment"; 88 FindUsageVisitor usage = new FindUsageVisitor(); 89 Preview preview_before = usage.findUsages(Collections.singletonList(schema), global_element); 90 System.out.println(preview_before.getUsages().size() + " occurances of " + oldVal + " found!!!"); 91 92 RefactorVisitor visitor = new RefactorVisitor(); 93 model.startTransaction(); 94 String oldName = global_element.getName(); 95 global_element.setName(newVal); 96 model.endTransaction(); 97 visitor.setRenamedElement(global_element, oldName); 98 model.startTransaction(); 99 visitor.rename(preview_before); 100 model.endTransaction(); 101 102 usage = new FindUsageVisitor(); 103 Preview preview_after = usage.findUsages(Collections.singletonList(schema), global_element); 104 assertEquals(preview_before.getUsages().size(), preview_after.getUsages().size()); 106 } 107 108 public static void renameComponent(ReferenceableSchemaComponent component, String newName) throws Exception { 109 SchemaModel model = component.getModel(); 110 Schema schema = model.getSchema(); 111 FindUsageVisitor usage = new FindUsageVisitor(); 112 Preview preview_before = usage.findUsages(Collections.singletonList(schema), component); 113 RefactorVisitor visitor = new RefactorVisitor(); 114 115 model.startTransaction(); 116 String oldName = component.getName(); 117 component.setName(newName); 118 visitor.setRenamedElement(component, oldName); 119 visitor.rename(preview_before); 120 model.endTransaction(); 121 } 122 123 public void testRenameSimpleTypeInUnionMemberType() throws Exception { 124 SchemaModel model = Util.loadSchemaModel("resources/PurchaseOrder_union.xsd"); 125 GlobalSimpleType moneyType = Util.findGlobalSimpleType(model.getSchema(), "Money"); 126 GlobalSimpleType unionType = Util.findGlobalSimpleType(model.getSchema(), "MoneyOrPercentageType"); 127 Union u = (Union) unionType.getDefinition(); 128 String memberTypes = ((AbstractDocumentComponent)u).getAttribute(SchemaAttributes.MEMBER_TYPES); 129 model.startTransaction(); 130 new RefactorVisitor().rename(moneyType, "USDollar"); 131 model.endTransaction(); 132 assertEquals("po:USDollar po:Percentage", ((AbstractDocumentComponent)u).getAttribute(SchemaAttributes.MEMBER_TYPES)); 133 } 134 } 135 | Popular Tags |