package modTransf.rules.core;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.Collection;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import modTransf.engine.RuleContext;
import modTransf.engine.SimpleRuleContext;
import modTransf.engine.Arguments;
import modTransf.engine.TransformationException;
import java.util.Map;
import java.util.HashMap;
import modTransf.model.ModelHelper;
import modTransf.engine.EngineException;
import modTransf.engine.ArrayArguments;
/**
* Test case for SimpleRuleContext.
*/
public class DomainTest extends TransformationTestCase
{
public DomainTest(String name)
{
super(name);
}
/**
* Initialize the context for a new test.
* This method is called befor each test
* @throws IOException
*/
public void setUp()
throws IOException
{
super.setUp();
}
/**
* Tear down the test.
* This method is called after each test.
*/
public void tearDown()
{
super.tearDown();
}
/**
* Create a src domain with a subproperty containing various actions for testing.
*/
public Domain createDomainWithSubPropertyAndActions()
throws TransformationException
{
return createDomainWithSubPropertyAndActions(true);
}
/**
* Create a domain with a subproperty containing various actions for testing.
*/
public Domain createDomainWithSubPropertyAndActions(boolean isSource)
throws TransformationException
{
Domain domain = createDomain( "test", 0, null, null, isSource);
ConceptDescriptor property = new ConceptDescriptor();
property.addAction(new ActionTrace("action"));
property.addSetUpLocalVarAction(new ActionTrace("setUpAction"));
property.addEnterGuard(new GuardTrace("enterGuard"));
property.addExitGuard(new GuardTrace("exitGuard"));
domain.addSubPropertyDescriptor(property);
return domain;
}
/**
* Test access to local value specified by the descriptor.
*/
public void testLocalValue()
{
String VARNAME = "aVarName";
Domain domain = new Domain();
domain.setVarName(VARNAME);
String value = new String("aValue");
domain.setLocalValue(value, context);
assertEquals("value stored in local context", value,
context.getAttribute(VARNAME, RuleContext.LOCAL_SCOPE));
assertEquals("value stored in local context", value, domain.getLocalValue(context));
}
/**
* Test the access to the property specified by this descriptor.
* For a domain, the property is an argument.
*/
public void testPropertyValue()
throws TransformationException
{
String VARNAME = "aVarName";
Domain domain = createDomain("src", 0, VARNAME, null);
String value = new String("aValue");
Arguments args = new ArrayArguments();
args.add(value);
contextImpl.setArguments(args);
// Try to get the value
Object res = domain.getPropertyValue(null, context); // Surrounding rule is not set
assertEquals("property value", value, res);
// Try to set the value
Object newValue = new String("aNewValue");
domain.setPropertyValue(null, newValue, context); // Surrounding rule is not set
res = domain.getPropertyValue(null, context); // Surrounding rule is not set
assertEquals("property value", newValue, res);
assertEquals("arguments out value", newValue, args.get(0));
}
/**
* Test the correct registration of the subproperties.
* Check if the domain is accessible from the subproperty.
* Check if the engine life cycle methods are called.
*/
public void testSubPropertyRegistration()
throws TransformationException
{
Domain domain = new Domain();
String value = new String("aValue");
Arguments args = new ArrayArguments();
args.add(value);
contextImpl.setArguments(args);
ConceptDescriptor property = new ConceptDescriptor();
domain.addSubPropertyDescriptor(property);
assertEquals("domain accessible from sub-property", domain, property.getDomain());
}
/**
* Test the creation of object of specified type.
*/
public void testCreate()
throws TransformationException, InstantiationException
{
String VARNAME = "aVarName";
Domain domain = createDomain("java", 0, VARNAME, MockBean.class.getName());
// Create a modelHelper, register it in a Transformation and in the context.
ModelHelper model = setUpJavaModel("java");
// Try to create the sub property
Object res = domain.create(context);
assertTrue("object type", res instanceof MockBean);
assertEquals("Created object stored in local context", res, context.getAttribute(VARNAME));
// The second create should return the object stored under VARNAME
Object res2 = domain.create(context);
assertTrue("object type", res2 instanceof MockBean);
assertEquals("Get object stored under varName", res, res2);
}
/**
* Test the creation of object of specified type, with subproperties.
*/
public void testComplexCreate()
throws TransformationException, InstantiationException
{
String VARNAME = "aVarName";
String PROPVARNAME = "p1";
// Create a modelHelper, register it in a Transformation and in the context.
ModelHelper model = setUpJavaModel("java");
Domain domain = createDomain("java", 0, VARNAME, MockBean.class.getName());
ConceptDescriptor property = createProperty("next", PROPVARNAME, MockBean.class.getName());
domain.addSubPropertyDescriptor(property);
// Try to create the sub property
Object res = domain.create(context);
assertTrue("object type", res instanceof MockBean);
assertEquals("Created object stored in local context", res, context.getAttribute(VARNAME));
Object createdProperty = ( (MockBean)res).getNext();
assertNotNull("object created", createdProperty);
assertTrue("object type", createdProperty instanceof MockBean);
assertEquals("Created property stored in local context", createdProperty,
context.getAttribute(PROPVARNAME));
}
/**
* Test the correct registration of the subproperties.
* Check if the domain is accessible from the subproperty.
* Check if the engine life cycle methods are called.
*/
public void testSubPropertyEngineStart()
throws EngineException, TransformationException
{
String methodName = ".engineStart()";
Domain domain = createDomainWithSubPropertyAndActions();
domain.engineStart(context);
assertTrue("trace", methodCallsTrace.remove("setUpAction"+methodName));
assertTrue("trace", methodCallsTrace.remove("enterGuard"+methodName));
assertTrue("trace", methodCallsTrace.remove("action"+methodName));
assertTrue("trace", methodCallsTrace.remove("exitGuard"+methodName));
assertEquals("trace", 0, methodCallsTrace.size());
}
/**
* Test the correct registration of the subproperties.
* Check if the domain is accessible from the subproperty.
* Check if the engine life cycle methods are called.
*/
public void testSubPropertyEngineFinnish()
throws TransformationException, EngineException
{
String methodName = ".engineFinish()";
Domain domain = createDomainWithSubPropertyAndActions();
domain.engineFinish(context);
assertTrue("trace", methodCallsTrace.remove("setUpAction"+methodName));
assertTrue("trace", methodCallsTrace.remove("enterGuard"+methodName));
assertTrue("trace", methodCallsTrace.remove("action"+methodName));
assertTrue("trace", methodCallsTrace.remove("exitGuard"+methodName));
assertEquals("trace", 0, methodCallsTrace.size());
}
/**
* Test the correct registration of the subproperties.
* Check if the domain is accessible from the subproperty.
* Check if the engine life cycle methods are called.
*/
public void testSubPropertyExecuteAction()
throws TransformationException
{
String methodName = ".execute()";
Domain domain = createDomainWithSubPropertyAndActions();
domain.executeAction(null, context);
assertTrue("trace", methodCallsTrace.remove("action"+methodName));
assertEquals("trace", 0, methodCallsTrace.size());
}
/**
* Test the correct registration of the subproperties.
* Check if the domain is accessible from the subproperty.
* Check if the engine life cycle methods are called.
*/
public void testSubPropertyIsConformToEnterGuard()
throws TransformationException
{
String methodName = ".isAllowed()";
Domain domain = createDomainWithSubPropertyAndActions();
domain.isConformToEnterGuard(null, context);
assertTrue("trace", methodCallsTrace.remove("enterGuard"+methodName));
assertEquals("trace", 0, methodCallsTrace.size());
}
/**
* Test the correct registration of the subproperties.
* Check if the domain is accessible from the subproperty.
* Check if the engine life cycle methods are called.
*/
public void testSubPropertyIsConformToExitGuard()
throws TransformationException
{
String methodName = ".isAllowed()";
Domain domain = createDomainWithSubPropertyAndActions();
domain.isConformToExitGuard(null, context);
assertTrue("trace", methodCallsTrace.remove("exitGuard"+methodName));
assertEquals("trace", 0, methodCallsTrace.size());
}
public void testEnterGuard()
throws TransformationException
{
Domain domain = createDomain( "src", 0, null, null, true) ;
String methodName = ".isAllowed()";
// Add a property to check that there is no interferences
domain.addSubPropertyDescriptor( createProperty("name", null, null));
domain.addEnterGuard(new GuardTrace("enterGuard"));
domain.addExitGuard(new GuardTrace("exitGuard"));
domain.isConformToEnterGuard(null, context);
assertTrue("trace", methodCallsTrace.remove("enterGuard"+methodName));
assertEquals("trace", 0, methodCallsTrace.size());
}
public void testExitGuard()
throws TransformationException
{
Domain domain = createDomain( "src", 0, null, null, false) ;
String methodName = ".isAllowed()";
// Add a property to check that there is no interferences
domain.addSubPropertyDescriptor( createProperty("name", null, null));
domain.addEnterGuard(new GuardTrace("enterGuard"));
domain.addExitGuard(new GuardTrace("exitGuard"));
domain.isConformToExitGuard(null, context);
assertTrue("trace", methodCallsTrace.remove("exitGuard"+methodName));
assertEquals("trace", 0, methodCallsTrace.size());
}
}