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

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

package ch.ethz.prose;

// used packages
import junit.framework.*;

import java.lang.reflect.*;

import ch.ethz.inf.iks.jvmai.jvmdi.HotSwapClassWeaver;
import ch.ethz.inf.iks.jvmai.jvmdi.HotSwapFieldWeaver;
import ch.ethz.inf.iks.jvmai.jvmdi.HotSwapAspectInterfaceImpl;
import ch.ethz.jvmai.*;
import ch.ethz.prose.crosscut.*;
import ch.ethz.prose.filter.*;

/**
 * JUnit testcase for Constructor join points (HotSwap advice weaving).
 *
 * @version     $Revision$
 * @author      Gerald Linhofer
 * @author    Angela Nicoara
 */
public class HotSwapConstructorJoinPointTest extends TestCase  {
  
  private HotSwapAspectInterfaceImpl aspectInterface;
  
  /**
   * Construct test with given name.
   * @param name test name
   */
  public HotSwapConstructorJoinPointTest(String name)
  {
    super(name);
  }
  
  /**
   * Set up fixture.
   */
  protected  void setUp()  {
    aspectInterface = HotSwapAspectInterfaceImpl.getInstance();
    aspectInterface.startup(new String[0], true);
  }
  
  /**
   * Clean up.
   */
  protected  void tearDown() {
    aspectInterface.teardown();
  }
  
  
  class MyClass1 {
    MyClass1(){
      super(); 
    }
  }
  
  class MyClass2 {
    String str;
    MyClass2(String s) {
      str = s;
    }
  }
  
  static class TestHook extends JoinPointHook {
    public int count; 
    public void reset() { count = 0; }
    public void onFieldAccess      (FieldAccessJoinPoint       joinPoint) { }
    public void onFieldModification(FieldModificationJoinPoint joinPoint) { }
    public void onMethodEntry      (MethodEntryJoinPoint       joinPoint) { }
    public void onMethodExit       (MethodExitJoinPoint        joinPoint) { }
    public void onExceptionThrow   (ExceptionJoinPoint         joinPoint) { }
    public void onExceptionCatch   (ExceptionCatchJoinPoint     joinPoint) { } 
    public void onClassLoad        (Class cls)                            { }
    public void onConstructor      (ConstructorJoinPoint joinPoint) { count++; }
  }
  
  static class TestHook2 extends JoinPointHook {
    public int count; 
    public void reset() { count = 0; }
    public void onFieldAccess      (FieldAccessJoinPoint       joinPoint) { }
    public void onFieldModification(FieldModificationJoinPoint joinPoint) { }
    public void onMethodEntry      (MethodEntryJoinPoint       joinPoint) { }
    public void onMethodExit       (MethodExitJoinPoint        joinPoint) { }
    public void onExceptionThrow   (ExceptionJoinPoint         joinPoint) { }
    public void onExceptionCatch   (ExceptionCatchJoinPoint     joinPoint) { } 
    public void onClassLoad        (Class cls)                            { }
    public void onConstructor      (ConstructorJoinPoint joinPoint) {
      joinPoint.setArg(0,"testHook2 was here");
    }
  }
  
  
  public void test_040_aspectInterface() {
    Object aopTag = new Object();
    Constructor c = MyClass1.class.getDeclaredConstructors()[0];
    
    TestHook testHook = new TestHook();
    testHook.reset();
    aspectInterface.setJoinPointHook( testHook );
    
    aspectInterface.suspendNotification( Thread.currentThread() );
    aspectInterface.setConstructorWatch( c, aopTag );
    aspectInterface.resumeNotification( Thread.currentThread() );
    
    // normal notification
    new MyClass1(); 
    assertEquals("onConstructor  notification", 1, testHook.count );
    
    // setWatch exceptions
    int exceptionCount = 0;
    try { aspectInterface.setConstructorWatch( null, aopTag ); }
    catch( NullPointerException e) { exceptionCount++; }
    try { aspectInterface.setConstructorWatch( c, null ); }
    catch( IllegalArgumentException e) { exceptionCount++; }
    try { aspectInterface.setConstructorWatch( c, aopTag ); }
    catch( WatchAlreadySetException e) { exceptionCount++; }
    assertEquals("number of exceptions ", 3, exceptionCount );
    
    // suspended notification
    aspectInterface.suspendNotification( Thread.currentThread() );
    new MyClass1();
    assertEquals("onConstructor suspended notification", 1, testHook.count );
    
    // clear watch
    aspectInterface.clearConstructorWatch( c );
    aspectInterface.resumeNotification( Thread.currentThread() );
    new MyClass1();
    assertEquals("clear constructor", 1, testHook.count );
    
    // change argument
    TestHook2 th2 = new TestHook2();
    aspectInterface.suspendNotification( Thread.currentThread() );
    aspectInterface.setConstructorWatch( MyClass2.class.getDeclaredConstructors()[0], aopTag );
    aspectInterface.setJoinPointHook( th2 );
    aspectInterface.resumeNotification( Thread.currentThread() );
    MyClass2 mc2 = new MyClass2("unmodified");
    assertEquals("modify argument", "testHook2 was here", mc2.str);
  }
  
  public class MyAspect1 extends DefaultAspect {
    
    int invokationCounter = 0;
    
    public Crosscut c1 = new ConstructorCut() {
      public void METHOD_ARGS(MyClass1 x, REST y) {
        invokationCounter++;
      }
      
      protected PointCutter pointCutter() {
        return Within.type("MyClass1.*");
      }
    };
  }
  
  public class MyAspect2 extends DefaultAspect {
    
    int invokationCounter = 0;
    String lastArg;
    
    public Crosscut c1 = new ConstructorCut() {
      public void METHOD_ARGS(MyClass2 x, String i, REST y) {
        lastArg = i;
        invokationCounter++;
      }
      
      protected PointCutter pointCutter() {
        return Within.type("MyClass2.*");
      }
    };
  }
  
  private String stackframes2string(Exception e) {
    StringBuffer sb = new StringBuffer();
    StackTraceElement sf[] = e.getStackTrace();
    for( int i = 0; i < sf.length; i++ ) {
      sb.append( "\n   " + sf[i].toString() );
    }
    return sb.toString();
  }
  
  public void test_050_aspectManager() {
    MyClass1 mc1 = null;
    MyClass2 mc2 = null;
    try {
      ProseSystem.startup();
      
      // Constructor without arguments
      MyAspect1 asp1 = new MyAspect1();
      ProseSystem.getAspectManager().insert( asp1 );
      for( int i = 0; i < 100; i++ ) {
        mc1 = new MyClass1();
      }
      assertEquals("num of constructor invokations", 100, asp1.invokationCounter);
      ProseSystem.getAspectManager().withdraw( asp1 );
      
      // Constructor with an argument
      MyAspect2 asp2 = new MyAspect2();
      ProseSystem.getAspectManager().insert( asp2 );
      for( int i = 0; i < 100; i++ ) {
        mc2 = new MyClass2( String.valueOf(i) );
        assertEquals("constructor arg modified", i, Integer.parseInt(asp2.lastArg) );
      }
      assertEquals("num of constructor invokations with arg", 100, asp2.invokationCounter);
      ProseSystem.getAspectManager().withdraw( asp2 );
      
    } catch(Exception e) { fail(e.getClass().getName() + ": " + e.getMessage() + /*" ->" + e.getStackTrace()[0].toString() */ "  trace: " + stackframes2string(e)); }
    try{ ProseSystem.teardown(); }
    catch(SystemTeardownException e) {}
  }
  
  /**
   * Test suite.
   * @return test instance
   */
  public static Test suite()
  {
    return new TestSuite(HotSwapConstructorJoinPointTest.class);
  }
}

//======================================================================
//
// $Log$
//
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.