Search in sources :

Example 1 with OptionalBinderBinding

use of com.google.inject.multibindings.OptionalBinderBinding in project guice by google.

the class SpiUtils method optionalInjectorTest.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> void optionalInjectorTest(Key<T> keyType, Iterable<? extends Module> modules, int expectedOtherOptionalBindings, BindResult<?> expectedDefault, BindResult<?> expectedActual, BindResult<?> expectedUserLinkedActual) {
    if (expectedUserLinkedActual != null) {
        assertNull("cannot have actual if expecting user binding", expectedActual);
        assertNull("cannot have default if expecting user binding", expectedDefault);
    }
    Key<Optional<T>> optionalKey = keyType.ofType(RealOptionalBinder.optionalOf(keyType.getTypeLiteral()));
    Key<?> javaOptionalKey = keyType.ofType(RealOptionalBinder.javaOptionalOf(keyType.getTypeLiteral()));
    Injector injector = Guice.createInjector(modules);
    Binding<Optional<T>> optionalBinding = injector.getBinding(optionalKey);
    Visitor visitor = new Visitor();
    OptionalBinderBinding<Optional<T>> optionalBinder = (OptionalBinderBinding<Optional<T>>) optionalBinding.acceptTargetVisitor(visitor);
    assertNotNull(optionalBinder);
    assertEquals(optionalKey, optionalBinder.getKey());
    Binding<?> javaOptionalBinding = injector.getBinding(javaOptionalKey);
    OptionalBinderBinding<?> javaOptionalBinder = (OptionalBinderBinding<?>) javaOptionalBinding.acceptTargetVisitor(visitor);
    assertNotNull(javaOptionalBinder);
    assertEquals(javaOptionalKey, javaOptionalBinder.getKey());
    if (expectedDefault == null) {
        assertNull("did not expect a default binding", optionalBinder.getDefaultBinding());
        assertNull("did not expect a default binding", javaOptionalBinder.getDefaultBinding());
    } else {
        assertTrue("expectedDefault: " + expectedDefault + ", actualDefault: " + optionalBinder.getDefaultBinding(), matches(optionalBinder.getDefaultBinding(), expectedDefault));
        assertTrue("expectedDefault: " + expectedDefault + ", actualDefault: " + javaOptionalBinder.getDefaultBinding(), matches(javaOptionalBinder.getDefaultBinding(), expectedDefault));
    }
    if (expectedActual == null && expectedUserLinkedActual == null) {
        assertNull(optionalBinder.getActualBinding());
        assertNull(javaOptionalBinder.getActualBinding());
    } else if (expectedActual != null) {
        assertTrue("expectedActual: " + expectedActual + ", actualActual: " + optionalBinder.getActualBinding(), matches(optionalBinder.getActualBinding(), expectedActual));
        assertTrue("expectedActual: " + expectedActual + ", actualActual: " + javaOptionalBinder.getActualBinding(), matches(javaOptionalBinder.getActualBinding(), expectedActual));
    } else if (expectedUserLinkedActual != null) {
        assertTrue("expectedUserLinkedActual: " + expectedUserLinkedActual + ", actualActual: " + optionalBinder.getActualBinding(), matches(optionalBinder.getActualBinding(), expectedUserLinkedActual));
        assertTrue("expectedUserLinkedActual: " + expectedUserLinkedActual + ", actualActual: " + javaOptionalBinder.getActualBinding(), matches(javaOptionalBinder.getActualBinding(), expectedUserLinkedActual));
    }
    Key<Optional<javax.inject.Provider<T>>> optionalJavaxProviderKey = keyType.ofType(RealOptionalBinder.optionalOfJavaxProvider(keyType.getTypeLiteral()));
    Key<?> javaOptionalJavaxProviderKey = keyType.ofType(RealOptionalBinder.javaOptionalOfJavaxProvider(keyType.getTypeLiteral()));
    Key<Optional<Provider<T>>> optionalProviderKey = keyType.ofType(RealOptionalBinder.optionalOfProvider(keyType.getTypeLiteral()));
    Key<?> javaOptionalProviderKey = keyType.ofType(RealOptionalBinder.javaOptionalOfProvider(keyType.getTypeLiteral()));
    assertEquals(ImmutableSet.of(optionalJavaxProviderKey, optionalProviderKey), optionalBinder.getAlternateKeys());
    assertEquals(ImmutableSet.of(javaOptionalJavaxProviderKey, javaOptionalProviderKey), javaOptionalBinder.getAlternateKeys());
    boolean keyMatch = false;
    boolean optionalKeyMatch = false;
    boolean javaOptionalKeyMatch = false;
    boolean optionalJavaxProviderKeyMatch = false;
    boolean javaOptionalJavaxProviderKeyMatch = false;
    boolean optionalProviderKeyMatch = false;
    boolean javaOptionalProviderKeyMatch = false;
    boolean defaultMatch = false;
    boolean actualMatch = false;
    List<Object> otherOptionalBindings = Lists.newArrayList();
    List<Binding> otherMatches = Lists.newArrayList();
    for (Binding b : injector.getAllBindings().values()) {
        boolean contains = optionalBinder.containsElement(b);
        assertEquals(contains, javaOptionalBinder.containsElement(b));
        Object visited = b.acceptTargetVisitor(visitor);
        if (visited instanceof OptionalBinderBinding) {
            if (visited.equals(optionalBinder)) {
                assertTrue(contains);
            } else if (visited.equals(javaOptionalBinder)) {
                assertTrue(contains);
            } else {
                otherOptionalBindings.add(visited);
            }
        }
        if (b.getKey().equals(keyType)) {
            // keyType might match because a user bound it
            // (which is possible in a purely absent OptionalBinder)
            assertEquals(expectedDefault != null || expectedActual != null, contains);
            if (contains) {
                keyMatch = true;
            }
        } else if (b.getKey().equals(optionalKey)) {
            assertTrue(contains);
            optionalKeyMatch = true;
        } else if (b.getKey().equals(javaOptionalKey)) {
            assertTrue(contains);
            javaOptionalKeyMatch = true;
        } else if (b.getKey().equals(optionalJavaxProviderKey)) {
            assertTrue(contains);
            optionalJavaxProviderKeyMatch = true;
        } else if (b.getKey().equals(javaOptionalJavaxProviderKey)) {
            assertTrue(contains);
            javaOptionalJavaxProviderKeyMatch = true;
        } else if (b.getKey().equals(optionalProviderKey)) {
            assertTrue(contains);
            optionalProviderKeyMatch = true;
        } else if (b.getKey().equals(javaOptionalProviderKey)) {
            assertTrue(contains);
            javaOptionalProviderKeyMatch = true;
        } else if (expectedDefault != null && matches(b, expectedDefault)) {
            assertTrue(contains);
            defaultMatch = true;
        } else if (expectedActual != null && matches(b, expectedActual)) {
            assertTrue(contains);
            actualMatch = true;
        } else if (contains) {
            otherMatches.add(b);
        }
    }
    assertEquals(otherMatches.toString(), 0, otherMatches.size());
    // only expect a keymatch if either default or actual are set
    assertEquals(expectedDefault != null || expectedActual != null, keyMatch);
    assertTrue(optionalKeyMatch);
    assertTrue(optionalJavaxProviderKeyMatch);
    assertTrue(optionalProviderKeyMatch);
    assertTrue(javaOptionalKeyMatch);
    assertTrue(javaOptionalJavaxProviderKeyMatch);
    assertTrue(javaOptionalProviderKeyMatch);
    assertEquals(expectedDefault != null, defaultMatch);
    assertEquals(expectedActual != null, actualMatch);
    assertEquals("other OptionalBindings found: " + otherOptionalBindings, expectedOtherOptionalBindings, otherOptionalBindings.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) OptionalBinderBinding(com.google.inject.multibindings.OptionalBinderBinding) Optional(com.google.common.base.Optional) MultibindingsTargetVisitor(com.google.inject.multibindings.MultibindingsTargetVisitor) DefaultBindingTargetVisitor(com.google.inject.spi.DefaultBindingTargetVisitor) Injector(com.google.inject.Injector)

Example 2 with OptionalBinderBinding

use of com.google.inject.multibindings.OptionalBinderBinding in project guice by google.

the class SpiUtils method optionalModuleTest.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> void optionalModuleTest(Key<T> keyType, Iterable<? extends Module> modules, int expectedOtherOptionalBindings, BindResult<?> expectedDefault, BindResult<?> expectedActual, BindResult<?> expectedUserLinkedActual) {
    if (expectedUserLinkedActual != null) {
        assertNull("cannot have actual if expecting user binding", expectedActual);
        assertNull("cannot have default if expecting user binding", expectedDefault);
    }
    Set<Element> elements = ImmutableSet.copyOf(Elements.getElements(modules));
    Map<Key<?>, Binding<?>> indexed = index(elements);
    Key<Optional<T>> optionalKey = keyType.ofType(RealOptionalBinder.optionalOf(keyType.getTypeLiteral()));
    Key<?> javaOptionalKey = keyType.ofType(RealOptionalBinder.javaOptionalOf(keyType.getTypeLiteral()));
    Visitor visitor = new Visitor();
    Key<?> defaultKey = null;
    Key<?> actualKey = null;
    Binding optionalBinding = indexed.get(optionalKey);
    OptionalBinderBinding<Optional<T>> optionalBinder = (OptionalBinderBinding<Optional<T>>) optionalBinding.acceptTargetVisitor(visitor);
    Binding javaOptionalBinding = indexed.get(javaOptionalKey);
    OptionalBinderBinding<?> javaOptionalBinder = (OptionalBinderBinding) javaOptionalBinding.acceptTargetVisitor(visitor);
    // Locate the defaultKey & actualKey
    for (Element element : elements) {
        if (optionalBinder.containsElement(element) && element instanceof Binding) {
            Binding binding = (Binding) element;
            if (isSourceEntry(binding, RealOptionalBinder.Source.DEFAULT)) {
                defaultKey = binding.getKey();
            } else if (isSourceEntry(binding, RealOptionalBinder.Source.ACTUAL)) {
                actualKey = binding.getKey();
            }
        }
    }
    assertNotNull(optionalBinder);
    assertNotNull(javaOptionalBinder);
    assertEquals(expectedDefault == null, defaultKey == null);
    assertEquals(expectedActual == null, actualKey == null);
    Key<Optional<javax.inject.Provider<T>>> optionalJavaxProviderKey = keyType.ofType(RealOptionalBinder.optionalOfJavaxProvider(keyType.getTypeLiteral()));
    Key<?> javaOptionalJavaxProviderKey = keyType.ofType(RealOptionalBinder.javaOptionalOfJavaxProvider(keyType.getTypeLiteral()));
    Key<Optional<Provider<T>>> optionalProviderKey = keyType.ofType(RealOptionalBinder.optionalOfProvider(keyType.getTypeLiteral()));
    Key<?> javaOptionalProviderKey = keyType.ofType(RealOptionalBinder.javaOptionalOfProvider(keyType.getTypeLiteral()));
    boolean keyMatch = false;
    boolean optionalKeyMatch = false;
    boolean javaOptionalKeyMatch = false;
    boolean optionalJavaxProviderKeyMatch = false;
    boolean javaOptionalJavaxProviderKeyMatch = false;
    boolean optionalProviderKeyMatch = false;
    boolean javaOptionalProviderKeyMatch = false;
    boolean defaultMatch = false;
    boolean actualMatch = false;
    List<Object> otherOptionalElements = Lists.newArrayList();
    List<Element> otherContains = Lists.newArrayList();
    List<Element> nonContainedElements = Lists.newArrayList();
    for (Element element : elements) {
        boolean contains = optionalBinder.containsElement(element);
        assertEquals(contains, javaOptionalBinder.containsElement(element));
        if (!contains) {
            nonContainedElements.add(element);
        }
        Key key = null;
        Binding b = null;
        if (element instanceof Binding) {
            b = (Binding) element;
            key = b.getKey();
            Object visited = b.acceptTargetVisitor(visitor);
            if (visited instanceof OptionalBinderBinding) {
                if (visited.equals(optionalBinder)) {
                    assertTrue(contains);
                } else if (visited.equals(javaOptionalBinder)) {
                    assertTrue(contains);
                } else {
                    otherOptionalElements.add(visited);
                }
            }
        } else if (element instanceof ProviderLookup) {
            key = ((ProviderLookup) element).getKey();
        }
        if (key != null && key.equals(keyType)) {
            // keyType might match because a user bound it
            // (which is possible in a purely absent OptionalBinder)
            assertEquals(expectedDefault != null || expectedActual != null, contains);
            if (contains) {
                keyMatch = true;
            }
        } else if (key != null && key.equals(optionalKey)) {
            assertTrue(contains);
            optionalKeyMatch = true;
        } else if (key != null && key.equals(javaOptionalKey)) {
            assertTrue(contains);
            javaOptionalKeyMatch = true;
        } else if (key != null && key.equals(optionalJavaxProviderKey)) {
            assertTrue(contains);
            optionalJavaxProviderKeyMatch = true;
        } else if (key != null && key.equals(javaOptionalJavaxProviderKey)) {
            assertTrue(contains);
            javaOptionalJavaxProviderKeyMatch = true;
        } else if (key != null && key.equals(optionalProviderKey)) {
            assertTrue(contains);
            optionalProviderKeyMatch = true;
        } else if (key != null && key.equals(javaOptionalProviderKey)) {
            assertTrue(contains);
            javaOptionalProviderKeyMatch = true;
        } else if (key != null && key.equals(defaultKey)) {
            assertTrue(contains);
            if (b != null) {
                // otherwise it might just be a ProviderLookup into it
                assertTrue("expected: " + expectedDefault + ", but was: " + b, matches(b, expectedDefault));
                defaultMatch = true;
            }
        } else if (key != null && key.equals(actualKey)) {
            assertTrue(contains);
            if (b != null) {
                // otherwise it might just be a ProviderLookup into it
                assertTrue("expected: " + expectedActual + ", but was: " + b, matches(b, expectedActual));
                actualMatch = true;
            }
        } else if (contains) {
            otherContains.add(element);
        }
    }
    // only expect a keymatch if either default or actual are set
    assertEquals(expectedDefault != null || expectedActual != null, keyMatch);
    assertTrue(optionalKeyMatch);
    assertTrue(optionalJavaxProviderKeyMatch);
    assertTrue(optionalProviderKeyMatch);
    assertTrue(javaOptionalKeyMatch);
    assertTrue(javaOptionalJavaxProviderKeyMatch);
    assertTrue(javaOptionalProviderKeyMatch);
    assertEquals(expectedDefault != null, defaultMatch);
    assertEquals(expectedActual != null, actualMatch);
    assertEquals(otherContains.toString(), 0, otherContains.size());
    assertEquals("other OptionalBindings found: " + otherOptionalElements, expectedOtherOptionalBindings, otherOptionalElements.size());
    // Validate that we can construct an injector out of the remaining bindings.
    Guice.createInjector(Elements.getModule(nonContainedElements));
}
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) OptionalBinderBinding(com.google.inject.multibindings.OptionalBinderBinding) Optional(com.google.common.base.Optional) MultibindingsTargetVisitor(com.google.inject.multibindings.MultibindingsTargetVisitor) DefaultBindingTargetVisitor(com.google.inject.spi.DefaultBindingTargetVisitor) Element(com.google.inject.spi.Element) ProviderLookup(com.google.inject.spi.ProviderLookup) Key(com.google.inject.Key)

Aggregations

Optional (com.google.common.base.Optional)2 Binding (com.google.inject.Binding)2 IndexedBinding (com.google.inject.internal.Indexer.IndexedBinding)2 MapBinderBinding (com.google.inject.multibindings.MapBinderBinding)2 MultibinderBinding (com.google.inject.multibindings.MultibinderBinding)2 MultibindingsTargetVisitor (com.google.inject.multibindings.MultibindingsTargetVisitor)2 OptionalBinderBinding (com.google.inject.multibindings.OptionalBinderBinding)2 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)2 InstanceBinding (com.google.inject.spi.InstanceBinding)2 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)2 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)2 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)2 Injector (com.google.inject.Injector)1 Key (com.google.inject.Key)1 Element (com.google.inject.spi.Element)1 ProviderLookup (com.google.inject.spi.ProviderLookup)1