001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package test.org.quickserver.net.server;
016:
017: import org.quickserver.net.server.*;
018: import org.quickserver.net.client.*;
019: import junit.framework.TestCase;
020: import junit.framework.AssertionFailedError;
021: import java.util.logging.*;
022: import org.quickserver.util.xmlreader.QuickServerConfig;
023:
024: /**
025: * JUnit test cases for QuickServer
026: */
027:
028: public class SimpleServerBlockTest extends TestCase {
029: private QuickServer server;
030: private ClientService client;
031: private String host = "127.0.0.1";
032: private int port = 54321;
033: private QuickServerConfig config;
034:
035: public boolean getServerMode() {
036: return false;
037: }
038:
039: public String getServerName() {
040: return "SimpleServerBlockTest";
041: }
042:
043: public SimpleServerBlockTest(String name) {
044: super (name);
045: }
046:
047: private void configServer() {
048: config = new QuickServerConfig();
049: config.setName(getServerName());
050: config
051: .setClientEventHandler("test.org.quickserver.net.server.TestEventHandler");
052: config
053: .setClientCommandHandler("test.org.quickserver.net.server.TestCommandHandler");
054: config.getServerMode().setBlocking(getServerMode());
055: config.setPort(port);
056: config.setBindAddr(host);
057: }
058:
059: public void setUp() {
060: server = new QuickServer();
061: client = new BlockingClient();
062: configServer();
063:
064: server.initService(config);
065:
066: TestEventHandler.reset();
067: TestCommandHandler.reset();
068:
069: try {
070: server.startServer();
071: } catch (Exception e) {
072: server = null;
073: fail("Server could not start: " + e);
074: }
075: }
076:
077: public void tearDown() {
078: try {
079: if (server != null)
080: server.stopServer();
081: } catch (Exception e) {
082: fail("Server could not stop: " + e);
083: }
084: //server = null;
085: }
086:
087: public static void main(String args[]) {
088: junit.textui.TestRunner.run(SimpleServerBlockTest.class);
089: }
090:
091: public void testQuickServerBasic() {
092: try {
093: assertEquals(0, TestEventHandler.getGotConnectedFlag());
094: assertEquals(0, TestEventHandler.getClosingConnectionFlag());
095: assertEquals(0, TestEventHandler.getLostConnectionFlag());
096: assertEquals(0, TestCommandHandler.getHandleCommandFlag());
097:
098: client.connect(host, port);
099: sleep(50);
100: assertEquals(1, TestEventHandler.getGotConnectedFlag());
101: System.out.println("GotConnected Pass");
102:
103: client.sendString("test1");
104: sleep(50);
105: assertEquals(1, TestCommandHandler.getHandleCommandFlag());
106: assertEquals("test1", TestCommandHandler.getRequest());
107: System.out.println("HandleCommand 1 Pass");
108:
109: TestCommandHandler.setResponse("junit");
110: client.sendString("test2");
111: sleep(50);
112: assertEquals(2, TestCommandHandler.getHandleCommandFlag());
113: assertEquals("test2", TestCommandHandler.getRequest());
114: assertEquals("junit", client.readString());
115: System.out.println("HandleCommand 2 Pass");
116:
117: TestCommandHandler.setResponse("quit");
118: client.sendString("test3");
119: sleep(50);
120: assertEquals(3, TestCommandHandler.getHandleCommandFlag());
121: assertEquals(1, TestEventHandler.getClosingConnectionFlag());
122: System.out.println("ClosingConnection Pass");
123:
124: sleep(100);
125: assertTrue(client.readString() == null);
126: System.out.println("isConnected Pass");
127: client.close();
128: } catch (Exception e) {
129: fail("Exception: " + e);
130: }
131: }
132:
133: public void testQuickServerLostCon() {
134: try {
135: assertEquals(0, TestEventHandler.getGotConnectedFlag());
136: assertEquals(0, TestEventHandler.getClosingConnectionFlag());
137: assertEquals(0, TestEventHandler.getLostConnectionFlag());
138: assertEquals(0, TestCommandHandler.getHandleCommandFlag());
139:
140: client.connect(host, port);
141: sleep(50);
142: assertEquals(1, TestEventHandler.getGotConnectedFlag());
143: System.out.println("GotConnected Pass");
144:
145: client.sendString("test1");
146: sleep(50);
147: assertEquals(1, TestCommandHandler.getHandleCommandFlag());
148: assertEquals("test1", TestCommandHandler.getRequest());
149: System.out.println("HandleCommand 1 Pass");
150:
151: client.close();
152: sleep(50);
153: assertEquals(1, TestEventHandler.getLostConnectionFlag());
154: System.out.println("LostConnection Pass");
155:
156: } catch (Exception e) {
157: fail("Exception: " + e);
158: }
159: }
160:
161: private void sleep(int time) {
162: try {
163: Thread.currentThread().sleep(time);
164: } catch (Exception e) {
165: }
166: }
167: }
|