Search in sources :

Example 86 with Injector

use of com.google.inject.Injector in project guice by google.

the class SpiUtils method setInjectorTest.

@SuppressWarnings("unchecked")
private static <T> void setInjectorTest(Key<Set<T>> setKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult... results) {
    Key<?> collectionOfProvidersKey = setKey.ofType(collectionOfProvidersOf(elementType));
    Key<?> collectionOfJavaxProvidersKey = setKey.ofType(collectionOfJavaxProvidersOf(elementType));
    Injector injector = Guice.createInjector(modules);
    Visitor<Set<T>> visitor = new Visitor<Set<T>>();
    Binding<Set<T>> binding = injector.getBinding(setKey);
    MultibinderBinding<Set<T>> multibinder = (MultibinderBinding<Set<T>>) binding.acceptTargetVisitor(visitor);
    assertNotNull(multibinder);
    assertEquals(elementType, multibinder.getElementTypeLiteral());
    assertEquals(allowDuplicates, multibinder.permitsDuplicates());
    List<Binding<?>> elements = Lists.newArrayList(multibinder.getElements());
    List<BindResult> bindResults = Lists.newArrayList(results);
    assertEquals("wrong bind elements, expected: " + bindResults + ", but was: " + multibinder.getElements(), bindResults.size(), elements.size());
    for (BindResult result : bindResults) {
        Binding found = null;
        for (Binding item : elements) {
            if (matches(item, result)) {
                found = item;
                break;
            }
        }
        if (found == null) {
            fail("Could not find element: " + result + " in remaining elements: " + elements);
        } else {
            elements.remove(found);
        }
    }
    if (!elements.isEmpty()) {
        fail("Found all elements of: " + bindResults + ", but more were left over: " + elements);
    }
    Set<Binding> setOfElements = new HashSet<Binding>(multibinder.getElements());
    Set<IndexedBinding> setOfIndexed = Sets.newHashSet();
    Indexer indexer = new Indexer(injector);
    for (Binding<?> oneBinding : setOfElements) {
        setOfIndexed.add(oneBinding.acceptTargetVisitor(indexer));
    }
    List<Object> otherMultibinders = Lists.newArrayList();
    List<Binding> otherContains = Lists.newArrayList();
    boolean collectionOfProvidersMatch = false;
    boolean collectionOfJavaxProvidersMatch = false;
    for (Binding b : injector.getAllBindings().values()) {
        boolean contains = multibinder.containsElement(b);
        Key key = b.getKey();
        Object visited = b.acceptTargetVisitor(visitor);
        if (visited != null) {
            if (visited.equals(multibinder)) {
                assertTrue(contains);
            } else {
                otherMultibinders.add(visited);
            }
        } else if (setOfElements.contains(b)) {
            assertTrue(contains);
        } else if (key.equals(collectionOfProvidersKey)) {
            assertTrue(contains);
            collectionOfProvidersMatch = true;
        } else if (key.equals(collectionOfJavaxProvidersKey)) {
            assertTrue(contains);
            collectionOfJavaxProvidersMatch = true;
        } else if (contains) {
            if (!indexer.isIndexable(b) || !setOfIndexed.contains(b.acceptTargetVisitor(indexer))) {
                otherContains.add(b);
            }
        }
    }
    assertTrue(collectionOfProvidersMatch);
    assertTrue(collectionOfJavaxProvidersMatch);
    if (allowDuplicates) {
        assertEquals("contained more than it should: " + otherContains, 1, otherContains.size());
    } else {
        assertTrue("contained more than it should: " + otherContains, otherContains.isEmpty());
    }
    assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings, otherMultibinders.size());
}
Also used : MultibinderBinding(com.google.inject.multibindings.MultibinderBinding) ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) IndexedBinding(com.google.inject.internal.Indexer.IndexedBinding) Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) MapBinderBinding(com.google.inject.multibindings.MapBinderBinding) OptionalBinderBinding(com.google.inject.multibindings.OptionalBinderBinding) ProviderKeyBinding(com.google.inject.spi.ProviderKeyBinding) LinkedKeyBinding(com.google.inject.spi.LinkedKeyBinding) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) MultibindingsTargetVisitor(com.google.inject.multibindings.MultibindingsTargetVisitor) DefaultBindingTargetVisitor(com.google.inject.spi.DefaultBindingTargetVisitor) IndexedBinding(com.google.inject.internal.Indexer.IndexedBinding) MultibinderBinding(com.google.inject.multibindings.MultibinderBinding) Injector(com.google.inject.Injector) Key(com.google.inject.Key) HashSet(java.util.HashSet)

Example 87 with Injector

use of com.google.inject.Injector in project camel by apache.

the class InjectorManager method closeInjectors.

protected void closeInjectors() throws CloseFailedException {
    CloseErrors errors = new CloseErrorsImpl(this);
    Set<Entry<Object, Injector>> entries = injectors.entrySet();
    for (Entry<Object, Injector> entry : entries) {
        Injector injector = entry.getValue();
        Injectors.close(injector, errors);
    }
    injectors.clear();
    errors.throwIfNecessary();
}
Also used : CloseErrorsImpl(org.apache.camel.guice.support.internal.CloseErrorsImpl) Entry(java.util.Map.Entry) Injector(com.google.inject.Injector) CloseErrors(org.apache.camel.guice.support.CloseErrors)

Example 88 with Injector

use of com.google.inject.Injector in project camel by apache.

the class InjectorManager method beforeTest.

public void beforeTest(Object test) throws Exception {
    Preconditions.checkNotNull(test, "test");
    Class<? extends Object> testType = test.getClass();
    moduleType = getModuleForTestClass(testType);
    Injector classInjector = injectors.get(moduleType);
    if (classInjector == null) {
        classInjector = createInjector(moduleType);
        Preconditions.checkNotNull(classInjector, "classInjector");
        injectors.put(moduleType, classInjector);
    }
    injectors.put(testType, classInjector);
    classInjector.injectMembers(test);
}
Also used : Injector(com.google.inject.Injector)

Example 89 with Injector

use of com.google.inject.Injector in project camel by apache.

the class CollectionOfCustomRoutesTest method xtestGuice.

public void xtestGuice() throws Exception {
    Injector injector = Guice.createInjector(new MyModule());
    CamelContext camelContext = injector.getInstance(CamelContext.class);
    List<Route> list = camelContext.getRoutes();
    assertEquals("size of " + list, 2, list.size());
    GuiceTest.assertCamelContextRunningThenCloseInjector(injector);
}
Also used : CamelContext(org.apache.camel.CamelContext) Injector(com.google.inject.Injector) Route(org.apache.camel.Route)

Example 90 with Injector

use of com.google.inject.Injector in project camel by apache.

the class ComponentFoundInRegistryTest method testGuice.

@Test
public void testGuice() throws Exception {
    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.PROVIDER_URL, GuiceInitialContextFactory.class.getName());
    env.put(Injectors.MODULE_CLASS_NAMES, MyModule.class.getName());
    InitialContext context = new InitialContext(env);
    Injector injector = (Injector) context.lookup(Injector.class.getName());
    assertNotNull("Found injector", injector);
    Object value = context.lookup("foo");
    assertNotNull("Should have found a value for foo!", value);
    CamelContext camelContext = injector.getInstance(CamelContext.class);
    Component component = camelContext.getComponent("foo");
    assertThat(component, is(MockComponent.class));
    Endpoint endpoint = camelContext.getEndpoint("foo:cheese");
    assertThat(endpoint, is(MockEndpoint.class));
}
Also used : CamelContext(org.apache.camel.CamelContext) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Hashtable(java.util.Hashtable) MockComponent(org.apache.camel.component.mock.MockComponent) InitialContext(javax.naming.InitialContext) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Injector(com.google.inject.Injector) MockComponent(org.apache.camel.component.mock.MockComponent) Component(org.apache.camel.Component) GuiceInitialContextFactory(org.apache.camel.guice.jndi.GuiceInitialContextFactory) Test(org.junit.Test)

Aggregations

Injector (com.google.inject.Injector)2159 AbstractModule (com.google.inject.AbstractModule)623 Test (org.junit.Test)513 Module (com.google.inject.Module)364 Test (org.testng.annotations.Test)131 Before (org.junit.Before)116 Binder (com.google.inject.Binder)114 Properties (java.util.Properties)110 Key (com.google.inject.Key)84 Map (java.util.Map)78 HttpServletRequest (javax.servlet.http.HttpServletRequest)78 Provider (com.google.inject.Provider)74 IOException (java.io.IOException)71 TypeLiteral (com.google.inject.TypeLiteral)70 Set (java.util.Set)64 BeforeClass (org.junit.BeforeClass)61 File (java.io.File)60 CConfiguration (co.cask.cdap.common.conf.CConfiguration)55 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)55 PrivateModule (com.google.inject.PrivateModule)55