Utils.java revision 8840:64a492bc0ba7
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
24package jdk.testlibrary;
25
26import static jdk.testlibrary.Asserts.assertTrue;
27
28import java.io.IOException;
29import java.net.InetAddress;
30import java.net.ServerSocket;
31import java.net.UnknownHostException;
32import java.util.ArrayList;
33import java.util.List;
34import java.util.Arrays;
35import java.util.Collections;
36
37/**
38 * Common library for various test helper functions.
39 */
40public final class Utils {
41
42    /**
43     * Returns the sequence used by operating system to separate lines.
44     */
45    public static final String NEW_LINE = System.getProperty("line.separator");
46
47    /**
48     * Returns the value of 'test.vm.opts'system property.
49     */
50    public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
51
52    /**
53     * Returns the value of 'test.java.opts'system property.
54     */
55    public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();
56
57
58    private Utils() {
59        // Private constructor to prevent class instantiation
60    }
61
62    /**
63     * Returns the list of VM options.
64     *
65     * @return List of VM options
66     */
67    public static List<String> getVmOptions() {
68        return Arrays.asList(safeSplitString(VM_OPTIONS));
69    }
70
71    /**
72     * Returns the list of VM options with -J prefix.
73     *
74     * @return The list of VM options with -J prefix
75     */
76    public static List<String> getForwardVmOptions() {
77        String[] opts = safeSplitString(VM_OPTIONS);
78        for (int i = 0; i < opts.length; i++) {
79            opts[i] = "-J" + opts[i];
80        }
81        return Arrays.asList(opts);
82    }
83
84    /**
85     * Returns the default JTReg arguments for a jvm running a test.
86     * This is the combination of JTReg arguments test.vm.opts and test.java.opts.
87     * @return An array of options, or an empty array if no opptions.
88     */
89    public static String[] getTestJavaOpts() {
90        List<String> opts = new ArrayList<String>();
91        Collections.addAll(opts, safeSplitString(VM_OPTIONS));
92        Collections.addAll(opts, safeSplitString(JAVA_OPTIONS));
93        return opts.toArray(new String[0]);
94    }
95
96    /**
97     * Combines given arguments with default JTReg arguments for a jvm running a test.
98     * This is the combination of JTReg arguments test.vm.opts and test.java.opts
99     * @return The combination of JTReg test java options and user args.
100     */
101    public static String[] addTestJavaOpts(String... userArgs) {
102        List<String> opts = new ArrayList<String>();
103        Collections.addAll(opts, getTestJavaOpts());
104        Collections.addAll(opts, userArgs);
105        return opts.toArray(new String[0]);
106    }
107
108    /**
109     * Splits a string by white space.
110     * Works like String.split(), but returns an empty array
111     * if the string is null or empty.
112     */
113    private static String[] safeSplitString(String s) {
114        if (s == null || s.trim().isEmpty()) {
115            return new String[] {};
116        }
117        return s.trim().split("\\s+");
118    }
119
120    /**
121     * @return The full command line for the ProcessBuilder.
122     */
123    public static String getCommandLine(ProcessBuilder pb) {
124        StringBuilder cmd = new StringBuilder();
125        for (String s : pb.command()) {
126            cmd.append(s).append(" ");
127        }
128        return cmd.toString();
129    }
130
131    /**
132     * Returns the free port on the local host.
133     * The function will spin until a valid port number is found.
134     *
135     * @return The port number
136     * @throws InterruptedException if any thread has interrupted the current thread
137     * @throws IOException if an I/O error occurs when opening the socket
138     */
139    public static int getFreePort() throws InterruptedException, IOException {
140        int port = -1;
141
142        while (port <= 0) {
143            Thread.sleep(100);
144
145            ServerSocket serverSocket = null;
146            try {
147                serverSocket = new ServerSocket(0);
148                port = serverSocket.getLocalPort();
149            } finally {
150                serverSocket.close();
151            }
152        }
153
154        return port;
155    }
156
157    /**
158     * Returns the name of the local host.
159     *
160     * @return The host name
161     * @throws UnknownHostException if IP address of a host could not be determined
162     */
163    public static String getHostname() throws UnknownHostException {
164        InetAddress inetAddress = InetAddress.getLocalHost();
165        String hostName = inetAddress.getHostName();
166
167        assertTrue((hostName != null && !hostName.isEmpty()),
168                "Cannot get hostname");
169
170        return hostName;
171    }
172
173}
174