(no commit message)
[utils] / test / enterprise / src / main / java / org / wamblee / test / inject / JavaEETestInjectorFactory.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.test.inject;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.persistence.EntityManager;
22 import javax.persistence.PersistenceContext;
23
24 import org.wamblee.inject.Injector;
25 import org.wamblee.inject.InjectorBuilder;
26 import org.wamblee.inject.InjectorFactory;
27 import org.wamblee.test.persistence.JpaBuilder;
28
29 /**
30  * <p>
31  * The test injector factory provides dependency injection of a contextual entity manager
32  * using the support/inject mini framework. It supports dependency injection of fields
33  * annoted with <code>&#064;PersistenceContext</code>. It only supports one persistence context
34  * at the moment. This injector can be easily used together with {@link JpaBuilder#getContextualEntityManager()}
35  * for obtaining an entity manager in unit test. 
36  * </p>
37  * 
38  * <p>
39  * The reason it is needed is because standard injection mechanisms (such as weld CDI) do not support
40  * entity manager injection in a Java SE environment out of the box.  
41  * </p>
42  *
43  * <p>
44  * To use it, construct the factory using one of the available constructors and set 
45  * <code>InjectorBuilder.setInjectorFactory(InjectorFactory)</code>.
46  * </p>
47  * 
48  * @author Erik Brakkee
49  *
50  */
51 public class JavaEETestInjectorFactory implements InjectorFactory {
52
53     private List<Binding> bindings;
54     private InjectorFactory delegate;
55     
56     /**
57      * Constructs the factory. 
58      * @param aInjectorFactory Injector factory to delegate to. 
59      */
60     public JavaEETestInjectorFactory(InjectorFactory aInjectorFactory) {
61         bindings = new ArrayList<Binding>();        
62         delegate = aInjectorFactory;
63     }
64
65     /**
66      * Adds default entity manager binding. Any field annotated with @PersistenceContext and of type 
67      * entity manager will get injected.
68      * @param aEntityManager Entitymanager object to inject.
69      * @return Factory to allow chaining.
70      */
71     public JavaEETestInjectorFactory addEntityManagerBinding(EntityManager aEntityManager) {
72         Binding em = new Binding(EntityManager.class, PersistenceContext.class, aEntityManager);
73         addBinding(em);
74         return this;
75     }
76     
77     /**
78      * Adds another custom injection binding. 
79      * @param aBinding Injection binding to use. 
80      * @return the factoryto allow chaining.
81      */
82     public JavaEETestInjectorFactory addBinding(Binding aBinding) { 
83         bindings.add(aBinding);
84         return this; 
85     }
86
87     /**
88      * Constructs the factory with the default injector factory obtained from 
89      * {@link InjectorBuilder#getInjector()}. 
90      */
91     public JavaEETestInjectorFactory() {
92         this(getDefaultInjectorFactory());
93     }
94
95     private static InjectorFactory getDefaultInjectorFactory() {
96         InjectorBuilder.setInjectorFactory(null);
97         return InjectorBuilder.getInjectorFactory();
98     }
99
100     @Override
101     public Injector create(Class aClass) {
102         return new JavaEETestInjector(aClass, bindings, delegate
103             .create(aClass));
104     }
105 }