+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
- <name>wamblee-support-cdi</name>
- <comment></comment>
- <projects>
- </projects>
- <buildSpec>
- <buildCommand>
- <name>org.eclipse.jdt.core.javabuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.maven.ide.eclipse.maven2Builder</name>
- <arguments>
- </arguments>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.eclipse.jdt.core.javanature</nature>
- <nature>org.maven.ide.eclipse.maven2Nature</nature>
- </natures>
-</projectDescription>
* In case no bean manager is found the beanmanager is set to null and the
* problem is logged.
*
- * This class does caching of the obtained reference to the bean manager.
- * As a result, it is necessary to call {@link #setBeanManager(BeanManager)}
- * in the setup phase of unit tests to make sure that the lookup is done again.
+ * This class does caching of the obtained reference to the bean manager. As a
+ * result, it is necessary to call {@link #setBeanManager(BeanManager)} in the
+ * setup phase of unit tests to make sure that the lookup is done again.
*
* @author Erik Brakkee
*/
*/
package org.wamblee.cdi;
-
public class BaseTestFixture {
@Before
- public void setUp() throws Exception {
+ public void setUp() throws Exception {
BeanManagerLookup.setBeanManager(null);
}
-
@After
- public void tearDown() throws Exception {
+ public void tearDown() throws Exception {
BeanManagerLookup.setBeanManager(null);
}
-
+
}
import org.wamblee.test.jndi.StubInitialContextFactory;
public class BeanManagerLookupTest extends BaseTestFixture {
-
+
@Test
public void testLookupInJndi() throws Exception {
BeanManagerSetup setup = new BeanManagerSetup();
setup.initialize();
StubInitialContextFactory.register();
- InitialContext ctx = new InitialContext();
+ InitialContext ctx = new InitialContext();
ctx.bind(BeanManagerLookup.BEAN_MANAGER_JNDI, setup.getBeanManager());
try {
assertSame(setup.getBeanManager(), BeanManagerLookup.lookup());
StubInitialContextFactory.unregister();
}
}
-
+
@Test
- public void testWithJndiButWithOverride() throws Exception {
+ public void testWithJndiButWithOverride() throws Exception {
BeanManagerSetup setup = new BeanManagerSetup();
setup.initialize();
StubInitialContextFactory.register();
- InitialContext ctx = new InitialContext();
+ InitialContext ctx = new InitialContext();
ctx.bind(BeanManagerLookup.BEAN_MANAGER_JNDI, setup.getBeanManager());
try {
BeanManager override = mock(BeanManager.class);
StubInitialContextFactory.unregister();
}
}
-
+
@Test
- public void testNoJndiButWithOverride() throws Exception {
+ public void testNoJndiButWithOverride() throws Exception {
BeanManager override = mock(BeanManager.class);
BeanManagerLookup.setBeanManager(override);
assertSame(override, BeanManagerLookup.lookup());
container = weld.initialize();
beanManager = container.getBeanManager();
}
-
+
public BeanManager getBeanManager() {
return beanManager;
}
public void shutdown() {
weld.shutdown();
weld = null;
- container = null;
- beanManager = null;
+ container = null;
+ beanManager = null;
}
}
@Test
public void testGetInjector() {
- InjectorFactory factory = new CdiInjectorFactory(manager.getBeanManager());
+ InjectorFactory factory = new CdiInjectorFactory(manager
+ .getBeanManager());
Injector injector = factory.create(MyPojo.class);
MyPojo pojo = new MyPojo();
injector.inject(pojo);
import org.wamblee.test.jndi.StubInitialContextFactory;
public class InjectorBuilderTest extends BaseTestFixture {
-
@Test
- public void testCdiInjectorFactoryIsFound() throws Exception {
+ public void testCdiInjectorFactoryIsFound() throws Exception {
BeanManagerSetup setup = new BeanManagerSetup();
setup.initialize();
StubInitialContextFactory.register();
- InitialContext ctx = new InitialContext();
+ InitialContext ctx = new InitialContext();
ctx.bind(BeanManagerLookup.BEAN_MANAGER_JNDI, setup.getBeanManager());
try {
InjectorFactory factory = InjectorBuilder.getInjectorFactory();
StubInitialContextFactory.unregister();
}
}
-
+
@Test
public void testInjectManyTimes() {
int n = 1000;
public class MyPojo {
@Inject
- private MySingleton singleton;
-
- public MyPojo() {
+ private MySingleton singleton;
+
+ public MyPojo() {
// Empty.
}
-
+
public MySingleton getSingleton() {
return singleton;
}
-
+
public void setSingleton(MySingleton aSingleton) {
singleton = aSingleton;
}
import javax.enterprise.context.ApplicationScoped;
-
@ApplicationScoped
public class MySingleton {
-
- private static int instances = 0;
- public MySingleton() {
- instances++;
+ private static int INSTANCES = 0;
+
+ public MySingleton() {
+ INSTANCES++;
}
-
- public static void reset() {
- instances = 0;
+
+ public static void reset() {
+ INSTANCES = 0;
}
-
+
public static int getInstances() {
- return instances;
+ return INSTANCES;
}
-
+
}
StubInitialContextFactory.register();
InitialContext ctx = new InitialContext();
ctx.bind(BeanManagerLookup.BEAN_MANAGER_JNDI, setup.getBeanManager());
- MySingleton.reset();
+ MySingleton.reset();
}
@After
@Test
public void testGetSingleton() {
MyPojo pojo = new MyPojo();
- SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory(BeanManagerLookup.lookup()));
+ SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory(
+ BeanManagerLookup.lookup()));
injector.inject(pojo);
MySingleton obj = pojo.getSingleton();
@Override
public void inject(Object aComponent) {
MyPojo pojo2 = (MyPojo) aComponent;
- pojo2.setSingleton(singleton);
+ pojo2.setSingleton(singleton);
}
});
-
+
injector.inject(pojo);
- // verify the custom injector was called.
+ // verify the custom injector was called.
assertSame(singleton, pojo.getSingleton());
}
-
+
@RequestScoped
- public static class Y {
-
+ public static class Y {
+
}
- public static class X {
+
+ public static class X {
@Inject
- private Y y;
-
+ private Y y;
+
}
-
- @Test
- public void testInjectStorage() throws Exception {
- X x = new X();
+
+ @Test
+ public void testInjectStorage() throws Exception {
+ X x = new X();
InjectorBuilder.setInjectorFactory(null);
InjectorBuilder.getInjector().inject(x);
}
-
- @Test
- public void testInjectStorage2() {
- X x = new X();
+
+ @Test
+ public void testInjectStorage2() {
+ X x = new X();
InjectorBuilder.setInjectorFactory(null);
InjectorBuilder.getInjector().inject(x);
}
-
+
}
import org.junit.Test;
public class WeldTest {
-
- private BeanManagerSetup setup;
-
+
+ private BeanManagerSetup setup;
+
@Before
public void setUp() {
- setup = new BeanManagerSetup();
- setup.initialize();
+ setup = new BeanManagerSetup();
+ setup.initialize();
MySingleton.reset();
}
-
+
@After
- public void tearDown() {
+ public void tearDown() {
setup.shutdown();
}
-
+
@Test
- public void testGetSingleton() {
- AnnotatedType<MyPojo> type = setup.getBeanManager().createAnnotatedType(MyPojo.class);
- InjectionTarget<MyPojo> target = setup.getBeanManager().createInjectionTarget(type);
- CreationalContext<MyPojo> ctx = setup.getBeanManager().createCreationalContext(null);
-
- MyPojo pojo = new MyPojo();
-
- target.inject(pojo, ctx);
-
- MySingleton obj = pojo.getSingleton();
-
+ public void testGetSingleton() {
+ AnnotatedType<MyPojo> type = setup.getBeanManager()
+ .createAnnotatedType(MyPojo.class);
+ InjectionTarget<MyPojo> target = setup.getBeanManager()
+ .createInjectionTarget(type);
+ CreationalContext<MyPojo> ctx = setup.getBeanManager()
+ .createCreationalContext(null);
+
+ MyPojo pojo = new MyPojo();
+
+ target.inject(pojo, ctx);
+
+ MySingleton obj = pojo.getSingleton();
+
assertNotNull(obj);
-
- MyPojo pojo2 = new MyPojo();
- target.inject(pojo2, ctx);
-
- // Objects will not be the same as they are contextual references to the same object.
+
+ MyPojo pojo2 = new MyPojo();
+ target.inject(pojo2, ctx);
+
+ // Objects will not be the same as they are contextual references to the
+ // same object.
// assertSame(pojo2, pojo);
assertEquals(1, MySingleton.getInstances());
}
-
+
@Test
- public void testAgain() {
+ public void testAgain() {
testGetSingleton();
}
}