From f52f54c9157d29ab06b5058dacebc58efadb9f64 Mon Sep 17 00:00:00 2001 From: benoit Date: Fri, 29 Jan 2016 16:09:48 +0100 Subject: [PATCH] Step 3 execute JUnit 4 tests We now scan for classes that extends TestCase (junit3) and for classes that have at least one method annotated with @Test 2 new tests executed in TestSamplingStatCalculator --- .../src/org/apache/jorphan/test/AllTests.java | 45 ++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/test/src/org/apache/jorphan/test/AllTests.java b/test/src/org/apache/jorphan/test/AllTests.java index 04e6b8e2827..9708c918a90 100644 --- a/test/src/org/apache/jorphan/test/AllTests.java +++ b/test/src/org/apache/jorphan/test/AllTests.java @@ -23,6 +23,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.Charset; @@ -135,7 +136,8 @@ public static void main(String[] args) { System.out.println("You must specify a comma-delimited list of paths to search " + "for unit tests"); return; } - String home=new File(System.getProperty("user.dir")).getParent(); + + String home = new File(System.getProperty("user.dir")).getParent(); System.out.println("Setting JMeterHome: "+home); JMeterUtils.setJMeterHome(home); initializeLogging(args); @@ -166,9 +168,9 @@ public static void main(String[] args) { logprop("os.version", true); logprop("os.arch"); logprop("java.class.version"); - // logprop("java.class.path"); + String cp = System.getProperty("java.class.path"); - String cpe[] = JOrphanUtils.split(cp, java.io.File.pathSeparator); + String[] cpe = JOrphanUtils.split(cp, java.io.File.pathSeparator); StringBuilder sb = new StringBuilder(3000); sb.append("java.class.path="); for (String path : cpe) { @@ -188,7 +190,7 @@ public static void main(String[] args) { System.out.println("------------"); try { - System.out.println("Searching junit tests in:"+args[0]); + System.out.println("Searching junit tests in : "+args[0]); List tests = findJMeterJUnitTests(args[0]); JUnitCore.main(tests.toArray(new String[0])); } catch (IOException e) { @@ -344,10 +346,17 @@ public boolean accept(String className) { try { Class c = Class.forName(className, false, contextClassLoader); - if (!c.isInterface() && !Modifier.isAbstract(c.getModifiers())) { + if (!c.isAnnotation() + && !c.isEnum() + && !c.isInterface() + && !Modifier.isAbstract(c.getModifiers())) + { if (TestCase.class.isAssignableFrom(c)) { isJunitTest = true; } + else { + isJunitTest = checkForJUnitAnnotations(c); + } } } catch (UnsupportedClassVersionError ignored) { log.debug(ignored.getLocalizedMessage()); @@ -360,5 +369,31 @@ public boolean accept(String className) { return isJunitTest; } + private boolean checkForJUnitAnnotations(Class clazz) + { + Class classToCheck = clazz; + while(classToCheck != null) { + if( checkforTestAnnotationOnMethods(classToCheck)) { + return true; + } + classToCheck = classToCheck.getSuperclass(); + } + + return false; + } + + private boolean checkforTestAnnotationOnMethods(Class clazz) + { + for(Method method : clazz.getDeclaredMethods()) { + for(Annotation annotation : method.getAnnotations() ) { + if (org.junit.Test.class.isAssignableFrom(annotation.annotationType())) { + return true; + } + } + } + + return false; + } + } }