(no commit message)
[utils] / test / enterprise / src / test / java / org / wamblee / test / inject / JavaEETestInjectorFactoryTest.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 static junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
20
21 import java.lang.annotation.ElementType;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.lang.annotation.Target;
25 import java.util.logging.ConsoleHandler;
26 import java.util.logging.Level;
27
28 import javax.persistence.EntityManager;
29 import javax.persistence.PersistenceContext;
30
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.wamblee.inject.InjectorBuilder;
35 import org.wamblee.reflection.ObjectTraversal;
36
37 public class JavaEETestInjectorFactoryTest {
38
39     @Before
40     public void setUp() {
41         ObjectTraversal.LOGGER.setLevel(Level.FINEST);
42         ConsoleHandler handler = new ConsoleHandler();
43         handler.setLevel(Level.FINEST);
44         ObjectTraversal.LOGGER.addHandler(handler);
45     }
46
47     public static final class X {
48
49         @PersistenceContext
50         private EntityManager entityManager;
51
52         private EntityManager entityManager2;
53
54         @PersistenceContext
55         public EntityManager getEntityManager() {
56             return entityManager2;
57         }
58
59         public void setEntityManager(EntityManager aEm) {
60             entityManager2 = aEm;
61         }
62     }
63
64     @Test
65     public void testXyz() {
66         EntityManager em = mock(EntityManager.class);
67         InjectorBuilder.setInjectorFactory(new JavaEETestInjectorFactory().addEntityManagerBinding(em));
68
69         X x = new X();
70
71         assertNull(x.entityManager);
72         assertNull(x.entityManager2);
73         InjectorBuilder.getInjector().inject(x);
74
75         assertNotNull(em);
76         assertSame(em, x.entityManager);
77         assertSame(em, x.entityManager2);
78
79     }
80
81     public static class Y {
82         @PersistenceContext
83         Integer wrongType;
84     }
85
86     @Test
87     public void testWrongType() {
88         EntityManager em = mock(EntityManager.class);
89         InjectorBuilder.setInjectorFactory(new JavaEETestInjectorFactory().addEntityManagerBinding(em));
90         Y y = new Y();
91         InjectorBuilder.getInjector().inject(y);
92         assertNull(y.wrongType);
93     }
94
95     @Retention(RetentionPolicy.RUNTIME)
96     @Target( { ElementType.FIELD, ElementType.METHOD })
97     public static @interface MyAnnotation {
98
99     }
100     
101     public static class Z {
102         @MyAnnotation
103         private int x;
104         
105         // any annotation will force traversal.
106         @Mock
107         private Z z; 
108
109     }
110
111     @Test
112     public void testCustomInjection() {
113         JavaEETestInjectorFactory factory = new JavaEETestInjectorFactory();
114         factory.addBinding(new Binding(int.class, MyAnnotation.class, 100));
115         InjectorBuilder.setInjectorFactory(factory);
116
117         Z z = new Z();
118         z.z = new Z();
119         assertEquals(0, z.x);
120         InjectorBuilder.getInjector().inject(z);
121         assertEquals(100, z.x);
122         assertEquals(100, z.z.x);
123     }
124     
125     
126 }