001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.jndi.tests.javax.naming;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.util.Random;
025:
026: import javax.naming.BinaryRefAddr;
027: import javax.naming.RefAddr;
028:
029: import junit.framework.Assert;
030: import junit.framework.TestCase;
031:
032: public class BinaryRefAddrTest extends TestCase {
033:
034: public void testBinaryRefAddr_SimpleNormale() {
035: byte[] ab = new byte[] { 1, 2, 3 };
036: byte[] ab2;
037:
038: // Test normal condition
039: BinaryRefAddr addr = new BinaryRefAddr("binary", ab);
040: Assert.assertNotNull(addr);
041: Assert.assertEquals("binary", addr.getType());
042: ab2 = (byte[]) addr.getContent();
043: Assert.assertEquals(ab.length, ab2.length);
044: for (int i = ab2.length - 1; i >= 0; i--) {
045: Assert.assertEquals(ab[i], ab2[i]);
046: }
047: assertNotSame(ab, ab2);
048: }
049:
050: public void testBinaryRefAddr_SimpleNullType() {
051: byte[] ab = new byte[] { 1, 2, 3 };
052: byte[] ab2;
053:
054: // Test null "type" parameter
055: BinaryRefAddr addr = new BinaryRefAddr(null, ab);
056: Assert.assertNotNull(addr);
057: Assert.assertNull(addr.getType());
058: ab2 = (byte[]) addr.getContent();
059: Assert.assertEquals(ab.length, ab2.length);
060: for (int i = ab2.length - 1; i >= 0; i--) {
061: Assert.assertEquals(ab[i], ab2[i]);
062: }
063: }
064:
065: public void testBinaryRefAddr_SimpleNullAddress() {
066: BinaryRefAddr addr = null;
067:
068: // Test null address content
069: try {
070: addr = new BinaryRefAddr("binary", null);
071: fail("Should throw NullPointerException here.");
072: } catch (NullPointerException e) {
073: }
074: Assert.assertNull(addr);
075: }
076:
077: public void testBinaryRefAddr_ComplexNormal() {
078: byte[] ab = new byte[] { 1, 2, 3 };
079: byte[] ab2;
080:
081: // Test normal condition
082: BinaryRefAddr addr = new BinaryRefAddr("binary", ab, 1, 1);
083: Assert.assertNotNull(addr);
084: Assert.assertEquals("binary", addr.getType());
085: ab2 = (byte[]) addr.getContent();
086: Assert.assertEquals(ab2.length, 1);
087: for (int i = ab2.length - 1; i >= 0; i--) {
088: Assert.assertEquals(ab[i + 1], ab2[i]);
089: }
090: assertNotSame(ab, ab2);
091: }
092:
093: public void testBinaryRefAddr_ComplexNullType() {
094: byte[] ab = new byte[] { 1, 2, 3 };
095: byte[] ab2;
096:
097: // Test null "type" parameter
098: BinaryRefAddr addr = new BinaryRefAddr(null, ab, 1, 1);
099: Assert.assertNotNull(addr);
100: Assert.assertNull(addr.getType());
101: ab2 = (byte[]) addr.getContent();
102: Assert.assertEquals(ab2.length, 1);
103: for (int i = ab2.length - 1; i >= 0; i--) {
104: Assert.assertEquals(ab[i + 1], ab2[i]);
105: }
106: }
107:
108: public void testBinaryRefAddr_ComplexNullAddress() {
109: BinaryRefAddr addr = null;
110:
111: // Test null address content
112: try {
113: addr = new BinaryRefAddr("binary", null, 1, 1);
114: fail("Should throw NullPointerException here.");
115: } catch (NullPointerException e) {
116: }
117: Assert.assertNull(addr);
118: }
119:
120: public void testBinaryRefAddr_TooSmallIndex() {
121: BinaryRefAddr addr = null;
122:
123: // Test too small index
124: try {
125: addr = new BinaryRefAddr("binary", new byte[] { 2, 3 }, -1,
126: 1);
127: fail("Should throw ArrayIndexOutOfBoundsException here.");
128: } catch (ArrayIndexOutOfBoundsException e) {
129: }
130: Assert.assertNull(addr);
131: }
132:
133: public void testBinaryRefAddr_TooBigIndex() {
134: BinaryRefAddr addr = null;
135:
136: // Test too big index
137: try {
138: addr = new BinaryRefAddr("binary", new byte[] { 2, 3 }, 2,
139: 1);
140: fail("Should throw ArrayIndexOutOfBoundsException here.");
141: } catch (ArrayIndexOutOfBoundsException e) {
142: }
143: Assert.assertNull(addr);
144: }
145:
146: public void testBinaryRefAddr_ComplexZeroSize() {
147: byte[] ab;
148: BinaryRefAddr addr = null;
149:
150: // Test zero size
151: addr = new BinaryRefAddr("binary", new byte[] { 2, 3 }, 0, 0);
152: ab = (byte[]) addr.getContent();
153: Assert.assertEquals(ab.length, 0);
154: Assert.assertNotNull(addr);
155: }
156:
157: public void testBinaryRefAddr_TooSmallSize() {
158: BinaryRefAddr addr = null;
159:
160: // Test too small size
161: try {
162: addr = new BinaryRefAddr("binary", new byte[] { 2, 3 }, 0,
163: -1);
164: fail("Should throw NegativeArraySizeException here.");
165: } catch (NegativeArraySizeException e) {
166: }
167: Assert.assertNull(addr);
168: }
169:
170: public void testBinaryRefAddr_TooBigSize() {
171: BinaryRefAddr addr = null;
172:
173: // Test too big size
174: try {
175: addr = new BinaryRefAddr("binary", new byte[] { 2, 3 }, 0,
176: 3);
177: fail("Should throw ArrayIndexOutOfBoundsException here.");
178: } catch (ArrayIndexOutOfBoundsException e) {
179: }
180: Assert.assertNull(addr);
181:
182: }
183:
184: public void testGetType() {
185: // Test empty type
186: BinaryRefAddr addr = new BinaryRefAddr("", new byte[] { 1 });
187: Assert.assertEquals("", addr.getType());
188:
189: // Other conditions are tested in testBinaryRefAddr_XXX
190: }
191:
192: public void testEquals_Simple() {
193: String type = "Binary Address";
194: int count = 10;
195: byte[] address0 = new byte[count];
196: byte[] address1 = new byte[count];
197: Random random = new Random(100);
198: for (int i = 0; i < count; i++) {
199: address0[i] = (byte) random.nextInt();
200: address1[i] = address0[i];
201: }
202: BinaryRefAddr addr0 = new BinaryRefAddr(type, address0);
203: BinaryRefAddr addr1 = new BinaryRefAddr(type, address1);
204: assertTrue(addr0.equals(addr0));
205: assertFalse(addr0.equals(null));
206: assertTrue(addr1.equals(addr0));
207: assertTrue(addr0.equals(addr1));
208: }
209:
210: public void testEquals_TypeNull() {
211: int count = 10;
212: byte[] address0 = new byte[count];
213: byte[] address1 = new byte[count];
214: Random random = new Random(10);
215: for (int i = 0; i < count; i++) {
216: address0[i] = (byte) random.nextInt();
217: address1[i] = address0[i];
218: }
219:
220: BinaryRefAddr addr0 = new BinaryRefAddr(null, address0);
221: BinaryRefAddr addr1 = new BinaryRefAddr(null, address1);
222: try {
223: addr0.equals(addr1);
224: fail("Should throw NullPointerException.");
225: } catch (NullPointerException e) {
226: }
227: }
228:
229: public void testtestEquals_refAddr() {
230: String type = "Binary Type";
231: byte[] address = { 1, 2, 3, 4 };
232: BinaryRefAddr addr = new BinaryRefAddr(type, address);
233: MyRefAddr addr2 = new MyRefAddr(type, address);
234:
235: assertFalse(addr.equals(addr2));
236: }
237:
238: public void testHashcode_Simple() {
239: String type = "Binary Address";
240:
241: int count = 10;
242: byte[] address = new byte[count];
243: Random random = new Random(20);
244: for (int i = 0; i < count; i++) {
245: address[i] = (byte) random.nextInt();
246: }
247:
248: int hashCode = type.hashCode();
249: for (byte element : address) {
250: hashCode += element;
251: }
252: BinaryRefAddr addr = new BinaryRefAddr(type, address);
253: assertEquals(hashCode, addr.hashCode());
254: }
255:
256: public void testHashcode_TypeNull() {
257: byte[] address = { 1, 2, 3, };
258:
259: BinaryRefAddr addr = new BinaryRefAddr(null, address);
260: try {
261: addr.hashCode();
262: fail("Should throw NullPointerException.");
263: } catch (NullPointerException e) {
264: }
265:
266: }
267:
268: public void testGetContent_Simple() {
269: String type = "Binary Address";
270: byte[] address = { 1, 2, 3, 4, 5, 6 };
271: BinaryRefAddr addr = new BinaryRefAddr(type, address);
272:
273: assertTrue(java.util.Arrays.equals(address, (byte[]) addr
274: .getContent()));
275: }
276:
277: public void testToString() {
278: String type = "Binary Address";
279: byte[] address = { 'a', 3, 0x7F, (byte) 0x80, (byte) 90,
280: (byte) 0xFF };
281:
282: BinaryRefAddr addr = new BinaryRefAddr(type, address);
283:
284: String str = "The type of the address is: " + type
285: + "\nThe content of the address is:";
286: for (byte element : address) {
287: str += " " + Integer.toHexString(element);
288: }
289: str += "\n";
290: // assertEquals(str, addr.toString());
291: assertNotNull(addr.toString());
292: }
293:
294: public void testToString_TypeNull() {
295: byte[] address = { 1, 2, 3, };
296: BinaryRefAddr addr = new BinaryRefAddr(null, address);
297: // assertEquals(str, addr.toString());
298: assertNotNull(addr.toString());
299: }
300:
301: public void testSerializable_Simple()
302: throws ClassNotFoundException, IOException {
303: String type = "Binary Address";
304: int count = 10;
305: byte[] address = new byte[count];
306: Random random = new Random(20);
307: for (int i = 0; i < count; i++) {
308: address[i] = (byte) random.nextInt();
309: }
310: BinaryRefAddr addr = new BinaryRefAddr(type, address);
311:
312: // write to byte array
313: ByteArrayOutputStream baos = new ByteArrayOutputStream();
314: ObjectOutputStream oos = new ObjectOutputStream(baos);
315: oos.writeObject(addr);
316: byte[] buffer = baos.toByteArray();
317: oos.close();
318: baos.close();
319:
320: // read from byte array
321: ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
322: ObjectInputStream ois = new ObjectInputStream(bais);
323: BinaryRefAddr addr2 = (BinaryRefAddr) ois.readObject();
324: ois.close();
325: bais.close();
326:
327: assertEquals(addr, addr2);
328: }
329:
330: public void testSerializable_compatibility()
331: throws ClassNotFoundException, IOException {
332: // TO DO R: need to implement
333: ObjectInputStream ois = new ObjectInputStream(
334: getClass()
335: .getClassLoader()
336: .getResourceAsStream(
337: "/serialization/javax/naming/BinaryRefAddr.ser"));
338: BinaryRefAddr addr = (BinaryRefAddr) ois.readObject();
339: ois.close();
340:
341: String type = "Binary Address";
342: int count = 100;
343: byte[] address = new byte[count];
344: for (int i = 0; i < count; i++) {
345: address[i] = (byte) i;
346: }
347: BinaryRefAddr addr2 = new BinaryRefAddr(type, address);
348: assertEquals(addr, addr2);
349: }
350:
351: class MyRefAddr extends RefAddr {
352: private static final long serialVersionUID = 1L;
353: byte[] address;
354:
355: public MyRefAddr(String type, byte[] address) {
356: super (type);
357: this .address = new byte[address.length];
358: System.arraycopy(address, 0, this .address, 0,
359: address.length);
360: }
361:
362: @Override
363: public Object getContent() {
364: return address;
365: }
366: }
367: }
|