X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fcdi%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fcdi%2FWeldTest.java;fp=support%2Fcdi%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fcdi%2FWeldTest.java;h=c8608825d240a13c1b8538ac4a3e1629448c8af3;hb=cbb032a4e384ae54d9c7f5f42758622f53a17bb7;hp=0000000000000000000000000000000000000000;hpb=2a90c02cde9e8e31ecd4cf01baf91f798b262eb3;p=utils diff --git a/support/cdi/src/test/java/org/wamblee/cdi/WeldTest.java b/support/cdi/src/test/java/org/wamblee/cdi/WeldTest.java new file mode 100644 index 00000000..c8608825 --- /dev/null +++ b/support/cdi/src/test/java/org/wamblee/cdi/WeldTest.java @@ -0,0 +1,55 @@ +package org.wamblee.cdi; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.AnnotatedType; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.InjectionTarget; + +import org.jboss.weld.environment.se.Weld; +import org.jboss.weld.environment.se.WeldContainer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static junit.framework.TestCase.*; + +public class WeldTest { + + private Weld weld; + private WeldContainer container; + private BeanManager beanManager; + + @Before + public void setUp() { + weld = new Weld(); + container = weld.initialize(); + beanManager = container.getBeanManager(); + } + + @After + public void tearDown() { + weld.shutdown(); + } + + @Test + public void testGetSingleton() { + AnnotatedType type = beanManager.createAnnotatedType(MyPojo.class); + InjectionTarget target = beanManager.createInjectionTarget(type); + CreationalContext ctx = beanManager.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. + // assertSame(pojo2, pojo); + + assertEquals(1, MySingleton.getInstances()); + } +}