org.otfeed.support.mock
Class MockStreamerFactory

java.lang.Object
  extended by org.otfeed.support.mock.MockStreamerFactory
All Implemented Interfaces:
IStreamerFactory

public class MockStreamerFactory
extends java.lang.Object
implements IStreamerFactory

Mock streaming layer for offline testing.

This object facilitates offline testing of object marshalling code by returning a pre-recorded sequence of frames.

Use this object to substitute streamerFactory of OTConnectionFactory.

IMPORTANT: set heartbeatInterval property of OTConnectionListener to reasonably large value to make sure that heartbeat frames are not sent to the streamer layer. Otherwise, programs that MockStreamerFactory executes may be confused by receiving a heartbeat packet.

IMPORTANT: recorded packets must contain correct requestId. Otherwise they will be silently dropped. Request id starts with zero (login request) and is incremented for every command prepared (event if not submitted). Thus, first request will have id of one, second - id of two, etc. Practically, its simpler to restrict testing to just a single command and then destroy the connection. This way, all frames for a command to be tested must have request id of 1.

Next, build a list of operations that will be the "program" for the streamer. Assign this list to the opList property of MockStreamerFactory.

Example program may look like this:

 // EXPECT ANY (will be login request frame)
 mockStreamerFactory.getOpList().add(MockStreamerFactory.expectAny());
 // SEND "login OK" response
 mockStreamerFactory.getOpList().add(MockStreamerFactory.sendLoginOK());
 // WAIT for any buffer
 mockStreamerFactory.getOpList().add(MockStreamerFactory.expectAny());
 // No more elements in the list: streamer will emulate
 // disconnect from the server (read error).  
 


Nested Class Summary
static interface MockStreamerFactory.Op
           
 
Constructor Summary
MockStreamerFactory()
          Creates new mock streamer.
 
Method Summary
 IStreamer connect(java.lang.String host, int port)
          Creates a connection (synchronously).
static MockStreamerFactory.Op expectAny()
          Creates an operation of "EXPECT ANY BUFFER" type.
static MockStreamerFactory.Op expectBuffer(java.nio.ByteBuffer e)
          Creates an operation of "EXPECT A BUFFER" type.
 IFormat<java.nio.ByteBuffer> getFormat()
          Format to use when parsing ByteBuffer s.
 java.lang.String getId()
          Identification string.
 java.util.List<MockStreamerFactory.Op> getOpList()
          List of operations that drive the mock streamer.
 boolean getTrace()
          Trace flag makes streamer print all frames to screen.
static java.nio.ByteBuffer parse(java.lang.String bufferString)
          Helper: creates a ByteBuffer from its hex-dump String representation.
static java.nio.ByteBuffer parseResource(java.lang.String resourceName)
          Helper: creates a ByteBuffer by parsing a resource file.
static MockStreamerFactory.Op send(java.nio.ByteBuffer s)
          Creates an operation of "SEND" type.
static MockStreamerFactory.Op sendLoginFailed()
          Helper: creates a "SEND" operation with the buffer telling that "login failed".
static MockStreamerFactory.Op sendLoginOK()
          Helper: creates a "SEND" operation with the buffer telling "login was OK".
 void setFormat(IFormat<java.nio.ByteBuffer> val)
          Sets format.
 void setId(java.lang.String val)
          Sets identification string.
 void setOpList(java.util.List<MockStreamerFactory.Op> val)
          Sets op list.
 void setTrace(boolean val)
          Sets trace flag.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockStreamerFactory

public MockStreamerFactory()
Creates new mock streamer.

Method Detail

parse

public static java.nio.ByteBuffer parse(java.lang.String bufferString)
Helper: creates a ByteBuffer from its hex-dump String representation.

Parameters:
bufferString - string in the hex format.
Returns:
buffer.

parseResource

public static java.nio.ByteBuffer parseResource(java.lang.String resourceName)
                                         throws java.io.IOException
Helper: creates a ByteBuffer by parsing a resource file.

Parameters:
resourceName - name of the resource.
Returns:
buffer.
Throws:
java.io.IOException - on IO error.

sendLoginOK

public static MockStreamerFactory.Op sendLoginOK()
Helper: creates a "SEND" operation with the buffer telling "login was OK".

Returns:
send operation.

sendLoginFailed

public static MockStreamerFactory.Op sendLoginFailed()
Helper: creates a "SEND" operation with the buffer telling that "login failed".

Returns:
send operation.

getFormat

public IFormat<java.nio.ByteBuffer> getFormat()
Format to use when parsing ByteBuffer s. Defaults to BufferFormat.

Returns:
format.

setFormat

public void setFormat(IFormat<java.nio.ByteBuffer> val)
Sets format.

Parameters:
val - format.

expectAny

public static MockStreamerFactory.Op expectAny()
Creates an operation of "EXPECT ANY BUFFER" type. This one will match any received buffer.

Returns:
operation.

expectBuffer

public static MockStreamerFactory.Op expectBuffer(java.nio.ByteBuffer e)
Creates an operation of "EXPECT A BUFFER" type. This one will try to match the model buffer against the actual one received from the upper layer.

Parameters:
e - - model buffer.
Returns:
operation.

send

public static MockStreamerFactory.Op send(java.nio.ByteBuffer s)
Creates an operation of "SEND" type. This one sends the buffer back to the driver (and, ultimately, to the client application as a set of events).

Parameters:
s - buffer to send.
Returns:
operation.

getOpList

public java.util.List<MockStreamerFactory.Op> getOpList()
List of operations that drive the mock streamer.

List of operations specifies program that mock streamer will execute. Following operations are defined:

Returns:
op list.

setOpList

public void setOpList(java.util.List<MockStreamerFactory.Op> val)
Sets op list.

Parameters:
val - op list.

getTrace

public boolean getTrace()
Trace flag makes streamer print all frames to screen.

Returns:
trace flag.

setTrace

public void setTrace(boolean val)
Sets trace flag.

Parameters:
val - trace value.

getId

public java.lang.String getId()
Identification string.

Returns:
id string.

setId

public void setId(java.lang.String val)
Sets identification string.

Parameters:
val - id string.

connect

public IStreamer connect(java.lang.String host,
                         int port)
                  throws java.io.IOException
Description copied from interface: IStreamerFactory
Creates a connection (synchronously).

Specified by:
connect in interface IStreamerFactory
Throws:
java.io.IOException


Copyright © 2007 Mike Kroutikov. All Rights Reserved.