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

jadex.micro.testcases.LoggerAgent 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.List;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IArgumentsResultsFeature;
import jadex.bridge.nonfunctional.annotation.NameValue;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.types.clock.IClock;
import jadex.bridge.service.types.clock.IClockService;
import jadex.commons.SUtil;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.Description;
import jadex.micro.annotation.Imports;
import jadex.micro.annotation.Properties;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;
import jadex.micro.annotation.Result;
import jadex.micro.annotation.Results;
import jadex.micro.testcases.LoggerAgent.TestLogHandler;

/**
 *  A minimal test case agent serving as a demonstrator.
 */
@Imports({"java.util.logging.*"})
@Description("Tests the logger.")
@Results(@Result(name="testresults", description= "The test results.", clazz=Testcase.class))
@RequiredServices({@RequiredService(name="clockservice", type=IClockService.class, 
	binding=@Binding(scope=RequiredServiceInfo.SCOPE_PLATFORM))})
@Properties({
	@NameValue(name="logging.level", value="Level.FINEST"),
//	@NameValue(name="logging.useParentHandlers", value="true"),
//	@NameValue(name="logging.addConsoleHandler", value="true"),
//	@NameValue(name="logging.file", value="log.txt"),
	@NameValue(name="logging.handlers", clazz=TestLogHandler.class)
//	@NameValue(name="logging.handlers", value="new LoggerAgent$TestLogHandler()")
})
@Agent
public class LoggerAgent
{
	@Agent
	protected IInternalAccess agent;
	
	/**
	 *  Just finish the test by setting the result and killing the agent.
	 */
	@AgentBody
	public IFuture executeBody()
	{
		final Future ret = new Future();
		
		agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("clockservice").addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(Object result)
			{
				final IClockService clock = (IClockService)result;
				final long start = clock.getTime();
				String ct = clock.getClockType();
				final boolean simclock = IClock.TYPE_EVENT_DRIVEN.equals(ct) || IClock.TYPE_TIME_DRIVEN.equals(ct);
				
				List reports = new ArrayList();
				
				final TestReport tr = new TestReport("#1", "Test logging.");
				agent.getLogger().setLevel(Level.FINEST);
				agent.getLogger().addHandler(new Handler()
				{
					public void publish(LogRecord record)
					{
//						System.out.println("log: "+record.getMillis());
						long end = clock.getTime();
						long diff = end-start;
						
						if(simclock && diff==0 || !simclock && diff<1000)
						{
							tr.setSucceeded(true);
						}
						else
						{
							tr.setReason("Time in log record differs substantially: "+end+" "+start+" "+diff);
						}
					}
					
					public void flush()
					{
					}
					
					public void close() throws SecurityException
					{
					}
				});
				reports.add(tr);
				
				agent.getLogger().info("test log message");
				
				TestReport tr2 = new TestReport("#2", "Test logging handler.");
				
				Handler[] handlers = agent.getLogger().getHandlers();
				for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy