Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » inject extension not injected in JUnit test (inject extension not injected in JUnit test)
inject extension not injected in JUnit test [message #964519] Tue, 30 October 2012 11:38 Go to previous message
Michael Veit is currently offline Michael Veit
Messages: 12
Registered: January 2012
Junior Member
Hello,

I want to run a model to model transformation with Xtend 2.3.1, without having a Xtext grammar.

My problem is:
When using the @Inject extension, it does not get injected.
When I run an JUnit test, the injected type is null and therefore creating a NullPointerException during runtime.

I started with a migration from Xtend 1, where the grammar was like:

extension my::package::classname_dt;

create targetmodel::Application mapApplication(Application app, targetmodel::Model model):
[...]

With Xtend2, it looks like:
class MyClass {
  @Inject extension ClassnameDt

 def targetmodel.Application create TargetmodelFactory::eINSTANCE.createApplication mapApplication(
    sourcemodel.Application app,
    targetmodel.Model model
  ) {
    [...]
  }
}

As I use a create method, I cannot inject the ClassnameDt as static extension, which would have solved my problem.
In the test class, I use
@RunWith(XtextRunner.class)
@InjectWith(InjectorProviderCustom.class)
public class TargetmodelFactoryServiceTest extends AbstractTargetmodelFactoryTest {

  @Test
  public void testAllParameterTypes() throws Exception {
    [...]
  }
}

which calls the transform() method of the TargetmodelFactory:
Injector injector = new mypackage.TargetmodelFactoryStandaloneSetup().createInjectorAndDoEMFRegistration();
//Injector injector = new Mwe2StandaloneSetup().createInjectorAndDoEMFRegistration();
//Injector injector = new XtendStandaloneSetup().createInjectorAndDoEMFRegistration();
Mwe2Runner mweRunner = injector.getInstance(Mwe2Runner.class);
//mweRunner.run(URI.createFileURI(mweFile.getAbsolutePath()), empty);
mweRunner.run(URI.createFileURI("myworkflow.mwe2"), prepareWorkflowProperties(), slots);

I tried several methods how to create the StandaloneSetup.
The TargetmodelFactoryStandaloneSetup looks like:
public class TargetmodelFactoryStandaloneSetup extends Mwe2StandaloneSetupGenerated {

  public static void doSetup() {
    new TargetmodelFactoryStandaloneSetup().createInjectorAndDoEMFRegistration();
  }

  public Injector createInjectorAndDoEMFRegistration() {
    org.eclipse.xtext.xbase.XbaseStandaloneSetup.doSetup();

    // register default ePackages
    if (!Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("ecore"))
      Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
          "ecore", new org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl());
    if (!Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("xmi"))
      Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
          "xmi", new org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl());
    if (!EPackage.Registry.INSTANCE.containsKey(org.eclipse.xtext.XtextPackage.eNS_URI))
EPackage.Registry.INSTANCE.put(org.eclipse.xtext.XtextPackage.eNS_URI, org.eclipse.xtext.XtextPackage.eINSTANCE);

    Injector injector = createInjector();
    register(injector);
    return injector;
  }
	
  public Injector createInjector() {
    return Guice.createInjector(Modules2.mixin(
        new mypackage.TargetmodelFactoryRuntimeModule(),
        new org.eclipse.emf.mwe2.language.Mwe2RuntimeModule()
    ));
  }
	
  public void register(Injector injector) {

    if (!EPackage.Registry.INSTANCE.containsKey("TargetmodelFactory")) {
      EPackage.Registry.INSTANCE.put("TargetmodelFactory", mypackage.TargetmodelFactory.eINSTANCE);
    }

    // Copied from Mwe2StandaloneSetupGenerated:
    org.eclipse.xtext.resource.IResourceFactory resourceFactory = injector.getInstance(org.eclipse.xtext.resource.IResourceFactory.class);
    org.eclipse.xtext.resource.IResourceServiceProvider serviceProvider = injector.getInstance(org.eclipse.xtext.resource.IResourceServiceProvider.class);
    Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("mwe2", resourceFactory);
    org.eclipse.xtext.resource.IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("mwe2", serviceProvider);
  }
}

The RuntimeModule looks like:
public class TargetmodelFactoryRuntimeModule extends AbstractGenericResourceRuntimeModule {

  // contributed by org.eclipse.xtext.generator.types.TypesGeneratorFragment
  public org.eclipse.xtext.common.types.TypesFactory bindTypesFactoryToInstance() {
    return org.eclipse.xtext.common.types.TypesFactory.eINSTANCE;
  }

  @Override
  protected String getLanguageName() {
    return "targetmodel";
  }

  @Override
  protected String getFileExtensions() {
    return "mod";
  }

}

The InjectorProviderCustom is a copy from the domainmodel example:
public class InjectorProviderCustom extends TargetmodelFactoryInjectorProvider {
	
  public Injector getInjector() {
    if (injector == null) {
      stateBeforeInjectorCreation = GlobalRegistries.makeCopyOfGlobalState();
      this.injector = new TargetmodelFactoryStandaloneSetup() {
        //@Override
        //public Injector createInjector() {
        //	return Guice.createInjector(new SMMFactoryRuntimeModule() {
            //@Override
            //public ClassLoader bindClassLoaderToInstance() {
            //	return InjectorProviderCustom.class.getClassLoader();
            //}
        //	});
        //}
      }.createInjectorAndDoEMFRegistration();
      stateAfterInjectorCreation = GlobalRegistries.makeCopyOfGlobalState();
    }
    return injector;
  }
}

Does anyone have any idea how to get these Xtend classes injected?

[Updated on: Wed, 31 October 2012 03:03]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic:Resolve proxies
Next Topic:How to access annotations in JvmAnnotaionReference?
Goto Forum:
  


Current Time: Mon May 20 04:02:59 EDT 2013

Powered by FUDForum. Page generated in 0.01819 seconds