001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.junit.runtime;
011:
012: import junit.framework.Assert;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IExtension;
017: import org.eclipse.core.runtime.IPlatformRunnable;
018: import org.eclipse.core.runtime.IProduct;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.ui.PlatformUI;
021: import org.eclipse.ui.testing.ITestHarness;
022: import org.eclipse.ui.testing.TestableObject;
023:
024: /**
025: * A Workbench that runs a test suite specified in the
026: * command line arguments.
027: */
028: public class LegacyUITestApplication implements IPlatformRunnable,
029: ITestHarness {
030:
031: private static final String DEFAULT_APP_3_0 = "org.eclipse.ui.ide.workbench"; //$NON-NLS-1$
032:
033: private TestableObject fTestableObject;
034:
035: /* (non-Javadoc)
036: * @see org.eclipse.core.runtime.IPlatformRunnable#run(java.lang.Object)
037: */
038: public Object run(final Object args) throws Exception {
039: IPlatformRunnable application = getApplication((String[]) args);
040:
041: Assert.assertNotNull(application);
042:
043: fTestableObject = PlatformUI.getTestableObject();
044: fTestableObject.setTestHarness(this );
045: return application.run(args);
046: }
047:
048: /*
049: * return the application to run, or null if not even the default application
050: * is found.
051: */
052: private IPlatformRunnable getApplication(String[] args)
053: throws CoreException {
054: // Find the name of the application as specified by the PDE JUnit launcher.
055: // If no application is specified, the 3.0 default workbench application
056: // is returned.
057: IExtension extension = Platform.getExtensionRegistry()
058: .getExtension(Platform.PI_RUNTIME,
059: Platform.PT_APPLICATIONS,
060: getApplicationToRun(args));
061:
062: Assert.assertNotNull(extension);
063:
064: // If the extension does not have the correct grammar, return null.
065: // Otherwise, return the application object.
066: IConfigurationElement[] elements = extension
067: .getConfigurationElements();
068: if (elements.length > 0) {
069: IConfigurationElement[] runs = elements[0]
070: .getChildren("run"); //$NON-NLS-1$
071: if (runs.length > 0) {
072: Object runnable = runs[0]
073: .createExecutableExtension("class"); //$NON-NLS-1$
074: if (runnable instanceof IPlatformRunnable)
075: return (IPlatformRunnable) runnable;
076: }
077: }
078: return null;
079: }
080:
081: /*
082: * The -testApplication argument specifies the application to be run.
083: * If the PDE JUnit launcher did not set this argument, then return
084: * the name of the default application.
085: * In 3.0, the default is the "org.eclipse.ui.ide.worbench" application.
086: *
087: */
088: private String getApplicationToRun(String[] args) {
089: IProduct product = Platform.getProduct();
090: if (product != null)
091: return product.getApplication();
092: for (int i = 0; i < args.length; i++) {
093: if (args[i].equals("-testApplication") && i < args.length - 1) //$NON-NLS-1$
094: return args[i + 1];
095: }
096: return DEFAULT_APP_3_0;
097: }
098:
099: /* (non-Javadoc)
100: * @see org.eclipse.ui.testing.ITestHarness#runTests()
101: */
102: public void runTests() {
103: fTestableObject.testingStarting();
104: fTestableObject.runTest(new Runnable() {
105: public void run() {
106: RemotePluginTestRunner.main(Platform
107: .getCommandLineArgs());
108: }
109: });
110: fTestableObject.testingFinished();
111: }
112:
113: }
|