< prev index next >

test/lib/jdk/test/lib/Utils.java

Print this page
rev 59477 : [mq]: cds_lambda


   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 package jdk.test.lib;
  25 
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.lang.annotation.Annotation;

  29 import java.net.Inet6Address;
  30 import java.net.InetAddress;
  31 import java.net.InetSocketAddress;
  32 import java.net.MalformedURLException;
  33 import java.net.ServerSocket;
  34 import java.net.URL;
  35 import java.net.URLClassLoader;
  36 import java.net.UnknownHostException;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.nio.file.attribute.FileAttribute;
  41 import java.nio.channels.SocketChannel;
  42 import java.util.ArrayList;
  43 import java.util.Arrays;
  44 import java.util.Collection;
  45 import java.util.Collections;
  46 import java.util.Iterator;
  47 import java.util.Map;
  48 import java.util.HashMap;


 817      */
 818     public static String distro() {
 819         try {
 820             return uname("-v").asLines().get(0);
 821         } catch (Throwable t) {
 822             throw new RuntimeException("Failed to determine distro.", t);
 823         }
 824     }
 825 
 826     // This method is intended to be called from a jtreg test.
 827     // It will identify the name of the test by means of stack walking.
 828     // It can handle both jtreg tests and a testng tests wrapped inside jtreg tests.
 829     // For jtreg tests the name of the test will be searched by stack-walking
 830     // until the method main() is found; the class containing that method is the
 831     // main test class and will be returned as the name of the test.
 832     // Special handling is used for testng tests.
 833     @SuppressWarnings("unchecked")
 834     public static String getTestName() {
 835         String result = null;
 836         // If we are using testng, then we should be able to load the "Test" annotation.
 837         Class<? extends Annotation> testClassAnnotation;
 838 
 839         try {
 840             testClassAnnotation = (Class<? extends Annotation>)Class.forName("org.testng.annotations.Test");
 841         } catch (ClassNotFoundException e) {
 842             testClassAnnotation = null;
 843         }
 844 







 845         StackTraceElement[] elms = (new Throwable()).getStackTrace();
 846         for (StackTraceElement n: elms) {
 847             String className = n.getClassName();
 848 
 849             // If this is a "main" method, then use its class name, but only
 850             // if we are not using testng.
 851             if (testClassAnnotation == null && "main".equals(n.getMethodName())) {

 852                 result = className;
 853                 break;
 854             }
 855 
 856             // If this is a testng test, the test will have no "main" method. We can
 857             // detect a testng test class by looking for the org.testng.annotations.Test
 858             // annotation. If present, then use the name of this class.
 859             if (testClassAnnotation != null) {
 860                 try {
 861                     Class<?> c = Class.forName(className);
 862                     if (c.isAnnotationPresent(testClassAnnotation)) {
 863                         result = className;
 864                         break;




















 865                     }
 866                 } catch (ClassNotFoundException e) {
 867                     throw new RuntimeException("Unexpected exception: " + e, e);
 868                 }
 869             }
 870         }
 871 
 872         if (result == null) {
 873             throw new RuntimeException("Couldn't find main test class in stack trace");
 874         }
 875 
 876         return result;
 877     }
 878 
 879     /**
 880      * Creates an empty file in "user.dir" if the property set.
 881      * <p>
 882      * This method is meant as a replacement for {@code Files#createTempFile(String, String, FileAttribute...)}
 883      * that doesn't leave files behind in /tmp directory of the test machine
 884      * <p>




   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 package jdk.test.lib;
  25 
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.lang.annotation.Annotation;
  29 import java.lang.reflect.Method;
  30 import java.net.Inet6Address;
  31 import java.net.InetAddress;
  32 import java.net.InetSocketAddress;
  33 import java.net.MalformedURLException;
  34 import java.net.ServerSocket;
  35 import java.net.URL;
  36 import java.net.URLClassLoader;
  37 import java.net.UnknownHostException;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.nio.file.attribute.FileAttribute;
  42 import java.nio.channels.SocketChannel;
  43 import java.util.ArrayList;
  44 import java.util.Arrays;
  45 import java.util.Collection;
  46 import java.util.Collections;
  47 import java.util.Iterator;
  48 import java.util.Map;
  49 import java.util.HashMap;


 818      */
 819     public static String distro() {
 820         try {
 821             return uname("-v").asLines().get(0);
 822         } catch (Throwable t) {
 823             throw new RuntimeException("Failed to determine distro.", t);
 824         }
 825     }
 826 
 827     // This method is intended to be called from a jtreg test.
 828     // It will identify the name of the test by means of stack walking.
 829     // It can handle both jtreg tests and a testng tests wrapped inside jtreg tests.
 830     // For jtreg tests the name of the test will be searched by stack-walking
 831     // until the method main() is found; the class containing that method is the
 832     // main test class and will be returned as the name of the test.
 833     // Special handling is used for testng tests.
 834     @SuppressWarnings("unchecked")
 835     public static String getTestName() {
 836         String result = null;
 837         // If we are using testng, then we should be able to load the "Test" annotation.
 838         Class<? extends Annotation> testClassAnnotation, junitTestClassAnnotation;
 839 
 840         try {
 841             testClassAnnotation = (Class<? extends Annotation>)Class.forName("org.testng.annotations.Test");
 842         } catch (ClassNotFoundException e) {
 843             testClassAnnotation = null;
 844         }
 845 
 846         // If we are using junit, then we should be able to load the "Test" annotation.
 847         try {
 848             junitTestClassAnnotation = (Class<? extends Annotation>)Class.forName("org.junit.Test");
 849         } catch (ClassNotFoundException e) {
 850             junitTestClassAnnotation = null;
 851         }
 852 
 853         StackTraceElement[] elms = (new Throwable()).getStackTrace();
 854         for (StackTraceElement n: elms) {
 855             String className = n.getClassName();
 856 
 857             // If this is a "main" method, then use its class name, but only
 858             // if we are not using testng or junit.
 859             if (testClassAnnotation == null && junitTestClassAnnotation == null &&
 860                 "main".equals(n.getMethodName())) {
 861                 result = className;
 862                 break;
 863             }
 864 
 865             // If this is a testng test, the test will have no "main" method. We can
 866             // detect a testng test class by looking for the org.testng.annotations.Test
 867             // annotation. If present, then use the name of this class.
 868             if (testClassAnnotation != null) {
 869                 try {
 870                     Class<?> c = Class.forName(className);
 871                     if (c.isAnnotationPresent(testClassAnnotation)) {
 872                         result = className;
 873                         break;
 874                     }
 875                 } catch (ClassNotFoundException e) {
 876                     throw new RuntimeException("Unexpected exception: " + e, e);
 877                 }
 878             }
 879 
 880             // If this is a junit test, the test will have no "main" method. We can
 881             // detect a junit test class by going through all the methods and
 882             // check if the method has the org.junit.Test annotation. If present,
 883             // then use the name of this class.
 884             if (junitTestClassAnnotation != null) {
 885                 try {
 886                     Class<?> c = Class.forName(className);
 887                     Method[] methods = c.getMethods();
 888                     for (Method method : methods) {
 889                         if (method.getName().equals(n.getMethodName()) &&
 890                             method.isAnnotationPresent(junitTestClassAnnotation)) {
 891                                 result = className;
 892                                 break;
 893                         }
 894                     }
 895                 } catch (ClassNotFoundException e) {
 896                     throw new RuntimeException("Unexpected exception: " + e, e);
 897                 }
 898             }
 899         }
 900 
 901         if (result == null) {
 902             throw new RuntimeException("Couldn't find main test class in stack trace");
 903         }
 904 
 905         return result;
 906     }
 907 
 908     /**
 909      * Creates an empty file in "user.dir" if the property set.
 910      * <p>
 911      * This method is meant as a replacement for {@code Files#createTempFile(String, String, FileAttribute...)}
 912      * that doesn't leave files behind in /tmp directory of the test machine
 913      * <p>


< prev index next >