(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
32  * entity manager using the support/inject mini framework. It supports
33  * dependency injection of fields annoted with
34  * <code>&#064;PersistenceContext</code>. It only supports one persistence
35  * context at the moment. This injector can be easily used together with
36  * {@link JpaBuilder#getContextualEntityManager()} for obtaining an entity
37  * manager in unit test.
38  * </p>
39  * 
40  * <p>
41  * The reason it is needed is because standard injection mechanisms (such as
42  * weld CDI) do not support entity manager injection in a Java SE environment
43  * out of the box.
44  * </p>
45  * 
46  * <p>
47  * To use it, construct the factory using one of the available constructors and
48  * set <code>InjectorBuilder.setInjectorFactory(InjectorFactory)</code>.
49  * </p>
50  * 
51  * @author Erik Brakkee
52  * 
53  */
54 public class JavaEETestInjectorFactory implements InjectorFactory {
55
56     private List<Binding> bindings;
57     private InjectorFactory delegate;
58
59     /**
60      * Constructs the factory.
61      * 
62      * @param aInjectorFactory
63      *            Injector factory to delegate to.
64      */
65     public JavaEETestInjectorFactory(InjectorFactory aInjectorFactory) {
66         bindings = new ArrayList<Binding>();
67         delegate = aInjectorFactory;
68     }
69
70     /**
71      * Adds default entity manager binding. Any field annotated with @PersistenceContext
72      * and of type entity manager will get injected.
73      * 
74      * @param aEntityManager
75      *            Entitymanager object to inject.
76      * @return Factory to allow chaining.
77      */
78     public JavaEETestInjectorFactory addEntityManagerBinding(
79         EntityManager aEntityManager) {
80         Binding em = new Binding(EntityManager.class, PersistenceContext.class,
81             aEntityManager);
82         addBinding(em);
83         return this;
84     }
85
86     /**
87      * Adds another custom injection binding.
88      * 
89      * @param aBinding
90      *            Injection binding to use.
91      * @return the factoryto allow chaining.
92      */
93     public JavaEETestInjectorFactory addBinding(Binding aBinding) {
94         bindings.add(aBinding);
95         return this;
96     }
97
98     /**
99      * Constructs the factory with the default injector factory obtained from
100      * {@link InjectorBuilder#getInjector()}.
101      */
102     public JavaEETestInjectorFactory() {
103         this(getDefaultInjectorFactory());
104     }
105
106     private static InjectorFactory getDefaultInjectorFactory() {
107         InjectorBuilder.setInjectorFactory(null);
108         return InjectorBuilder.getInjectorFactory();
109     }
110
111     @Override
112     public Injector create(Class aClass) {
113         return new JavaEETestInjector(aClass, bindings, delegate.create(aClass));
114     }
115 }