001: /*
002: * Copyright (c) xsocket.org, 2006-2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection;
022:
023: import java.io.IOException;
024: import java.nio.BufferUnderflowException;
025: import java.nio.channels.ClosedChannelException;
026:
027: import org.junit.Assert;
028: import org.junit.Test;
029: import org.xsocket.MaxReadSizeExceededException;
030: import org.xsocket.QAUtil;
031: import org.xsocket.connection.BlockingConnection;
032: import org.xsocket.connection.IBlockingConnection;
033: import org.xsocket.connection.IConnectHandler;
034: import org.xsocket.connection.IDataHandler;
035: import org.xsocket.connection.INonBlockingConnection;
036: import org.xsocket.connection.IServer;
037: import org.xsocket.connection.Server;
038: import org.xsocket.connection.ConnectionUtils;
039:
040: /**
041: *
042: * @author grro@xsocket.org
043: */
044: public final class HandlerThrowsIOExceptionTest {
045:
046: private static final String DELIMITER = "\r";
047: private static final String THROW_EXCPTION_CMD = "throw a io exception!";
048:
049: @Test
050: public void testDataHandler() throws Exception {
051:
052: Handler hdl = new Handler();
053: IServer server = new Server(hdl);
054: ConnectionUtils.start(server);
055:
056: IBlockingConnection connection = new BlockingConnection(
057: "localhost", server.getLocalPort());
058:
059: connection.write("some cmd" + DELIMITER);
060: String result = connection.readStringByDelimiter(DELIMITER);
061: Assert.assertEquals(result, "some cmd");
062:
063: try {
064: connection.write(THROW_EXCPTION_CMD + DELIMITER);
065:
066: QAUtil.sleep(100);
067: Assert.assertFalse(hdl.connection.isOpen());
068:
069: String result2 = connection
070: .readStringByDelimiter(DELIMITER);
071: Assert
072: .fail("a ClosedConnectionException should have been thrown");
073: } catch (ClosedChannelException shouldbeThrown) {
074: }
075:
076: connection.close();
077: server.close();
078: }
079:
080: private static final class Handler implements IConnectHandler,
081: IDataHandler {
082:
083: private INonBlockingConnection connection = null;
084:
085: public boolean onConnect(INonBlockingConnection connection)
086: throws IOException {
087: this .connection = connection;
088:
089: return true;
090: }
091:
092: public boolean onData(INonBlockingConnection connection)
093: throws IOException, BufferUnderflowException,
094: MaxReadSizeExceededException {
095: String cmd = connection.readStringByDelimiter(DELIMITER);
096: if (cmd.equals(THROW_EXCPTION_CMD)) {
097: throw new IOException("kill");
098: } else {
099: connection.write(cmd + DELIMITER);
100: }
101: return true;
102: }
103: }
104: }
|