KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > UITestApplication


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.test;
12
13 import java.io.IOException JavaDoc;
14
15 import junit.framework.Assert;
16
17 import org.eclipse.core.runtime.IPlatformRunnable;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtension;
21 import org.eclipse.core.runtime.Platform;
22
23 import org.eclipse.ui.IWindowListener;
24 import org.eclipse.ui.IWorkbench;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.testing.ITestHarness;
28 import org.eclipse.ui.testing.TestableObject;
29
30 /**
31  * A Workbench that runs a test suite specified in the
32  * command line arguments.
33  */

34 public class UITestApplication implements IPlatformRunnable, ITestHarness {
35
36     private static final String JavaDoc DEFAULT_APP_3_0 = "org.eclipse.ui.ide.workbench"; //$NON-NLS-1$
37
private static final String JavaDoc DEFAULT_APP_PRE_3_0 = "org.eclipse.ui.workbench"; //$NON-NLS-1$
38

39     private boolean fInDeprecatedMode = false;
40     private TestableObject fTestableObject;
41     private int fTestRunnerResult = -1;
42     
43     
44     /* (non-Javadoc)
45      * @see org.eclipse.core.boot.IPlatformRunnable
46      */

47     public Object JavaDoc run(final Object JavaDoc args) throws Exception JavaDoc {
48         // Get the application to test
49
IPlatformRunnable application = getApplication((String JavaDoc[])args);
50         
51         Assert.assertNotNull(application);
52         
53         Object JavaDoc result;
54         if (fInDeprecatedMode) {
55             result = runDeprecatedApplication(application, args);
56         }
57         else {
58             result = runApplication(application, args);
59         }
60         if (!IPlatformRunnable.EXIT_OK.equals(result)) {
61             System.err.println("UITestRunner: Unexpected result from running application " + application + ": " + result);
62         }
63         return new Integer JavaDoc(fTestRunnerResult);
64     }
65     
66     
67     /*
68      * return the application to run, or null if not even the default application
69      * is found.
70      */

71     private IPlatformRunnable getApplication(String JavaDoc[] args) throws CoreException {
72         // Assume we are in 3.0 mode.
73
// Find the name of the application as specified by the PDE JUnit launcher.
74
// If no application is specified, the 3.0 default workbench application
75
// is returned.
76
IExtension extension =
77         Platform.getExtensionRegistry().getExtension(
78                 Platform.PI_RUNTIME,
79                 Platform.PT_APPLICATIONS,
80                 getApplicationToRun(args));
81         
82         // If no 3.0 extension can be found, search the registry
83
// for the pre-3.0 default workbench application, i.e. org.eclipse ui.workbench
84
// Set the deprecated flag to true
85
if (extension == null) {
86             extension = Platform.getExtensionRegistry().getExtension(
87                     Platform.PI_RUNTIME,
88                     Platform.PT_APPLICATIONS,
89                     DEFAULT_APP_PRE_3_0);
90             fInDeprecatedMode = true;
91         }
92         
93         Assert.assertNotNull(extension);
94         
95         // If the extension does not have the correct grammar, return null.
96
// Otherwise, return the application object.
97
IConfigurationElement[] elements = extension.getConfigurationElements();
98         if (elements.length > 0) {
99             IConfigurationElement[] runs = elements[0].getChildren("run"); //$NON-NLS-1$
100
if (runs.length > 0) {
101                 Object JavaDoc runnable = runs[0].createExecutableExtension("class"); //$NON-NLS-1$
102
if (runnable instanceof IPlatformRunnable)
103                     return (IPlatformRunnable) runnable;
104             }
105         }
106         return null;
107     }
108     
109     /**
110      * The -testApplication argument specifies the application to be run.
111      * If the PDE JUnit launcher did not set this argument, then return
112      * the name of the default application.
113      * In 3.0, the default is the "org.eclipse.ui.ide.worbench" application.
114      *
115      */

116     private String JavaDoc getApplicationToRun(String JavaDoc[] args) {
117         for (int i = 0; i < args.length; i++) {
118             if (args[i].equals("-testApplication") && i < args.length -1) //$NON-NLS-1$
119
return args[i+1];
120         }
121         return DEFAULT_APP_3_0;
122     }
123     
124     /**
125      * In 3.0 mode
126      *
127      */

128     private Object JavaDoc runApplication(IPlatformRunnable application, Object JavaDoc args) throws Exception JavaDoc {
129         fTestableObject = PlatformUI.getTestableObject();
130         fTestableObject.setTestHarness(this);
131         return application.run(args);
132         
133     }
134     
135     /*
136      * If we are in pre-3.0 mode, then the application to run is
137      * "org.eclipse.ui.workbench" Therefore, we safely cast the runnable object
138      * to IWorkbenchWindow. We add a listener to it, so that we know when the
139      * window opens so that we can start running the tests. When the tests are
140      * done, we explicitly call close() on the workbench.
141      */

142     private Object JavaDoc runDeprecatedApplication(
143         IPlatformRunnable object,
144         final Object JavaDoc args)
145         throws Exception JavaDoc {
146
147         Assert.assertTrue(object instanceof IWorkbench);
148
149         final IWorkbench workbench = (IWorkbench) object;
150         // the 'started' flag is used so that we only run tests when the window
151
// is opened
152
// for the first time only.
153
final boolean[] started = { false };
154         workbench.addWindowListener(new IWindowListener() {
155             public void windowOpened(IWorkbenchWindow w) {
156                 if (started[0])
157                     return;
158                 w.getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
159                     public void run() {
160                         started[0] = true;
161                         try {
162                             fTestRunnerResult = EclipseTestRunner.run((String JavaDoc[]) args);
163                         } catch (IOException JavaDoc e) {
164                             e.printStackTrace();
165                         }
166                         workbench.close();
167                     }
168                 });
169             }
170             public void windowActivated(IWorkbenchWindow window) {
171             }
172             public void windowDeactivated(IWorkbenchWindow window) {
173             }
174             public void windowClosed(IWorkbenchWindow window) {
175             }
176         });
177         return ((IPlatformRunnable) workbench).run(args);
178     }
179
180     /* (non-Javadoc)
181      * @see org.eclipse.ui.testing.ITestHarness#runTests()
182      */

183     public void runTests() {
184         fTestableObject.testingStarting();
185         fTestableObject.runTest(new Runnable JavaDoc() {
186             public void run() {
187                 try {
188                     fTestRunnerResult = EclipseTestRunner.run(Platform.getCommandLineArgs());
189                 } catch (IOException JavaDoc e) {
190                     e.printStackTrace();
191                 }
192             }
193         });
194         fTestableObject.testingFinished();
195     }
196     
197 }
198
199
Popular Tags