(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).
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.
45  * </p>
46  * 
47  * @author Erik Brakkee
48  */
49 public class JavaEETestInjector implements Injector {
50
51     private class InjectionVisitor implements ObjectVisitor {
52         @Override
53         public boolean mustVisit(Class aClass) {
54             if (EntityManager.class.isAssignableFrom(aClass)) {
55                 return false;
56             }
57             return true;
58         }
59
60         @Override
61         public boolean mustVisit(Field aField) {
62             // just process any field with annotations
63             return aField.getAnnotations().length > 0;
64         }
65
66         @Override
67         public boolean mustVisit(Method aMethod) {
68             return false;
69         }
70
71         @Override
72         public boolean visitArray(Object aArray) {
73             return true;
74         }
75
76         @Override
77         public boolean visitList(List aObject) {
78             return true;
79         }
80
81         @Override
82         public boolean visitMap(Map aObject) {
83             return true;
84         }
85
86         @Override
87         public boolean visitPlainObject(Object aObject) {
88             performTestInjections(aObject);
89             return true;
90         }
91
92         @Override
93         public boolean visitSet(Set aSet) {
94             return true;
95         }
96     }
97
98     private List<Binding> bindings;
99     private Injector delegate;
100
101     /**
102      * Constructs the injector.
103      * 
104      * @param aClass
105      *            Class to inject for.
106      * @param aBindings
107      *            Binding of an object value.
108      * @param aDelegate
109      *            Injecto to delegate to to perform the standard injections.
110      */
111     public JavaEETestInjector(Class aClass, List<Binding> aBindings,
112         Injector aDelegate) {
113         bindings = aBindings;
114         delegate = aDelegate;
115     }
116
117     @Override
118     public void inject(Object aComponent) {
119         // basic injection
120         delegate.inject(aComponent);
121
122         // Now perform test injections.
123         ObjectTraversal traversal = new ObjectTraversal(new InjectionVisitor());
124         traversal.accept(aComponent);
125     }
126
127     private void performTestInjections(Object aComponent) {
128         for (Binding binding : bindings) {
129             binding.inject(aComponent);
130         }
131     }
132
133 }