@Test(dataProvider = "builders")
  public void testUsingInWithProvider(LifecycleInjectorBuilder lifecycleInjectorBuilder) {
    Injector injector =
        lifecycleInjectorBuilder
            .withModules(
                new Module() {
                  @Override
                  public void configure(Binder binder) {
                    binder.bind(LazySingletonObject.class).in(LazySingletonScope.get());
                  }
                })
            .createInjector();

    Assert.assertEquals(LazySingletonObject.constructorCount.get(), 0);
    Assert.assertEquals(LazySingletonObject.postConstructCount.get(), 0);

    InjectedProvider injectedProvider = injector.getInstance(InjectedProvider.class);
    Assert.assertEquals(LazySingletonObject.constructorCount.get(), 0);
    Assert.assertEquals(LazySingletonObject.postConstructCount.get(), 0);

    LazySingletonObject instance = injectedProvider.provider.get();
    Assert.assertEquals(LazySingletonObject.constructorCount.get(), 1);
    Assert.assertEquals(LazySingletonObject.postConstructCount.get(), 1);

    LazySingletonObject instance2 = injectedProvider.provider.get();
    Assert.assertEquals(LazySingletonObject.constructorCount.get(), 1);
    Assert.assertEquals(LazySingletonObject.postConstructCount.get(), 1);

    Assert.assertSame(instance, instance2);
  }
Example #2
0
  @Test(dataProvider = "builders")
  public void testMultiple(LifecycleInjectorBuilder lifecycleInjectorBuilder) {
    final AutoBindProvider<AutoBind> provider =
        new AutoBindProvider<AutoBind>() {
          @Override
          public void configure(Binder binder, AutoBind autoBindAnnotation) {
            binder
                .bind(MockWithParameter.class)
                .annotatedWith(autoBindAnnotation)
                .toInstance(new MockWithParameter(autoBindAnnotation.value()));
          }
        };

    Injector injector =
        lifecycleInjectorBuilder
            .ignoringAutoBindClasses(
                Collections.<Class<?>>singleton(ObjectWithCustomAutoBind.class))
            .withBootstrapModule(
                new BootstrapModule() {
                  @Override
                  public void configure(BootstrapBinder binder) {
                    binder
                        .bind(new TypeLiteral<AutoBindProvider<AutoBind>>() {})
                        .toInstance(provider);
                  }
                })
            .usingBasePackages("com.netflix.governator.autobind")
            .createInjector();
    SimpleWithMultipleAutoBinds instance = injector.getInstance(SimpleWithMultipleAutoBinds.class);
    Assert.assertEquals(instance.getArg1().getParameter(), "one");
    Assert.assertEquals(instance.getArg2().getParameter(), "two");
    Assert.assertEquals(instance.getArg3().getParameter(), "three");
    Assert.assertEquals(instance.getArg4().getParameter(), "four");
  }
Example #3
0
 @Test(dataProvider = "builders")
 public void testCustom(LifecycleInjectorBuilder lifecycleInjectorBuilder) {
   @SuppressWarnings("RedundantCast")
   Injector injector =
       lifecycleInjectorBuilder
           .ignoringAutoBindClasses(
               Lists.newArrayList(
                   (Class<?>) SimpleAutoBind.class,
                   (Class<?>) SimpleWithMultipleAutoBinds.class,
                   (Class<?>) SimpleWithFieldAutoBind.class,
                   (Class<?>) SimpleWithMethodAutoBind.class))
           .withBootstrapModule(
               new BootstrapModule() {
                 @Override
                 public void configure(BootstrapBinder binder) {
                   binder
                       .bind(new TypeLiteral<AutoBindProvider<CustomAutoBind>>() {})
                       .to(CustomAutoBindProvider.class)
                       .asEagerSingleton();
                 }
               })
           .usingBasePackages("com.netflix.governator.autobind")
           .createInjector();
   ObjectWithCustomAutoBind instance = injector.getInstance(ObjectWithCustomAutoBind.class);
   Assert.assertEquals(instance.getInjectable().getStr(), "hey");
   Assert.assertEquals(instance.getInjectable().getValue(), 1234);
 }
Example #4
0
  @Test(dataProvider = "builders")
  public void testSimple(LifecycleInjectorBuilder lifecycleInjectorBuilder) {
    final AutoBindProvider<AutoBind> provider =
        new AutoBindProvider<AutoBind>() {
          @Override
          public void configure(Binder binder, AutoBind autoBindAnnotation) {
            binder.bind(String.class).annotatedWith(autoBindAnnotation).toInstance("a is a");
          }
        };

    Injector injector =
        lifecycleInjectorBuilder
            .ignoringAutoBindClasses(
                Collections.<Class<?>>singleton(ObjectWithCustomAutoBind.class))
            .withBootstrapModule(
                new BootstrapModule() {
                  @Override
                  public void configure(BootstrapBinder binder) {
                    binder
                        .bind(new TypeLiteral<AutoBindProvider<AutoBind>>() {})
                        .toInstance(provider);
                  }
                })
            .usingBasePackages("com.netflix.governator.autobind")
            .createInjector();
    SimpleAutoBind instance = injector.getInstance(SimpleAutoBind.class);
    Assert.assertEquals(instance.getString(), "a is a");
  }
Example #5
0
  @Test
  @UseDataProvider("builders")
  public void basicTest(LifecycleInjectorBuilder lifecycleInjectorBuilder) throws Exception {
    final AtomicInteger classResourceCount = new AtomicInteger(0);
    final ResourceLocator resourceLocator =
        new ResourceLocator() {
          @Override
          public Object locate(Resource resource, ResourceLocator nextInChain) throws Exception {
            if (resource.name().equals(ObjectWithResources.class.getName() + "/myResource")) {
              return "a";
            }

            if (resource.name().equals("overrideInt")) {
              return BigInteger.valueOf(2);
            }

            if (resource.name().equals(ObjectWithResources.class.getName() + "/p")) {
              return new Point(3, 4);
            }

            if (resource.name().equals("overrideRect")) {
              return new Rectangle(5, 6);
            }

            if (resource.name().equals("classResource")) {
              classResourceCount.incrementAndGet();
              return 7.8;
            }

            return null;
          }
        };
    Injector injector =
        lifecycleInjectorBuilder
            .withBootstrapModule(
                new BootstrapModule() {
                  @Override
                  public void configure(BootstrapBinder binder) {
                    binder.bindResourceLocator().toInstance(resourceLocator);
                  }
                })
            .createInjector();

    ObjectWithResources obj = injector.getInstance(ObjectWithResources.class);
    Assert.assertEquals(obj.getMyResource(), "a");
    Assert.assertEquals(obj.getMyOverrideResource(), BigInteger.valueOf(2));
    Assert.assertEquals(obj.getP(), new Point(3, 4));
    Assert.assertEquals(obj.getR(), new Rectangle(5, 6));
    Assert.assertEquals(classResourceCount.get(), 1);
  }
  @Test(dataProvider = "builders")
  public void testUsingAnnotation(LifecycleInjectorBuilder lifecycleInjectorBuilder) {
    Injector injector = lifecycleInjectorBuilder.createInjector();

    Assert.assertEquals(AnnotatedLazySingletonObject.constructorCount.get(), 0);
    Assert.assertEquals(AnnotatedLazySingletonObject.postConstructCount.get(), 0);

    AnnotatedLazySingletonObject instance =
        injector.getInstance(AnnotatedLazySingletonObject.class);
    Assert.assertEquals(AnnotatedLazySingletonObject.constructorCount.get(), 1);
    Assert.assertEquals(AnnotatedLazySingletonObject.postConstructCount.get(), 1);

    AnnotatedLazySingletonObject instance2 =
        injector.getInstance(AnnotatedLazySingletonObject.class);
    Assert.assertEquals(AnnotatedLazySingletonObject.constructorCount.get(), 1);
    Assert.assertEquals(AnnotatedLazySingletonObject.postConstructCount.get(), 1);

    Assert.assertSame(instance, instance2);
  }
Example #7
0
  @Test
  @UseDataProvider("builders")
  public void testChained(LifecycleInjectorBuilder lifecycleInjectorBuilder) throws Exception {
    final AtomicInteger resourceLocator1Count = new AtomicInteger(0);
    final AtomicInteger resourceLocator2Count = new AtomicInteger(0);

    final ResourceLocator resourceLocator1 =
        new ResourceLocator() {
          @Override
          public Object locate(Resource resource, ResourceLocator nextInChain) throws Exception {
            resourceLocator1Count.incrementAndGet();
            return nextInChain.locate(resource, nextInChain);
          }
        };
    final ResourceLocator resourceLocator2 =
        new ResourceLocator() {
          @Override
          public Object locate(Resource resource, ResourceLocator nextInChain) throws Exception {
            resourceLocator2Count.incrementAndGet();
            return nextInChain.locate(resource, nextInChain);
          }
        };
    Injector injector =
        lifecycleInjectorBuilder
            .withBootstrapModule(
                new BootstrapModule() {
                  @Override
                  public void configure(BootstrapBinder binder) {
                    binder.bindResourceLocator().toInstance(resourceLocator1);
                    binder.bindResourceLocator().toInstance(resourceLocator2);
                    binder.bind(BigInteger.class).toInstance(BigInteger.valueOf(1));
                    binder.bind(Double.class).toInstance(1.1);
                  }
                })
            .createInjector();

    injector.getInstance(ObjectWithResources.class);
    Assert.assertEquals(resourceLocator1Count.get(), 5); // 1 for each @Resource
    Assert.assertEquals(resourceLocator2Count.get(), 5); //      "       "
  }