KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > refactoring > xsd > SchemaUsageRefactoringEngineTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.refactoring.xsd;
21
22 import java.util.List JavaDoc;
23 import junit.framework.*;
24 import org.netbeans.modules.xml.refactoring.RefactorRequest;
25 import org.netbeans.modules.xml.refactoring.RenameRequest;
26 import org.netbeans.modules.xml.refactoring.Usage;
27 import org.netbeans.modules.xml.refactoring.UsageGroup;
28 import org.netbeans.modules.xml.refactoring.UsageSet;
29 import org.netbeans.modules.xml.refactoring.spi.ChangeExecutor;
30 import org.netbeans.modules.xml.schema.model.GlobalElement;
31 import org.netbeans.modules.xml.schema.model.SchemaModel;
32 import org.netbeans.modules.xml.wsdl.model.Part;
33 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
34 import org.netbeans.modules.xml.wsdl.refactoring.NamespaceLocation;
35 import org.netbeans.modules.xml.wsdl.refactoring.TestCatalogModel;
36 import org.netbeans.modules.xml.wsdl.refactoring.Util;
37 import org.openide.util.Lookup;
38
39 /**
40  *
41  * @author nn136682
42  */

43 public class SchemaUsageRefactoringEngineTest extends TestCase {
44     SchemaUsageRefactoringEngine instance = new SchemaUsageRefactoringEngine();
45     
46     public SchemaUsageRefactoringEngineTest(String JavaDoc testName) {
47         super(testName);
48     }
49     
50     protected void setUp() throws Exception JavaDoc {
51     }
52     
53     protected void tearDown() throws Exception JavaDoc {
54     }
55     
56     public static Test suite() {
57         TestSuite suite = new TestSuite(SchemaUsageRefactoringEngineTest.class);
58         
59         return suite;
60     }
61     
62     public void testFindUsages() throws Exception JavaDoc {
63         SchemaModel schemaMod = TestCatalogModel.getDefault().getSchemaModel(NamespaceLocation.OTA);
64         WSDLModel model = TestCatalogModel.getDefault().getWSDLModel(NamespaceLocation.TRAVEL);
65         GlobalElement el = (GlobalElement) Util.findGlobalComponentByName(schemaMod.getSchema(), "TravelItinerary");
66         
67         List JavaDoc<UsageGroup> result = instance.findUsages(el, model.getDefinitions());
68         assertEquals(1, result.size());
69         assertEquals(2, result.get(0).getItems().size());
70         Usage usage0 = (Usage) result.get(0).getItems().get(0);
71         assertEquals("itinerary", ((Part)usage0.getComponent()).getName());
72     }
73     
74     private void executeChange(RefactorRequest request) throws Exception JavaDoc {
75         Lookup.Result results = Lookup.getDefault().lookup(
76                 new Lookup.Template(ChangeExecutor.class));
77         for (Object JavaDoc service : results.allInstances()){
78             ChangeExecutor exec = (ChangeExecutor)service;
79             if (exec.canChange(RenameRequest.class, request.getTarget())) {
80                 exec.doChange(request);
81                 return;
82             }
83         }
84         
85         fail("Failed to find a change executor for schema component");
86     }
87     
88     public void testRefactorUsages() throws Exception JavaDoc {
89         SchemaModel schemaMod = TestCatalogModel.getDefault().getSchemaModel(NamespaceLocation.OTA);
90         WSDLModel model = TestCatalogModel.getDefault().getWSDLModel(NamespaceLocation.TRAVEL);
91         GlobalElement el = (GlobalElement) Util.findGlobalComponentByName(schemaMod.getSchema(), "TravelItinerary");
92         List JavaDoc<UsageGroup> usageList = instance.findUsages(el, model.getDefinitions());
93         UsageSet usages = new UsageSet(el);
94         usages.addUsages(usageList);
95         String JavaDoc newName = "myItirenary";
96         RenameRequest request = new RenameRequest(el, newName);
97         request.setUsages(usages);
98         executeChange(request);
99         assertTrue(request.confirmChangePerformed());
100         instance.refactorUsages(request);
101         
102         Part part = model.findComponentByName("itinerary", Part.class);
103         assertEquals(newName, part.getElement().getQName().getLocalPart());
104         usageList = instance.findUsages((GlobalElement)request.getRenamedTarget(), model.getDefinitions());
105         assertEquals(2, usageList.get(0).getItems().size());
106     }
107     
108 }
109
Popular Tags