Search in sources :

Example 6 with Element

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;
}
Also used : ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) InstanceBinding(org.elasticsearch.common.inject.spi.InstanceBinding) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding)

Example 7 with Element

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);
}
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 8 with Element

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

Example 9 with Element

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

Aggregations

Element (org.elasticsearch.common.inject.spi.Element)9 ProviderInstanceBinding (org.elasticsearch.common.inject.spi.ProviderInstanceBinding)5 InstanceBinding (org.elasticsearch.common.inject.spi.InstanceBinding)3 LinkedKeyBinding (org.elasticsearch.common.inject.spi.LinkedKeyBinding)3 Type (java.lang.reflect.Type)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Key (org.elasticsearch.common.inject.Key)1 PrivateBinder (org.elasticsearch.common.inject.PrivateBinder)1 Errors (org.elasticsearch.common.inject.internal.Errors)1 ProviderLookup (org.elasticsearch.common.inject.spi.ProviderLookup)1 UntargettedBinding (org.elasticsearch.common.inject.spi.UntargettedBinding)1