Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/behavior
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv28952/input/javasrc/biz/xsoftware/examples/behavior
Added Files:
TCPSocket.java TestExample.java
Log Message:
add behavior example and fix socket example.
--- 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.behavior;
import java.io.IOException;
import java.nio.ByteBuffer;
import junit.framework.TestCase;
import biz.xsoftware.mock.Behavior;
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 MockObject mockSocket;
private TCPSocket tcpSocket;
/**
* @showcode
*/
public TestExample(String name) {
super(name);
}
/**
* @showcode
*/
public void setUp() throws Exception {
mockSocket = MockObjectFactory.createMock(TCPSocket.class);
tcpSocket = (TCPSocket)mockSocket;
}
/**
* @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.
* @throws IOException
*
* @showcode
*/
public void testNetworkProblems() throws IOException {
mockSocket.addBehavior(new MyBehavior(), "write");
byte[] data = new byte[] { 1, 2, 3, 4 };
ByteBuffer b = ByteBuffer.allocate(100);
b.put(data);
b.flip();
tcpSocket.write(b);
//now, our mockobject better have simulated what a real tcpSocket would do...
assertEquals(0, b.remaining());
//now the param passed to the mockobject better be what we passed.....
ByteBuffer b2 = (ByteBuffer) mockSocket.expect("write").getAllParams()[0];
byte[] actual = new byte[4];
b2.get(actual);
assertEquals(data.length, actual.length);
for(int i = 0; i < actual.length; i++) {
assertEquals(data[i], actual[i]);
}
}
private class MyBehavior implements Behavior {
public Object[] writeCloner(ByteBuffer b) {
ByteBuffer clone = ByteBuffer.allocate(b.capacity());
clone.put(b);
//rewind original buffer so it appears not have been modified by
//cloning method....
b.rewind();
//flip the clone and it will match the given buffer exactly....
clone.flip();
//return the clone
return new Object[] { clone };
}
public int write(ByteBuffer b) {
//simulate the ByteBuffer being read....
int remaining = b.remaining();
byte[] data = new byte[remaining];
b.get(data);
return remaining;
}
}
}
--- 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.behavior;
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;
}
|