1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2007 Ludovic Dewailly <ludovic.dewailly@dreameffect.org>
4    *
5    *  It is licensed under the following two licenses as alternatives:
6    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
7    *    2. Apache License (the "AL") Version 2.0
8    *
9    *  You may not use this file except in compliance with at least one of
10   *  the above two licenses.
11   *
12   *  You may obtain a copy of the LGPL at
13   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
14   *
15   *  You may obtain a copy of the AL at
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the LGPL or the AL for the specific language governing permissions and
22   *  limitations.
23   */
24  
25  package org.microemu.cldc.datagram;
26  
27  import java.io.IOException;
28  import java.net.InetAddress;
29  
30  import javax.microedition.io.Datagram;
31  
32  import junit.framework.TestCase;
33  
34  /**
35   * TestCase for {@link Connection}
36   */
37  public class ConnectionTest extends TestCase {
38  
39  	public void testNewDatagramInt() throws Exception {
40  		Connection conn = new Connection();
41  		conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false);
42  		Datagram datagram = conn.newDatagram(512);
43  		assertNotNull(datagram);
44  		assertEquals("localhost:1234", datagram.getAddress());
45  		assertEquals(512, datagram.getData().length);
46  		assertEquals(512, datagram.getLength());
47  		assertEquals(0, datagram.getOffset());
48  		conn.close();
49  	}
50  
51  	public void testNewDatagramIntString() throws Exception {
52  		Connection conn = new Connection();
53  		conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false);
54  		Datagram datagram = conn.newDatagram(512, "localhost:123");
55  		assertNotNull(datagram);
56  		assertEquals("localhost:123", datagram.getAddress());
57  		assertEquals(512, datagram.getData().length);
58  		assertEquals(512, datagram.getLength());
59  		assertEquals(0, datagram.getOffset());
60  		conn.close();
61  	}
62  
63  	public void testNewDatagramByteArrayInt() throws Exception {
64  		Connection conn = new Connection();
65  		conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false);
66  		byte[] data = new byte[1024];
67  		Datagram datagram = conn.newDatagram(data, data.length);
68  		assertNotNull(datagram);
69  		assertEquals("localhost:1234", datagram.getAddress());
70  		assertEquals(data, datagram.getData());
71  		assertEquals(data.length, datagram.getLength());
72  		assertEquals(0, datagram.getOffset());
73  		conn.close();
74  	}
75  
76  	public void testNewDatagramByteArrayIntString() throws Exception {
77  		Connection conn = new Connection();
78  		conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false);
79  		byte[] data = new byte[1024];
80  		Datagram datagram = conn.newDatagram(data, data.length, "localhost:123");
81  		assertNotNull(datagram);
82  		assertEquals("localhost:123", datagram.getAddress());
83  		assertEquals(data, datagram.getData());
84  		assertEquals(data.length, datagram.getLength());
85  		assertEquals(0, datagram.getOffset());
86  		conn.close();
87  	}
88  
89  	public void testGetLocalAddress() throws Exception {
90  		Connection conn = new Connection();
91  		// server connection
92  		conn.openConnection(Connection.PROTOCOL + ":1234", 0, false);
93  		assertEquals(InetAddress.getLocalHost().getHostAddress(), conn.getLocalAddress());
94  		conn.close();
95  		// client connection
96  		conn.openConnection(Connection.PROTOCOL + InetAddress.getLocalHost().getHostAddress() + ":1234", 0, false);
97  		assertEquals(InetAddress.getLocalHost().getHostAddress(), conn.getLocalAddress());
98  		conn.close();
99  	}
100 
101 	public void testGetLocalPort() throws Exception {
102 		Connection conn = new Connection();
103 		// server connection
104 		conn.openConnection(Connection.PROTOCOL + ":1234", 0, false);
105 		assertEquals(1234, conn.getLocalPort());
106 		conn.close();
107 		// client connection
108 		conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false);
109 		assertTrue(1234 != conn.getLocalPort());
110 		conn.close();
111 	}
112 
113 	public void testOpenConnection() throws Exception {
114 		Connection conn = new Connection();
115 		boolean exception;
116 		// invalid connection string
117 		try {
118 			conn.openConnection("http://test", 0, false);
119 			exception = false;
120 		} catch (IOException e) {
121 			exception = true;
122 		}
123 		assertTrue(exception);
124 		// missing port
125 		try {
126 			conn.openConnection(Connection.PROTOCOL + "localhost", 0, false);
127 			exception = false;
128 		} catch (IOException e) {
129 			exception = true;
130 		}
131 		assertTrue(exception);
132 		// client connection
133 		assertNotNull(conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false));
134 		conn.close();
135 		// server connection
136 		assertNotNull(conn.openConnection(Connection.PROTOCOL + ":1234", 0, false));
137 		conn.close();
138 	}
139 }