Interface definitions
You can define interfaces for the echo
behavior.
Behavior interface
package com.example.behaviour.echo;
import
com.greenhat.tester.api.behaviour.LifecycleAwareBehaviour;
public interface Echo extends LifecycleAwareBehaviour
{
void say( String phrase );
}
Behavior factory
package com.example.behaviour.echo;
import java.util.Map;
public class EchoFactory implements BehaviourFactory<Echo>
{
public EchoFactory()
{
}
@Override
public Echo create( BehaviourServices services,
Map<String, Object> configuration, Object callback )
{
String echoCountKey = "echoCountKey";
int echoCount = 1;
if ( configuration.containsKey( echoCountKey ) )
{
echoCount = (Integer)configuration.get(
echoCountKey );
}
return new EchoImpl( (EchoListener)callback,
echoCount );
}
}
The echo
behavior implementation is shown in the following
section.
Behavior implementation
package com.example.behaviour.echo.impl;
import java.util.concurrent.Executor;
public class EchoImpl implements Echo
{
private final EchoListener callback;
private final int echoCount;
private final Executor executor =
Executors.newSingleThreadExecutor();
public EchoImpl( EchoListener callback, int
echoCount )
{
this.callback = callback;
this.echoCount = echoCount;
}
@Override
public void say( final String phrase )
{
executor.execute( new Runnable()
{
@Override
public void run()
{
for( int i = 0; i < echoCount; i++ )
{
callback.onEcho( phrase );
}
}
});
}
//..
Callback interface
package com.example.behaviour.echo;
public interface EchoListener
{
void onEcho( string phrase );
}
IBM® Rational® Test Virtualization Server stubs
will be able to invoke the say(String phrase)
operation of this behaviour and can
opt to handle the onEcho(String phrase)
event.
Create a behavior factory class and add an implementation for the new behavior.
The factory will retrieve configuration from the supplied configuration map and use it to
parameterize the echo
behavior instance: