[jboss-cvs] JBossAS SVN: r97746 - in projects/kernel/trunk/kernel/src: test/java/org/jboss/test/kernel/annotations/support and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Dec 11 12:07:34 EST 2009


Author: kabir.khan at jboss.com
Date: 2009-12-11 12:07:34 -0500 (Fri, 11 Dec 2009)
New Revision: 97746

Added:
   projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/AnnotatedMethodBean.java
   projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/MethodA.java
   projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/MethodB.java
   projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/method/
   projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/method/AnnotatedMethodTestCase.java
   projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/method/AnnotatedMethodTestSuite.java
Modified:
   projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/plugins/dependency/KernelScopeInfo.java
   projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/AnnotationsTestSuite.java
Log:
[JBKERNEL-68] Push Install method metadata annotations into MDR

Modified: projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/plugins/dependency/KernelScopeInfo.java
===================================================================
--- projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/plugins/dependency/KernelScopeInfo.java	2009-12-11 17:01:25 UTC (rev 97745)
+++ projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/plugins/dependency/KernelScopeInfo.java	2009-12-11 17:07:34 UTC (rev 97746)
@@ -31,6 +31,7 @@
 import org.jboss.beans.metadata.spi.BeanMetaData;
 import org.jboss.beans.metadata.spi.CachingAnnotationMetaData;
 import org.jboss.beans.metadata.spi.ConstructorMetaData;
+import org.jboss.beans.metadata.spi.InstallMetaData;
 import org.jboss.beans.metadata.spi.ParameterMetaData;
 import org.jboss.beans.metadata.spi.PropertyMetaData;
 import org.jboss.dependency.plugins.AbstractScopeInfo;
@@ -49,9 +50,12 @@
 import org.jboss.metadata.spi.scope.ScopeKey;
 import org.jboss.metadata.spi.signature.ConstructorParametersSignature;
 import org.jboss.metadata.spi.signature.ConstructorSignature;
+import org.jboss.metadata.spi.signature.DeclaredMethodSignature;
 import org.jboss.metadata.spi.signature.FieldSignature;
+import org.jboss.metadata.spi.signature.MethodParametersSignature;
 import org.jboss.metadata.spi.signature.MethodSignature;
 import org.jboss.metadata.spi.signature.Signature;
+import org.jboss.reflect.spi.ClassInfo;
 import org.jboss.reflect.spi.ConstructorInfo;
 import org.jboss.reflect.spi.FieldInfo;
 import org.jboss.reflect.spi.MethodInfo;
@@ -70,7 +74,8 @@
    
    /** The bean metadata */
    private BeanMetaData beanMetaData;
-   
+
+   private static final String[] NO_PARAM_TYPES = new String[0];
    /**
     * Create a new KernelScopeInfo.
     * 
@@ -132,6 +137,7 @@
       {
          updateConstructorAnnotations(repository, (ComponentMutableMetaData) mutable, kernelContext, add);
          updatePropertyAnnotations(repository, (ComponentMutableMetaData) mutable, kernelContext, add);
+         updateInstallAnnotations(repository, (ComponentMutableMetaData) mutable, kernelContext, add);
       }
       else if (add == true)
          log.warn("Unable to add constructors and properties to mutable metadata that does not support components: " + mutable + " for " + context.toShortString());
@@ -228,8 +234,94 @@
       if (fieldInfo != null)
          updateAnnotations(repository, classloader, mutable, context, fieldInfo, propertyAnnotations, add);
    }
+
+   /**
+    * Update (un)install metadata annotations for install methods on this bean
+    * 
+    * @param repository the repository
+    * @param mutable the mutable
+    * @param context the kernel controller contex
+    * @param add true for add, false for remove
+    */
+   private void updateInstallAnnotations(MutableMetaDataRepository repository, ComponentMutableMetaData mutable, KernelControllerContext context, boolean add)
+   {
+      BeanMetaData beanMetaData = context.getBeanMetaData();
+      if (beanMetaData == null)
+         return;
+
+      List<InstallMetaData> installs = beanMetaData.getInstalls();
+      List<InstallMetaData> uninstalls = beanMetaData.getUninstalls();
+
+      if ((installs == null || installs.size() == 0) && (uninstalls == null || uninstalls.size() == 0))
+         return;
+
+      BeanInfo beanInfo = context.getBeanInfo();
+      if (beanInfo == null)
+         return;
+
+      ClassLoader cl;
+      try
+      {
+         cl = Configurator.getClassLoader(beanMetaData);
+      }
+      catch(Throwable t)
+      {
+         throw new RuntimeException("Error getting classloader for metadata");
+      }
+      
+      if (installs != null && installs.size() > 0)
+      {
+         for (InstallMetaData install : installs)
+            updateInstallAnnotations(repository, cl, mutable, context, install, add);
+      }
+
+      if (uninstalls != null && uninstalls.size() > 0)
+      {
+         for (InstallMetaData uninstall : uninstalls)
+            updateInstallAnnotations(repository, cl, mutable, context, uninstall, add);
+      }
+   }
    
    /**
+    * Update (un)install metadata annotations for install methods on this bean
+    *
+    * @param repository the repository
+    * @param classloader the classloader
+    * @param component the component metadata
+    * @param context the context
+    * @param install the install metadata
+    * @param add true for add, false for remove
+    */
+   private void updateInstallAnnotations(MutableMetaDataRepository repository, ClassLoader classloader, ComponentMutableMetaData component, KernelControllerContext context, InstallMetaData install, boolean add)
+   {
+      if (install.getBean() != null && install.getBean().equals(context.getName()) == false)
+         return;
+      
+      List<ParameterMetaData> parameters = install.getParameters();
+      String[] paramTypes = parameters == null ? NO_PARAM_TYPES : new String[parameters.size()];
+      MethodInfo method = Configurator.findMethodInfo(context.getBeanInfo().getClassInfo(), install.getMethodName(), paramTypes, false);
+      
+      Set<AnnotationMetaData> methodAnnotations = install.getAnnotations();
+      
+      if (methodAnnotations != null && methodAnnotations.size() > 0)
+      {
+         updateAnnotations(repository, classloader, component, context, method, methodAnnotations, add);
+      }
+      
+      if (parameters == null || parameters.size() == 0)
+         return;
+
+      for (ParameterMetaData parameterMetaData : parameters)
+      {
+         Set<AnnotationMetaData> parameterAnnotations = parameterMetaData.getAnnotations();
+         if (parameterAnnotations == null || parameterAnnotations.size() == 0)
+            continue;
+         
+         updateAnnotations(repository, classloader, component, context, method, parameterMetaData.getIndex(), parameterAnnotations, add);
+      }
+   }  
+
+   /**
     * Update constructor annotations
     * 
     * @param repository the repository
@@ -387,7 +479,30 @@
       ScopeKey scope = new ScopeKey(CommonLevels.JOINPOINT_OVERRIDE, methodInfo.getName());
       updateAnnotations(repository, classloader, component, context, signature, scope, annotations, add);
    }
+
+   /**
+    * Update annotations for a method parameter
+    *
+    * @param repository the repository
+    * @param classloader the classloader
+    * @param component the mutable metadata
+    * @param context the context
+    * @param methodInfo the method info
+    * @param parameterIndex the index of the parameter
+    * @param annotations the annotations
+    * @param add true for add, false for remove
+    */
+   private void updateAnnotations(MutableMetaDataRepository repository, ClassLoader classloader, ComponentMutableMetaData component, KernelControllerContext context, MethodInfo methodInfo, int parameterIndex, Set<AnnotationMetaData> annotations, boolean add)
+   {
+      if (annotations == null || annotations.isEmpty())
+         return;
+
+      Signature signature = new MethodParametersSignature(methodInfo, parameterIndex);
+      ScopeKey scope = new ScopeKey(CommonLevels.JOINPOINT_OVERRIDE, methodInfo.getName() + parameterIndex);
+      updateAnnotations(repository, classloader, component, context, signature, scope, annotations, add);
+   }
    
+
    /**
     * Add annotations for a field
     *

Added: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/AnnotatedMethodBean.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/AnnotatedMethodBean.java	                        (rev 0)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/AnnotatedMethodBean.java	2009-12-11 17:07:34 UTC (rev 97746)
@@ -0,0 +1,51 @@
+/*
+* 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.kernel.annotations.support;
+
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class AnnotatedMethodBean
+{
+   public long i;
+   
+   public long j;
+   
+   public void method()
+   {
+      
+   }
+   
+   public void method(int i)
+   {
+      this.i += i;
+   }
+   
+   public void method(long i, long j)
+   {
+      this.i += i;
+      this.j += j;
+   }
+}

Added: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/MethodA.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/MethodA.java	                        (rev 0)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/MethodA.java	2009-12-11 17:07:34 UTC (rev 97746)
@@ -0,0 +1,39 @@
+/*
+* 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.kernel.annotations.support;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target({ElementType.PARAMETER, ElementType.METHOD})
+public @interface MethodA 
+{
+
+}

Added: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/MethodB.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/MethodB.java	                        (rev 0)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/MethodB.java	2009-12-11 17:07:34 UTC (rev 97746)
@@ -0,0 +1,39 @@
+/*
+* 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.kernel.annotations.support;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target({ElementType.PARAMETER, ElementType.METHOD})
+public @interface MethodB 
+{
+
+}

Modified: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/AnnotationsTestSuite.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/AnnotationsTestSuite.java	2009-12-11 17:01:25 UTC (rev 97745)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/AnnotationsTestSuite.java	2009-12-11 17:07:34 UTC (rev 97746)
@@ -24,9 +24,12 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
+
+import org.jboss.test.kernel.annotations.test.constructor.AnnotatedConstructorTestSuite;
 import org.jboss.test.kernel.annotations.test.factory.AnnotationFactoryTestSuite;
 import org.jboss.test.kernel.annotations.test.field.AnnotationFieldTestSuite;
 import org.jboss.test.kernel.annotations.test.inheritance.AnnotationsInheritanceTestSuite;
+import org.jboss.test.kernel.annotations.test.method.AnnotatedMethodTestSuite;
 import org.jboss.test.kernel.annotations.test.override.AnnotationsOverrideTestSuite;
 import org.jboss.test.kernel.annotations.test.wb.WBTestSuite;
 import org.jboss.test.kernel.annotations.test.search.SearchTestSuite;
@@ -54,6 +57,8 @@
       suite.addTest(AnnotationFactoryTestSuite.suite());
       suite.addTest(WBTestSuite.suite());
       suite.addTest(SearchTestSuite.suite());
+      suite.addTest(AnnotatedConstructorTestSuite.suite());
+      suite.addTest(AnnotatedMethodTestSuite.suite());
 
       return suite;
    }

Added: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/method/AnnotatedMethodTestCase.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/method/AnnotatedMethodTestCase.java	                        (rev 0)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/method/AnnotatedMethodTestCase.java	2009-12-11 17:07:34 UTC (rev 97746)
@@ -0,0 +1,477 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, 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.kernel.annotations.test.method;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import junit.framework.Test;
+
+import org.jboss.beans.metadata.plugins.AbstractAnnotationMetaData;
+import org.jboss.beans.metadata.plugins.AbstractBeanMetaData;
+import org.jboss.beans.metadata.plugins.AbstractInstallMetaData;
+import org.jboss.beans.metadata.plugins.AbstractParameterMetaData;
+import org.jboss.beans.metadata.spi.AnnotationMetaData;
+import org.jboss.beans.metadata.spi.InstallMetaData;
+import org.jboss.beans.metadata.spi.ParameterMetaData;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.kernel.spi.metadata.KernelMetaDataRepository;
+import org.jboss.metadata.spi.MetaData;
+import org.jboss.metadata.spi.signature.DeclaredMethodSignature;
+import org.jboss.metadata.spi.signature.MethodParametersSignature;
+import org.jboss.metadata.spi.signature.Signature;
+import org.jboss.test.kernel.annotations.support.AnnotatedMethodBean;
+import org.jboss.test.kernel.annotations.support.ConstructorB;
+import org.jboss.test.kernel.annotations.support.MethodA;
+import org.jboss.test.kernel.annotations.support.MethodB;
+import org.jboss.test.kernel.junit.MicrocontainerTest;
+
+/**
+ * Check that constructor and constructor parameter metadata annotations show up in MDR
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ */
+public class AnnotatedMethodTestCase extends MicrocontainerTest
+{
+   private final static String METHOD_NAME = "method";
+   
+   public AnnotatedMethodTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(AnnotatedMethodTestCase.class);
+   }
+
+   public void testAnnotatedMethodNoParametersNoAnnotations() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         assertEquals(0, bean.i);
+         assertEquals(0, bean.j);
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME);
+
+         assertMethodAnnotations(context, method);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   public void testAnnotatedMethodNoParameters() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      imd.setAnnotations(createAnnotationMetaDataSet(MethodA.class, ConstructorB.class));
+      addInstallMetaData(bmd, imd);
+      
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME);
+         assertEquals(0, bean.i);
+         assertEquals(0, bean.j);
+         assertMethodAnnotations(context, method, MethodA.class, ConstructorB.class);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   public void testAnnotatedMethodOneParameterNoAnnotations() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      AbstractParameterMetaData pmd = new AbstractParameterMetaData("int", 1);
+      List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+      params.add(pmd);
+      imd.setParameters(params);
+      addInstallMetaData(bmd, imd);
+      
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME, Integer.TYPE);
+         assertEquals(1, bean.i);
+         assertEquals(0, bean.j);
+         assertMethodAnnotations(context, method);
+         assertMethodParameterAnnotations(context, method, 0);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   public void testAnnotatedMethodOneParameterAnnotationsOnMethod() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      imd.setAnnotations(createAnnotationMetaDataSet(MethodA.class));
+      AbstractParameterMetaData pmd = new AbstractParameterMetaData("int", 1);
+      List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+      params.add(pmd);
+      imd.setParameters(params);
+      addInstallMetaData(bmd, imd);
+
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME, Integer.TYPE);
+         assertEquals(1, bean.i);
+         assertEquals(0, bean.j);
+         assertMethodAnnotations(context, method, MethodA.class);
+         assertMethodParameterAnnotations(context, method, 0);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   public void testAnnotatedMethodOneParameterAnnotationsOnConstructorAndParameter() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      imd.setAnnotations(createAnnotationMetaDataSet(MethodA.class));
+      AbstractParameterMetaData pmd = new AbstractParameterMetaData("int", 1);
+      pmd.setAnnotations(createAnnotationMetaDataSet(MethodB.class));
+      List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+      params.add(pmd);
+      imd.setParameters(params);
+      addInstallMetaData(bmd, imd);
+
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME, Integer.TYPE);
+         assertEquals(1, bean.i);
+         assertEquals(0, bean.j);
+         assertMethodAnnotations(context, method, MethodA.class);
+         assertMethodParameterAnnotations(context, method, 0, MethodB.class);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   public void testAnnotatedMethodOneParameterAnnotationsOnParameter() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      AbstractParameterMetaData pmd = new AbstractParameterMetaData("int", 1);
+      pmd.setAnnotations(createAnnotationMetaDataSet(MethodA.class));
+      List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+      params.add(pmd);
+      imd.setParameters(params);
+      addInstallMetaData(bmd, imd);
+
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME, Integer.TYPE);
+         assertEquals(1, bean.i);
+         assertEquals(0, bean.j);
+         assertMethodAnnotations(context, method);
+         assertMethodParameterAnnotations(context, method, 0, MethodA.class);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   public void testAnnotatedMethodTwoConstructorAnnotations() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      imd.setAnnotations(createAnnotationMetaDataSet(MethodA.class));
+      AbstractParameterMetaData pmd0 = new AbstractParameterMetaData("long", 1);
+      AbstractParameterMetaData pmd1 = new AbstractParameterMetaData("long", 2);
+      List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+      params.add(pmd0);
+      params.add(pmd1);
+      imd.setParameters(params);
+      addInstallMetaData(bmd, imd);
+      
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME, Long.TYPE, Long.TYPE);
+         assertEquals(1, bean.i);
+         assertEquals(2, bean.j);
+         assertMethodAnnotations(context, method, MethodA.class);
+         assertMethodParameterAnnotations(context, method, 0);
+         assertMethodParameterAnnotations(context, method, 1);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   public void testAnnotatedMethodTwoMethodAndParameterOneAnnotations() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      imd.setAnnotations(createAnnotationMetaDataSet(MethodA.class));
+      AbstractParameterMetaData pmd0 = new AbstractParameterMetaData("long", 1);
+      pmd0.setAnnotations(createAnnotationMetaDataSet(MethodB.class));
+      AbstractParameterMetaData pmd1 = new AbstractParameterMetaData("long", 2);
+      List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+      params.add(pmd0);
+      params.add(pmd1);
+      imd.setParameters(params);
+      addInstallMetaData(bmd, imd);
+      
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME, Long.TYPE, Long.TYPE);
+         assertEquals(1, bean.i);
+         assertEquals(2, bean.j);
+         assertMethodAnnotations(context, method, MethodA.class);
+         assertMethodParameterAnnotations(context, method, 0, MethodB.class);
+         assertMethodParameterAnnotations(context, method, 1);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   public void testAnnotatedMethodTwoMethodAndParameterTwoAnnotations() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      imd.setAnnotations(createAnnotationMetaDataSet(MethodA.class));
+      AbstractParameterMetaData pmd0 = new AbstractParameterMetaData("long", 1);
+      AbstractParameterMetaData pmd1 = new AbstractParameterMetaData("long", 2);
+      pmd1.setAnnotations(createAnnotationMetaDataSet(MethodB.class));
+      List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+      params.add(pmd0);
+      params.add(pmd1);
+      imd.setParameters(params);
+      addInstallMetaData(bmd, imd);
+      
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME, Long.TYPE, Long.TYPE);
+         assertEquals(1, bean.i);
+         assertEquals(2, bean.j);
+         assertMethodAnnotations(context, method, MethodA.class);
+         assertMethodParameterAnnotations(context, method, 0);
+         assertMethodParameterAnnotations(context, method, 1, MethodB.class);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   public void testAnnotatedMethodTwoMethodAndParameterOneTwoAnnotations() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      imd.setAnnotations(createAnnotationMetaDataSet(MethodA.class));
+      AbstractParameterMetaData pmd0 = new AbstractParameterMetaData("long", 1);
+      pmd0.setAnnotations(createAnnotationMetaDataSet(MethodA.class, MethodB.class));
+      AbstractParameterMetaData pmd1 = new AbstractParameterMetaData("long", 2);
+      pmd1.setAnnotations(createAnnotationMetaDataSet(MethodB.class));
+      List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+      params.add(pmd0);
+      params.add(pmd1);
+      imd.setParameters(params);
+      addInstallMetaData(bmd, imd);
+      
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME, Long.TYPE, Long.TYPE);
+         assertEquals(1, bean.i);
+         assertEquals(2, bean.j);
+         assertMethodAnnotations(context, method, MethodA.class);
+         assertMethodParameterAnnotations(context, method, 0, MethodA.class, MethodB.class);
+         assertMethodParameterAnnotations(context, method, 1, MethodB.class);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   public void testOverloadedInstallMethods() throws Throwable
+   {
+      AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedMethodBean.class.getName());
+      
+      AbstractInstallMetaData imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      imd.setAnnotations(createAnnotationMetaDataSet(MethodA.class));
+      AbstractParameterMetaData pmd0 = new AbstractParameterMetaData("long", 1);
+      pmd0.setAnnotations(createAnnotationMetaDataSet(MethodA.class, MethodB.class));
+      AbstractParameterMetaData pmd1 = new AbstractParameterMetaData("long", 2);
+      pmd1.setAnnotations(createAnnotationMetaDataSet(MethodB.class));
+      List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+      params.add(pmd0);
+      params.add(pmd1);
+      imd.setParameters(params);
+
+      List<InstallMetaData> installs = new ArrayList<InstallMetaData>();
+      installs.add(imd);
+      
+      imd = new AbstractInstallMetaData();
+      imd.setMethodName(METHOD_NAME);
+      imd.setAnnotations(createAnnotationMetaDataSet(MethodB.class));
+      pmd0 = new AbstractParameterMetaData("int", 2);
+      pmd0.setAnnotations(createAnnotationMetaDataSet(MethodA.class));
+      params = new ArrayList<ParameterMetaData>();
+      params.add(pmd0);
+      imd.setParameters(params);
+      installs.add(imd);
+      
+      bmd.setInstalls(installs);
+      
+      KernelControllerContext context = null;
+      try
+      {
+         context = deploy(bmd);
+         AnnotatedMethodBean bean = assertBean("Bean", AnnotatedMethodBean.class);
+         assertEquals(3, bean.i);
+         assertEquals(2, bean.j);
+
+         Method method = AnnotatedMethodBean.class.getMethod(METHOD_NAME, Long.TYPE, Long.TYPE);
+         assertMethodAnnotations(context, method, MethodA.class);
+         assertMethodParameterAnnotations(context, method, 0, MethodA.class, MethodB.class);
+         assertMethodParameterAnnotations(context, method, 1, MethodB.class);
+         
+         method = AnnotatedMethodBean.class.getMethod(METHOD_NAME, Integer.TYPE);
+         assertMethodAnnotations(context, method, MethodB.class);
+         assertMethodParameterAnnotations(context, method, 0, MethodA.class);
+      }
+      finally
+      {
+         undeploy(context);
+      }
+   }
+   
+   private void assertMethodAnnotations(KernelControllerContext context, Method method, Class<? extends Annotation>...expectedAnnotations)
+   {
+      Annotation[] annotations = getComponentMetaData(context, new DeclaredMethodSignature(method));
+      assertAnnnotations(annotations, expectedAnnotations);
+   }
+   
+   private void assertMethodParameterAnnotations(KernelControllerContext context, Method method, int parameterIndex, Class<? extends Annotation>...expectedAnnotations)
+   {
+      Annotation[] annotations = getComponentMetaData(context, new MethodParametersSignature(method, parameterIndex));
+      assertAnnnotations(annotations, expectedAnnotations);
+   }
+   
+   private void assertAnnnotations(Annotation[] annotations, Class<? extends Annotation>[] expectedAnnotations)
+   {
+      assertEquals(expectedAnnotations.length, annotations.length);
+      for (Class<? extends Annotation> expectedAnnotation : expectedAnnotations)
+      {
+         boolean found = false;
+         for (Annotation annotation : annotations)
+         {
+            if (annotation.annotationType() == expectedAnnotation)
+            {
+               found = true;
+               break;
+            }
+         }
+         assertTrue("Did not find annotation " + expectedAnnotation, found);
+      }
+   }
+   
+   private Annotation[] getComponentMetaData(KernelControllerContext context, Signature signature)
+   {
+      KernelMetaDataRepository repository = context.getKernel().getMetaDataRepository();
+      MetaData metaData = repository.getMetaData(context);
+      MetaData component = metaData.getComponentMetaData(signature);
+      return component.getAnnotations();
+   }
+   
+   private Set<AnnotationMetaData> createAnnotationMetaDataSet(Class<? extends Annotation>...classes)
+   {
+      if (classes == null || classes.length == 0)
+         return null;
+      
+      Set<AnnotationMetaData> annotations = new HashSet<AnnotationMetaData>();
+      for (Class<? extends Annotation> clazz : classes)
+      {
+         annotations.add(new AbstractAnnotationMetaData("@" + clazz.getName()));
+      }
+      return annotations;
+   }
+   
+   private void addInstallMetaData(AbstractBeanMetaData bmd, AbstractInstallMetaData imd)
+   {
+      List<InstallMetaData> installs = new ArrayList<InstallMetaData>();
+      installs.add(imd);
+      bmd.setInstalls(installs);
+   }
+}

Added: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/method/AnnotatedMethodTestSuite.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/method/AnnotatedMethodTestSuite.java	                        (rev 0)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/method/AnnotatedMethodTestSuite.java	2009-12-11 17:07:34 UTC (rev 97746)
@@ -0,0 +1,48 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, 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.kernel.annotations.test.method;
+
+import junit.textui.TestRunner;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Field annotation tests.
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ */
+public class AnnotatedMethodTestSuite extends TestSuite
+{
+   public static void main(String[] args)
+   {
+      TestRunner.run(suite());
+   }
+
+   public static Test suite()
+   {
+      TestSuite suite = new TestSuite("Annotated Constructor Tests");
+
+      suite.addTest(AnnotatedMethodTestCase.suite());
+
+      return suite;
+   }
+}




More information about the jboss-cvs-commits mailing list