1+ /** Copyright Payara Services Limited **/
2+ package org .javaee7 .ejb .remote ;
3+
4+ import static org .javaee7 .ServerOperations .addUsersToContainerIdentityStore ;
5+ import static org .jboss .shrinkwrap .api .asset .EmptyAsset .INSTANCE ;
6+ import static org .junit .Assert .assertEquals ;
7+ import static org .junit .Assume .assumeTrue ;
8+
9+ import javax .naming .Context ;
10+ import javax .naming .NamingException ;
11+
12+ import org .javaee7 .RemoteEJBContextFactory ;
13+ import org .javaee7 .RemoteEJBContextProvider ;
14+ import org .javaee7 .ejb .remote .remote .Bean ;
15+ import org .javaee7 .ejb .remote .remote .BeanRemote ;
16+ import org .jboss .arquillian .container .test .api .Deployment ;
17+ import org .jboss .arquillian .container .test .api .RunAsClient ;
18+ import org .jboss .arquillian .junit .Arquillian ;
19+ import org .jboss .shrinkwrap .api .Archive ;
20+ import org .jboss .shrinkwrap .api .ShrinkWrap ;
21+ import org .jboss .shrinkwrap .api .spec .JavaArchive ;
22+ import org .junit .After ;
23+ import org .junit .Before ;
24+ import org .junit .Test ;
25+ import org .junit .runner .RunWith ;
26+
27+ /**
28+ * This class demonstrates and tests how to request an EJB bean from a remote server.
29+ *
30+ * <p>
31+ * {@link RemoteEJBContextProvider} is used, which is a test artifact abstracting the different
32+ * ways this is done for different servers.
33+ *
34+ * @author Arjan Tijms
35+ *
36+ */
37+ @ RunWith (Arquillian .class )
38+ public class RemoteBeanTest {
39+
40+ private RemoteEJBContextProvider remoteEJBContextProvider ;
41+
42+ @ Deployment
43+ public static Archive <?> deployment () {
44+
45+ // Add user u1 with password p1 and group g1 to the container's native identity store
46+ addUsersToContainerIdentityStore ();
47+
48+ return ShrinkWrap .create (JavaArchive .class )
49+ .addClasses (Bean .class , BeanRemote .class )
50+ .addAsManifestResource (INSTANCE , "beans.xml" );
51+ }
52+
53+ @ Before
54+ public void before () {
55+ remoteEJBContextProvider = RemoteEJBContextFactory .getProvider ();
56+ assumeTrue (
57+ "No RemoteEJBContextProvider available in current profile" ,
58+ remoteEJBContextProvider != null );
59+ }
60+
61+ @ After
62+ public void after () {
63+ remoteEJBContextProvider .releaseContext ();
64+ }
65+
66+ @ Test
67+ @ RunAsClient
68+ public void callProtectedRemoteBean () throws NamingException {
69+
70+ // Obtain the JNDI naming context in a vendor specific way.
71+ Context ejbRemoteContext = remoteEJBContextProvider .getContextWithCredentialsSet ("u1" , "p1" );
72+
73+ BeanRemote beanRemote = (BeanRemote ) ejbRemoteContext .lookup ("java:global/test/Bean" );
74+
75+ assertEquals ("method" , beanRemote .method ());
76+ }
77+
78+ }
0 commit comments