author | katleman |
Thu, 17 Apr 2014 10:13:36 -0700 | |
changeset 23942 | 7b722b4cdd2d |
parent 14415 | 7a31b0e0cfaf |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13028
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* Test utilities |
|
25 |
* |
|
26 |
*/ |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.net.*; |
|
30 |
import java.nio.channels.*; |
|
31 |
import java.util.Random; |
|
32 |
||
33 |
||
34 |
public class TestUtil { |
|
35 |
||
36 |
// Test hosts used by the channels tests - change these when |
|
37 |
// executing in a different network. |
|
38 |
public static final String UNRESOLVABLE_HOST = "blah-blah.blah-blah.blah"; |
|
39 |
||
40 |
private TestUtil() { } |
|
41 |
||
42 |
// Repeatedly try random ports until we bind to one. You might be tempted |
|
43 |
// to do this: |
|
44 |
// |
|
45 |
// ServerSocketChannel ssc = ServerSocketChannel.open(); |
|
46 |
// ssc.socket().bind(new InetSocketAddress(0)); |
|
47 |
// SocketAddress sa = ssc.socket().getLocalSocketAddress(); |
|
48 |
// |
|
49 |
// but unfortunately it doesn't work on NT 4.0. |
|
50 |
// |
|
51 |
// Returns the bound port. |
|
52 |
// |
|
53 |
static int bind(ServerSocketChannel ssc) throws IOException { |
|
54 |
InetAddress lh = InetAddress.getLocalHost(); |
|
55 |
Random r = new Random(); |
|
56 |
for (;;) { |
|
57 |
int p = r.nextInt((1 << 16) - 1024) + 1024; |
|
58 |
InetSocketAddress isa = new InetSocketAddress(lh, p); |
|
59 |
try { |
|
60 |
ssc.socket().bind(isa); |
|
61 |
} catch (IOException x) { |
|
62 |
continue; |
|
63 |
} |
|
64 |
return p; |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
// A more convenient form of bind(ServerSocketChannel) that returns a full |
|
69 |
// socket address. |
|
70 |
// |
|
71 |
static InetSocketAddress bindToRandomPort(ServerSocketChannel ssc) |
|
72 |
throws IOException |
|
73 |
{ |
|
74 |
int p = bind(ssc); |
|
75 |
return new InetSocketAddress(InetAddress.getLocalHost(), p); |
|
76 |
} |
|
77 |
||
78 |
private static String osName = System.getProperty("os.name"); |
|
79 |
||
80 |
static boolean onSolaris() { |
|
81 |
return osName.startsWith("SunOS"); |
|
82 |
} |
|
83 |
||
84 |
static boolean onWindows() { |
|
85 |
return osName.startsWith("Windows"); |
|
86 |
} |
|
87 |
} |