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 junit.framework.TestCase;
28  
29  /**
30   * TestCase for {@link DatagramImpl}.
31   */
32  public class DatagramImplTest extends TestCase {
33  
34  	public void testDatagramImplInt() {
35  		boolean exception;
36  		try {
37  			new DatagramImpl(-1);
38  			exception = false;
39  		} catch (IllegalArgumentException e) {
40  			exception = true;
41  		}
42  		assertTrue(exception);
43  		DatagramImpl datagram = new DatagramImpl(100);
44  		assertEquals(100, datagram.getLength());
45  	}
46  
47  	public void testDatagramImplByteArrayInt() {
48  		byte[] buffer = new byte[100];
49  		DatagramImpl datagram = new DatagramImpl(buffer, 100);
50  		assertEquals(buffer, datagram.getData());
51  		assertEquals(100, datagram.getLength());
52  	}
53  
54  	public void testGetAddress() throws Exception {
55  		DatagramImpl datagram = new DatagramImpl(100);
56  		String address = "localhost:1234";
57  		datagram.setAddress(address);
58  		assertEquals(address, datagram.getAddress());
59  	}
60  
61  	public void testGetData() {
62  		byte[] data = new byte[20];
63  		DatagramImpl datagram = new DatagramImpl(data, data.length);
64  		for (int i = 0; i < data.length; i++) {
65  			data[i] = (byte) i;
66  			assertEquals(datagram.getData()[i], data[i]);
67  		}
68  	}
69  
70  	public void testGetLength() {
71  		DatagramImpl datagram = new DatagramImpl(20);
72  		for (int i = 1; i < 20; i++) {
73  			datagram.setLength(i);
74  			assertEquals(i, datagram.getLength());
75  		}
76  	}
77  
78  	public void testSetAddressString() throws Exception {
79  		DatagramImpl datagram = new DatagramImpl(20);
80  		boolean exception;
81  		try {
82  			datagram.setAddress((String) null);
83  			exception = false;
84  		} catch (NullPointerException e) {
85  			exception = true;
86  		}
87  		assertTrue(exception);
88  		// missing port
89  		try {
90  			datagram.setAddress("localhost");
91  			exception = false;
92  		} catch (IllegalArgumentException e) {
93  			exception = true;
94  		}
95  		assertTrue(exception);
96  		datagram.setAddress("localhost:1111");
97  		assertEquals("localhost:1111", datagram.getAddress());
98  
99  	}
100 
101 	public void testSetAddressDatagram() throws Exception {
102 		DatagramImpl datagram1 = new DatagramImpl(20);
103 		DatagramImpl datagram2 = new DatagramImpl(20);
104 		datagram1.setAddress("localhost:1111");
105 		datagram2.setAddress(datagram1);
106 		assertEquals("localhost:1111", datagram2.getAddress());
107 	}
108 
109 	public void testReadWriteBoolean() throws Exception {
110 		DatagramImpl datagram = new DatagramImpl(100);
111 		boolean value = true;
112 		for (int i = 0; i < 10; i++) {
113 			datagram.writeBoolean(value);
114 			value = !value;
115 		}
116 		value = true;
117 		for (int i = 0; i < 10; i++) {
118 			assertEquals(value, datagram.readBoolean());
119 			value = !value;
120 		}
121 		datagram.reset();
122 		value = true;
123 		for (int i = 0; i < 10; i++) {
124 			assertEquals(value, datagram.readBoolean());
125 			value = !value;
126 		}
127 		datagram.reset();
128 		value = false;
129 		for (int i = 0; i < 10; i++) {
130 			datagram.writeBoolean(value);
131 			value = !value;
132 		}
133 		value = false;
134 		for (int i = 0; i < 10; i++) {
135 			assertEquals(value, datagram.readBoolean());
136 			value = !value;
137 		}
138 	}
139 
140 	public void testReadWriteByte() throws Exception {
141 		DatagramImpl datagram = new DatagramImpl(100);
142 		for (int i = 0; i < 10; i++) {
143 			datagram.writeByte(i);
144 		}
145 		for (int i = 0; i < 10; i++) {
146 			assertEquals(i, datagram.readByte());
147 		}
148 		datagram.reset();
149 		for (int i = 0; i < 10; i++) {
150 			assertEquals(i, datagram.readByte());
151 		}
152 		datagram.reset();
153 		for (int i = 0; i < 10; i++) {
154 			datagram.writeByte(i + 10);
155 		}
156 		for (int i = 0; i < 10; i++) {
157 			assertEquals(i + 10, datagram.readByte());
158 		}
159 	}
160 
161 	public void testReadWriteChar() throws Exception {
162 		DatagramImpl datagram = new DatagramImpl(100);
163 		for (int i = 0; i < 10; i++) {
164 			datagram.writeChar((int) 'a' + i);
165 		}
166 		for (int i = 0; i < 10; i++) {
167 			assertEquals((int) 'a' + i, datagram.readChar());
168 		}
169 		datagram.reset();
170 		for (int i = 0; i < 10; i++) {
171 			assertEquals((int) 'a' + i, datagram.readChar());
172 		}
173 		datagram.reset();
174 		for (int i = 0; i < 10; i++) {
175 			datagram.writeChar((int) 'A' + i);
176 		}
177 		for (int i = 0; i < 10; i++) {
178 			assertEquals((int) 'A' + i, datagram.readChar());
179 		}
180 	}
181 
182 	public void testReadWriteUTF() throws Exception {
183 		String[] text = new String[] { "HelloWorld!", "Some text", "", "Good Stuff" };
184 		DatagramImpl datagram = new DatagramImpl(100);
185 		for (int i = 0; i < text.length; i++) {
186 			datagram.writeUTF(text[i]);
187 		}
188 		for (int i = 0; i < text.length; i++) {
189 			assertEquals(text[i], datagram.readUTF());
190 		}
191 		datagram.reset();
192 		for (int i = 0; i < text.length; i++) {
193 			assertEquals(text[i], datagram.readUTF());
194 		}
195 		datagram.reset();
196 		for (int i = 0; i < text.length; i++) {
197 			datagram.writeUTF("xxx" + text[i]);
198 		}
199 		for (int i = 0; i < text.length; i++) {
200 			assertEquals("xxx" + text[i], datagram.readUTF());
201 		}
202 	}
203 
204 	public void testSkipBytes() throws Exception {
205 		byte[] data = new byte[20];
206 		for (int i = 0; i < data.length; i++) {
207 			data[i] = (byte) i;
208 		}
209 		DatagramImpl datagram = new DatagramImpl(data, data.length);
210 		for (int i = 0; i < data.length; i++) {
211 			datagram.reset();
212 			datagram.skipBytes(i);
213 			for (int j = 0; j < data.length - i; j++) {
214 				assertEquals(data[i + j], datagram.readByte());
215 			}
216 		}
217 	}
218 }