+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package org.wamblee.test.inject;
+import static junit.framework.Assert.*;
+import static org.mockito.Mockito.*;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Level;
+
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
+import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
import org.wamblee.inject.InjectorBuilder;
-
-import static org.mockito.Mockito.*;
-import static junit.framework.TestCase.*;
+import org.wamblee.reflection.ObjectTraversal;
public class JavaEETestInjectorFactoryTest {
-
- @PersistenceContext
- private EntityManager entityManager;
-
- private EntityManager entityManager2;
-
- @PersistenceContext
- public EntityManager getEntityManager() {
- return entityManager2;
+
+ @Before
+ public void setUp() {
+ ObjectTraversal.LOGGER.setLevel(Level.FINEST);
+ ConsoleHandler handler = new ConsoleHandler();
+ handler.setLevel(Level.FINEST);
+ ObjectTraversal.LOGGER.addHandler(handler);
}
-
- public void setEntityManager(EntityManager aEm) {
- entityManager2 = aEm;
+
+ public static final class X {
+
+ @PersistenceContext
+ private EntityManager entityManager;
+
+ private EntityManager entityManager2;
+
+ @PersistenceContext
+ public EntityManager getEntityManager() {
+ return entityManager2;
+ }
+
+ public void setEntityManager(EntityManager aEm) {
+ entityManager2 = aEm;
+ }
}
@Test
- public void testXyz() {
+ public void testXyz() {
EntityManager em = mock(EntityManager.class);
- InjectorBuilder.setInjectorFactory(new JavaEETestInjectorFactory(em));
-
- assertNull(entityManager);
- assertNull(entityManager2);
- InjectorBuilder.getInjector().inject(this);
-
+ InjectorBuilder.setInjectorFactory(new JavaEETestInjectorFactory().addEntityManagerBinding(em));
+
+ X x = new X();
+
+ assertNull(x.entityManager);
+ assertNull(x.entityManager2);
+ InjectorBuilder.getInjector().inject(x);
+
assertNotNull(em);
- assertSame(em, entityManager);
- assertSame(em, entityManager2);
+ assertSame(em, x.entityManager);
+ assertSame(em, x.entityManager2);
+
}
-
- public static class X {
+
+ public static class Y {
@PersistenceContext
- Integer wrongType;
+ Integer wrongType;
}
-
- @Test(expected = IllegalArgumentException.class)
- public void testWrongType() {
+
+ @Test
+ public void testWrongType() {
EntityManager em = mock(EntityManager.class);
- InjectorBuilder.setInjectorFactory(new JavaEETestInjectorFactory(em));
- X x= new X();
- InjectorBuilder.getInjector().inject(x);
+ InjectorBuilder.setInjectorFactory(new JavaEETestInjectorFactory().addEntityManagerBinding(em));
+ Y y = new Y();
+ InjectorBuilder.getInjector().inject(y);
+ assertNull(y.wrongType);
+ }
+
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target( { ElementType.FIELD, ElementType.METHOD })
+ public static @interface MyAnnotation {
+
}
+
+ public static class Z {
+ @MyAnnotation
+ private int x;
+
+ // any annotation will force traversal.
+ @Mock
+ private Z z;
+
+ }
+
+ @Test
+ public void testCustomInjection() {
+ JavaEETestInjectorFactory factory = new JavaEETestInjectorFactory();
+ factory.addBinding(new Binding(int.class, MyAnnotation.class, 100));
+ InjectorBuilder.setInjectorFactory(factory);
+
+ Z z = new Z();
+ z.z = new Z();
+ assertEquals(0, z.x);
+ InjectorBuilder.getInjector().inject(z);
+ assertEquals(100, z.x);
+ assertEquals(100, z.z.x);
+ }
+
+
}