[jboss-cvs] JBossAS SVN: r92572 - 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
Wed Aug 19 07:39:31 EDT 2009
Author: kabir.khan at jboss.com
Date: 2009-08-19 07:39:30 -0400 (Wed, 19 Aug 2009)
New Revision: 92572
Added:
projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/AnnotatedConstructorBean.java
projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/ConstructorA.java
projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/ConstructorB.java
projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/constructor/
projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/constructor/AnnotatedConstructorTestCase.java
projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/constructor/AnnotatedConstructorTestSuite.java
Modified:
projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/plugins/dependency/KernelScopeInfo.java
Log:
[JBKERNEL-52] Add constructor and parameter metadata annotations to 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-08-19 11:06:10 UTC (rev 92571)
+++ projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/plugins/dependency/KernelScopeInfo.java 2009-08-19 11:39:30 UTC (rev 92572)
@@ -30,6 +30,8 @@
import org.jboss.beans.metadata.spi.AnnotationMetaData;
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.ParameterMetaData;
import org.jboss.beans.metadata.spi.PropertyMetaData;
import org.jboss.dependency.plugins.AbstractScopeInfo;
import org.jboss.dependency.spi.Controller;
@@ -45,9 +47,12 @@
import org.jboss.metadata.spi.scope.CommonLevels;
import org.jboss.metadata.spi.scope.Scope;
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.FieldSignature;
import org.jboss.metadata.spi.signature.MethodSignature;
import org.jboss.metadata.spi.signature.Signature;
+import org.jboss.reflect.spi.ConstructorInfo;
import org.jboss.reflect.spi.FieldInfo;
import org.jboss.reflect.spi.MethodInfo;
@@ -55,6 +60,7 @@
* KernelScopeInfo.
*
* @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
* @version $Revision: 1.1 $
*/
public class KernelScopeInfo extends AbstractScopeInfo
@@ -122,9 +128,12 @@
KernelControllerContext kernelContext = (KernelControllerContext) context;
updateClassAnnotations(mutable, kernelContext, add);
if (mutable instanceof ComponentMutableMetaData)
- updatePropertyAnnotations(repository, (ComponentMutableMetaData) mutable, kernelContext, add);
+ {
+ updateConstructorAnnotations(repository, (ComponentMutableMetaData) mutable, kernelContext, add);
+ updatePropertyAnnotations(repository, (ComponentMutableMetaData) mutable, kernelContext, add);
+ }
else if (add == true)
- log.warn("Unable to add properties to mutable metadata that does not support components: " + mutable + " for " + context.toShortString());
+ log.warn("Unable to add constructors and properties to mutable metadata that does not support components: " + mutable + " for " + context.toShortString());
}
/**
@@ -220,6 +229,60 @@
}
/**
+ * Update constructor annotations
+ *
+ * @param repository the repository
+ * @param mutable the mutable
+ * @param context the kernel controller contex
+ * @param add true for add, false for remove
+ */
+ private void updateConstructorAnnotations(MutableMetaDataRepository repository, ComponentMutableMetaData mutable, KernelControllerContext context, boolean add)
+ {
+ BeanMetaData beanMetaData = context.getBeanMetaData();
+ if (beanMetaData == null)
+ return;
+
+ ConstructorMetaData constructorMetaData = beanMetaData.getConstructor();
+ if (constructorMetaData == null)
+ 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");
+ }
+
+ Set<AnnotationMetaData> constructorAnnotations = constructorMetaData.getAnnotations();
+ List<ParameterMetaData> parameterMetaDatas = constructorMetaData.getParameters();
+ if (constructorAnnotations != null && constructorAnnotations.size() > 0 || parameterMetaDatas != null && parameterMetaDatas.size() > 0)
+ {
+ ConstructorInfo constructorInfo = Configurator.resolveConstructor(log.isTraceEnabled(), beanInfo, constructorMetaData);
+ if (constructorAnnotations != null && constructorAnnotations.size() > 0)
+ updateAnnotations(repository, cl, mutable, context, constructorInfo, constructorAnnotations, add);
+
+ if (parameterMetaDatas != null && parameterMetaDatas.size() > 0)
+ {
+ for (ParameterMetaData parameterMetaData : parameterMetaDatas)
+ {
+ Set<AnnotationMetaData> parameterAnnotations = parameterMetaData.getAnnotations();
+ if (parameterAnnotations == null || parameterAnnotations.size() == 0)
+ continue;
+
+ updateAnnotations(repository, cl, mutable, context, constructorInfo, parameterMetaData.getIndex(), parameterAnnotations, add);
+ }
+ }
+ }
+ }
+
+ /**
* Update component annotations
*
* @param repository the repository
@@ -258,6 +321,49 @@
}
/**
+ * Update annotations for a constructor
+ *
+ * @param repository the repository
+ * @param classloader the classloader
+ * @param component the mutable metadata
+ * @param context the context
+ * @param constructorInfo the constructor info
+ * @param annotations the annotations
+ * @param add true for add, false for remove
+ */
+ private void updateAnnotations(MutableMetaDataRepository repository, ClassLoader classloader, ComponentMutableMetaData component, KernelControllerContext context, ConstructorInfo constructorInfo, Set<AnnotationMetaData> annotations, boolean add)
+ {
+ if (annotations == null || annotations.isEmpty())
+ return;
+
+ Signature signature = new ConstructorSignature(constructorInfo);
+ ScopeKey scope = new ScopeKey(CommonLevels.JOINPOINT_OVERRIDE, "<NEW>");
+ updateAnnotations(repository, classloader, component, context, signature, scope, annotations, add);
+ }
+
+ /**
+ * Update annotations for a constructor parameter
+ *
+ * @param repository the repository
+ * @param classloader the classloader
+ * @param component the mutable metadata
+ * @param context the context
+ * @param constructorInfo the constructor 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, ConstructorInfo constructorInfo, int parameterIndex, Set<AnnotationMetaData> annotations, boolean add)
+ {
+ if (annotations == null || annotations.isEmpty())
+ return;
+
+ Signature signature = new ConstructorParametersSignature(constructorInfo, parameterIndex);
+ ScopeKey scope = new ScopeKey(CommonLevels.JOINPOINT_OVERRIDE, "<NEW>" + parameterIndex);
+ updateAnnotations(repository, classloader, component, context, signature, scope, annotations, add);
+ }
+
+ /**
* Update annotations for a method
*
* @param repository the repository
Added: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/AnnotatedConstructorBean.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/AnnotatedConstructorBean.java (rev 0)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/AnnotatedConstructorBean.java 2009-08-19 11:39:30 UTC (rev 92572)
@@ -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 AnnotatedConstructorBean
+{
+ public long i;
+
+ public long j;
+
+ public AnnotatedConstructorBean()
+ {
+
+ }
+
+ public AnnotatedConstructorBean(int i)
+ {
+ this.i = i;
+ }
+
+ public AnnotatedConstructorBean(long i, long j)
+ {
+ this.i = i;
+ this.j = j;
+ }
+}
Added: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/ConstructorA.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/ConstructorA.java (rev 0)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/ConstructorA.java 2009-08-19 11:39:30 UTC (rev 92572)
@@ -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.CONSTRUCTOR})
+public @interface ConstructorA
+{
+
+}
Added: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/ConstructorB.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/ConstructorB.java (rev 0)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/support/ConstructorB.java 2009-08-19 11:39:30 UTC (rev 92572)
@@ -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.CONSTRUCTOR})
+public @interface ConstructorB
+{
+
+}
Added: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/constructor/AnnotatedConstructorTestCase.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/constructor/AnnotatedConstructorTestCase.java (rev 0)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/constructor/AnnotatedConstructorTestCase.java 2009-08-19 11:39:30 UTC (rev 92572)
@@ -0,0 +1,400 @@
+/*
+* 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.constructor;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+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.AbstractConstructorMetaData;
+import org.jboss.beans.metadata.plugins.AbstractParameterMetaData;
+import org.jboss.beans.metadata.spi.AnnotationMetaData;
+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.ConstructorParametersSignature;
+import org.jboss.metadata.spi.signature.ConstructorSignature;
+import org.jboss.metadata.spi.signature.Signature;
+import org.jboss.test.kernel.annotations.support.AnnotatedConstructorBean;
+import org.jboss.test.kernel.annotations.support.ConstructorA;
+import org.jboss.test.kernel.annotations.support.ConstructorB;
+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 AnnotatedConstructorTestCase extends MicrocontainerTest
+{
+ public AnnotatedConstructorTestCase(String name)
+ {
+ super(name);
+ }
+
+ public static Test suite()
+ {
+ return suite(AnnotatedConstructorTestCase.class);
+ }
+
+ public void testAnnotatedConstructorNoParametersNoAnnotations() throws Throwable
+ {
+ AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedConstructorBean.class.getName());
+
+ KernelControllerContext context = null;
+ try
+ {
+ context = deploy(bmd);
+ AnnotatedConstructorBean bean = assertBean("Bean", AnnotatedConstructorBean.class);
+ Constructor<?> constructor = AnnotatedConstructorBean.class.getConstructor();
+ assertEquals(0, bean.i);
+ assertEquals(0, bean.j);
+ assertConstructorAnnotations(context, constructor);
+ }
+ finally
+ {
+ undeploy(context);
+ }
+ }
+
+ public void testAnnotatedConstructorNoParameters() throws Throwable
+ {
+ AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedConstructorBean.class.getName());
+ AbstractConstructorMetaData cmd = new AbstractConstructorMetaData();
+ cmd.setAnnotations(createAnnotationMetaDataSet(ConstructorA.class, ConstructorB.class));
+ bmd.setConstructor(cmd);
+
+ KernelControllerContext context = null;
+ try
+ {
+ context = deploy(bmd);
+ AnnotatedConstructorBean bean = assertBean("Bean", AnnotatedConstructorBean.class);
+ Constructor<?> constructor = AnnotatedConstructorBean.class.getConstructor();
+ assertEquals(0, bean.i);
+ assertEquals(0, bean.j);
+ assertConstructorAnnotations(context, constructor, ConstructorA.class, ConstructorB.class);
+ }
+ finally
+ {
+ undeploy(context);
+ }
+ }
+
+ public void testAnnotatedConstructorOneParameterNoAnnotations() throws Throwable
+ {
+ AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedConstructorBean.class.getName());
+ AbstractConstructorMetaData cmd = new AbstractConstructorMetaData();
+ AbstractParameterMetaData pmd = new AbstractParameterMetaData("int", 1);
+ List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+ params.add(pmd);
+ cmd.setParameters(params);
+ bmd.setConstructor(cmd);
+
+ KernelControllerContext context = null;
+ try
+ {
+ context = deploy(bmd);
+ AnnotatedConstructorBean bean = assertBean("Bean", AnnotatedConstructorBean.class);
+ Constructor<?> constructor = AnnotatedConstructorBean.class.getConstructor(Integer.TYPE);
+ assertEquals(1, bean.i);
+ assertEquals(0, bean.j);
+ assertConstructorAnnotations(context, constructor);
+ assertConstructorParameterAnnotations(context, constructor, 0);
+ }
+ finally
+ {
+ undeploy(context);
+ }
+ }
+
+ public void testAnnotatedConstructorOneParameterAnnotationsOnConstructor() throws Throwable
+ {
+ AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedConstructorBean.class.getName());
+ AbstractConstructorMetaData cmd = new AbstractConstructorMetaData();
+ cmd.setAnnotations(createAnnotationMetaDataSet(ConstructorA.class));
+ AbstractParameterMetaData pmd = new AbstractParameterMetaData("int", 1);
+ List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+ params.add(pmd);
+ cmd.setParameters(params);
+ bmd.setConstructor(cmd);
+
+ KernelControllerContext context = null;
+ try
+ {
+ context = deploy(bmd);
+ AnnotatedConstructorBean bean = assertBean("Bean", AnnotatedConstructorBean.class);
+ Constructor<?> constructor = AnnotatedConstructorBean.class.getConstructor(Integer.TYPE);
+ assertEquals(1, bean.i);
+ assertEquals(0, bean.j);
+ assertConstructorAnnotations(context, constructor, ConstructorA.class);
+ assertConstructorParameterAnnotations(context, constructor, 0);
+ }
+ finally
+ {
+ undeploy(context);
+ }
+ }
+
+ public void testAnnotatedConstructorOneParameterAnnotationsOnConstructorAndParameter() throws Throwable
+ {
+ AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedConstructorBean.class.getName());
+ AbstractConstructorMetaData cmd = new AbstractConstructorMetaData();
+ cmd.setAnnotations(createAnnotationMetaDataSet(ConstructorA.class));
+ AbstractParameterMetaData pmd = new AbstractParameterMetaData("int", 1);
+ pmd.setAnnotations(createAnnotationMetaDataSet(ConstructorB.class));
+ List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+ params.add(pmd);
+ cmd.setParameters(params);
+ bmd.setConstructor(cmd);
+
+ KernelControllerContext context = null;
+ try
+ {
+ context = deploy(bmd);
+ AnnotatedConstructorBean bean = assertBean("Bean", AnnotatedConstructorBean.class);
+ Constructor<?> constructor = AnnotatedConstructorBean.class.getConstructor(Integer.TYPE);
+ assertEquals(1, bean.i);
+ assertEquals(0, bean.j);
+ assertConstructorAnnotations(context, constructor, ConstructorA.class);
+ assertConstructorParameterAnnotations(context, constructor, 0, ConstructorB.class);
+ }
+ finally
+ {
+ undeploy(context);
+ }
+ }
+
+ public void testAnnotatedConstructorOneParameterAnnotationsOnParameter() throws Throwable
+ {
+ AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedConstructorBean.class.getName());
+ AbstractConstructorMetaData cmd = new AbstractConstructorMetaData();
+ AbstractParameterMetaData pmd = new AbstractParameterMetaData("int", 1);
+ pmd.setAnnotations(createAnnotationMetaDataSet(ConstructorA.class));
+ List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+ params.add(pmd);
+ cmd.setParameters(params);
+ bmd.setConstructor(cmd);
+
+ KernelControllerContext context = null;
+ try
+ {
+ context = deploy(bmd);
+ AnnotatedConstructorBean bean = assertBean("Bean", AnnotatedConstructorBean.class);
+ Constructor<?> constructor = AnnotatedConstructorBean.class.getConstructor(Integer.TYPE);
+ assertEquals(1, bean.i);
+ assertEquals(0, bean.j);
+ assertConstructorAnnotations(context, constructor);
+ assertConstructorParameterAnnotations(context, constructor, 0, ConstructorA.class);
+ }
+ finally
+ {
+ undeploy(context);
+ }
+ }
+
+ public void testAnnotatedConstructorTwoConstructorAnnotations() throws Throwable
+ {
+ AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedConstructorBean.class.getName());
+ AbstractConstructorMetaData cmd = new AbstractConstructorMetaData();
+ cmd.setAnnotations(createAnnotationMetaDataSet(ConstructorA.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);
+ cmd.setParameters(params);
+ bmd.setConstructor(cmd);
+
+ KernelControllerContext context = null;
+ try
+ {
+ context = deploy(bmd);
+ AnnotatedConstructorBean bean = assertBean("Bean", AnnotatedConstructorBean.class);
+ Constructor<?> constructor = AnnotatedConstructorBean.class.getConstructor(Long.TYPE, Long.TYPE);
+ assertEquals(1, bean.i);
+ assertEquals(2, bean.j);
+ assertConstructorAnnotations(context, constructor, ConstructorA.class);
+ assertConstructorParameterAnnotations(context, constructor, 0);
+ assertConstructorParameterAnnotations(context, constructor, 1);
+ }
+ finally
+ {
+ undeploy(context);
+ }
+ }
+
+ public void testAnnotatedConstructorTwoConstructorAndParameterOneAnnotations() throws Throwable
+ {
+ AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedConstructorBean.class.getName());
+ AbstractConstructorMetaData cmd = new AbstractConstructorMetaData();
+ cmd.setAnnotations(createAnnotationMetaDataSet(ConstructorA.class));
+ AbstractParameterMetaData pmd0 = new AbstractParameterMetaData("long", 1);
+ pmd0.setAnnotations(createAnnotationMetaDataSet(ConstructorB.class));
+ AbstractParameterMetaData pmd1 = new AbstractParameterMetaData("long", 2);
+ List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+ params.add(pmd0);
+ params.add(pmd1);
+ cmd.setParameters(params);
+ bmd.setConstructor(cmd);
+
+ KernelControllerContext context = null;
+ try
+ {
+ context = deploy(bmd);
+ AnnotatedConstructorBean bean = assertBean("Bean", AnnotatedConstructorBean.class);
+ Constructor<?> constructor = AnnotatedConstructorBean.class.getConstructor(Long.TYPE, Long.TYPE);
+ assertEquals(1, bean.i);
+ assertEquals(2, bean.j);
+ assertConstructorAnnotations(context, constructor, ConstructorA.class);
+ assertConstructorParameterAnnotations(context, constructor, 0, ConstructorB.class);
+ assertConstructorParameterAnnotations(context, constructor, 1);
+ }
+ finally
+ {
+ undeploy(context);
+ }
+ }
+
+ public void testAnnotatedConstructorTwoConstructorAndParameterTwoAnnotations() throws Throwable
+ {
+ AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedConstructorBean.class.getName());
+ AbstractConstructorMetaData cmd = new AbstractConstructorMetaData();
+ cmd.setAnnotations(createAnnotationMetaDataSet(ConstructorA.class));
+ AbstractParameterMetaData pmd0 = new AbstractParameterMetaData("long", 1);
+ AbstractParameterMetaData pmd1 = new AbstractParameterMetaData("long", 2);
+ pmd1.setAnnotations(createAnnotationMetaDataSet(ConstructorB.class));
+ List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+ params.add(pmd0);
+ params.add(pmd1);
+ cmd.setParameters(params);
+ bmd.setConstructor(cmd);
+
+ KernelControllerContext context = null;
+ try
+ {
+ context = deploy(bmd);
+ AnnotatedConstructorBean bean = assertBean("Bean", AnnotatedConstructorBean.class);
+ Constructor<?> constructor = AnnotatedConstructorBean.class.getConstructor(Long.TYPE, Long.TYPE);
+ assertEquals(1, bean.i);
+ assertEquals(2, bean.j);
+ assertConstructorAnnotations(context, constructor, ConstructorA.class);
+ assertConstructorParameterAnnotations(context, constructor, 0);
+ assertConstructorParameterAnnotations(context, constructor, 1, ConstructorB.class);
+ }
+ finally
+ {
+ undeploy(context);
+ }
+ }
+
+ public void testAnnotatedConstructorTwoConstructorAndParameterOneTwoAnnotations() throws Throwable
+ {
+ AbstractBeanMetaData bmd = new AbstractBeanMetaData("Bean", AnnotatedConstructorBean.class.getName());
+ AbstractConstructorMetaData cmd = new AbstractConstructorMetaData();
+ cmd.setAnnotations(createAnnotationMetaDataSet(ConstructorA.class));
+ AbstractParameterMetaData pmd0 = new AbstractParameterMetaData("long", 1);
+ pmd0.setAnnotations(createAnnotationMetaDataSet(ConstructorA.class, ConstructorB.class));
+ AbstractParameterMetaData pmd1 = new AbstractParameterMetaData("long", 2);
+ pmd1.setAnnotations(createAnnotationMetaDataSet(ConstructorB.class));
+ List<ParameterMetaData> params = new ArrayList<ParameterMetaData>();
+ params.add(pmd0);
+ params.add(pmd1);
+ cmd.setParameters(params);
+ bmd.setConstructor(cmd);
+
+ KernelControllerContext context = null;
+ try
+ {
+ context = deploy(bmd);
+ AnnotatedConstructorBean bean = assertBean("Bean", AnnotatedConstructorBean.class);
+ Constructor<?> constructor = AnnotatedConstructorBean.class.getConstructor(Long.TYPE, Long.TYPE);
+ assertEquals(1, bean.i);
+ assertEquals(2, bean.j);
+ assertConstructorAnnotations(context, constructor, ConstructorA.class);
+ assertConstructorParameterAnnotations(context, constructor, 0, ConstructorA.class, ConstructorB.class);
+ assertConstructorParameterAnnotations(context, constructor, 1, ConstructorB.class);
+ }
+ finally
+ {
+ undeploy(context);
+ }
+ }
+
+ private void assertConstructorAnnotations(KernelControllerContext context, Constructor<?> constructor, Class<? extends Annotation>...expectedAnnotations)
+ {
+ Annotation[] annotations = getComponentMetaData(context, new ConstructorSignature(constructor));
+ assertAnnnotations(annotations, expectedAnnotations);
+ }
+
+ private void assertConstructorParameterAnnotations(KernelControllerContext context, Constructor<?> constructor, int parameterIndex, Class<? extends Annotation>...expectedAnnotations)
+ {
+ Annotation[] annotations = getComponentMetaData(context, new ConstructorParametersSignature(constructor, 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;
+ }
+}
Added: projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/constructor/AnnotatedConstructorTestSuite.java
===================================================================
--- projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/constructor/AnnotatedConstructorTestSuite.java (rev 0)
+++ projects/kernel/trunk/kernel/src/test/java/org/jboss/test/kernel/annotations/test/constructor/AnnotatedConstructorTestSuite.java 2009-08-19 11:39:30 UTC (rev 92572)
@@ -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.constructor;
+
+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 AnnotatedConstructorTestSuite extends TestSuite
+{
+ public static void main(String[] args)
+ {
+ TestRunner.run(suite());
+ }
+
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("Annotated Constructor Tests");
+
+ suite.addTest(AnnotatedConstructorTestCase.suite());
+
+ return suite;
+ }
+}
More information about the jboss-cvs-commits
mailing list