Add ServiceUsage utility and OSGiServiceInjector test-rule#726
Add ServiceUsage utility and OSGiServiceInjector test-rule#726HannesWell wants to merge 1 commit intoeclipse-m2e:masterfrom
Conversation
| if(context == null) { | ||
| throw new IllegalStateException("Test bundle <" + bundle.getSymbolicName() + "> not started"); | ||
| } | ||
| // TODO: BundleContextOSGi ungets services immediately! |
There was a problem hiding this comment.
This is something that should be addressed at E4 and not at m2e
|
If you want to only access a service once eclipse has "ServiceCaller" class already, but one should be aware that getting/ungetting a service might create noise, so I think this should really only be used in very rare cases. |
Thank you for the suggestion. I plan to create a corresponding PR to add something like the suggested Junit-Rule to inject OSGi-services for tests to org.eclipse.test and will then abandon this PR. |
OSGi Test already has ways to fetch services by annotations: I really like to see this getting used but currently it is a bit horrible to use inside PDE see: and not very intuitive to include it in Tycho see: so probably it would be much more better to improve the support here at PDE/Tycho than adding more "helpers" ... |
This PR is is intended to incubate/try out two utilities to simplify the correct usage of OSGi services:
ungetafter it has been used. It is currently located inM2EUtilsand can be obtained via theuseService(Class)respectivelyuseService(Class, Function).The implementation currently based on the E4's
ContextInjectionFactorybut it likely has to be implemented 'manually' by using a ServiceTracker per field that is closed after the evaluation because theEclipseContextOSGiungets the service-reference immediately. Using custom annotations like@InjectServicewould also make it more clear what is happening and would allow parameters if necessary (to specify the element type of a collection?).The same functionality could be implemented for JUnit-5 using a corresponding
Extension.I already applied them to a few locations within the m2e-core code-base and in tesla/m2e-core-tests#155 but there are more possible.
I think similar functionality could also be useful for many Eclipse-Platform projects because Services are there often not used correctly.
Finally it could maybe even be considered for inclusion into OSGi itself respectively osgi-test (although
useService()requires Java-9+).@laeubi what do you think?