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 libcore.java.net;
19
20import java.io.IOException;
21import java.net.DatagramPacket;
22import java.net.DatagramSocket;
23import java.net.InetAddress;
24
25public class OldDatagramPacketTest extends junit.framework.TestCase {
26
27    public void test_getPort() throws Exception {
28        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress.getLocalHost(), 1000);
29        assertEquals("Incorrect port returned", 1000, dp.getPort());
30
31        final DatagramSocket ss = new DatagramSocket();
32        Thread thread = new Thread(new Runnable() {
33            public void run() {
34                try {
35                    DatagramPacket packet = new DatagramPacket(new byte[256], 256);
36                    ss.setSoTimeout(3000);
37                    ss.receive(packet);
38                    ss.send(packet);
39                } catch (IOException e) {
40                    System.out.println("thread exception: " + e);
41                }
42            }
43        });
44        thread.start();
45
46        DatagramSocket cs = new DatagramSocket();
47        try {
48            byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6 };
49            DatagramPacket packet = new DatagramPacket(bytes, 6, InetAddress.getByName("localhost"), ss.getLocalPort());
50            cs.send(packet);
51            cs.setSoTimeout(3000);
52            cs.receive(packet);
53            cs.close();
54            assertEquals(packet.getPort(), ss.getLocalPort());
55        } finally {
56            cs.close();
57            ss.close();
58        }
59    }
60
61    public void test_setLengthI() {
62        try {
63            new DatagramPacket("Hello".getBytes(), 6);
64            fail("IllegalArgumentException was not thrown.");
65        } catch(IllegalArgumentException expected) {
66        }
67
68        try {
69            new DatagramPacket("Hello".getBytes(), -1);
70            fail("IllegalArgumentException was not thrown.");
71        } catch(IllegalArgumentException expected) {
72        }
73    }
74
75    public void test_setData$BII() {
76        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5);
77        try {
78            dp.setData(null, 2, 3);
79            fail("NullPointerException was not thrown.");
80        } catch(NullPointerException expected) {
81        }
82    }
83
84    public void test_setData$B() {
85        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5);
86        try {
87            dp.setData(null);
88            fail("NullPointerException was not thrown.");
89        } catch(NullPointerException expected) {
90        }
91    }
92}
93