(no commit message)
[utils] / test / enterprise / src / main / java / org / wamblee / test / inject / JavaEETestInjectorFactory.java
index b36dfbf8675243dc09cc3e4aa222a2b1c65b8bc8..f5814981064a369c26929b034732c976c233f13f 100644 (file)
  */
 package org.wamblee.test.inject;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
 
 import org.wamblee.inject.Injector;
+import org.wamblee.inject.InjectorBuilder;
 import org.wamblee.inject.InjectorFactory;
-import org.wamblee.inject.InjectorFactoryBuilder;
 import org.wamblee.test.persistence.JpaBuilder;
 
 /**
  * <p>
- * The test injector factory provides dependency injection of a contextual entity manager
- * using the support/inject mini framework. It supports dependency injection of fields
- * annoted with <code>&#064;PersistenceContext</code>. It only supports one persistence context
- * at the moment. This injector can be easily used together with {@link JpaBuilder#getContextualEntityManager()}
- * for obtaining an entity manager in unit test. 
+ * The test injector factory provides dependency injection of a contextual
+ * entity manager using the support/inject mini framework. It supports
+ * dependency injection of fields annoted with
+ * <code>&#064;PersistenceContext</code>. It only supports one persistence
+ * context at the moment. This injector can be easily used together with
+ * {@link JpaBuilder#getContextualEntityManager()} for obtaining an entity
+ * manager in unit test.
  * </p>
  * 
  * <p>
- * The reason it is needed is because standard injection mechanisms (such as weld CDI) do not support
- * entity manager injection in a Java SE environment out of the box.  
+ * The reason it is needed is because standard injection mechanisms (such as
+ * weld CDI) do not support entity manager injection in a Java SE environment
+ * out of the box.
  * </p>
- *
+ * 
  * <p>
- * To use it, construct the factory using one of the available constructors and set 
- * {@link InjectorFactoryBuilder#setInjectorFactory(InjectorFactory)).
+ * To use it, construct the factory using one of the available constructors and
+ * set <code>InjectorBuilder.setInjectorFactory(InjectorFactory)</code>.
  * </p>
  * 
  * @author Erik Brakkee
- *
+ * 
  */
 public class JavaEETestInjectorFactory implements InjectorFactory {
 
-    private EntityManager entityManager;
+    private List<Binding> bindings;
     private InjectorFactory delegate;
-    
+
     /**
-     * Constructs the factory. 
-     * @param aEntityManager Contextual entity manager to inject. 
-     * @param aInjectorFactory Injector factory to delegate to. 
+     * Constructs the factory.
+     * 
+     * @param aInjectorFactory
+     *            Injector factory to delegate to.
      */
-    public JavaEETestInjectorFactory(EntityManager aEntityManager, InjectorFactory aInjectorFactory) { 
-        entityManager = aEntityManager; 
+    public JavaEETestInjectorFactory(InjectorFactory aInjectorFactory) {
+        bindings = new ArrayList<Binding>();
         delegate = aInjectorFactory;
     }
 
     /**
-     * Constructs the factory with the default injector factory obtained from 
-     * {@link InjectorFactoryBuilder#getInjector()}. 
-     * @param aEntityManager Contextual entity manager to inject. 
+     * Adds default entity manager binding. Any field annotated with @PersistenceContext
+     * and of type entity manager will get injected.
+     * 
+     * @param aEntityManager
+     *            Entitymanager object to inject.
+     * @return Factory to allow chaining.
+     */
+    public JavaEETestInjectorFactory addEntityManagerBinding(
+        EntityManager aEntityManager) {
+        Binding em = new Binding(EntityManager.class, PersistenceContext.class,
+            aEntityManager);
+        addBinding(em);
+        return this;
+    }
+
+    /**
+     * Adds another custom injection binding.
+     * 
+     * @param aBinding
+     *            Injection binding to use.
+     * @return the factoryto allow chaining.
+     */
+    public JavaEETestInjectorFactory addBinding(Binding aBinding) {
+        bindings.add(aBinding);
+        return this;
+    }
+
+    /**
+     * Constructs the factory with the default injector factory obtained from
+     * {@link InjectorBuilder#getInjector()}.
      */
-    public JavaEETestInjectorFactory(EntityManager aEntityManager) {
-        this(aEntityManager,  getDefaultInjectorFactory());
+    public JavaEETestInjectorFactory() {
+        this(getDefaultInjectorFactory());
     }
 
     private static InjectorFactory getDefaultInjectorFactory() {
-        InjectorFactoryBuilder.setInjectorFactory(null);
-        return InjectorFactoryBuilder.getInjectorFactory();
+        InjectorBuilder.setInjectorFactory(null);
+        return InjectorBuilder.getInjectorFactory();
     }
 
     @Override
     public Injector create(Class aClass) {
-        return new JavaEETestInjector(aClass, entityManager, delegate
-            .create(aClass));
+        return new JavaEETestInjector(aClass, bindings, delegate.create(aClass));
     }
 }