1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2007 Ludovic Dewailly <ludovic.dewailly@dreameffect.org>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.microemu.cldc.datagram;
21  
22  import java.io.IOException;
23  import java.net.InetAddress;
24  
25  import javax.microedition.io.Datagram;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * TestCase for {@link Connection}
31   */
32  public class ConnectionTest extends TestCase {
33  
34  	public void testNewDatagramInt() throws Exception {
35  		Connection conn = new Connection();
36  		conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false);
37  		Datagram datagram = conn.newDatagram(512);
38  		assertNotNull(datagram);
39  		assertEquals("localhost:1234", datagram.getAddress());
40  		assertEquals(512, datagram.getData().length);
41  		assertEquals(512, datagram.getLength());
42  		assertEquals(0, datagram.getOffset());
43  		conn.close();
44  	}
45  
46  	public void testNewDatagramIntString() throws Exception {
47  		Connection conn = new Connection();
48  		conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false);
49  		Datagram datagram = conn.newDatagram(512, "localhost:123");
50  		assertNotNull(datagram);
51  		assertEquals("localhost:123", datagram.getAddress());
52  		assertEquals(512, datagram.getData().length);
53  		assertEquals(512, datagram.getLength());
54  		assertEquals(0, datagram.getOffset());
55  		conn.close();
56  	}
57  
58  	public void testNewDatagramByteArrayInt() throws Exception {
59  		Connection conn = new Connection();
60  		conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false);
61  		byte[] data = new byte[1024];
62  		Datagram datagram = conn.newDatagram(data, data.length);
63  		assertNotNull(datagram);
64  		assertEquals("localhost:1234", datagram.getAddress());
65  		assertEquals(data, datagram.getData());
66  		assertEquals(data.length, datagram.getLength());
67  		assertEquals(0, datagram.getOffset());
68  		conn.close();
69  	}
70  
71  	public void testNewDatagramByteArrayIntString() throws Exception {
72  		Connection conn = new Connection();
73  		conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false);
74  		byte[] data = new byte[1024];
75  		Datagram datagram = conn.newDatagram(data, data.length, "localhost:123");
76  		assertNotNull(datagram);
77  		assertEquals("localhost:123", datagram.getAddress());
78  		assertEquals(data, datagram.getData());
79  		assertEquals(data.length, datagram.getLength());
80  		assertEquals(0, datagram.getOffset());
81  		conn.close();
82  	}
83  
84  	public void testGetLocalAddress() throws Exception {
85  		Connection conn = new Connection();
86  		// server connection
87  		conn.openConnection(Connection.PROTOCOL + ":1234", 0, false);
88  		assertEquals(InetAddress.getLocalHost().getHostAddress(), conn.getLocalAddress());
89  		conn.close();
90  		// client connection
91  		conn.openConnection(Connection.PROTOCOL + InetAddress.getLocalHost().getHostAddress() + ":1234", 0, false);
92  		assertEquals(InetAddress.getLocalHost().getHostAddress(), conn.getLocalAddress());
93  		conn.close();
94  	}
95  
96  	public void testGetLocalPort() throws Exception {
97  		Connection conn = new Connection();
98  		// server connection
99  		conn.openConnection(Connection.PROTOCOL + ":1234", 0, false);
100 		assertEquals(1234, conn.getLocalPort());
101 		conn.close();
102 		// client connection
103 		conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false);
104 		assertTrue(1234 != conn.getLocalPort());
105 		conn.close();
106 	}
107 
108 	public void testOpenConnection() throws Exception {
109 		Connection conn = new Connection();
110 		boolean exception;
111 		// invalid connection string
112 		try {
113 			conn.openConnection("http://test", 0, false);
114 			exception = false;
115 		} catch (IOException e) {
116 			exception = true;
117 		}
118 		assertTrue(exception);
119 		// missing port
120 		try {
121 			conn.openConnection(Connection.PROTOCOL + "localhost", 0, false);
122 			exception = false;
123 		} catch (IOException e) {
124 			exception = true;
125 		}
126 		assertTrue(exception);
127 		// client connection
128 		assertNotNull(conn.openConnection(Connection.PROTOCOL + "localhost:1234", 0, false));
129 		conn.close();
130 		// server connection
131 		assertNotNull(conn.openConnection(Connection.PROTOCOL + ":1234", 0, false));
132 		conn.close();
133 	}
134 }