KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > launcher > JUnitLaunchDescription


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * David Saff (saff@mit.edu) - initial API and implementation
11  * (bug 102632: [JUnit] Support for JUnit 4.)
12  *******************************************************************************/

13
14 package org.eclipse.jdt.internal.junit.launcher;
15
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
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 JavaDoc[] ATTRIBUTES_THAT_MUST_MATCH = new String JavaDoc[] {
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 JavaDoc EMPTY= ""; //$NON-NLS-1$
44

45     private static final String JavaDoc DEFAULT_VALUE= ""; //$NON-NLS-1$
46

47     private Map JavaDoc fAttributes= new HashMap JavaDoc();
48
49     private final IJavaElement fElement;
50
51     private final String JavaDoc fName;
52
53     public JUnitLaunchDescription(IJavaElement element, String JavaDoc 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 JavaDoc definedAttributes= getDefinedAttributes();
64         for (Iterator JavaDoc iter= definedAttributes.iterator(); iter.hasNext();) {
65             Entry attribute= (Entry) iter.next();
66             wc.setAttribute((String JavaDoc) attribute.getKey(), (String JavaDoc) attribute.getValue());
67         }
68     }
69
70     public boolean equals(Object JavaDoc 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 JavaDoc getAttribute(String JavaDoc attr) {
77         if (fAttributes.containsKey(attr))
78             return (String JavaDoc) fAttributes.get(attr);
79         return DEFAULT_VALUE;
80     }
81
82     public String JavaDoc getContainer() {
83         return getAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR);
84     }
85
86     public Set JavaDoc getDefinedAttributes() {
87         return fAttributes.entrySet();
88     }
89
90     public IJavaElement getElement() {
91         return fElement;
92     }
93
94     public String JavaDoc getName() {
95         return fName;
96     }
97
98     public JUnitLaunchDescription setContainer(String JavaDoc handleIdentifier) {
99         return setAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, handleIdentifier);
100     }
101
102     public JUnitLaunchDescription setMainType(String JavaDoc mainType) {
103         return setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mainType);
104     }
105
106     public JUnitLaunchDescription setTestKind(String JavaDoc testKindId) {
107         return setAttribute(JUnitBaseLaunchConfiguration.TEST_KIND_ATTR, testKindId);
108     }
109
110     public JUnitLaunchDescription setTestName(String JavaDoc testName) {
111         return setAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, testName);
112     }
113
114     public String JavaDoc toString() {
115         return "JUnitLaunchDescription(" + fName + ")"; //$NON-NLS-1$//$NON-NLS-2$
116
}
117
118     protected String JavaDoc 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 JavaDoc 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 JavaDoc thing, Object JavaDoc 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 JavaDoc attr, String JavaDoc value) {
151         fAttributes.put(attr, value);
152         return this;
153     }
154 }
155
Popular Tags