Search in sources :

Example 1 with LinkedKeyBinding

use of com.google.inject.spi.LinkedKeyBinding in project roboguice by roboguice.

the class MultibinderTest method testMultibinderDependenciesInToolStage.

/**
   * We just want to make sure that multibinder's binding depends on each of its values. We don't
   * really care about the underlying structure of those bindings, which are implementation details.
   */
public void testMultibinderDependenciesInToolStage() {
    Injector injector = Guice.createInjector(Stage.TOOL, new AbstractModule() {

        protected void configure() {
            Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
            multibinder.addBinding().toInstance("A");
            multibinder.addBinding().to(Key.get(String.class, Names.named("b")));
            bindConstant().annotatedWith(Names.named("b")).to("B");
        }
    });
    Binding<Set<String>> binding = injector.getBinding(new Key<Set<String>>() {
    });
    HasDependencies withDependencies = (HasDependencies) binding;
    InstanceBinding<?> instanceBinding = null;
    LinkedKeyBinding<?> linkedBinding = null;
    // the Key of @Named("b") String=B
    for (Dependency<?> dependency : withDependencies.getDependencies()) {
        Binding<?> b = injector.getBinding(dependency.getKey());
        if (b instanceof InstanceBinding) {
            if (instanceBinding != null) {
                fail("Already have an instance binding of: " + instanceBinding + ", and now want to add: " + b);
            } else {
                instanceBinding = (InstanceBinding) b;
            }
        } else if (b instanceof LinkedKeyBinding) {
            if (linkedBinding != null) {
                fail("Already have a linked binding of: " + linkedBinding + ", and now want to add: " + b);
            } else {
                linkedBinding = (LinkedKeyBinding) b;
            }
        } else {
            fail("Unexpected dependency of: " + dependency);
        }
    }
    assertNotNull(instanceBinding);
    assertNotNull(linkedBinding);
    assertEquals("A", instanceBinding.getInstance());
    assertEquals(Key.get(String.class, Names.named("b")), linkedBinding.getLinkedKey());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) HasDependencies(com.google.inject.spi.HasDependencies) AbstractModule(com.google.inject.AbstractModule) InstanceBinding(com.google.inject.spi.InstanceBinding) Injector(com.google.inject.Injector) LinkedKeyBinding(com.google.inject.spi.LinkedKeyBinding)

Example 2 with LinkedKeyBinding

use of com.google.inject.spi.LinkedKeyBinding in project guice by google.

the class MultibinderTest method testMultibinderDependenciesInToolStage.

/**
   * We just want to make sure that multibinder's binding depends on each of its values. We don't
   * really care about the underlying structure of those bindings, which are implementation details.
   */
public void testMultibinderDependenciesInToolStage() {
    Injector injector = Guice.createInjector(Stage.TOOL, new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
            multibinder.addBinding().toInstance("A");
            multibinder.addBinding().to(Key.get(String.class, Names.named("b")));
            bindConstant().annotatedWith(Names.named("b")).to("B");
        }
    });
    Binding<Set<String>> binding = injector.getBinding(new Key<Set<String>>() {
    });
    HasDependencies withDependencies = (HasDependencies) binding;
    InstanceBinding<?> instanceBinding = null;
    LinkedKeyBinding<?> linkedBinding = null;
    // the Key of @Named("b") String=B
    for (Dependency<?> dependency : withDependencies.getDependencies()) {
        Binding<?> b = injector.getBinding(dependency.getKey());
        if (b instanceof InstanceBinding) {
            if (instanceBinding != null) {
                fail("Already have an instance binding of: " + instanceBinding + ", and now want to add: " + b);
            } else {
                instanceBinding = (InstanceBinding) b;
            }
        } else if (b instanceof LinkedKeyBinding) {
            if (linkedBinding != null) {
                fail("Already have a linked binding of: " + linkedBinding + ", and now want to add: " + b);
            } else {
                linkedBinding = (LinkedKeyBinding) b;
            }
        } else {
            fail("Unexpected dependency of: " + dependency);
        }
    }
    assertNotNull(instanceBinding);
    assertNotNull(linkedBinding);
    assertEquals("A", instanceBinding.getInstance());
    assertEquals(Key.get(String.class, Names.named("b")), linkedBinding.getLinkedKey());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) Multibinder(com.google.inject.multibindings.Multibinder) HasDependencies(com.google.inject.spi.HasDependencies) AbstractModule(com.google.inject.AbstractModule) InstanceBinding(com.google.inject.spi.InstanceBinding) Injector(com.google.inject.Injector) LinkedKeyBinding(com.google.inject.spi.LinkedKeyBinding)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)2 AbstractModule (com.google.inject.AbstractModule)2 Injector (com.google.inject.Injector)2 HasDependencies (com.google.inject.spi.HasDependencies)2 InstanceBinding (com.google.inject.spi.InstanceBinding)2 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Multibinder (com.google.inject.multibindings.Multibinder)1