KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > test > J2EERenameRefactoringTest


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

19
20 package org.netbeans.modules.j2ee.refactoring.test;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import junit.textui.TestRunner;
25 import org.netbeans.junit.NbTestSuite;
26 import org.netbeans.modules.web.taglib.TLDDataObject;
27 import org.netbeans.modules.j2ee.refactoring.test.util.Helper;
28 import org.openide.cookies.EditorCookie;
29 import org.openide.cookies.OpenCookie;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileStateInvalidException;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.loaders.DataObject;
34
35 /**
36  *
37  * @author Martin Grebac
38  */

39 public class J2EERenameRefactoringTest extends J2EERefactoringTest {
40     private static final String JavaDoc PROJECT_EJBMOD1 = "ejbmod1";
41     private static final String JavaDoc PROJECT_WEBAPP1 = "webapp1";
42
43     public J2EERenameRefactoringTest(String JavaDoc testName) {
44         super(testName);
45     }
46
47     public static NbTestSuite suite() {
48         NbTestSuite suite = new NbTestSuite("J2EERenameRefactoringTest");
49         suite.addTest(new J2EERenameRefactoringTest("testPrepare"));
50         suite.addTest(new J2EERenameRefactoringTest("testTldRenameClass"));
51         suite.addTest(new J2EERenameRefactoringTest("testWebRenameClass"));
52         suite.addTest(new J2EERenameRefactoringTest("testEjbRenameClass"));
53         suite.addTest(new J2EERenameRefactoringTest("testWSRenameClass"));
54         suite.addTest(new J2EERenameRefactoringTest("testCleanup"));
55         return suite;
56     }
57
58     /**
59      * Opens projects for tests
60      */

61     public void testPrepare() {
62         openProject(PROJECT_WEBAPP1);
63         openProject(PROJECT_EJBMOD1);
64     }
65
66     /** Renames
67      * tag handler
68      * and checks renamed files and mytld.tld for differences
69      */

70     public void testTldRenameClass() throws FileStateInvalidException, IOException JavaDoc {
71         File JavaDoc f = getProjectFile(PROJECT_WEBAPP1, "/web/WEB-INF/tlds/mytld.tld");
72         FileObject fo = FileUtil.toFileObject(f);
73
74         // this part is here to test if the documents opened in editor are changed, too, which is a bug 55405
75
TLDDataObject tdo = ((TLDDataObject)DataObject.find(fo));
76         OpenCookie oc = (OpenCookie)tdo.getCookie(OpenCookie.class);
77         oc.open();
78         // end of 55405 part
79

80         doRenameRefactoring(PROJECT_WEBAPP1, "abc.NewTagHandler", "RenamedTagHandler", "rename tag handler");
81
82         Helper.waitForAWTDispatchThread();
83
84         // another 55405 specific part
85
EditorCookie ec = (EditorCookie)tdo.getCookie(EditorCookie.class);
86         try {
87             ec.getDocument().insertString(0, " ", null);
88             ec.saveDocument();
89             ec.getDocument().remove(0, 1);
90             ec.saveDocument();
91         } catch (Exception JavaDoc e) {
92             fail(e.toString());
93         }
94         // end of 55405 specific part
95

96         Helper.waitForAWTDispatchThread();
97         // check modified files
98
try {
99             checkGoldenFile(PROJECT_WEBAPP1, "src/java/abc/RenamedTagHandler.java", "RenamedTagHandler.pass");
100
101             // check TLD
102
checkFile(PROJECT_WEBAPP1, "web/WEB-INF/tlds/mytld.tld", new String JavaDoc[]{">abc.RenamedTagHandler<"},
103                     new String JavaDoc[]{">abc.NewTagHandler<"});
104
105         } catch (FileStateInvalidException e) {
106             fail(e.getMessage());
107         } catch (IOException JavaDoc e) {
108             fail(e.getMessage());
109         }
110     }
111
112     /** Renames
113      * servlet
114      * filter
115      * listener
116      * and checks renamed files and web.xml for differences
117      */

118     public void testWebRenameClass() throws FileStateInvalidException, IOException JavaDoc {
119
120         doRenameRefactoring(PROJECT_WEBAPP1, "abc.NewServlet", "RenamedServlet", "rename servlet");
121         doRenameRefactoring(PROJECT_WEBAPP1, "abc.NewSimpleFilter", "RenamedFilter", "rename filter");
122         doRenameRefactoring(PROJECT_WEBAPP1, "abc.NewServletListener", "RenamedListener", "rename listener");
123
124         Helper.waitForAWTDispatchThread();
125
126         // check modified files
127
try {
128             checkGoldenFile(PROJECT_WEBAPP1, "src/java/abc/RenamedServlet.java", "RenamedServlet.pass");
129             checkGoldenFile(PROJECT_WEBAPP1, "src/java/abc/RenamedFilter.java", "RenamedFilter.pass");
130             checkGoldenFile(PROJECT_WEBAPP1, "src/java/abc/RenamedListener.java", "RenamedListener.pass");
131
132             // check web.xml
133
checkFile(PROJECT_WEBAPP1, "web/WEB-INF/web.xml",
134                     new String JavaDoc[]{">abc.RenamedServlet<", ">abc.RenamedFilter<", ">abc.RenamedListener<"},
135                     new String JavaDoc[]{">abc.NewServlet<", ">abc.NewSimpleFilter<", ">abc.NewServletListener<"});
136
137         } catch (FileStateInvalidException e) {
138             fail(e.getMessage());
139         } catch (IOException JavaDoc e) {
140             fail(e.getMessage());
141         }
142     }
143
144     /** Renames
145      * session bean implementation class
146      * session bean local interface
147      * session bean local home interface
148      * session bean remote interface
149      * session bean remote home interface
150      * queue message bean impl class
151      * topic message bean impl class
152      * CMP entity bean impl class
153      * CMP entity bean local interface
154      * CMP entity bean local home interface
155      * BMP entity bean impl class
156      * BMP entity bean local interface
157      * BMP entity bean local home interface
158      * and checks renamed files and ejb-jar.xml for differences
159      */

160     public void testEjbRenameClass() throws FileStateInvalidException, IOException JavaDoc {
161         Helper.waitForAWTDispatchThread();
162         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewSessionBean", "RenamedSessionBean", "rename session bean");
163         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewSessionLocal", "RenamedSessionLocal",
164                 "rename session bean's local interface");
165         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewSessionLocalHome", "RenamedSessionLocalHome",
166                 "rename session bean's local home interface");
167         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewSessionRemote", "RenamedSessionRemote",
168                 "rename session bean's remote interface");
169         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewSessionRemoteHome", "RenamedSessionRemoteHome",
170                 "rename session bean's remote home interface");
171         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewMessageBean", "RenamedMessageBean", "rename queue message bean");
172         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewMessageTopicBean", "RenamedMessageTopicBean",
173                 "rename topic message bean");
174         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewEntityContainerBean", "RenamedEntityContainerBean",
175                 "rename entity container bean");
176         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewEntityContainerLocal", "RenamedEntityContainerLocal",
177                 "rename entity container bean's local interface");
178         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewEntityContainerLocalHome", "RenamedEntityContainerLocalHome",
179                 "rename entity container bean's local home interface");
180         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewEntityBeanMPBean", "RenamedEntityBeanMPBean",
181                 "rename entity bmp bean");
182         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewEntityBeanMPLocal", "RenamedEntityBeanMPLocal",
183                 "rename entity bmp bean's local interface");
184         doRenameRefactoring(PROJECT_EJBMOD1, "my.enterprise.beans.NewEntityBeanMPLocalHome", "RenamedEntityBeanMPLocalHome",
185                 "rename entity bmp bean's local home interface");
186         
187         Helper.waitForAWTDispatchThread();
188
189         // check modified files
190
try {
191             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedSessionBean.java",
192                     "RenamedSessionBean.pass");
193             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedSessionLocal.java",
194                     "RenamedSessionLocal.pass");
195             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedSessionLocalHome.java",
196                     "RenamedSessionLocalHome.pass");
197             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedSessionRemote.java",
198                     "RenamedSessionRemote.pass");
199             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedSessionRemoteHome.java",
200                     "RenamedSessionRemoteHome.pass");
201             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedMessageBean.java",
202                     "RenamedMessageBean.pass");
203             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedMessageTopicBean.java",
204                     "RenamedMessageTopicBean.pass");
205             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedEntityContainerBean.java",
206                     "RenamedEntityContainerBean.pass");
207             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedEntityContainerLocal.java",
208                     "RenamedEntityContainerLocal.pass");
209             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedEntityContainerLocalHome.java",
210                     "RenamedEntityContainerLocalHome.pass");
211             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedEntityBeanMPBean.java",
212                     "RenamedEntityBeanMPBean.pass");
213             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedEntityBeanMPLocal.java",
214                     "RenamedEntityBeanMPLocal.pass");
215             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/enterprise/beans/RenamedEntityBeanMPLocalHome.java",
216                     "RenamedEntityBeanMPLocalHome.pass");
217
218             // check ejb-jar
219
String JavaDoc[] mustContain = new String JavaDoc[]{
220                     ">my.enterprise.beans.RenamedSessionBean<",
221                     ">my.enterprise.beans.RenamedSessionLocal<",
222                     ">my.enterprise.beans.RenamedSessionLocalHome<",
223                     ">my.enterprise.beans.RenamedSessionRemote<",
224                     ">my.enterprise.beans.RenamedSessionRemoteHome<",
225                     ">my.enterprise.beans.RenamedMessageBean<",
226                     ">my.enterprise.beans.RenamedMessageTopicBean<",
227                     ">my.enterprise.beans.RenamedEntityContainerBean<",
228                     ">my.enterprise.beans.RenamedEntityContainerLocal<",
229                     ">my.enterprise.beans.RenamedEntityContainerLocalHome<",
230                     ">my.enterprise.beans.RenamedEntityBeanMPBean<",
231                     ">my.enterprise.beans.RenamedEntityBeanMPLocal<",
232                     ">my.enterprise.beans.RenamedEntityBeanMPLocalHome<"
233             };
234             String JavaDoc[] mustNotContain = new String JavaDoc[]{
235                     ">my.enterprise.beans.NewSessionBean<",
236                     ">my.enterprise.beans.NewSessionLocal<",
237                     ">my.enterprise.beans.NewSessionLocalHome<",
238                     ">my.enterprise.beans.NewSessionRemote<",
239                     ">my.enterprise.beans.NewSessionRemoteHome<",
240                     ">my.enterprise.beans.NewMessageBean<",
241                     ">my.enterprise.beans.NewMessageTopicBean<",
242                     ">my.enterprise.beans.NewEntityContainerBean<",
243                     ">my.enterprise.beans.NewEntityContainerLocal<",
244                     ">my.enterprise.beans.NewEntityContainerLocalHome<",
245                     ">my.enterprise.beans.NewEntityBeanMPBean<",
246                     ">my.enterprise.beans.NewEntityBeanMPLocal<",
247                     ">my.enterprise.beans.NewEntityBeanMPLocalHome<"
248             };
249
250             // wait for saving data to ejb-jar.xml
251
try {
252                 Thread.sleep(2000);
253             } catch (InterruptedException JavaDoc e) {
254                 //
255
}
256
257             checkFile(PROJECT_EJBMOD1, "src/conf/ejb-jar.xml", mustContain, mustNotContain);
258
259         } catch (FileStateInvalidException e) {
260             fail(e.getMessage());
261         } catch (IOException JavaDoc e) {
262             fail(e.getMessage());
263         }
264     }
265
266     /** Renames
267      * WS server impl bean
268      * WS server SEI interface
269      * and checks renamed files and wscompile config and ejb-jar.xml and webservices.xml for differences
270      */

271     public void testWSRenameClass() throws FileStateInvalidException, IOException JavaDoc {
272
273         doRenameRefactoring(PROJECT_EJBMOD1, "my.ws.server.NewWebServiceBean", "RenamedWebServiceBean", "rename ws server impl bean");
274         doRenameRefactoring(PROJECT_EJBMOD1, "my.ws.server.NewWebServiceSEI", "RenamedWebServiceSEI",
275                 "rename ws server servlet endpoint interface");
276
277         Helper.waitForAWTDispatchThread();
278
279         // check modified files
280
try {
281             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/ws/server/RenamedWebServiceBean.java", "RenamedWebServiceBean.pass");
282             checkGoldenFile(PROJECT_EJBMOD1, "src/java/my/ws/server/RenamedWebServiceSEI.java", "RenamedWebServiceSEI.pass");
283
284             // check wscompile config file
285
checkFile(PROJECT_EJBMOD1, "src/java/my/ws/server/NewWebService-config.xml",
286                     new String JavaDoc[]{"my.ws.server.RenamedWebServiceSEI", "my.ws.server.RenamedWebServiceBean"},
287                     new String JavaDoc[]{"my.ws.server.NewWebServiceSEI", "my.ws.server.NewWebServiceBean"});
288
289             // check ejb-jar
290
checkFile(PROJECT_EJBMOD1, "src/conf/ejb-jar.xml", new String JavaDoc[]{">my.ws.server.RenamedWebServiceBean<"},
291                     new String JavaDoc[]{">my.ws.server.NewWebServiceBean<"});
292
293             // check webservices.xml
294
checkFile(PROJECT_EJBMOD1, "src/conf/webservices.xml", new String JavaDoc[]{">my.ws.server.RenamedWebServiceSEI<"},
295                     new String JavaDoc[]{">my.ws.server.NewWebServiceSEI<"});
296
297             // check webservices.xml
298
checkFile(PROJECT_EJBMOD1, "src/conf/webservices.xml", new String JavaDoc[]{">wsdl-port_ns:RenamedWebServiceSEIPort<"},
299                     new String JavaDoc[]{">wsdl-port_ns:NewWebServiceSEIPort<"});
300                     
301         } catch (FileStateInvalidException e) {
302             fail(e.getMessage());
303         } catch (IOException JavaDoc e) {
304             fail(e.getMessage());
305         }
306     }
307
308     /**
309      * Opens projects for tests
310      */

311     public void testCleanup() {
312         closeProject(PROJECT_WEBAPP1);
313         closeProject(PROJECT_EJBMOD1);
314     }
315
316     /**
317      * Used for running test from inside the IDE by internal execution.
318      *
319      * @param args the command line arguments
320      */

321     public static void main(String JavaDoc[] args) {
322         TestRunner.run(suite());
323     }
324
325 }
326
Popular Tags