/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.testlibrary; import static jdk.testlibrary.Asserts.assertTrue; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; /** * Common library for various test helper functions. */ public final class Utils { /** * Returns the sequence used by operating system to separate lines. */ public static final String NEW_LINE = System.getProperty("line.separator"); /** * Returns the value of 'test.vm.opts'system property. */ public static final String VM_OPTIONS = System.getProperty("test.vm.opts", ""); private Utils() { // Private constructor to prevent class instantiation } /** * Returns the list of VM options. * * @return List of VM options */ public static List getVmOptions() { return getVmOptions(false); } /** * Returns the list of VM options with -J prefix. * * @return The list of VM options with -J prefix */ public static List getForwardVmOptions() { return getVmOptions(true); } private static List getVmOptions(boolean forward) { List optionsList = new ArrayList<>(); String options = VM_OPTIONS.trim(); if (!options.isEmpty()) { options = options.replaceAll("\\s+", " "); for (String option : options.split(" ")) { if (forward) { optionsList.add("-J" + option); } else { optionsList.add(option); } } } return optionsList; } /** * Returns the free port on the local host. * The function will spin until a valid port number is found. * * @return The port number * @throws InterruptedException if any thread has interrupted the current thread * @throws IOException if an I/O error occurs when opening the socket */ public static int getFreePort() throws InterruptedException, IOException { int port = -1; while (port <= 0) { Thread.sleep(100); ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(0); port = serverSocket.getLocalPort(); } finally { serverSocket.close(); } } return port; } /** * Returns the name of the local host. * * @return The host name * @throws UnknownHostException if IP address of a host could not be determined */ public static String getHostname() throws UnknownHostException { InetAddress inetAddress = InetAddress.getLocalHost(); String hostName = inetAddress.getHostName(); assertTrue((hostName != null && !hostName.isEmpty()), "Cannot get hostname"); return hostName; } }