eclipse / xtext

Eclipse Xtext™ is a language development framework
http://www.eclipse.org/Xtext
Eclipse Public License 2.0
769 stars 321 forks source link

Exception when using a customized UIInjectorProvider for UI tests. #2475

Open miklossy opened 4 years ago

miklossy commented 4 years ago

I would like to use a customized UIInjectorProvider while executing the ContentAssist UI tests. However, while executing, I get the following exception:

1    [main] ERROR org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory  - java.lang.RuntimeException: java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.NullPointerException
    at java.util.concurrent.FutureTask.report(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory.doCreateContexts(ContentAssistContextFactory.java:181)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory.create(ContentAssistContextFactory.java:131)
    at org.eclipse.xtext.ui.editor.contentassist.antlr.DelegatingContentAssistContextFactory$StatefulFactory.doCreateContexts(DelegatingContentAssistContextFactory.java:93)
    at org.eclipse.xtext.ui.editor.contentassist.antlr.DelegatingContentAssistContextFactory$StatefulFactory.create(DelegatingContentAssistContextFactory.java:79)
    at org.eclipse.xtext.ui.editor.contentassist.antlr.DelegatingContentAssistContextFactory.create(DelegatingContentAssistContextFactory.java:56)
    at org.eclipse.xtext.ui.editor.contentassist.CompletionProposalComputer.createContentAssistContexts(CompletionProposalComputer.java:68)
    at org.eclipse.xtext.ui.editor.contentassist.CompletionProposalComputer.exec(CompletionProposalComputer.java:50)
    at org.eclipse.xtext.ui.editor.contentassist.CompletionProposalComputer.exec(CompletionProposalComputer.java:1)
    at org.eclipse.xtext.resource.OutdatedStateManager.exec(OutdatedStateManager.java:70)
    at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.internalReadOnly(XtextDocument.java:525)
    at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.priorityReadOnly(XtextDocument.java:489)
    at org.eclipse.xtext.ui.editor.model.XtextDocument.priorityReadOnly(XtextDocument.java:145)
    at org.eclipse.xtext.ui.editor.contentassist.XtextContentAssistProcessor.computeCompletionProposals(XtextContentAssistProcessor.java:77)
    at org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.computeCompletionProposals(ContentAssistProcessorTestBuilder.java:520)
    at org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.computeCompletionProposals(ContentAssistProcessorTestBuilder.java:511)
    at org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.computeCompletionProposals(ContentAssistProcessorTestBuilder.java:501)
    at org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.computeCompletionProposals(ContentAssistProcessorTestBuilder.java:489)
    at org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.assertTextAtCursorPosition(ContentAssistProcessorTestBuilder.java:343)
    at org.xtext.example.mydsl.ui.tests.ContentAssistTest.testContentAssistant(ContentAssistTest.java:47)
    at org.xtext.example.mydsl.ui.tests.ContentAssistTest.test(ContentAssistTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.main(RemotePluginTestRunner.java:171)
    at org.eclipse.pde.internal.junit.runtime.PlatformUITestHarness.lambda$0(PlatformUITestHarness.java:45)
    at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:40)
    at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:185)
    at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3897)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3527)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1160)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1049)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
    at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:658)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:557)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:154)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:150)
    at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.runApp(NonUIThreadTestApplication.java:55)
    at org.eclipse.pde.internal.junit.runtime.UITestApplication.runApp(UITestApplication.java:46)
    at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.start(NonUIThreadTestApplication.java:49)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:137)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:107)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:657)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:594)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1447)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1420)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:246)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:237)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:232)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.AbstractContentAssistParser.getFollowElements(AbstractContentAssistParser.java:49)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.AbstractContentAssistParser.getFollowElements(AbstractContentAssistParser.java:27)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:264)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory.handleLastCompleteNodeIsAtEndOfDatatypeNode(ContentAssistContextFactory.java:230)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory$2.call(ContentAssistContextFactory.java:161)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory$2.call(ContentAssistContextFactory.java:158)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at java.lang.Class.searchMethods(Unknown Source)
    at java.lang.Class.privateGetMethodRecursive(Unknown Source)
    at java.lang.Class.getMethod0(Unknown Source)
    at java.lang.Class.getMethod(Unknown Source)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:169)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:242)
    ... 12 more

!ENTRY org.apache.log4j 4 0 2020-04-30 15:16:01.420
!MESSAGE org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory  - java.lang.RuntimeException: java.lang.NullPointerException

!STACK 0
java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.NullPointerException
    at java.util.concurrent.FutureTask.report(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory.doCreateContexts(ContentAssistContextFactory.java:181)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory.create(ContentAssistContextFactory.java:131)
    at org.eclipse.xtext.ui.editor.contentassist.antlr.DelegatingContentAssistContextFactory$StatefulFactory.doCreateContexts(DelegatingContentAssistContextFactory.java:93)
    at org.eclipse.xtext.ui.editor.contentassist.antlr.DelegatingContentAssistContextFactory$StatefulFactory.create(DelegatingContentAssistContextFactory.java:79)
    at org.eclipse.xtext.ui.editor.contentassist.antlr.DelegatingContentAssistContextFactory.create(DelegatingContentAssistContextFactory.java:56)
    at org.eclipse.xtext.ui.editor.contentassist.CompletionProposalComputer.createContentAssistContexts(CompletionProposalComputer.java:68)
    at org.eclipse.xtext.ui.editor.contentassist.CompletionProposalComputer.exec(CompletionProposalComputer.java:50)
    at org.eclipse.xtext.ui.editor.contentassist.CompletionProposalComputer.exec(CompletionProposalComputer.java:1)
    at org.eclipse.xtext.resource.OutdatedStateManager.exec(OutdatedStateManager.java:70)
    at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.internalReadOnly(XtextDocument.java:525)
    at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.priorityReadOnly(XtextDocument.java:489)
    at org.eclipse.xtext.ui.editor.model.XtextDocument.priorityReadOnly(XtextDocument.java:145)
    at org.eclipse.xtext.ui.editor.contentassist.XtextContentAssistProcessor.computeCompletionProposals(XtextContentAssistProcessor.java:77)
    at org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.computeCompletionProposals(ContentAssistProcessorTestBuilder.java:520)
    at org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.computeCompletionProposals(ContentAssistProcessorTestBuilder.java:511)
    at org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.computeCompletionProposals(ContentAssistProcessorTestBuilder.java:501)
    at org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.computeCompletionProposals(ContentAssistProcessorTestBuilder.java:489)
    at org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.assertTextAtCursorPosition(ContentAssistProcessorTestBuilder.java:343)
    at org.xtext.example.mydsl.ui.tests.ContentAssistTest.testContentAssistant(ContentAssistTest.java:47)
    at org.xtext.example.mydsl.ui.tests.ContentAssistTest.test(ContentAssistTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.main(RemotePluginTestRunner.java:171)
    at org.eclipse.pde.internal.junit.runtime.PlatformUITestHarness.lambda$0(PlatformUITestHarness.java:45)
    at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:40)
    at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:185)
    at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3897)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3527)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1160)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1049)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
    at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:658)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:557)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:154)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:150)
    at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.runApp(NonUIThreadTestApplication.java:55)
    at org.eclipse.pde.internal.junit.runtime.UITestApplication.runApp(UITestApplication.java:46)
    at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.start(NonUIThreadTestApplication.java:49)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:137)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:107)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:657)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:594)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1447)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1420)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:246)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:237)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:232)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.AbstractContentAssistParser.getFollowElements(AbstractContentAssistParser.java:49)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.AbstractContentAssistParser.getFollowElements(AbstractContentAssistParser.java:27)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:264)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory.handleLastCompleteNodeIsAtEndOfDatatypeNode(ContentAssistContextFactory.java:230)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory$2.call(ContentAssistContextFactory.java:161)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory$2.call(ContentAssistContextFactory.java:158)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at java.lang.Class.searchMethods(Unknown Source)
    at java.lang.Class.privateGetMethodRecursive(Unknown Source)
    at java.lang.Class.getMethod0(Unknown Source)
    at java.lang.Class.getMethod(Unknown Source)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:169)
    at org.eclipse.xtext.ide.editor.contentassist.antlr.BaseContentAssistParser.getFollowElements(BaseContentAssistParser.java:242)
    ... 12 more

!ENTRY org.eclipse.core.resources 2 10035 2020-04-30 15:16:03.001
!MESSAGE The workspace will exit with unsaved changes in this session.

I use the following code snippet to create the customized UI Injector provider:

@RunWith(XtextRunner)
@InjectWith(CustomizedMyDslUiInjectorProvider)
class ContentAssistTest extends AbstractContentAssistTest {
   ...
}
public class CustomizedMyDslUiInjectorProvider implements IInjectorProvider {

    @Override
    public Injector getInjector() {
        return Guice.createInjector(Modules2.mixin(
            new TestMyDslRuntimeModule(),
            new SharedStateModule(),
            new MyDslUiModule(MydslActivator.getInstance())));
    }

}
public class TestMyDslRuntimeModule extends MyDslRuntimeModule {
    // override some bindings that should differ in the test environment
}
szarnekow commented 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.

miklossy commented 4 years ago

I uploaded the project based on the mydsl example into the Xtext issues GitHub repository

szarnekow commented 4 years ago

A little bit of an explanation:

miklossy commented 4 years ago

Thanks for the quick reply! I will give it a try using a child injector of the "real" UI injector.

miklossy commented 4 years ago

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? screenshot

szarnekow commented 4 years ago

Yes, that is not supported. Maybe a step backwards: what is the concrete problem you are working on?

miklossy commented 4 years ago

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.