KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > webservice > test > AuthenticationServiceSystemTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.webservice.test;
18
19 import junit.framework.AssertionFailedError;
20 import junit.framework.TestCase;
21
22 import org.alfresco.webservice.authentication.AuthenticationFault;
23 import org.alfresco.webservice.authentication.AuthenticationResult;
24 import org.alfresco.webservice.authentication.AuthenticationServiceSoapBindingStub;
25 import org.alfresco.webservice.util.WebServiceFactory;
26
27 /**
28  * Tests the AuthenticationService by trying to login as admin/admin and
29  * attempting to login with incorrect credentials.
30  *
31  * @author gavinc
32  */

33 public class AuthenticationServiceSystemTest extends TestCase
34 {
35    /**
36     * Tests whether the authentication service is working correctly
37     *
38     * @throws Exception
39     */

40    public void testSuccessfulLogin() throws Exception JavaDoc
41    {
42       try
43       {
44          AuthenticationResult value = WebServiceFactory.getAuthenticationService().startSession("admin", "admin");
45          assertNotNull("result must not be null", value);
46          System.out.println("ticket = " + value.getTicket());
47       }
48       catch (AuthenticationFault error)
49       {
50          throw new AssertionFailedError("AuthenticationFault Exception caught: " + error);
51       }
52    }
53    
54    /**
55     * Tests that a failed authentication attempt fails as expected
56     *
57     * @throws Exception
58     */

59    public void testFailedLogin() throws Exception JavaDoc
60    {
61       try
62       {
63           WebServiceFactory.getAuthenticationService().startSession("wrong", "credentials");
64          fail("The credentials are incorrect so an AuthenticationFault should have been thrown");
65       }
66       catch (AuthenticationFault error)
67       {
68          // we expected this
69
}
70    }
71    
72    /**
73     * Tests endSession
74     *
75     * @throws Exception
76     */

77    public void testEndSession() throws Exception JavaDoc
78    {
79        AuthenticationServiceSoapBindingStub authenticationService = WebServiceFactory.getAuthenticationService();
80        
81        // Create and end a session
82
AuthenticationResult result = authenticationService.startSession("admin", "admin");
83        authenticationService.endSession(result.getTicket());
84        
85        try
86        {
87            // Try and end an invalid session
88
authenticationService.endSession("badSessionId");
89            fail("An exception should have been thrown since we are trying to end an invalid session");
90        }
91        catch (Throwable JavaDoc exception)
92        {
93            // Web are expecting this exception
94
}
95    }
96 }
97
Popular Tags