001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 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.equinox.app.IApplication;
021: import org.eclipse.equinox.app.IApplicationContext;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.testing.ITestHarness;
024: import org.eclipse.ui.testing.TestableObject;
025:
026: /**
027: * A Workbench that runs a test suite specified in the
028: * command line arguments.
029: */
030: public class UITestApplication implements IApplication, ITestHarness {
031:
032: private static final String DEFAULT_APP_3_0 = "org.eclipse.ui.ide.workbench"; //$NON-NLS-1$
033:
034: private TestableObject fTestableObject;
035: private IApplication fApplication;
036:
037: /*
038: * (non-Javadoc)
039: * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
040: */
041: public Object start(IApplicationContext context) throws Exception {
042: String[] args = (String[]) context.getArguments().get(
043: IApplicationContext.APPLICATION_ARGS);
044: Object app = getApplication(args);
045:
046: Assert.assertNotNull(app);
047:
048: fTestableObject = PlatformUI.getTestableObject();
049: fTestableObject.setTestHarness(this );
050: if (app instanceof IApplication) {
051: fApplication = (IApplication) app;
052: return fApplication.start(context);
053: }
054: return ((IPlatformRunnable) app).run(args);
055: }
056:
057: /*
058: * (non-Javadoc)
059: * @see org.eclipse.equinox.app.IApplication#stop()
060: */
061: public void stop() {
062: if (fApplication != null)
063: fApplication.stop();
064: }
065:
066: /*
067: * return the application to run, or null if not even the default application
068: * is found.
069: */
070: private Object getApplication(String[] args) throws CoreException {
071: // Find the name of the application as specified by the PDE JUnit launcher.
072: // If no application is specified, the 3.0 default workbench application
073: // is returned.
074: IExtension extension = Platform.getExtensionRegistry()
075: .getExtension(Platform.PI_RUNTIME,
076: Platform.PT_APPLICATIONS,
077: getApplicationToRun(args));
078:
079: Assert.assertNotNull(extension);
080:
081: // If the extension does not have the correct grammar, return null.
082: // Otherwise, return the application object.
083: IConfigurationElement[] elements = extension
084: .getConfigurationElements();
085: if (elements.length > 0) {
086: IConfigurationElement[] runs = elements[0]
087: .getChildren("run"); //$NON-NLS-1$
088: if (runs.length > 0) {
089: Object runnable = runs[0]
090: .createExecutableExtension("class"); //$NON-NLS-1$
091: if (runnable instanceof IPlatformRunnable
092: || runnable instanceof IApplication)
093: return runnable;
094: }
095: }
096: return null;
097: }
098:
099: /*
100: * The -testApplication argument specifies the application to be run.
101: * If the PDE JUnit launcher did not set this argument, then return
102: * the name of the default application.
103: * In 3.0, the default is the "org.eclipse.ui.ide.worbench" application.
104: *
105: */
106: private String getApplicationToRun(String[] args) {
107: IProduct product = Platform.getProduct();
108: if (product != null)
109: return product.getApplication();
110: for (int i = 0; i < args.length; i++) {
111: if (args[i].equals("-testApplication") && i < args.length - 1) //$NON-NLS-1$
112: return args[i + 1];
113: }
114: return DEFAULT_APP_3_0;
115: }
116:
117: /*
118: * (non-Javadoc)
119: * @see org.eclipse.ui.testing.ITestHarness#runTests()
120: */
121: public void runTests() {
122: fTestableObject.testingStarting();
123: fTestableObject.runTest(new Runnable() {
124: public void run() {
125: RemotePluginTestRunner.main(Platform
126: .getCommandLineArgs());
127: }
128: });
129: fTestableObject.testingFinished();
130: }
131:
132: }
|