Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/socket
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv28952/input/javasrc/biz/xsoftware/examples/socket
Added Files:
TestExample.java SysUnderTest.java OtherSubsystem.java
TCPSocket.java TCPSocketImpl.java
Removed Files:
MockServer.java.bak SysUnderTest.java.bak TesExample.java.bak
OtherSubsystem.java.bak TCPSocket.java.bak
TCPSocketImpl.java.bak TCPSocketMock.java.bak
Log Message:
add behavior example and fix socket example.
--- MockServer.java.bak DELETED ---
--- TCPSocket.java.bak DELETED ---
--- TCPSocketMock.java.bak DELETED ---
--- TCPSocketImpl.java.bak DELETED ---
--- NEW FILE: OtherSubsystem.java ---
package biz.xsoftware.examples.socket;
import java.nio.ByteBuffer;
public interface OtherSubsystem {
public ByteBuffer getUserData(int id);
public void failedToSendUser(int id);
}
--- NEW FILE: TestExample.java ---
/*
* 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.nio.ByteBuffer;
import junit.framework.TestCase;
import biz.xsoftware.mock.CalledMethod;
import biz.xsoftware.mock.MockObject;
import biz.xsoftware.mock.MockObjectFactory;
/**
* JUnit Suite of TestCases for demonstrating mocking out socket
* interfaces to test network failures.
*
* @author Dean Hiller
*/
public class TestExample extends TestCase {
private SysUnderTest sysUnderTest;
private MockObject mockSubsys;
private MockObject mockSocket;
/**
* @showcode
*/
public TestExample(String name) {
super(name);
}
/**
* @showcode
*/
public void setUp() throws Exception {
mockSubsys = MockObjectFactory.createMock(OtherSubsystem.class);
mockSocket = MockObjectFactory.createMock(TCPSocket.class);
sysUnderTest = new SysUnderTest((TCPSocket)mockSocket, (OtherSubsystem)mockSubsys);
}
/**
* @showcode
*/
public void tearDown() throws IOException {
}
/**
* 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(new IOException("problems writing to server"), "write");
ByteBuffer buffer = ByteBuffer.allocate(100);
buffer.put(new byte[] { 1, 2, 3, 4});
buffer.flip();
mockSubsys.addReturnValue(buffer, "getUserData");
int id = 10;
sysUnderTest.sendUserToServer(10);
CalledMethod[] methods = mockSubsys.expect("getUserData", "failedToSendUser");
CalledMethod getUserData = methods[0];
CalledMethod failedToSend = methods[1];
assertEquals(new Integer(id), getUserData.getAllParams()[0]);
assertEquals(new Integer(id), failedToSend.getParameter(0));
}
}
--- SysUnderTest.java.bak DELETED ---
--- NEW FILE: TCPSocketImpl.java ---
/*
* 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 ---
/*
* 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;
/**
* SysUnderTest that uses TCPSockets to talk to a server.
*
* @author Dean Hiller
*/
public class SysUnderTest {
private TCPSocket s;
private OtherSubsystem userService;
/**
* @showcode
*/
public SysUnderTest(TCPSocket s, OtherSubsystem userSvc) {
this.s = s;
this.userService = userSvc;
}
/**
* @showcode
*/
public void sendUserToServer(int id) {
ByteBuffer userData = userService.getUserData(id);
try {
s.write(userData);
} catch(IOException e) {
userService.failedToSendUser(id);
}
}
}
--- TesExample.java.bak DELETED ---
--- OtherSubsystem.java.bak DELETED ---
--- NEW FILE: TCPSocket.java ---
/*
* 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;
}
|