[P-unit-devel] SF.net SVN: p-unit: [312] trunk/punit/src/org/punit
Status: Beta
Brought to you by:
zhanghuangzhu
|
From: <cu...@us...> - 2008-05-28 09:34:40
|
Revision: 312
http://p-unit.svn.sourceforge.net/p-unit/?rev=312&view=rev
Author: cuvavu
Date: 2008-05-28 02:34:47 -0700 (Wed, 28 May 2008)
Log Message:
-----------
Adding in a general method for dealing with before and after annotations. This is a bit bulky.
Modified Paths:
--------------
trunk/punit/src/org/punit/convention/AnnotationConvention.java
trunk/punit/src/org/punit/exception/AnnotationException.java
Modified: trunk/punit/src/org/punit/convention/AnnotationConvention.java
===================================================================
--- trunk/punit/src/org/punit/convention/AnnotationConvention.java 2008-05-28 00:44:46 UTC (rev 311)
+++ trunk/punit/src/org/punit/convention/AnnotationConvention.java 2008-05-28 09:34:47 UTC (rev 312)
@@ -3,6 +3,7 @@
package org.punit.convention;
import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
@@ -101,109 +102,98 @@
@SuppressWarnings("unchecked")
public Method[] getAfterClassMethod(Class test) {
- return getAfterClassMethod(test, false);
+ try {
+ return getClassAnnotationMethod(test, false, _afterClass, _delegate.getClass().getMethod("getAfterClassMethod", Class.class), true, false);
+ } catch (SecurityException e) {
+ throw new AnnotationException( "Problem finding method", e );
+ } catch (NoSuchMethodException e) {
+ throw new AnnotationException( "Problem finding method", e );
+ }
}
- @SuppressWarnings("unchecked")
- private Method[] getAfterClassMethod(Class test, boolean annotationOnly) {
- if (test == null) return new Method[0];
-
- Method[] annotatedMethods = AnnotationUtil.getMethodsByAnnotation(test, _afterClass);
- if (annotatedMethods != null && annotatedMethods.length > 1) throw new AnnotationException( "Too many @AfterClass annotations - only one allowed per class" );
-
- // Get the AfterClass methods from the superclasses (if any) - only look at annotations, though
- Method[] superMethods = getAfterClassMethod(test.getSuperclass(), true);
-
- // Look for un-anotated method in this class if there has been no annotated methods in superclass
- if (annotationOnly = false && (annotatedMethods == null || annotatedMethods.length == 0) && (superMethods == null || superMethods.length==0)) {
- return _delegate.getAfterClassMethod(test);
- }
-
- // This classes method needs to be on the list before its superclasses
- List<Method> methodsList = new ArrayList<Method>();
- if (annotatedMethods != null && annotatedMethods.length > 0) methodsList.add(annotatedMethods[0]);
- if (superMethods != null) methodsList.addAll(Arrays.asList(superMethods));
-
- return methodsList.size() == 0 ? null : methodsList.toArray(new Method[0]);
- }
@SuppressWarnings("unchecked")
public Method[] getBeforeClassMethod(Class test) {
- return getBeforeClassMethod(test, false);
+ try {
+ return getClassAnnotationMethod(test, false, _beforeClass, _delegate.getClass().getMethod("getBeforeClassMethod", Class.class), false, false);
+ } catch (SecurityException e) {
+ throw new AnnotationException( "Problem finding method", e );
+ } catch (NoSuchMethodException e) {
+ throw new AnnotationException( "Problem finding method", e );
+ }
}
- @SuppressWarnings("unchecked")
- private Method[] getBeforeClassMethod(Class test, boolean annotationOnly) {
- if (test == null) return new Method[0];
-
- Method[] annotatedMethods = AnnotationUtil
- .getMethodsByAnnotation(test, _beforeClass);
- if (annotatedMethods != null && annotatedMethods.length > 1) throw new AnnotationException( "Too many @BeforeClass annotations - only one allowed per class" );
- // Get the BeforeClass methods from the superclasses (if any) - only look at annotations, though
- Method[] superMethods = getBeforeClassMethod(test.getSuperclass(), true);
-
- // Look for un-anotated method in this class if there has been no annotated methods in superclass
- if (annotationOnly = false && (annotatedMethods == null || annotatedMethods.length == 0) && (superMethods == null || superMethods.length==0)) {
- return _delegate.getBeforeClassMethod(test);
- }
-
- // This classes method needs to be on the list after its superclasses
- List<Method> methodsList = new ArrayList<Method>();
- if (superMethods != null) methodsList.addAll(Arrays.asList(superMethods));
- if (annotatedMethods != null && annotatedMethods.length > 0) methodsList.add(annotatedMethods[0]);
-
- return methodsList.size() == 0 ? null : methodsList.toArray(new Method[0]);
- }
-
@SuppressWarnings("unchecked")
public Method[] getSetUpMethod(Class test) {
- return getSetUpMethod(test, false);
+ try {
+ return getClassAnnotationMethod(test, false, _before, _delegate.getClass().getMethod("getSetUpMethod", Class.class), false, true);
+ } catch (SecurityException e) {
+ throw new AnnotationException( "Problem finding method", e );
+ } catch (NoSuchMethodException e) {
+ throw new AnnotationException( "Problem finding method", e );
+ }
}
- @SuppressWarnings("unchecked")
- private Method[] getSetUpMethod(Class test, boolean annotationOnly) {
- if (test == null) return new Method[0];
-
- Method[] annotatedMethods = AnnotationUtil.getMethodsByAnnotation(test, _before);
-
- // Get the Before methods from the superclasses (if any)
- Method[] superMethods = getSetUpMethod(test.getSuperclass(), true);
-
- // Look for un-anotated method in this class if there has been no annotated methods in superclass
- if (annotationOnly = false && (annotatedMethods == null || annotatedMethods.length == 0) && (superMethods == null || superMethods.length==0)) {
- return _delegate.getSetUpMethod(test);
- }
-
- // This classes method needs to be on the list after its superclasses
- List<Method> methodsList = new ArrayList<Method>();
- if (superMethods != null) methodsList.addAll(Arrays.asList(superMethods));
- if (annotatedMethods != null) methodsList.addAll(Arrays.asList(annotatedMethods));
-
- return methodsList.size() == 0 ? null : methodsList.toArray(new Method[0]);
- }
@SuppressWarnings("unchecked")
public Method[] getTearDownMethod(Class test) {
- return getTearDownMethod(test, false);
+ try {
+ return getClassAnnotationMethod(test, false, _after, _delegate.getClass().getMethod("getTearDownMethod", Class.class), true, true);
+ } catch (SecurityException e) {
+ throw new AnnotationException( "Problem finding method", e );
+ } catch (NoSuchMethodException e) {
+ throw new AnnotationException( "Problem finding method", e );
+ }
}
+
+ /**
+ * Method to get, recursively, annotated methods from superclasses. This is a bit of a beast because
+ * it needs to account for for slightly different ways of doing things.
+ *
+ * @param test Class to test
+ * @param annotationOnly Check for old-style name based methods?
+ * @param annotation Annotation to look for
+ * @param delegateMethod Method to use when looking for alternatives name-based methods
+ * @param before Is this method an "after" method (or a before)?
+ * @param allowMultiple Allow multiple tests?
+ * @return
+ */
@SuppressWarnings("unchecked")
- private Method[] getTearDownMethod(Class test, boolean annotationOnly) {
+ private Method[] getClassAnnotationMethod(Class test,
+ boolean annotationOnly,
+ Class<? extends Annotation> annotation,
+ Method delegateMethod,
+ boolean after,
+ boolean allowMultiple) {
if (test == null) return new Method[0];
- Method[] annotatedMethods = AnnotationUtil.getMethodsByAnnotation(test, _after);
+ Method[] annotatedMethods = AnnotationUtil.getMethodsByAnnotation(test, annotation);
+ if (!allowMultiple && annotatedMethods != null && annotatedMethods.length > 1) throw new AnnotationException( "Too many " + annotation.getCanonicalName() + "annotations - only one allowed per class" );
- // Get the After methods from the superclasses (if any)
- Method[] superMethods = getTearDownMethod(test.getSuperclass(), true);
+ // Get the annotated methods from the superclasses (if any) - only look at annotations, though
+ Method[] superMethods = getClassAnnotationMethod(test.getSuperclass(), true, annotation, delegateMethod, after, allowMultiple);
// Look for un-anotated method in this class if there has been no annotated methods in superclass
- if (annotationOnly = false && (annotatedMethods == null || annotatedMethods.length == 0) && (superMethods == null || superMethods.length==0)) {
- return _delegate.getTearDownMethod(test);
+ if (annotationOnly = false && (annotatedMethods == null || annotatedMethods.length == 0) && (superMethods == null || superMethods.length==0)) {
+ try {
+ return (Method[])delegateMethod.invoke(_delegate, test);
+ } catch (IllegalArgumentException e) {
+ throw new AnnotationException( "Problem running method", e );
+ } catch (IllegalAccessException e) {
+ throw new AnnotationException( "Problem running method", e );
+ } catch (InvocationTargetException e) {
+ throw new AnnotationException( "Problem running method", e );
+ }
}
- // This classes method needs to be on the list before its superclasses
+ // Do we want the superclass code to be before or after the current code?
List<Method> methodsList = new ArrayList<Method>();
- if (annotatedMethods != null) methodsList.addAll(Arrays.asList(annotatedMethods));
- if (superMethods != null) methodsList.addAll(Arrays.asList(superMethods));
+ if (after) {
+ if (annotatedMethods != null && annotatedMethods.length > 0) methodsList.add(annotatedMethods[0]);
+ if (superMethods != null) methodsList.addAll(Arrays.asList(superMethods));
+ } else {
+ if (superMethods != null) methodsList.addAll(Arrays.asList(superMethods));
+ if (annotatedMethods != null && annotatedMethods.length > 0) methodsList.add(annotatedMethods[0]);
+ }
return methodsList.size() == 0 ? null : methodsList.toArray(new Method[0]);
}
-
-}
+}
\ No newline at end of file
Modified: trunk/punit/src/org/punit/exception/AnnotationException.java
===================================================================
--- trunk/punit/src/org/punit/exception/AnnotationException.java 2008-05-28 00:44:46 UTC (rev 311)
+++ trunk/punit/src/org/punit/exception/AnnotationException.java 2008-05-28 09:34:47 UTC (rev 312)
@@ -7,4 +7,8 @@
public AnnotationException(String message) {
super(message);
}
+
+ public AnnotationException(String message, Throwable t) {
+ super(message, t);
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|