Search in sources :

Example 1 with LinkedKeyBinding

use of org.elasticsearch.common.inject.spi.LinkedKeyBinding in project elasticsearch by elastic.

the class ModuleTestCase method assertMapMultiBinding.

/**
     * Configures the module and checks a Map<String, Class> of the "to" class
     * is bound to "theClass".
     */
public void assertMapMultiBinding(Module module, Class to, Class theClass) {
    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.Map<java.lang.String, " + to.getName() + ">")) {
                providerFound = true;
            }
        }
    }
    if (bindings.contains(theClass) == false) {
        fail("Expected to find " + theClass.getName() + " as binding to " + to.getName() + ", found these classes:\n" + bindings);
    }
    assertTrue("Did not find provider for map of " + to.getName(), providerFound);
}
Also used : Type(java.lang.reflect.Type) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) HashSet(java.util.HashSet) LinkedKeyBinding(org.elasticsearch.common.inject.spi.LinkedKeyBinding)

Example 2 with LinkedKeyBinding

use of org.elasticsearch.common.inject.spi.LinkedKeyBinding 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);
}
Also used : Type(java.lang.reflect.Type) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) HashSet(java.util.HashSet) LinkedKeyBinding(org.elasticsearch.common.inject.spi.LinkedKeyBinding)

Example 3 with LinkedKeyBinding

use of org.elasticsearch.common.inject.spi.LinkedKeyBinding 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);
}
Also used : UntargettedBinding(org.elasticsearch.common.inject.spi.UntargettedBinding) Element(org.elasticsearch.common.inject.spi.Element) LinkedKeyBinding(org.elasticsearch.common.inject.spi.LinkedKeyBinding)

Aggregations

Element (org.elasticsearch.common.inject.spi.Element)3 LinkedKeyBinding (org.elasticsearch.common.inject.spi.LinkedKeyBinding)3 Type (java.lang.reflect.Type)2 HashSet (java.util.HashSet)2 ProviderInstanceBinding (org.elasticsearch.common.inject.spi.ProviderInstanceBinding)2 UntargettedBinding (org.elasticsearch.common.inject.spi.UntargettedBinding)1