1 11 12 package org.eclipse.jdt.internal.junit.buildpath; 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.net.URL ; 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 39 public class BuildPathSupport { 40 41 public static class JUnitPluginDescription { 42 private final String fBundleId; 43 private final VersionRange fVersionRange; 44 private final boolean fIsOrbitBundle; 45 46 public JUnitPluginDescription(String 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 version= (String ) curr.getHeaders().get(Constants.BUNDLE_VERSION); 58 try { 59 if (fVersionRange.isIncluded(Version.parseVersion(version))) { 60 return curr; 61 } 62 } catch (IllegalArgumentException e) { 63 } 65 } 66 } 67 return null; 68 } 69 70 public String 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); public static final JUnitPluginDescription JUNIT4_PLUGIN= new JUnitPluginDescription("org.junit4", new VersionRange("[4.3.1,4.4.0)"), false); 82 public static IPath getBundleLocation(JUnitPluginDescription pluginDesc) { 83 Bundle bundle= pluginDesc.getBundle(); 84 if (bundle == null) 85 return null; 86 87 URL local= null; 88 try { 89 local= FileLocator.toFileURL(bundle.getEntry("/")); } catch (IOException e) { 91 return null; 92 } 93 String fullPath= new File (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 version= (String )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); if (bundles != null && bundles.length > 0) { 111 sourceBundle= bundles[0]; 112 } 113 } else { 114 sourceBundle= JUnitPlugin.getDefault().getBundle("org.eclipse.jdt.source"); } 116 if (sourceBundle == null) { 117 return null; 118 } 119 URL local= null; 120 try { 121 local= FileLocator.toFileURL(sourceBundle.getEntry("/")); } catch (IOException e) { 123 return null; 124 } 125 String fullPath= new File (local.getPath()).getAbsolutePath() 126 + File.separator + "src" + File.separator + pluginDesc.getBundleId() + "_" + version; 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"); 143 IPath sourceBase= getSourceLocation(JUNIT3_PLUGIN); 144 IPath srcLocation= sourceBase != null ? sourceBase.append("junitsrc.zip") : null; 146 IAccessRule[] accessRules= { }; 147 148 String 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"); 161 IPath sourceBase= getSourceLocation(JUNIT4_PLUGIN); 162 IPath srcLocation= sourceBase != null ? sourceBase.append("junitsrc.zip") : null; 164 IAccessRule[] accessRules= { }; 165 166 String 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 |