21
21
import org .jboss .weld .bootstrap .api .helpers .RegistrySingletonProvider ;
22
22
import org .jboss .weld .environment .se .Weld ;
23
23
import org .jboss .weld .environment .se .WeldContainer ;
24
+ import org .junit .AfterClass ;
24
25
import org .junit .BeforeClass ;
25
26
import org .junit .Test ;
26
- import org .springframework .mock .jndi .SimpleNamingContextBuilder ;
27
27
28
28
import jakarta .enterprise .inject .spi .InjectionTarget ;
29
+ import javax .naming .Context ;
30
+ import javax .naming .InitialContext ;
31
+ import javax .naming .NamingException ;
29
32
30
33
import static org .junit .Assert .assertNotNull ;
31
34
import static org .junit .Assert .assertSame ;
32
35
import static org .junit .Assert .assertTrue ;
33
36
34
37
public class CdiObjectFactoryTest {
35
38
39
+ private static final String SHARED_JNDI = "org.osjava.sj.jndi.shared" ;
40
+ private static InitialContext context ;
41
+ private static WeldContainer container ;
42
+
36
43
@ BeforeClass
37
44
public static void setup () throws Exception {
38
- Weld weld = new Weld ().containerId (RegistrySingletonProvider .STATIC_INSTANCE );
39
- WeldContainer container = weld .initialize ();
45
+ container = new Weld ().containerId (RegistrySingletonProvider .STATIC_INSTANCE ).initialize ();
46
+
47
+ System .setProperty (Context .INITIAL_CONTEXT_FACTORY , "org.osjava.sj.SimpleContextFactory" );
48
+ System .setProperty (SHARED_JNDI , "true" );
49
+ context = new InitialContext ();
50
+ context .bind (CdiObjectFactory .CDI_JNDIKEY_BEANMANAGER_COMP , container .getBeanManager ());
51
+ }
52
+
53
+ @ AfterClass
54
+ public static void tearDown () throws NamingException {
55
+ container .shutdown ();
40
56
41
- SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder ();
42
- builder .activate ();
43
- builder .bind (CdiObjectFactory .CDI_JNDIKEY_BEANMANAGER_COMP , container .getBeanManager ());
57
+ context .unbind (CdiObjectFactory .CDI_JNDIKEY_BEANMANAGER_COMP );
58
+ context .close ();
59
+ System .clearProperty (Context .INITIAL_CONTEXT_FACTORY );
60
+ System .clearProperty (SHARED_JNDI );
44
61
}
45
62
46
63
@ Test
@@ -50,16 +67,16 @@ public void testFindBeanManager() {
50
67
51
68
@ Test
52
69
public void testGetBean () throws Exception {
53
- final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory ();
54
- FooConsumer fooConsumer = (FooConsumer ) cdiObjectFactory .buildBean (FooConsumer .class .getCanonicalName (), null , false );
70
+ var cdiObjectFactory = new CdiObjectFactory ();
71
+ var fooConsumer = (FooConsumer ) cdiObjectFactory .buildBean (FooConsumer .class .getCanonicalName (), null , false );
55
72
assertNotNull (fooConsumer );
56
73
assertNotNull (fooConsumer .fooService );
57
74
}
58
75
59
76
@ Test
60
77
public void testGetInjectionTarget () {
61
- final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory ();
62
- final InjectionTarget <?> injectionTarget = cdiObjectFactory .getInjectionTarget (FooConsumer .class );
78
+ var cdiObjectFactory = new CdiObjectFactory ();
79
+ InjectionTarget <?> injectionTarget = cdiObjectFactory .getInjectionTarget (FooConsumer .class );
63
80
assertNotNull (injectionTarget );
64
81
assertTrue (cdiObjectFactory .injectionTargetCache .containsKey (FooConsumer .class ));
65
82
assertSame (cdiObjectFactory .getInjectionTarget (FooConsumer .class ), injectionTarget );
0 commit comments