additions for testability.
authorErik Brakkee <erik@brakkee.org>
Wed, 21 Jul 2010 10:35:47 +0000 (10:35 +0000)
committerErik Brakkee <erik@brakkee.org>
Wed, 21 Jul 2010 10:35:47 +0000 (10:35 +0000)
support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java
support/inject/src/main/java/org/wamblee/inject/InjectorFactoryBuilder.java
support/inject/src/test/java/org/wamblee/inject/InjectorFactoryBuilderTest.java

index 8dd0986a63ac0d9762d927d1506ee5e077b557ac..944bd34aa3a028df0543626e86205f70bfb6fcbb 100644 (file)
@@ -46,7 +46,7 @@ public class PersistentFactory {
      */
     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);
@@ -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<? extends Annotation> aAnnotation) {
         List<Field> fields = ReflectionUtils.getAllFields(aClass);
         for (Field field : fields) {
index d8190919db6a0ca2f2e0163b541521b3edf55a04..13db8412d886a54474b808aa329ff69792aa7327 100644 (file)
@@ -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 <code>ServiceLoader</code>
+     * 
+     * @return InjectorFactory.
+     */
+    private static InjectorFactory findInjectorFactory() {
         ServiceLoader<InjectorFactory> factories = ServiceLoader
             .load(InjectorFactory.class);
         try {
index 1b1bf279469b6333b8e226ccdbf62e91a992de8f..f72045f72261feb2bedb927dca697b7998a49470 100644 (file)
@@ -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);  
+    }
 }