Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/socket
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv29004/input/javasrc/biz/xsoftware/examples/socket
Added Files:
TCPSocketMock.java.bak MockServer.java.bak TesExample.java.bak
SysUnderTest.java.bak OtherSubsystem.java.bak
TCPSocket.java.bak TCPSocketImpl.java.bak
Log Message:
add files that will be used later.
--- NEW FILE: MockServer.java.bak ---
/*
* Created on Jul 3, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.socket;
import java.net.InetSocketAddress;
/**
* The MockServer that the SysUnderTest talks to.
*
* @author Dean Hiller
*/
public class MockServer {
/**
* @showcode
*/
public InetSocketAddress start() {
return null;
}
/**
* @showcode
*/
public void stop() {
}
/**
* @showcode
*/
public void closeSocket() {
}
}
--- NEW FILE: TCPSocket.java.bak ---
/*
* Created on Jul 3, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.socket;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* Abstraction of a Socket to allow testing.
*
* @author Dean Hiller
*/
public interface TCPSocket {
public int read(ByteBuffer b) throws IOException;
public int write(ByteBuffer b) throws IOException;
}
--- NEW FILE: TCPSocketMock.java.bak ---
/*
* Created on Jul 3, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.socket;
import java.io.IOException;
import java.nio.ByteBuffer;
import biz.xsoftware.mock.MockSuperclass;
/**
* A mock TCPSocket.
* @see TCPSocket
* @author Dean Hiller
*/
public class TCPSocketMock extends MockSuperclass implements TCPSocket {
public final static String READ_METHOD = "read method";
public final static String WRITE_METHOD = "write method";
private TCPSocketImpl socket;
/**
* @showcode
*/
public TCPSocketMock(TCPSocketImpl socket) {
this.socket = socket;
}
/**
* @showcode
*/
public int read(ByteBuffer b) throws IOException {
//allows us to throw Exceptions like the network went down!!!
methodCalled(READ_METHOD, b);
return socket.read(b);
}
/**
* @showcode
*/
public int write(ByteBuffer b) throws IOException {
//allows us to throw Exceptions like the network went down!!!
methodCalled(WRITE_METHOD, b);
return socket.read(b);
}
/* (non-Javadoc)
* @see biz.xsoftware.mock.MockSuperclass#getClasses()
*/
public Class[] getClasses() {
return null;
}
public Object inst() {
// TODO Auto-generated method stub
return null;
}
}
--- NEW FILE: TCPSocketImpl.java.bak ---
/*
* Created on Jul 3, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.socket;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
/**
* The real implementation behind TCPSocket.
*
* @author Dean Hiller
*/
public class TCPSocketImpl implements TCPSocket {
private Socket socket;
/**
* @showcode
*/
public TCPSocketImpl(Socket s) {
socket = s;
}
/**
* @see biz.xsoftware.examples.socket.TCPSocket#read(java.nio.ByteBuffer)
* @showcode
*/
public int read(ByteBuffer b) throws IOException {
return socket.getChannel().read(b);
}
/**
* @see biz.xsoftware.examples.socket.TCPSocket#write(java.nio.ByteBuffer)
* @showcode
*/
public int write(ByteBuffer b) throws IOException {
return socket.getChannel().write(b);
}
}
--- NEW FILE: SysUnderTest.java.bak ---
/*
* Created on Jul 3, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.socket;
/**
* SysUnderTest that uses TCPSockets to talk to a server.
*
* @author Dean Hiller
*/
public class SysUnderTest {
/**
* @showcode
*/
public SysUnderTest(TCPSocket s, OtherSubsystem mockSubsys) {
}
/**
* @showcode
*/
public void sendDataToServer() {
}
}
--- NEW FILE: TesExample.java.bak ---
/*
* Created on Jun 28, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.socket;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import junit.framework.TestCase;
import biz.xsoftware.mock.MockObjectFactory;
import biz.xsoftware.mock.MockObject;
/**
* JUnit Suite of TestCases for demonstrating mocking out socket
* interfaces to test network failures.
*
* @author Dean Hiller
*/
public class TesExample extends TestCase {
private SysUnderTest sysUnderTest;
private MockObject mockSubsys;
private TCPSocketMock mockSocket;
private MockServer mockServer;
private Socket s;
/**
* @showcode
*/
public TesExample(String name) {
super(name);
}
/**
* @showcode
*/
public void setUp() throws Exception {
s = new Socket();
mockServer = new MockServer();
InetSocketAddress serverAddr = mockServer.start();
InetAddress localHost = InetAddress.getLocalHost();
s.bind(new InetSocketAddress(localHost, 0));
s.connect(serverAddr);
TCPSocketImpl tcpSock = new TCPSocketImpl(s);
mockSocket = new TCPSocketMock(tcpSock);
mockSubsys = MockObjectFactory.createMock(OtherSubsystem.class);
sysUnderTest = new SysUnderTest(mockSocket, (OtherSubsystem)mockSubsys);
}
/**
* @showcode
*/
public void tearDown() throws IOException {
mockServer.stop();
s.close();
}
/**
* Test that upon network problems like IOException when writing to server
* that other subsystems get cleaned up properly. The simple test below
* shows how this can be done and extended to more complicated network
* recovery algorithms.
*
* @showcode
*/
public void testNetworkProblems() {
mockSocket.addThrowException("write", new IOException("problems writing to server"));
sysUnderTest.sendDataToServer();
mockSubsys.expectCall("cleanup");
}
/**
* Test that when the server unexpectedly closes it's socket, the system
* tears down correctly on this unexpected event.
*
* @showcode
*/
public void testServerClosingSocket() {
mockServer.closeSocket();
mockSubsys.expectCall("cleanup");
}
}
--- NEW FILE: OtherSubsystem.java.bak ---
/*
* Created on Jul 3, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.socket;
/**
* Some other subsystem.
* @author Dean Hiller
*/
public interface OtherSubsystem {
public void cleanup();
}
|