001: /*
002: * Copyright (C) The Spice Group. All rights reserved.
003: *
004: * This software is published under the terms of the Spice
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.spice.netserve.sockets.impl;
009:
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.io.OutputStream;
013: import java.net.InetAddress;
014: import java.net.ServerSocket;
015: import java.net.Socket;
016: import java.net.UnknownHostException;
017: import junit.framework.TestCase;
018: import org.codehaus.spice.netserve.sockets.ServerSocketFactory;
019: import org.codehaus.spice.netserve.sockets.SocketFactory;
020:
021: /**
022: * TestCase for {@link org.codehaus.spice.netserve.sockets.SocketFactory} and {@link org.codehaus.spice.netserve.sockets.ServerSocketFactory}.
023: *
024: * @author Peter Donald
025: * @version $Revision: 1.2 $ $Date: 2004/03/21 23:43:00 $
026: */
027: public class SocketTestCase extends TestCase {
028: private static final int PORT = 1977;
029: private static final InetAddress HOST = getLocalHost();
030: private static final byte DATA = 23;
031:
032: public SocketTestCase(final String name) {
033: super (name);
034: }
035:
036: public void testDefault() throws Exception {
037: final SocketFactory csf = new DefaultSocketFactory();
038: final ServerSocketFactory ssf = new DefaultServerSocketFactory();
039:
040: doSocketPairTest(csf, ssf);
041: }
042:
043: private void doSocketPairTest(final SocketFactory csf,
044: final ServerSocketFactory ssf) throws IOException {
045: try {
046: csf.createSocket(HOST, PORT);
047: final String message = "Should not be able to create "
048: + "client socket when server socket not initialized.";
049: fail(message);
050: } catch (IOException e) {
051: }
052:
053: final ServerSocket ss1 = ssf.createServerSocket(PORT);
054: readWriteTestsOnSingleSocket(ss1, csf);
055:
056: final ServerSocket ss2 = ssf.createServerSocket(PORT, 1);
057: readWriteTestsOnSingleSocket(ss2, csf);
058:
059: final ServerSocket ss3 = ssf.createServerSocket(PORT, 1, HOST);
060: readWriteTestsOnSingleSocket(ss3, csf);
061: }
062:
063: private void readWriteTestsOnSingleSocket(
064: final ServerSocket serverSocket, final SocketFactory csf)
065: throws IOException {
066: final Socket client = csf.createSocket(HOST, PORT);
067: final Socket server = serverSocket.accept();
068:
069: pushPullData("Testing single socket conn", client, server);
070:
071: final Socket client2 = csf.createSocket(HOST, PORT, HOST, 0);
072: final Socket server2 = serverSocket.accept();
073: pushPullData("Testing single socket conn2", client2, server2);
074:
075: serverSocket.close();
076: }
077:
078: private void pushPullData(final String prefix, final Socket client,
079: final Socket server) throws IOException {
080: pushData(prefix + ": c2s", client, server);
081: pushData(prefix + ": s2c", server, client);
082:
083: client.close();
084: server.close();
085: }
086:
087: /**
088: * Push data from socket1 to socket2.
089: */
090: private void pushData(final String prefix, final Socket socket1,
091: final Socket socket2) throws IOException {
092: final OutputStream cos = socket1.getOutputStream();
093: cos.write(DATA);
094: cos.flush();
095:
096: final InputStream sis = socket2.getInputStream();
097: assertEquals(prefix + ": Write/read byte", DATA, sis.read());
098: }
099:
100: private static InetAddress getLocalHost() {
101: try {
102: return InetAddress.getLocalHost();
103: } catch (UnknownHostException e) {
104: throw new IllegalStateException(e.toString());
105: }
106: }
107: }
|