Package modTransf.rules.core

Source Code of modTransf.rules.core.TransformationTestCase$ActionTrace

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.Transformation;
import modTransf.engine.SimpleMockTransformation;
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.model.java.JavaObjectModelHelper;
import modTransf.engine.Rule;
import modTransf.engine.ParameterDescriptor;

/**
* Base class for Test on Transformation, Rule, Domain, ...
*/
public class TransformationTestCase extends TestCase
{

  /** A list of called methods, setted by the rule handler */
protected List methodCallsTrace = new ArrayList();

  /**
   * Map of objects created in handlers.
   */
  protected Map objectTrace = new HashMap();
  /**
   * The context being tested.
   */
  protected RuleContext context;
  /**
   * The context being tested.
   */
  protected SimpleRuleContext contextImpl;

  protected Transformation transformation;
  protected SimpleMockTransformation transformationImpl;


  protected String attr0 = new String("attr0");
  protected String attr1 = new String("attr1");
  protected String attr2 = new String("attr2");
  protected String attr3 = new String("attr3");

  protected String attr0Name = "attr0";
  protected String attr1Name = "attr1";
  protected String attr2Name = "attr2";
  protected String attr3Name = "attr3";

  /**
   * Constructor.
   * @param name String
   */
  public TransformationTestCase(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
  {
    // create context.
    contextImpl = new SimpleRuleContext();
    context = contextImpl;

    // reset traces
    methodCallsTrace.clear();
    objectTrace.clear();
  }

  /**
   * Tear down the test.
   * This method is called after each test.
   */
  public void tearDown()
  {
   context = null;
  }

  /**
   * Initialize a Java modelHelper.
   *
   * @param modelName String
   */
  public ModelHelper setUpJavaModel(String modelName)
  {
    // Add a model 'java'
    ModelHelper javaModel = new JavaObjectModelHelper();

    setUpTransformation();
    transformationImpl.getModels().registerModel(modelName, javaModel);
    return javaModel;
  }

  /**
   * Create a Transformation object and register it to the context.
   */
  public void setUpTransformation()
  {
    if( transformationImpl != null )
      return;

    transformationImpl = new SimpleMockTransformation();
    transformation = transformationImpl;
    contextImpl.setTransformation(transformation);
  }

  public void tearDownTransformation()
  {
    transformationImpl = null;
    transformation = null;
  }

  /**
   * Register the trace.
   * @param name String
   */
  protected void registerTrace( String name )
  {
    methodCallsTrace.add(name);
  }

  /**
   * Register the object.
   * @param name String
   */
  protected void registerObject( String name, Object object )
  {
    objectTrace.put(name, object);
  }

  /**
   * Create a simple rule.
   *
   * @param ruleName String
   * @param argDirections String
   * @param result The result returned by the rule when invoked.
   * @return Rule
   */
  protected Rule createSimpleRule( String ruleName, String argDirections, boolean result )
  {
    return new SimpleRule(ruleName, argDirections, result);
  }

  /**
   * Create a domain object.
   * @param model String
   * @param index int
   * @param varName String
   * @param type String
   * @return Domain
   */
  protected Domain createDomain(String model, int index, String varName, String type)
  {
    Domain domain = new Domain();
    domain.setModelName(model);
    domain.setVarName(varName);
    domain.setArgIndex(index);
    domain.setType(type);
    return domain;
  }

  /**
   * Create a domain object.
   * @param model String
   * @param index int
   * @param varName String
   * @param type String
   * @return Domain
   */
  protected Domain createDomain(String model, int index, String varName,
                                String type, boolean isSource)
  {
    Domain domain = new MockDomain(isSource);
    domain.setModelName(model);
    domain.setVarName(varName);
    domain.setArgIndex(index);
    domain.setType(type);
    return domain;
  }

  /**
   * Create a property.
   */
  public ConceptDescriptor createProperty(String propertyName, String varName, String type)
    throws TransformationException
  {
    ConceptDescriptor property = new ConceptDescriptor();
    property.setPropertyName(propertyName);
    property.setVarName(varName);
    property.setType(type);
    return property;
  }

  /**
   * Create a domain with a subproperty containing various actions for testing.
   */
  public Domain createDomainWithSubProperty(String propertyName, String varName, int index)
    throws TransformationException
  {
    Domain domain = createDomain( propertyName, index, varName, null);

    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;
  }


  /**
   * An base action registering with methods registering trace.
   * <p>Titre : ModTransf V3</p>
   * <p>Description : </p>
   * <p>Copyright : Copyright (c) 2005</p>
   * <p>Soci�t� : </p>
   * @author Cedric Dumoulin
   * @version 3.0
   */
  protected class ActionTrace implements ActionClause
{
   String name;

   public ActionTrace( String name)
   {
     this.name = name;
   }
    /**
     * engineFinish
     *
     * @param context RuleContext
     */
    public void engineFinish(RuleContext context)
    {
      registerTrace( name + ".engineFinish()");
    }

    /**
     * engineStart
     *
     * @param context RuleContext
     */
    public void engineStart(RuleContext context)
    {
      registerTrace( name + ".engineStart()");
    }

    /**
     * execute
     *
     * @param bean Object
     * @param request RuleContext
     * @return Object
     */
    public Object execute(Object bean, RuleContext request)
    {
      registerTrace( name + ".execute()");
      return "";
    }

    /**
     * setUpLocalVariables
     *
     * @param object Object
     * @param context RuleContext
     */
    public void setUpLocalVariables(Object object, RuleContext context)
    {
      registerTrace( name + ".setUpLocalVariables()");
    }

  }

  /**
   *
   * <p>Titre : ModTransf V3</p>
   * <p>Description : </p>
   * <p>Copyright : Copyright (c) 2005</p>
   * <p>Soci�t� : </p>
   * @author Cedric Dumoulin
   * @version 3.0
   */
  protected class GuardTrace extends ActionTrace implements GuardClause
  {
    public GuardTrace(String name)
    {
      super(name);
    }

    /**
     * isAllowed
     *
     * @param bean Object
     * @param request RuleContext
     * @return boolean
     */
    public boolean isAllowed(Object bean, RuleContext request)
    {
      registerTrace( name + ".isAllowed()");
      return true;
    }

  }

  protected class MockDomain extends Domain
  {
    private boolean isSource;
    public MockDomain( boolean isSource )
    {
      this.isSource = isSource;
    }
    public boolean isSourceDomain( RuleContext context )
      {
        return isSource;
      }

      public boolean isTargetDomain( RuleContext context )
        {
          return !isSource;
        }
  }

  /**
   *
   * <p>Titre : ModTransf V3</p>
   * <p>Description : </p>
   * <p>Copyright : Copyright (c) 2005</p>
   * <p>Soci�t� : </p>
   * @author Cedric Dumoulin
   * @version 3.0
   */
  public class SimpleRule implements Rule {

    protected String name;
    private List parameterDescriptors = new ArrayList();
    private boolean result=true;

    /**
     * Constructor.
     * @param name String
     */
    public SimpleRule( String name )
    {
      this.name =name;
    }

    /**
      * Constructor.
      * @param name String
      */
     public SimpleRule( String name, String directions )
     {
       this(name, directions, true);
     }

    /**
     * Constructor.
     *
     * @param name String
     * @param argsDirection String
     * @param result The result returned when the rule is invoked.
     */
    public SimpleRule( String name, String argsDirection, boolean result )
    {
      this.name =name;
      this.result = result;
      String dirs[] = argsDirection.split(",");
      for( int i=0; i<dirs.length; i++)
      {
        String dir = dirs[i].trim();

        if( "in".equalsIgnoreCase( dir ) )
          parameterDescriptors.add( new SimpleParameterDescriptor(true, false) );
        else if( "out".equalsIgnoreCase( dir ) )
          parameterDescriptors.add( new SimpleParameterDescriptor(false, true) );
        else if( "inout".equalsIgnoreCase( dir ) )
          parameterDescriptors.add( new SimpleParameterDescriptor(true, true) );
        else
          throw new IllegalArgumentException("Direction '" + dir + "' not recognize.");

      }

    }
    /**
     * execute
     *
     * @param args Arguments
     * @param context RuleContext
     * @return boolean
     */
    public boolean execute(Arguments args, RuleContext context)
    {
      registerTrace( name + ".execute()");
      registerObject( name + ".execute().args", args);
      return result;
    }

    /**
     * getParameterDescriptors
     *
     * @return List
     */
    public List getParameterDescriptors()
    {
      //registerTrace( name + ".getParameterDescriptors()");
      return parameterDescriptors;
    }

    public void addParameterDescriptor( ParameterDescriptor desc )
    {
      parameterDescriptors.add(desc);
    }

    /**
     * getRuleName
     *
     * @return String
     */
    public String getRuleName()
    {
      return name;
    }

    /**
     * isAllowed
     *
     * @param args Arguments
     * @param context RuleContext
     * @return boolean
     */
    public boolean isAllowed(Arguments args, RuleContext context)
    {
      registerTrace( name + ".isAllowed()");
      return result;
    }

  }

  public class SimpleParameterDescriptor implements ParameterDescriptor
  {
    private boolean isIn;
    private boolean isOut;

    public SimpleParameterDescriptor( boolean isIn, boolean isOut )
    {
      this.isIn = isIn;
      this.isOut = isOut;
    }

    /**
     * isIn
     *
     * @param context RuleContext
     * @return boolean
     */
    public boolean isIn(RuleContext context)
    {
      return isIn;
    }

    /**
     * isOut
     *
     * @param context RuleContext
     * @return boolean
     */
    public boolean isOut(RuleContext context)
    {
      return isOut;
    }

  }

}
TOP

Related Classes of modTransf.rules.core.TransformationTestCase$ActionTrace

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.