(no commit message)
[utils] / test / enterprise / src / main / java / org / wamblee / test / inject / JavaEETestInjector.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.lang.reflect.Field;
19 import java.lang.reflect.Method;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23
24 import javax.persistence.EntityManager;
25 import javax.persistence.PersistenceContext;
26
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;
32
33 /**
34  * <p>
35  * Injector that performs additional injection on top of the injections that are
36  * standard available (e.g. entity manager in Java SE environment).
37  * </p>
38  * 
39  * <p>
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. 
44  * </p>
45  * 
46  * @author Erik Brakkee
47  */
48 public class JavaEETestInjector implements Injector {
49
50     private class InjectionVisitor implements ObjectVisitor {
51         @Override
52         public boolean mustVisit(Class aClass) {
53             if (EntityManager.class.isAssignableFrom(aClass)) { 
54                 return false;
55             }
56             return true;
57         }
58         @Override
59         public boolean mustVisit(Field aField) {
60             // just process any field with annotations
61             return aField.getAnnotations().length > 0;  
62         }
63         @Override
64         public boolean mustVisit(Method aMethod) {
65             return false;
66         }
67         @Override
68         public boolean visitArray(Object aArray) {
69             return true;
70         }
71         @Override
72         public boolean visitList(List aObject) {
73             return true;
74         }
75         @Override
76         public boolean visitMap(Map aObject) {
77             return true;
78         }
79         @Override
80         public boolean visitPlainObject(Object aObject) {
81             performTestInjections(aObject);
82             return true;
83         }
84         @Override
85         public boolean visitSet(Set aSet) {
86             return true;
87         }
88     }
89
90     private List<Binding> bindings;
91     private Injector delegate;
92
93     /**
94      * Constructs the injector.
95      * 
96      * @param aClass
97      *            Class to inject for.
98      * @param aBindings
99      *            Binding of an object value.
100      * @param aDelegate
101      *            Injecto to delegate to to perform the standard injections.
102      */
103     public JavaEETestInjector(Class aClass, List<Binding> aBindings,
104         Injector aDelegate) {
105         bindings = aBindings;
106         delegate = aDelegate;
107     }
108
109     @Override
110     public void inject(Object aComponent) {
111         // basic injection
112         delegate.inject(aComponent);
113         
114         // Now perform test injections.
115         ObjectTraversal traversal = new ObjectTraversal(new InjectionVisitor());
116         traversal.accept(aComponent);
117     }
118
119     private void performTestInjections(Object aComponent) {
120         for (Binding binding : bindings) {
121             binding.inject(aComponent);
122         }
123     }
124
125 }