MCPcopy Create free account
hub / github.com/google/guice / testIsScopedPositive

Method testIsScopedPositive

core/test/com/google/inject/ScopesTest.java:759–815  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

757 }
758
759 public void testIsScopedPositive() {
760 final Key<String> a = Key.get(String.class, named("A"));
761 final Key<String> b = Key.get(String.class, named("B"));
762 final Key<String> c = Key.get(String.class, named("C"));
763 final Key<String> d = Key.get(String.class, named("D"));
764 final Key<String> e = Key.get(String.class, named("E"));
765 final Key<Object> f = Key.get(Object.class, named("F"));
766 final Key<String> g = Key.get(String.class, named("G"));
767
768 Module customBindings =
769 new AbstractModule() {
770 @Override
771 protected void configure() {
772 bindScope(CustomScoped.class, CUSTOM_SCOPE);
773 bind(a).to(b);
774 bind(b).to(c);
775 bind(c).toProvider(Providers.of("c")).in(CUSTOM_SCOPE);
776 bind(d).toProvider(Providers.of("d")).in(CustomScoped.class);
777 bind(f).to(AnnotatedCustomScoped.class);
778 install(
779 new PrivateModule() {
780 @Override
781 protected void configure() {
782 bind(g).toProvider(Providers.of("g")).in(CustomScoped.class);
783 expose(g);
784 }
785 });
786 }
787
788 @Provides
789 @Named("E")
790 @CustomScoped
791 String provideE() {
792 return "e";
793 }
794 };
795
796 @SuppressWarnings("unchecked") // we know the module contains only bindings
797 List<Element> moduleBindings = Elements.getElements(customBindings);
798 ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
799 assertFalse(isCustomScoped(map.get(a))); // linked bindings are not followed by modules
800 assertFalse(isCustomScoped(map.get(b)));
801 assertTrue(isCustomScoped(map.get(c)));
802 assertTrue(isCustomScoped(map.get(d)));
803 assertTrue(isCustomScoped(map.get(e)));
804 assertFalse(isCustomScoped(map.get(f))); // annotated classes are not followed by modules
805 assertTrue(isCustomScoped(map.get(g)));
806
807 Injector injector = Guice.createInjector(customBindings);
808 assertTrue(isCustomScoped(injector.getBinding(a)));
809 assertTrue(isCustomScoped(injector.getBinding(b)));
810 assertTrue(isCustomScoped(injector.getBinding(c)));
811 assertTrue(isCustomScoped(injector.getBinding(d)));
812 assertTrue(isCustomScoped(injector.getBinding(e)));
813 assertTrue(isCustomScoped(injector.getBinding(f)));
814 assertTrue(isCustomScoped(injector.getBinding(g)));
815 }
816

Callers

nothing calls this directly

Calls 8

getMethod · 0.95
getElementsMethod · 0.95
indexBindingsMethod · 0.95
isCustomScopedMethod · 0.95
createInjectorMethod · 0.95
getBindingMethod · 0.95
namedMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected