[jboss-cvs] JBossAS SVN: r103344 - in projects/scanning/trunk: deployers/src/main/java/org/jboss/scanning/deployers and 10 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Mar 31 11:12:14 EDT 2010


Author: alesj
Date: 2010-03-31 11:12:13 -0400 (Wed, 31 Mar 2010)
New Revision: 103344

Added:
   projects/scanning/trunk/testsuite/src/test/java/org/
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/AnnotationsHolder.java
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoader.java
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoaderSystem.java
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoaderSystemDeployer.java
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/TestAnnotation.java
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/test/
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/test/AnnotationEnvTestCase.java
   projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/test/AnnotationsTest.java
Modified:
   projects/scanning/trunk/deployers/src/main/java/org/jboss/scanning/deployers/ScanningDeployer.java
   projects/scanning/trunk/pom.xml
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/DefaultScanner.java
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/DeploymentUnitScanner.java
   projects/scanning/trunk/testsuite/pom.xml
Log:
Port annotations scanning tests.

Modified: projects/scanning/trunk/deployers/src/main/java/org/jboss/scanning/deployers/ScanningDeployer.java
===================================================================
--- projects/scanning/trunk/deployers/src/main/java/org/jboss/scanning/deployers/ScanningDeployer.java	2010-03-31 15:08:13 UTC (rev 103343)
+++ projects/scanning/trunk/deployers/src/main/java/org/jboss/scanning/deployers/ScanningDeployer.java	2010-03-31 15:12:13 UTC (rev 103344)
@@ -31,9 +31,10 @@
 import org.jboss.deployers.spi.deployer.DeploymentStages;
 import org.jboss.deployers.spi.deployer.helpers.AbstractDeployer;
 import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.deployers.structure.spi.DeploymentUnitFilter;
 import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
-import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnitFilter;
 import org.jboss.scanning.plugins.DeploymentScanningPluginFactory;
+import org.jboss.scanning.plugins.DeploymentUnitScanner;
 import org.jboss.scanning.spi.ScanningPlugin;
 import org.jboss.scanning.spi.helpers.AbstractScanner;
 
@@ -47,7 +48,7 @@
    /** The scanning plugins attachment key */
    public static String PLUGINS_KEY = "SCANNING_PLUGINS_KEY";
    /** The filter */
-   private VFSDeploymentUnitFilter filter;
+   private DeploymentUnitFilter filter;
    /** The plugin factories */
    private Set<DeploymentScanningPluginFactory> factories = new CopyOnWriteArraySet<DeploymentScanningPluginFactory>();
 
@@ -59,13 +60,8 @@
 
    public void deploy(DeploymentUnit unit) throws DeploymentException
    {
-      if (unit instanceof VFSDeploymentUnit == false)
-         return;
-
-      VFSDeploymentUnit vdu = VFSDeploymentUnit.class.cast(unit);
-
       // running this deployer is costly, check if it should be run
-      if (filter != null && filter.accepts(vdu) == false)
+      if (filter != null && filter.accepts(unit) == false)
          return;
 
       Set<ScanningPlugin> plugins = new HashSet<ScanningPlugin>();
@@ -77,12 +73,16 @@
 
       if (plugins.isEmpty() == false)
       {
+         VFSDeploymentUnit vdu = null;
+         if (unit instanceof VFSDeploymentUnit)
+            vdu = VFSDeploymentUnit.class.cast(unit);
+
          try
          {
-            AbstractScanner scanner = new VFSDeploymentUnitScanner(vdu);
+            AbstractScanner scanner = (vdu != null) ? new VFSDeploymentUnitScanner(vdu) : new DeploymentUnitScanner(unit);
             scanner.setPlugins(plugins);
             scanner.scan();
-
+            // add used plugins as attachment
             unit.addAttachment(PLUGINS_KEY, plugins); // add used plugins as attachment
          }
          catch (Exception e)
@@ -123,7 +123,7 @@
     *
     * @param filter the vfs deployment filter.
     */
-   public void setFilter(VFSDeploymentUnitFilter filter)
+   public void setFilter(DeploymentUnitFilter filter)
    {
       this.filter = filter;
    }

Modified: projects/scanning/trunk/pom.xml
===================================================================
--- projects/scanning/trunk/pom.xml	2010-03-31 15:08:13 UTC (rev 103343)
+++ projects/scanning/trunk/pom.xml	2010-03-31 15:12:13 UTC (rev 103344)
@@ -163,6 +163,18 @@
       </dependency>
       <dependency>
         <groupId>org.jboss.deployers</groupId>
+        <artifactId>jboss-deployers-impl</artifactId>
+        <version>${version.org.jboss.deployers}</version>
+        <exclusions>
+            <exclusion>
+                <groupId>org.jboss.mcann</groupId>
+                <artifactId>mcann-core</artifactId>
+            </exclusion>
+        </exclusions>
+        <type>test-jar</type>
+      </dependency>
+      <dependency>
+        <groupId>org.jboss.deployers</groupId>
         <artifactId>jboss-deployers-vfs</artifactId>
         <version>${version.org.jboss.deployers}</version>
         <exclusions>

Modified: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/DefaultScanner.java
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/DefaultScanner.java	2010-03-31 15:08:13 UTC (rev 103343)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/DefaultScanner.java	2010-03-31 15:12:13 UTC (rev 103344)
@@ -68,7 +68,18 @@
       }
       FederatedResourceVisitor visitor = new FederatedResourceVisitor(plugins.toArray(new ResourceVisitor[plugins.size()]));
       ResourceFilter filter = visitor.getFilter();
-      VFSResourceVisitor.visit(roots, excludedRoots, included, excluded, classLoader, visitor, filter, recurseFilter, urls);
+
+      // something in javassist uses TCL
+      ClassLoader tcl = Thread.currentThread().getContextClassLoader();
+      Thread.currentThread().setContextClassLoader(classLoader);
+      try
+      {
+         VFSResourceVisitor.visit(roots, excludedRoots, included, excluded, classLoader, visitor, filter, recurseFilter, urls);
+      }
+      finally
+      {
+         Thread.currentThread().setContextClassLoader(tcl);
+      }
    }
 
    @Override

Modified: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/DeploymentUnitScanner.java
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/DeploymentUnitScanner.java	2010-03-31 15:08:13 UTC (rev 103343)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/DeploymentUnitScanner.java	2010-03-31 15:12:13 UTC (rev 103344)
@@ -63,7 +63,18 @@
       ScanningPlugin[] array = plugins.toArray(new ScanningPlugin[plugins.size()]);
       FederatedResourceVisitor visitor = new FederatedResourceVisitor(array, array, null);
       ResourceFilter filter = visitor.getFilter();
-      module.visit(visitor, filter, recurseFilter, getRoots());
+
+      // something in javassist uses TCL
+      ClassLoader tcl = Thread.currentThread().getContextClassLoader();
+      Thread.currentThread().setContextClassLoader(unit.getClassLoader());
+      try
+      {
+         module.visit(visitor, filter, recurseFilter, getRoots());
+      }
+      finally
+      {
+         Thread.currentThread().setContextClassLoader(tcl);
+      }
    }
 
    @SuppressWarnings("unchecked")

Modified: projects/scanning/trunk/testsuite/pom.xml
===================================================================
--- projects/scanning/trunk/testsuite/pom.xml	2010-03-31 15:08:13 UTC (rev 103343)
+++ projects/scanning/trunk/testsuite/pom.xml	2010-03-31 15:12:13 UTC (rev 103344)
@@ -80,6 +80,12 @@
     </dependency>
     <dependency>
       <groupId>org.jboss.deployers</groupId>
+      <artifactId>jboss-deployers-impl</artifactId>
+      <scope>test</scope>
+      <type>test-jar</type>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.deployers</groupId>
       <artifactId>jboss-deployers-vfs</artifactId>
       <scope>test</scope>
       <type>test-jar</type>

Added: projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/AnnotationsHolder.java
===================================================================
--- projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/AnnotationsHolder.java	                        (rev 0)
+++ projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/AnnotationsHolder.java	2010-03-31 15:12:13 UTC (rev 103344)
@@ -0,0 +1,44 @@
+/*
+* 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.scanning.annotations.support;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+ at TestAnnotation("class")
+public class AnnotationsHolder
+{
+   @TestAnnotation("field")
+   @SuppressWarnings("unused")
+   private Object object;
+
+   @TestAnnotation("constructor")
+   public AnnotationsHolder(@TestAnnotation("cparameter") Object object)
+   {
+   }
+
+   @TestAnnotation("method")
+   public void something(@TestAnnotation("mparameter") Object object)
+   {
+      System.out.println("object = " + object);
+   }
+}

Added: projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoader.java
===================================================================
--- projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoader.java	                        (rev 0)
+++ projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoader.java	2010-03-31 15:12:13 UTC (rev 103344)
@@ -0,0 +1,115 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.scanning.annotations.support;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * InterceptionClassLoader.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class InterceptionClassLoader extends ClassLoader
+{
+   private ClassLoader delegate;
+   private Set<String> loaded = new HashSet<String>();
+
+   public InterceptionClassLoader(ClassLoader delegate)
+   {
+      super();
+
+      if (delegate == null)
+         throw new IllegalArgumentException("Null delegate.");
+      this.delegate = delegate;
+   }
+
+   public Set<String> getLoaded()
+   {
+      return loaded;
+   }
+
+   public ClassLoader getDelegate()
+   {
+      return delegate;
+   }
+
+   public Class<?> loadClass(String name) throws ClassNotFoundException
+   {
+      loaded.add(name);
+      return delegate.loadClass(name);
+   }
+
+   public URL getResource(String name)
+   {
+      return delegate.getResource(name);
+   }
+
+   public Enumeration<URL> getResources(String name) throws IOException
+   {
+      return delegate.getResources(name);
+   }
+
+   public InputStream getResourceAsStream(String name)
+   {
+      return delegate.getResourceAsStream(name);
+   }
+
+   public synchronized void setDefaultAssertionStatus(boolean enabled)
+   {
+      delegate.setDefaultAssertionStatus(enabled);
+   }
+
+   public synchronized void setPackageAssertionStatus(String packageName, boolean enabled)
+   {
+      delegate.setPackageAssertionStatus(packageName, enabled);
+   }
+
+   public synchronized void setClassAssertionStatus(String className, boolean enabled)
+   {
+      delegate.setClassAssertionStatus(className, enabled);
+   }
+
+   public synchronized void clearAssertionStatus()
+   {
+      delegate.clearAssertionStatus();
+   }
+
+   public int hashCode()
+   {
+      return delegate.hashCode();
+   }
+
+   public boolean equals(Object obj)
+   {
+      return delegate.equals(obj);
+   }
+
+   public String toString()
+   {
+      return delegate.toString();
+   }
+}
\ No newline at end of file

Added: projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoaderSystem.java
===================================================================
--- projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoaderSystem.java	                        (rev 0)
+++ projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoaderSystem.java	2010-03-31 15:12:13 UTC (rev 103344)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.scanning.annotations.support;
+
+import org.jboss.classloader.plugins.system.DefaultClassLoaderSystem;
+import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderPolicy;
+
+/**
+ * InterceptionClassLoaderSystemDeployer.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class InterceptionClassLoaderSystem extends DefaultClassLoaderSystem
+{
+   @Override
+   public ClassLoader registerClassLoaderPolicy(ClassLoaderDomain domain, ClassLoaderPolicy policy)
+   {
+      return wrapClassLoader(super.registerClassLoaderPolicy(domain, policy));
+   }
+
+   protected ClassLoader wrapClassLoader(ClassLoader loader)
+   {
+      return new InterceptionClassLoader(loader);
+   }
+}
\ No newline at end of file

Added: projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoaderSystemDeployer.java
===================================================================
--- projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoaderSystemDeployer.java	                        (rev 0)
+++ projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/InterceptionClassLoaderSystemDeployer.java	2010-03-31 15:12:13 UTC (rev 103344)
@@ -0,0 +1,76 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.scanning.annotations.support;
+
+import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloading.spi.dependency.Module;
+import org.jboss.deployers.plugins.classloading.AbstractLevelClassLoaderSystemDeployer;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+
+/**
+ * InterceptionClassLoaderSystemDeployer.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class InterceptionClassLoaderSystemDeployer extends AbstractLevelClassLoaderSystemDeployer
+{
+   @Override
+   public void removeClassLoader(DeploymentUnit unit) throws Exception
+   {
+      Module module = unit.getAttachment(Module.class);
+      if (module == null)
+         return;
+
+      ClassLoader classLoader = unit.getClassLoader();
+      if (classLoader instanceof InterceptionClassLoader)
+      {
+         InterceptionClassLoader icl = (InterceptionClassLoader)classLoader;
+         classLoader = icl.getDelegate();
+      }
+
+      try
+      {
+         try
+         {
+            // Remove the classloader
+            getSystem().unregisterClassLoader(classLoader);
+         }
+         finally
+         {
+            // Try to tidy up empty domains
+            String domainName = module.getDeterminedDomainName();
+            if (ClassLoaderSystem.DEFAULT_DOMAIN_NAME.equals(domainName) == false)
+            {
+               ClassLoaderDomain domain = getSystem().getDomain(domainName);
+               if (domain.hasClassLoaders() == false)
+                  getSystem().unregisterDomain(domain);
+            }
+         }
+      }
+      finally
+      {
+         cleanup(unit, module);
+         module.reset();
+      }
+   }
+}
\ No newline at end of file

Added: projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/TestAnnotation.java
===================================================================
--- projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/TestAnnotation.java	                        (rev 0)
+++ projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/support/TestAnnotation.java	2010-03-31 15:12:13 UTC (rev 103344)
@@ -0,0 +1,37 @@
+/*
+* 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.scanning.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="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+ at Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface TestAnnotation
+{
+   String value() default "";
+}

Added: projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/test/AnnotationEnvTestCase.java
===================================================================
--- projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/test/AnnotationEnvTestCase.java	                        (rev 0)
+++ projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/test/AnnotationEnvTestCase.java	2010-03-31 15:12:13 UTC (rev 103344)
@@ -0,0 +1,212 @@
+/*
+* 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.scanning.annotations.test;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Set;
+
+import org.jboss.classloader.plugins.ClassLoaderUtils;
+import org.jboss.deployers.client.spi.DeployerClient;
+import org.jboss.deployers.client.spi.Deployment;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.scanning.annotations.spi.AnnotationRepository;
+import org.jboss.scanning.annotations.spi.Element;
+import org.jboss.test.scanning.annotations.support.AnnotationsHolder;
+import org.jboss.test.scanning.annotations.support.TestAnnotation;
+
+import junit.framework.Test;
+
+/**
+ * AnnotationEnvTestCase.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class AnnotationEnvTestCase extends AnnotationsTest
+{
+   public AnnotationEnvTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(AnnotationEnvTestCase.class);
+   }
+
+   @SuppressWarnings("unchecked")
+   public void testDirectClassUsage() throws Exception
+   {
+      DeployerClient deployer = getMainDeployer();
+
+      Deployment deployment = createSimpleDeployment("a");
+      addClassLoadingMetaData(
+            deployment,
+            deployment.getName(),
+            null,
+            ClassLoaderUtils.classNameToPath("org.jboss.test.scanning.annotations.support.AnnotationsHolder"),
+            ClassLoaderUtils.classNameToPath("org.jboss.test.scanning.annotations.support.TestAnnotation"),
+            ClassLoaderUtils.classNameToPath("org.jboss.reflect.plugins.javassist.JavassistMethod")
+      );
+
+      DeploymentUnit unit = assertDeploy(deployer, deployment);
+      try
+      {
+         ClassLoader cl = unit.getClassLoader();
+         Class<TestAnnotation> taClass = (Class<TestAnnotation>)cl.loadClass("org.jboss.test.scanning.annotations.support.TestAnnotation");
+
+         assertNotLoaded(unit, "org.jboss.test.scanning.annotations.support.AnnotationsHolder");
+         // annotations are loaded, OK?
+         assertLoaded(unit, "org.jboss.test.scanning.annotations.support.TestAnnotation");
+
+         AnnotationRepository env = getAnnotationRepository(unit);
+         Set<Element<TestAnnotation, Class<?>>> classes = env.classIsAnnotatedWith(taClass);
+         assertNotNull(classes);
+         assertEquals(1, classes.size());
+         assertEquals(AnnotationsHolder.class.getName(), classes.iterator().next().getOwnerClassName());
+         assertNotLoaded(unit, "org.jboss.test.scanning.annotations.support.AnnotationsHolder");
+
+         Element<TestAnnotation, Class<?>> ecl = getSingleton(env.classIsAnnotatedWith(taClass));
+         Annotation tacl = ecl.getAnnotation();
+         assertNotNull(tacl);
+         assertEquals("class", getValue(tacl));
+
+         Element<TestAnnotation, Constructor<?>> ec = getSingleton(env.classHasConstructorAnnotatedWith(taClass));
+         Annotation ta = ec.getAnnotation();
+         assertNotNull(ta);
+         assertEquals("constructor", getValue(ta));
+         assertInstanceOf(ec.getAnnotatedElement(), Constructor.class, false);
+
+         Element<TestAnnotation, Field> ef = getSingleton(env.classHasFieldAnnotatedWith(taClass));
+         ta = ef.getAnnotation();
+         assertNotNull(ta);
+         assertEquals("field", getValue(ta));
+         assertInstanceOf(ef.getAnnotatedElement(), Field.class, false);
+
+         Element<TestAnnotation, Method> em = getSingleton(env.classHasMethodAnnotatedWith(taClass));
+         ta = em.getAnnotation();
+         assertNotNull(ta);
+         assertEquals("method", getValue(ta));
+         assertInstanceOf(em.getAnnotatedElement(), Method.class, false);
+
+         Set<Element<TestAnnotation, AnnotatedElement>> eps = env.classHasParameterAnnotatedWith(taClass);
+         assertNotNull(eps);
+         assertEquals(2, eps.size());
+         for (Element<TestAnnotation, AnnotatedElement> ep : eps)
+         {
+            ta = ep.getAnnotation();
+            assertNotNull(ta);
+            Object value = getValue(ta);
+            AnnotatedElement ao = ep.getAnnotatedElement();
+            if ("cparameter".equals(value))
+               assertInstanceOf(ao, Constructor.class, false);
+            else if ("mparameter".equals(value))
+               assertInstanceOf(ao, Method.class, false);
+            else
+               fail("Illegal annotation value: " + value);
+         }
+      }
+      finally
+      {
+         assertUndeploy(deployer, deployment);
+      }
+   }
+
+   public void testSimpleClassUsage() throws Exception
+   {
+      DeployerClient deployer = getMainDeployer();
+
+      Deployment deployment = createSimpleDeployment("a");
+      addClassLoadingMetaData(
+            deployment,
+            deployment.getName(),
+            null,
+            ClassLoaderUtils.classNameToPath("org.jboss.test.scanning.annotations.support.AnnotationsHolder"),
+            ClassLoaderUtils.classNameToPath("org.jboss.test.scanning.annotations.support.TestAnnotation"),
+            ClassLoaderUtils.classNameToPath("org.jboss.reflect.plugins.javassist.JavassistMethod")
+      );
+
+      DeploymentUnit unit = assertDeploy(deployer, deployment);
+      try
+      {
+         String annotationName = "org.jboss.test.scanning.annotations.support.TestAnnotation";
+
+         assertNotLoaded(unit, "org.jboss.test.scanning.annotations.support.AnnotationsHolder");
+         // annotations are loaded, OK?
+         assertLoaded(unit, "org.jboss.test.scanning.annotations.support.TestAnnotation");
+
+         AnnotationRepository env = getAnnotationRepository(unit);
+         Set<Element<Annotation, Class<?>>> classes = env.classIsAnnotatedWith(annotationName);
+         assertNotNull(classes);
+         assertEquals(1, classes.size());
+         assertEquals(AnnotationsHolder.class.getName(), classes.iterator().next().getOwnerClassName());
+         assertNotLoaded(unit, "org.jboss.test.scanning.annotations.support.AnnotationsHolder");
+
+         Element<Annotation, Class<?>> ecl = getSingleton(env.classIsAnnotatedWith(annotationName));
+         Annotation tacl = ecl.getAnnotation();
+         assertNotNull(tacl);
+         assertEquals("class", getValue(tacl));
+
+         Element<Annotation, Constructor<?>> ec = getSingleton(env.classHasConstructorAnnotatedWith(annotationName));
+         Annotation ta = ec.getAnnotation();
+         assertNotNull(ta);
+         assertEquals("constructor", getValue(ta));
+         assertInstanceOf(ec.getAnnotatedElement(), Constructor.class, false);
+
+         Element<Annotation, Field> ef = getSingleton(env.classHasFieldAnnotatedWith(annotationName));
+         ta = ef.getAnnotation();
+         assertNotNull(ta);
+         assertEquals("field", getValue(ta));
+         assertInstanceOf(ef.getAnnotatedElement(), Field.class, false);
+
+         Element<Annotation, Method> em = getSingleton(env.classHasMethodAnnotatedWith(annotationName));
+         ta = em.getAnnotation();
+         assertNotNull(ta);
+         assertEquals("method", getValue(ta));
+         assertInstanceOf(em.getAnnotatedElement(), Method.class, false);
+
+         Set<Element<Annotation, AnnotatedElement>> eps = env.classHasParameterAnnotatedWith(annotationName);
+         assertNotNull(eps);
+         assertEquals(2, eps.size());
+         for (Element<Annotation, AnnotatedElement> ep : eps)
+         {
+            ta = ep.getAnnotation();
+            assertNotNull(ta);
+            Object value = getValue(ta);
+            AnnotatedElement ao = ep.getAnnotatedElement();
+            if ("cparameter".equals(value))
+               assertInstanceOf(ao, Constructor.class, false);
+            else if ("mparameter".equals(value))
+               assertInstanceOf(ao, Method.class, false);
+            else
+               fail("Illegal annotation value: " + value);
+         }
+      }
+      finally
+      {
+         assertUndeploy(deployer, deployment);
+      }
+   }
+}

Added: projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/test/AnnotationsTest.java
===================================================================
--- projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/test/AnnotationsTest.java	                        (rev 0)
+++ projects/scanning/trunk/testsuite/src/test/java/org/jboss/test/scanning/annotations/test/AnnotationsTest.java	2010-03-31 15:12:13 UTC (rev 103344)
@@ -0,0 +1,204 @@
+/*
+* 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.scanning.annotations.test;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.util.Set;
+
+import org.jboss.classloader.plugins.filter.CombiningClassFilter;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.spi.ParentPolicy;
+import org.jboss.classloader.spi.filter.ClassFilter;
+import org.jboss.classloader.spi.filter.ClassFilterUtils;
+import org.jboss.classloader.spi.filter.PackageClassFilter;
+import org.jboss.classloading.spi.dependency.ClassLoading;
+import org.jboss.classloading.spi.dependency.policy.mock.MockClassLoadingMetaData;
+import org.jboss.classloading.spi.metadata.CapabilitiesMetaData;
+import org.jboss.classloading.spi.metadata.Capability;
+import org.jboss.classloading.spi.metadata.ClassLoadingMetaData;
+import org.jboss.classloading.spi.metadata.ClassLoadingMetaDataFactory;
+import org.jboss.classloading.spi.version.Version;
+import org.jboss.deployers.client.spi.DeployerClient;
+import org.jboss.deployers.plugins.classloading.AbstractClassLoaderDescribeDeployer;
+import org.jboss.deployers.spi.attachments.Attachments;
+import org.jboss.deployers.spi.attachments.MutableAttachments;
+import org.jboss.deployers.spi.attachments.PredeterminedManagedObjectAttachments;
+import org.jboss.deployers.spi.deployer.Deployer;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.scanning.annotations.plugins.AnnotationsScanningPluginFactory;
+import org.jboss.scanning.annotations.spi.AnnotationIndex;
+import org.jboss.scanning.annotations.spi.AnnotationRepository;
+import org.jboss.scanning.annotations.spi.Element;
+import org.jboss.scanning.deployers.ScanningDeployer;
+import org.jboss.test.deployers.AbstractDeployerTest;
+import org.jboss.test.deployers.classloading.support.MockClassLoaderDescribeDeployer;
+import org.jboss.test.scanning.annotations.support.InterceptionClassLoader;
+import org.jboss.test.scanning.annotations.support.InterceptionClassLoaderSystem;
+import org.jboss.test.scanning.annotations.support.InterceptionClassLoaderSystemDeployer;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public abstract class AnnotationsTest extends AbstractDeployerTest
+{
+   private static ClassLoadingMetaDataFactory classLoadingMetaDataFactory = ClassLoadingMetaDataFactory.getInstance();
+
+   protected AbstractClassLoaderDescribeDeployer deployer1;
+   protected InterceptionClassLoaderSystemDeployer deployer2;
+
+   protected AnnotationsTest(String name)
+   {
+      super(name);
+   }
+
+   protected static ClassLoadingMetaData addClassLoadingMetaData(PredeterminedManagedObjectAttachments deployment, String name, Version version, String... paths)
+   {
+      return addClassLoadingMetaData(deployment, name, version, false, paths);
+   }
+
+   protected static ClassLoadingMetaData addClassLoadingMetaData(PredeterminedManagedObjectAttachments deployment, String name, Version version, boolean useVersionOnPackages, String... paths)
+   {
+      ClassLoadingMetaData classLoadingMetaData = createMetaData(name, version, useVersionOnPackages, paths);
+      addMetaData(deployment, classLoadingMetaData);
+      return classLoadingMetaData;
+   }
+
+   protected static ClassLoadingMetaData createMetaData(String name, Version version, String... paths)
+   {
+      return createMetaData(name, version, false, paths);
+   }
+
+   protected static ClassLoadingMetaData createMetaData(String name, Version version, boolean useVersionOnPackages, String... paths)
+   {
+      MockClassLoadingMetaData classLoadingMetaData = new MockClassLoadingMetaData(name, version);
+
+      classLoadingMetaData.setPaths(paths);
+
+      CapabilitiesMetaData capabilities = classLoadingMetaData.getCapabilities();
+      Capability capability = classLoadingMetaDataFactory.createModule(name, version);
+      capabilities.addCapability(capability);
+
+      if (paths != null)
+      {
+         for (String path : paths)
+         {
+            if (useVersionOnPackages)
+               capability = classLoadingMetaDataFactory.createPackage(path, version);
+            else
+               capability = classLoadingMetaDataFactory.createPackage(path);
+            capabilities.addCapability(capability);
+         }
+      }
+
+      classLoadingMetaData.setCapabilities(capabilities);
+      return classLoadingMetaData;
+   }
+
+   protected static void addMetaData(PredeterminedManagedObjectAttachments attachments, ClassLoadingMetaData md)
+   {
+      MutableAttachments mutable = (MutableAttachments) attachments.getPredeterminedManagedObjects();
+      mutable.addAttachment(ClassLoadingMetaData.class, md);
+   }
+
+   protected AnnotationRepository getAnnotationRepository(Attachments attachments)
+   {
+      AnnotationRepository env = attachments.getAttachment(AnnotationIndex.class);
+      assertNotNull(env);
+      return env;
+   }
+
+   protected <A extends Annotation, M extends AnnotatedElement> Element<A,M> getSingleton(Set<Element<A,M>> elements)
+   {
+      assertNotNull(elements);
+      assertEquals(1, elements.size());
+      return elements.iterator().next();
+   }
+
+   protected Object getValue(Annotation annotation) throws Exception
+   {
+      assertNotNull(annotation);
+      Class<? extends Annotation> clazz = annotation.annotationType();
+      return clazz.getMethod("value").invoke(annotation);
+   }
+
+   protected void assertNotLoaded(DeploymentUnit unit, String className)
+   {
+      assertCheckLoaded(unit, className, false);
+   }
+
+   protected void assertLoaded(DeploymentUnit unit, String className)
+   {
+      assertCheckLoaded(unit, className, true);
+   }
+
+   private void assertCheckLoaded(DeploymentUnit unit, String className, boolean checkFlag)
+   {
+      ClassLoader cl = unit.getClassLoader();
+      if (cl instanceof InterceptionClassLoader == false)
+         throw new IllegalArgumentException("Expecting InterceptionClassLoader instance: " + cl);
+
+      InterceptionClassLoader icl = (InterceptionClassLoader)cl;
+      Set<String> loaded = icl.getLoaded();
+      assertNotNull(loaded);
+      assertEquals(checkFlag, loaded.contains(className));
+   }
+
+   protected DeployerClient getMainDeployer(Deployer... deployers)
+   {
+      ClassLoading classLoading = new ClassLoading();
+      ClassLoaderSystem system = new InterceptionClassLoaderSystem();
+      // we need to see javassist
+      ClassFilter javassistFilter = new PackageClassFilter(new String[]{"org.jboss.reflect.plugins.javassist"});
+      ClassFilter before = new CombiningClassFilter(false, new ClassFilter[]{ClassFilterUtils.JAVA_ONLY, javassistFilter});
+      ParentPolicy parentPolicy = new ParentPolicy(before, ClassFilterUtils.NOTHING);
+      system.getDefaultDomain().setParentPolicy(parentPolicy);
+
+      deployer1 = new MockClassLoaderDescribeDeployer();
+      deployer1.setClassLoading(classLoading);
+
+      deployer2 = new InterceptionClassLoaderSystemDeployer();
+      deployer2.setClassLoading(classLoading);
+      deployer2.setSystem(system);
+
+      Deployer deployer3 = createScanningDeployer();
+
+      if (deployers != null && deployers.length > 0)
+      {
+         Deployer[] allDeployers = new Deployer[deployers.length + 3];
+         allDeployers[0] = deployer1;
+         allDeployers[1] = deployer2;
+         allDeployers[2] = deployer3;
+         System.arraycopy(deployers, 0, allDeployers, 3, deployers.length);
+         return createMainDeployer(allDeployers);
+      }
+
+      return createMainDeployer(deployer1, deployer2, deployer3);
+   }
+
+   protected Deployer createScanningDeployer()
+   {
+      ScanningDeployer deployer = new ScanningDeployer();
+      deployer.addPlugin(new AnnotationsScanningPluginFactory());
+      return deployer;
+   }
+}




More information about the jboss-cvs-commits mailing list