All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.micro.testcases.CAgent Maven / Gradle / Ivy

Go to download

The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.

There is a newer version: 4.0.267
Show newest version
package jadex.micro.testcases;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IArgumentsResultsFeature;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.annotation.Service;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.search.SServiceProvider;
import jadex.commons.future.DefaultResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.IntermediateFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.Implementation;
import jadex.micro.annotation.ProvidedService;
import jadex.micro.annotation.ProvidedServices;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;
import jadex.micro.annotation.Result;
import jadex.micro.annotation.Results;

/**
 *  Simple test agent with one service for testing parameter and result copying.
 */
@ProvidedServices(@ProvidedService(type=ICService.class, implementation=@Implementation(expression="$pojoagent")))
@RequiredServices(@RequiredService(name="cservice", type=ICService.class, binding=@Binding(scope=RequiredServiceInfo.SCOPE_LOCAL)))
@Results(@Result(name="testresults", clazz=Testcase.class))
@Service(ICService.class)
@Agent
public class CAgent implements ICService
{
	@Agent
	protected IInternalAccess agent;
	
	/**
	 *  Test if copy parameters work.
	 */
	@AgentBody
	public IFuture executeBody()
	{
		final Future ret = new Future();
		
		final List testcases = new ArrayList();
		
		// Test with required service proxy.
		IFuture	fut	= agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("cservice");
		fut.addResultListener(new DefaultResultListener()
		{
			public void resultAvailable(ICService result)
			{
				testService(testcases, result)
					.addResultListener(new DefaultResultListener()
				{
					public void resultAvailable(Void result)
					{
						// Test with provided service proxy.
						SServiceProvider.getService(agent, ICService.class)
							.addResultListener(new DefaultResultListener()
						{
							public void resultAvailable(ICService result)
							{
								testService(testcases, result)
									.addResultListener(new DefaultResultListener()
								{
									public void resultAvailable(Void result)
									{										
										agent.getComponentFeature(IArgumentsResultsFeature.class).getResults().put("testresults", new Testcase(testcases.size(),
											(TestReport[])testcases.toArray(new TestReport[testcases.size()])));
//										killAgent();
										ret.setResult(null);
									}
								});
							}
						});
					}
				});
			}
		});
		
		return ret;
	}
	
	/**
	 *  Test if no copy works.
	 */
	public IFuture testArgumentReference(Object arg, int hash)
	{
//		System.out.println("called service");
		return arg.hashCode()==hash ? IFuture.TRUE: IFuture.FALSE;
	}
	
	/**
	 *  Test if no copy works.
	 */
	public IFuture testArgumentCopy(Object arg, int hash)
	{
		return arg.hashCode()!=hash ? IFuture.TRUE: IFuture.FALSE;
	}
	
	/**
	 *  Test if result value can be passed by reference.
	 */
	public IFuture testResultReference(Object arg)
	{
		return new Future(arg);
	}
	
	/**
	 *  Test if result value can be passed by copy.
	 */
	public IFuture testResultCopy(Object arg)
	{
		return new Future(arg);
	}
	
	/**
	 *  Test if result value can be passed by reference.
	 */
	public IIntermediateFuture testResultReferences(Object[] args)
	{
		return new IntermediateFuture(Arrays.asList(args));
	}
	
	/**
	 *  Test if result value can be passed by copy.
	 */
	public IIntermediateFuture testResultCopies(Object[] args)
	{
		return new IntermediateFuture(Arrays.asList(args));		
	}
	
	//-------- helper methods --------
	/**
	 *  Perform test with the service.
	 */
	protected IFuture	testService(final List testcases, final ICService cservice)
	{
		final Future	ret	= new Future();
		
		final Object arg = new Object();
		cservice.testArgumentReference(arg, arg.hashCode()).addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(Boolean result)
			{
				TestReport tr = new TestReport("#1", "Test if argument is not copied.");
				if(result.booleanValue())
				{
					tr.setSucceeded(true);
				}
				else
				{
					tr.setReason("Hashcode is not equal.");
				}
				testcases.add(tr);
				
				cservice.testArgumentCopy(arg, arg.hashCode()).addResultListener(new ExceptionDelegationResultListener(ret)
				{
					public void customResultAvailable(Boolean result)
					{
						TestReport tr = new TestReport("#2", "Test if argument is copied.");
						if(result.booleanValue())
						{
							tr.setSucceeded(true);
						}
						else
						{
							tr.setReason("Hashcode is equal.");
						}
						testcases.add(tr);
				
						cservice.testResultReference(arg).addResultListener(new ExceptionDelegationResultListener(ret)
						{
							public void customResultAvailable(Object result)
							{
								TestReport tr = new TestReport("#3", "Test if result is not copied.");
								if(arg.hashCode()==result.hashCode())
								{
									tr.setSucceeded(true);
								}
								else
								{
									tr.setReason("Hashcode is not equal.");
								}
								testcases.add(tr);
								
								cservice.testResultCopy(arg).addResultListener(new ExceptionDelegationResultListener(ret)
								{
									public void customResultAvailable(Object result)
									{
										TestReport tr = new TestReport("#4", "Test if result is copied.");
										if(arg.hashCode()!=result.hashCode())
										{
											tr.setSucceeded(true);
										}
										else
										{
											tr.setReason("Hashcode is equal.");
										}
										testcases.add(tr);
										
										final Object[]	args	= new Object[]{arg, new Object()};
										cservice.testResultReferences(args)
											.addResultListener(new ExceptionDelegationResultListener, Void>(ret)
										{
											public void customResultAvailable(Collection result)
											{
												TestReport tr = new TestReport("#4", "Test if results are not copied.");
												if(args.length!=result.size())
												{
													tr.setReason("Wrong number of results.");													
												}
												else
												{
													boolean	match	= true;
													Iterator	it	= result.iterator();
													for(int i=0; match && i, Void>(ret)
												{
													public void customResultAvailable(Collection result)
													{
														TestReport tr = new TestReport("#5", "Test if results are copied.");
														if(args.length!=result.size())
														{
															tr.setReason("Wrong number of results.");													
														}
														else
														{
															boolean	match	= false;
															Iterator	it	= result.iterator();
															for(int i=0; !match && i