use of org.elasticsearch.common.inject.spi.Element in project elasticsearch by elastic.
the class ModuleTestCase method bindAndGetInstance.
/**
* Configures the module, and returns an instance bound to the "to" class.
*/
public static <T> T bindAndGetInstance(Module module, Class<T> to) {
List<Element> elements = Elements.getElements(module);
for (Element element : elements) {
if (element instanceof InstanceBinding) {
InstanceBinding binding = (InstanceBinding) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
return to.cast(binding.getInstance());
}
} else if (element instanceof ProviderInstanceBinding) {
ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
return to.cast(binding.getProviderInstance().get());
}
}
}
fail("can't get instance for class " + to);
// won't happen ;)
return null;
}
use of org.elasticsearch.common.inject.spi.Element in project elasticsearch by elastic.
the class ModuleTestCase method assertSetMultiBinding.
/**
* Configures the module and checks a Set of the "to" class
* is bound to "classes". There may be more classes bound
* to "to" than just "classes".
*/
public void assertSetMultiBinding(Module module, Class to, Class... classes) {
List<Element> elements = Elements.getElements(module);
Set<Type> bindings = new HashSet<>();
boolean providerFound = false;
for (Element element : elements) {
if (element instanceof LinkedKeyBinding) {
LinkedKeyBinding binding = (LinkedKeyBinding) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
bindings.add(binding.getLinkedKey().getTypeLiteral().getType());
}
} else if (element instanceof ProviderInstanceBinding) {
ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
String setType = binding.getKey().getTypeLiteral().getType().toString();
if (setType.equals("java.util.Set<" + to.getName() + ">")) {
providerFound = true;
}
}
}
for (Class clazz : classes) {
if (bindings.contains(clazz) == false) {
fail("Expected to find " + clazz.getName() + " as set binding to " + to.getName() + ", found these classes:\n" + bindings);
}
}
assertTrue("Did not find provider for set of " + to.getName(), providerFound);
}
use of org.elasticsearch.common.inject.spi.Element in project elasticsearch by elastic.
the class ModuleTestCase method assertBinding.
/** Configures the module and asserts "clazz" is bound to "to". */
public void assertBinding(Module module, Class to, Class clazz) {
List<Element> elements = Elements.getElements(module);
for (Element element : elements) {
if (element instanceof LinkedKeyBinding) {
LinkedKeyBinding binding = (LinkedKeyBinding) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
assertSame(clazz, binding.getLinkedKey().getTypeLiteral().getType());
return;
}
} else if (element instanceof UntargettedBinding) {
UntargettedBinding binding = (UntargettedBinding) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
assertSame(clazz, to);
return;
}
}
}
StringBuilder s = new StringBuilder();
for (Element element : elements) {
s.append(element + "\n");
}
fail("Did not find any binding to " + to.getName() + ". Found these bindings:\n" + s);
}
use of org.elasticsearch.common.inject.spi.Element in project elasticsearch by elastic.
the class ModuleTestCase method assertBindingFailure.
/**
* Attempts to configure the module, and asserts an {@link IllegalArgumentException} is
* caught, containing the given messages
*/
public void assertBindingFailure(Module module, String... msgs) {
try {
List<Element> elements = Elements.getElements(module);
StringBuilder s = new StringBuilder();
for (Element element : elements) {
s.append(element + "\n");
}
fail("Expected exception from configuring module. Found these bindings:\n" + s);
} catch (IllegalArgumentException e) {
for (String msg : msgs) {
assertTrue(e.getMessage() + " didn't contain: " + msg, e.getMessage().contains(msg));
}
}
}
Aggregations