You are on page 1of 2

/******************************************************************************* * Copyright (c) 2009 IBM Corporation and others. * All rights reserved.

This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation ******************************************************************************/ package org.eclipse.ui.incubator.testrunner.handlers; import import import import import import import import import import import import import import import org.eclipse.core.commands.AbstractHandler; org.eclipse.core.commands.ExecutionEvent; org.eclipse.core.commands.ExecutionException; org.eclipse.core.runtime.CoreException; org.eclipse.core.runtime.IProgressMonitor; org.eclipse.core.runtime.IStatus; org.eclipse.core.runtime.MultiStatus; org.eclipse.core.runtime.Status; org.eclipse.core.runtime.jobs.Job; org.eclipse.debug.core.DebugPlugin; org.eclipse.debug.core.ILaunch; org.eclipse.debug.core.ILaunchConfiguration; org.eclipse.debug.core.ILaunchConfigurationType; org.eclipse.debug.core.ILaunchManager; org.eclipse.ui.incubator.testrunner.Activator;

/** * Our sample handler extends AbstractHandler, an IHandler base class. * @see org.eclipse.core.commands.IHandler * @see org.eclipse.core.commands.AbstractHandler */ public class LaunchTestsHandler extends AbstractHandler { /** * The constructor. */ public LaunchTestsHandler() { } /** * the command has been executed, so extract extract the needed informat ion * from the application context. */ public Object execute(ExecutionEvent event) throws ExecutionException { new TestsLauncherJob().schedule(); return null; } class TestsLauncherJob extends Job { public TestsLauncherJob() { super("Tests Launcher Job"); } @Override protected IStatus run(IProgressMonitor monitor) {

ILaunchManager launchManager = DebugPlugin.getDefault(). getLaunchManager(); ILaunchConfigurationType junitLaunch = launchManager.get LaunchConfigurationType("org.eclipse.pde.ui.JunitLaunchConfig"); ILaunchConfiguration[] launchConfigurations; MultiStatus testStatus = new MultiStatus(Activator.PLUGI N_ID, 0, "Test results", null); try { launchConfigurations = launchManager.getLaunchCo nfigurations(junitLaunch); monitor.beginTask("Launching tests...", launchCo nfigurations.length); outer: for (ILaunchConfiguration iLaunchConfiguration : launchConfigurations) { try { ILaunch launch = iLaunchConfigur ation.launch(ILaunchManager.RUN_MODE, null); monitor.subTask(iLaunchConfigura tion.getName() + " running."); do { Thread.sleep(5 * 1000); // sleep for 5 secs if (monitor.isCanceled() ) { if (launch.canTe rminate()) { monitor. subTask("Terminating the launch"); launch.t erminate(); } break outer; } } while (!launch.isTerminated()) ; monitor.worked(1); } catch (Exception e) { IStatus status = new Status(ISta tus.ERROR, Activator.PLUGIN_ID, "Error launching test: " + iLaunchConfiguration. getName(), e); testStatus.add(status); } } } catch (CoreException e) { IStatus status = new Status(IStatus.ERROR, Activ ator.PLUGIN_ID, "Error launching tests", e); testStatus.add(status); } return testStatus; } } }

You might also like