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.lang.reflect.Field;
19 import java.lang.reflect.Method;
20 import java.util.List;
24 import javax.persistence.EntityManager;
25 import javax.persistence.PersistenceContext;
27 import org.wamblee.inject.Injector;
28 import org.wamblee.reflection.Accessor;
29 import org.wamblee.reflection.AnnotationUtils;
30 import org.wamblee.reflection.ObjectTraversal;
31 import org.wamblee.reflection.ObjectTraversal.ObjectVisitor;
35 * Injector that performs additional injection on top of the injections that are
36 * standard available (e.g. entity manager in Java SE environment).
40 * It works by first delegating to the default injector (typically CDI). Afterwards it traverses the
41 * object graph of the injected object and performs custom injection of test objects as specified by the
42 * {@link Binding} class. This approach makes sure that test dependencies also find their way into
43 * objects that were created by the injection framework.
46 * @author Erik Brakkee
48 public class JavaEETestInjector implements Injector {
50 private class InjectionVisitor implements ObjectVisitor {
52 public boolean mustVisit(Class aClass) {
53 if (EntityManager.class.isAssignableFrom(aClass)) {
59 public boolean mustVisit(Field aField) {
60 // just process any field with annotations
61 return aField.getAnnotations().length > 0;
64 public boolean mustVisit(Method aMethod) {
68 public boolean visitArray(Object aArray) {
72 public boolean visitList(List aObject) {
76 public boolean visitMap(Map aObject) {
80 public boolean visitPlainObject(Object aObject) {
81 performTestInjections(aObject);
85 public boolean visitSet(Set aSet) {
90 private List<Binding> bindings;
91 private Injector delegate;
94 * Constructs the injector.
97 * Class to inject for.
99 * Binding of an object value.
101 * Injecto to delegate to to perform the standard injections.
103 public JavaEETestInjector(Class aClass, List<Binding> aBindings,
104 Injector aDelegate) {
105 bindings = aBindings;
106 delegate = aDelegate;
110 public void inject(Object aComponent) {
112 delegate.inject(aComponent);
114 // Now perform test injections.
115 ObjectTraversal traversal = new ObjectTraversal(new InjectionVisitor());
116 traversal.accept(aComponent);
119 private void performTestInjections(Object aComponent) {
120 for (Binding binding : bindings) {
121 binding.inject(aComponent);