2
|
1 |
/*
|
|
2 |
* Copyright 2000-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/* Test utilities
|
|
25 |
*
|
|
26 |
*/
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
import java.net.*;
|
|
30 |
import java.nio.*;
|
|
31 |
import java.nio.channels.*;
|
|
32 |
import java.util.Random;
|
|
33 |
|
|
34 |
|
|
35 |
public class TestUtil {
|
|
36 |
|
|
37 |
// Test hosts used by the channels tests - change these when
|
|
38 |
// executing in a different network.
|
|
39 |
public static final String HOST = "javaweb.sfbay.sun.com";
|
|
40 |
public static final String REFUSING_HOST = "jano.sfbay.sun.com";
|
|
41 |
public static final String FAR_HOST = "theclub.ireland.sun.com";
|
|
42 |
public static final String UNRESOLVABLE_HOST = "blah-blah.blah-blah.blah";
|
|
43 |
|
|
44 |
private TestUtil() { }
|
|
45 |
|
|
46 |
// Repeatedly try random ports until we bind to one. You might be tempted
|
|
47 |
// to do this:
|
|
48 |
//
|
|
49 |
// ServerSocketChannel ssc = ServerSocketChannel.open();
|
|
50 |
// ssc.socket().bind(new InetSocketAddress(0));
|
|
51 |
// SocketAddress sa = ssc.socket().getLocalSocketAddress();
|
|
52 |
//
|
|
53 |
// but unfortunately it doesn't work on NT 4.0.
|
|
54 |
//
|
|
55 |
// Returns the bound port.
|
|
56 |
//
|
|
57 |
static int bind(ServerSocketChannel ssc) throws IOException {
|
|
58 |
InetAddress lh = InetAddress.getLocalHost();
|
|
59 |
Random r = new Random();
|
|
60 |
for (;;) {
|
|
61 |
int p = r.nextInt((1 << 16) - 1024) + 1024;
|
|
62 |
InetSocketAddress isa = new InetSocketAddress(lh, p);
|
|
63 |
try {
|
|
64 |
ssc.socket().bind(isa);
|
|
65 |
} catch (IOException x) {
|
|
66 |
continue;
|
|
67 |
}
|
|
68 |
return p;
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
// A more convenient form of bind(ServerSocketChannel) that returns a full
|
|
73 |
// socket address.
|
|
74 |
//
|
|
75 |
static InetSocketAddress bindToRandomPort(ServerSocketChannel ssc)
|
|
76 |
throws IOException
|
|
77 |
{
|
|
78 |
int p = bind(ssc);
|
|
79 |
return new InetSocketAddress(InetAddress.getLocalHost(), p);
|
|
80 |
}
|
|
81 |
|
|
82 |
private static String osName = System.getProperty("os.name");
|
|
83 |
|
|
84 |
// Examines os.name property to determine if running on 95/98/ME.
|
|
85 |
//
|
|
86 |
// Returns true if running on windows95/98/ME.
|
|
87 |
//
|
|
88 |
static boolean onME() {
|
|
89 |
if (osName.startsWith("Windows")) {
|
|
90 |
if (osName.indexOf("9") > 0)
|
|
91 |
return true;
|
|
92 |
if (osName.indexOf("M") > 0)
|
|
93 |
return true;
|
|
94 |
}
|
|
95 |
return false;
|
|
96 |
}
|
|
97 |
|
|
98 |
static boolean onSolaris() {
|
|
99 |
return osName.startsWith("SunOS");
|
|
100 |
}
|
|
101 |
|
|
102 |
static boolean onWindows() {
|
|
103 |
return osName.startsWith("Windows");
|
|
104 |
}
|
|
105 |
|
|
106 |
}
|