From 7723e6b4dee1d423bf6e52fdbf58f89c18ce5720 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Wed, 21 Jul 2010 10:35:47 +0000 Subject: [PATCH] additions for testability. --- .../persistence/PersistentFactory.java | 5 ++-- .../inject/InjectorFactoryBuilder.java | 28 +++++++++++++++++-- .../inject/InjectorFactoryBuilderTest.java | 11 ++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java b/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java index 8dd0986a..944bd34a 100644 --- a/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java +++ b/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java @@ -46,7 +46,7 @@ public class PersistentFactory { */ private static Map CACHE = new ConcurrentHashMap(); - static interface Accessor { + public static interface Accessor { void set(Object aEntity, T aValue); T get(Object aEntity); @@ -224,7 +224,8 @@ public class PersistentFactory { * 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 aAnnotation) { List fields = ReflectionUtils.getAllFields(aClass); for (Field field : fields) { diff --git a/support/inject/src/main/java/org/wamblee/inject/InjectorFactoryBuilder.java b/support/inject/src/main/java/org/wamblee/inject/InjectorFactoryBuilder.java index d8190919..13db8412 100644 --- a/support/inject/src/main/java/org/wamblee/inject/InjectorFactoryBuilder.java +++ b/support/inject/src/main/java/org/wamblee/inject/InjectorFactoryBuilder.java @@ -26,13 +26,35 @@ import java.util.ServiceLoader; */ 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 ServiceLoader + * + * @return InjectorFactory. + */ + private static InjectorFactory findInjectorFactory() { ServiceLoader factories = ServiceLoader .load(InjectorFactory.class); try { diff --git a/support/inject/src/test/java/org/wamblee/inject/InjectorFactoryBuilderTest.java b/support/inject/src/test/java/org/wamblee/inject/InjectorFactoryBuilderTest.java index 1b1bf279..f72045f7 100644 --- a/support/inject/src/test/java/org/wamblee/inject/InjectorFactoryBuilderTest.java +++ b/support/inject/src/test/java/org/wamblee/inject/InjectorFactoryBuilderTest.java @@ -18,6 +18,8 @@ package org.wamblee.inject; import org.junit.Test; import static junit.framework.TestCase.*; +import static org.mockito.Mockito.*; + public class InjectorFactoryBuilderTest { @Test @@ -25,4 +27,13 @@ public class InjectorFactoryBuilderTest { 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); + } } -- 2.31.1