1 13 14 package org.eclipse.jdt.internal.junit.launcher; 15 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Set ; 20 import java.util.Map.Entry; 21 22 import org.eclipse.core.runtime.CoreException; 23 24 import org.eclipse.debug.core.ILaunchConfiguration; 25 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 26 27 import org.eclipse.jdt.core.IJavaElement; 28 import org.eclipse.jdt.core.IJavaProject; 29 import org.eclipse.jdt.core.IType; 30 31 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 32 33 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 34 35 public class JUnitLaunchDescription { 36 static final String [] ATTRIBUTES_THAT_MUST_MATCH = new String [] { 37 IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, 38 JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, 39 IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, 40 JUnitBaseLaunchConfiguration.TESTNAME_ATTR 41 }; 42 43 static final String EMPTY= ""; 45 private static final String DEFAULT_VALUE= ""; 47 private Map fAttributes= new HashMap (); 48 49 private final IJavaElement fElement; 50 51 private final String fName; 52 53 public JUnitLaunchDescription(IJavaElement element, String name) { 54 fElement= element; 55 fName= name; 56 setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, getProjectName()); 57 } 58 59 public void copyAttributesInto(ILaunchConfigurationWorkingCopy wc) { 60 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, getProjectName()); 61 wc.setAttribute(JUnitBaseLaunchConfiguration.ATTR_KEEPRUNNING, false); 62 63 Set definedAttributes= getDefinedAttributes(); 64 for (Iterator iter= definedAttributes.iterator(); iter.hasNext();) { 65 Entry attribute= (Entry) iter.next(); 66 wc.setAttribute((String ) attribute.getKey(), (String ) attribute.getValue()); 67 } 68 } 69 70 public boolean equals(Object arg0) { 71 JUnitLaunchDescription desc = (JUnitLaunchDescription) arg0; 72 return areEqual(desc.fElement, fElement) && areEqual(desc.fName, fName) 73 && areEqual(desc.fAttributes, fAttributes); 74 } 75 76 public String getAttribute(String attr) { 77 if (fAttributes.containsKey(attr)) 78 return (String ) fAttributes.get(attr); 79 return DEFAULT_VALUE; 80 } 81 82 public String getContainer() { 83 return getAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR); 84 } 85 86 public Set getDefinedAttributes() { 87 return fAttributes.entrySet(); 88 } 89 90 public IJavaElement getElement() { 91 return fElement; 92 } 93 94 public String getName() { 95 return fName; 96 } 97 98 public JUnitLaunchDescription setContainer(String handleIdentifier) { 99 return setAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, handleIdentifier); 100 } 101 102 public JUnitLaunchDescription setMainType(String mainType) { 103 return setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mainType); 104 } 105 106 public JUnitLaunchDescription setTestKind(String testKindId) { 107 return setAttribute(JUnitBaseLaunchConfiguration.TEST_KIND_ATTR, testKindId); 108 } 109 110 public JUnitLaunchDescription setTestName(String testName) { 111 return setAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, testName); 112 } 113 114 public String toString() { 115 return "JUnitLaunchDescription(" + fName + ")"; } 117 118 protected String getProjectName() { 119 IJavaProject project = getProject(); 120 return project == null ? null : project.getElementName(); 121 } 122 123 boolean attributesMatch(ILaunchConfiguration config) throws CoreException { 124 for (int i = 0; i < ATTRIBUTES_THAT_MUST_MATCH.length; i++) { 125 if (! configurationMatches(ATTRIBUTES_THAT_MUST_MATCH[i], config)) { 126 return false; 127 } 128 } 129 return true; 130 } 131 132 boolean configurationMatches(final String attributeName, ILaunchConfiguration config) throws CoreException { 133 return config.getAttribute(attributeName, EMPTY).equals(getAttribute(attributeName)); 134 } 135 136 void setMainType(IType type) { 137 setMainType(JavaModelUtil.getFullyQualifiedName(type)); 138 } 139 140 private boolean areEqual(Object thing, Object otherThing) { 141 if (thing == null) 142 return otherThing == null; 143 return thing.equals(otherThing); 144 } 145 146 public IJavaProject getProject() { 147 return fElement == null ? null : fElement.getJavaProject(); 148 } 149 150 private JUnitLaunchDescription setAttribute(String attr, String value) { 151 fAttributes.put(attr, value); 152 return this; 153 } 154 } 155 | Popular Tags |