KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > jbws309 > JBWS309TestCase


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.jbws309;
23
24 import java.io.File JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26
27 import javax.naming.InitialContext JavaDoc;
28 import javax.xml.namespace.QName JavaDoc;
29 import javax.xml.rpc.Call JavaDoc;
30 import javax.xml.rpc.Service JavaDoc;
31 import javax.xml.rpc.ServiceFactory JavaDoc;
32 import javax.xml.rpc.Stub JavaDoc;
33
34 import junit.framework.Test;
35
36 import org.jboss.security.SecurityAssociation;
37 import org.jboss.security.SimplePrincipal;
38 import org.jboss.test.webservice.WebserviceTestBase;
39
40 /**
41  * Authorization Error using JBossWS together with JACC
42  *
43  * http://jira.jboss.org/jira/browse/JBWS-309
44  *
45  * This test should be run against the jacc configuration.
46  * It should also succedd with standard jboss authentication (no jacc) enabled.
47  *
48  * @author Thomas.Diesler@jboss.org
49  * @since 08-Jul-2005
50  */

51 public class JBWS309TestCase extends WebserviceTestBase
52 {
53    private static final String JavaDoc nsURI = "http://org.jboss.test.webservice/jbws309";
54    private static final String JavaDoc USERNAME = "kermit";
55    private static final String JavaDoc PASSWORD = "thefrog";
56
57    public JBWS309TestCase(String JavaDoc name)
58    {
59       super(name);
60    }
61
62    /** Deploy the test */
63    public static Test suite() throws Exception JavaDoc
64    {
65       return getDeploySetup(JBWS309TestCase.class, "ws4ee-jbws309.jar, ws4ee-jbws309-client.jar");
66    }
67
68    protected void setUp() throws Exception JavaDoc
69    {
70       super.setUp();
71       SecurityAssociation.setPrincipal(null);
72       SecurityAssociation.setCredential(null);
73    }
74
75    /** Test required principal/credential for this bean
76     */

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

102    public void testBasicSecuredSLSB() throws Exception JavaDoc
103    {
104       InitialContext JavaDoc iniCtx = getClientContext();
105       OrganizationHome home = (OrganizationHome)iniCtx.lookup("ejb/BasicSecuredSLSB");
106
107       OrganizationRemote bean = home.create();
108       String JavaDoc info = bean.getContactInfo("mafia");
109       assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
110    }
111
112    public void testBasicSecuredServiceAccess() throws Exception JavaDoc
113    {
114       InitialContext JavaDoc iniCtx = getClientContext();
115       Service JavaDoc service = (Service JavaDoc)iniCtx.lookup("java:comp/env/service/BasicSecured");
116       Organization endpoint = (Organization)service.getPort(new QName JavaDoc(nsURI, "BasicSecuredPort"), Organization.class);
117
118       try
119       {
120          endpoint.getContactInfo("mafia");
121          fail("Security exception expected");
122       }
123       catch (RemoteException JavaDoc ignore)
124       {
125          // ignore expected exception
126
}
127
128       Stub JavaDoc stub = (Stub JavaDoc)endpoint;
129       stub._setProperty(Stub.USERNAME_PROPERTY, USERNAME);
130       stub._setProperty(Stub.PASSWORD_PROPERTY, PASSWORD);
131
132       String JavaDoc info = endpoint.getContactInfo("mafia");
133       assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
134    }
135
136    /**
137     * DII client access a WSDL using basic auth
138     *
139     * http://jira.jboss.org/jira/browse/JBWS-483
140     */

141    public void testBasicSecuredDIIAccess() throws Exception JavaDoc
142    {
143       String JavaDoc targetAddress = "http://" + getServerHost() + ":8080/ws4ee-jbws309/BasicSecured";
144
145       File JavaDoc wsdlFile = new File JavaDoc("resources/webservice/jbws309/META-INF/wsdl/OrganizationService.wsdl");
146       assertTrue("wsdl file exists", wsdlFile.exists());
147
148       ServiceFactory JavaDoc factory = ServiceFactory.newInstance();
149       Service JavaDoc service = factory.createService(wsdlFile.toURL(), new QName JavaDoc(nsURI, "OrganizationService"));
150       Call JavaDoc call = service.createCall(new QName JavaDoc(nsURI, "BasicSecuredPort"), "getContactInfo");
151       call.setTargetEndpointAddress(targetAddress);
152
153       try
154       {
155          call.invoke(new Object JavaDoc[] { "mafia" });
156          fail("Security exception expected");
157       }
158       catch (RemoteException JavaDoc ignore)
159       {
160          // ignore expected exception
161
}
162
163       call.setProperty(Stub.USERNAME_PROPERTY, USERNAME);
164       call.setProperty(Stub.PASSWORD_PROPERTY, PASSWORD);
165
166       Object JavaDoc retObj = call.invoke(new Object JavaDoc[] { "mafia" });
167       assertEquals("The 'mafia' boss is currently out of office, please call again.", retObj);
168    }
169
170    public void testRoleSecuredServiceAccess() throws Exception JavaDoc
171    {
172       InitialContext JavaDoc iniCtx = getClientContext();
173       Service JavaDoc service = (Service JavaDoc)iniCtx.lookup("java:comp/env/service/RoleSecured");
174       Organization endpoint = (Organization)service.getPort(new QName JavaDoc(nsURI, "RoleSecuredPort"), Organization.class);
175
176       try
177       {
178          endpoint.getContactInfo("mafia");
179          fail("Security exception expected");
180       }
181       catch (RemoteException JavaDoc ignore)
182       {
183          // ignore expected exception
184
}
185
186       Stub JavaDoc stub = (Stub JavaDoc)endpoint;
187       stub._setProperty(Stub.USERNAME_PROPERTY, USERNAME);
188       stub._setProperty(Stub.PASSWORD_PROPERTY, PASSWORD);
189
190       String JavaDoc info = endpoint.getContactInfo("mafia");
191       assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
192    }
193 }
194
Popular Tags