Coverage Report - biz.xsoftware.examples.socket.TestExample
 
Classes in this File Line Coverage Branch Coverage Complexity
TestExample
100%
20/20
N/A
1
 
 1  
 /*
 2  
  * Created on Jun 28, 2004
 3  
  *
 4  
  * To change the template for this generated file go to
 5  
  * Window - Preferences - Java - Code Generation - Code and Comments
 6  
  */
 7  
 package biz.xsoftware.examples.socket;
 8  
 
 9  
 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  1
                 super(name);
 34  1
         }
 35  
         /**
 36  
          * @showcode
 37  
          */
 38  
         public void setUp() throws Exception {
 39  1
                 mockSubsys = MockObjectFactory.createMock(OtherSubsystem.class);
 40  1
                 mockSocket = MockObjectFactory.createMock(TCPSocket.class);
 41  
                 
 42  1
                 sysUnderTest = new SysUnderTest((TCPSocket)mockSocket, (OtherSubsystem)mockSubsys);
 43  1
         }
 44  
         /**
 45  
          * @showcode
 46  
          */
 47  
         public void tearDown() throws IOException {
 48  
 
 49  1
         }
 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  1
                 mockSocket.addThrowException("write", new IOException("problems writing to server"));
 61  1
                 ByteBuffer buffer = ByteBuffer.allocate(100);
 62  1
                 buffer.put(new byte[] { 1, 2, 3, 4});
 63  1
                 buffer.flip();
 64  1
                 mockSubsys.addReturnValue("getUserData", buffer);
 65  
                 
 66  1
                 int id = 10;
 67  1
                 sysUnderTest.sendUserToServer(10);
 68  
                 
 69  1
                 CalledMethod[] methods = mockSubsys.expect("getUserData", "failedToSendUser");
 70  1
                 CalledMethod getUserData = methods[0];
 71  1
                 CalledMethod failedToSend = methods[1];
 72  
                 
 73  1
                 assertEquals(new Integer(id), getUserData.getAllParams()[0]);
 74  1
                 assertEquals(new Integer(id), failedToSend.getParameter(0));
 75  1
         }
 76  
 
 77  
         
 78  
 }