< prev index next >

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

Print this page
rev 59575 : [mq]: 8246387-jtreg5.1


  95      * Returns the value of 'test.root' system property.
  96      */
  97     public static final String TEST_ROOT = System.getProperty("test.root", "").trim();
  98 
  99     /*
 100      * Returns the value of 'test.jdk' system property
 101      */
 102     public static final String TEST_JDK = System.getProperty("test.jdk");
 103 
 104     /*
 105      * Returns the value of 'compile.jdk' system property
 106      */
 107     public static final String COMPILE_JDK = System.getProperty("compile.jdk", TEST_JDK);
 108 
 109     /**
 110      * Returns the value of 'test.classes' system property
 111      */
 112     public static final String TEST_CLASSES = System.getProperty("test.classes", ".");
 113 
 114     /**





 115      * Defines property name for seed value.
 116      */
 117     public static final String SEED_PROPERTY_NAME = "jdk.test.lib.random.seed";
 118 
 119     /* (non-javadoc)
 120      * Random generator with (or without) predefined seed. Depends on
 121      * "jdk.test.lib.random.seed" property value.
 122      */
 123     private static volatile Random RANDOM_GENERATOR;
 124 
 125     /**
 126      * Maximum number of attempts to get free socket
 127      */
 128     private static final int MAX_SOCKET_TRIES = 10;
 129 
 130     /**
 131      * Contains the seed value used for {@link java.util.Random} creation.
 132      */
 133     public static final long SEED = Long.getLong(SEED_PROPERTY_NAME, new Random().nextLong());
 134     /**


 806      * Run uname with specified arguments.
 807      */
 808     public static OutputAnalyzer uname(String... args) throws Throwable {
 809         String[] cmds = new String[args.length + 1];
 810         cmds[0] = "uname";
 811         System.arraycopy(args, 0, cmds, 1, args.length);
 812         return ProcessTools.executeCommand(cmds);
 813     }
 814 
 815     /*
 816      * Returns the system distro.
 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>
 885      * If the property "user.dir" is not set, "." will be used.
 886      *
 887      * @param prefix
 888      * @param suffix
 889      * @param attrs
 890      * @return the path to the newly created file that did not exist before this
 891      *         method was invoked
 892      * @throws IOException
 893      *
 894      * @see {@link Files#createTempFile(String, String, FileAttribute...)}
 895      */
 896     public static Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException {
 897         Path dir = Paths.get(System.getProperty("user.dir", "."));
 898         return Files.createTempFile(dir, prefix, suffix);




  95      * Returns the value of 'test.root' system property.
  96      */
  97     public static final String TEST_ROOT = System.getProperty("test.root", "").trim();
  98 
  99     /*
 100      * Returns the value of 'test.jdk' system property
 101      */
 102     public static final String TEST_JDK = System.getProperty("test.jdk");
 103 
 104     /*
 105      * Returns the value of 'compile.jdk' system property
 106      */
 107     public static final String COMPILE_JDK = System.getProperty("compile.jdk", TEST_JDK);
 108 
 109     /**
 110      * Returns the value of 'test.classes' system property
 111      */
 112     public static final String TEST_CLASSES = System.getProperty("test.classes", ".");
 113 
 114     /**
 115      * Returns the value of 'test.name' system property
 116      */
 117     public static final String TEST_NAME = System.getProperty("test.name", ".");
 118 
 119    /**
 120      * Defines property name for seed value.
 121      */
 122     public static final String SEED_PROPERTY_NAME = "jdk.test.lib.random.seed";
 123 
 124     /* (non-javadoc)
 125      * Random generator with (or without) predefined seed. Depends on
 126      * "jdk.test.lib.random.seed" property value.
 127      */
 128     private static volatile Random RANDOM_GENERATOR;
 129 
 130     /**
 131      * Maximum number of attempts to get free socket
 132      */
 133     private static final int MAX_SOCKET_TRIES = 10;
 134 
 135     /**
 136      * Contains the seed value used for {@link java.util.Random} creation.
 137      */
 138     public static final long SEED = Long.getLong(SEED_PROPERTY_NAME, new Random().nextLong());
 139     /**


 811      * Run uname with specified arguments.
 812      */
 813     public static OutputAnalyzer uname(String... args) throws Throwable {
 814         String[] cmds = new String[args.length + 1];
 815         cmds[0] = "uname";
 816         System.arraycopy(args, 0, cmds, 1, args.length);
 817         return ProcessTools.executeCommand(cmds);
 818     }
 819 
 820     /*
 821      * Returns the system distro.
 822      */
 823     public static String distro() {
 824         try {
 825             return uname("-v").asLines().get(0);
 826         } catch (Throwable t) {
 827             throw new RuntimeException("Failed to determine distro.", t);
 828         }
 829     }
 830 





















































 831     /**
 832      * Creates an empty file in "user.dir" if the property set.
 833      * <p>
 834      * This method is meant as a replacement for {@code Files#createTempFile(String, String, FileAttribute...)}
 835      * that doesn't leave files behind in /tmp directory of the test machine
 836      * <p>
 837      * If the property "user.dir" is not set, "." will be used.
 838      *
 839      * @param prefix
 840      * @param suffix
 841      * @param attrs
 842      * @return the path to the newly created file that did not exist before this
 843      *         method was invoked
 844      * @throws IOException
 845      *
 846      * @see {@link Files#createTempFile(String, String, FileAttribute...)}
 847      */
 848     public static Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException {
 849         Path dir = Paths.get(System.getProperty("user.dir", "."));
 850         return Files.createTempFile(dir, prefix, suffix);


< prev index next >