FieldNotificationTestWithOutput.java :  » Byte-Code » PROSE » ch » ethz » prose » Java Open Source

Java Open Source » Byte Code » PROSE 
PROSE » ch » ethz » prose » FieldNotificationTestWithOutput.java
// $Id $
// =====================================================================
//
// (history at end)
//

package ch.ethz.prose;

// used packages
import java.lang.reflect.Field;

import junit.framework.*;
import ch.ethz.jvmai.FieldAccessJoinPoint;
import ch.ethz.jvmai.FieldModificationJoinPoint;
import ch.ethz.prose.crosscut.*;
import ch.ethz.prose.filter.PointCutter;


/**
 * FieldNotificationTestWithOutput. This is almost the same as
 * <code>FieldNotificationTest</code>, but this one produces output
 * rather than doing some assertion tests to allow the user to see how
 * the field access and modification mechanism actually works.
 *
 * @version  $Revision: 1.2 $
 * @author Gerard Roos
 */
public class FieldNotificationTestWithOutput extends TestCase {
  
  static class TestClass {
    int initial = 1;
    int inConstr;
    int undefined;
    
    int[] array;
    
    Object o = null;
    
    public TestClass() {
      inConstr = 2;
    }
    
  }
  
  public static class TestExtension extends DefaultAspect {
    
    public Crosscut c1 = new GetCut()
    {
      public void GET_ARGS()
      {
        FieldAccessJoinPoint theEv = (FieldAccessJoinPoint)thisJoinPoint();
        Field f = theEv.getField();
        
        Object owner = theEv.getTarget();
        
        try
        {
          System.out.println("READ ACCESS");
          System.out.println("class: " + owner.getClass().getName());
          System.out.println("owner: " + owner);
          System.out.println("fieldname: " + f.getName());
          System.out.println("value: " + f.get(owner));
          System.out.println();
        }
        catch ( Exception e )
        {
          e.printStackTrace();
        }
      }
      
      protected PointCutter pointCutter()
      { return null;}
      
    };
    
    public Crosscut c = new SetCut()
    {
      public void SET_ARGS()
      {
        try
        {
          FieldModificationJoinPoint fme = (FieldModificationJoinPoint)thisJoinPoint();
          
          System.out.println("MODIFICATION ACCESS");
          System.out.println("class: " + fme.getTarget().getClass().getName());
          System.out.println("owner: " + fme.getTarget());
          System.out.println("fieldname: " + fme.getField().getName());
          System.out.println("old value: " + fme.getField().get(fme.getTarget()));
          System.out.println("new value: " + fme.getNewValue());
          System.out.println();
        }
        catch ( Exception e )
        {
          e.printStackTrace();
        }
      }
      
      protected PointCutter pointCutter()
      { return null;}
      
      
      // restrict to TestClass
      protected Class[] potentialCrosscutClasses() {
        Class[] result = {TestClass.class};
        return result;
      }
    };
  };
  
  TestClass test;
  
  /**
   * Construct test with given name.
   * @param name test name
   */
  public FieldNotificationTestWithOutput(String name) {
    super(name);
  }
  
  /**
   * Set up fixture.
   */
  protected void setUp() {
    try {
      ProseSystem.startup();
    }
    catch ( Exception e ) {
      Assert.fail("ProseSystem.startup() failed.");
    }
  }
  
  protected void tearDown() throws SystemTeardownException {
    try {
      ProseSystem.teardown();
    }
    catch ( Exception e ) {
      Assert.fail("ProseSystem.teardown() failed.");
    }
  }
  
  public void testInstantiation() throws Exception {
    
    int[] arr = {1,2,3,4,5};
    
    System.out.println("\n\ncreate new instance...\n");
    ProseSystem.getAspectManager().insert(new TestExtension());
    test = new TestClass();
    
    System.out.println("\naccess the fields...\n");
    test.undefined = -5;
    test.o = new Object();
    test.array = arr;
    arr[0] = 0;
    test.array = arr;
    test.array[1] = 9;
    test.array[3] = 1;
    test.array[4] = 10;
  }
  
  /**
   * Test suite.
   * @return test instance
   */
  public static Test suite() {
    return new TestSuite(FieldNotificationTestWithOutput.class);
  }
  
}

//======================================================================
//
// $Log: FieldNotificationTestWithOutput.java,v $
// Revision 1.2  2004/05/12 17:26:51  anicoara
// Adapt Junit tests to 3.8.1 version and the new package structure
//
// Revision 1.1.1.1  2003/07/02 15:30:42  apopovic
// Imported from ETH Zurich
//
// Revision 1.1  2003/05/05 14:02:30  popovici
// renaming from runes to prose
//
// Revision 1.12  2003/04/27 13:08:40  popovici
// Specializers renamed to PointCutter
//
// Revision 1.11  2003/04/17 15:15:02  popovici
// Extension->Aspect renaming
//
// Revision 1.10  2003/04/17 12:49:39  popovici
// Refactoring of the crosscut package
//  ExceptionCut renamed to ThrowCut
//  McutSignature is now SignaturePattern
//
// Revision 1.9  2003/04/17 08:46:44  popovici
// Important functionality additions
//  - Cflow specializers
//  - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
//  - Transactional capabilities
//  - Total refactoring of Specializer evaluation, which permits fine-grained distinction
//    between static and dynamic specializers.
//  - Functionality pulled up in abstract classes
//  - Uniformization of advice methods patterns and names
//
// Revision 1.8  2003/03/04 18:36:09  popovici
// Organization of imprts
//
// Revision 1.7  2003/03/04 11:25:57  popovici
// Important refactorization step (march):
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
//   structures
//
// Revision 1.6  2002/11/26 17:15:31  pschoch
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
// ProseSystem now owns and starts the Aspect interface.
// ProseSystem now containes  a 'test' AspectManager
// AspectManager now owns the JoinPointManager.
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
// Documentation updated accordingly.
//
// Revision 1.5  2002/06/06 14:39:53  popovici
// Renamings: FunctionalCrosscut->MethodCut
// AllFields->SetCut
// SetCu.fieldModiticationAdvice -> SetCut.setAdvice
//
// Revision 1.4  2002/06/06 12:01:47  popovici
// fieldAccessAdvice removed from AllFields; tests and usage of AllFields's
// ability to intercept gets moved to 'GetCut'
// Minor bug fixes;
//
// Revision 1.3  2002/06/05 12:03:49  popovici
// thisJoinPoint() updated everywhere. The 'fieldModificationAdvice is now parameterless'; older implemnentations now
// use 'thisJoinPoint()'
//
// Revision 1.2  2002/02/05 11:20:43  smarkwal
// modifications to test JVMAI-based implementation
//
// Revision 1.1.1.1  2001/11/29 18:13:30  popovici
// Sources from runes
//
// Revision 1.1.2.2  2001/02/22 16:52:23  popovici
// ProseSystem.setup replaced with startup; teardown introduced
//
// Revision 1.1.2.1  2001/01/17 21:25:47  groos
// initial revision.
//
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.