Search in sources :

Example 1 with InstanceBinding

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

the class ModuleTestCase method assertMapInstanceBinding.

/**
     * Configures the module, and ensures a map exists between the "keyType" and "valueType",
     * and that all of the "expected" values are bound.
     */
@SuppressWarnings("unchecked")
public <K, V> void assertMapInstanceBinding(Module module, Class<K> keyType, Class<V> valueType, Map<K, V> expected) throws Exception {
    // this method is insane because java type erasure makes it incredibly difficult...
    Map<K, Key> keys = new HashMap<>();
    Map<Key, V> values = new HashMap<>();
    List<Element> elements = Elements.getElements(module);
    for (Element element : elements) {
        if (element instanceof InstanceBinding) {
            InstanceBinding binding = (InstanceBinding) element;
            if (binding.getKey().getRawType().equals(valueType)) {
                values.put(binding.getKey(), (V) binding.getInstance());
            } else if (binding.getInstance() instanceof Map.Entry) {
                Map.Entry entry = (Map.Entry) binding.getInstance();
                Object key = entry.getKey();
                Object providerValue = entry.getValue();
                if (key.getClass().equals(keyType) && providerValue instanceof ProviderLookup.ProviderImpl) {
                    ProviderLookup.ProviderImpl provider = (ProviderLookup.ProviderImpl) providerValue;
                    keys.put((K) key, provider.getKey());
                }
            }
        }
    }
    for (Map.Entry<K, V> entry : expected.entrySet()) {
        Key valueKey = keys.get(entry.getKey());
        assertNotNull("Could not find binding for key [" + entry.getKey() + "], found these keys:\n" + keys.keySet(), valueKey);
        V value = values.get(valueKey);
        assertNotNull("Could not find value for instance key [" + valueKey + "], found these bindings:\n" + elements);
        assertEquals(entry.getValue(), value);
    }
}
Also used : HashMap(java.util.HashMap) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) InstanceBinding(org.elasticsearch.common.inject.spi.InstanceBinding) ProviderLookup(org.elasticsearch.common.inject.spi.ProviderLookup) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with InstanceBinding

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

the class ModuleTestCase method assertInstanceBindingWithAnnotation.

/**
     * Like {@link #assertInstanceBinding(Module, Class, Predicate)}, but filters the
     * classes checked by the given annotation.
     */
public <T> void assertInstanceBindingWithAnnotation(Module module, Class<T> to, Predicate<T> tester, Class<? extends Annotation> annotation) {
    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())) {
                if (annotation == null || annotation.equals(binding.getKey().getAnnotationType())) {
                    assertTrue(tester.test(to.cast(binding.getInstance())));
                    return;
                }
            }
        } else if (element instanceof ProviderInstanceBinding) {
            ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
            if (to.equals(binding.getKey().getTypeLiteral().getType())) {
                assertTrue(tester.test(to.cast(binding.getProviderInstance().get())));
                return;
            }
        }
    }
    StringBuilder s = new StringBuilder();
    for (Element element : elements) {
        s.append(element + "\n");
    }
    fail("Did not find any instance binding to " + to.getName() + ". Found these bindings:\n" + s);
}
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 3 with InstanceBinding

use of org.elasticsearch.common.inject.spi.InstanceBinding 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)

Aggregations

Element (org.elasticsearch.common.inject.spi.Element)3 InstanceBinding (org.elasticsearch.common.inject.spi.InstanceBinding)3 ProviderInstanceBinding (org.elasticsearch.common.inject.spi.ProviderInstanceBinding)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ProviderLookup (org.elasticsearch.common.inject.spi.ProviderLookup)1