KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > buildpath > BuildPathSupport


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.junit.buildpath;
13
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.net.URL JavaDoc;
17
18 import org.eclipse.core.runtime.FileLocator;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21
22 import org.eclipse.osgi.service.resolver.VersionRange;
23
24 import org.eclipse.jdt.core.IAccessRule;
25 import org.eclipse.jdt.core.IClasspathAttribute;
26 import org.eclipse.jdt.core.IClasspathEntry;
27 import org.eclipse.jdt.core.JavaCore;
28
29 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
30 import org.eclipse.jdt.internal.junit.ui.JUnitPreferencesConstants;
31
32 import org.osgi.framework.Bundle;
33 import org.osgi.framework.Constants;
34 import org.osgi.framework.Version;
35
36 /**
37  *
38  */

39 public class BuildPathSupport {
40     
41     public static class JUnitPluginDescription {
42         private final String JavaDoc fBundleId;
43         private final VersionRange fVersionRange;
44         private final boolean fIsOrbitBundle;
45         
46         public JUnitPluginDescription(String JavaDoc bundleId, VersionRange versionRange, boolean isOrbitBundle) {
47             fBundleId= bundleId;
48             fVersionRange= versionRange;
49             fIsOrbitBundle= isOrbitBundle;
50         }
51         
52         public Bundle getBundle() {
53             Bundle[] bundles= JUnitPlugin.getDefault().getBundles(fBundleId, null);
54             if (bundles != null) {
55                 for (int i= 0; i < bundles.length; i++) {
56                     Bundle curr= bundles[i];
57                     String JavaDoc version= (String JavaDoc) curr.getHeaders().get(Constants.BUNDLE_VERSION);
58                     try {
59                         if (fVersionRange.isIncluded(Version.parseVersion(version))) {
60                             return curr;
61                         }
62                     } catch (IllegalArgumentException JavaDoc e) {
63                         // ignore
64
}
65                 }
66             }
67             return null;
68         }
69         
70         public String JavaDoc getBundleId() {
71             return fBundleId;
72         }
73         
74         public boolean isOrbitBundle() {
75             return fIsOrbitBundle;
76         }
77     }
78     
79     public static final JUnitPluginDescription JUNIT3_PLUGIN= new JUnitPluginDescription("org.junit", new VersionRange("[3.8.2,3.9)"), true); //$NON-NLS-1$//$NON-NLS-2$
80
public static final JUnitPluginDescription JUNIT4_PLUGIN= new JUnitPluginDescription("org.junit4", new VersionRange("[4.3.1,4.4.0)"), false); //$NON-NLS-1$ //$NON-NLS-2$
81

82     public static IPath getBundleLocation(JUnitPluginDescription pluginDesc) {
83         Bundle bundle= pluginDesc.getBundle();
84         if (bundle == null)
85             return null;
86         
87         URL JavaDoc local= null;
88         try {
89             local= FileLocator.toFileURL(bundle.getEntry("/")); //$NON-NLS-1$
90
} catch (IOException JavaDoc e) {
91             return null;
92         }
93         String JavaDoc fullPath= new File JavaDoc(local.getPath()).getAbsolutePath();
94         return Path.fromOSString(fullPath);
95     }
96     
97     public static IPath getSourceLocation(JUnitPluginDescription pluginDesc) {
98         Bundle bundle= pluginDesc.getBundle();
99         if (bundle == null)
100             return null;
101             
102         String JavaDoc version= (String JavaDoc)bundle.getHeaders().get(Constants.BUNDLE_VERSION);
103         if (version == null) {
104             return null;
105         }
106
107         Bundle sourceBundle= null;
108         if (pluginDesc.isOrbitBundle()) {
109              Bundle[] bundles= JUnitPlugin.getDefault().getBundles(pluginDesc.getBundleId() + ".source", version); //$NON-NLS-1$
110
if (bundles != null && bundles.length > 0) {
111                 sourceBundle= bundles[0];
112              }
113         } else {
114             sourceBundle= JUnitPlugin.getDefault().getBundle("org.eclipse.jdt.source"); //$NON-NLS-1$
115
}
116         if (sourceBundle == null) {
117             return null;
118         }
119         URL JavaDoc local= null;
120         try {
121             local= FileLocator.toFileURL(sourceBundle.getEntry("/")); //$NON-NLS-1$
122
} catch (IOException JavaDoc e) {
123             return null;
124         }
125         String JavaDoc fullPath= new File JavaDoc(local.getPath()).getAbsolutePath()
126             + File.separator + "src" + File.separator + pluginDesc.getBundleId() + "_" + version; //$NON-NLS-1$ //$NON-NLS-2$
127
return Path.fromOSString(fullPath);
128     }
129     
130     public static IClasspathEntry getJUnit3ClasspathEntry() {
131         return JavaCore.newContainerEntry(JUnitContainerInitializer.JUNIT3_PATH);
132     }
133     
134     public static IClasspathEntry getJUnit4ClasspathEntry() {
135         return JavaCore.newContainerEntry(JUnitContainerInitializer.JUNIT4_PATH);
136     }
137     
138     public static IClasspathEntry getJUnit3LibraryEntry() {
139         IPath bundleBase= getBundleLocation(JUNIT3_PLUGIN);
140         if (bundleBase != null) {
141             IPath jarLocation= bundleBase.append("junit.jar"); //$NON-NLS-1$
142

143             IPath sourceBase= getSourceLocation(JUNIT3_PLUGIN);
144             IPath srcLocation= sourceBase != null ? sourceBase.append("junitsrc.zip") : null; //$NON-NLS-1$
145

146             IAccessRule[] accessRules= { };
147             
148             String JavaDoc javadocLocation= JUnitPlugin.getDefault().getPreferenceStore().getString(JUnitPreferencesConstants.JUNIT3_JAVADOC);
149             IClasspathAttribute[] attributes= { JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadocLocation) };
150             
151             return JavaCore.newLibraryEntry(jarLocation, srcLocation, null, accessRules, attributes, false);
152         }
153         return null;
154     }
155     
156     public static IClasspathEntry getJUnit4LibraryEntry() {
157         IPath bundleBase= getBundleLocation(JUNIT4_PLUGIN);
158         if (bundleBase != null) {
159             IPath jarLocation= bundleBase.append("junit.jar"); //$NON-NLS-1$
160

161             IPath sourceBase= getSourceLocation(JUNIT4_PLUGIN);
162             IPath srcLocation= sourceBase != null ? sourceBase.append("junitsrc.zip") : null; //$NON-NLS-1$
163

164             IAccessRule[] accessRules= { };
165             
166             String JavaDoc javadocLocation= JUnitPlugin.getDefault().getPreferenceStore().getString(JUnitPreferencesConstants.JUNIT4_JAVADOC);
167             IClasspathAttribute[] attributes= { JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadocLocation) };
168             
169             return JavaCore.newLibraryEntry(jarLocation, srcLocation, null, accessRules, attributes, false);
170         }
171         return null;
172     }
173 }
174
Popular Tags