KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > junit > runtime > UITestApplication


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.pde.internal.junit.runtime;
12
13 import junit.framework.Assert;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExtension;
18 import org.eclipse.core.runtime.IPlatformRunnable;
19 import org.eclipse.core.runtime.IProduct;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.equinox.app.IApplication;
22 import org.eclipse.equinox.app.IApplicationContext;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.testing.ITestHarness;
25 import org.eclipse.ui.testing.TestableObject;
26
27 /**
28  * A Workbench that runs a test suite specified in the
29  * command line arguments.
30  */

31 public class UITestApplication implements IApplication, ITestHarness {
32     
33     private static final String JavaDoc DEFAULT_APP_3_0 = "org.eclipse.ui.ide.workbench"; //$NON-NLS-1$
34

35     private TestableObject fTestableObject;
36     private IApplication fApplication;
37     
38     /*
39      * (non-Javadoc)
40      * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
41      */

42     public Object JavaDoc start(IApplicationContext context) throws Exception JavaDoc {
43         String JavaDoc[] args = (String JavaDoc[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
44         Object JavaDoc app = getApplication(args);
45
46         Assert.assertNotNull(app);
47
48         fTestableObject = PlatformUI.getTestableObject();
49         fTestableObject.setTestHarness(this);
50         if (app instanceof IApplication) {
51             fApplication = (IApplication) app;
52             return fApplication.start(context);
53         }
54         return ((IPlatformRunnable) app).run(args);
55     }
56
57     /*
58      * (non-Javadoc)
59      * @see org.eclipse.equinox.app.IApplication#stop()
60      */

61     public void stop() {
62         if (fApplication != null)
63             fApplication.stop();
64     }
65
66     /*
67      * return the application to run, or null if not even the default application
68      * is found.
69      */

70     private Object JavaDoc getApplication(String JavaDoc[] args) throws CoreException {
71         // Find the name of the application as specified by the PDE JUnit launcher.
72
// If no application is specified, the 3.0 default workbench application
73
// is returned.
74
IExtension extension =
75             Platform.getExtensionRegistry().getExtension(
76                 Platform.PI_RUNTIME,
77                 Platform.PT_APPLICATIONS,
78                 getApplicationToRun(args));
79             
80         Assert.assertNotNull(extension);
81         
82         // If the extension does not have the correct grammar, return null.
83
// Otherwise, return the application object.
84
IConfigurationElement[] elements = extension.getConfigurationElements();
85         if (elements.length > 0) {
86             IConfigurationElement[] runs = elements[0].getChildren("run"); //$NON-NLS-1$
87
if (runs.length > 0) {
88                 Object JavaDoc runnable = runs[0].createExecutableExtension("class"); //$NON-NLS-1$
89
if (runnable instanceof IPlatformRunnable || runnable instanceof IApplication)
90                     return runnable;
91             }
92         }
93         return null;
94     }
95     
96     /*
97      * The -testApplication argument specifies the application to be run.
98      * If the PDE JUnit launcher did not set this argument, then return
99      * the name of the default application.
100      * In 3.0, the default is the "org.eclipse.ui.ide.worbench" application.
101      *
102      */

103     private String JavaDoc getApplicationToRun(String JavaDoc[] args) {
104         IProduct product = Platform.getProduct();
105         if (product != null)
106             return product.getApplication();
107         for (int i = 0; i < args.length; i++) {
108             if (args[i].equals("-testApplication") && i < args.length -1) //$NON-NLS-1$
109
return args[i+1];
110         }
111         return DEFAULT_APP_3_0;
112     }
113     
114     /*
115      * (non-Javadoc)
116      * @see org.eclipse.ui.testing.ITestHarness#runTests()
117      */

118     public void runTests() {
119         fTestableObject.testingStarting();
120         fTestableObject.runTest(new Runnable JavaDoc() {
121             public void run() {
122                 RemotePluginTestRunner.main(Platform.getCommandLineArgs());
123             }
124         });
125         fTestableObject.testingFinished();
126     }
127         
128 }
129
Popular Tags