< prev index next >

test/testlibrary/com/oracle/java/testlibrary/Utils.java

Print this page
rev 8910 : full patch for jfr
   1 /*
   2  * Copyright (c) 2013, 2018, 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 package com.oracle.java.testlibrary;
  25 
  26 import static com.oracle.java.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.lang.reflect.Field;
  42 import sun.misc.Unsafe;
  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.


 353     }
 354     private static final char[] hexArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 355 
 356     /**
 357      * Returns hex view of byte array
 358      *
 359      * @param bytes byte array to process
 360      * @return Space separated hexadecimal string representation of bytes
 361      */
 362 
 363     public static String toHexString(byte[] bytes) {
 364         char[] hexView = new char[bytes.length * 3];
 365         int i = 0;
 366         for (byte b : bytes) {
 367             hexView[i++] = hexArray[(b >> 4) & 0x0F];
 368             hexView[i++] = hexArray[b & 0x0F];
 369             hexView[i++] = ' ';
 370         }
 371         return new String(hexView);
 372     }














































 373 }
   1 /*
   2  * Copyright (c) 2013, 2019, 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 package com.oracle.java.testlibrary;
  25 
  26 import static com.oracle.java.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.function.BooleanSupplier;
  42 import java.util.function.Function;
  43 import java.lang.reflect.Field;
  44 import sun.misc.Unsafe;
  45 
  46 /**
  47  * Common library for various test helper functions.
  48  */
  49 public final class Utils {
  50 
  51     /**
  52      * Returns the sequence used by operating system to separate lines.
  53      */
  54     public static final String NEW_LINE = System.getProperty("line.separator");
  55 
  56     /**
  57      * Returns the value of 'test.vm.opts'system property.
  58      */
  59     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  60 
  61     /**
  62      * Returns the value of 'test.java.opts'system property.


 355     }
 356     private static final char[] hexArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 357 
 358     /**
 359      * Returns hex view of byte array
 360      *
 361      * @param bytes byte array to process
 362      * @return Space separated hexadecimal string representation of bytes
 363      */
 364 
 365     public static String toHexString(byte[] bytes) {
 366         char[] hexView = new char[bytes.length * 3];
 367         int i = 0;
 368         for (byte b : bytes) {
 369             hexView[i++] = hexArray[(b >> 4) & 0x0F];
 370             hexView[i++] = hexArray[b & 0x0F];
 371             hexView[i++] = ' ';
 372         }
 373         return new String(hexView);
 374     }
 375 
 376     /**
 377      * Wait for condition to be true
 378      *
 379      * @param condition, a condition to wait for
 380      */
 381     public static final void waitForCondition(BooleanSupplier condition) {
 382         waitForCondition(condition, -1L, 100L);
 383     }
 384 
 385     /**
 386      * Wait until timeout for condition to be true
 387      *
 388      * @param condition, a condition to wait for
 389      * @param timeout a time in milliseconds to wait for condition to be true
 390      * specifying -1 will wait forever
 391      * @return condition value, to determine if wait was successful
 392      */
 393     public static final boolean waitForCondition(BooleanSupplier condition,
 394             long timeout) {
 395         return waitForCondition(condition, timeout, 100L);
 396     }
 397 
 398     /**
 399      * Wait until timeout for condition to be true for specified time
 400      *
 401      * @param condition, a condition to wait for
 402      * @param timeout a time in milliseconds to wait for condition to be true,
 403      * specifying -1 will wait forever
 404      * @param sleepTime a time to sleep value in milliseconds
 405      * @return condition value, to determine if wait was successful
 406      */
 407     public static final boolean waitForCondition(BooleanSupplier condition,
 408             long timeout, long sleepTime) {
 409         long startTime = System.currentTimeMillis();
 410         while (!(condition.getAsBoolean() || (timeout != -1L
 411                 && ((System.currentTimeMillis() - startTime) > timeout)))) {
 412             try {
 413                 Thread.sleep(sleepTime);
 414             } catch (InterruptedException e) {
 415                 Thread.currentThread().interrupt();
 416                 throw new Error(e);
 417             }
 418         }
 419         return condition.getAsBoolean();
 420     }
 421 }
< prev index next >