jadex.micro.testcases.RemovedServiceAgent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-micro Show documentation
Show all versions of jadex-applications-micro Show documentation
The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.
package jadex.micro.testcases;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.ComponentTerminatedException;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IArgumentsResultsFeature;
import jadex.bridge.service.ServiceInvalidException;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.search.ServiceNotFoundException;
import jadex.bridge.service.types.cms.CreationInfo;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.commons.future.IntermediateFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.AgentService;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.Description;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;
import jadex.micro.annotation.Result;
import jadex.micro.annotation.Results;
import jadex.micro.servicecall.DecoupledServiceAgent;
import jadex.micro.servicecall.DirectServiceAgent;
import jadex.micro.servicecall.IServiceCallService;
import jadex.micro.servicecall.RawServiceAgent;
/**
* A test case for testing access to services of already terminated components.
*/
@Description("A test case for testing access to services of already terminated components.")
@Results(@Result(name="testresults", clazz=Testcase.class))
@RequiredServices(@RequiredService(name="cms", type=IComponentManagementService.class, binding=@Binding(scope=Binding.SCOPE_PLATFORM)))
@Agent
public class RemovedServiceAgent
{
/** The agent reference. */
@Agent
protected IInternalAccess agent;
/** The cms service. */
@AgentService
protected IComponentManagementService cms;
/** The test counter. */
protected int cnt;
/**
* Perform the tests and indicate completion in the future.
*/
@AgentBody
public IFuture body()
{
final Future> reports = new Future>();
performTests(RawServiceAgent.class.getName()+".class", true).addResultListener(new DelegationResultListener>(reports)
{
public void customResultAvailable(final Collection reports1)
{
performTests(DirectServiceAgent.class.getName()+".class", true).addResultListener(new DelegationResultListener>(reports)
{
public void customResultAvailable(final Collection reports2)
{
performTests(DecoupledServiceAgent.class.getName()+".class", false).addResultListener(new DelegationResultListener>(reports)
{
public void customResultAvailable(Collection reports3)
{
Collection result = new ArrayList();
result.addAll(reports1);
result.addAll(reports2);
result.addAll(reports3);
super.customResultAvailable(result);
}
});
}
});
}
});
final Future ret = new Future();
reports.addResultListener(new IResultListener>()
{
public void resultAvailable(Collection results)
{
agent.getComponentFeature(IArgumentsResultsFeature.class).getResults().put("testresults", new Testcase(results.size(), results.toArray(new TestReport[results.size()])));
ret.setResult(null);
}
public void exceptionOccurred(Exception exception)
{
final TestReport tr = new TestReport("#1", "Exception during test.");
tr.setFailed(exception.toString());
agent.getComponentFeature(IArgumentsResultsFeature.class).getResults().put("testresults", new Testcase(1, new TestReport[]{tr}));
ret.setResult(null);
}
});
return ret;
}
/**
* Perform the tests and indicate completion in the future.
*/
public IFuture> performTests(final String agentname, final boolean callsuccess)
{
final IntermediateFuture testfut = new IntermediateFuture();
// Create agent to call service on.
cms.createComponent(null, agentname, new CreationInfo(agent.getComponentIdentifier()), null)
.addResultListener(new ExceptionDelegationResultListener>(testfut)
{
public void customResultAvailable(final IComponentIdentifier cid)
{
// Get service reference of created agent.
agent.getComponentFeature(IRequiredServicesFeature.class).searchService(IServiceCallService.class, Binding.SCOPE_PLATFORM)
.addResultListener(new ExceptionDelegationResultListener>(testfut)
{
public void customResultAvailable(final IServiceCallService scs)
{
final TestReport tr1 = new TestReport("#"+(++cnt), "Test if service of "+agentname+" can be called.");
testfut.addIntermediateResult(tr1);
scs.call().addResultListener(new ExceptionDelegationResultListener>(testfut)
{
public void customResultAvailable(Void result)
{
tr1.setSucceeded(true);
// Now kill the agent.
cms.destroyComponent(cid).addResultListener(new ExceptionDelegationResultListener