Skip to content

Instantly share code, notes, and snippets.

@rohangarg
Last active August 24, 2022 16:14
Show Gist options
  • Save rohangarg/838489720d0ef5093fe60df7f7b922d7 to your computer and use it in GitHub Desktop.
Save rohangarg/838489720d0ef5093fe60df7f7b922d7 to your computer and use it in GitHub Desktop.
PolyBindTest
@Test
public void foo()
{
// The test adds following things :
// 1. Adds three property KVs (prop1, goA), (prop2, goB), (prop3, goA)
// 2. Adds two options for Gogo.class in separate modules : GoA and GoB which have binding keys as 'goA' and 'goB'
// 3. Adds three choices for Gogo.class in separate modules which correspond to the three properties
// 4. checks via injector that first and third choice create GoA instances whereas choice second creates GoB instance
props = new Properties();
props.setProperty("prop1", "goA");
props.setProperty("prop2", "goB");
props.setProperty("prop3", "goA");
injector = Guice.createInjector(
Iterables.concat(
Collections.singletonList(
(Module) binder -> {
binder.bind(Properties.class).toInstance(props); // bind properties
}
),
Collections.singletonList(
(Module) binder -> {
PolyBind.optionBinder(binder, Key.get(Gogo.class))
.addBinding("goA")
.to(GoA.class); // bind one option
}
),
Collections.singletonList(
(Module) binder -> {
PolyBind.optionBinder(binder, Key.get(Gogo.class))
.addBinding("goB")
.to(GoB.class); // bind another option
}
),
Collections.singletonList(
(Module) binder -> {
PolyBind.createChoiceWithName(
binder,
"prop1",
Key.get(Gogo.class),
null,
Key.get(Gogo.class, Names.named("goA1"))
).in(Singleton.class); // create a choice with prop1 and name goA1
}
),
Collections.singletonList(
(Module) binder -> {
PolyBind.createChoiceWithName(
binder,
"prop2",
Key.get(Gogo.class),
null,
Key.get(Gogo.class, Names.named("goB"))
).in(Singleton.class); // create a choice with prop2 and name goB
}
),
Collections.singletonList(
(Module) binder -> {
PolyBind.createChoiceWithName(
binder,
"prop3",
Key.get(Gogo.class),
null,
Key.get(Gogo.class, Names.named("goA2"))
).in(Singleton.class); // create a choice with prop3 and name goA2
}
)
)
);
Gogo goA1 = injector.getInstance(Key.get(Gogo.class, Names.named("goA1")));
Gogo goB = injector.getInstance(Key.get(Gogo.class, Names.named("goB")));
Gogo goA2 = injector.getInstance(Key.get(Gogo.class, Names.named("goA2")));
Assert.assertTrue(goA1 instanceof GoA);
Assert.assertTrue(goB instanceof GoB);
Assert.assertTrue(goA2 instanceof GoA);
}
// method to support Named Interface Key for creation of multiple object of same type but with different names
// Added in PolyBind.java class
public static <T> ScopedBindingBuilder createChoiceWithName(
Binder binder,
String property,
Key<T> interfaceKey,
@Nullable Key<? extends T> defaultKey,
@Nullable Key<T> namedInterfaceKey
)
{
ConfiggedProvider<T> provider = new ConfiggedProvider<>(interfaceKey, property, defaultKey, null);
if (namedInterfaceKey == null) {
return binder.bind(interfaceKey).toProvider(provider);
}
return binder.bind(namedInterfaceKey).toProvider(provider);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment