1 19 20 package org.netbeans.modules.xml.schema.core; 21 22 import java.util.Collection ; 23 import java.util.List ; 24 import javax.swing.undo.UndoManager ; 25 import junit.framework.*; 26 import org.netbeans.api.project.Project; 27 import org.netbeans.modules.xml.refactoring.DeleteRequest; 28 import org.netbeans.modules.xml.refactoring.ErrorItem; 29 import org.netbeans.modules.xml.refactoring.FileRenameRequest; 30 import org.netbeans.modules.xml.refactoring.FindUsageResult; 31 import org.netbeans.modules.xml.refactoring.RefactoringManager; 32 import org.netbeans.modules.xml.refactoring.RenameRequest; 33 import org.netbeans.modules.xml.refactoring.Usage; 34 import org.netbeans.modules.xml.refactoring.UsageGroup; 35 import org.netbeans.modules.xml.refactoring.UsageSet; 36 import org.netbeans.modules.xml.refactoring.actions.RefactoringRedoAction; 37 import org.netbeans.modules.xml.refactoring.actions.RefactoringUndoAction; 38 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil; 39 import org.netbeans.modules.xml.schema.model.GlobalComplexType; 40 import org.netbeans.modules.xml.schema.model.GlobalElement; 41 import org.netbeans.modules.xml.schema.model.LocalElement; 42 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent; 43 import org.netbeans.modules.xml.schema.model.SchemaModel; 44 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 45 import org.netbeans.modules.xml.schema.model.Sequence; 46 import org.netbeans.modules.xml.xam.Component; 47 import org.netbeans.modules.xml.xam.ModelSource; 48 import org.netbeans.modules.xml.retriever.catalog.Utilities; 49 import org.openide.cookies.SaveCookie; 50 import org.openide.filesystems.FileObject; 51 import org.openide.loaders.DataObject; 52 import org.openide.util.actions.SystemAction; 53 54 58 public class SchemaRefactoringTest extends TestCase { 59 public static final String XSD_PO = "PurchaseOrder.xsd"; 60 public static final String XSD_LA = "LoanApplication.xsd"; 61 62 private Project project; 63 private SchemaModel model; 64 private FileObject purchaseOrderFO; 65 66 public SchemaRefactoringTest(String testName) { 67 super(testName); 68 } 69 70 protected void setUp() throws Exception { 71 project = Util.createJavaTestProject(); 72 purchaseOrderFO = Util.populateProject( 73 project, "resources/"+XSD_PO, "src/com/acme/schemas"); 74 ModelSource ms = Utilities.getModelSource(purchaseOrderFO, true); 75 model = SchemaModelFactory.getDefault().getModel(ms); 76 } 77 78 protected void tearDown() throws Exception { 79 deleteModelFile(purchaseOrderFO); 80 } 81 82 private static void deleteModelFile(FileObject fo) throws Exception { 83 if (fo == null) return; 84 SchemaDataObject sdo = (SchemaDataObject) DataObject.find(fo); 85 if (sdo != null) { 86 SaveCookie save = (SaveCookie) sdo.getCookie(SaveCookie.class); 87 if (save != null) save.save(); 88 } 89 fo.delete(); 90 } 91 92 public static Test suite() { 93 TestSuite suite = new TestSuite(SchemaRefactoringTest.class); 94 return suite; 95 } 96 97 private static int totalItems(UsageSet set) { 98 int count = 0; 99 for (UsageGroup u : set.getUsages()) { 100 count += u.getItems().size(); 101 } 102 return count; 103 } 104 105 public void testFindUsages() throws Exception { 106 GlobalComplexType poType = model.getSchema().getComplexTypes().iterator().next(); 107 FindUsageResult result = RefactoringManager.getInstance().findUsages(poType); 108 assertEquals(1, result.get().getUsages().iterator().next().getItems().size()); 109 assertTrue(result.get().getUsages().iterator().next().getErrors().isEmpty()); 110 UsageSet us = result.get(); 111 UsageGroup u = us.getUsages().iterator().next(); 112 assertNotNull(u.getSourceGroup()); 113 assertNotNull(u.getTarget()); 114 assertNotNull(u.getTargetComponent()); 115 assertNotNull(u.getModel()); 116 assertNotNull(u.getTargetModel()); 117 assertNotNull(u.getEngine()); 118 assertNotNull(u.getFileObject()); 119 Collection <Usage> items = u.getItems(); 120 Usage item = items.iterator().next(); 121 assertNotNull(item.getContainer()); 122 assertEquals(2, item.getPathFromRoot().size()); 123 assertEquals(Usage.Type.REFERENCE, item.getType()); 124 Component referencing = item.getComponent(); 125 assertEquals("purchaseOrder", ((GlobalElement)referencing).getName()); 126 } 127 128 public void testRefactorUsagesUndoRedo() throws Exception { 129 UndoManager myUndoManager = new UndoManager (); 130 model.addUndoableEditListener(myUndoManager); 131 132 ReferenceableSchemaComponent poType = Util.findGlobalComponentByName(model.getSchema(), "PurchaseOrderType"); 133 RenameRequest request = new RenameRequest(poType, "MyPoType"); 134 List <ErrorItem> errors = RefactoringManager.getInstance().precheckUsages(request); 135 assertEquals(0, errors.size()); 136 RefactoringManager.getInstance().process(request); 137 GlobalElement ge = (GlobalElement)Util.findGlobalComponentByName(model.getSchema(), "purchaseOrder"); 138 assertEquals("po:MyPoType", ge.getType().getRefString()); 139 140 assertFalse("other undomanager should be emptied", myUndoManager.canUndo()); 141 assertFalse("other undomanager should be emptied", myUndoManager.canRedo()); 142 assertTrue(RefactoringManager.getInstance().canUndo()); 143 RefactoringManager.getInstance().undo(); 144 poType = Util.findGlobalComponentByName(model.getSchema(), "PurchaseOrderType"); 145 assertNotNull(poType); 146 assertEquals("po:PurchaseOrderType", ge.getType().getRefString()); 147 assertFalse(RefactoringManager.getInstance().canUndo()); 148 149 assertTrue(RefactoringManager.getInstance().canRedo()); 150 RefactoringManager.getInstance().redo(); 151 poType = Util.findGlobalComponentByName(model.getSchema(), "MyPoType"); 152 assertNotNull(poType); 153 assertEquals("po:MyPoType", ge.getType().getRefString()); 154 assertFalse(RefactoringManager.getInstance().canRedo()); 155 156 assertTrue(RefactoringManager.getInstance().canUndo()); 157 assertTrue(RefactoringManager.getInstance().canUndo()); 158 RefactoringManager.getInstance().undo(); 159 poType = Util.findGlobalComponentByName(model.getSchema(), "PurchaseOrderType"); 160 assertNotNull(poType); 161 assertEquals("po:PurchaseOrderType", ge.getType().getRefString()); 162 assertFalse(RefactoringManager.getInstance().canUndo()); 163 164 assertTrue(RefactoringManager.getInstance().canRedo()); 165 assertTrue(RefactoringManager.getInstance().canRedo()); 166 RefactoringManager.getInstance().redo(); 167 poType = Util.findGlobalComponentByName(model.getSchema(), "MyPoType"); 168 assertNotNull(poType); 169 assertEquals("po:MyPoType", ge.getType().getRefString()); 170 assertFalse(RefactoringManager.getInstance().canRedo()); 171 172 model.startTransaction(); 173 poType.setId("foo"); 174 model.endTransaction(); 175 176 assertTrue("other undomanager should be intact", myUndoManager.canUndo()); 177 } 178 179 public void testMultiFiles() throws Exception { 180 FileObject loanApplicationFO = Util.populateProject( 181 project, "resources/"+XSD_LA, "src/com/acme/schemas"); 182 try { 183 ModelSource ms = Utilities.getModelSource(loanApplicationFO, true); 184 SchemaModel laModel = SchemaModelFactory.getDefault().getModel(ms); 185 186 ReferenceableSchemaComponent addressType = Util.findGlobalComponentByName(laModel.getSchema(), "AddressType"); 187 FindUsageResult result = RefactoringManager.getInstance().findUsages(addressType); 188 UsageSet us = result.get(); 189 assertEquals(2, us.getUsages().size()); 190 assertEquals(3, totalItems(us)); 191 192 assertTrue(RefactoringManager.getInstance().canChange(RenameRequest.class, addressType)); 193 RenameRequest request = new RenameRequest(addressType, "MyAddressType"); 194 List <ErrorItem> errors = RefactoringManager.getInstance().precheckChange(request); 195 assertTrue(errors.toString(), errors.isEmpty()); 196 errors = RefactoringManager.getInstance().precheckUsages(request); 197 assertEquals(0, errors.size()); 198 RefactoringManager.getInstance().process(request); 199 String xpath = "/xs:schema/xs:complexType[@name='ResidenceType']/xs:sequence/xs:element[@name='address']"; 200 LocalElement le = (LocalElement) Util.findComponent(laModel.getSchema(), xpath); 201 assertEquals("MyAddressType", le.getType().getRefString()); 202 addressType = Util.findGlobalComponentByName(laModel.getSchema(), "MyAddressType"); 203 assertEquals(3, totalItems(RefactoringManager.getInstance().findUsages(addressType).get())); 204 205 assertTrue(RefactoringManager.getInstance().canUndo()); 206 RefactoringManager.getInstance().undo(); 207 208 addressType = Util.findGlobalComponentByName(laModel.getSchema(), "AddressType"); 209 assertNotNull(addressType); 210 assertEquals("AddressType", le.getType().getRefString()); 211 GlobalComplexType poType = (GlobalComplexType) Util.findGlobalComponentByName(model.getSchema(), "PurchaseOrderType"); 212 Sequence seq = (Sequence) poType.getDefinition(); 213 assertEquals("la:AddressType", ((LocalElement)seq.getContent().get(1)).getType().getRefString()); 214 addressType = Util.findGlobalComponentByName(laModel.getSchema(), "AddressType"); 215 assertEquals(3, totalItems(RefactoringManager.getInstance().findUsages(addressType).get())); 216 217 assertFalse(RefactoringManager.getInstance().canUndo()); 218 assertTrue(RefactoringManager.getInstance().canRedo()); 219 RefactoringManager.getInstance().redo(); 220 221 addressType = Util.findGlobalComponentByName(laModel.getSchema(), "MyAddressType"); 222 assertEquals(3, totalItems(RefactoringManager.getInstance().findUsages(addressType).get())); 223 } finally { 224 deleteModelFile(loanApplicationFO); 225 } 226 } 227 228 public void testInvalidDeleteUndoRedo() throws Exception { 229 FileObject fo = Util.populateProject( 230 project, "resources/newXmlSchema.xsd", "src/com/acme/schemas"); 231 try { 232 ModelSource ms = Utilities.getModelSource(fo, true); 233 SchemaModel model = SchemaModelFactory.getDefault().getModel(ms); 234 Collection <GlobalElement> ges = model.getSchema().getElements(); 235 GlobalElement ge = ges.iterator().next(); 236 GlobalElement copy = (GlobalElement) ge.copy(model.getSchema()); 237 copy.setName("newName"); 238 DeleteRequest delete = new DeleteRequest(ge); 239 RefactoringManager.getInstance().execute(delete, false); 240 assertTrue(RefactoringManager.getInstance().canUndo()); 241 model.startTransaction(); 242 model.getSchema().addElement(copy); 243 model.endTransaction(); 244 assertFalse(RefactoringManager.getInstance().canUndo()); 245 assertFalse(RefactoringManager.getInstance().canRedo()); 246 } finally { 247 deleteModelFile(fo); 248 } 249 } 250 251 public void testRefactorLocalElementUndoRedo() throws Exception { 252 GlobalComplexType poType = (GlobalComplexType) Util.findGlobalComponentByName(model.getSchema(), "PurchaseOrderType"); 253 LocalElement le= poType.getDefinition().getChildren(LocalElement.class).get(0); 254 String expected = "shipToAddress"; 255 RenameRequest request = new RenameRequest(le, expected); 256 RefactoringManager.getInstance().execute(request, false); 257 assertEquals(expected, le.getName()); 258 259 SystemAction undoAction = SystemAction.get(RefactoringUndoAction.class); 260 SystemAction redoAction = SystemAction.get(RefactoringRedoAction.class); 261 262 assertTrue(RefactoringManager.getInstance().canUndo()); 263 assertTrue(RefactoringManager.getInstance().canUndo()); 264 assertFalse(RefactoringManager.getInstance().canRedo()); 265 assertFalse(RefactoringManager.getInstance().canRedo()); 266 RefactoringManager.getInstance().undo(); 267 268 assertFalse(RefactoringManager.getInstance().canUndo()); 269 assertFalse(RefactoringManager.getInstance().canUndo()); 270 assertTrue(RefactoringManager.getInstance().canRedo()); 271 assertTrue(RefactoringManager.getInstance().canRedo()); 272 RefactoringManager.getInstance().redo(); 273 274 assertTrue(RefactoringManager.getInstance().canUndo()); 275 assertTrue(RefactoringManager.getInstance().canUndo()); 276 assertFalse(RefactoringManager.getInstance().canRedo()); 277 assertFalse(RefactoringManager.getInstance().canRedo()); 278 RefactoringManager.getInstance().undo(); 279 280 assertFalse(RefactoringManager.getInstance().canUndo()); 281 assertFalse(RefactoringManager.getInstance().canUndo()); 282 assertTrue(RefactoringManager.getInstance().canRedo()); 283 assertTrue(RefactoringManager.getInstance().canRedo()); 284 RefactoringManager.getInstance().redo(); 285 } 286 287 public void testOneRefactoringInvalidateOther() throws Exception { 288 String expected = "address"; 289 GlobalComplexType poType = (GlobalComplexType) Util.findGlobalComponentByName(model.getSchema(), "PurchaseOrderType"); 290 LocalElement element1 = poType.getDefinition().getChildren(LocalElement.class).get(0); 291 RenameRequest request1 = new RenameRequest(element1, expected); 292 LocalElement element2 = poType.getDefinition().getChildren(LocalElement.class).get(1); 293 RenameRequest request2 = new RenameRequest(element2, expected); 294 RefactoringManager.getInstance().precheckUsages(request1); 295 RefactoringManager.getInstance().precheckUsages(request2); 296 RefactoringManager.getInstance().process(request1); 297 298 assertEquals(expected, element1.getName()); 299 assertFalse("preview of second refactoring should be invalidated", request2.getUsages().isValid()); 300 301 assertTrue(RefactoringManager.getInstance().canUndo()); 302 request2.setUsages(null); 303 RefactoringManager.getInstance().precheckUsages(request2); 304 assertTrue(request2.getUsages().isValid()); 305 assertTrue("precheckUsages should not invalidate undo", RefactoringManager.getInstance().canUndo()); 306 307 model.startTransaction(); 308 model.getSchema().setVersion("9999"); 309 model.endTransaction(); 310 311 assertFalse(RefactoringManager.getInstance().canUndo()); 312 assertFalse(RefactoringManager.getInstance().canRedo()); 313 } 314 315 public void testMultipleRefactoringEffectsOnUndo() throws Exception { 316 UndoManager um = new UndoManager (); 317 model.addUndoableEditListener(um); 318 319 String expected1 = "PurchaseOrderType1"; 320 String expected2 = "PurchaseOrderType2"; 321 String expected3 = "PurchaseOrderType3"; 322 323 GlobalComplexType poType = (GlobalComplexType) Util.findGlobalComponentByName(model.getSchema(), "PurchaseOrderType"); 324 325 RenameRequest request1 = new RenameRequest(poType, expected1); 326 RefactoringManager.getInstance().precheckUsages(request1); 327 RefactoringManager.getInstance().process(request1); 328 assertEquals(expected1, poType.getName()); 329 assertTrue(RefactoringManager.getInstance().canUndo()); 330 331 model.startTransaction(); 332 model.getSchema().setVersion("8888"); 333 model.endTransaction(); 334 335 assertFalse(RefactoringManager.getInstance().canUndo()); 336 assertFalse(RefactoringManager.getInstance().canRedo()); 337 assertTrue(um.canUndo()); 338 339 RenameRequest request2 = new RenameRequest(poType, expected2); 340 RefactoringManager.getInstance().precheckUsages(request2); 341 RefactoringManager.getInstance().process(request2); 342 assertEquals(expected2, poType.getName()); 343 assertTrue(RefactoringManager.getInstance().canUndo()); 344 345 RenameRequest request3 = new RenameRequest(poType, expected3); 346 RefactoringManager.getInstance().precheckUsages(request3); 347 RefactoringManager.getInstance().process(request3); 348 assertEquals(expected3, poType.getName()); 349 assertTrue(RefactoringManager.getInstance().canUndo()); 350 351 model.startTransaction(); 352 model.getSchema().setVersion("0000"); 353 model.endTransaction(); 354 355 assertFalse(RefactoringManager.getInstance().canUndo()); 356 assertFalse(RefactoringManager.getInstance().canRedo()); 357 assertTrue(um.canUndo()); 358 } 359 360 public void testFileRefactoring() throws Exception { 361 FileObject loanApplicationFO = Util.populateProject( 362 project, "resources/"+XSD_LA, "src/com/acme/schemas"); 363 try { 364 ModelSource ms = Utilities.getModelSource(loanApplicationFO, true); 365 SchemaModel laModel = SchemaModelFactory.getDefault().getModel(ms); 366 UndoManager um = new UndoManager (); 367 model.addUndoableEditListener(um); 368 369 laModel.startTransaction(); 370 laModel.getSchema().setVersion("1.0"); 371 laModel.endTransaction(); 372 373 FileRenameRequest request = new FileRenameRequest(laModel, "newLoanApp"); 374 RefactoringManager.getInstance().precheckUsages(request); 375 assertEquals(1, request.getUsages().getUsages().iterator().next().getItems().size()); 376 RefactoringManager.getInstance().process(request); 377 378 assertEquals("newLoanApp", loanApplicationFO.getName()); 379 assertEquals("newLoanApp.xsd", model.getSchema().getImports().iterator().next().getSchemaLocation()); 380 assertTrue(RefactoringManager.getInstance().canUndo()); 381 382 RefactoringManager.getInstance().undo(); 383 384 assertEquals("LoanApplication", loanApplicationFO.getName()); 385 assertEquals("LoanApplication.xsd", model.getSchema().getImports().iterator().next().getSchemaLocation()); 386 assertTrue(RefactoringManager.getInstance().canRedo()); 387 } finally { 388 deleteModelFile(loanApplicationFO); 389 } 390 } 391 392 public void testUndoRedoSaveFile() throws Exception { 393 FileObject loanApplicationFO = Util.populateProject( 394 project, "resources/"+XSD_LA, "src/com/acme/schemas"); 395 ModelSource ms = Utilities.getModelSource(loanApplicationFO, true); 396 SchemaModel laModel = SchemaModelFactory.getDefault().getModel(ms); 397 UndoManager um = new UndoManager (); 398 laModel.addUndoableEditListener(um); 399 400 ReferenceableSchemaComponent addressType = Util.findGlobalComponentByName(laModel.getSchema(), "AddressType"); 401 RenameRequest request = new RenameRequest(addressType, "addressType2"); 402 request.setScopeLocal(); 403 RefactoringManager.getInstance().execute(request, false); 404 405 assertTrue(RefactoringUtil.isDirty(laModel)); 406 um.undo(); 407 assertTrue(RefactoringUtil.isDirty(laModel)); 408 409 request = new RenameRequest(addressType, "addressType2"); 410 RefactoringManager.getInstance().precheckUsages(request); 411 assertEquals(3, totalItems(request.getUsages())); 412 413 RefactoringManager.getInstance().process(request); 414 415 assertTrue(RefactoringUtil.isDirty(laModel)); 416 assertFalse(RefactoringUtil.isDirty(model)); 417 request = new RenameRequest(addressType, "addressType3"); 418 RefactoringManager.getInstance().precheckUsages(request); 419 assertEquals(3, totalItems(request.getUsages())); 420 421 RefactoringManager.getInstance().undo(); 422 423 assertTrue(RefactoringUtil.isDirty(laModel)); 424 assertFalse(RefactoringUtil.isDirty(model)); 425 request = new RenameRequest(addressType, "addressType3"); 426 RefactoringManager.getInstance().precheckUsages(request); 427 assertEquals(3, totalItems(request.getUsages())); 428 429 RefactoringManager.getInstance().redo(); 430 431 assertTrue(RefactoringUtil.isDirty(laModel)); 432 assertFalse(RefactoringUtil.isDirty(model)); 433 request = new RenameRequest(addressType, "addressType3"); 434 RefactoringManager.getInstance().precheckUsages(request); 435 assertEquals(3, totalItems(request.getUsages())); 436 } 437 } 438 | Popular Tags |