KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > org > quickserver > net > server > SimpleServerBlockTest


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package test.org.quickserver.net.server;
16
17 import org.quickserver.net.server.*;
18 import org.quickserver.net.client.*;
19 import junit.framework.TestCase;
20 import junit.framework.AssertionFailedError;
21 import java.util.logging.*;
22 import org.quickserver.util.xmlreader.QuickServerConfig;
23
24 /**
25  * JUnit test cases for QuickServer
26  */

27
28 public class SimpleServerBlockTest extends TestCase {
29     private QuickServer server;
30     private ClientService client;
31     private String JavaDoc host = "127.0.0.1";
32     private int port = 54321;
33     private QuickServerConfig config;
34
35     public boolean getServerMode() {
36         return false;
37     }
38
39     public String JavaDoc getServerName() {
40         return "SimpleServerBlockTest";
41     }
42
43     public SimpleServerBlockTest(String JavaDoc name) {
44         super(name);
45     }
46
47     private void configServer() {
48         config = new QuickServerConfig();
49         config.setName(getServerName());
50         config.setClientEventHandler("test.org.quickserver.net.server.TestEventHandler");
51         config.setClientCommandHandler("test.org.quickserver.net.server.TestCommandHandler");
52         config.getServerMode().setBlocking(getServerMode());
53         config.setPort(port);
54         config.setBindAddr(host);
55     }
56
57     public void setUp() {
58         server = new QuickServer();
59         client = new BlockingClient();
60         configServer();
61
62         server.initService(config);
63
64         TestEventHandler.reset();
65         TestCommandHandler.reset();
66         
67         try {
68             server.startServer();
69         } catch(Exception JavaDoc e) {
70             server = null;
71             fail("Server could not start: "+e);
72         }
73     }
74
75     public void tearDown() {
76         try {
77             if(server!=null) server.stopServer();
78         } catch(Exception JavaDoc e) {
79             fail("Server could not stop: "+e);
80         }
81         //server = null;
82
}
83
84     public static void main(String JavaDoc args[]) {
85         junit.textui.TestRunner.run(SimpleServerBlockTest.class);
86     }
87
88    
89     public void testQuickServerBasic() {
90         try {
91             assertEquals(0, TestEventHandler.getGotConnectedFlag());
92             assertEquals(0, TestEventHandler.getClosingConnectionFlag());
93             assertEquals(0, TestEventHandler.getLostConnectionFlag());
94             assertEquals(0, TestCommandHandler.getHandleCommandFlag());
95
96             client.connect(host, port);
97             sleep(50);
98             assertEquals(1, TestEventHandler.getGotConnectedFlag());
99             System.out.println("GotConnected Pass");
100
101             client.sendString("test1");
102             sleep(50);
103             assertEquals(1, TestCommandHandler.getHandleCommandFlag());
104             assertEquals("test1", TestCommandHandler.getRequest());
105             System.out.println("HandleCommand 1 Pass");
106
107             TestCommandHandler.setResponse("junit");
108             client.sendString("test2");
109             sleep(50);
110             assertEquals(2, TestCommandHandler.getHandleCommandFlag());
111             assertEquals("test2", TestCommandHandler.getRequest());
112             assertEquals("junit", client.readString());
113             System.out.println("HandleCommand 2 Pass");
114
115             TestCommandHandler.setResponse("quit");
116             client.sendString("test3");
117             sleep(50);
118             assertEquals(3, TestCommandHandler.getHandleCommandFlag());
119             assertEquals(1, TestEventHandler.getClosingConnectionFlag());
120             System.out.println("ClosingConnection Pass");
121
122             sleep(100);
123             assertTrue(client.readString()==null);
124             System.out.println("isConnected Pass");
125             client.close();
126         } catch(Exception JavaDoc e) {
127             fail("Exception: "+e);
128         }
129     }
130
131     public void testQuickServerLostCon() {
132         try {
133             assertEquals(0, TestEventHandler.getGotConnectedFlag());
134             assertEquals(0, TestEventHandler.getClosingConnectionFlag());
135             assertEquals(0, TestEventHandler.getLostConnectionFlag());
136             assertEquals(0, TestCommandHandler.getHandleCommandFlag());
137
138             client.connect(host, port);
139             sleep(50);
140             assertEquals(1, TestEventHandler.getGotConnectedFlag());
141             System.out.println("GotConnected Pass");
142
143             client.sendString("test1");
144             sleep(50);
145             assertEquals(1, TestCommandHandler.getHandleCommandFlag());
146             assertEquals("test1", TestCommandHandler.getRequest());
147             System.out.println("HandleCommand 1 Pass");
148
149             client.close();
150             sleep(50);
151             assertEquals(1, TestEventHandler.getLostConnectionFlag());
152             System.out.println("LostConnection Pass");
153
154         } catch(Exception JavaDoc e) {
155             fail("Exception: "+e);
156         }
157     }
158
159     private void sleep(int time) {
160         try {
161             Thread.currentThread().sleep(time);
162         } catch(Exception JavaDoc e) {}
163     }
164 }
Popular Tags