1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.java.net; 18 19import junit.framework.TestCase; 20 21import java.net.DatagramSocket; 22import java.net.InetSocketAddress; 23 24public class DatagramSocketTest extends TestCase { 25 26 public void testInitialState() throws Exception { 27 DatagramSocket ds = new DatagramSocket(); 28 try { 29 assertTrue(ds.isBound()); 30 assertTrue(ds.getBroadcast()); // The RI starts DatagramSocket in broadcast mode. 31 assertFalse(ds.isClosed()); 32 assertFalse(ds.isConnected()); 33 assertTrue(ds.getLocalPort() > 0); 34 assertTrue(ds.getLocalAddress().isAnyLocalAddress()); 35 InetSocketAddress socketAddress = (InetSocketAddress) ds.getLocalSocketAddress(); 36 assertEquals(ds.getLocalPort(), socketAddress.getPort()); 37 assertEquals(ds.getLocalAddress(), socketAddress.getAddress()); 38 assertNull(ds.getInetAddress()); 39 assertEquals(-1, ds.getPort()); 40 assertNull(ds.getRemoteSocketAddress()); 41 assertFalse(ds.getReuseAddress()); 42 assertNull(ds.getChannel()); 43 } finally { 44 ds.close(); 45 } 46 } 47 48 public void testStateAfterClose() throws Exception { 49 DatagramSocket ds = new DatagramSocket(); 50 ds.close(); 51 assertTrue(ds.isBound()); 52 assertTrue(ds.isClosed()); 53 assertFalse(ds.isConnected()); 54 assertNull(ds.getLocalAddress()); 55 assertEquals(-1, ds.getLocalPort()); 56 assertNull(ds.getLocalSocketAddress()); 57 } 58} 59