/** * 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); }
/** * 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); }
/** * 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).append("\n"); } fail("Did not find any instance binding to " + to.getName() + ". Found these bindings:\n" + s); }
/** * 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); return null; // won't happen ;) }