| 
01 /*02  * Created on Jun 28, 2004
 03  *
 04  * To change the template for this generated file go to
 05  * Window - Preferences - Java - Code Generation - Code and Comments
 06  */
 07 package biz.xsoftware.examples.socket;
 08
 09 import java.io.IOException;
 10 import java.nio.ByteBuffer;
 11
 12 import junit.framework.TestCase;
 13 import biz.xsoftware.mock.CalledMethod;
 14 import biz.xsoftware.mock.MockObject;
 15 import biz.xsoftware.mock.MockObjectFactory;
 16
 17 /**
 18  * JUnit Suite of TestCases for demonstrating mocking out socket
 19  * interfaces to test network failures.
 20  *
 21  * @author Dean Hiller
 22  */
 23 public class TestExample extends TestCase {
 24
 25   private SysUnderTest sysUnderTest;
 26   private MockObject mockSubsys;
 27   private MockObject mockSocket;
 28
 29   /**
 30    * @showcode
 31    */
 32   public TestExample(String name) {
 33     super(name);
 34   }
 35   /**
 36    * @showcode
 37    */
 38   public void setUp() throws Exception {
 39     mockSubsys = MockObjectFactory.createMock(OtherSubsystem.class);
 40     mockSocket = MockObjectFactory.createMock(TCPSocket.class);
 41
 42     sysUnderTest = new SysUnderTest((TCPSocket)mockSocket, (OtherSubsystem)mockSubsys);
 43   }
 44   /**
 45    * @showcode
 46    */
 47   public void tearDown() throws IOException {
 48
 49   }
 50
 51   /**
 52    * Test that upon network problems like IOException when writing to server
 53    * that other subsystems get cleaned up properly.  The simple test below
 54    * shows how this can be done and extended to more complicated network
 55    * recovery algorithms.
 56    *
 57    * @showcode
 58    */
 59   public void testNetworkProblems() {
 60     mockSocket.addThrowException("write", new IOException("problems writing to server"));
 61     ByteBuffer buffer = ByteBuffer.allocate(100);
 62     buffer.put(new byte[] { 1, 2, 3, 4});
 63     buffer.flip();
 64     mockSubsys.addReturnValue("getUserData", buffer);
 65
 66     int id = 10;
 67     sysUnderTest.sendUserToServer(10);
 68
 69     CalledMethod[] methods = mockSubsys.expect("getUserData", "failedToSendUser");
 70     CalledMethod getUserData = methods[0];
 71     CalledMethod failedToSend = methods[1];
 72
 73     assertEquals(new Integer(id), getUserData.getAllParams()[0]);
 74     assertEquals(new Integer(id), failedToSend.getParameter(0));
 75   }
 76
 77
 78 }
 |