[jboss-cvs] JBossAS SVN: r67333 - in projects/metadata/trunk/src/test/java/org/jboss/test/metadata: common and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Nov 21 08:32:55 EST 2007
Author: wolfc
Date: 2007-11-21 08:32:55 -0500 (Wed, 21 Nov 2007)
New Revision: 67333
Added:
projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/ClassHelper.java
projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/PackageScanner.java
projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/ScanPackage.java
projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/StackTraceHelper.java
Modified:
projects/metadata/trunk/src/test/java/org/jboss/test/metadata/annotation/ejb3/AnnotationEjb3UnitTestCase.java
Log:
JBAS-4506: ScanPackage annotation for unit tests
Modified: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/annotation/ejb3/AnnotationEjb3UnitTestCase.java
===================================================================
--- projects/metadata/trunk/src/test/java/org/jboss/test/metadata/annotation/ejb3/AnnotationEjb3UnitTestCase.java 2007-11-21 12:28:15 UTC (rev 67332)
+++ projects/metadata/trunk/src/test/java/org/jboss/test/metadata/annotation/ejb3/AnnotationEjb3UnitTestCase.java 2007-11-21 13:32:55 UTC (rev 67333)
@@ -83,8 +83,11 @@
import org.jboss.metadata.javaee.spec.SecurityRolesMetaData;
import org.jboss.test.metadata.annotation.ejb3.multiview.Multiview21Remote;
import org.jboss.test.metadata.annotation.ejb3.multiview.Multiview3Remote;
+import org.jboss.test.metadata.annotation.ejb3.multiview.MultiviewBean;
import org.jboss.test.metadata.annotation.ejb3.multiview.MultiviewHome;
import org.jboss.test.metadata.annotation.ejb3.runas.InterMediate;
+import org.jboss.test.metadata.common.PackageScanner;
+import org.jboss.test.metadata.common.ScanPackage;
import org.jboss.test.metadata.common.SetHelper;
import org.jboss.test.metadata.javaee.AbstractJavaEEMetaDataTest;
import org.jboss.xb.binding.sunday.unmarshalling.DefaultSchemaResolver;
@@ -521,11 +524,13 @@
assertEquals("InternalUser", runAs.getRoleName());
}
+ @SuppressWarnings("unchecked")
+ @ScanPackage("org.jboss.test.metadata.annotation.ejb3.multiview")
public void testMultiview() throws Exception
{
AnnotationFinder<AnnotatedElement> finder = new DefaultAnnotationFinder<AnnotatedElement>();
- Collection<Class> classes = loadClassesFromRelativeClassDir("./multiview");
+ Collection<Class> classes = (Collection<Class>) PackageScanner.loadClasses();
System.out.println("Processing classes: "+classes);
enableTrace("org.jboss.metadata.annotation.creator");
Added: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/ClassHelper.java
===================================================================
--- projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/ClassHelper.java (rev 0)
+++ projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/ClassHelper.java 2007-11-21 13:32:55 UTC (rev 67333)
@@ -0,0 +1,73 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, Red Hat Middleware LLC, 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.metadata.common;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Useful methods on classes.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class ClassHelper
+{
+ /**
+ * Find that one method.
+ *
+ * @param cls the class to scan
+ * @param methodName the method to find
+ * @return the method
+ * @throws NoSuchMethodException method can't be found
+ * @throws RuntimeException multiple methods found
+ */
+ public static Method getMethodByName(Class<?> cls, String methodName) throws NoSuchMethodException
+ {
+ List<Method> methods = getMethodsByName(cls, methodName);
+ if(methods.size() == 0)
+ throw new NoSuchMethodException(methodName + " on " + cls);
+ if(methods.size() > 1)
+ throw new RuntimeException("Doh! Too many methods named " + methodName + " on " + cls);
+ return methods.get(0);
+ }
+
+ /**
+ * Find all methods with a specific name on a class regardless
+ * of parameter signature.
+ *
+ * @param cls the class to scan
+ * @param methodName the name of the methods to find
+ * @return a list of methods found, or empty
+ */
+ public static List<Method> getMethodsByName(Class<?> cls, String methodName)
+ {
+ List<Method> methods = new ArrayList<Method>();
+ for(Method method : cls.getDeclaredMethods())
+ {
+ if(method.getName().equals(methodName))
+ methods.add(method);
+ }
+ return methods;
+ }
+}
Property changes on: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/ClassHelper.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/PackageScanner.java
===================================================================
--- projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/PackageScanner.java (rev 0)
+++ projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/PackageScanner.java 2007-11-21 13:32:55 UTC (rev 67333)
@@ -0,0 +1,117 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, Red Hat Middleware LLC, 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.metadata.common;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.lang.reflect.Method;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * The package scanner allows loading of all classes from a package.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class PackageScanner
+{
+ /**
+ * Load all classes a package. The package to load the classes
+ * from is determined by the existance of the ScanPackage annotation on either
+ * the calling method, or the calling class. If the ScanPackage annotation
+ * exists on the method, the package specified in the annotation will be used.
+ * If it exists on the classes, the package specified there will be used.
+ * If no annotation can be found, the package in which the caller resides is loaded.
+ *
+ * @return a collection of classes found
+ */
+ public static Collection<Class<?>> loadClasses()
+ {
+ Method caller = StackTraceHelper.whoCalledMe();
+ ScanPackage annotation = caller.getAnnotation(ScanPackage.class);
+ if(annotation == null)
+ annotation = caller.getDeclaringClass().getAnnotation(ScanPackage.class);
+ // Don't try Package.getPackage, might return null
+ if(annotation == null)
+ return loadClasses(caller.getDeclaringClass().getPackage());
+ else
+ return loadClasses(annotation.value());
+ }
+
+ /**
+ * Load all classes from the specified package.
+ *
+ * @param packageName the name of the package
+ * @return all classes found in this package
+ */
+ public static Collection<Class<?>> loadClasses(String packageName)
+ {
+ Collection<Class<?>> classes = new ArrayList<Class<?>>();
+ String dir = packageName.replace('.', '/');
+ URL currentClassDirURL = Thread.currentThread().getContextClassLoader().getResource(dir);
+ File currentDir;
+ try
+ {
+ currentDir = new File(currentClassDirURL.toURI());
+ }
+ catch (URISyntaxException e)
+ {
+ throw new RuntimeException(e);
+ }
+ String classFileNames[] = currentDir.list(new FilenameFilter() {
+ public boolean accept(File dir, String name)
+ {
+ return name.endsWith(".class");
+ }
+ });
+ if(classFileNames == null)
+ throw new RuntimeException("list failed");
+
+ Arrays.sort(classFileNames);
+
+ if(packageName.length() > 0)
+ packageName += ".";
+
+ for(String classFileName : classFileNames)
+ {
+ String className = packageName + classFileName.substring(0, classFileName.length() - 6);
+ try
+ {
+ classes.add(Class.forName(className));
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ return classes;
+ }
+
+ public static Collection<Class<?>> loadClasses(Package pkg)
+ {
+ return loadClasses(pkg.getName());
+ }
+}
Property changes on: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/PackageScanner.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/ScanPackage.java
===================================================================
--- projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/ScanPackage.java (rev 0)
+++ projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/ScanPackage.java 2007-11-21 13:32:55 UTC (rev 67333)
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, Red Hat Middleware LLC, 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.metadata.common;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Specifies which package the package scanner loads by default.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Target({ElementType.METHOD, ElementType.TYPE})
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface ScanPackage {
+ String value();
+}
Property changes on: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/ScanPackage.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/StackTraceHelper.java
===================================================================
--- projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/StackTraceHelper.java (rev 0)
+++ projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/StackTraceHelper.java 2007-11-21 13:32:55 UTC (rev 67333)
@@ -0,0 +1,77 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, Red Hat Middleware LLC, 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.metadata.common;
+
+import java.lang.reflect.Method;
+
+/**
+ * Allows better use of StackTrace.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class StackTraceHelper
+{
+ private static int indexOfMe(StackTraceElement stackTrace[])
+ {
+ for(int i = 0; i < stackTrace.length; i++)
+ {
+ if(stackTrace[i].getClassName().equals(StackTraceHelper.class.getName()))
+ return i;
+ }
+ return -1;
+ }
+
+ /**
+ * Finds the method who called upon the caller of this method.
+ * <br/>
+ * <b>TODO: </b>If the caller has multiple methods with the same name, this
+ * method fails.
+ *
+ * @return the method who called the caller
+ */
+ public static Method whoCalledMe()
+ {
+ try
+ {
+ StackTraceElement stackTrace[] = Thread.currentThread().getStackTrace();
+ // Make sure we work on other VM implementations
+ int i = indexOfMe(stackTrace);
+ assert i != -1;
+ StackTraceElement element = stackTrace[i + 2];
+ Class<?> cls = Class.forName(element.getClassName());
+ // TODO: this is not a safe way to find out the right method
+ Method method = ClassHelper.getMethodByName(cls, element.getMethodName());
+ return method;
+ }
+ catch(ClassNotFoundException e)
+ {
+ // Should not happen
+ throw new RuntimeException(e);
+ }
+ catch (NoSuchMethodException e)
+ {
+ // Should not happen
+ throw new RuntimeException(e);
+ }
+ }
+}
Property changes on: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/common/StackTraceHelper.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
More information about the jboss-cvs-commits
mailing list