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 entity manager
32 * using the support/inject mini framework. It supports dependency injection of fields
33 * annoted with <code>@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.
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.
44 * To use it, construct the factory using one of the available constructors and set
45 * <code>InjectorBuilder.setInjectorFactory(InjectorFactory)</code>.
48 * @author Erik Brakkee
51 public class JavaEETestInjectorFactory implements InjectorFactory {
53 private List<Binding> bindings;
54 private InjectorFactory delegate;
57 * Constructs the factory.
58 * @param aInjectorFactory Injector factory to delegate to.
60 public JavaEETestInjectorFactory(InjectorFactory aInjectorFactory) {
61 bindings = new ArrayList<Binding>();
62 delegate = aInjectorFactory;
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.
71 public JavaEETestInjectorFactory addEntityManagerBinding(EntityManager aEntityManager) {
72 Binding em = new Binding(EntityManager.class, PersistenceContext.class, aEntityManager);
78 * Adds another custom injection binding.
79 * @param aBinding Injection binding to use.
80 * @return the factoryto allow chaining.
82 public JavaEETestInjectorFactory addBinding(Binding aBinding) {
83 bindings.add(aBinding);
88 * Constructs the factory with the default injector factory obtained from
89 * {@link InjectorBuilder#getInjector()}.
91 public JavaEETestInjectorFactory() {
92 this(getDefaultInjectorFactory());
95 private static InjectorFactory getDefaultInjectorFactory() {
96 InjectorBuilder.setInjectorFactory(null);
97 return InjectorBuilder.getInjectorFactory();
101 public Injector create(Class aClass) {
102 return new JavaEETestInjector(aClass, bindings, delegate