Change display name of unit tests in JUnit Eclipse plugin - eclipse

Is it possible for an Eclipse plugin to change the display name of unit tests run by the default Eclipse JUnit plugin?
Some background: I'm trying to create a plugin which will display unit test names (in the JUnit view in Eclipse) a bit different from the actual method name. If the actual test method name is "anEmptyCollectionHasSizeZero" I want it to be displayed in the Eclipse JUnit view as "An empty collection has size zero".
I've managed to display the test method names in the way described above as a separate view in a plugin, but would as I said like to get this feature integrated in the normal JUnit view. I tried to see if I could find a related extension point, but could not find anything. Is it possible to do this?
Also, is there a plugin which already does this, or similar?

I don't know the exact answer, but the best place to start is likely by looking at the code for org.eclipse.jdt.internal.junit.ui.TestRunnerViewPart (in the plug-in org.eclipse.jdt.junit). That ViewPart is the JUnit view that you see in Eclipse.
It looks like org.eclipse.jdt.internal.junit.model.TestCaseElement.getTestMethodName() might be the place where the name is obtained from. Maybe you can trace how/when those elements are created and inject some other name. It looks like the implementation class is internal though (org.eclipse.jdt.internal.junit.model.TestElement), so you might be out of luck.
I'd suggest asking on the JDT forums about the possibility of doing this.

There don't seem to be any plugable way to change the label or the colors used.
(I had hoped the label provider used for the TestViewer was based on a PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator() so the image, label and color could be decorated, but that does not seem to be the case...)

You may want to look at the jnario sources. They have implemented something like that, where the JUnit window displays something that is surely not a stack frame.

You may use the setName() method of the class junit.framework.TestCase (or the constructor with the name parameter).
public MyTestCase() {
super("This is my JUnit test");
By default a TestCase defines the methods to be executed by it's name, which we have just overwritten. To make it runnable we have to overwrite the run method like this.
#Override
protected void runTest() throws Throwable {
testMyTest();
}

Related

Intellij Main Class Always Changing

my question today is if there is a way for IntelliJ to build with the current class file I have Instead of always going into the configurations setting and changing the Main class from there.
Picture :
Ok I now understand want you want :)
I hope this image helps.
You can create a run configuration for each class with a main method. In this example we have three classes Main1, Main2 and Main3 which you can see on the left in the image. So I created three run configurations called Main1, Main2 and Main3 which have the corresponding classes set as Main class.
You can add multiple run configurations by clicking on the green plus.
In the toolbar you then can choose the run configuration which you want to run.
EDIT:
You can just make a right click on the main method and choose the option run. I guess that is what you want :)
You can probably go in and change the default to default to that class

How to access the view of another plugin in Eclipse?

I'm developing an Eclipse plugin / extension of another plugin. For this I need to access the information displayed in a view by the other plugin.
Is there a way to accomplish this? I've only found how to do this if the view I want to access is created by the plugin I am currently developing
(using workbench.getActiveWorkbenchWindow().getActivePage().findView(MyView.ID);)
but this doesn't seem to be suitable in my situation as I do not know the ID of the view and do not have the MyView object.
Edit: The view contains a table with Strings / ints which I need. I guess it's also important to note that I have parts the source code of the other plugin available, so the class of the view.
Edit2: Because I have the source code of the other plugin, I was able to solve this - see comments.

Using an EditorPart inside a Composite

I'm new to RCP and I'm trying to create a new View or Editor. At the current state I'm extending ViewPart.
The Layout I want to achieve is a SashForm where both sides have a CTabFolder. One of the sides of the SashForm is basically a MultiPageEditorPart. One of it's tabs should be an editor for a specific language with syntax highlighting and similar features. From what I understand I could extend AbstractTextEditor for that (and maybe use it as one tab of a MultiPageEditorPart).
If I try to achieve this layout with a ViewPart as top level container which contains a SashForm, I can't add an EditorPart to one of the sides of the SashForm. Of course I could implement this editor from scratch as Composite but I want to avoid that.
I am also willing to use an EditorPart or MultiPageEditorPart as top level container but then I'd have to find a way to get the SashForm layout working. The whole editor should be splited first and then each side should have tabs.
Does anyone have an idea how to solve this?
If anything is unclear please ask. I've got the feeling I've put this into words a little complicatedly.
I think you should just make a ViewPart that has a text editing component of some kind on the left, instead of trying to find a way to use an EditorPart. All that EditorPart is really buying you is dirty flag management and Save support; syntax highlight and so forth you can add to the text editing component yourself (I was surprised at how few drop-in text components I found while poking around the internet just now; I expected to find "a few" and instead I found "basically none").
You can see one way to do very rudimentary syntax highlighting with a StyledText component here: JavaSourceCodeViewer
To see a more robust implementation of things like syntax highlight and autocomplete, take a look at the class that Eclipse uses for the editing of Java source code: CompilationUnitEditor
I think what you are trying to achieve is quite complicated and might require a lot of extra work. See an editor is not just different controls laid out in some order, but it has a lot of additional features that any editor class expects to work. These include things like selection service and action bars etc, that you will need to hook in to ensure smooth running.
That said, it should be possible to achieve what you are hoping for. You can have a look at the source code of the MultiPageEditorPart itself to see how it converts a single editor into a multi page editor, which is capable of hosting a completely independent editor on each of its pages. You would need to achieve something similar if you want your editor to host two MultiPageEditorParts separated by a sash. If you want to carry on, you should start implementing some stuff and if you run into any problems, post them here. You would be able to get a lot better help then.
You need something like MultiPageEditorSite. Use it for inspiration when implementing an EditorSiteDelegate. MultiPageEditorSite has support for separate keybindings between the pages, for example.
class ChildEditorSite implements IEditorSite {
IEditorSite parent;
public Object method() {
return parent.method();
}
}
Using this class, you can easily do the following in your main EditorPart:
class MyCoolPart extends EditorPart {
public void createControl(Composite parent) {
EditorPart child1 = new MyChild();
child1.init(new ChildEditorSite(getEditorSite()), myInput);
EditorPart child2 = new MyChild();
child2.init(new ChildEditorSite(getEditorSite()), myInput);
child1.createPartControl(parent);
child2.createPartControl(parent);
}
}
Be sure to also dispose of your children correctly when you dispose of your MyCoolPart. Please note that this only works in the most basic of cases. An EditorPart that is a DocumentEditor or that relies on IPersistablePart or implements listeners/adapters for confirming a save is probably going to require a lot more Lifecycle management...

Toolbar items dynamically

I need to create dynamically buttons in main toolbar. I found a solution, but I can create just one button (dynamic contribution item - class extending ContributionItem). But I need to create more than one button, but I cannot find the solution.
I'm fighting with task to create plugin, which parses a XML file containing structure of menu and toolbars. We've already done this plugin for Visual Studio. Its quite easy in principle, but I found swiftly, that not for Eclipse. There is one small but critical otherness. Plugins are implemented declaratively in Eclipse. The file plugin.xml is the gist of plugin's infrastructure, Java code is just ancillary.
The customer wants to refresh the menu and toolbar whenever the selected project is changed. Eclipse lacks several features needed to get the task done. Main menu and main toolbar are cteated at Eclipse's start-up and then they can be hardly rebuilt.
In the most cases the conditions defined at enabledWhen/visibleWhen elements are sufficient to filter contributions according to the context (active part, selected object, whatever else).
If you need to have more freedom, please try E4 ToolControl that allows you to implement your own UI elements:
#PostConstruct
public void createControls(Composite parent) {
//your custom code here
}
More details here https://www.vogella.com/tutorials/EclipseRCP/article.html#toolcontrols
From my understanding you want to have different buttons on the main toolbar depending on the selection of the project explorer (eg. 1 project is java project, the other is javascript etc.). First you will have to contribute to the main toolbar. I think there are some tutorial available so google will help.
The main steps are:
1. create a command (org.eclipse.ui.commmands)
2. create a handler (org.eclipse.ui.handlers) with the previously declared command id
3. contribute to the main toolbar (org.eclipse.ui.menus) with menucontribution and commandId with the following locationURI: toolbar:org.eclipse.ui.main.toolbar?after=misc
showing/hiding, enabling/disabling a menu item/button also can be done declaratively or "mixed". Declaratively means eg. using enabledWhen/visibleWhen...
Mixed means using property tester (org.eclipse.core.expressions.propertyTester). With this you can define your "enablement logic" in Java code.
In Eclipse e4 the UI is generated from a, EMF based, model. The Application.e4xmi serves as a base for that model. Contributions to the model can be done via fragments, which are again XML, or via processors. Processors are written in Java and use e4 services, like the part service, to modify the model at runtime.
I think you want to write a processor that parses your custom XML and modifies the eclipse e4 model accordingly.

How to implement Quick Fix / Quick Assist for custom eclipse editor?

I have extended org.eclipse.ui.editors.text.TextEditor to implement a custom editor.
For this editor, I have defined a marker type (org.eclipse.core.resources.markers extension point) and an annotation type (org.eclipse.ui.editors.annotationTypes extension point) to mark specific parts of code in my editor. I use a reconciler to update my annotation model.
Now I want to add a quick fix / quick assist feature. I simply want eclipse, to show a box with proposals, when I hover over an annotated part of the code and replace that part with a given string, when I click on a proposal. Just like the quick fix feature for the java editor.
So, what is the best way to implement this behavior?
I read about marker resolution generators and quick assist processors, but I'm still confused how it all works together...
I would be glad, if someone could point me to the right direction.
EDIT: From what I've understood so far, a MarkerResolutionGenerator is responsible for showing quick fixes in the problems view. To get quick fixes in the source viewer, I would have to set a QuickAssistAssistant for my SourceViewer and implement a QuickAssistProcessor which returns CompletionProposals.
Is this the right way to do it?
EDIT2: I'm wondering if I need Markers at all, or only Annotations, I'm confused...
I finally found out how to get Quick Fix to work for my editor.
I use the annotationTypes extension point to register my own annotation type and the markerAnnotationSpecification extension point to specify the look and feel. In my custom SourceViewerConfiguration class I override getAnnotationHover(...) to return a DefaultAnnotationHover object and getTextHover(...) to return a DefaultTextHover object, so the annotations are shown in my source viewer.
Then I override getReconciler(...) to return a MonoReconciler with my own implementation of IReconcilingStrategy to create the annotations in its reconcile(...) method. And finally I override getQuickAssistAssistant(...) to return a QuickAssistAssistant with my own implementation of IQuickAssistProcessor. The computeQuickAssistProposals(...) method in the processor class computes the quick fix proposals which show up, when I press CTRL+1.
I don't create any Marker objects and don't use a MarkerResolutionGenerator, since the marker concept is much more heavyweight than using only annotations and the functionality which annotations provide fits my needs.
You have to register an extension to the extension point org.eclipse.ui.ide.markerResolution. This extension refers to a markerType (using the markerId), and also a resolution generator.
The latter component is responsible for calculating the possible fixes: it reads the marker, it can check the related files, etc., and creates marker resolution instances. These resolution instances basically process the erroneous files, and hopefully fix the original problem.
During marker resolution, you should not worry about removing the markers, as after the fix is executed, sometimes the validation would run again (e.g. during the build, or if no automatic validation is available, then manually - but it is not the task of the marker resolution to update the list of markers).