1 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 ; 42 import java.io.File ; 43 import java.util.Collection ; 44 import java.util.ArrayList ; 45 import java.util.Map ; 46 import java.util.HashMap ; 47 48 51 public class J2EERefactoringTest extends NbTestCase { 52 private static Map projectMap = new HashMap (); 53 54 public J2EERefactoringTest(String name) { 55 super(name); 56 } 57 58 protected void doRenameRefactoring(String project, String className, String newName, String 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 project, String relativePath, String goldenFileName) throws IOException { 70 assertFile(getProjectFile(project, relativePath), getGoldenFile(goldenFileName), getWorkDir()); 71 } 72 73 protected String getProjectPath(String relativePath) { 74 return getDataDir().getAbsolutePath() + "/projects/" + relativePath; 75 } 76 77 protected void doMoveClassRefactoring(String sourceProject, String className, String targetProject, 78 String targetPackageName, String 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 sourceProject, String className1, String className2, String className3, 85 String targetPackageName, String sessionDescription, String 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 sourceProject, String className, 92 String targetProject, String 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 targetPackageName, 100 String 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 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 sourceProject, String className1, String className2, 124 String className3, String targetProject, String targetPackage) { 125 JavaClass javaClass = findClass(sourceProject, className1); 126 Collection c = new ArrayList (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 project, String relativePath, String [] mustContain, String [] mustNotContain) throws IOException { 136 File file = getProjectFile(project, relativePath); 137 String content = Helper.readFileContent(file); 138 if (mustContain != null) { 139 for (int i = 0; i < mustContain.length; i++) { 140 String 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 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 file) { 160 String fileName = file.getName(); 161 try { 162 FileUtil.copyFile(FileUtil.toFileObject(file), FileUtil.toFileObject(getWorkDir()), 163 fileName.substring(0, fileName.indexOf('.'))); 164 } catch (Exception e) { 165 System.out.println("Exc: " + e); 166 } 167 } 168 169 protected void openProject(String projectName) { 170 projectMap.put(projectName, ProjectSupport.openProject(getProjectFile(projectName, ""))); 171 } 172 173 protected void closeProject(String 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 getProjectFile(String project, String relativePath) { 181 return new File (getProjectPath(project + "/" + relativePath)); 182 } 183 184 public JavaClass findClass(String project, String 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 e) { 194 e.printStackTrace(); 195 return null; 196 } 197 } 198 } 199 throw new IllegalStateException ("Class " + className + " not found."); 200 } 201 202 private static JavaClass findClass(String 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 |