Index: modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/DatagramChannelTest.java =================================================================== --- modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/DatagramChannelTest.java (revision 500995) +++ modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/DatagramChannelTest.java (working copy) @@ -73,13 +73,13 @@ this.channel1 = DatagramChannel.open(); this.channel2 = DatagramChannel.open(); this.localAddr1 = new InetSocketAddress("127.0.0.1", - Support_PortManager.getNextPort()); + Support_PortManager.getNextPortForUDP()); this.localAddr2 = new InetSocketAddress("127.0.0.1", - Support_PortManager.getNextPort()); + Support_PortManager.getNextPortForUDP()); this.datagramSocket1 = new DatagramSocket(Support_PortManager - .getNextPort()); + .getNextPortForUDP()); this.datagramSocket2 = new DatagramSocket(Support_PortManager - .getNextPort()); + .getNextPortForUDP()); } protected void tearDown() throws Exception { @@ -1285,7 +1285,7 @@ public void testReceiveSend_Normal_S2S() throws Exception { String msg = "normal string in testReceiveSend_Normal_S2S"; this.datagramSocket1 = new DatagramSocket(Support_PortManager - .getNextPort()); + .getNextPortForUDP()); DatagramPacket rdp = new DatagramPacket(msg.getBytes(), msg.length(), localAddr2); datagramSocket2 = new DatagramSocket(localAddr2.getPort()); @@ -1342,7 +1342,7 @@ public void testReceiveSend_Empty_S2S() throws Exception { String msg = ""; this.datagramSocket1 = new DatagramSocket(Support_PortManager - .getNextPort()); + .getNextPortForUDP()); DatagramPacket rdp = new DatagramPacket(msg.getBytes(), msg.length(), localAddr2); datagramSocket2 = new DatagramSocket(localAddr2.getPort()); @@ -1419,7 +1419,7 @@ private void sendByDatagramSocket(String data, InetSocketAddress address) throws Exception { this.datagramSocket1 = new DatagramSocket(Support_PortManager - .getNextPort()); + .getNextPortForUDP()); DatagramPacket rdp = new DatagramPacket(data.getBytes(), data.length(), address); this.datagramSocket1.send(rdp); @@ -1512,7 +1512,7 @@ ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); String strHello = "hello"; localAddr1 = new InetSocketAddress("127.0.0.1", Support_PortManager - .getNextPort()); + .getNextPortForUDP()); this.channel1.socket().bind(localAddr1); this.channel2.socket().bind(localAddr2); this.channel1.connect(localAddr2); @@ -1532,7 +1532,7 @@ ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); String strHello = "hello"; localAddr1 = new InetSocketAddress("127.0.0.1", Support_PortManager - .getNextPort()); + .getNextPortForUDP()); this.channel1.socket().bind(localAddr1); sendByChannel(strHello, localAddr1); final SecurityManager sm = System.getSecurityManager(); Index: support/src/test/java/tests/support/Support_PortManager.java =================================================================== --- support/src/test/java/tests/support/Support_PortManager.java (revision 500995) +++ support/src/test/java/tests/support/Support_PortManager.java (working copy) @@ -17,6 +17,7 @@ package tests.support; +import java.net.DatagramSocket; import java.net.ServerSocket; import java.util.Calendar; import java.util.TimeZone; @@ -25,6 +26,7 @@ private static int lastAssignedPort = somewhatRandomPort(); private static boolean failedOnce = false; + private static boolean failedOnceUDP = false; public static synchronized int getNextPort() { if (!failedOnce) { @@ -41,6 +43,21 @@ return getNextPort_unsafe(); } + public static synchronized int getNextPortForUDP() { + if (!failedOnceUDP) { + try { + DatagramSocket ds = new DatagramSocket(); + int port = ds.getLocalPort(); + + ds.close(); + return port; + } catch (Exception ex) { + failedOnceUDP = true; + } + } + return getNextPort_unsafe(); + } + public static synchronized int getNextPort_unsafe() { if (++lastAssignedPort > 65534) { lastAssignedPort = 6000;