KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > samples > ServerSideEJBSecTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.webservice.samples;
23
24 import junit.framework.Test;
25 import org.jboss.security.SecurityAssociation;
26 import org.jboss.security.SimplePrincipal;
27 import org.jboss.test.webservice.WebserviceTestBase;
28
29 import javax.naming.InitialContext JavaDoc;
30 import javax.xml.namespace.QName JavaDoc;
31 import javax.xml.rpc.Service JavaDoc;
32 import javax.xml.rpc.Stub JavaDoc;
33 import java.rmi.RemoteException JavaDoc;
34
35
36 /**
37  * A web service client that connects to a secured SLSB endpoint using.
38  *
39  * @author Thomas.Diesler@jboss.org
40  * @since 26-Apr-2004
41  */

42 public class ServerSideEJBSecTestCase extends WebserviceTestBase
43 {
44    public static final String JavaDoc USERNAME = "kermit";
45    public static final String JavaDoc PASSWORD = "thefrog";
46
47    public ServerSideEJBSecTestCase(String JavaDoc name)
48    {
49       super(name);
50    }
51
52    /** Deploy the test */
53    public static Test suite() throws Exception JavaDoc
54    {
55       return getDeploySetup(ServerSideEJBSecTestCase.class, "ws4ee-samples-server-ejb-sec.jar, ws4ee-samples-server-ejb-sec-client.jar");
56    }
57
58    protected void setUp() throws Exception JavaDoc
59    {
60       super.setUp();
61       SecurityAssociation.setPrincipal(null);
62       SecurityAssociation.setCredential(null);
63    }
64
65    /** Test required principal/credential for this bean
66     */

67    public void testRoleSecuredSLSB() throws Exception JavaDoc
68    {
69       InitialContext JavaDoc iniCtx = getClientContext();
70       OrganizationHome home = (OrganizationHome)iniCtx.lookup("ejb/RoleSecuredSLSB");
71
72       OrganizationRemote bean = null;
73       try
74       {
75          bean = home.create();
76          fail("Security exception expected");
77       }
78       catch (Exception JavaDoc e)
79       {
80          // all cool, now try again with valid credentials
81
SecurityAssociation.setPrincipal(new SimplePrincipal(USERNAME));
82          SecurityAssociation.setCredential(PASSWORD);
83          bean = home.create();
84       }
85
86       String JavaDoc info = bean.getContactInfo("mafia");
87       assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
88    }
89
90    /** Test that the remote access to this bean is unchecked
91     */

92    public void testBasicSecuredSLSB() throws Exception JavaDoc
93    {
94       InitialContext JavaDoc iniCtx = getClientContext();
95       OrganizationHome home = (OrganizationHome)iniCtx.lookup("ejb/BasicSecuredSLSB");
96
97       OrganizationRemote bean = home.create();
98       String JavaDoc info = bean.getContactInfo("mafia");
99       assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
100    }
101
102    public void testBasicSecuredServiceAccess() throws Exception JavaDoc
103    {
104       InitialContext JavaDoc iniCtx = getClientContext();
105       Service JavaDoc service = (Service JavaDoc)iniCtx.lookup("java:comp/env/service/BasicSecured");
106       QName JavaDoc portName = new QName JavaDoc("http://org.jboss.test.webservice/samples", "BasicSecuredPort");
107       Organization endpoint = (Organization)service.getPort(portName, Organization.class);
108
109       try
110       {
111          endpoint.getContactInfo("mafia");
112          fail("Security exception expected");
113       }
114       catch (RemoteException JavaDoc ignore)
115       {
116          // ignore expected exception
117
}
118
119       Stub JavaDoc stub = (Stub JavaDoc)endpoint;
120       stub._setProperty(Stub.USERNAME_PROPERTY, USERNAME);
121       stub._setProperty(Stub.PASSWORD_PROPERTY, PASSWORD);
122
123       String JavaDoc info = endpoint.getContactInfo("mafia");
124       assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
125    }
126
127    public void testRoleSecuredServiceAccess() throws Exception JavaDoc
128    {
129       InitialContext JavaDoc iniCtx = getClientContext();
130       Service JavaDoc service = (Service JavaDoc)iniCtx.lookup("java:comp/env/service/RoleSecured");
131       QName JavaDoc portName = new QName JavaDoc("http://org.jboss.test.webservice/samples", "RoleSecuredPort");
132       Organization endpoint = (Organization)service.getPort(portName, Organization.class);
133
134       try
135       {
136          endpoint.getContactInfo("mafia");
137          fail("Security exception expected");
138       }
139       catch (RemoteException JavaDoc ignore)
140       {
141          // ignore expected exception
142
}
143
144       Stub JavaDoc stub = (Stub JavaDoc)endpoint;
145       stub._setProperty(Stub.USERNAME_PROPERTY, USERNAME);
146       stub._setProperty(Stub.PASSWORD_PROPERTY, PASSWORD);
147
148       String JavaDoc info = endpoint.getContactInfo("mafia");
149       assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
150    }
151 }
152
Popular Tags