18658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes/* 28658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Licensed to the Apache Software Foundation (ASF) under one or more 38658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * contributor license agreements. See the NOTICE file distributed with 48658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * this work for additional information regarding copyright ownership. 58658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0 68658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * (the "License"); you may not use this file except in compliance with 78658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * the License. You may obtain a copy of the License at 88658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * 98658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * 118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Unless required by applicable law or agreed to in writing, software 128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * See the License for the specific language governing permissions and 158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * limitations under the License. 168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18e5fea3d504609d22337a5311d3ce0e72314bceeeNarayan Kamathpackage org.apache.harmony.tests.java.nio.channels; 198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 206e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniakimport android.system.ErrnoException; 216e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniakimport android.system.OsConstants; 228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.io.IOException; 238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.net.DatagramPacket; 248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.net.DatagramSocket; 2582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamathimport java.net.Inet6Address; 268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.net.InetSocketAddress; 278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.net.SocketAddress; 288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.net.SocketException; 298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.nio.ByteBuffer; 308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.nio.channels.AsynchronousCloseException; 318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.nio.channels.ClosedChannelException; 328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.nio.channels.DatagramChannel; 338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.nio.channels.IllegalBlockingModeException; 348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.nio.channels.NotYetConnectedException; 358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.nio.channels.UnresolvedAddressException; 368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.nio.channels.UnsupportedAddressTypeException; 378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport java.nio.channels.spi.SelectorProvider; 386e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniakimport java.util.concurrent.atomic.AtomicReference; 398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughesimport junit.framework.TestCase; 4082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamathimport libcore.io.IoUtils; 416e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniakimport libcore.io.Libcore; 428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes/** 448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test for DatagramChannel 458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughespublic class DatagramChannelTest extends TestCase { 478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private static final int CAPACITY_NORMAL = 200; 498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private static final int CAPACITY_1KB = 1024; 518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private static final int CAPACITY_64KB = 65536; 538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private static final int CAPACITY_ZERO = 0; 558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private static final int CAPACITY_ONE = 1; 578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private static final int TIME_UNIT = 500; 598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath private InetSocketAddress datagramSocket1Address; 6182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath private InetSocketAddress datagramSocket2Address; 628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath private InetSocketAddress channel1Address; 6482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath private InetSocketAddress channel2Address; 658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private DatagramChannel channel1; 678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private DatagramChannel channel2; 688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private DatagramSocket datagramSocket1; 708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private DatagramSocket datagramSocket2; 718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes protected void setUp() throws Exception { 738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes super.setUp(); 7482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 7582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1 = DatagramChannel.open(); 7682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2 = DatagramChannel.open(); 7782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 7863744c884dd4b4f4307f2b021fb894af164972afElliott Hughes channel1.socket().bind(new InetSocketAddress(Inet6Address.LOOPBACK, 0)); 7963744c884dd4b4f4307f2b021fb894af164972afElliott Hughes channel2.socket().bind(new InetSocketAddress(Inet6Address.LOOPBACK, 0)); 8082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 8163744c884dd4b4f4307f2b021fb894af164972afElliott Hughes channel1Address = (InetSocketAddress) channel1.socket().getLocalSocketAddress(); 8263744c884dd4b4f4307f2b021fb894af164972afElliott Hughes channel2Address = (InetSocketAddress) channel2.socket().getLocalSocketAddress(); 8382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 8482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.datagramSocket1 = new DatagramSocket(0, Inet6Address.LOOPBACK); 8582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.datagramSocket2 = new DatagramSocket(0, Inet6Address.LOOPBACK); 8682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 8782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath datagramSocket1Address = (InetSocketAddress) datagramSocket1.getLocalSocketAddress(); 8882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath datagramSocket2Address = (InetSocketAddress) datagramSocket2.getLocalSocketAddress(); 898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes protected void tearDown() throws Exception { 9282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath IoUtils.closeQuietly(channel1); 9382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath IoUtils.closeQuietly(channel2); 9482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath IoUtils.closeQuietly(datagramSocket1); 9582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath IoUtils.closeQuietly(datagramSocket2); 9682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 9782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath datagramSocket1Address = null; 9882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath datagramSocket2Address = null; 998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes super.tearDown(); 1008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 1018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 1028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 1038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test for methods in abstract class. 1048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 1058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 1068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'java.nio.channels.DatagramChannel.validOps()' 1078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 1088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testValidOps() { 1098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 1108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes .provider()); 1118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMocknull = new MockDatagramChannel(null); 1128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int val = this.channel1.validOps(); 1138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(5, val); 1148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(val, testMock.validOps()); 1158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(val, testMocknull.validOps()); 1168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 1178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 1188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 1198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'java.nio.channels.DatagramChannel.open()' 1208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 1218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testOpen() { 1228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 1238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes .provider()); 1248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMocknull = new MockDatagramChannel(null); 1258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertNull(testMocknull.provider()); 1268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertNotNull(testMock.provider()); 1278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(this.channel1.provider(), testMock.provider()); 1288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(5, testMock.validOps()); 1298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 1308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 1318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 1328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'java.nio.channels.DatagramChannel.read(ByteBuffer)' 1338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 1348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadByteBufferArray() throws IOException { 1358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes final int testNum = 0; 1368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 1378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes .provider()); 1388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMocknull = new MockDatagramChannel(null); 1398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int bufSize = 10; 1408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] readBuf = null; 1418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 1428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(readBuf); 1438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 1448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 1458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 1468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 14882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath long readres; 1498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 1508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readres = testMock.read(readBuf); 1518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 1528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 1538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 1548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 1558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readBuf = new ByteBuffer[bufSize]; 1568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 1578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readres = this.channel1.read(readBuf); 1588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NotYetConnectedException"); 15982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } catch (NotYetConnectedException expected) { 1608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 1628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readres = testMock.read(readBuf); 1638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(testNum, readres); 1648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readres = testMocknull.read(readBuf); 1658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(testNum, readres); 1668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 1678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 1688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 1698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'java.nio.channels.DatagramChannel.read(ByteBuffer)' 1708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 1718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadByteBufferArray_BufNull() throws IOException { 1728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 1738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes .provider()); 1748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMocknull = new MockDatagramChannel(null); 1758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 1768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] readBuf = null; 1778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 1788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(readBuf); 1798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 18082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } catch (NullPointerException expected) { 1818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 1828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 1838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes testMock.read(readBuf); 1848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 18582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } catch (NullPointerException expected) { 1868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 1878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 1888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes testMocknull.read(readBuf); 1898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 19082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } catch (NullPointerException expected) { 1918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 1928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 1938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 1948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 1958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'java.nio.channels.DatagramChannel.write(ByteBuffer)' 1968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 1978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBuffer() throws IOException { 1988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 1998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes .provider()); 2008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMocknull = new MockDatagramChannel(null); 2018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int bufSize = 10; 2028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] readBuf = null; 2038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 2048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(readBuf); 2058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 2068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 2078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 2088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 2098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 2108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes testMock.write(readBuf); 2118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 2128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 2138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 2148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 2158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readBuf = new ByteBuffer[bufSize]; 2168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 2178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(readBuf); 2188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NotYetConnectedException"); 2198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NotYetConnectedException e) { 2208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 2218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 2228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes long writeres = 0; 2238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeres = testMock.write(readBuf); 2248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 2258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, writeres); 2268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeres = testMocknull.write(readBuf); 2278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, writeres); 2288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 2298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 2308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 2318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'java.nio.channels.DatagramChannel.write(ByteBuffer)' 2328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 2338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBuffer_Bufnull() throws IOException { 2348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 2358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes .provider()); 2368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes MockDatagramChannel testMocknull = new MockDatagramChannel(null); 2378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] readBuf = null; 2388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 2398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(readBuf); 2408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 2418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 2428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 2438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 2448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 2458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes testMock.write(readBuf); 2468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 2478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 2488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 2498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 2508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 2518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes testMocknull.write(readBuf); 2528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 2538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 2548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 2558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 2568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 2578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 2588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 2598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test for socket() 2608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 2618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 2628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 2638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.socket()' 2648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 26582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testSocket_BasicStatusBeforeConnect() throws Exception { 26682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath final DatagramChannel dc = DatagramChannel.open(); 26782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 26882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertFalse(dc.isConnected());// not connected 26982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramSocket s1 = dc.socket(); 270933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 271933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertFalse(s1.isBound()); 272933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertFalse(s1.isClosed()); 273933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertFalse(s1.isConnected()); 274933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertFalse(s1.getBroadcast()); 275933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertFalse(s1.getReuseAddress()); 276933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertNull(s1.getInetAddress()); 277933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertTrue(s1.getLocalAddress().isAnyLocalAddress()); 278933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertEquals(s1.getLocalPort(), 0); 279933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertNull(s1.getLocalSocketAddress()); 280933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertEquals(s1.getPort(), -1); 281933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertTrue(s1.getReceiveBufferSize() >= 8192); 282933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertNull(s1.getRemoteSocketAddress()); 283933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertFalse(s1.getReuseAddress()); 284933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertTrue(s1.getSendBufferSize() >= 8192); 285933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertEquals(s1.getSoTimeout(), 0); 286933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertEquals(s1.getTrafficClass(), 0); 287933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 28882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramSocket s2 = dc.socket(); 2898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // same 2908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertSame(s1, s2); 291933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 292933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller dc.close(); 2938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 2948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 2958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 2968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.socket()' 2978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 2988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testSocket_Block_BasicStatusAfterConnect() throws IOException { 29982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath final DatagramChannel dc = DatagramChannel.open(); 30082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 30182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath dc.connect(datagramSocket1Address); 30282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramSocket s1 = dc.socket(); 3038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertSocketAfterConnect(s1); 30482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramSocket s2 = dc.socket(); 3058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // same 3068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertSame(s1, s2); 307933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 308933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller dc.close(); 3098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 3108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 311933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller public void testSocket_NonBlock_BasicStatusAfterConnect() throws IOException { 31282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath final DatagramChannel dc = DatagramChannel.open(); 31382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath dc.connect(datagramSocket1Address); 31482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath dc.configureBlocking(false); 31582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 31682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramSocket s1 = dc.socket(); 3178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertSocketAfterConnect(s1); 31882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramSocket s2 = dc.socket(); 3198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // same 3208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertSame(s1, s2); 3218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 322933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller dc.close(); 3238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 3248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 325933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller private void assertSocketAfterConnect(DatagramSocket s) throws SocketException { 3268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(s.isBound()); 3278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(s.isClosed()); 3288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(s.isConnected()); 3298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(s.getBroadcast()); 3308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(s.getReuseAddress()); 3318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertNotNull(s.getLocalSocketAddress()); 33282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(s.getPort(), datagramSocket1Address.getPort()); 3338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(s.getReceiveBufferSize() >= 8192); 3348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // not same , but equals 33582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertNotSame(s.getRemoteSocketAddress(), datagramSocket1Address); 33682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(s.getRemoteSocketAddress(), datagramSocket1Address); 3378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(s.getReuseAddress()); 3388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(s.getSendBufferSize() >= 8192); 3398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(s.getSoTimeout(), 0); 3408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(s.getTrafficClass(), 0); 3418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 3428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 343933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller /** 344933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller * Test method for 'DatagramChannelImpl.socket()' 345933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller */ 346933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller public void testSocket_ActionsBeforeConnect() throws IOException { 347933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertFalse(channel1.isConnected());// not connected 348933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertTrue(channel1.isBlocking()); 349933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller DatagramSocket s = channel1.socket(); 350933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 35182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath s.connect(datagramSocket2Address); 352933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertTrue(channel1.isConnected()); 353933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertTrue(s.isConnected()); 3548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 3558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes s.disconnect(); 356933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertFalse(channel1.isConnected()); 3578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(s.isConnected()); 3588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 3598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes s.close(); 3608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(s.isClosed()); 361933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertFalse(channel1.isOpen()); 3628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 3638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 364933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller /** 365933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller * Test method for 'DatagramChannelImpl.socket()' 366933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller */ 367933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller public void testSocket_Block_ActionsAfterConnect() throws IOException { 368933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertFalse(this.channel1.isConnected());// not connected 369933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller this.channel1.connect(datagramSocket1Address); 370933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller DatagramSocket s = this.channel1.socket(); 371933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertSocketActionAfterConnect(s); 372933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller } 373933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 374933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller public void testSocket_NonBlock_ActionsAfterConnect() throws IOException { 375933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller this.channel1.connect(datagramSocket1Address); 376933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller this.channel1.configureBlocking(false); 377933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller DatagramSocket s = this.channel1.socket(); 378933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertSocketActionAfterConnect(s); 379933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller } 380933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 381933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller private void assertSocketActionAfterConnect(DatagramSocket s) throws IOException { 38282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(s.getPort(), datagramSocket1Address.getPort()); 383933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller try { 384933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller s.connect(datagramSocket2Address); 385933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller fail(); 38647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } catch (IllegalStateException expected) { 38747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 388933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 3898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 3908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(s.isConnected()); 3918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // not changed 39282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(s.getPort(), datagramSocket1Address.getPort()); 3938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 3948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes s.disconnect(); 3958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 3968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(s.isConnected()); 3978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 3988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes s.close(); 3998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(s.isClosed()); 4008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 4018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 4038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 4048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test for isConnected() 4058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 4068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 4078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 4088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.isConnected()' 4098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 4108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testIsConnected_WithServer() throws IOException { 4118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectLocalServer(); 4128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes disconnectAfterConnected(); 4138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket1.close(); 4148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 4158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 4168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 4188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 4198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test for connect() 4208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 4218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 4228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 4238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 4248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 4258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testConnect_BlockWithServer() throws IOException { 4268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // blocking mode 4278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isBlocking()); 4288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectLocalServer(); 4298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes datagramSocket1.close(); 4308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes disconnectAfterConnected(); 4318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 4338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 4348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 4358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 4368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testConnect_BlockNoServer() throws IOException { 4378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectWithoutServer(); 4388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes disconnectAfterConnected(); 4398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 4418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 4428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 4438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 4448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testConnect_NonBlockWithServer() throws IOException { 4458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Non blocking mode 4468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 4478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectLocalServer(); 4488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes datagramSocket1.close(); 4498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes disconnectAfterConnected(); 4508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 4528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 4538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 4548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 4558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testConnect_Null() throws IOException { 4568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 4578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 4588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(null); 4598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$ 4608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalArgumentException e) { 4618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 4628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 4658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 4668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 4678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 4688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testConnect_UnsupportedType() throws IOException { 4698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 4708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes class SubSocketAddress extends SocketAddress { 4718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private static final long serialVersionUID = 1L; 4728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 4738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public SubSocketAddress() { 4748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes super(); 4758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes SocketAddress newTypeAddress = new SubSocketAddress(); 4788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 4798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(newTypeAddress); 4808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw an UnsupportedAddressTypeException here."); 4818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (UnsupportedAddressTypeException e) { 4828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 4838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 4868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 4878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 4888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 4898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testConnect_Unresolved() throws IOException { 4908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 4918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes InetSocketAddress unresolved = new InetSocketAddress( 4928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes "unresolved address", 1080); 4938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 4948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(unresolved); 4958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw an UnresolvedAddressException here."); //$NON-NLS-1$ 4968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (UnresolvedAddressException e) { 4978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 4988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 4998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testConnect_EmptyHost() throws Exception { 5028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 5038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(this.channel1, this.channel1 505396f2361ba82cce530a4ff8aa9ac0780af8d4d56Narayan Kamath .connect(new InetSocketAddress("", 1081))); 5068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 5108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 5118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 5128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testConnect_ClosedChannelException() throws IOException { 5138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 5148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 5158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 5168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 51782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel1.connect(datagramSocket1Address); 5188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw ClosedChannelException."); //$NON-NLS-1$ 5198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (ClosedChannelException e) { 5208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 5218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 5258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 5268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 5278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testConnect_IllegalStateException() throws IOException { 5288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 52982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel1.connect(datagramSocket1Address); 5308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 5318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // connect after connected. 5328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 53382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel1.connect(datagramSocket1Address); 5348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw IllegalStateException."); //$NON-NLS-1$ 5358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalStateException e) { 5368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 5378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 5418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 5428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 5438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testConnect_CheckOpenBeforeStatus() throws IOException { 5448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 54582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel1.connect(datagramSocket1Address); 5468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 5478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // connect after connected. 5488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 5498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 5508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // checking open is before checking status. 5518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 55282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel1.connect(datagramSocket1Address); 5538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw ClosedChannelException."); //$NON-NLS-1$ 5548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (ClosedChannelException e) { 5558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 5568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void disconnectAfterConnected() throws IOException { 5608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 5618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.disconnect(); 5628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 5638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void disconnectAfterClosed() throws IOException { 5668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 5678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 5688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.disconnect(); 5698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 5708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void connectLocalServer() throws IOException { 5738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 5748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.datagramSocket1.isBound()); 57582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertSame(this.channel1, this.channel1.connect(datagramSocket1Address)); 5768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 5778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void connectWithoutServer() throws IOException { 5808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 5818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket1.close(); 5828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.datagramSocket1.isClosed()); 58382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertSame(this.channel1, this.channel1.connect(datagramSocket1Address)); 5848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 5858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 5888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test for disconnect() 5898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 5908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 5918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 5928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.disconnect()' 5938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 5948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testDisconnect_BeforeConnect() throws IOException { 5958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 5968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(this.channel1, this.channel1.disconnect()); 5978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 5988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 5998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 6018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.disconnect()' 6028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 6038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testDisconnect_UnconnectedClosed() throws IOException { 6048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 6058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 6068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 6078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(this.channel1, this.channel1.disconnect()); 6088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 6098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 6108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 6128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.disconnect()' 6138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 6148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testDisconnect_BlockWithServerChannelClosed() 6158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 6168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isBlocking()); 6178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectLocalServer(); 6188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // disconnect after channel close 6198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 6208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes disconnectAfterClosed(); 6218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 6228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 6248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.disconnect()' 6258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 6268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testDisconnect_NonBlockWithServerChannelClosed() 6278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 6288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 6298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectLocalServer(); 6308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // disconnect after channel close 6318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 6328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes disconnectAfterClosed(); 6338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 6348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 6368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.disconnect()' 6378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 6388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testDisconnect_BlockWithServerServerClosed() throws IOException { 6398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isBlocking()); 6408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectLocalServer(); 6418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // disconnect after server close 6428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket1.close(); 6438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isOpen()); 6448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 6458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes disconnectAfterConnected(); 6468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 6478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 6498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.disconnect()' 6508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 6518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testDisconnect_NonBlockWithServerServerClosed() 6528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 6538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 6548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isBlocking()); 6558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectLocalServer(); 6568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // disconnect after server close 6578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket1.close(); 6588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isOpen()); 6598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 6608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes disconnectAfterConnected(); 6618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 6628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 6648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test for receive(): Behavior Without Server. 6658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 6668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 6688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 6698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 6708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_UnconnectedNull() throws Exception { 6718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 6728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 6738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.receive(null); 6748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw a NPE here."); //$NON-NLS-1$ 6758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 6768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 6778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 6788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 6798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 6818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 6828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 6838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_UnconnectedReadonly() throws Exception { 6848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 6858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL) 6868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes .asReadOnlyBuffer(); 6878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(dst.isReadOnly()); 6888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 6898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.receive(dst); 6908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$ 6918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalArgumentException e) { 6928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 6938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 6948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 6958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 6968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 6978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 6988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 6998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_UnconnectedBufEmpty() throws Exception { 7008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 7018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 7028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 7038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertNull(this.channel1.receive(dst)); 7048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 7058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 70682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testReceive_UnboundBufZero() throws Exception { 70782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramChannel dc = DatagramChannel.open(); 70882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 70982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertFalse(dc.isConnected()); 71082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertFalse(dc.socket().isBound()); 7118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ZERO); 71282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertNull(dc.receive(dst)); 713933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 714933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller dc.close(); 7158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 7168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 71782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testReceive_UnboundBufNotEmpty() throws Exception { 71882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramChannel dc = DatagramChannel.open(); 71982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertFalse(dc.isConnected()); 72082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertFalse(dc.socket().isBound()); 72182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 7228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 7238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // buf is not empty 7248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes dst.put((byte) 88); 7258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(dst.position() + CAPACITY_NORMAL - 1, dst.limit()); 72682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertNull(dc.receive(dst)); 727933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 728933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller dc.close(); 7298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 7308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 73182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testReceive_UnboundBufFull() throws Exception { 73282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramChannel dc = DatagramChannel.open(); 73382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 73482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertFalse(dc.isConnected()); 73582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertFalse(dc.socket().isBound()); 7368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ONE); 73782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 7388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // buf is full 7398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes dst.put((byte) 88); 7408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(dst.position(), dst.limit()); 74182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertNull(dc.receive(dst)); 742933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 743933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller dc.close(); 7448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 7458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 7468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 7478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 7488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 7498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_UnconnectedClose() throws Exception { 7508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 7518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 7528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 7538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 7548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 7558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertNull(this.channel1.receive(dst)); 7568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw a ClosedChannelException here."); //$NON-NLS-1$ 7578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (ClosedChannelException e) { 7588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 7598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 7608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 7618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 7628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 7638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 7648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 7658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_UnconnectedCloseNull() throws Exception { 7668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 7678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 7688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 7698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // checking buffer before checking open 7708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 7718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.receive(null); 7728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw a NPE here."); //$NON-NLS-1$ 7738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 7748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 7758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 7768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 7778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 7788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 7798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 7808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 7818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_UnconnectedCloseReadonly() throws Exception { 7828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isConnected()); 7838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL) 7848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes .asReadOnlyBuffer(); 7858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(dst.isReadOnly()); 7868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 7878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 7888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 7898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.receive(dst); 7908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$ 7918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalArgumentException e) { 7928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 7938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 7948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 7958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 7968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 7978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 7988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 7998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_NonBlockNoServerBufEmpty() throws Exception { 8008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 8018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNonBlockNoServer(CAPACITY_NORMAL); 8028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_BlockNoServerNull() throws Exception { 8088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isBlocking()); 8098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNoServerNull(); 8108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_NonBlockNoServerNull() throws Exception { 8168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 8178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNoServerNull(); 8188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_BlockNoServerReadonly() throws Exception { 8248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isBlocking()); 8258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNoServerReadonly(); 8268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_NonBlockNoServerReadonly() throws Exception { 8328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 8338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNoServerReadonly(); 8348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_NonBlockNoServerBufZero() throws Exception { 8408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 8418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNonBlockNoServer(CAPACITY_ZERO); 8428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_NonBlockNoServerBufNotEmpty() throws Exception { 8488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 8498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectWithoutServer(); 8508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = allocateNonEmptyBuf(); 8518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertNull(this.channel1.receive(dst)); 8528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_NonBlockNoServerBufFull() throws Exception { 8598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 8608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectWithoutServer(); 8618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = allocateFullBuf(); 8628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertNull(this.channel1.receive(dst)); 8638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_BlockNoServerChannelClose() throws Exception { 8698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isBlocking()); 8708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNoServerChannelClose(); 8718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_NonBlockNoServerChannelClose() throws Exception { 8778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 8788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNoServerChannelClose(); 8798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_BlockNoServerCloseNull() throws Exception { 8858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isBlocking()); 8868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNoServerChannelCloseNull(); 8878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 8928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_NonBlockNoServerCloseNull() throws Exception { 8938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 8948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNoServerChannelCloseNull(); 8958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 8968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 8978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 8988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 8998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 9008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_NonBlockNoServerCloseReadonly() throws Exception { 9018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 9028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNoServerChannelCloseReadonly(); 9038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 9058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 9068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 9078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 9088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_BlockNoServerCloseReadonly() throws Exception { 9098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isBlocking()); 9108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes receiveNoServerChannelCloseReadonly(); 9118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 9138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void receiveNoServerNull() throws IOException { 9148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectWithoutServer(); 9158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 9168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.receive(null); 9178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw a NPE here."); //$NON-NLS-1$ 9188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 9198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 9208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 9238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void receiveNoServerReadonly() throws IOException { 9248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectWithoutServer(); 9258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL) 9268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes .asReadOnlyBuffer(); 9278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(dst.isReadOnly()); 9288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 9298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.receive(dst); 9308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$ 9318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalArgumentException e) { 9328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 9338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 9368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void receiveNonBlockNoServer(int size) throws IOException { 9378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectWithoutServer(); 9388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(size); 9398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertNull(this.channel1.receive(dst)); 9408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 9428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void receiveNoServerChannelClose() throws IOException { 9438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectWithoutServer(); 9448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 9458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 9468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 9478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 9488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertNull(this.channel1.receive(dst)); 9498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw a ClosedChannelException here."); //$NON-NLS-1$ 9508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (ClosedChannelException e) { 9518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 9528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 9558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void receiveNoServerChannelCloseNull() throws IOException { 9568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectWithoutServer(); 9578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 9588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 9598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 9608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.receive(null); 9618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw a NPE here."); //$NON-NLS-1$ 9628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 9638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 9648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 9678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void receiveNoServerChannelCloseReadonly() throws IOException { 9688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes connectWithoutServer(); 9698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 9708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isOpen()); 9718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL) 9728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes .asReadOnlyBuffer(); 9738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(dst.isReadOnly()); 9748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 9758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.receive(dst); 9768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$ 9778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalArgumentException e) { 9788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 9798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 9828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private ByteBuffer allocateFullBuf() { 9838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ONE); 9848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // buf is full 9858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes dst.put((byte) 88); 9868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(dst.position(), dst.limit()); 9878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes return dst; 9888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 9908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private ByteBuffer allocateNonEmptyBuf() { 9918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 9928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // buf is not empty 9938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes dst.put((byte) 88); 9948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes dst.put((byte) 99); 9958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(dst.position() + CAPACITY_NORMAL - 2, dst.limit()); 9968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes return dst; 9978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 9988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 9998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 10008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test for send(): Behavior without server. 10018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 10028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 10038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void sendDataBlocking(InetSocketAddress addr, ByteBuffer writeBuf) 10048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 10058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes InetSocketAddress ipAddr = addr; 10068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, this.channel1.send(writeBuf, ipAddr)); 10078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isOpen()); 10088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isBlocking()); 10098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 10108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 10118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 10138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void sendDataNonBlocking(InetSocketAddress addr, ByteBuffer writeBuf) 10148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 10158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes InetSocketAddress ipAddr = addr; 10168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 10178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, this.channel1.send(writeBuf, ipAddr)); 10188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isOpen()); 10198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel1.isBlocking()); 10208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 10218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 10228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 10248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 10258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.send(ByteBuffer, SocketAddress)' 10268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 10278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testSend_NoServerBlockingCommon() throws IOException { 10288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 102982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendDataBlocking(datagramSocket1Address, writeBuf); 10308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 10328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testSend_NoServerNonblockingCommon() throws IOException { 10338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 103482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendDataNonBlocking(datagramSocket1Address, writeBuf); 10358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 10378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testSend_NoServerTwice() throws IOException { 10388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 103982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendDataBlocking(datagramSocket1Address, writeBuf); 10408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // can not buffer twice! 104182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(0, this.channel1.send(writeBuf, datagramSocket1Address)); 10428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 104382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.send(writeBuf, datagramSocket2Address); 10448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw IllegalArgumentException"); 10458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalArgumentException e) { 10468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 10478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 10508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testSend_NoServerNonBlockingTwice() throws IOException { 10518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 105282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendDataNonBlocking(datagramSocket1Address, writeBuf); 10538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // can not buffer twice! 105482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(0, this.channel1.send(writeBuf, datagramSocket1Address)); 10558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 105682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.send(writeBuf, datagramSocket2Address); 10578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw IllegalArgumentException"); 10588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalArgumentException e) { 10598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 10608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 10638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testSend_NoServerBufNull() throws IOException { 10648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 106582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendDataBlocking(datagramSocket1Address, null); 10668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw a NPE here."); 10678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 10688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 10698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 10728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testSend_NoServerBufNullTwice() throws IOException { 10738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 10748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 107582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendDataBlocking(datagramSocket1Address, null); 10768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw a NPE here."); 10778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 10788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 10798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 108082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendDataBlocking(datagramSocket1Address, writeBuf); 10818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 108282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.send(null, datagramSocket2Address); 10838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE"); 10848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 10858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 10868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 10898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testSend_NoServerAddrNull() throws IOException { 10908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 10918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 10928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sendDataBlocking(null, writeBuf); 1093396f2361ba82cce530a4ff8aa9ac0780af8d4d56Narayan Kamath fail("Should throw a IAE here."); 1094396f2361ba82cce530a4ff8aa9ac0780af8d4d56Narayan Kamath } catch (IllegalArgumentException expected) { 10958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 10978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 10988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testSend_NoServerAddrNullTwice() throws IOException { 10998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 11008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 11018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sendDataBlocking(null, writeBuf); 1102396f2361ba82cce530a4ff8aa9ac0780af8d4d56Narayan Kamath fail("Should throw a IAE here."); 1103396f2361ba82cce530a4ff8aa9ac0780af8d4d56Narayan Kamath } catch (IllegalArgumentException expected) { 11048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 1105396f2361ba82cce530a4ff8aa9ac0780af8d4d56Narayan Kamath 110682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendDataBlocking(datagramSocket1Address, writeBuf); 11078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 11088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.send(writeBuf, null); 1109396f2361ba82cce530a4ff8aa9ac0780af8d4d56Narayan Kamath fail("Should throw a IAE here."); 1110396f2361ba82cce530a4ff8aa9ac0780af8d4d56Narayan Kamath } catch (IllegalArgumentException expected) { 11118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 11158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test for receive()and send(): Send and Receive with Real Data 11168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 11178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Block_Normal() throws Exception { 111982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2("some normal string in testReceiveSend_Normal", 112082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1Address); 112182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnChannel1AndClose(CAPACITY_NORMAL, channel2Address, 11228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes "some normal string in testReceiveSend_Normal"); 11238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_NonBlock_NotBound() throws Exception { 11268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // not bound 11278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 11288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.configureBlocking(false); 112982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2("some normal string in testReceiveSend_Normal", 113082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath datagramSocket2Address); 11318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer buf = ByteBuffer.wrap(new byte[CAPACITY_NORMAL]); 113282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertNull(this.channel1.receive(buf)); 11338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Block_Normal_S2C() throws Exception { 113682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnDatagramSocket1( 113782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath "some normal string in testReceiveSend_Normal_S2C", channel1Address); 113882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnChannel1AndClose(CAPACITY_NORMAL, datagramSocket1Address, 11398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes "some normal string in testReceiveSend_Normal_S2C"); 11408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Block_Normal_C2S() throws Exception { 11438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes String str1 = "some normal string in testReceiveSend_Normal_C2S"; 114482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2(str1, datagramSocket1Address); 114582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnDatagramSocket1(CAPACITY_NORMAL, str1); 11468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_NonBlock_Normal_C2S() throws Exception { 11498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 11508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.configureBlocking(false); 11518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes String str1 = "some normal string in testReceiveSend_Normal_C2S"; 115282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2(str1, datagramSocket1Address); 115382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnDatagramSocket1(CAPACITY_NORMAL, str1); 11548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Normal_S2S() throws Exception { 11578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes String msg = "normal string in testReceiveSend_Normal_S2S"; 11588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramPacket rdp = new DatagramPacket(msg.getBytes(), msg.length(), 115982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath datagramSocket2Address); 11608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket1.send(rdp); 11618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] buf = new byte[CAPACITY_NORMAL]; 11628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket2.setSoTimeout(TIME_UNIT); 11638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes rdp = new DatagramPacket(buf, buf.length); 11648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket2.receive(rdp); 11658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(new String(buf, 0, CAPACITY_NORMAL).trim(), msg); 11668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Block_Empty() throws Exception { 116982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2("", channel1Address); 117082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnChannel1AndClose(CAPACITY_NORMAL, channel2Address, ""); 11718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_NonBlock_Empty() throws Exception { 11748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 11758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.configureBlocking(false); 117682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2("", channel1Address); 117782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnChannel1AndClose(CAPACITY_NORMAL, channel2Address, ""); 11788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Block_Empty_S2C() throws Exception { 118182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnDatagramSocket1("", channel1Address); 118282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnChannel1AndClose(CAPACITY_NORMAL, datagramSocket1Address, ""); 11838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_NonBlock_Empty_S2C() throws Exception { 11868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 11878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.configureBlocking(false); 118882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnDatagramSocket1("", channel1Address); 118982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnChannel1AndClose(CAPACITY_NORMAL, datagramSocket1Address, ""); 11908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Block_Empty_C2S() throws Exception { 119382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2("", datagramSocket1Address); 119482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnDatagramSocket1(CAPACITY_NORMAL, ""); 11958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 11968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 11978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_NonBlock_Empty_C2S() throws Exception { 11988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 11998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.configureBlocking(false); 120082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2("", datagramSocket1Address); 120182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnDatagramSocket1(CAPACITY_NORMAL, ""); 12028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 12048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Empty_S2S() throws Exception { 12058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes String msg = ""; 12068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramPacket rdp = new DatagramPacket(msg.getBytes(), msg.length(), 120782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath datagramSocket2Address); 12088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket1.send(rdp); 12098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] buf = new byte[CAPACITY_NORMAL]; 12108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket2.setSoTimeout(TIME_UNIT); 12118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes rdp = new DatagramPacket(buf, buf.length); 12128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket2.receive(rdp); 12138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(new String(buf, 0, CAPACITY_NORMAL).trim(), msg); 12148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 12168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Block_Oversize() throws Exception { 121782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2("0123456789", channel1Address); 121882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnChannel1AndClose(5, channel2Address, "01234"); 12198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 12218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Block_Oversize_C2S() throws Exception { 122282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2("0123456789", datagramSocket1Address); 122382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnDatagramSocket1(5, "01234"); 12248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 12268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_NonBlock_Oversize_C2S() throws Exception { 12278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 12288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.configureBlocking(false); 122982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2("0123456789", datagramSocket1Address); 123082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnDatagramSocket1(5, "01234"); 12318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 12338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_Block_Oversize_S2C() throws Exception { 123482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnDatagramSocket1("0123456789", channel1Address); 123582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnChannel1AndClose(5, datagramSocket1Address, "01234"); 12368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 12388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_8K() throws Exception { 12398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes StringBuffer str8k = new StringBuffer(); 12408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < 8 * CAPACITY_1KB; i++) { 12418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes str8k.append('a'); 12428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes String str = str8k.toString(); 124482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 124582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2(str, channel1Address); 124682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath receiveOnChannel1AndClose(8 * CAPACITY_1KB, channel2Address, str); 12478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 12498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceiveSend_64K() throws Exception { 12508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes StringBuffer str64k = new StringBuffer(); 12518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < CAPACITY_64KB; i++) { 12528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes str64k.append('a'); 12538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes String str = str64k.toString(); 12558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 12568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes Thread.sleep(TIME_UNIT); 125782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.send(ByteBuffer.wrap(str.getBytes()), datagramSocket1Address); 12588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw SocketException!"); 125947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } catch (SocketException expected) { 12608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 126382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath private void sendOnChannel2(String data, SocketAddress address) 126482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath throws IOException { 126582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(data.length(), channel2.send(ByteBuffer.wrap(data.getBytes()), address)); 12668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 126882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath private void sendOnDatagramSocket1(String data, InetSocketAddress address) 12698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws Exception { 127082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramPacket rdp = new DatagramPacket(data.getBytes(), data.length(), address); 12718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket1.send(rdp); 12728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 127482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath private void receiveOnChannel1AndClose(int bufSize, 127582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress expectedAddress, String expectedString) throws IOException { 12768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 12778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer buf = ByteBuffer.wrap(new byte[bufSize]); 1278933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller InetSocketAddress senderAddr; 12798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes long startTime = System.currentTimeMillis(); 12808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes do { 128182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath senderAddr = (InetSocketAddress) this.channel1.receive(buf); 12828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // continue loop when channel1 is non-blocking and no data was 12838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // received. 128482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath if (channel1.isBlocking() || senderAddr != null) { 12858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes break; 12868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // avoid dead loop 12888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTimeout(startTime, 10000); 12898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } while (true); 129082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 129182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(senderAddr.getAddress(), Inet6Address.LOOPBACK); 129282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(expectedAddress.getPort(), senderAddr.getPort()); 129382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(new String(buf.array(), 0, buf.position()), expectedString); 12948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } finally { 12958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 12968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 12988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 12998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 13008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Fails if the difference between current time and start time is greater 13018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * than timeout. 13028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 13038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void assertTimeout(long startTime, long timeout) { 13048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes long currentTime = System.currentTimeMillis(); 13058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes if ((currentTime - startTime) > timeout) { 13068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Timeout"); 13078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 131082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath private void receiveOnDatagramSocket1(int bufSize, String expectedString) 13118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 13128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] buf = new byte[bufSize]; 13138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket1.setSoTimeout(6000); 13148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramPacket rdp = new DatagramPacket(buf, buf.length); 13158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.datagramSocket1.receive(rdp); 13168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(new String(buf, 0, bufSize).trim(), expectedString); 13178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 131982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testRead_fromSend() throws Exception { 13208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); 13218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes String strHello = "hello"; 132282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 132382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel1.connect(channel2Address); 132482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel2.send(ByteBuffer.wrap(strHello.getBytes()), channel1Address); 132582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 13268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(strHello.length(), this.channel1.read(buf)); 13278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertAscii(buf, strHello); 13288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 13308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReceive_Peek_NoSecurity_Nonblocking() throws Exception { 13318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes String strHello = "hello"; 133282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 133382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath sendOnChannel2(strHello, channel1Address); 13348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 13358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // for accepted addr, no problem. 13368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); 133782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 13388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes InetSocketAddress source = (InetSocketAddress) this.channel1.receive(buf); 133982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(channel2Address, source); 13408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertAscii(buf, strHello); 13418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 13438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private static void assertAscii(ByteBuffer b, String s) { 13448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(s.length(), b.position()); 13458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < s.length(); ++i) { 13468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(s.charAt(i), b.get(i)); 13478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 13508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 13518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test for write() 13528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 13538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 13548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void connectWriteBuf(InetSocketAddress ipAddr, ByteBuffer buf) 13558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 13568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 13578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 13588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, this.channel1.write(buf)); 13598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.write(buf)); 13608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 13628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void noconnectWrite(ByteBuffer buf) throws IOException { 13638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 13648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(buf); 13658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw NotYetConnectedException"); 13668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NotYetConnectedException e) { 13678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 13688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 13718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 13728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.write(ByteBuffer)' 13738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 13748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBuffer_Block() throws IOException { 13758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 137682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath connectWriteBuf(datagramSocket1Address, writeBuf); 13778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 13798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBuffer_NonBlock() throws IOException { 13808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 13818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 138282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath connectWriteBuf(datagramSocket1Address, writeBuf); 13838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 13858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBuffer_Block_closed() throws IOException { 13868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 138782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 13888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes noconnectWrite(writeBuf); 13898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 13908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 13918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 13928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 13938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.write(writeBuf); 13948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw ClosedChannelException"); 13958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (ClosedChannelException e) { 13968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 13978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 13998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 14008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBuffer_NonBlock_closed() throws IOException { 14018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 140282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 14038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // non block mode 14048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 14058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes noconnectWrite(writeBuf); 14068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 14078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 14088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 14098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 14108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.write(writeBuf); 14118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw ClosedChannelException"); 14128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (ClosedChannelException e) { 14138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 14148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 14178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBuffer_Block_BufNull() throws IOException { 14188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(0); 141982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 14208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 14218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write((ByteBuffer) null); 14228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE."); 14238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 14248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 14258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 14278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 14288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 14298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write((ByteBuffer) null); 14308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE."); 14318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 14328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 14338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.write(writeBuf)); 14358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes datagramSocket1.close(); 14368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 14378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write((ByteBuffer) null); 14388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE."); 14398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 14408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 14418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 14448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBuffer_NonBlock_BufNull() throws IOException { 14458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer writeBuf = ByteBuffer.allocateDirect(0); 144682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 14478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 14488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // non block mode 14498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 14508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 14518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 14528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write((ByteBuffer) null); 14538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE."); 14548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 14558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 14568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 14588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 14598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 14608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write((ByteBuffer) null); 14618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE."); 14628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 14638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 14648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.write(writeBuf)); 14668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes datagramSocket1.close(); 14678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 14688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write((ByteBuffer) null); 14698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NPE."); 14708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 14718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 14728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 14758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 14768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.write(ByteBuffer[], int, int)' 14778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 14788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBufferArrayIntInt_Block() throws IOException { 14798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] writeBuf = new ByteBuffer[2]; 14808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 14818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 148282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 14838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 14848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(writeBuf, 0, 2); 14858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NotYetConnectedException."); 14868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NotYetConnectedException e) { 14878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 14888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 14908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 14918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2)); 14928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // cannot be buffered again! 14938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.write(writeBuf, 0, 1)); 14948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 14958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 14968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 14978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBufferArrayIntInt_NonBlock() throws IOException { 14988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] writeBuf = new ByteBuffer[2]; 14998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 15008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 150182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 15028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // non-block mode 15038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 15048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 15058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(writeBuf, 0, 2); 15068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NotYetConnectedException."); 15078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NotYetConnectedException e) { 15088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 15098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 15118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 15128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2)); 15138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // cannot be buffered again! 15148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.write(writeBuf, 0, 1)); 15158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 15168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 15188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBufferArrayIntInt_NoConnectIndexBad() 15198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 15208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] writeBuf = new ByteBuffer[2]; 15218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 15228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 152382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 15248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 15258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(writeBuf, -1, 2); 15268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw IndexOutOfBoundsException"); 15278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IndexOutOfBoundsException e) { 15288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 15298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 15318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(writeBuf, 0, -1); 15328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw IndexOutOfBoundsException"); 15338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IndexOutOfBoundsException e) { 15348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 15358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 15378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 15388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2)); 15398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // cannot be buffered again! 15408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.write(writeBuf, 0, 1)); 15418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 15438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBufferArrayIntInt_ConnectedIndexBad() 15448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 15458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] writeBuf = new ByteBuffer[2]; 15468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 15478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 154882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 15498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 15508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 15518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 15528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(writeBuf, -1, 2); 15538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw IndexOutOfBoundsException"); 15548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IndexOutOfBoundsException e) { 15558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 15568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 15588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(writeBuf, 0, -1); 15598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw IndexOutOfBoundsException"); 15608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IndexOutOfBoundsException e) { 15618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 15628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 15658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBufferArrayIntInt_BufNullNoConnect() 15668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 15678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] writeBuf = new ByteBuffer[2]; 15688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 15698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 15708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(null, 0, 2); 15718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail(); 15728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException expected) { 15738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 15758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(writeBuf, -1, 2); 15768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail(); 15778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IndexOutOfBoundsException expected) { 15788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 15808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(writeBuf, 0, 3); 15818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail(); 15828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IndexOutOfBoundsException expected) { 15838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 15868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testWriteByteBufferArrayIntInt_BufNullConnect() 15878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 15888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] writeBuf = new ByteBuffer[2]; 15898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 159082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 15918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 15928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 15938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 15948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(null, 0, 2); 15958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw NPE"); 15968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 15978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 15988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 15998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 16008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(writeBuf, 0, 3); 16018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw IndexOutOfBoundsException"); 16028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IndexOutOfBoundsException e) { 16038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 16048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes datagramSocket1.close(); 16068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 16078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.write(null, 0, 2); 16088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw NPE"); 16098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 16108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 16118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 16148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 16158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test for read() 16168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 16178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 16188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 16198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.read(ByteBuffer)' 16208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 16218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadByteBuffer() throws IOException { 16228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer readBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 16238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 16248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(readBuf); 16258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw NotYetConnectedException"); 16268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NotYetConnectedException e) { 16278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 16288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 162982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel1.connect(datagramSocket1Address); 16308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 16318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 16328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // note : blocking-mode will make the read process endless! 16338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.read(readBuf)); 16348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.close(); 16358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 16368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(readBuf); 16378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw ClosedChannelException"); 16388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (ClosedChannelException e) { 16398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 16408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 16438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadByteBuffer_bufNull() throws IOException { 16448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer readBuf = ByteBuffer.allocateDirect(0); 164582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 16468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 16478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(readBuf); 16488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw NotYetConnectedException"); 16498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NotYetConnectedException e) { 16508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 16518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 16538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 16548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 16558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.read((ByteBuffer) null); 16568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw NPE"); 16578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 16588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 16598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 16618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // note : blocking-mode will make the read process endless! 16628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.read(readBuf)); 16638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes datagramSocket1.close(); 16648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 16668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 16678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * Test method for 'DatagramChannelImpl.read(ByteBuffer[], int, int)' 16688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 16698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadByteBufferArrayIntInt() throws IOException { 16708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] readBuf = new ByteBuffer[2]; 16718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 16728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 167382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 16748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 16758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(readBuf, 0, 2); 16768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw NotYetConnectedException"); 16778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NotYetConnectedException e) { 16788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 16798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 16818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 16828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 16838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // note : blocking-mode will make the read process endless! 16848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.read(readBuf, 0, 1)); 16858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.read(readBuf, 0, 2)); 16868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes datagramSocket1.close(); 16878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 16898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadByteBufferArrayIntInt_exceptions() throws IOException { 16908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes //regression test for HARMONY-932 16918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 16928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel.open().read(new ByteBuffer[] {}, 2, Integer.MAX_VALUE); 16938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail(); 16948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IndexOutOfBoundsException expected) { 16958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 16968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 16978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel.open().read(new ByteBuffer[] {}, -1, 0); 16988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail(); 16998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IndexOutOfBoundsException expected) { 17008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 17028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel.open().read((ByteBuffer[]) null, 0, 0); 17038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail(); 17048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException expected) { 17058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 17088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadByteBufferArrayIntInt_BufNull() throws IOException { 17098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] readBuf = new ByteBuffer[2]; 17108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 171182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath InetSocketAddress ipAddr = datagramSocket1Address; 17128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 17138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(null, 0, 0); 17148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw NPE"); 17158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 17168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 17178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.connect(ipAddr); 17198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel1.isConnected()); 17208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 17218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // note : blocking-mode will make the read process endless! 17228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 17238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(null, 0, 0); 17248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw NPE"); 17258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 17268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 17278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.read(readBuf, 0, 1)); 17298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 17308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(readBuf, 0, 2); 17318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw NPE"); 17328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 17338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 17348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 17368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(readBuf, 0, 3); 17378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw IndexOutOfBoundsException"); 17388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IndexOutOfBoundsException e) { 17398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // correct 17408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes datagramSocket1.close(); 17428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 17448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 17458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // test read and write 17468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 17478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 174882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public static class A { 174982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath protected int z; 175082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } 175182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 175282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public static class B extends A { 175382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath protected int z; 175482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 175582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath void foo() { 175682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath super.z+=1; 175782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } 175882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } 175982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 176082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 176182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testReadWrite_asyncClose() throws Exception { 17628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[2]; 17638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 17648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 176582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 176682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 176782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 17688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes new Thread() { 17698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void run() { 17708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 17718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes Thread.sleep(TIME_UNIT); 17728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.close(); 17738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (Exception e) { 17748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes //ignore 17758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes }.start(); 177882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 17798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 17808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.read(targetBuf); 17818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw AsynchronousCloseException"); 17828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (AsynchronousCloseException e) { 17838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ok 17848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 17868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 17878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadWrite_Block_Zero() throws Exception { 17888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[0]; 17898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[0]; 179082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 179182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 179282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 17938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 17948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 17958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 17968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.write(sourceBuf)); 17978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 17988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 17998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 18008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int readCount = this.channel2.read(targetBuf); 18018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, readCount); 18038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 18048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadWrite_Block_Normal() throws Exception { 18068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 18078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 18088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 18098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 18108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 18118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 181282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 181382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 18148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readWriteReadData(this.channel1, sourceArray, this.channel2, 18168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes targetArray, CAPACITY_NORMAL, "testReadWrite_Block_Normal"); 18178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 18188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadWrite_Block_Empty() throws Exception { 18208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // empty buf 18218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = "".getBytes(); 18228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 18238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 182482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 182582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 18268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 18288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 18298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.write(sourceBuf)); 18308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 18328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 18338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // empty message let the reader blocked 18348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes closeBlockedReaderChannel2(targetBuf); 18358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 18368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadWrite_changeBlock_Empty() throws Exception { 18388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // empty buf 18398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = "".getBytes(); 18408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 18418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 184282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 184382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 18448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 18468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 18478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel1.write(sourceBuf)); 18488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 18508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 18518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // empty message let the reader blocked 18528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes new Thread() { 18538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void run() { 18548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 18558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes Thread.sleep(TIME_UNIT); 18568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel2.configureBlocking(false); 18578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes Thread.sleep(TIME_UNIT * 5); 18588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel2.close(); 18598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (Exception e) { 18608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // do nothing 18618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 18628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 18638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes }.start(); 18648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 18658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(this.channel2.isBlocking()); 18668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.read(targetBuf); 18678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw AsynchronousCloseException"); 18688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (AsynchronousCloseException e) { 18698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertFalse(this.channel2.isBlocking()); 18708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 18718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 18728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 18738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadWrite_Block_8KB() throws Exception { 18758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_1KB * 8]; 18768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_1KB * 8]; 18778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 18788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 18798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 18808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 188182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 188282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 18838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readWriteReadData(this.channel1, sourceArray, this.channel2, 18858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes targetArray, 8 * CAPACITY_1KB, "testReadWrite_Block_8KB"); 18868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 18878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /* 18898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * sender write the sourceArray whose size is dataSize, and receiver read 18908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * the data into targetArray 18918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 18928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void readWriteReadData(DatagramChannel sender, byte[] sourceArray, 18938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel receiver, byte[] targetArray, int dataSize, 18948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes String methodName) throws IOException { 18958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 18968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 18978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(dataSize, sender.write(sourceBuf)); 18988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 18998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 19008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 19018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int count = 0; 19038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int total = 0; 19048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes long beginTime = System.currentTimeMillis(); 19058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes while (total < dataSize && (count = receiver.read(targetBuf)) != -1) { 19068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes total = total + count; 19078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // 3s timeout to avoid dead loop 19088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes if (System.currentTimeMillis() - beginTime > 3000){ 19098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes break; 19108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(dataSize, total); 19148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(targetBuf.position(), total); 19158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes targetBuf.flip(); 19168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes targetArray = targetBuf.array(); 19178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < targetArray.length; i++) { 19188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(targetArray[i], (byte) i); 19198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadWrite_Block_64K() throws Exception { 19238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_64KB]; 19248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 19258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 19268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 192882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 19298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 19318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 19328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 19338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.write(sourceBuf); 19348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw IOException"); 193582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } catch (IOException expected) { 19368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // too big 19378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadWrite_Block_DifferentAddr() throws Exception { 19418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 19428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 19438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 19448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 19458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 194763744c884dd4b4f4307f2b021fb894af164972afElliott Hughes this.channel1.connect(channel1.socket().getLocalSocketAddress()); 194882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel2.connect(datagramSocket1Address); // the different addr 19498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 19518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 19528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, this.channel1.write(sourceBuf)); 19538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 19558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 19568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // the wrong connected addr will make the read blocked. 19578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // we close the blocked channel 19588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes closeBlockedReaderChannel2(targetBuf); 19598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 196182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testReadWrite_Block_WriterNotBound() throws Exception { 19628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 19638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 19648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 19658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 19668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 196882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramChannel dc = DatagramChannel.open(); 196982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // The writer isn't bound, but is connected. 197082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath dc.connect(channel1Address); 19718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 19738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 197482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(CAPACITY_NORMAL, dc.write(sourceBuf)); 197582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 197682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // Connect channel2 after data has been written. 197763744c884dd4b4f4307f2b021fb894af164972afElliott Hughes channel2.connect(dc.socket().getLocalSocketAddress()); 19788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 19808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 19818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes closeBlockedReaderChannel2(targetBuf); 1982933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 1983933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller dc.close(); 19848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 19858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 198682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // NOTE: The original harmony test tested that things still work 198782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // if there's no socket bound at the the address we're connecting to. 198882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // 198982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // It isn't really feasible to implement that in a non-racy way. 199082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testReadWrite_Block_WriterConnectLater() throws Exception { 19918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 19938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 199482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // The reader is bound & connected to channel1. 199582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 19968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 19978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 19988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 19998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes new Thread() { 20008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void run() { 20018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 20028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes Thread.sleep(TIME_UNIT); 20038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // bind later 20048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 20058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 20068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 20078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 200882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 200982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 20108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write later 20118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 20128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, channel1.write(sourceBuf)); 20138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (Exception e) { 20148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // do nothing 20158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes }.start(); 20188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 20198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int count = 0; 20208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int total = 0; 20218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes long beginTime = System.currentTimeMillis(); 202282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 20238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes while (total < CAPACITY_NORMAL && (count = channel2.read(targetBuf)) != -1) { 20248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes total = total + count; 20258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // 3s timeout to avoid dead loop 20268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes if (System.currentTimeMillis() - beginTime > 3000){ 20278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes break; 20288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 20318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, total); 20328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(targetBuf.position(), total); 20338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes targetBuf.flip(); 20348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes targetArray = targetBuf.array(); 20358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < targetArray.length; i++) { 20368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(targetArray[i], (byte) i); 20378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 204082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // NOTE: The original harmony test tested that things still work 204182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // if there's no socket bound at the the address we're connecting to. 204282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // 204382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // It isn't really feasible to implement that in a non-racy way. 204482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testReadWrite_Block_ReaderNotConnected() throws Exception { 20458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 20468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 20478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 20488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 20498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 205182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // reader channel2 is not connected. 205282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel1.connect(channel2Address); 20538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 20548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 20558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 20568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, this.channel1.write(sourceBuf)); 20578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 20588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 20598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 206082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath try { 206182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath this.channel2.read(targetBuf); 206282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath fail(); 206382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } catch (NotYetConnectedException expected) { 206482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } 20658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 20678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes private void closeBlockedReaderChannel2(ByteBuffer targetBuf) 20688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws IOException { 206982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertTrue(this.channel2.isBlocking()); 207082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 20718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes new Thread() { 20728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void run() { 20738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 20748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes Thread.sleep(TIME_UNIT); 207582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } catch (InterruptedException ie) { 207682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath fail(); 20778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 207882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath IoUtils.closeQuietly(channel2); 20798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes }.start(); 208182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 20828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 20838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.read(targetBuf); 20848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw AsynchronousCloseException"); 20858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (AsynchronousCloseException e) { 20868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // OK. 20878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 20908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 20918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Test read and write in non-block mode. 20928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // ------------------------------------------------------------------- 20938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadWrite_NonBlock_Normal() throws Exception { 20948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 20958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 20968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 20978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 20988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 20998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 21018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.configureBlocking(false); 21028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 210382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 210482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 21058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readWriteReadData(this.channel1, sourceArray, this.channel2, 21078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes targetArray, CAPACITY_NORMAL, "testReadWrite_NonBlock_Normal"); 21088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 21098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadWrite_NonBlock_8KB() throws Exception { 21118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_1KB * 8]; 21128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_1KB * 8]; 21138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 21148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 21158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 21168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 21188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.configureBlocking(false); 21198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // bind and connect 212182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 212282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 21238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readWriteReadData(this.channel1, sourceArray, this.channel2, 21258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes targetArray, 8 * CAPACITY_1KB, "testReadWrite_NonBlock_8KB"); 21268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 21278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void testReadWrite_NonBlock_DifferentAddr() throws Exception { 21298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 21308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 21318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 21328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 21338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 21348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 21368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.configureBlocking(false); 21378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 213882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 213982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(datagramSocket1Address);// the different addr 21408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21418658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 21428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 21438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, this.channel1.write(sourceBuf)); 21448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 21468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 21478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel2.read(targetBuf)); 21488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 21498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 215082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 215182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testReadWrite_NonBlock_WriterNotBound() throws Exception { 21528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 21538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 21548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 21558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 21568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 21578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 215882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath DatagramChannel dc = DatagramChannel.open(); 215982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // The writer isn't bound, but is connected. 216082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath dc.connect(channel1Address); 216182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath dc.configureBlocking(false); 216282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.configureBlocking(false); 21638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 21658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 216682c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(CAPACITY_NORMAL, dc.write(sourceBuf)); 216782c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 216882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // Connect channel2 after data has been written. 216963744c884dd4b4f4307f2b021fb894af164972afElliott Hughes channel2.connect(dc.socket().getLocalSocketAddress()); 21708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 21728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 21738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(0, this.channel2.read(targetBuf)); 2174933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 2175933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller dc.close(); 21768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 21778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 217882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // NOTE: The original harmony test tested that things still work 217982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // if there's no socket bound at the the address we're connecting to. 218082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // 218182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath // It isn't really feasible to implement that in a non-racy way. 218282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath public void testReadWrite_NonBlock_ReaderNotConnected() throws Exception { 21838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 21848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] targetArray = new byte[CAPACITY_NORMAL]; 21858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes for (int i = 0; i < sourceArray.length; i++) { 21868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceArray[i] = (byte) i; 21878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 21888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel1.configureBlocking(false); 21908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes this.channel2.configureBlocking(false); 21918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 219282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 21938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 21958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 21968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, this.channel1.write(sourceBuf)); 21978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 21988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // read 21998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 220082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 220182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath try { 220282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath assertEquals(0, this.channel2.read(targetBuf)); 220382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath fail(); 220482c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } catch (NotYetConnectedException expected) { 220582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath } 22068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 22078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 22088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void test_write_LBuffer_positioned() throws Exception { 22098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // Regression test for Harmony-683 22108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int position = 16; 22118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel dc = DatagramChannel.open(); 22128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 221382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath dc.connect(datagramSocket1Address); 22148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // write 22158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 22168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceBuf.position(position); 22178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL - position, dc.write(sourceBuf)); 22188658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 22198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 22208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void test_send_LBuffer_LSocketAddress_PositionNotZero() 22218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes throws Exception { 22228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // regression test for Harmony-701 22238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int CAPACITY_NORMAL = 256; 22248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes int position = 16; 22258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel dc = DatagramChannel.open(); 22268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes byte[] sourceArray = new byte[CAPACITY_NORMAL]; 22278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // send ByteBuffer whose position is not zero 22288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 22298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sourceBuf.position(position); 223082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath int ret = dc.send(sourceBuf, datagramSocket1Address); 22318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // assert send (256 - 16) bytes 22328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL - position, ret); 22338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // assert the position of ByteBuffer has been set 22348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, sourceBuf.position()); 22358658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 22368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 22378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 22388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * @tests DatagramChannel#read(ByteBuffer[]) 22398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 22408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void test_read_$LByteBuffer() throws Exception { 224182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 224282c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 224382c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 22448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // regression test for Harmony-754 22458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel2.write(ByteBuffer.allocate(CAPACITY_NORMAL)); 22468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 22478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] readBuf = new ByteBuffer[2]; 22488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 22498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 22508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 22518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.configureBlocking(true); 22528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertEquals(CAPACITY_NORMAL, channel1.read(readBuf)); 22538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 22548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 22558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 22568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * @tests DatagramChannel#read(ByteBuffer[],int,int) 22578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 22588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void test_read_$LByteBufferII() throws Exception { 225982c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.connect(channel2Address); 226082c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel2.connect(channel1Address); 226182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath 22628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // regression test for Harmony-754 22638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel2.write(ByteBuffer.allocate(CAPACITY_NORMAL)); 22648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 22658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer[] readBuf = new ByteBuffer[2]; 22668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 22678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes readBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 22688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 22698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.configureBlocking(true); 2270933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller assertEquals(CAPACITY_NORMAL, channel1.read(readBuf, 0, 2)); 22718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 22728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 22738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 22748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * @tests DatagramChannel#read(ByteBuffer) 22758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 22768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void test_read_LByteBuffer_closed_nullBuf() throws Exception { 22778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // regression test for Harmony-754 22788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer c = null; 22798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel channel = DatagramChannel.open(); 22808658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel.close(); 22818658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try{ 22828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel.read(c); 22838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NullPointerException"); 22848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e){ 22858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // expected 22868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 22878658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 22888658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 22898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 22908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * @tests DatagramChannel#read(ByteBuffer) 22918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 22928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void test_read_LByteBuffer_NotConnected_nullBuf() throws Exception { 22938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // regression test for Harmony-754 22948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer c = null; 22958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel channel = DatagramChannel.open(); 22968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try{ 22978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel.read(c); 22988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NullPointerException"); 22998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e){ 23008658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // expected 23018658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23028658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23038658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 23048658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 23058658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * @tests DatagramChannel#read(ByteBuffer) 23068658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 23078658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void test_read_LByteBuffer_readOnlyBuf() throws Exception { 23088658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // regression test for Harmony-754 23098658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer c = ByteBuffer.allocate(1); 23108658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel channel = DatagramChannel.open(); 23118658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try{ 23128658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel.read(c.asReadOnlyBuffer()); 23138658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NotYetConnectedException"); 23148658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NotYetConnectedException e){ 23158658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalArgumentException e){ 23168658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // expected 23178658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 231882c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel.connect(datagramSocket1Address); 23198658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try{ 23208658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel.read(c.asReadOnlyBuffer()); 23218658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw IllegalArgumentException"); 23228658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalArgumentException e){ 23238658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // expected 23248658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23258658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23268658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 23278658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 23288658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * @tests DatagramChannel#send(ByteBuffer, SocketAddress) 23298658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 23308658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void test_send_LByteBuffer_LSocketAddress_closed() throws IOException{ 23318658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // regression test for Harmony-913 23328658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.close(); 23338658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); 23348658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 233582c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.send(buf, datagramSocket1Address); 23368658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw ClosedChannelException"); 23378658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (ClosedChannelException e) { 23388658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes //pass 23398658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23408658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 234182c642531ba3c9ad29cb3125f7547a226b661b11Narayan Kamath channel1.send(null, datagramSocket1Address); 23428658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NullPointerException"); 23438658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 23448658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes //pass 23458658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23468658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 23478658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.send(buf, null); 23488658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw ClosedChannelException"); 23498658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (ClosedChannelException e) { 23508658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes //pass 23518658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23528658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 23538658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel1.send(null, null); 23548658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("Should throw NullPointerException"); 23558658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (NullPointerException e) { 23568658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes //pass 23578658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23588658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23598658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 23608658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes /** 23618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes * @tests DatagramChannel#socket() 23628658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes */ 23638658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void test_socket_IllegalBlockingModeException() throws Exception { 23648658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // regression test for Harmony-1036 23658658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel channel = DatagramChannel.open(); 23668658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes channel.configureBlocking(false); 23678658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramSocket socket = channel.socket(); 23688658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 23698658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes socket.send(null); 23708658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw IllegalBlockingModeException"); 23718658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalBlockingModeException e) { 23728658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // expected 23738658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23748658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes try { 23758658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes socket.receive(null); 23768658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes fail("should throw IllegalBlockingModeException"); 23778658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } catch (IllegalBlockingModeException e) { 23788658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes // expected 23798658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 2380933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller 2381933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller channel.close(); 23828658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 23838658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 23848658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes public void test_bounded_harmony6493() throws IOException { 23858658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel server = DatagramChannel.open(); 23868658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes InetSocketAddress addr = new InetSocketAddress("localhost", 0); 238763744c884dd4b4f4307f2b021fb894af164972afElliott Hughes server.socket().bind(addr); 238863744c884dd4b4f4307f2b021fb894af164972afElliott Hughes SocketAddress boundedAddress = server.socket().getLocalSocketAddress(); 23898658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 23908658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes DatagramChannel client = DatagramChannel.open(); 23918658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes ByteBuffer sent = ByteBuffer.allocate(1024); 23928658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sent.put("test".getBytes()); 23938658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes sent.flip(); 23948658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes client.send(sent, boundedAddress); 23958658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes assertTrue(client.socket().isBound()); 23968658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes 23978658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes server.close(); 23988658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes client.close(); 23998658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes } 240047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 240147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller public void test_bind_null() throws Exception { 240247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller DatagramChannel dc = DatagramChannel.open(); 240347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller try { 240463744c884dd4b4f4307f2b021fb894af164972afElliott Hughes assertNull(dc.socket().getLocalSocketAddress()); 240547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 240663744c884dd4b4f4307f2b021fb894af164972afElliott Hughes dc.socket().bind(null); 240747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 240863744c884dd4b4f4307f2b021fb894af164972afElliott Hughes InetSocketAddress localAddress = (InetSocketAddress) dc.socket().getLocalSocketAddress(); 240947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertTrue(localAddress.getAddress().isAnyLocalAddress()); 241047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertTrue(localAddress.getPort() > 0); 241147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } finally { 241247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller dc.close(); 241347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 241447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 241547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 241647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller public void test_bind_failure() throws Exception { 241747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller DatagramChannel dc = DatagramChannel.open(); 241847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller try { 241947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller // Bind to a local address that is in use 242063744c884dd4b4f4307f2b021fb894af164972afElliott Hughes dc.socket().bind(channel1Address); 242147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller fail(); 242247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } catch (IOException expected) { 242347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } finally { 242447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller dc.close(); 242547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 242647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 242747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 242847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller public void test_bind_closed() throws Exception { 242947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller DatagramChannel dc = DatagramChannel.open(); 243047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller dc.close(); 243147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 243247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller try { 243363744c884dd4b4f4307f2b021fb894af164972afElliott Hughes dc.socket().bind(null); 243447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller fail(); 243563744c884dd4b4f4307f2b021fb894af164972afElliott Hughes } catch (IOException expected) { 243647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } finally { 243747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller dc.close(); 243847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 243947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 244047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 244147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller public void test_bind_explicitPort() throws Exception { 244263744c884dd4b4f4307f2b021fb894af164972afElliott Hughes InetSocketAddress address = (InetSocketAddress) channel1.socket().getLocalSocketAddress(); 244347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertTrue(address.getPort() > 0); 244447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 244547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller DatagramChannel dc = DatagramChannel.open(); 244647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller // Allow the socket to bind to a port we know is already in use. 244747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller dc.socket().setReuseAddress(true); 244847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller InetSocketAddress bindAddress = new InetSocketAddress("localhost", address.getPort()); 244963744c884dd4b4f4307f2b021fb894af164972afElliott Hughes dc.socket().bind(bindAddress); 245047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 245163744c884dd4b4f4307f2b021fb894af164972afElliott Hughes InetSocketAddress boundAddress = (InetSocketAddress) dc.socket().getLocalSocketAddress(); 245247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertEquals(bindAddress.getHostName(), boundAddress.getHostName()); 245347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertEquals(bindAddress.getPort(), boundAddress.getPort()); 245447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 245547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller dc.close(); 245647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller channel1.close(); 245747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 245847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 245947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller /** Checks that the SocketChannel and associated Socket agree on the socket state. */ 246047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller public void test_bind_socketSync() throws IOException { 246147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller DatagramChannel dc = DatagramChannel.open(); 246263744c884dd4b4f4307f2b021fb894af164972afElliott Hughes assertNull(dc.socket().getLocalSocketAddress()); 246347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 246447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller DatagramSocket socket = dc.socket(); 246547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertNull(socket.getLocalSocketAddress()); 246647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertFalse(socket.isBound()); 246747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 246847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); 246963744c884dd4b4f4307f2b021fb894af164972afElliott Hughes dc.socket().bind(bindAddr); 247047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 247163744c884dd4b4f4307f2b021fb894af164972afElliott Hughes InetSocketAddress actualAddr = (InetSocketAddress) dc.socket().getLocalSocketAddress(); 247247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertEquals(actualAddr, socket.getLocalSocketAddress()); 247347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertEquals(bindAddr.getHostName(), actualAddr.getHostName()); 247447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertTrue(socket.isBound()); 247547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertFalse(socket.isConnected()); 247647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertFalse(socket.isClosed()); 247747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 247847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller dc.close(); 247947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 248047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertFalse(dc.isOpen()); 248147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertTrue(socket.isClosed()); 248247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 248347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 248447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller /** 248547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * Checks that the SocketChannel and associated Socket agree on the socket state, even if 248647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * the Socket object is requested/created after bind(). 248747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller */ 248847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller public void test_bind_socketSyncAfterBind() throws IOException { 248947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller DatagramChannel dc = DatagramChannel.open(); 249063744c884dd4b4f4307f2b021fb894af164972afElliott Hughes assertNull(dc.socket().getLocalSocketAddress()); 249147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 249247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); 249363744c884dd4b4f4307f2b021fb894af164972afElliott Hughes dc.socket().bind(bindAddr); 249447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 249547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller // Socket creation after bind(). 249647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller DatagramSocket socket = dc.socket(); 249763744c884dd4b4f4307f2b021fb894af164972afElliott Hughes InetSocketAddress actualAddr = (InetSocketAddress) dc.socket().getLocalSocketAddress(); 249847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertEquals(actualAddr, socket.getLocalSocketAddress()); 249947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertEquals(bindAddr.getHostName(), actualAddr.getHostName()); 250047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertTrue(socket.isBound()); 250147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertFalse(socket.isConnected()); 250247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertFalse(socket.isClosed()); 250347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 250447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller dc.close(); 250547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 250647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertFalse(dc.isOpen()); 250747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertTrue(socket.isClosed()); 250847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 250947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 251047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller public void test_getLocalSocketAddress_afterClose() throws IOException { 251147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller DatagramChannel dc = DatagramChannel.open(); 251263744c884dd4b4f4307f2b021fb894af164972afElliott Hughes assertNull(dc.socket().getLocalSocketAddress()); 251347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 251447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); 251563744c884dd4b4f4307f2b021fb894af164972afElliott Hughes dc.socket().bind(bindAddr); 251647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 251763744c884dd4b4f4307f2b021fb894af164972afElliott Hughes assertNotNull(dc.socket().getLocalSocketAddress()); 251847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 251947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller dc.close(); 252047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 252147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller assertFalse(dc.isOpen()); 252247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller 252363744c884dd4b4f4307f2b021fb894af164972afElliott Hughes dc.socket().getLocalSocketAddress(); 252447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller } 25256e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak 25266e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak // b/27294715 25276e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak public void test_concurrentShutdown() throws Exception { 25286e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak DatagramChannel dc = DatagramChannel.open(); 25296e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak dc.configureBlocking(true); 25306e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak dc.bind(new InetSocketAddress(Inet6Address.LOOPBACK, 0)); 25316e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak // Set 4s timeout 25326e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak dc.socket().setSoTimeout(4000); 25336e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak 25346e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak final AtomicReference<Exception> killerThreadException = new AtomicReference<Exception>(null); 25356e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak final Thread killer = new Thread(new Runnable() { 25366e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak public void run() { 25376e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak try { 25386e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak Thread.sleep(2000); 25396e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak try { 25406e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak Libcore.os.shutdown(dc.socket().getFileDescriptor$(), OsConstants.SHUT_RDWR); 25416e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak } catch (ErrnoException expected) { 25426e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak if (OsConstants.ENOTCONN != expected.errno) { 25436e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak killerThreadException.set(expected); 25446e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak } 25456e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak } 25466e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak } catch (Exception ex) { 25476e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak killerThreadException.set(ex); 25486e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak } 25496e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak } 25506e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak }); 25516e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak killer.start(); 25526e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak 25536e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak ByteBuffer dst = ByteBuffer.allocate(CAPACITY_NORMAL); 25546e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak assertEquals(null, dc.receive(dst)); 25556e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak assertEquals(0, dst.position()); 25566e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak dc.close(); 25576e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak 25586e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak killer.join(); 25596e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak assertNull(killerThreadException.get()); 25606e888e9390649a9ab2557da5b28bb75be39e1b74Przemyslaw Szczepaniak } 25618658a3f1236f1801f00b9416c9294486a125faaeElliott Hughes} 2562