Open miklossy opened 4 years ago
A custom UI injector provider will usually lead to EMF singletons being corrupted.
The only viable option is to use a child injector of the "real" UI injector.
I uploaded the project based on the mydsl example into the Xtext issues GitHub repository
A little bit of an explanation:
Thanks for the quick reply! I will give it a try using a child injector of the "real" UI injector.
I have been experimenting with the child injector for a while and still no success. Could it be that the child injector is not designed to override bindings of the parent injector?
Yes, that is not supported. Maybe a step backwards: what is the concrete problem you are working on?
Generally speaking I am working on an Xtext project based on the MyDsl example where the valid greeting names are coming from an external webservice/database. The content assist should be adapted that the valid greeting names are offered to the user. To do that, I defined the IGreetingNamesProvider interface:
package org.xtext.example.mydsl;
import java.util.List;
public interface IGreetingNamesProvider {
List<String> getNames();
}
and defined two implementations of it, the ProductiveGreetingNamesProvider and the TestGreetingNamesProvider classes. Moreover, I bound the IGreetingNamesProvider
interface to the ProductiveGreetingNamesProvider
class in the MyDslRuntimeModule:
public class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
public Class<? extends IGreetingNamesProvider> bindIGreetingNamesProvider() {
return ProductiveGreetingNamesProvider.class;
}
}
Since the productive implementation of the IGreetingNamesProvider
could be time consuming and fragile (e.g fetching the data from an external database or webservice), I would like to use the TestGreetingNamesProvider
dummy implementation in the content assist test cases.
In particular I am about to update the content assist test cases of the TargetPlatformDSL project. Currently it is based on the org.eclipse.xtext.junit4.ui.AbstractContentAssistProcessorTest base class that is deprecated for a while. I try to make it work with the recommended org.eclipse.xtext.ui.testing.AbstractContentAssistTest class. I thought the update would be straight-forward.
Currently I came up with a working solution. I customized the createInjector method of the generated MydslActivator
class to create the injector using different modules:
public class MyDslActivatorEx extends MydslActivator {
private Module extraModule = null;
@Override
protected Injector createInjector(String language) {
com.google.inject.Module runtimeModule = getRuntimeModule(language);
com.google.inject.Module sharedStateModule = getSharedStateModule();
com.google.inject.Module uiModule = getUiModule(language);
com.google.inject.Module mergedModule = Modules2.mixin(runtimeModule, sharedStateModule, uiModule);
if (extraModule != null) {
mergedModule = Modules2.mixin(mergedModule, extraModule);
}
return Guice.createInjector(mergedModule);
}
public void setExtraModule(Module extraModule) {
this.extraModule = extraModule;
}
}
and use the CustomizedMyDslUiInjectorProvider
public class CustomizedMyDslUiInjectorProvider extends MyDslUiInjectorProvider {
@Override
public Injector getInjector() {
MyDslActivatorEx myDslActivatorEx = (MyDslActivatorEx) MyDslActivatorEx.getInstance();
myDslActivatorEx.setExtraModule(new TestMyDslExtraModule());
return super.getInjector();
}
}
public class TestMyDslExtraModule implements Module {
@Override
public void configure(Binder binder) {
binder.bind(IGreetingNamesProvider.class).to(TestGreetingNamesProvider.class);
}
}
in the ContentAssist test cases:
@RunWith(XtextRunner)
@InjectWith(CustomizedMyDslUiInjectorProvider)
class ContentAssistTest extends AbstractContentAssistTest {
...
}
I still keep thinking about if this solution could be further simplified.
I would like to use a customized UIInjectorProvider while executing the ContentAssist UI tests. However, while executing, I get the following exception:
I use the following code snippet to create the customized UI Injector provider: