KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.junit.NbTestCase;
23 import org.netbeans.junit.ide.ProjectSupport;
24 import org.netbeans.jmi.javamodel.JavaClass;
25 import org.netbeans.jmi.javamodel.UnresolvedClass;
26 import org.netbeans.modules.refactoring.api.RenameRefactoring;
27 import org.netbeans.modules.refactoring.api.RefactoringSession;
28 import org.netbeans.modules.refactoring.api.MoveClassRefactoring;
29 import org.netbeans.modules.j2ee.refactoring.test.util.Helper;
30 import org.netbeans.modules.javacore.api.JavaModel;
31 import org.netbeans.api.java.classpath.ClassPath;
32 import org.netbeans.api.java.project.JavaProjectConstants;
33 import org.netbeans.api.project.Sources;
34 import org.netbeans.api.project.ProjectUtils;
35 import org.netbeans.api.project.FileOwnerQuery;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.api.project.SourceGroup;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.filesystems.FileObject;
40
41 import java.io.IOException JavaDoc;
42 import java.io.File JavaDoc;
43 import java.util.Collection JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Map JavaDoc;
46 import java.util.HashMap JavaDoc;
47
48 /**
49  * @author pfiala
50  */

51 public class J2EERefactoringTest extends NbTestCase {
52     private static Map JavaDoc projectMap = new HashMap JavaDoc();
53
54     public J2EERefactoringTest(String JavaDoc name) {
55         super(name);
56     }
57
58     protected void doRenameRefactoring(String JavaDoc project, String JavaDoc className, String JavaDoc newName, String JavaDoc sessionDescription) {
59         JavaClass javaClass = findClass(project, className);
60         RenameRefactoring refactoring = new RenameRefactoring(javaClass);
61         refactoring.preCheck();
62         refactoring.setNewName(newName);
63         refactoring.checkParameters();
64         RefactoringSession result = RefactoringSession.create(sessionDescription);
65         refactoring.prepare(result);
66         assertNull(result.doRefactoring(true));
67     }
68
69     protected void checkGoldenFile(String JavaDoc project, String JavaDoc relativePath, String JavaDoc goldenFileName) throws IOException JavaDoc {
70         assertFile(getProjectFile(project, relativePath), getGoldenFile(goldenFileName), getWorkDir());
71     }
72
73     protected String JavaDoc getProjectPath(String JavaDoc relativePath) {
74         return getDataDir().getAbsolutePath() + "/projects/" + relativePath;
75     }
76
77     protected void doMoveClassRefactoring(String JavaDoc sourceProject, String JavaDoc className, String JavaDoc targetProject,
78             String JavaDoc targetPackageName, String JavaDoc sessionDescription) {
79         RefactoringSession result = RefactoringSession.create(sessionDescription);
80         createMoveClassRefactoring(sourceProject, className, targetProject, targetPackageName).prepare(result);
81         assertNull(result.doRefactoring(true));
82     }
83
84     protected void doMoveClassRefactoring(String JavaDoc sourceProject, String JavaDoc className1, String JavaDoc className2, String JavaDoc className3,
85             String JavaDoc targetPackageName, String JavaDoc sessionDescription, String JavaDoc targetProject) {
86         RefactoringSession result = RefactoringSession.create(sessionDescription);
87         createMoveClassRefactoring(sourceProject, className1, className2, className3, targetProject, targetPackageName).prepare(result);
88         assertNull(result.doRefactoring(true));
89     }
90
91     protected MoveClassRefactoring createMoveClassRefactoring(String JavaDoc sourceProject, String JavaDoc className,
92             String JavaDoc targetProject, String JavaDoc targetPackage) {
93         JavaClass javaClass = findClass(sourceProject, className);
94         MoveClassRefactoring refactoring = new MoveClassRefactoring(javaClass);
95         prepareMoveClassRefactoring(refactoring, javaClass, targetPackage, targetProject);
96         return refactoring;
97     }
98
99     private void prepareMoveClassRefactoring(MoveClassRefactoring refactoring, JavaClass javaClass, String JavaDoc targetPackageName,
100             String JavaDoc projectName) {
101         refactoring.preCheck();
102         FileObject targetClassPathRoot;
103         if (projectName == null) {
104             FileObject fileObject = JavaModel.getFileObject(javaClass.getResource());
105             targetClassPathRoot = ClassPath.getClassPath(fileObject, ClassPath.SOURCE).getRoots()[0];
106         } else {
107             targetClassPathRoot = getProjectClasspathRoot(projectName);
108         }
109         refactoring.setTargetClassPathRoot(targetClassPathRoot);
110         refactoring.setTargetPackageName(targetPackageName);
111         refactoring.checkParameters();
112     }
113
114     private FileObject getProjectClasspathRoot(String JavaDoc projectName) {
115         FileObject targetClassPathRoot;
116         FileObject fileObject = FileUtil.toFileObject(getProjectFile(projectName, ""));
117         Sources sources = ProjectUtils.getSources(FileOwnerQuery.getOwner(fileObject));
118         SourceGroup[] sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
119         targetClassPathRoot = sourceGroups[0].getRootFolder();
120         return targetClassPathRoot;
121     }
122
123     private MoveClassRefactoring createMoveClassRefactoring(String JavaDoc sourceProject, String JavaDoc className1, String JavaDoc className2,
124             String JavaDoc className3, String JavaDoc targetProject, String JavaDoc targetPackage) {
125         JavaClass javaClass = findClass(sourceProject, className1);
126         Collection JavaDoc c = new ArrayList JavaDoc(3);
127         c.add(javaClass.getResource());
128         c.add(findClass(sourceProject, className2).getResource());
129         c.add(findClass(sourceProject, className3).getResource());
130         MoveClassRefactoring refactoring = new MoveClassRefactoring(c);
131         prepareMoveClassRefactoring(refactoring, javaClass, targetPackage, targetProject);
132         return refactoring;
133     }
134
135     protected void checkFile(String JavaDoc project, String JavaDoc relativePath, String JavaDoc[] mustContain, String JavaDoc[] mustNotContain) throws IOException JavaDoc{
136         File JavaDoc file = getProjectFile(project, relativePath);
137         String JavaDoc content = Helper.readFileContent(file);
138         if (mustContain != null) {
139             for (int i = 0; i < mustContain.length; i++) {
140                 String JavaDoc s = mustContain[i];
141                 if (!(content.indexOf(s) >= 0)) {
142                     copyFile(file);
143                     fail("File: " + file.getName() + " does not contain string: " + s);
144                 }
145             }
146         }
147         if (mustNotContain != null) {
148             for (int i = 0; i < mustNotContain.length; i++) {
149                 String JavaDoc s = mustNotContain[i];
150                 if (content.indexOf(s) >= 0) {
151                     copyFile(file);
152                     fail("File: " + file.getName() + " contains string: " + s);
153                 }
154
155             }
156         }
157     }
158
159     private void copyFile(File JavaDoc file) {
160         String JavaDoc fileName = file.getName();
161         try {
162             FileUtil.copyFile(FileUtil.toFileObject(file), FileUtil.toFileObject(getWorkDir()),
163                     fileName.substring(0, fileName.indexOf('.')));
164         } catch (Exception JavaDoc e) {
165             System.out.println("Exc: " + e);
166         }
167     }
168
169     protected void openProject(String JavaDoc projectName) {
170         projectMap.put(projectName, ProjectSupport.openProject(getProjectFile(projectName, "")));
171     }
172
173     protected void closeProject(String JavaDoc projectName) {
174         Project project = (Project) projectMap.remove(projectName);
175         if (project != null) {
176             ProjectSupport.closeProject(ProjectUtils.getInformation(project).getDisplayName());
177         }
178     }
179
180     protected File JavaDoc getProjectFile(String JavaDoc project, String JavaDoc relativePath) {
181         return new File JavaDoc(getProjectPath(project + "/" + relativePath));
182     }
183
184     public JavaClass findClass(String JavaDoc project, String JavaDoc className) {
185         for (int i = 0; i < 20; i++) {
186             ClassPath classPath = ClassPath.getClassPath(getProjectClasspathRoot(project), ClassPath.SOURCE);
187             JavaClass javaClass = findClass(className, classPath);
188             if (javaClass !=null) {
189                 return javaClass;
190             } else {
191                 try {
192                     Thread.sleep(1000);
193                 } catch (InterruptedException JavaDoc e) {
194                     e.printStackTrace();
195                     return null;
196                 }
197             }
198         }
199         throw new IllegalStateException JavaDoc("Class " + className + " not found.");
200     }
201
202     private static JavaClass findClass(String JavaDoc className, ClassPath classPath) {
203         JavaModel.getJavaRepository().beginTrans(false);
204         try {
205             JavaModel.setClassPath(classPath);
206             JavaClass javaClass = (JavaClass) JavaModel.getDefaultExtent().getType().resolve(className);
207             return javaClass instanceof UnresolvedClass ? null : javaClass;
208         } finally {
209             JavaModel.getJavaRepository().endTrans();
210         }
211     }
212 }
213
Popular Tags