[jboss-cvs] JBossAS SVN: r72057 - in projects/aop/trunk/aspects/src: main/org/jboss/aspects/mock and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Apr 11 17:02:30 EDT 2008


Author: stalep
Date: 2008-04-11 17:02:30 -0400 (Fri, 11 Apr 2008)
New Revision: 72057

Added:
   projects/aop/trunk/aspects/src/main/org/jboss/aspects/mock/
   projects/aop/trunk/aspects/src/main/org/jboss/aspects/mock/MockAspect.java
   projects/aop/trunk/aspects/src/main/org/jboss/aspects/mock/Replace.java
   projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/
   projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/
   projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/BusinessService.java
   projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/ExternalClass.java
   projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/MockTestCase.java
Log:
A MockAspect written by Thomas Roka-Aardal. 
The idea is to make it easier to mock up tests without going through the 
hassle of setting up a lot of conf files etc.


Added: projects/aop/trunk/aspects/src/main/org/jboss/aspects/mock/MockAspect.java
===================================================================
--- projects/aop/trunk/aspects/src/main/org/jboss/aspects/mock/MockAspect.java	                        (rev 0)
+++ projects/aop/trunk/aspects/src/main/org/jboss/aspects/mock/MockAspect.java	2008-04-11 21:02:30 UTC (rev 72057)
@@ -0,0 +1,112 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt 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.aspects.mock;
+
+import java.lang.reflect.Method;
+
+import org.jboss.aop.Aspect;
+import org.jboss.aop.AspectManager;
+import org.jboss.aop.Bind;
+import org.jboss.aop.advice.AdviceBinding;
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.advice.Scope;
+import org.jboss.aop.joinpoint.Invocation;
+import org.jboss.aop.joinpoint.MethodInvocation;
+
+ at Aspect(scope = Scope.PER_VM)
+public class MockAspect implements Interceptor
+{
+
+   private static String callbackClass;
+
+   private static String callbackMethod;
+
+   private static long expectedCalls;
+
+   private static long realCalls;
+
+   private final String BINDING_NAME = "_TestBinding";
+
+   @Bind(pointcut = "execution(* *->@org.jboss.aspects.mock.Replace(..))")
+   public Object advise(Invocation i) throws Throwable
+   {
+      try
+      {
+         // The pointcut above is a method pointcut (see Replace), so we can safely
+         // cast this to a method invocation.
+         MethodInvocation mi = (MethodInvocation) i;
+
+         // Read value of Replace annotation
+         Replace annotation = mi.getMethod().getAnnotation(Replace.class);
+         String invocation = "execution(* " + annotation.invocation() + ")";
+
+         // Save the annotation values as static variables for the interceptor to use later
+         callbackClass = annotation.callbackClass();
+         callbackMethod = annotation.callbackMethod();
+         expectedCalls = annotation.expectedCalls();
+
+         // Create new runtime interception for the pointcut given in the annotation variable
+         AdviceBinding binding = new AdviceBinding(BINDING_NAME, invocation, null);
+         binding.addInterceptor(MockAspect.class);
+         AspectManager.instance().addBinding(binding);
+
+         // Invoke the interceptor stack, calling the newly added interceptor
+         return i.invokeNext();
+      }
+      finally
+      {
+         // Before returning the result, remove the newly added interceptor binding so that other
+         // calls don't get the same binding. 
+         AspectManager.instance().removeBinding(BINDING_NAME);
+
+         // Before returning, verify that the number of calls wasn't less than expected
+         if (realCalls < expectedCalls && expectedCalls < Long.MAX_VALUE)
+         {
+            throw new RuntimeException("Method: " + callbackMethod + " was called less than the expected "
+                  + expectedCalls + " times! (" + realCalls + " times)");
+         }
+
+         // Re-initiate the used static variables.
+         realCalls = 0;
+      }
+   }
+
+   public java.lang.Object invoke(Invocation invocation) throws java.lang.Throwable
+   {
+      realCalls++;
+      if (realCalls > expectedCalls)
+      {
+         throw new RuntimeException("Method: " + callbackMethod + " was called more than the expected " + expectedCalls
+               + " times!");
+      }
+      MethodInvocation mi = (MethodInvocation) invocation;
+      Class<?> clazz = Class.forName(callbackClass);
+      Method method = clazz.getDeclaredMethod(callbackMethod, mi.getActualMethod().getParameterTypes());
+      return method.invoke(clazz.newInstance(), mi.getArguments());
+   }
+
+   public String getName()
+   {
+      return "Conduct Isolate Aspect and Interceptor";
+   }
+
+}

Added: projects/aop/trunk/aspects/src/main/org/jboss/aspects/mock/Replace.java
===================================================================
--- projects/aop/trunk/aspects/src/main/org/jboss/aspects/mock/Replace.java	                        (rev 0)
+++ projects/aop/trunk/aspects/src/main/org/jboss/aspects/mock/Replace.java	2008-04-11 21:02:30 UTC (rev 72057)
@@ -0,0 +1,45 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt 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.aspects.mock;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+/**
+ * This annotation is used by tests to describe a specific method that is to
+ * be intercepted and replaced by a call to a given other method in order to
+ * control isolation of unit tests.
+ * 
+ * @author Thomas Roka-Aardal
+ *
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target(ElementType.METHOD)
+public @interface Replace {
+    public String invocation();
+    public String callbackClass();
+    public String callbackMethod();
+    public long expectedCalls() default Long.MAX_VALUE;
+}
\ No newline at end of file

Added: projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/BusinessService.java
===================================================================
--- projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/BusinessService.java	                        (rev 0)
+++ projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/BusinessService.java	2008-04-11 21:02:30 UTC (rev 72057)
@@ -0,0 +1,48 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt 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.aspects.mock;
+
+/**
+ * A BusinessService.
+ * 
+ *@author Thomas Roka-Aardal
+ * @version $Revision: 1.1 $
+ */
+public class BusinessService
+{
+   private int number;
+
+   public BusinessService(int number)
+   {
+      this.number = number;
+   }
+
+   public int getNumber()
+   {
+      return number;
+   }
+
+   public int calculateExternal()
+   {
+      return ExternalClass.calculate(number);
+   }
+}

Added: projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/ExternalClass.java
===================================================================
--- projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/ExternalClass.java	                        (rev 0)
+++ projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/ExternalClass.java	2008-04-11 21:02:30 UTC (rev 72057)
@@ -0,0 +1,36 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt 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.aspects.mock;
+
+/**
+ * A ExternalClass.
+ * 
+ * @author Thomas Roka-Aardal
+ * @version $Revision: 1.1 $
+ */
+public class ExternalClass
+{
+   public static int calculate(int number)
+   {
+      return 2 * number;
+   }
+}

Added: projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/MockTestCase.java
===================================================================
--- projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/MockTestCase.java	                        (rev 0)
+++ projects/aop/trunk/aspects/src/test/org/jboss/test/aspects/mock/MockTestCase.java	2008-04-11 21:02:30 UTC (rev 72057)
@@ -0,0 +1,126 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt 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.aspects.mock;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.jboss.aspects.mock.Replace;
+import org.jboss.test.aop.AOPTestWithSetup;
+
+/**
+ * A MockTestCase.
+ * 
+ * @author <a href="stale.pedersen at jboss.org">Stale W. Pedersen</a>
+ * @author Thomas Roka-Aardal
+ * 
+ * @version $Revision: 1.1 $
+ */
+public class MockTestCase extends AOPTestWithSetup
+{
+   
+   public MockTestCase() 
+   {  
+      super("Default");  
+   }
+
+   public MockTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      TestSuite suite = new TestSuite("MockTestCase");
+      suite.addTestSuite(MockTestCase.class);
+      return suite;
+   }
+
+   @Replace(invocation = "org.jboss.test.aspects.mock.ExternalClass->calculate(int)", 
+         callbackClass = "org.jboss.test.aspects.mock.MockTestCase", 
+         callbackMethod = "callback")
+   public void testBusinessServiceGetNumberExternalIsolated() throws Exception
+   {
+      BusinessService service = new BusinessService(666);
+      assertEquals(1998, service.calculateExternal());
+   }
+
+   @Replace(invocation = "org.jboss.test.aspects.mock.ExternalClass->calculate(int)", 
+         callbackClass = "org.jboss.test.aspects.mock.MockTestCase", 
+         callbackMethod = "callbackAlternate")
+   public void testBusinessServiceGetNumberExternalIsolatedAlternate() throws Exception
+   {
+      BusinessService service = new BusinessService(666);
+      assertEquals(42, service.calculateExternal());
+   }
+
+   @Replace(invocation = "org.jboss.test.aspects.mock.ExternalClass->calculate(int)", 
+         callbackClass = "org.jboss.test.aspects.mock.MockTestCase", 
+         callbackMethod = "callbackThreeTimes", expectedCalls = 3)
+   public void testBusinessServiceGetNumberExternalIsolatedExactlyThreeTimes() throws Exception
+   {
+      BusinessService service = new BusinessService(666);
+      assertEquals(0, service.calculateExternal());
+      assertEquals(0, service.calculateExternal());
+      assertEquals(0, service.calculateExternal());
+   }
+
+   @Replace(invocation = "org.jboss.test.aspects.mock.ExternalClass->calculate(int)", 
+         callbackClass = "org.jboss.test.aspects.mock.MockTestCase", 
+         callbackMethod = "callbackThreeTimes", expectedCalls = 3)
+   public void testBusinessServiceGetNumberExternalIsolatedMoreThanThreeTimes() throws Exception
+   {
+      BusinessService service = new BusinessService(666);
+      assertEquals(0, service.calculateExternal());
+      assertEquals(0, service.calculateExternal());
+      assertEquals(0, service.calculateExternal());
+      try
+      {
+         assertEquals(0, service.calculateExternal());
+      }
+      catch (RuntimeException e)
+      {
+         // pass
+      }
+   }
+
+   public void testBusinessServiceGetNumberExternal() throws Exception
+   {
+      BusinessService service = new BusinessService(666);
+      assertEquals(1332, service.calculateExternal());
+   }
+
+   public int callback(int value)
+   {
+      return value * 3;
+   }
+
+   public int callbackAlternate(int value)
+   {
+      return 42;
+   }
+
+   public int callbackThreeTimes(int value)
+   {
+      return 0;
+   }
+}




More information about the jboss-cvs-commits mailing list