1 /* 2 * Copyright (c) 2013, 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 jdk.testlibrary; 25 26 import static jdk.testlibrary.Asserts.assertTrue; 27 28 import java.io.IOException; 29 import java.net.InetAddress; 30 import java.net.ServerSocket; 31 import java.net.UnknownHostException; 32 import java.util.ArrayList; 33 import java.util.List; 34 35 /** 36 * Common library for various test helper functions. 37 */ 38 public final class Utils { 39 40 /** 41 * Returns the sequence used by operating system to separate lines. 42 */ 43 public static final String NEW_LINE = System.getProperty("line.separator"); 44 45 /** 46 * Returns the value of 'test.vm.opts'system property. 47 */ 48 public static final String VM_OPTIONS = System.getProperty("test.vm.opts", ""); 49 50 51 private Utils() { 52 // Private constructor to prevent class instantiation 53 } 54 55 /** 56 * Returns the list of VM options. 57 * 58 * @return List of VM options 59 */ 60 public static List<String> getVmOptions() { 61 return getVmOptions(false); 62 } 63 64 /** 65 * Returns the list of VM options with -J prefix. 66 * 67 * @return List of VM options with -J prefix 68 */ 69 public static List<String> getForwardVmOptions() { 70 return getVmOptions(true); 71 } 72 73 private static List<String> getVmOptions(boolean forward) { 74 List<String> optionsList = new ArrayList<>(); 75 String options = VM_OPTIONS.trim(); 76 if (!options.isEmpty()) { 77 options = options.replaceAll("\\s+", " "); 78 for (String option : options.split(" ")) { 79 if (forward) { 80 optionsList.add("-J" + option); 81 } else { 82 optionsList.add(option); 83 } 84 } 85 } 86 87 return optionsList; 88 } 89 90 /** 91 * Returns the free port on the local host. 92 * The function will spin until a valid port number is found. 93 * 94 * @return Port number 95 * @throws InterruptedException if any thread has interrupted the current thread 96 * @throws IOException if an I/O error occurs when opening the socket 97 */ 98 public static int getFreePort() throws InterruptedException, IOException { 99 int port = -1; 100 101 while (port <= 0) { 102 System.out.println("Retrieving free port..."); 103 Thread.sleep(100); 104 105 ServerSocket serverSocket = null; 106 try { 107 serverSocket = new ServerSocket(0); 108 port = serverSocket.getLocalPort(); 109 } finally { 110 serverSocket.close(); 111 } 112 } 113 114 System.out.println("Free port: " + port); 115 return port; 116 } 117 118 /** 119 * Returns the name of the local host. 120 * 121 * @return Host name 122 * @throws UnknownHostException if IP address of a host could not be determined 123 */ 124 public static String getHostname() throws UnknownHostException { 125 InetAddress inetAddress = InetAddress.getLocalHost(); 126 String hostName = inetAddress.getHostName(); 127 128 assertTrue((hostName != null && !hostName.isEmpty()), 129 "Cannot get hostname"); 130 131 return hostName; 132 } 133 134 }