KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > chatserver > test > ServerTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package echo2example.chatserver.test;
31
32 import echo2example.chatserver.Log;
33 import echo2example.chatserver.Message;
34 import echo2example.chatserver.Server;
35 import junit.framework.TestCase;
36
37 /**
38  * Unit tests for the chat server.
39  */

40 public class ServerTest extends TestCase {
41    
42     private static final String JavaDoc REMOTE_HOST = "192.168.0.20";
43     
44     public void testAuthenticationAndPosting() {
45         Log.setLogStream(null);
46         Server server = new Server();
47         String JavaDoc bobAuthToken = server.addUser("Bob.Smith", REMOTE_HOST);
48         assertNotNull(bobAuthToken);
49         String JavaDoc sallyAuthToken = server.addUser("Sally.Jones", REMOTE_HOST);
50         assertNotNull(sallyAuthToken);
51         assertNull(server.addUser("Bob.Smith", REMOTE_HOST));
52         assertTrue(server.postMessage("Bob.Smith", bobAuthToken, REMOTE_HOST, "Hi, everyone!"));
53         assertFalse(server.postMessage("Bob.Smith", sallyAuthToken, REMOTE_HOST, "Hi, everyone!"));
54         assertFalse(server.postMessage("Bob.Smith", null, REMOTE_HOST, "Hi, everyone!"));
55         assertFalse(server.postMessage("Bob.Smith", bobAuthToken + "x", REMOTE_HOST, "Hi, everyone!"));
56         assertTrue(server.postMessage("Sally.Jones", sallyAuthToken, REMOTE_HOST, "Hi, Bob!"));
57     }
58     
59     public void testMessageRetrieval()
60     throws InterruptedException JavaDoc {
61         Server server = new Server();
62         Message[] messages;
63         long lastRetrievedId;
64
65         // Retrieve recent messages.
66
messages = server.getRecentMessages();
67         assertEquals(0, messages.length);
68         
69         String JavaDoc bobAuthToken = server.addUser("Bob.Smith", REMOTE_HOST);
70
71         // Retrieve authentication message.
72
messages = server.getRecentMessages();
73         assertEquals(1, messages.length);
74         
75         // Post ten messages.
76
for (int i = 0; i < 10; ++i) {
77             server.postMessage("Bob.Smith", bobAuthToken, REMOTE_HOST, Integer.toString(i));
78         }
79         
80         // Retrieve all messages.
81
messages = server.getMessages(-1);
82         assertEquals(11, messages.length);
83         assertEquals("0", messages[1].getContent());
84         assertEquals("1", messages[2].getContent());
85         assertEquals("9", messages[10].getContent());
86         
87         lastRetrievedId = messages[10].getId();
88         
89         // Retrieve recent messages.
90
messages = server.getRecentMessages();
91         assertEquals(11, messages.length);
92         assertEquals("0", messages[1].getContent());
93         assertEquals("1", messages[2].getContent());
94         assertEquals("9", messages[10].getContent());
95         
96         // Post ten messages.
97
for (int i = 10; i < 20; ++i) {
98             server.postMessage("Bob.Smith", bobAuthToken, REMOTE_HOST, Integer.toString(i));
99         }
100         
101         // Retrieve messages.
102
messages = server.getMessages(lastRetrievedId);
103         assertEquals(10, messages.length);
104         assertEquals("10", messages[0].getContent());
105         assertEquals("11", messages[1].getContent());
106         assertEquals("19", messages[9].getContent());
107
108         lastRetrievedId = messages[9].getId();
109
110         // Post one thousand messages.
111
for (int i = 20; i < 1020; ++i) {
112             server.postMessage("Bob.Smith", bobAuthToken, REMOTE_HOST, Integer.toString(i));
113         }
114         
115         // Retrieve messages.
116
messages = server.getMessages(lastRetrievedId);
117         assertEquals(1000, messages.length);
118         assertEquals("20", messages[0].getContent());
119         assertEquals("21", messages[1].getContent());
120         assertEquals("520", messages[500].getContent());
121         assertEquals("1018", messages[998].getContent());
122         assertEquals("1019", messages[999].getContent());
123         
124         // Retrieve messages from a different starting point.
125
messages = server.getMessages(messages[500].getId());
126         assertEquals(499, messages.length);
127         assertEquals("521", messages[0].getContent());
128         assertEquals("1019", messages[498].getContent());
129         
130         // Retrieve recent messages.
131
messages = server.getRecentMessages();
132         assertEquals(15, messages.length);
133         assertEquals("1005", messages[0].getContent());
134         assertEquals("1006", messages[1].getContent());
135         assertEquals("1019", messages[14].getContent());
136         
137         lastRetrievedId = messages[14].getId();
138         
139         // Post message with UTF-8 characters.
140
server.postMessage("Bob.Smith", bobAuthToken, REMOTE_HOST, "\u0416\u066f");
141         messages = server.getMessages(lastRetrievedId);
142         assertEquals("\u0416\u066f", messages[0].getContent());
143     }
144 }
145
Popular Tags