*/
private static Map<String, EntityAccessor> CACHE = new ConcurrentHashMap<String, EntityAccessor>();
- static interface Accessor<T> {
+ public static interface Accessor<T> {
void set(Object aEntity, T aValue);
T get(Object aEntity);
* Annotation that must be present.
* @return Accessor to use or null if the annotation is not present.
*/
- private static Accessor analyse(Class aClass,
+ // TODO move generic analysis part to the reflection package.
+ public static Accessor analyse(Class aClass,
Class<? extends Annotation> aAnnotation) {
List<Field> fields = ReflectionUtils.getAllFields(aClass);
for (Field field : fields) {
*/
public class InjectorFactoryBuilder {
+ private static InjectorFactory FACTORY;
+
+ /**
+ * Sets the injector factory. This is useful for testing.
+ * @param aFactory Factory to use.
+ */
+ public static void setInjectorFactory(InjectorFactory aFactory) {
+ FACTORY = aFactory;
+ }
+
/**
- * Gets the injector factory by using the first one found using
- * {@link ServiceLoader}.
+ * Gets the injector factory by using the first one found using
+ * {@link ServiceLoader}.
*
- * @return InjectorFactory.
+ * @return InjectorFactory.
*/
public static InjectorFactory getInjectorFactory() {
+ if (FACTORY == null) {
+ FACTORY = findInjectorFactory();
+ }
+ return FACTORY;
+ }
+
+ /**
+ * Finds the injector factory musing <code>ServiceLoader</code>
+ *
+ * @return InjectorFactory.
+ */
+ private static InjectorFactory findInjectorFactory() {
ServiceLoader<InjectorFactory> factories = ServiceLoader
.load(InjectorFactory.class);
try {
import org.junit.Test;
import static junit.framework.TestCase.*;
+import static org.mockito.Mockito.*;
+
public class InjectorFactoryBuilderTest {
@Test
InjectorFactory factory = InjectorFactoryBuilder.getInjectorFactory();
assertTrue(factory instanceof TestInjectorFactory);
}
+
+ @Test
+ public void testOVerrideInjectorFactory() {
+ InjectorFactory factory = mock(InjectorFactory.class);
+
+ InjectorFactoryBuilder.setInjectorFactory(factory);
+ InjectorFactory factory2 = InjectorFactoryBuilder.getInjectorFactory();
+ assertSame(factory, factory2);
+ }
}