/** * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. */ package org.thingml.xtext.tests; import com.google.inject.Guice; import com.google.inject.Injector; import org.eclipse.xtext.junit4.GlobalRegistries; import org.eclipse.xtext.junit4.GlobalRegistries.GlobalStateMemento; import org.eclipse.xtext.junit4.IInjectorProvider; import org.eclipse.xtext.junit4.IRegistryConfigurator; import org.thingml.xtext.ThingMLRuntimeModule; import org.thingml.xtext.ThingMLStandaloneSetup; public class ThingMLInjectorProvider implements IInjectorProvider, IRegistryConfigurator { protected GlobalStateMemento stateBeforeInjectorCreation; protected GlobalStateMemento stateAfterInjectorCreation; protected Injector injector; static { GlobalRegistries.initializeDefaults(); } @Override public Injector getInjector() { if (injector == null) { stateBeforeInjectorCreation = GlobalRegistries.makeCopyOfGlobalState(); this.injector = internalCreateInjector(); stateAfterInjectorCreation = GlobalRegistries.makeCopyOfGlobalState(); } return injector; } protected Injector internalCreateInjector() { return new ThingMLStandaloneSetup() { @Override public Injector createInjector() { return Guice.createInjector(createRuntimeModule()); } }.createInjectorAndDoEMFRegistration(); } protected ThingMLRuntimeModule createRuntimeModule() { // make it work also with Maven/Tycho and OSGI // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=493672 return new ThingMLRuntimeModule() { @Override public ClassLoader bindClassLoaderToInstance() { return ThingMLInjectorProvider.class .getClassLoader(); } }; } @Override public void restoreRegistry() { stateBeforeInjectorCreation.restoreGlobalState(); } @Override public void setupRegistry() { getInjector(); stateAfterInjectorCreation.restoreGlobalState(); } }