KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > LoginTest


1 /**
2  * $RCSfile$
3  * $Revision: 2483 $
4  * $Date: 2005-04-14 00:24:04 -0300 (Thu, 14 Apr 2005) $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.smack;
13
14 import org.jivesoftware.smack.test.SmackTestCase;
15
16 /**
17  * Includes set of login tests.
18  *
19  * @author Gaston Dombiak
20  */

21 public class LoginTest extends SmackTestCase {
22
23     public LoginTest(String JavaDoc arg0) {
24         super(arg0);
25     }
26
27     /**
28      * Check that the server is returning the correct error when trying to login using an invalid
29      * (i.e. non-existent) user.
30      */

31     public void testInvalidLogin() {
32         try {
33             XMPPConnection connection = new XMPPConnection(getHost(), getPort());
34             try {
35                 // Login with an invalid user
36
connection.login("invaliduser" , "invalidpass");
37                 connection.close();
38                 fail("Invalid user was able to log into the server");
39             }
40             catch (XMPPException e) {
41                 assertEquals("Incorrect error code while login with an invalid user", 401,
42                         e.getXMPPError().getCode());
43             }
44             // Wait here while trying tests with exodus
45
//Thread.sleep(300);
46
}
47         catch (Exception JavaDoc e) {
48             e.printStackTrace();
49             fail(e.getMessage());
50         }
51     }
52
53     /**
54      * Check that the server handles anonymous users correctly.
55      */

56     public void testAnonymousLogin() {
57         try {
58             XMPPConnection conn1 = new XMPPConnection(getHost(), getPort());
59             XMPPConnection conn2 = new XMPPConnection(getHost(), getPort());
60             try {
61                 // Try to login anonymously
62
conn1.loginAnonymously();
63                 conn2.loginAnonymously();
64             }
65             catch (XMPPException e) {
66                 e.printStackTrace();
67                 fail(e.getMessage());
68             }
69             // Close the connection
70
conn1.close();
71             conn2.close();
72         }
73         catch (Exception JavaDoc e) {
74             e.printStackTrace();
75             fail(e.getMessage());
76         }
77     }
78
79     /**
80      * Check that the server does not allow to log in without specifying a resource.
81      */

82     public void testLoginWithNoResource() {
83         try {
84             XMPPConnection conn = new XMPPConnection(getHost(), getPort());
85             try {
86                 conn.getAccountManager().createAccount("user_1", "user_1");
87             } catch (XMPPException e) {
88                 // Do nothing if the accout already exists
89
if (e.getXMPPError().getCode() != 409) {
90                     throw e;
91                 }
92             }
93             conn.login("user_1", "user_1", null);
94             fail("User with no resource was able to log into the server");
95
96         } catch (XMPPException e) {
97             assertEquals("Wrong error code returned", 406, e.getXMPPError().getCode());
98         }
99     }
100
101     protected int getMaxConnections() {
102         return 0;
103     }
104 }
105
Popular Tags