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.ByteBuffer;
026: import java.nio.channels.ClosedChannelException;
027:
028: import org.junit.Assert;
029: import org.junit.Test;
030: import org.xsocket.MaxReadSizeExceededException;
031: import org.xsocket.QAUtil;
032: import org.xsocket.connection.IServer;
033: import org.xsocket.connection.Server;
034: import org.xsocket.connection.ConnectionUtils;
035:
036: /**
037: *
038: * @author grro@xsocket.org
039: */
040: public final class CompatibilityWriteableByteChannelTest {
041:
042: @Test
043: public void testNonBlockingWriteClientClose() throws Exception {
044:
045: ServerHandler srvHdl = new ServerHandler();
046: IServer server = new Server(srvHdl);
047: ConnectionUtils.start(server);
048:
049: INonBlockingConnection clientCon = new NonBlockingConnection(
050: "localhost", server.getLocalPort());
051:
052: QAUtil.sleep(200);
053: INonBlockingConnection serverCon = srvHdl.getConection();
054:
055: ByteBuffer buffer = QAUtil.generateByteBuffer(40);
056: int written = clientCon.write(buffer);
057: Assert.assertEquals(40, written);
058:
059: QAUtil.sleep(200);
060: Assert.assertEquals(40, serverCon.available());
061:
062: clientCon.close();
063:
064: buffer = QAUtil.generateByteBuffer(40);
065: try {
066: written = clientCon.write(buffer);
067: Assert.fail("ClosedChannelException expected");
068: } catch (ClosedChannelException expected) {
069: }
070:
071: server.close();
072: }
073:
074: @Test
075: public void testNonBlockingWriteServerConnectionClose()
076: throws Exception {
077:
078: ServerHandler srvHdl = new ServerHandler();
079: IServer server = new Server(srvHdl);
080: ConnectionUtils.start(server);
081:
082: INonBlockingConnection clientCon = new NonBlockingConnection(
083: "localhost", server.getLocalPort());
084:
085: QAUtil.sleep(200);
086: INonBlockingConnection serverCon = srvHdl.getConection();
087:
088: ByteBuffer buffer = QAUtil.generateByteBuffer(40);
089: int written = clientCon.write(buffer);
090: Assert.assertEquals(40, written);
091:
092: QAUtil.sleep(200);
093: Assert.assertEquals(40, serverCon.available());
094:
095: serverCon.close();
096:
097: QAUtil.sleep(200);
098: buffer = QAUtil.generateByteBuffer(40);
099: try {
100: written = clientCon.write(buffer);
101: Assert.fail("ClosedChannelException expected");
102: } catch (ClosedChannelException expected) {
103: }
104:
105: clientCon.close();
106: server.close();
107: }
108:
109: @Test
110: public void testNonBlockingWriteServerConClose() throws Exception {
111:
112: ServerHandler srvHdl = new ServerHandler();
113: IServer server = new Server(srvHdl);
114: ConnectionUtils.start(server);
115:
116: INonBlockingConnection clientCon = new NonBlockingConnection(
117: "localhost", server.getLocalPort());
118:
119: QAUtil.sleep(200);
120: INonBlockingConnection serverCon = srvHdl.getConection();
121:
122: ByteBuffer buffer = QAUtil.generateByteBuffer(40);
123: int written = clientCon.write(buffer);
124: Assert.assertEquals(40, written);
125:
126: QAUtil.sleep(200);
127: Assert.assertEquals(40, serverCon.available());
128:
129: server.close();
130:
131: buffer = QAUtil.generateByteBuffer(40);
132: try {
133: written = clientCon.write(buffer);
134: Assert.fail("ClosedChannelException expected");
135: } catch (ClosedChannelException expected) {
136: }
137:
138: clientCon.close();
139: }
140:
141: private static final class ServerHandler implements IConnectHandler {
142:
143: private INonBlockingConnection connection = null;
144:
145: public boolean onConnect(INonBlockingConnection connection)
146: throws IOException, BufferUnderflowException,
147: MaxReadSizeExceededException {
148: this .connection = connection;
149: return true;
150: }
151:
152: INonBlockingConnection getConection() {
153: return connection;
154: }
155: }
156: }
|