Call JUnit from other Eclipse Plugin - eclipse

I am developing for Eclipse, and one feature is run JUnit tests. Now my plugin detect JUnit tests on a project in workspace and after this I want to call JUnit to run this tests.
I heard about ILaunch, ILaunchConfigurationDelegate, JUnitLaunchConfigurationDelegate but I cannot found an example of this and also I'm not sure if I have to use this!
-- Thanks in advance

Please see my answer to How does Eclipse actually run Junit tests?. You will need to create a Run Configuration and then call JUnitLaunchConfigurationDelegate#launch() with the configuration.
The easiest way to do this is to add shortcuts to the extension point org.eclipse.debug.ui.launchShortcuts. With the correct configurationType, you can create the correct type and normally, Eclipse will do the rest. In fact this is exactly what we've done in the Scala IDE.
Here is the relevant XML from scala-ide:
<extension point="org.eclipse.debug.ui.launchShortcuts">
<shortcut
label="%JUnitShortcut.label"
icon="$nl$/icons/full/obj16/julaunch.gif"
helpContextId="org.eclipse.jdt.junit.launch_shortcut"
class="org.eclipse.jdt.junit.launcher.JUnitLaunchShortcut"
modes="run, debug"
id="scala.tools.eclipse.scalatest.junitShortcut">
<contextualLaunch>
<enablement>
<with variable="selection">
<count value="1"/>
<iterate>
<adapt type="org.eclipse.jdt.core.IJavaElement">
<test property="org.eclipse.debug.ui.matchesPattern" value="*.scala"/>
<test property="org.eclipse.jdt.core.isInJavaProject"/>
<test property="org.eclipse.jdt.core.hasTypeOnClasspath" value="junit.framework.Test"/>
<or>
<test property="scala.tools.eclipse.launching.canLaunchAsJUnit" forcePluginActivation="true"/>
<test property="scala.tools.eclipse.launching.junit.canLaunchAsJUnit" forcePluginActivation="true"/>
</or>
</adapt>
</iterate>
</with>
</enablement>
</contextualLaunch>
<configurationType
id="org.eclipse.jdt.junit.launchconfig">
</configurationType>
<description
description="%DebugJUnitLaunchShortcut.description"
mode="debug">
</description>
<description
description="%RunJUnitLaunchShortcut.description"
mode="run">
</description>
</shortcut>
</extension>
The important element is the <contextualLaunch>, which defines a set of tests which need to be true in order that the option to launch as JUnit be presented to the user. Most of these are self explanatory, but we've also scala.tools.eclipse.launching.canLaunchAsJUnit, which references an extension point org.eclipse.core.expressions.propertyTesters. These property testers test whether the code can be launched as JUnit or not (for instance, the class under test extends TestCase or whatever).
If you need more details, I recommend downloading Scala IDE, and looking at the code, but it is written in Scala.

I rewrote part of launche junit services, then inform the project it calls the debug and run.
See my implementation to call the JUnitLaunch

Related

Eclipse Plugin: what property attribut for testing name of subversion resources

I'm writing a plugin. To start it, the menu entry is displayed when doing a rightclick on a project in SVN-Plugin-View. By using "org.tigris.subversion.subclipse.ui.repository.RepositoriesView" as locationURI inside the plugin.xml my menu entry is visible, as I wanted.
But now I want to make it visible under special conditions. I don't find out, what property I can use to test the name of the Project.
I already did something similar inside Eclipse with
<command...
<visible when>
<with variable="activeMenuSelection">
<iterate ifEmpty="false">
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.name" value="Test_*"/>
</adapt>
</iterate>
</with>
</visibleWhen>
</command>
Now I use
but I don't find out what to use as test property
Like in my working (without SVN) example I tried
<test property="org.tigris.subversion.subclipse.core.resources.name" value="Test_*/>"
But it did not work. The menu entry was not visible for SVN-Projects with names starting with "Test_"

Submenu on method level for custom launcher in RCP client

I have written a custom launcher in Eclipse which I can access via the "Run As" and "Debug As" on class level. I have a similar configuration as described in How do I use "org.eclipse.debug.ui.launchShortcuts"?. Unfortunately I can't see the submenu in the package explorer at the method level (for example submenu to launch the customer launcher in order to execute a single JUnit test). How can I do this?
JUnit uses a contextualLaunch based on the selected element being a org.eclipse.jdt.core.IJavaElement rather than just a resource. You will need to do the same.
This is what JUnit uses:
<contextualLaunch>
<enablement>
<with variable="selection">
<count value="1"/>
<iterate>
<adapt type="org.eclipse.jdt.core.IJavaElement">
<and>
<test property="org.eclipse.jdt.core.isInJavaProject"/>
<or>
<test property="org.eclipse.jdt.core.hasTypeOnClasspath" value="junit.framework.Test"/>
<test property="org.eclipse.jdt.core.hasTypeOnClasspath" value="org.junit.platform.commons.annotation.Testable"/>
</or>
<test property="org.eclipse.jdt.junit.canLaunchAsJUnit" forcePluginActivation="true"/>
</and>
</adapt>
</iterate>
</with>
</enablement>
</contextualLaunch>
Some of the <test elements are specific to JUnit so you will need to use something different.

How to make a custom Eclipse run launcher that launches a particular Class?

I am developing an eclipse plugin. I want to build a custom run launcher that would be executed when i use it against a specific class. So the scenario is, when i run the plugin via eclipse run time environment i want to run a particular class which is already written in this particular eclipse run time. So i will execute this class with my custom run launcher. For now, i do not need any tab or custom UI. I just need to show the output of that particular class in default java console where normally outputs are shown in eclipse. I did not find great stuff in this regard. As i am new so i am rather getting confused more. Please have a look on what i have tried so far. I am not using org.eclipse.debug.core.launchConfigurationTypes extension point. Rather i am using ILaunchShortcut in this case. So i tried to call that particular class and execute it in launch method using the below code.
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
ILaunchConfigurationWorkingCopy wc = type.newInstance(null, "SampleConfig");
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "Test");
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "Test1");
ILaunchConfiguration config = wc.doSave();
config.launch(ILaunchManager.RUN_MODE, null);
But here the problem is i am not able to resolve IJavaLaunchConfigurationConstants in my code anyway. So i am fully stuck here. See my plugin.xml file also for your convenience.
<plugin>
<extension
point="org.eclipse.debug.ui.launchShortcuts">
<shortcut
class="launcher.LaunchShortcut"
id="launcher.shortcut2"
label="Launcher Test"
modes="run">
<contextualLaunch>
<contextLabel mode="run" label="Run Launcher" />
<enablement>
<with
variable="selection">
<count
value="1">
</count>
<iterate>
<adapt type="org.eclipse.core.resources.IResource">
<and>
<test property="org.eclipse.core.resources.name" value="Test1.java"/>
</and>
</adapt>
</iterate>
</with>
</enablement>
</contextualLaunch>
</shortcut>
</extension>
</plugin>
What should i do now to make this code successfully running? I need your suggestions and references. Thanks.
As per comment of greg-449 i added following the extension org.eclipse.jdt.launching.classpathProviders in the plugin.xml and the code worked perfectly.

Command Expression for active debug session?

I added the extension point org.eclipse.ui.menus to create a new button in the debug view.
That works perfectly, but this particular button is now always visible. How can I add a visibleWhen condition that checks if a debug session is currently running?
I checked the Command Core Expressions, but did not find any expressions that may work in that case.
The menus in the org.eclipse.jdt.debug.ui plugin use expressions like this:
<visibleWhen
checkEnabled="false">
<and>
<systemTest
property="org.eclipse.jdt.debug.ui.debuggerActive"
value="true">
</systemTest>
... other tests ...
</and>
</visibleWhen>
There is a good example project in CDT on how to get started customizing DSF. Please have a look at it's plugin.xml and follow the uses of the command id defined there: org.eclipse.cdt.examples.dsf.gdb.command.showVersion
Here is one of the visibleWhen's from that code.
<visibleWhen
checkEnabled="false">
<and>
<reference
definitionId="org.eclipse.cdt.debug.ui.testIsDebugActionSetActive">
</reference>
<with variable="org.eclipse.core.runtime.Platform">
<test property="org.eclipse.core.runtime.bundleState"
args="org.eclipse.cdt.examples.dsf.gdb"
value="ACTIVE">
</test>
</with>
</and>
</visibleWhen>

Use of ILaunchConfigurationMigrationDelegate

I do not understand how migration of Eclipse Launch Configrations (ILaunchConfiguration) works. In my dreams, I'd love to have the following:
My plugin, version 1.0, is used to create a Launch Configuration referencing file something_1.0/foo
The plugin is upgraded to version 2.0, which also deletes the file still referenced in all old launch configurations of the plugin
The old launch configurations are automatically upgraded so that something_2.0/foo is referenced instead of the non-existing something_1.0/foo
Step 3 sadly is the one that does not work, although I wrote and connected a corresponding implementation of ILaunchConfigurationMigrationDelegate. It seems the code is never executed.
When exactly are configurations migrated? According to the code, there is a Migrate button, which I cannot find. According to the documentation configurations might be migrated automatically. How is this triggered?
Thanks,
Carsten
This implementation of this interface should be declared in extension point.
You can refer to JDT itself implementation org.eclipse.jdt.internal.launching.JavaMigrationDelegate.
Please note the launch shortcut specify the configuration type as the one defined via extension.
<extension
point="org.eclipse.debug.ui.launchShortcuts">
<shortcut
class="org.eclipse.jdt.debug.ui.launchConfigurations.JavaApplicationLaunchShortcut"
description="%JavaLaunchShortcut.description"
helpContextId="org.eclipse.jdt.debug.ui.shortcut_local_java_application"
icon="$nl$/icons/full/etool16/java_app.gif"
id="org.eclipse.jdt.debug.ui.localJavaShortcut"
label="%JavaApplicationShortcut.label"
modes="run, debug">
<contextualLaunch>
<enablement>
<with variable="selection">
<count value="1"/>
<iterate>
<and>
<adapt type="org.eclipse.jdt.core.IJavaElement">
<test property="org.eclipse.jdt.core.isInJavaProject"/>
</adapt>
<or>
<test property="org.eclipse.jdt.launching.hasMain"/>
<test property="org.eclipse.jdt.launching.isContainer"/>
<test property="org.eclipse.jdt.launching.isPackageFragment"/>
<test property="org.eclipse.jdt.launching.isPackageFragmentRoot"/>
</or>
</and>
</iterate>
</with>
</enablement>
</contextualLaunch>
<configurationType
id="org.eclipse.jdt.launching.localJavaApplication">
</configurationType>
<description
description="%RunJavaLaunchShortcut.description"
mode="run">
</description>
<description
description="%DebugJavaLaunchShortcut.description"
mode="debug">
</description>
</shortcut>