[jboss-cvs] JBossAS SVN: r58684 - projects/aop/trunk/aop/src/test/org/jboss/test/aop/field

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Nov 27 09:12:01 EST 2006


Author: kabir.khan at jboss.com
Date: 2006-11-27 09:11:59 -0500 (Mon, 27 Nov 2006)
New Revision: 58684

Added:
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/FieldPerJoinpointInterceptor.java
Modified:
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/FieldTestCase.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/POJO.java
Log:
Add a test for Ben to see how to use instanceadvisor to get hold of the aspects

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/FieldPerJoinpointInterceptor.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/FieldPerJoinpointInterceptor.java	2006-11-27 14:11:22 UTC (rev 58683)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/FieldPerJoinpointInterceptor.java	2006-11-27 14:11:59 UTC (rev 58684)
@@ -0,0 +1,54 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/ 
+package org.jboss.test.aop.field;
+
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.joinpoint.FieldInvocation;
+import org.jboss.aop.joinpoint.Invocation;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class FieldPerJoinpointInterceptor implements Interceptor
+{
+   public static FieldPerJoinpointInterceptor last;
+   public String getName()
+   {
+      return this.getClass().getName();
+   }
+
+   public Object invoke(Invocation invocation) throws Throwable
+   {
+      if (!(invocation instanceof FieldInvocation))
+      {
+         throw new RuntimeException("Should only be attached to fields");
+      }
+      
+      FieldInvocation fi = (FieldInvocation)invocation;
+      last = this;
+      System.out.println("PerJoinpointInterceptor " + this + " intercepting " + fi.getField().getName());
+      return invocation.invokeNext();
+   }
+
+}

Modified: projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/FieldTestCase.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/FieldTestCase.java	2006-11-27 14:11:22 UTC (rev 58683)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/FieldTestCase.java	2006-11-27 14:11:59 UTC (rev 58684)
@@ -21,12 +21,19 @@
   */
 package org.jboss.test.aop.field;
 
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
 
+import org.jboss.aop.Advised;
+import org.jboss.aop.AspectManager;
+import org.jboss.aop.ClassAdvisor;
+import org.jboss.aop.InstanceAdvisor;
+import org.jboss.aop.advice.AspectDefinition;
+import org.jboss.aop.joinpoint.FieldJoinpoint;
 import org.jboss.test.aop.AOPTestWithSetup;
 
 /**
@@ -91,5 +98,54 @@
       pojo.subpojoInherited = 5;
       assertTrue(TraceInterceptor.intercepted);
    }
-   
+
+   public void testPerJoinpoint() throws Exception
+   {
+      SubSubPOJO pojo1 = new SubSubPOJO(5);
+      SubSubPOJO pojo2 = new SubSubPOJO(5);
+
+      FieldPerJoinpointInterceptor.last = null;
+      pojo1.mine = 10;
+      assertNotNull(FieldPerJoinpointInterceptor.last);
+      FieldPerJoinpointInterceptor mineWrite1 = FieldPerJoinpointInterceptor.last;
+
+      FieldPerJoinpointInterceptor.last = null;
+      int x = pojo1.mine;
+      assertNotNull(FieldPerJoinpointInterceptor.last);
+      FieldPerJoinpointInterceptor mineRead1 = FieldPerJoinpointInterceptor.last;
+      
+      assertSame(mineRead1, mineWrite1);
+      
+      FieldPerJoinpointInterceptor.last = null;
+      pojo2.mine = 10;
+      assertNotNull(FieldPerJoinpointInterceptor.last);
+      FieldPerJoinpointInterceptor mineWrite2 = FieldPerJoinpointInterceptor.last;
+
+      assertNotSame(mineRead1, mineWrite2);
+      
+      FieldPerJoinpointInterceptor.last = null;
+      pojo1.pojoInherited = 10;
+      assertNotNull(FieldPerJoinpointInterceptor.last);
+      FieldPerJoinpointInterceptor inheritedWrite1 = FieldPerJoinpointInterceptor.last;
+      
+      assertNotSame(inheritedWrite1, mineWrite1);
+
+      Field mine = pojo1.getClass().getField("mine"); 
+      Field pojoInherited = pojo1.getClass().getSuperclass().getSuperclass().getField("pojoInherited");
+      
+      AspectDefinition def = AspectManager.instance().getAspectDefinition("org.jboss.test.aop.field.FieldPerJoinpointInterceptor");
+      assertNotNull(def);
+      InstanceAdvisor ia1 = ((Advised)pojo1)._getInstanceAdvisor();
+      InstanceAdvisor ia2 = ((Advised)pojo2)._getInstanceAdvisor();
+      FieldPerJoinpointInterceptor ia1Mine = (FieldPerJoinpointInterceptor)ia1.getPerInstanceJoinpointAspect(new FieldJoinpoint(mine), def);
+      assertNotNull(ia1Mine);
+      FieldPerJoinpointInterceptor ia2Mine = (FieldPerJoinpointInterceptor)ia2.getPerInstanceJoinpointAspect(new FieldJoinpoint(mine), def);
+      assertNotNull(ia2Mine);
+      FieldPerJoinpointInterceptor ia1Inherited = (FieldPerJoinpointInterceptor)ia1.getPerInstanceJoinpointAspect(new FieldJoinpoint(pojoInherited), def); 
+      assertNotNull(ia1Inherited);
+      
+      assertSame(mineRead1, ia1Mine);
+      assertSame(mineWrite2, ia2Mine);
+      assertSame(inheritedWrite1, ia1Inherited);
+   }
 }

Modified: projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/POJO.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/POJO.java	2006-11-27 14:11:22 UTC (rev 58683)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/field/POJO.java	2006-11-27 14:11:59 UTC (rev 58684)
@@ -29,7 +29,7 @@
 public class POJO
 {
    int field;
-   int pojoInherited;
+   public int pojoInherited;
    
    public POJO() {}
    




More information about the jboss-cvs-commits mailing list