DatagramSocketImplTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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
18package tests.api.java.net;
19
20import dalvik.annotation.TestTargetClass;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23
24import java.io.FileDescriptor;
25import java.io.IOException;
26import java.net.DatagramPacket;
27import java.net.DatagramSocketImpl;
28import java.net.InetAddress;
29import java.net.NetworkInterface;
30import java.net.SocketAddress;
31import java.net.SocketException;
32
33/*
34 * DatagramSocketImplFactory can be specified only once,
35 * therefore we can't check DatagramSocketImpl functionality.
36 */
37
38@TestTargetClass(value = DatagramSocketImpl.class,
39                 untestedMethods = {
40                 @TestTargetNew(
41                     level = TestLevel.NOT_FEASIBLE,
42                     notes = "",
43                     method = "bind",
44                     args = {int.class, InetAddress.class}
45                 ),
46                 @TestTargetNew(
47                     level = TestLevel.NOT_FEASIBLE,
48                     notes = "",
49                     method = "close",
50                     args = {}
51                 ),
52                 @TestTargetNew(
53                     level = TestLevel.NOT_FEASIBLE,
54                     notes = "",
55                     method = "create",
56                     args = {}
57                 ),
58                 @TestTargetNew(
59                     level = TestLevel.NOT_FEASIBLE,
60                     notes = "",
61                     method = "getTimeToLive",
62                     args = {}
63                 ),
64                 @TestTargetNew(
65                     level = TestLevel.NOT_FEASIBLE,
66                     notes = "",
67                     method = "getTTL",
68                     args = {}
69                 ),
70                 @TestTargetNew(
71                     level = TestLevel.NOT_FEASIBLE,
72                     notes = "",
73                     method = "join",
74                     args = {InetAddress.class}
75                 ),
76                 @TestTargetNew(
77                     level = TestLevel.NOT_FEASIBLE,
78                     notes = "",
79                     method = "joinGroup",
80                     args = { SocketAddress.class, NetworkInterface.class }
81                 ),
82                 @TestTargetNew(
83                     level = TestLevel.NOT_FEASIBLE,
84                     notes = "",
85                     method = "leave",
86                     args = { InetAddress.class }
87                 ),
88                 @TestTargetNew(
89                     level = TestLevel.NOT_FEASIBLE,
90                     notes = "",
91                     method = "leaveGroup",
92                     args = { SocketAddress.class, NetworkInterface.class }
93                 ),
94                 @TestTargetNew(
95                     level = TestLevel.NOT_FEASIBLE,
96                     notes = "",
97                     method = "peek",
98                     args = { InetAddress.class }
99                 ),
100                 @TestTargetNew(
101                     level = TestLevel.NOT_FEASIBLE,
102                     notes = "",
103                     method = "peekData",
104                     args = { DatagramPacket.class }
105                 ),
106                 @TestTargetNew(
107                     level = TestLevel.NOT_FEASIBLE,
108                     notes = "",
109                     method = "receive",
110                     args = { DatagramPacket.class }
111                 ),
112                 @TestTargetNew(
113                     level = TestLevel.NOT_FEASIBLE,
114                     notes = "",
115                     method = "send",
116                     args = { DatagramPacket.class }
117                 ),
118                 @TestTargetNew(
119                     level = TestLevel.NOT_FEASIBLE,
120                     notes = "",
121                     method = "setTimeToLive",
122                     args = { int.class }
123                 ),
124                 @TestTargetNew(
125                     level = TestLevel.NOT_FEASIBLE,
126                     notes = "",
127                     method = "setTTL",
128                     args = { byte.class }
129                 ),
130                 @TestTargetNew(
131                     level = TestLevel.NOT_FEASIBLE,
132                     notes = "",
133                     method = "setOption",
134                     args = { int.class, Object.class }
135                 ),
136                 @TestTargetNew(
137                     level = TestLevel.NOT_FEASIBLE,
138                     notes = "",
139                     method = "getOption",
140                     args = { int.class }
141                 )
142             })
143public class DatagramSocketImplTest extends junit.framework.TestCase {
144
145    MockDatagramSocketImpl ds;
146
147    public void setUp() {
148        ds = new MockDatagramSocketImpl();
149    }
150
151    public void tearDown() {
152        ds.close();
153        ds = null;
154    }
155    /**
156     * @tests java.net.DatagramSocketImpl#DatagramSocketImpl()
157     */
158    @TestTargetNew(
159        level = TestLevel.COMPLETE,
160        notes = "",
161        method = "DatagramSocketImpl",
162        args = {}
163    )
164    public void test_Constructor() throws Exception {
165        // regression test for Harmony-1117
166        MockDatagramSocketImpl impl = new MockDatagramSocketImpl();
167        assertNull(impl.getFileDescriptor());
168    }
169
170    @TestTargetNew(
171        level = TestLevel.SUFFICIENT,
172        notes = "SocketException is not checked.",
173        method = "connect",
174        args = {java.net.InetAddress.class, int.class}
175    )
176    public void test_connect() {
177        try {
178            InetAddress localHost = InetAddress.getLocalHost();
179            //int port = ds.getLocalPort();
180            ds.connect(localHost, 0);
181            DatagramPacket send = new DatagramPacket(new byte[10], 10,
182                    localHost, 0);
183            ds.send(send);
184        } catch (IOException e) {
185            fail("Unexpected IOException : " + e.getMessage());
186        } finally {
187            ds.close();
188        }
189    }
190
191    @TestTargetNew(
192        level = TestLevel.COMPLETE,
193        notes = "",
194        method = "disconnect",
195        args = {}
196    )
197    public void test_disconnect() {
198        try {
199            InetAddress localHost = InetAddress.getLocalHost();
200            //int port = ds.getLocalPort();
201            ds.connect(localHost, 0);
202            DatagramPacket send = new DatagramPacket(new byte[10], 10,
203                    localHost, 0);
204            ds.send(send);
205            ds.disconnect();
206        } catch (IOException e) {
207            fail("Unexpected IOException : " + e.getMessage());
208        } finally {
209            ds.close();
210        }
211    }
212
213    @TestTargetNew(
214        level = TestLevel.COMPLETE,
215        notes = "",
216        method = "getFileDescriptor",
217        args = {}
218    )
219    public void test_getFileDescriptor() {
220        assertNull(ds.getFileDescriptor());
221    }
222
223    @TestTargetNew(
224        level = TestLevel.COMPLETE,
225        notes = "",
226        method = "getLocalPort",
227        args = {}
228    )
229    public void test_getLocalPort() {
230        // RI fails here. RI returns 0. the spec doesn't say what the value for
231        // an unbound DatagramSocket should be. The same difference can be seen
232        // for Socket.getLocalPort. But there the spec says that unbound
233        // Sockets return -1. And for that method also the RI returns 0 which
234        // goes against the spec.
235        assertEquals(-1, ds.getLocalPort());
236    }
237}
238
239class MockDatagramSocketImpl extends DatagramSocketImpl {
240
241    @Override
242    public FileDescriptor getFileDescriptor() {
243        return super.getFileDescriptor();
244    }
245
246    @Override
247    public void bind(int port, InetAddress addr) throws SocketException {
248        // empty
249    }
250
251    @Override
252    public void close() {
253        // empty
254    }
255
256    @Override
257    public void create() throws SocketException {
258        // empty
259    }
260
261
262    @Override
263    public byte getTTL() throws IOException {
264        return 0;
265    }
266
267    @Override
268    public int getTimeToLive() throws IOException {
269        return 0;
270    }
271
272    @Override
273    public void join(InetAddress addr) throws IOException {
274        // empty
275    }
276
277    @Override
278    public void joinGroup(SocketAddress addr, NetworkInterface netInterface)
279            throws IOException {
280        // empty
281    }
282
283    @Override
284    public void leave(InetAddress addr) throws IOException {
285        // empty
286    }
287
288    @Override
289    public void leaveGroup(SocketAddress addr, NetworkInterface netInterface)
290            throws IOException {
291        // empty
292    }
293
294    @Override
295    public int peek(InetAddress sender) throws IOException {
296        return 0;
297    }
298
299    @Override
300    public int peekData(DatagramPacket pack) throws IOException {
301        return 0;
302    }
303
304    @Override
305    public void receive(DatagramPacket pack) throws IOException {
306        // empty
307    }
308
309    @Override
310    public void send(DatagramPacket pack) throws IOException {
311        // TODO Auto-generated method stub
312
313    }
314
315
316    @Override
317    public void setTTL(byte ttl) throws IOException {
318        // empty
319    }
320
321    @Override
322    public void setTimeToLive(int ttl) throws IOException {
323        // empty
324    }
325
326    public Object getOption(int optID) throws SocketException {
327        // TODO Auto-generated method stub
328        return null;
329    }
330
331    public void setOption(int optID, Object value) throws SocketException {
332        // TODO Auto-generated method stub
333
334    }
335
336    public void connect(InetAddress address, int port) throws SocketException {
337        super.connect(address, port);
338    }
339
340    public void disconnect() {
341        super.disconnect();
342    }
343
344    public int getLocalPort() {
345        return super.getLocalPort();
346    }
347}
348