< prev index next >

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

Print this page
rev 11104 : imported patch 8067295.forwebrev.open.diff


  22  */
  23 
  24 package jdk.testlibrary;
  25 
  26 import static jdk.testlibrary.Asserts.assertTrue;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.File;
  30 import java.io.FileReader;
  31 import java.io.IOException;
  32 import java.net.InetAddress;
  33 import java.net.ServerSocket;
  34 import java.net.UnknownHostException;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.Arrays;
  38 import java.util.Collections;
  39 import java.util.regex.Pattern;
  40 import java.util.regex.Matcher;
  41 import java.util.concurrent.TimeUnit;

  42 
  43 /**
  44  * Common library for various test helper functions.
  45  */
  46 public final class Utils {
  47 
  48     /**
  49      * Returns the sequence used by operating system to separate lines.
  50      */
  51     public static final String NEW_LINE = System.getProperty("line.separator");
  52 
  53     /**
  54      * Returns the value of 'test.vm.opts'system property.
  55      */
  56     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  57 
  58     /**
  59      * Returns the value of 'test.java.opts'system property.
  60      */
  61     public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();


 280     public static List<String> fileAsList(File file) throws IOException {
 281         assertTrue(file.exists() && file.isFile(),
 282                 file.getAbsolutePath() + " does not exist or not a file");
 283         List<String> output = new ArrayList<>();
 284         try (BufferedReader reader = new BufferedReader(new FileReader(file.getAbsolutePath()))) {
 285             while (reader.ready()) {
 286                 output.add(reader.readLine().replace(NEW_LINE, ""));
 287             }
 288         }
 289         return output;
 290     }
 291 
 292     /**
 293      * Adjusts the provided timeout value for the TIMEOUT_FACTOR
 294      * @param tOut the timeout value to be adjusted
 295      * @return The timeout value adjusted for the value of "test.timeout.factor"
 296      *         system property
 297      */
 298     public static long adjustTimeout(long tOut) {
 299         return Math.round(tOut * Utils.TIMEOUT_FACTOR);














































 300     }
 301 }


  22  */
  23 
  24 package jdk.testlibrary;
  25 
  26 import static jdk.testlibrary.Asserts.assertTrue;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.File;
  30 import java.io.FileReader;
  31 import java.io.IOException;
  32 import java.net.InetAddress;
  33 import java.net.ServerSocket;
  34 import java.net.UnknownHostException;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.Arrays;
  38 import java.util.Collections;
  39 import java.util.regex.Pattern;
  40 import java.util.regex.Matcher;
  41 import java.util.concurrent.TimeUnit;
  42 import java.util.function.BooleanSupplier;
  43 
  44 /**
  45  * Common library for various test helper functions.
  46  */
  47 public final class Utils {
  48 
  49     /**
  50      * Returns the sequence used by operating system to separate lines.
  51      */
  52     public static final String NEW_LINE = System.getProperty("line.separator");
  53 
  54     /**
  55      * Returns the value of 'test.vm.opts'system property.
  56      */
  57     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  58 
  59     /**
  60      * Returns the value of 'test.java.opts'system property.
  61      */
  62     public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();


 281     public static List<String> fileAsList(File file) throws IOException {
 282         assertTrue(file.exists() && file.isFile(),
 283                 file.getAbsolutePath() + " does not exist or not a file");
 284         List<String> output = new ArrayList<>();
 285         try (BufferedReader reader = new BufferedReader(new FileReader(file.getAbsolutePath()))) {
 286             while (reader.ready()) {
 287                 output.add(reader.readLine().replace(NEW_LINE, ""));
 288             }
 289         }
 290         return output;
 291     }
 292 
 293     /**
 294      * Adjusts the provided timeout value for the TIMEOUT_FACTOR
 295      * @param tOut the timeout value to be adjusted
 296      * @return The timeout value adjusted for the value of "test.timeout.factor"
 297      *         system property
 298      */
 299     public static long adjustTimeout(long tOut) {
 300         return Math.round(tOut * Utils.TIMEOUT_FACTOR);
 301     }
 302 
 303     /**
 304      * Wait for condition to be true
 305      *
 306      * @param condition, a condition to wait for
 307      */
 308     public static final void waitForCondition(BooleanSupplier condition) {
 309         waitForCondition(condition, -1L, 100L);
 310     }
 311 
 312     /**
 313      * Wait until timeout for condition to be true
 314      *
 315      * @param condition, a condition to wait for
 316      * @param timeout a time in milliseconds to wait for condition to be true
 317      * specifying -1 will wait forever
 318      * @return condition value, to determine if wait was successfull
 319      */
 320     public static final boolean waitForCondition(BooleanSupplier condition,
 321             long timeout) {
 322         return waitForCondition(condition, timeout, 100L);
 323     }
 324 
 325     /**
 326      * Wait until timeout for condition to be true for specified time
 327      *
 328      * @param condition, a condition to wait for
 329      * @param timeout a time in milliseconds to wait for condition to be true,
 330      * specifying -1 will wait forever
 331      * @param sleepTime a time to sleep value in milliseconds
 332      * @return condition value, to determine if wait was successfull
 333      */
 334     public static final boolean waitForCondition(BooleanSupplier condition,
 335             long timeout, long sleepTime) {
 336         long startTime = System.currentTimeMillis();
 337         while (!(condition.getAsBoolean() || (timeout != -1L
 338                 && ((System.currentTimeMillis() - startTime) > timeout)))) {
 339             try {
 340                 Thread.sleep(sleepTime);
 341             } catch (InterruptedException e) {
 342                 Thread.currentThread().interrupt();
 343                 throw new Error(e);
 344             }
 345         } 
 346         return condition.getAsBoolean();
 347     }
 348 }
< prev index next >