コード例 #1
0
  public void testProviderMethodDependenciesAreExposed() throws Exception {
    Module module =
        new AbstractModule() {
          @Override
          protected void configure() {
            bind(Integer.class).toInstance(50);
            bindConstant().annotatedWith(Names.named("units")).to("Kg");
          }

          @Provides
          @Named("weight")
          String provideWeight(Integer count, @Named("units") String units) {
            return count + units;
          }
        };
    Injector injector = Guice.createInjector(module);

    ProviderInstanceBinding<?> binding =
        (ProviderInstanceBinding<?>)
            injector.getBinding(Key.get(String.class, Names.named("weight")));
    Method method =
        module.getClass().getDeclaredMethod("provideWeight", Integer.class, String.class);
    InjectionPoint point = new InjectionPoint(TypeLiteral.get(module.getClass()), method, false);
    assertEquals(
        ImmutableSet.<Dependency<?>>of(
            new Dependency<Integer>(point, Key.get(Integer.class), false, 0),
            new Dependency<String>(point, Key.get(String.class, Names.named("units")), false, 1)),
        binding.getDependencies());
  }
コード例 #2
0
  public void testModuleBindings() throws Exception {
    Module module =
        new AbstractModule() {
          @Override
          protected void configure() {}

          @Provides
          Integer fail() {
            return 1;
          }
        };
    // sanity check that the injector works
    Injector injector = Guice.createInjector(module);
    assertEquals(1, injector.getInstance(Integer.class).intValue());
    ProviderInstanceBinding injectorBinding =
        (ProviderInstanceBinding) injector.getBinding(Integer.class);
    assertEquals(1, injectorBinding.getUserSuppliedProvider().get());

    ProviderInstanceBinding moduleBinding =
        (ProviderInstanceBinding) Iterables.getOnlyElement(Elements.getElements(module));
    try {
      moduleBinding.getUserSuppliedProvider().get();
      fail();
    } catch (IllegalStateException ise) {
      assertEquals(
          "This Provider cannot be used until the Injector has been created.", ise.getMessage());
    }
  }
コード例 #3
0
  public void testNonModuleProviderMethods() {
    final Object methodsObject =
        new Object() {
          @Provides
          @Named("foo")
          String provideFoo() {
            return "foo-value";
          }
        };

    Module module =
        new AbstractModule() {
          @Override
          protected void configure() {
            install(ProviderMethodsModule.forObject(methodsObject));
          }
        };

    Injector injector = Guice.createInjector(module);

    Key<String> key = Key.get(String.class, Names.named("foo"));
    assertEquals("foo-value", injector.getInstance(key));

    // Test the provider method object itself. This makes sure getInstance works, since GIN uses it
    List<Element> elements = Elements.getElements(module);
    assertEquals(1, elements.size());

    Element element = elements.get(0);
    assertTrue(
        element + " instanceof ProviderInstanceBinding",
        element instanceof ProviderInstanceBinding);

    ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
    javax.inject.Provider provider = binding.getUserSuppliedProvider();
    assertTrue(provider instanceof ProviderMethod);
    assertEquals(methodsObject, ((ProviderMethod) provider).getInstance());
    assertSame(provider, binding.getProviderInstance());
  }