< prev index next >

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

Print this page




   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 package jdk.test.lib;
  25 

  26 import static jdk.test.lib.Asserts.assertTrue;
  27 import java.io.IOException;
  28 import java.lang.reflect.Field;
  29 import java.net.InetAddress;

  30 import java.net.ServerSocket;


  31 import java.net.UnknownHostException;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 import java.util.ArrayList;
  36 import java.util.Arrays;
  37 import java.util.Collection;
  38 import java.util.Collections;
  39 import java.util.Iterator;
  40 import java.util.List;
  41 import java.util.Random;
  42 import java.util.function.BooleanSupplier;
  43 import java.util.concurrent.TimeUnit;
  44 import java.util.regex.Matcher;
  45 import java.util.regex.Pattern;
  46 import java.util.stream.Collectors;
  47 import sun.misc.Unsafe;
  48 
  49 /**
  50  * Common library for various test helper functions.
  51  */
  52 public final class Utils {
  53 
  54     /**





  55      * Returns the sequence used by operating system to separate lines.
  56      */
  57     public static final String NEW_LINE = System.getProperty("line.separator");
  58 
  59     /**
  60      * Returns the value of 'test.vm.opts' system property.
  61      */
  62     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  63 
  64     /**
  65      * Returns the value of 'test.java.opts' system property.
  66      */
  67     public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();
  68 
  69     /**
  70      * Returns the value of 'test.src' system property.
  71      */
  72     public static final String TEST_SRC = System.getProperty("test.src", "").trim();
  73 
  74     private static Unsafe unsafe = null;


 439         while (!(condition.getAsBoolean() || (timeout != -1L
 440                 && ((System.currentTimeMillis() - startTime) > timeout)))) {
 441             try {
 442                 Thread.sleep(sleepTime);
 443             } catch (InterruptedException e) {
 444                 Thread.currentThread().interrupt();
 445                 throw new Error(e);
 446             }
 447         }
 448         return condition.getAsBoolean();
 449     }
 450 
 451     /**
 452      * Adjusts the provided timeout value for the TIMEOUT_FACTOR
 453      * @param tOut the timeout value to be adjusted
 454      * @return The timeout value adjusted for the value of "test.timeout.factor"
 455      *         system property
 456      */
 457     public static long adjustTimeout(long tOut) {
 458         return Math.round(tOut * Utils.TIMEOUT_FACTOR);





















 459     }
 460 
 461     /**
 462      * Runs runnable and checks that it throws expected exception. If exceptionException is null it means
 463      * that we expect no exception to be thrown.
 464      * @param runnable what we run
 465      * @param expectedException expected exception
 466      */
 467     public static void runAndCheckException(Runnable runnable, Class<? extends Throwable> expectedException) {
 468         boolean expectedExceptionWasNotThrown = false;
 469         try {
 470             runnable.run();
 471             if (expectedException != null) {
 472                 expectedExceptionWasNotThrown = true;
 473             }
 474         } catch (Throwable t) {
 475             if (expectedException == null) {
 476                 throw new AssertionError("Got unexpected exception ", t);
 477             }
 478             if (!expectedException.isAssignableFrom(t.getClass())) {




   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 package jdk.test.lib;
  25 
  26 import java.io.File;
  27 import static jdk.test.lib.Asserts.assertTrue;
  28 import java.io.IOException;
  29 import java.lang.reflect.Field;
  30 import java.net.InetAddress;
  31 import java.net.MalformedURLException;
  32 import java.net.ServerSocket;
  33 import java.net.URL;
  34 import java.net.URLClassLoader;
  35 import java.net.UnknownHostException;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 import java.util.ArrayList;
  40 import java.util.Arrays;
  41 import java.util.Collection;
  42 import java.util.Collections;
  43 import java.util.Iterator;
  44 import java.util.List;
  45 import java.util.Random;
  46 import java.util.function.BooleanSupplier;
  47 import java.util.concurrent.TimeUnit;
  48 import java.util.regex.Matcher;
  49 import java.util.regex.Pattern;
  50 import java.util.stream.Collectors;
  51 import sun.misc.Unsafe;
  52 
  53 /**
  54  * Common library for various test helper functions.
  55  */
  56 public final class Utils {
  57 
  58     /**
  59      * Returns the value of 'test.class.path' system property.
  60      */
  61     public static final String TEST_CLASS_PATH = System.getProperty("test.class.path", ".");
  62 
  63     /**
  64      * Returns the sequence used by operating system to separate lines.
  65      */
  66     public static final String NEW_LINE = System.getProperty("line.separator");
  67 
  68     /**
  69      * Returns the value of 'test.vm.opts' system property.
  70      */
  71     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  72 
  73     /**
  74      * Returns the value of 'test.java.opts' system property.
  75      */
  76     public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();
  77 
  78     /**
  79      * Returns the value of 'test.src' system property.
  80      */
  81     public static final String TEST_SRC = System.getProperty("test.src", "").trim();
  82 
  83     private static Unsafe unsafe = null;


 448         while (!(condition.getAsBoolean() || (timeout != -1L
 449                 && ((System.currentTimeMillis() - startTime) > timeout)))) {
 450             try {
 451                 Thread.sleep(sleepTime);
 452             } catch (InterruptedException e) {
 453                 Thread.currentThread().interrupt();
 454                 throw new Error(e);
 455             }
 456         }
 457         return condition.getAsBoolean();
 458     }
 459 
 460     /**
 461      * Adjusts the provided timeout value for the TIMEOUT_FACTOR
 462      * @param tOut the timeout value to be adjusted
 463      * @return The timeout value adjusted for the value of "test.timeout.factor"
 464      *         system property
 465      */
 466     public static long adjustTimeout(long tOut) {
 467         return Math.round(tOut * Utils.TIMEOUT_FACTOR);
 468     }
 469 
 470     /**
 471      * @param parent a class loader to be the parent for the returned one
 472      * @return an UrlClassLoader with urls made of the 'test.class.path' jtreg
 473      *         property and with the given parent
 474      */
 475     public static URLClassLoader getTestClassPathURLClassLoader(ClassLoader parent) {
 476         URL[] urls = Arrays.stream(TEST_CLASS_PATH.split(File.pathSeparator))
 477                 .map(Paths::get)
 478                 .map(Path::toUri)
 479                 .map(x -> {
 480                     try {
 481                         return x.toURL();
 482                     } catch (MalformedURLException ex) {
 483                         throw new Error("Test issue. JTREG property"
 484                                 + " 'test.class.path'"
 485                                 + " is not defined correctly", ex);
 486                     }
 487                 }).toArray(URL[]::new);
 488         return new URLClassLoader(urls, parent);
 489     }
 490 
 491     /**
 492      * Runs runnable and checks that it throws expected exception. If exceptionException is null it means
 493      * that we expect no exception to be thrown.
 494      * @param runnable what we run
 495      * @param expectedException expected exception
 496      */
 497     public static void runAndCheckException(Runnable runnable, Class<? extends Throwable> expectedException) {
 498         boolean expectedExceptionWasNotThrown = false;
 499         try {
 500             runnable.run();
 501             if (expectedException != null) {
 502                 expectedExceptionWasNotThrown = true;
 503             }
 504         } catch (Throwable t) {
 505             if (expectedException == null) {
 506                 throw new AssertionError("Got unexpected exception ", t);
 507             }
 508             if (!expectedException.isAssignableFrom(t.getClass())) {


< prev index next >