1 11 package org.eclipse.jdt.internal.junit.launcher; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.Path; 15 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 19 import org.eclipse.debug.core.ILaunchConfiguration; 20 import org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate; 21 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 22 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.IJavaModel; 25 import org.eclipse.jdt.core.IJavaProject; 26 import org.eclipse.jdt.core.JavaCore; 27 28 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 29 30 public class JUnitMigrationDelegate implements ILaunchConfigurationMigrationDelegate { 31 32 protected static final String EMPTY_STRING= ""; 34 public JUnitMigrationDelegate() { 35 36 } 37 38 43 public boolean isCandidate(ILaunchConfiguration candidate) throws CoreException { 44 IResource[] mapped = candidate.getMappedResources(); 45 IResource target = getResource(candidate); 46 if (target == null) { 47 return mapped == null; 48 } else { 49 if (mapped == null) { 50 return true; 51 } else { 52 if (mapped.length != 1) { 53 return true; 54 } else { 55 return !target.equals(mapped[0]); 56 } 57 } 58 } 59 } 60 61 66 public void migrate(ILaunchConfiguration candidate) throws CoreException { 67 ILaunchConfigurationWorkingCopy wc= candidate.getWorkingCopy(); 68 mapResources(wc); 69 wc.doSave(); 70 } 71 72 78 public static void mapResources(ILaunchConfigurationWorkingCopy config) throws CoreException { 79 IResource resource = getResource(config); 80 if (resource == null) { 81 config.setMappedResources(null); 82 } else { 83 config.setMappedResources(new IResource[]{resource}); 84 } 85 } 86 87 95 private static IResource getResource(ILaunchConfiguration config) throws CoreException { 96 String projName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String )null); 97 String containerHandle = config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, (String )null); 98 String typeName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String )null); 99 IJavaElement element = null; 100 if (projName != null && Path.ROOT.isValidSegment(projName)) { 101 IJavaProject javaProject = getJavaModel().getJavaProject(projName); 102 if (javaProject.exists()) { 103 if (typeName != null) { 104 element = javaProject.findType(typeName); 105 } 106 if (element == null) { 107 element = javaProject; 108 } 109 } 110 } else if (containerHandle != null) { 111 element = JavaCore.create(containerHandle); 112 } 113 IResource resource = null; 114 if (element != null) { 115 resource = element.getResource(); 116 } 117 return resource; 118 } 119 120 123 private static IJavaModel getJavaModel() { 124 return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 125 } 126 127 } 128 | Popular Tags |