Package tests.jfun.yan.monitoring

Source Code of tests.jfun.yan.monitoring.MonitoredContainerTestCase

package tests.jfun.yan.monitoring;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;

import jfun.yan.ComponentInstantiationException;
import jfun.yan.Components;
import jfun.yan.Container;
import jfun.yan.monitoring.ComponentMonitor;
import jfun.yan.monitoring.ComponentMonitorQueue;
import jfun.yan.monitoring.CtorMonitor;
import jfun.yan.monitoring.MethodMonitor;
import jfun.yan.monitoring.MonitoringContainer;
import jfun.yan.monitoring.Monitors;
import junit.framework.TestCase;

public class MonitoredContainerTestCase extends TestCase {
  private static final Integer arg0 = new Integer(11);
  private static final String arg1 = "hello";
  private static final String arg2 = "world";
  public void testMonitoringContainer(){
    final BookmarkMonitor mon = new BookmarkMonitor(){
      private void verifyCtorArgs(Object[] args){
        assertEquals(2, args.length);
        assertEquals(arg0, args[0]);
        assertEquals(arg1, args[1]);
      }
      public void constructing(Constructor ctor, Object[] args) {
        // TODO Auto-generated method stub
        assertEquals(MonitoredObject.class, ctor.getDeclaringClass());
        verifyCtorArgs(args);
        super.constructing(ctor, args);
      }
      public void constructionFailed(Constructor ctor, Object[] args, Throwable e, long duration) {
        // TODO Auto-generated method stub
        assertEquals(MonitoredObject.class, ctor.getDeclaringClass());
        verifyCtorArgs(args);
        assertEquals(IllegalStateException.class, e.getClass());
        super.constructionFailed(ctor, args, e, duration);
      }
      private void verifyFactory(Object obj, Method mtd, Object[] args){
        assertNull(obj);
        assertEquals("instance",mtd.getName());
        assertEquals(3, args.length);
        assertEquals(arg0, args[0]);
        assertEquals(arg1, args[1]);
        assertEquals(arg0, args[0]);
        assertEquals(arg2, args[2]);
      }
      public void invoked(Object obj, Method mtd, Object[] args, Object result, long duration) {
        // TODO Auto-generated method stub
        verifyFactory(obj, mtd, args);
        final MonitoredObject mo = (MonitoredObject)result;
        assertEquals(arg0.intValue(), mo.getId());
        assertEquals(arg1, mo.getName());
        assertEquals(arg2, mo.getDesc());
        super.invoked(obj, mtd, args, result, duration);
      }
      public void invoking(Object obj, Method mtd, Object[] args) {
        // TODO Auto-generated method stub
        verifyFactory(obj, mtd, args);
        super.invoking(obj, mtd, args);
      }
    };
    testMonitor(mon, mon);
    final BookmarkMonitor mon1 = new BookmarkMonitor(){
      private void verifyCtorArgs(Object[] args){
        assertEquals(2, args.length);
        assertEquals(arg0, args[0]);
        assertEquals(arg1, args[1]);
      }
      public void constructing(Constructor ctor, Object[] args) {
        // TODO Auto-generated method stub
        assertEquals(MonitoredObject.class, ctor.getDeclaringClass());
        verifyCtorArgs(args);
        super.constructing(ctor, args);
      }
      public void constructionFailed(Constructor ctor, Object[] args, Throwable e, long duration) {
        // TODO Auto-generated method stub
        assertEquals(MonitoredObject.class, ctor.getDeclaringClass());
        verifyCtorArgs(args);
        assertEquals(IllegalStateException.class, e.getClass());
        super.constructionFailed(ctor, args, e, duration);
      }
    };
    final BookmarkMonitor mon2 = new BookmarkMonitor(){
      private void verifyFactory(Object obj, Method mtd, Object[] args){
        assertNull(obj);
        assertEquals("instance",mtd.getName());
        assertEquals(3, args.length);
        assertEquals(arg0, args[0]);
        assertEquals(arg1, args[1]);
        assertEquals(arg0, args[0]);
        assertEquals(arg2, args[2]);
      }
      public void invoked(Object obj, Method mtd, Object[] args, Object result, long duration) {
        // TODO Auto-generated method stub
        verifyFactory(obj, mtd, args);
        final MonitoredObject mo = (MonitoredObject)result;
        assertEquals(arg0.intValue(), mo.getId());
        assertEquals(arg1, mo.getName());
        assertEquals(arg2, mo.getDesc());
        super.invoked(obj, mtd, args, result, duration);
      }
      public void invoking(Object obj, Method mtd, Object[] args) {
        // TODO Auto-generated method stub
        verifyFactory(obj, mtd, args);
        super.invoking(obj, mtd, args);
      }
    };
    final HashMap marks = new HashMap();
    final CtorMonitor cmon = new CtorMonitor(){
      public void constructed(Constructor ctor, Object[] args, Object instance, long duration) {
        // TODO Auto-generated method stub
        marks.put("constructed", "b");
      }

      public void constructing(Constructor ctor, Object[] args) {
        // TODO Auto-generated method stub
        marks.put("constructing", "a");
      }

      public void constructionFailed(Constructor ctor, Object[] args, Throwable e, long duration) {
        // TODO Auto-generated method stub
        marks.put("constructionFailed", "c");
      }
    };
    final MethodMonitor mmon = new MethodMonitor(){

      public void invocationFailed(Object obj, Method mtd, Object[] args, Throwable err, long duration) {
        marks.put("invocationFailed", "z");
       
      }

      public void invoked(Object obj, Method mtd, Object[] args, Object result, long duration) {
        // TODO Auto-generated method stub
        marks.put("invoked", "y");
      }

      public void invoking(Object obj, Method mtd, Object[] args) {
        marks.put("invoking", "x");
       
      }
     
    };
    final ComponentMonitor mm = new ComponentMonitorQueue()
      .addCtorMonitor(mon1).addMethodMonitor(mon2)
      .addCtorMonitor(cmon).addMethodMonitor(mmon)
      .getComponentMonitor();
    testMonitor(mon1, mm);
    assertEquals("a", marks.get("constructing"));
    assertEquals("c", marks.get("constructionFailed"));
    assertNull(marks.get("constructed"));
    assertEquals("x", marks.get("invoking"));
    assertEquals("y", marks.get("invoked"));
    assertNull(marks.get("invocationFailed"));
    //testMonitor(mon2, mm);
    verifyMonitor(mon1);
    verifyMonitor(mon2);
  }
  private void verifyMonitor(BookmarkMonitor mon){
    assertTrue(mon.isConstructing());
    assertFalse(mon.isConstructed());
    assertTrue(mon.isConstructionFailed());
    assertTrue(mon.isInvoking());
    assertTrue(mon.isInvoked());
    assertFalse(mon.isInvocationFailed());
  }
  private void testMonitor(BookmarkMonitor mon, ComponentMonitor m){
    final Container yan = new MonitoringContainer(m);
    yan.registerValue(arg0);
    yan.registerValue(arg1);
    yan.registerConstructor("target", MonitoredObject.class);
    try{
      yan.getInstance("target");
      fail("should have failed");
    }
    catch(ComponentInstantiationException e){     
    }
    assertTrue(mon.isConstructing());
    assertFalse(mon.isConstructed());
    assertTrue(mon.isConstructionFailed());
    assertFalse(mon.isInvoking());
    yan.registerComponent("target",
        new Monitors(m).static_method(MonitoredObject.class, "instance")
        .withArgument(2, Components.value(arg2)));
    yan.getInstance("target");
    assertTrue(mon.isInvoking());
    assertTrue(mon.isInvoked());
    assertFalse(mon.isInvocationFailed());
  }
}
TOP

Related Classes of tests.jfun.yan.monitoring.MonitoredContainerTestCase

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.