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

Java Open Source » Byte Code » PROSE 
PROSE » ch » ethz » prose » engine » JoinPointManagerTest.java
// $Id: JoinPointManagerTest.java,v 1.3 2004/05/12 17:26:52 anicoara Exp $
// =====================================================================
//
// (history at end)
//

package ch.ethz.prose.engine;

// used packages
import java.util.*;

import junit.framework.*;
import ch.ethz.inf.iks.jvmai.jvmdi.MethodExecutionJoinPointImpl;
import ch.ethz.jvmai.*;

/**
 * JUnit testcase for class JoinPointManager (WhiteBox).
 *
 * @version  $Revision: 1.3 $
 * @author  Andrei Popovici
 */
public class JoinPointManagerTest extends TestCase {
  
  static class TestListener extends JoinPointListener
  {
    public List receivedEvents = new Vector();
    public void joinPointReached(FieldAccessJoinPoint e)
    {
      receivedEvents.add(e);
    }
    
    public void joinPointReached(FieldModificationJoinPoint e)
    {
      receivedEvents.add(e);
    }
    
    public void joinPointReached(MethodEntryJoinPoint e)
    {
      receivedEvents.add(e);
    }
    
    public void joinPointReached(MethodExitJoinPoint e)
    {
      receivedEvents.add(e);
    }
    
    public void joinPointReached(ExceptionJoinPoint e)
    {
      receivedEvents.add(e);
    }
    
    public void joinPointReached(ExceptionCatchJoinPoint e)    
    {
      receivedEvents.add(e);
    }
    
    public void joinPointReached(ConstructorJoinPoint e)    
    {
      receivedEvents.add(e);
    }
  }
  
  
  static class TestRequest extends JoinPointRequest
  {
    public String getKind()
    {
      return "";
    }
    public int getMask()
    {
      return 0;
    }
    long mySignature;
    public TestRequest(long signature)
    {
      super(null);
      mySignature = signature;
    }
    protected void setWatch(Object listeners) { }
    protected void clearWatch() { }
    public boolean equals(Object other) { return ((other instanceof TestRequest) && (((TestRequest)other).mySignature==mySignature)); }
    public int hashCode() { return (int)mySignature; }
  }
  
  /**
   * Construct test with given name.
   * @param name test name
   */
  public JoinPointManagerTest(String name)
  {
    super(name);
  }
  
  
  class VisibleJoinPointManager extends JoinPointManager
  {
    public VisibleJoinPointManager(boolean ic, JVMAspectInterface ai, boolean rev) {super(ic,ai,rev);}
    public Map getReq2listener()
    {  return req2listener; }
    public Map getListener2Req()
    { return listener2req; }
  }
  
  VisibleJoinPointManager jpMgr = null;
  
  JoinPointRequest  jpr1    = null;
  JoinPointRequest  jpr2    = null;
  JoinPointRequest  jpr3    = null;
  JoinPointListener jpList1 = null;
  JoinPointListener jpList2 = null;
  MethodEntryJoinPoint  jp1 = null;
  MethodEntryJoinPoint  jp2 = null;
  static class TestMethodEntryJoinPoint  extends MethodExecutionJoinPointImpl implements MethodEntryJoinPoint
  {
    ListenerList llist;
    protected TestMethodEntryJoinPoint(ListenerList l)
    {
      super(null,null);
      llist=l;
    }
    
    public Object getAopTag()
    {
      return llist;
    }
    
    public boolean equals(Object other)
    {
      if (other instanceof TestMethodEntryJoinPoint)
        return true;
      else
        return false;
    }
    
    public int hashCode()
    {
      return 0xDEADBEEF;
    }
  };
  
  /**
   * Set up fixture.
   */
  protected void setUp()
  {
    jpMgr   = new VisibleJoinPointManager(false, null,  true);  // isConnected == false because not connectedToJVMAI!
    jpr1    = new TestRequest(1000);
    jpr2    = new TestRequest(2000);
    jpr3    = new TestRequest(1000);
    jpList1 = new TestListener();
    jpList2 = new TestListener();
    
    // we register the following tupels:
    // (jpr1,jpList1)
    // (jpr2,jpList2)
    // (jpr3,jpList2)
    // the result shoud be
    // jpr1,jpr3 -> listener 1,2
    // jpr2      -> listener 2
    // we expect to have a map of length 2 (sig2listeners)
    jpMgr.registerListener(jpList1,jpr1);
    jpMgr.registerListener(jpList2,jpr2);
    jpMgr.registerListener(jpList2,jpr3);
    
    
    jp1 = new TestMethodEntryJoinPoint((ListenerList)((VisibleJoinPointManager)jpMgr).getReq2listener().get(jpr1));
    jp2 = new TestMethodEntryJoinPoint((ListenerList)((VisibleJoinPointManager)jpMgr).getReq2listener().get(jpr2));
  }
  
  public void testEvents()
  {
    // check whether the test-classes created for this test are ok..
    assertTrue(jpr1.equals(jpr3));
    assertTrue(!jpr1.equals(jpr2));
  }
  
  public void testRegisterListener()
  {
    assertTrue(((VisibleJoinPointManager)jpMgr).getReq2listener().size() == 2);
    assertTrue(((VisibleJoinPointManager)jpMgr).getListener2Req().size() == 2);
    assertTrue(((Set)((VisibleJoinPointManager)jpMgr).getListener2Req().get(jpList2)).size() == 2);
  }
  
  
  
  public void testNotifyListeners()
  {
    // we notify classes with jpe1; we expect the listeners 1 & 2 to be notified
    // (to contain at least one event this event should be the same);
    jpMgr.onMethodEntry(jp1);
    assertEquals(((TestListener)jpList1).receivedEvents,((TestListener)jpList2).receivedEvents);
    assertTrue(((TestListener)jpList1).receivedEvents.size() > 0);
    
    // clean the listeners's log
    ((TestListener)jpList1).receivedEvents = new Vector();
    ((TestListener)jpList2).receivedEvents = new Vector();
    
    // we notify with jpe2; only listener 2 should be notified
    jpMgr.onMethodEntry(jp2);
    assertTrue(((TestListener)jpList1).receivedEvents.isEmpty());
    assertTrue(!((TestListener)jpList2).receivedEvents.isEmpty());
    assertTrue(((TestListener)jpList2).receivedEvents.get(0).equals(jp2));
  }
  
  public void testUnregisterListeners()
  {
    // we deregister listener 1;
    jpMgr.unregisterListener(jpList2);
    
    // we expect the length of the sig2listener mapping to be 1;
    assertTrue(((VisibleJoinPointManager)jpMgr).getReq2listener().size() == 1);
    assertTrue(((VisibleJoinPointManager)jpMgr).getListener2Req().size() == 1);
    assertTrue(((Set)((VisibleJoinPointManager)jpMgr).getListener2Req().get(jpList2)) == null);
    
    // we expect that the only listener remaining is listener 1 listening on
    // events with signature 1000
    // notifyEvents
    jpMgr.onMethodEntry(jp1);
    assertTrue(!((TestListener)jpList1).receivedEvents.isEmpty());
    assertTrue(((TestListener)jpList2).receivedEvents.isEmpty());
  }
  
  /**
   * Test suite.
   * @return test instance
   */
  public static Test suite()
  {
    return new TestSuite(JoinPointManagerTest.class);
  }
}

//======================================================================
//
// $Log: JoinPointManagerTest.java,v $
// Revision 1.3  2004/05/12 17:26:52  anicoara
// Adapt Junit tests to 3.8.1 version and the new package structure
//
// Revision 1.1.1.1  2003/07/02 15:30:43  apopovic
// Imported from ETH Zurich
//
// Revision 1.4  2003/07/02 12:42:35  anicoara
// Added CatchJoinPoint Functionality (Requests, Join-Points, Filters, CatchCuts, Tests)
//
// Revision 1.3  2003/05/20 16:04:55  popovici
//
// New QueryManager replaces functionality in AspectManager (better Soc)
// New 'Surrogate' classes for usage in the QueryManager
// The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
//
// Revision 1.2  2003/05/05 17:46:16  popovici
// Refactorization step (runes->prose) cleanup
//
// Revision 1.1  2003/05/05 14:03:30  popovici
// renaming from runes to prose
//
// Revision 1.12  2003/04/17 15:15:18  popovici
// Extension->Aspect renaming
//
// Revision 1.11  2003/04/17 12:49:44  popovici
// Refactoring of the crosscut package
//  ExceptionCut renamed to ThrowCut
//  McutSignature is now SignaturePattern
//
// Revision 1.10  2003/04/17 08:46:54  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.9  2003/03/04 18:36:42  popovici
// Organization of imprts
//
// Revision 1.8  2003/03/04 11:26:05  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.7  2002/11/26 17:15:46  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.6  2002/10/31 18:26:47  pschoch
// Capability of crosscutting Exceptions added to prose.
//
// Revision 1.5  2002/10/25 07:42:29  popovici
// Undo Chnages Phillippe
//
// Revision 1.3  2002/03/12 09:49:33  popovici
// Join Point listener now abstract class (performance reasons)
//
// Revision 1.2  2002/02/21 13:03:07  popovici
// Updated to new performance-optimized design: Crosscuts receive joinpoints, no Event notification, etc
//
// Revision 1.1  2002/02/05 11:16:16  smarkwal
// AbstractJoinPointManagerTest renamed to JoinPointManagerTest and changed to support new JVMAI-based JoinPointManager
//
// Revision 1.1.1.1  2001/11/29 18:13:33  popovici
// Sources from runes
//
// Revision 1.1.2.2  2001/11/22 09:42:25  popovici
// Indentation, minor non-semantic changes, message improvements,
// method name harmonization
//
// Revision 1.1.2.1  2000/10/30 15:58:15  popovici
// Initial Revision; Renaming  from 'JoinPointMangerImpl'
//
// Revision 1.1.2.2  2000/10/24 18:10:13  popovici
// Minor documentation fix.
//
// Revision 1.1.2.1  2000/10/18 19:33:48  popovici
// Useless System.err.println removed.
//
// Revision 1.1  2000/10/16 11:42:53  popovici
// 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.