1 /******************************************************************************* 2 * Copyright (c) 2000, 2016 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 * Mirko Raner <mirko@raner.ws> - Expose JUnitModel.exportTestRunSession(...) as API - https://bugs.eclipse.org/316199 14 *******************************************************************************/ 15 16 package org.eclipse.jdt.junit; 17 18 19 import java.io.File; 20 import java.io.OutputStream; 21 import java.lang.reflect.InvocationTargetException; 22 import java.util.HashSet; 23 import java.util.Set; 24 25 import javax.xml.transform.TransformerException; 26 27 import org.eclipse.jdt.junit.model.ITestRunSession; 28 29 import org.eclipse.core.runtime.CoreException; 30 import org.eclipse.core.runtime.IPath; 31 import org.eclipse.core.runtime.IProgressMonitor; 32 import org.eclipse.core.runtime.IStatus; 33 import org.eclipse.core.runtime.OperationCanceledException; 34 import org.eclipse.core.runtime.Path; 35 import org.eclipse.core.runtime.Status; 36 37 import org.eclipse.jdt.core.IClasspathContainer; 38 import org.eclipse.jdt.core.IJavaElement; 39 import org.eclipse.jdt.core.IType; 40 41 import org.eclipse.jdt.internal.junit.JUnitCorePlugin; 42 import org.eclipse.jdt.internal.junit.launcher.JUnit4TestFinder; 43 import org.eclipse.jdt.internal.junit.model.JUnitModel; 44 import org.eclipse.jdt.internal.junit.model.ModelMessages; 45 import org.eclipse.jdt.internal.junit.model.TestRunSession; 46 47 /** 48 * Class for accessing JUnit support; all functionality is provided by 49 * static methods. 50 * <p> 51 * This class is not intended to be subclassed or instantiated by clients. 52 * </p> 53 * 54 * @since 2.1 55 * 56 * @noextend This class is not intended to be subclassed by clients. 57 * @noinstantiate This class is not intended to be instantiated by clients. 58 */ 59 public class JUnitCore { 60 61 /** 62 * ID of the JUnit {@linkplain IClasspathContainer classpath container}. 63 * The general format of classpath entries using this container is unspecified. 64 * 65 * @see #JUNIT3_CONTAINER_PATH 66 * @see #JUNIT4_CONTAINER_PATH 67 * @see #JUNIT5_CONTAINER_PATH 68 * @since 3.6 69 */ 70 public static final String JUNIT_CONTAINER_ID= "org.eclipse.jdt.junit.JUNIT_CONTAINER"; //$NON-NLS-1$ 71 72 /** 73 * Path of the JUnit 3 {@linkplain IClasspathContainer classpath container}. 74 * 75 * @since 3.6 76 */ 77 public final static IPath JUNIT3_CONTAINER_PATH= new Path(JUNIT_CONTAINER_ID).append("3"); //$NON-NLS-1$ 78 79 /** 80 * Path of the JUnit 4 {@linkplain IClasspathContainer classpath container}. 81 * 82 * @since 3.6 83 */ 84 public final static IPath JUNIT4_CONTAINER_PATH= new Path(JUNIT_CONTAINER_ID).append("4"); //$NON-NLS-1$ 85 86 /** 87 * Path of the JUnit 5 {@linkplain IClasspathContainer classpath container}. 88 * 89 * @since 3.10 90 */ 91 public final static IPath JUNIT5_CONTAINER_PATH= new Path(JUNIT_CONTAINER_ID).append("5"); //$NON-NLS-1$ 92 93 /** 94 * Adds a listener for test runs. 95 * 96 * @param listener listener to be added 97 * @deprecated As of 3.3, replaced by {@link #addTestRunListener(TestRunListener)} 98 */ 99 @Deprecated addTestRunListener(ITestRunListener listener)100 public static void addTestRunListener(ITestRunListener listener) { 101 JUnitCorePlugin.getDefault().addTestRunListener(listener); 102 } 103 104 /** 105 * Removes a listener for test runs. 106 * 107 * @param listener listener to be removed 108 * @deprecated As of 3.3, replaced by {@link #removeTestRunListener(TestRunListener)} 109 */ 110 @Deprecated removeTestRunListener(ITestRunListener listener)111 public static void removeTestRunListener(ITestRunListener listener) { 112 JUnitCorePlugin.getDefault().removeTestRunListener(listener); 113 } 114 115 /** 116 * Adds a listener for test runs. 117 * <p> 118 * <strong>Note:</strong> If your plug-in should be loaded when a test run starts, 119 * please contribute to the <code>org.eclipse.jdt.junit.testRunListeners</code> extension point instead. 120 * </p> 121 * 122 * @param listener the listener to be added 123 * @since 3.3 124 */ addTestRunListener(TestRunListener listener)125 public static void addTestRunListener(TestRunListener listener) { 126 JUnitCorePlugin.getDefault().getNewTestRunListeners().add(listener); 127 } 128 129 /** 130 * Removes a listener for test runs. 131 * 132 * @param listener the listener to be removed 133 * @since 3.3 134 */ removeTestRunListener(TestRunListener listener)135 public static void removeTestRunListener(TestRunListener listener) { 136 JUnitCorePlugin.getDefault().getNewTestRunListeners().remove(listener); 137 } 138 139 /** 140 * Finds types that contain JUnit tests in the given container. 141 * 142 * @param container the container 143 * @param monitor the progress monitor used to report progress and request cancelation, 144 * or <code>null</code> if none 145 * @return test types inside the given container 146 * @throws CoreException when a problem occurs while accessing <code>container</code> or its children 147 * @throws OperationCanceledException if the operation has been canceled 148 * 149 * @since 3.5 150 */ findTestTypes(IJavaElement container, IProgressMonitor monitor)151 public static IType[] findTestTypes(IJavaElement container, IProgressMonitor monitor) throws CoreException, OperationCanceledException { 152 final Set<IType> result= new HashSet<>(); 153 JUnit4TestFinder finder= new JUnit4TestFinder(); 154 finder.findTestsInContainer(container, result, monitor); 155 156 return result.toArray(new IType[result.size()]); 157 } 158 159 /** 160 * Exports the given test run session into an XML report file. 161 * 162 * @param testRunSession the test run session 163 * @param file the destination 164 * @throws CoreException if an error occurred 165 * 166 * @since 3.7 167 */ exportTestRunSession(ITestRunSession testRunSession, File file)168 public static void exportTestRunSession(ITestRunSession testRunSession, File file) throws CoreException { 169 JUnitModel.exportTestRunSession((TestRunSession)testRunSession, file); 170 } 171 172 /** 173 * Exports the given test run session to an output stream. 174 * 175 * @param testRunSession the test run session 176 * @param output the output stream 177 * @throws CoreException if an error occurred 178 * 179 * @since 3.7 180 */ exportTestRunSession(ITestRunSession testRunSession, OutputStream output)181 public static void exportTestRunSession(ITestRunSession testRunSession, OutputStream output) throws CoreException { 182 try { 183 JUnitModel.exportTestRunSession((TestRunSession)testRunSession, output); 184 185 } catch (TransformerException exception) { 186 String pluginID= JUnitCorePlugin.getPluginId(); 187 String message= ModelMessages.JUnitModel_could_not_export; 188 throw new CoreException(new Status(IStatus.ERROR, pluginID, message, exception)); 189 } 190 } 191 192 /** 193 * Imports a test run session from the given file. 194 * 195 * @param file a file containing a test run session transcript 196 * @return the imported test run session 197 * @throws CoreException if the import failed 198 * 199 * @since 3.7 200 */ importTestRunSession(File file)201 public static ITestRunSession importTestRunSession(File file) throws CoreException { 202 return JUnitModel.importTestRunSession(file); 203 } 204 205 /** 206 * Imports a test run session from the given URL. 207 * 208 * @param url an URL to a test run session transcript 209 * @param monitor a progress monitor for cancellation 210 * @return the imported test run session, or <code>null</code> if the import has been cancelled 211 * @throws CoreException if the import failed 212 * 213 * @since 3.7 214 */ importTestRunSession(final String url, IProgressMonitor monitor)215 public static ITestRunSession importTestRunSession(final String url, IProgressMonitor monitor) throws CoreException { 216 try { 217 return JUnitModel.importTestRunSession(url, monitor); 218 219 } catch (InvocationTargetException exception) { 220 String pluginID= JUnitCorePlugin.getPluginId(); 221 String message= ModelMessages.JUnitModel_could_not_import; 222 Throwable throwable= exception.getCause() != null ? exception.getCause() : exception; 223 throw new CoreException(new Status(IStatus.ERROR, pluginID, message, throwable)); 224 225 } catch (InterruptedException interrupt) { 226 return null; 227 } 228 } 229 } 230