1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 import java.lang.annotation.Annotation;
  26 import java.lang.reflect.InvocationTargetException;
  27 import java.lang.reflect.Method;
  28 import java.util.Arrays;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 
  32 public class TestStreamApp {
  33     public static final String testAnnotation = "@org.testng.annotations.Test";
  34     public static void main(String args[]) throws Exception {
  35         try {
  36             Class<?> testClass = Class.forName(args[0]);
  37             System.out.println(testClass);
  38             Annotation[] classAnnotations = null;
  39             boolean classHasTestAnnotation = false;
  40             try {
  41                 classAnnotations= testClass.getDeclaredAnnotations();
  42                 for (Annotation classAnnotation : classAnnotations) {
  43                     String annoString = classAnnotation.toString();
  44                     System.out.println("     class annotation: " + annoString);
  45                     if (annoString.startsWith(testAnnotation)) {
  46                         classHasTestAnnotation = true;
  47                     }
  48                 }
  49             } catch (Throwable th) {
  50                 System.out.println("Skip class annotation");
  51             }
  52             Object obj = testClass.newInstance();
  53             final List<Method> allMethods = new ArrayList<Method>(Arrays.asList(testClass.getDeclaredMethods()));
  54             for (final Method method : allMethods) {
  55                 //System.out.println(method.toString());
  56                 Annotation[] annotations = method.getDeclaredAnnotations();
  57                 boolean isTest = false;
  58                 /*
  59                 if (classHasTestAnnotation) {
  60                     if (method.toString().indexOf(args[0] + ".test") != -1) {
  61                         isTest = true;
  62                     }
  63                 } else {
  64                     for (Annotation annotation : annotations) {
  65                         String annotationString = annotation.toString();
  66                         System.out.println("     annotation: " + annotationString);
  67                         if (annotationString.startsWith(testAnnotation)) {
  68                             isTest = true;
  69                         }
  70                     }
  71                 }
  72                 */
  73                 if (method.getName().startsWith("test")) {
  74                     isTest = true;
  75                 }
  76                 if (isTest) {
  77                     System.out.println("    invoking method: " + method.getName());
  78                     try {
  79                         method.invoke(obj);
  80                     } catch (IllegalAccessException iae) {
  81                         System.out.println("Got IllegalAccessException!!!");
  82                         System.out.println(iae.getCause());
  83                     } catch (InvocationTargetException ite) {
  84                         System.out.println("Got InvocationTargetException!!!");
  85                         //System.out.println(ite.getCause());
  86                         throw ite;
  87                     }
  88                }
  89             }
  90         } catch (ClassNotFoundException cnfe) {
  91             System.out.println("Class not found!");
  92         }
  93     }
  94 }