2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.test.inject;
18 import java.util.ArrayList;
19 import java.util.List;
21 import javax.persistence.EntityManager;
22 import javax.persistence.PersistenceContext;
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;
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>@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.
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
47 * To use it, construct the factory using one of the available constructors and
48 * set <code>InjectorBuilder.setInjectorFactory(InjectorFactory)</code>.
51 * @author Erik Brakkee
54 public class JavaEETestInjectorFactory implements InjectorFactory {
56 private List<Binding> bindings;
57 private InjectorFactory delegate;
60 * Constructs the factory.
62 * @param aInjectorFactory
63 * Injector factory to delegate to.
65 public JavaEETestInjectorFactory(InjectorFactory aInjectorFactory) {
66 bindings = new ArrayList<Binding>();
67 delegate = aInjectorFactory;
71 * Adds default entity manager binding. Any field annotated with @PersistenceContext
72 * and of type entity manager will get injected.
74 * @param aEntityManager
75 * Entitymanager object to inject.
76 * @return Factory to allow chaining.
78 public JavaEETestInjectorFactory addEntityManagerBinding(
79 EntityManager aEntityManager) {
80 Binding em = new Binding(EntityManager.class, PersistenceContext.class,
87 * Adds another custom injection binding.
90 * Injection binding to use.
91 * @return the factoryto allow chaining.
93 public JavaEETestInjectorFactory addBinding(Binding aBinding) {
94 bindings.add(aBinding);
99 * Constructs the factory with the default injector factory obtained from
100 * {@link InjectorBuilder#getInjector()}.
102 public JavaEETestInjectorFactory() {
103 this(getDefaultInjectorFactory());
106 private static InjectorFactory getDefaultInjectorFactory() {
107 InjectorBuilder.setInjectorFactory(null);
108 return InjectorBuilder.getInjectorFactory();
112 public Injector create(Class aClass) {
113 return new JavaEETestInjector(aClass, bindings, delegate.create(aClass));