1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.tests.java.net; 19 20 import java.io.FileDescriptor; 21 import java.io.IOException; 22 import java.net.DatagramPacket; 23 import java.net.DatagramSocketImpl; 24 import java.net.InetAddress; 25 import java.net.NetworkInterface; 26 import java.net.SocketAddress; 27 import java.net.SocketException; 28 import libcore.junit.junit3.TestCaseWithRules; 29 import libcore.junit.util.ResourceLeakageDetector; 30 import org.junit.Rule; 31 import org.junit.rules.TestRule; 32 33 public class DatagramSocketImplTest extends TestCaseWithRules { 34 @Rule 35 public TestRule guardRule = ResourceLeakageDetector.getRule(); 36 37 /** 38 * java.net.DatagramSocketImpl#DatagramSocketImpl() 39 */ test_Constructor()40 public void test_Constructor() throws Exception { 41 // regression test for Harmony-1117 42 MockDatagramSocketImpl impl = new MockDatagramSocketImpl(); 43 assertNull(impl.getFileDescriptor()); 44 } 45 46 test_connect()47 public void test_connect() throws Exception { 48 MockDatagramSocketImpl impl = new MockDatagramSocketImpl(); 49 InetAddress localhost = InetAddress.getByName("localhost"); //$NON-NLS-1$ 50 // connect do nothing, so will not throw exception 51 impl.test_connect(localhost, 0); 52 impl.test_connect(localhost, -1); 53 impl.test_connect(null, -1); 54 // disconnect 55 impl.test_disconnect(); 56 } 57 } 58 59 class MockDatagramSocketImpl extends DatagramSocketImpl { 60 61 @Override getFileDescriptor()62 public FileDescriptor getFileDescriptor() { 63 return super.getFileDescriptor(); 64 } 65 66 @Override bind(int port, InetAddress addr)67 protected void bind(int port, InetAddress addr) throws SocketException { 68 // empty 69 } 70 71 @Override close()72 protected void close() { 73 // empty 74 } 75 76 @Override create()77 protected void create() throws SocketException { 78 // empty 79 } 80 getOption(int optID)81 public Object getOption(int optID) throws SocketException { 82 return null; 83 } 84 85 @Override getTTL()86 protected byte getTTL() throws IOException { 87 return 0; 88 } 89 90 @Override getTimeToLive()91 protected int getTimeToLive() throws IOException { 92 return 0; 93 } 94 95 @Override join(InetAddress addr)96 protected void join(InetAddress addr) throws IOException { 97 // empty 98 } 99 100 @Override joinGroup(SocketAddress addr, NetworkInterface netInterface)101 protected void joinGroup(SocketAddress addr, NetworkInterface netInterface) 102 throws IOException { 103 // empty 104 } 105 106 @Override leave(InetAddress addr)107 protected void leave(InetAddress addr) throws IOException { 108 // empty 109 } 110 111 @Override leaveGroup(SocketAddress addr, NetworkInterface netInterface)112 protected void leaveGroup(SocketAddress addr, NetworkInterface netInterface) 113 throws IOException { 114 // empty 115 } 116 117 @Override peek(InetAddress sender)118 protected int peek(InetAddress sender) throws IOException { 119 return 0; 120 } 121 122 @Override peekData(DatagramPacket pack)123 protected int peekData(DatagramPacket pack) throws IOException { 124 return 0; 125 } 126 127 @Override receive(DatagramPacket pack)128 protected void receive(DatagramPacket pack) throws IOException { 129 // empty 130 } 131 132 @Override send(DatagramPacket pack)133 protected void send(DatagramPacket pack) throws IOException { 134 // empty 135 136 } 137 setOption(int optID, Object val)138 public void setOption(int optID, Object val) throws SocketException { 139 // empty 140 } 141 142 @Override setTTL(byte ttl)143 protected void setTTL(byte ttl) throws IOException { 144 // empty 145 } 146 147 @Override setTimeToLive(int ttl)148 protected void setTimeToLive(int ttl) throws IOException { 149 // empty 150 } 151 test_connect(InetAddress inetAddr, int port)152 public void test_connect(InetAddress inetAddr, int port) throws SocketException { 153 super.connect(inetAddr, port); 154 } 155 test_disconnect()156 public void test_disconnect() { 157 super.disconnect(); 158 } 159 } 160