/*
 * A program to test several aspects of the SimpleConfigurator. 
 * It DOES NOT test everything. It should be extended later to cover all the
 * methods and exceptions.
 * @author  Fabio Kon (f-kon@cs.uiuc.edu)
 * @version 0.2
 * @see     SimpleConfigurator
 */

import configuration.*;
import java.util.Vector;

public class test
{
  public static void main(String[] args)
  { 
    System.out.println ("starting test.");

    SimpleComponent a = new SimpleComponent ("Fabio");
    SimpleComponent b = new SimpleComponent ("Paula");
    System.out.println ("created simple components.");


    DependencyAttributes atribA = new DependencyAttributes ();
    DependencyAttributes atribB = new DependencyAttributes ();
    DependencyAttributes atribC = new DependencyAttributes ();
	
    atribA.setAttribute ("dedos", "5");
    atribA.setAttribute ("aneis", "nenhum");
    atribA.setAttribute ("forca", "pequena");

    atribB.setAttribute ("dedos", "5");
    atribB.setAttribute ("aneis", "2");
    atribB.setAttribute ("forca", "sobre-humana");

    atribC.setAttribute ("pontualidade", "boa");
    atribC.setAttribute ("prioridade", "maxima");

    System.out.println ("created attributes.");

    try {
      a.configurator.addHook ("LeftHand", atribA);
      a.configurator.addHook ("RightHand", null);
      b.configurator.addHook ("LeftHand", null);
      b.configurator.addHook ("RightHand", null);

      System.out.println ("added hooks.");
            
      a.configurator.hook ("LeftHand", b.configurator);
      b.configurator.registerClient (a.configurator, "LeftHand", null);

      b.configurator.hook ("RightHand", a.configurator, atribB);
      a.configurator.registerClient (b.configurator, "RightHand", atribC);
            
      System.out.println ("hooked components and registered clients.");
            
      // Now, let's print the list of hooks in the 2 components.

      System.out.println ("\n*********** HOOK INFORMATION ************");

      a.configurator.printHooks (System.out);
      b.configurator.printHooks (System.out);

      System.out.println ("Removing some atributes");
      atribA.removeAttribute ("dedos");
      atribA.removeAttribute ("forca");
      a.configurator.printHooks (System.out);

      try {
	System.out.println ("now, let's test an exception");
	atribA.removeAttribute ("pes");
      }
      catch (Exception e) {
	System.out.println("Caught the following expected exception: " +
			   e.toString ());
      }

      // Now, let's print the list of clients in the 2 components.

      System.out.println ("\n*********** CLIENT INFORMATION ************");
      
      a.configurator.printClients (System.out);
      b.configurator.printClients (System.out);


    }
    catch (Exception e) {
      System.out.println("Caught the following exception: " +
			 e.toString ());
    }
        
    System.out.println ("done.");
	
  }// main ()



  public static void printHooks (ComponentConfigurator cc)
  {
    Vector v = cc.listHooks ();
    
    int i, size = v.size();
    
    System.out.println ("Component " + cc.name() + ":");
    for (i = 0; i < size; i++)
      {
	System.out.print ("\thook = " + 
			  ((DependencySpecification)v.elementAt (i)).hook_);
	if (((DependencySpecification)v.elementAt (i)).component_ != null)
	  System.out.println (" is attached to component " + 
			      ((DependencySpecification)v.elementAt (i)).component_.name ());
	else
	  System.out.println (" is not attached to any component at this time.");
	
	// The following 2 lines could be condensed in a single line.
	// It's this way for pedagogical reasons.
	DependencySpecification ds = (DependencySpecification)v.elementAt (i);
	DependencyAttributes atribs = ds.attributes_;

	if (atribs == null)
	  System.out.println ("\tThis dependency has no attributes");
	else
	  {
	    System.out.println ("\tAttributes of this dependency:");

	    Vector atribVector = atribs.getAttributes ();

	    int j, atribSize = atribVector.size ();
	    
	    for (j = 0; j < atribSize; j++)
	      {
		System.out.println ("\t\tname = " + 
				    ((DependencyAttributes.NameValue)atribVector.elementAt(j)).name_ +
				    " value = "   + 
				    ((DependencyAttributes.NameValue)atribVector.elementAt(j)).value_);
	      }
	  }
      }
  }

}// class test


