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).
41 * Afterwards it traverses the object graph of the injected object and performs
42 * custom injection of test objects as specified by the {@link Binding} class.
43 * This approach makes sure that test dependencies also find their way into
44 * objects that were created by the injection framework.
47 * @author Erik Brakkee
49 public class JavaEETestInjector implements Injector {
51 private class InjectionVisitor implements ObjectVisitor {
53 public boolean mustVisit(Class aClass) {
54 if (EntityManager.class.isAssignableFrom(aClass)) {
61 public boolean mustVisit(Field aField) {
62 // just process any field with annotations
63 return aField.getAnnotations().length > 0;
67 public boolean mustVisit(Method aMethod) {
72 public boolean visitArray(Object aArray) {
77 public boolean visitList(List aObject) {
82 public boolean visitMap(Map aObject) {
87 public boolean visitPlainObject(Object aObject) {
88 performTestInjections(aObject);
93 public boolean visitSet(Set aSet) {
98 private List<Binding> bindings;
99 private Injector delegate;
102 * Constructs the injector.
105 * Class to inject for.
107 * Binding of an object value.
109 * Injecto to delegate to to perform the standard injections.
111 public JavaEETestInjector(Class aClass, List<Binding> aBindings,
112 Injector aDelegate) {
113 bindings = aBindings;
114 delegate = aDelegate;
118 public void inject(Object aComponent) {
120 delegate.inject(aComponent);
122 // Now perform test injections.
123 ObjectTraversal traversal = new ObjectTraversal(new InjectionVisitor());
124 traversal.accept(aComponent);
127 private void performTestInjections(Object aComponent) {
128 for (Binding binding : bindings) {
129 binding.inject(aComponent);