(no commit message)
authorErik Brakkee <erik@brakkee.org>
Sat, 31 Jul 2010 17:33:16 +0000 (17:33 +0000)
committerErik Brakkee <erik@brakkee.org>
Sat, 31 Jul 2010 17:33:16 +0000 (17:33 +0000)
support/inject/src/main/java/org/wamblee/inject/Injectable.java
support/inject/src/main/java/org/wamblee/inject/Injector.java
support/inject/src/main/java/org/wamblee/inject/InjectorBuilder.java
support/inject/src/main/java/org/wamblee/inject/InjectorCache.java
support/inject/src/main/java/org/wamblee/inject/InjectorFactory.java
support/inject/src/main/java/org/wamblee/inject/SimpleInjector.java
support/inject/src/test/java/org/wamblee/inject/InjectableTest.java
support/inject/src/test/java/org/wamblee/inject/InjectorBuilderTest.java
support/inject/src/test/java/org/wamblee/inject/InjectorCacheTest.java
support/inject/src/test/java/org/wamblee/inject/Pojo.java

index ccc408f7a0eca2e5a914ce3d7d80f205b4e7986e..7cfdc3d1b56945cbfc54fcfe6044f1b5207dd09c 100644 (file)
  */
 package org.wamblee.inject;
 
-
-
 /**
  * This abstract base class performs injection at construction. Be sure not to
  * initialize fields of derived classes to null as these will override the
  * initializations of this base class.
  * 
- * This class uses {@link InjectorBuilder} to obtain an implementation of 
- * a {@link InjectorFactory} to use. 
+ * This class uses {@link InjectorBuilder} to obtain an implementation of a
+ * {@link InjectorFactory} to use.
  * 
  * @author Erik Brakkee
  */
 public abstract class Injectable {
 
-    private final Injector injector = 
-        InjectorBuilder.getInjector();
+    private final Injector injector = InjectorBuilder.getInjector();
 
     /**
      * Inheritance style constructor.
@@ -39,19 +36,19 @@ public abstract class Injectable {
         inject();
     }
 
-
     /**
-     * Inject references. This can be useful when injection is to be done again after construction
-     * to obtain new references or after zeroing out of the references. 
+     * Inject references. This can be useful when injection is to be done again
+     * after construction to obtain new references or after zeroing out of the
+     * references.
      */
     public void inject() {
         injector.inject(this);
-    }    
-   
-    
+    }
+
     /**
      * Gets the default injector.
-     * @return Injector. 
+     * 
+     * @return Injector.
      */
     public Injector getInjector() {
         return injector;
index e42cc5f76343604bfd1e9a391fd5f615a16aa8bc..42d458f514681f39f58b7f9857d3e8c927c38682 100644 (file)
@@ -16,8 +16,8 @@
 package org.wamblee.inject;
 
 /**
- * Interface used to perform injection with. An injector instance knows how to perform injection in 
- * one or more specific classes. 
+ * Interface used to perform injection with. An injector instance knows how to
+ * perform injection in one or more specific classes.
  * 
  * @author Erik Brakkee
  */
@@ -28,8 +28,10 @@ public interface Injector {
      * 
      * @param aComponent
      *            Component to inject into.
-     * @throws IllegalArgumentException In case the argument passed is not supported for injection
-     * @throws NullPointerException In case the argument passed is null. 
+     * @throws IllegalArgumentException
+     *             In case the argument passed is not supported for injection
+     * @throws NullPointerException
+     *             In case the argument passed is null.
      */
     void inject(Object aComponent);
 
index fe6c2a93bf5422f7f76e295738b4c89e1015cf74..f0c0292b2c40d23cf306413c2ab26aae687f0426 100644 (file)
@@ -20,25 +20,28 @@ import java.util.ServiceLoader;
 
 /**
  * Utility for obtaining an implementation of the {@link InjectorFactory} using
- * {@link ServiceLoader} and for obtaining a {@link SimpleInjector}. 
+ * {@link ServiceLoader} and for obtaining a {@link SimpleInjector}.
  * 
- * The builder takes care that the factory and simple injector are built only once. 
- * For test code, make sure to call {@link #setInjectorFactory(InjectorFactory)}
- * before each test case to force the retrieval of a new factory and injector. This 
- * is important because if the simple injector is not created again it will use 
- * cached {@link Injector} instances from other tests. 
+ * The builder takes care that the factory and simple injector are built only
+ * once. For test code, make sure to call
+ * {@link #setInjectorFactory(InjectorFactory)} before each test case to force
+ * the retrieval of a new factory and injector. This is important because if the
+ * simple injector is not created again it will use cached {@link Injector}
+ * instances from other tests.
  * 
  * @author Erik Brakkee
  */
 public class InjectorBuilder {
 
     private static InjectorFactory FACTORY;
-    
-    private static SimpleInjector INJECTOR; 
+
+    private static SimpleInjector INJECTOR;
 
     /**
-     * Sets the injector factory. This is useful for testing. 
-     * @param aFactory Factory to use. 
+     * Sets the injector factory. This is useful for testing.
+     * 
+     * @param aFactory
+     *            Factory to use.
      */
     public static void setInjectorFactory(InjectorFactory aFactory) {
         FACTORY = aFactory;
@@ -53,18 +56,20 @@ public class InjectorBuilder {
      */
     public static InjectorFactory getInjectorFactory() {
         if (FACTORY == null) {
-            FACTORY = findInjectorFactory(); 
+            FACTORY = findInjectorFactory();
             INJECTOR = new SimpleInjector(FACTORY);
         }
         return FACTORY;
     }
-    
+
     /**
-     * Gets an injector that support injection into any type of object and 
-     * performs caching of the injector obtained from the {@link InjectorFactory}.
+     * Gets an injector that support injection into any type of object and
+     * performs caching of the injector obtained from the
+     * {@link InjectorFactory}.
+     * 
      * @return Injector.
      */
-    public static Injector getInjector() { 
+    public static Injector getInjector() {
         getInjectorFactory();
         return INJECTOR;
     }
index 3e272f5fb35bd753088adab041195624e5c9584a..63136847a7880f2c822a6a19e0ae826f3bcf3586 100644 (file)
@@ -33,7 +33,8 @@ public class InjectorCache {
     /**
      * Constructs an empty cache.
      * 
-     * @param aInjectorFactory Injector factory to create Injectors. 
+     * @param aInjectorFactory
+     *            Injector factory to create Injectors.
      */
     public InjectorCache(InjectorFactory aInjectorFactory) {
         injectorFactory = aInjectorFactory;
index d36720de8eca93ebc6853f5e861502be5677ea98..104c4e272ac8bdc7fc25a0e4de702d3a96e11911 100644 (file)
  */
 package org.wamblee.inject;
 
-
 /**
- * The injector factory is responsible for creating injectors for a given class. 
- * This must be implemneted to interface to specific dependency injection frameworks or for
- * testing. 
+ * The injector factory is responsible for creating injectors for a given class.
+ * This must be implemneted to interface to specific dependency injection
+ * frameworks or for testing.
  * 
- * Implementations of this class must have a default no-arg constructor to be usable by 
- * {@link InjectorBuilder}.
+ * Implementations of this class must have a default no-arg constructor to be
+ * usable by {@link InjectorBuilder}.
  * 
  * @author Erik Brakkee
  */
@@ -30,8 +29,10 @@ public interface InjectorFactory {
 
     /**
      * Creates an injector.
+     * 
      * @return Injector fot he given class.
-     * @throws IllegalArgumentException In case the given class cannot be used. 
+     * @throws IllegalArgumentException
+     *             In case the given class cannot be used.
      */
     Injector create(Class aClass);
 
index b41eb4c35f65cd6faa5f577bc031edd076b9d5df..449a65a33162bf97114510bbafd8879e5fcce8fc 100644 (file)
@@ -15,7 +15,6 @@
  */
 package org.wamblee.inject;
 
-
 /**
  * The main entry point for programmatic dependency injection. A different
  * {@link InjectorFactory} can be plugged in for testing.
@@ -23,6 +22,7 @@ package org.wamblee.inject;
  * <p>
  * Given the following class:
  * </p>
+ * 
  * <pre>
  *   class Pojo {
  *     &#064;EJB
@@ -47,13 +47,12 @@ package org.wamblee.inject;
  * The <code>SimpleInjector</code> should be cached. This is because the
  * <code>SimpleInjector</code> caches the {@link Injector} objects that it uses
  * internally for performance. This is done because creation of these internal
- * <code>Injector</code> objects may be costly. Caching the simple injector makes sure
- * that a class is not analysed again for annotations every time injection is
- * used.
+ * <code>Injector</code> objects may be costly. Caching the simple injector
+ * makes sure that a class is not analysed again for annotations every time
+ * injection is used.
  * 
  * For more advanced cases, the injector factory can also be directly
- * constructed instead of being obtained through the
- * {@link InjectorBuilder}.
+ * constructed instead of being obtained through the {@link InjectorBuilder}.
  * 
  * @author Erik Brakkee
  */
index 150d4ad31130aebfed1f6f75f8618b6f52e975a2..1eeb06478fa2b7f051bef401154736880e4ad2c2 100644 (file)
@@ -22,8 +22,8 @@ import org.junit.Test;
 public class InjectableTest {
 
     @Test
-    public void testInjectInDerivedClass() { 
-        Pojo pojo = new Pojo(); 
-        assertEquals(TestInjectorFactory.MAGIC_VALUE, pojo.getValue()); 
+    public void testInjectInDerivedClass() {
+        Pojo pojo = new Pojo();
+        assertEquals(TestInjectorFactory.MAGIC_VALUE, pojo.getValue());
     }
 }
index e1c10a805c82dcd76a049f180596df7967b6c3e2..95fbb7f94b3a6e9f0c2c7b60361efa82a2c76462 100644 (file)
@@ -17,29 +17,29 @@ package org.wamblee.inject;
 
 import org.junit.After;
 import org.junit.Test;
-import static junit.framework.TestCase.*; 
+import static junit.framework.TestCase.*;
 
-import static org.mockito.Mockito.*; 
+import static org.mockito.Mockito.*;
 
 public class InjectorBuilderTest {
-    
+
     @After
-    public void tearDown() { 
+    public void tearDown() {
         InjectorBuilder.setInjectorFactory(null);
     }
 
     @Test
-    public void testGetInjectorFactory() { 
-        InjectorFactory factory = InjectorBuilder.getInjectorFactory(); 
+    public void testGetInjectorFactory() {
+        InjectorFactory factory = InjectorBuilder.getInjectorFactory();
         assertTrue(factory instanceof TestInjectorFactory);
     }
-    
+
     @Test
-    public void testOVerrideInjectorFactory() { 
+    public void testOVerrideInjectorFactory() {
         InjectorFactory factory = mock(InjectorFactory.class);
-        
-        InjectorBuilder.setInjectorFactory(factory); 
-        InjectorFactory factory2 = InjectorBuilder.getInjectorFactory(); 
-        assertSame(factory, factory2);  
+
+        InjectorBuilder.setInjectorFactory(factory);
+        InjectorFactory factory2 = InjectorBuilder.getInjectorFactory();
+        assertSame(factory, factory2);
     }
 }
index 8a5b2dee30b0434946fc95e0f895f5c89eecd1db..bcf6287451e1122f9d37e48094be90243bf2335e 100644 (file)
@@ -20,43 +20,47 @@ import static junit.framework.Assert.*;
 import org.junit.Test;
 
 public class InjectorCacheTest {
-    
-    private static class MyInjector implements Injector { 
-        private String classname; 
-        
-        public MyInjector(String aClassname) { 
-            classname = aClassname; 
+
+    private static class MyInjector implements Injector {
+        private String classname;
+
+        public MyInjector(String aClassname) {
+            classname = aClassname;
         }
+
         @Override
         public void inject(Object aComponent) {
             throw new RuntimeException("not implemented");
         }
+
         public String getClassname() {
             return classname;
         }
     }
 
     @Test
-    public void testCache() { 
+    public void testCache() {
         InjectorFactory factory = new InjectorFactory() {
-            
+
             @Override
             public Injector create(Class aClass) {
                 return new MyInjector(aClass.getName());
             }
         };
-        InjectorCache cache = new InjectorCache(factory); 
+        InjectorCache cache = new InjectorCache(factory);
         // Unrealistic exmaple, but sufficient for test.
-      
+
         Injector injector = cache.getInjector(String.class);
-        assertEquals(String.class.getName(), ((MyInjector)injector).getClassname());
-        
-        Injector injector2 = cache.getInjector(String.class); 
+        assertEquals(String.class.getName(), ((MyInjector) injector)
+            .getClassname());
+
+        Injector injector2 = cache.getInjector(String.class);
         assertSame(injector, injector2);
-        
-        // verify we get another one for another class. 
+
+        // verify we get another one for another class.
         Injector injector3 = cache.getInjector(Long.class);
-        assertEquals(Long.class.getName(), ((MyInjector)injector3).getClassname());
-        
+        assertEquals(Long.class.getName(), ((MyInjector) injector3)
+            .getClassname());
+
     }
 }
index 38afd7bb4dca39ec7522f88928dc0338fd0f4b63..2a3c6a122af7eabd69c82cdb5855430741ccce8f 100644 (file)
@@ -17,12 +17,12 @@ package org.wamblee.inject;
 
 public class Pojo extends Injectable {
 
-    private String value; 
-    
+    private String value;
+
     public void setValue(String aValue) {
         value = aValue;
     }
-    
+
     public String getValue() {
         return value;
     }