/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package jdk.testlibrary; import static jdk.testlibrary.Asserts.assertTrue; import java.net.InetAddress; import java.net.ServerSocket; import java.util.ArrayList; import java.util.List; public final class Utils { public static final String NEW_LINE = System.getProperty("line.separator"); public static final String VM_OPTIONS = System.getProperty("test.vm.opts", ""); public static List getVmOptions() { return getVmOptions(false); } 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; } /** * The function will spin until a free port is found */ public static int getFreePort() throws Exception { int port = -1; while (port <= 0) { System.out.println("Retrieving free port..."); Thread.sleep(100); ServerSocket serverSocket = new ServerSocket(0); port = serverSocket.getLocalPort(); serverSocket.close(); } System.out.println("Free port: " + port); return port; } public static String getHostname() throws Exception { InetAddress inetAddress = InetAddress.getLocalHost(); String hostName = inetAddress.getHostName(); assertTrue((hostName != null && !hostName.isEmpty()), "Cannot get hostname"); return hostName; } }