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.Enumeration;
025: import java.util.Random;
026:
027: import javax.naming.BinaryRefAddr;
028: import javax.naming.RefAddr;
029: import javax.naming.Reference;
030: import javax.naming.StringRefAddr;
031:
032: import junit.framework.TestCase;
033:
034: public class ReferenceTest extends TestCase {
035:
036: private Reference ref;
037:
038: private byte[] buffer;
039:
040: @Override
041: protected void setUp() {
042: int bufferLen = 50;
043: buffer = new byte[bufferLen];
044: Random random = new Random();
045: for (int i = 0; i < bufferLen; i++) {
046: buffer[i] = (byte) random.nextInt(0x100);
047: }
048:
049: String className = "java.util.Hashtable";
050: ref = new Reference(className);
051: }
052:
053: /**
054: * test create Reference using a className
055: */
056: public void testConstructor_Simple() {
057: String className = "java.util.Hashtable";
058: Reference reference = new Reference(className);
059:
060: assertEquals(className, reference.getClassName());
061: assertNull(reference.getFactoryClassName());
062: assertNull(reference.getFactoryClassLocation());
063: assertEquals(0, reference.size());
064: }
065:
066: public void testConstructor_SimpleNull() {
067: Reference reference = new Reference(null);
068:
069: assertNull(reference.getClassName());
070: assertEquals(0, reference.size());
071: }
072:
073: public void testConstructor_ByRefAddr() {
074: String className = "java.util.Hashtable";
075: String type = "Binary";
076: RefAddr refAddr = new BinaryRefAddr(type, buffer);
077: Reference reference = new Reference(className, refAddr);
078:
079: assertEquals(className, reference.getClassName());
080: assertEquals(refAddr, reference.get(0));
081: assertNull(reference.getFactoryClassName());
082: assertNull(reference.getFactoryClassLocation());
083: assertEquals(1, reference.size());
084: }
085:
086: public void testConstructor_ByRefAddrNull() {
087: Reference reference = new Reference(null, null);
088:
089: assertNull(reference.getClassName());
090: assertNull(reference.getFactoryClassName());
091: assertNull(reference.getFactoryClassLocation());
092: assertNull(reference.get(0));
093: assertEquals(1, reference.size());
094: }
095:
096: public void testConstructor_ByFactory() {
097: String className = "java.util.Hashtable";
098: String factoryName = "factory name";
099: String factoryLocation = "file:///home/";
100: Reference reference = new Reference(className, factoryName,
101: factoryLocation);
102:
103: assertEquals(className, reference.getClassName());
104: assertEquals(factoryName, reference.getFactoryClassName());
105: assertEquals(factoryLocation, reference
106: .getFactoryClassLocation());
107: assertEquals(0, reference.size());
108: }
109:
110: public void testConstructor_ByFactoryNull() {
111: Reference reference = new Reference(null, null, null);
112:
113: assertNull(reference.getClassName());
114: assertNull(reference.getFactoryClassName());
115: assertNull(reference.getFactoryClassLocation());
116: assertEquals(0, reference.size());
117: }
118:
119: public void testConstructor_Full() {
120: String className = "java.util.Hashtable";
121: String factoryName = "factory name";
122: String factoryLocation = "file:///home/";
123:
124: String type = "Binary";
125: RefAddr refAddr = new BinaryRefAddr(type, buffer);
126:
127: Reference reference = new Reference(className, refAddr,
128: factoryName, factoryLocation);
129:
130: assertEquals(className, reference.getClassName());
131: assertEquals(factoryName, reference.getFactoryClassName());
132: assertEquals(factoryLocation, reference
133: .getFactoryClassLocation());
134: assertEquals(1, reference.size());
135: assertEquals(refAddr, reference.get(0));
136: }
137:
138: public void testConstructor_FullNull() {
139:
140: Reference reference = new Reference(null, null, null, null);
141:
142: assertNull(reference.getClassName());
143: assertNull(reference.getFactoryClassName());
144: assertNull(reference.getFactoryClassLocation());
145: assertNull(reference.get(0));
146: assertEquals(1, reference.size());
147: }
148:
149: public void testAdd_Simple() {
150: String type = "Binary";
151: BinaryRefAddr refAddr0 = new BinaryRefAddr(type, buffer);
152: byte[] buffer1 = { 1, 2, 3, 4 };
153: BinaryRefAddr refAddr1 = new BinaryRefAddr(type, buffer1);
154: ref.add(refAddr0);
155: ref.add(refAddr1);
156:
157: assertEquals(2, ref.size());
158: assertEquals(refAddr0, ref.get(0));
159: assertEquals(refAddr1, ref.get(1));
160: }
161:
162: public void testAdd_SimpleNull() {
163: ref.add(null);
164:
165: assertEquals(1, ref.size());
166: assertNull(ref.get(0));
167: }
168:
169: public void testAdd_ByIndex() {
170: String type = "Binary";
171: BinaryRefAddr refAddr0 = new BinaryRefAddr(type, buffer);
172: byte[] buffer1 = { 1, 2, 3, 4 };
173: BinaryRefAddr refAddr1 = new BinaryRefAddr(type, buffer1);
174: ref.add(0, refAddr0);
175: ref.add(1, refAddr1);
176:
177: assertEquals(2, ref.size());
178: assertEquals(refAddr0, ref.get(0));
179: assertEquals(refAddr1, ref.get(1));
180: }
181:
182: public void testAdd_ByIndexInsert() {
183: String type = "Binary";
184: BinaryRefAddr refAddr0 = new BinaryRefAddr(type, buffer);
185: byte[] buffer1 = { 1, 2, 3, 4 };
186: BinaryRefAddr refAddr1 = new BinaryRefAddr(type, buffer1);
187: byte[] buffer2 = { 1, 2, 3, 4, 5 };
188: BinaryRefAddr refAddr2 = new BinaryRefAddr(type, buffer2);
189:
190: ref.add(0, refAddr0);
191: ref.add(1, refAddr1);
192: ref.add(1, refAddr2);
193:
194: assertEquals(3, ref.size());
195: assertEquals(refAddr0, ref.get(0));
196: assertEquals(refAddr2, ref.get(1));
197: }
198:
199: public void testAdd_ByIndexInvalidGreat() {
200: String type = "Binary";
201: BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
202: try {
203: ref.add(1, refAddr);
204: fail("This should throw a ArrayIndexOutOfBoundsException");
205: } catch (ArrayIndexOutOfBoundsException e) {
206: }
207: }
208:
209: public void testAdd_ByIndexInvalidLess() {
210: String type = "Binary";
211: BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
212: try {
213: ref.add(-1, refAddr);
214: fail("This should throw a ArrayIndexOutOfBoundsException");
215: } catch (ArrayIndexOutOfBoundsException e) {
216: }
217: }
218:
219: public void testGet_SimpleInvalidGreat() {
220: String type = "Binary";
221: BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
222: ref.add(refAddr);
223:
224: try {
225: ref.get(ref.size());
226: fail("This should throw a ArrayIndexOutOfBoundsException");
227: } catch (ArrayIndexOutOfBoundsException e) {
228: }
229: }
230:
231: public void testGet_SimpleInvalidLess() {
232: String type = "Binary";
233: BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
234: ref.add(refAddr);
235:
236: try {
237: ref.get(-1);
238: fail("This should throw a ArrayIndexOutOfBoundsException");
239: } catch (ArrayIndexOutOfBoundsException e) {
240: }
241: }
242:
243: public void testGet_ByType() {
244: String[] types = { "Binary", "String", };
245:
246: byte[][] buffers = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, };
247:
248: BinaryRefAddr[] refAddrs = new BinaryRefAddr[types.length];
249:
250: for (int i = 0; i < types.length; i++) {
251: refAddrs[i] = new BinaryRefAddr(types[i], buffers[i]);
252: ref.add(refAddrs[i]);
253: }
254:
255: for (int i = 0; i < types.length; i++) {
256: assertEquals(refAddrs[i], ref.get(types[i]));
257: }
258: }
259:
260: public void testGet_ByTypeNotExist() {
261: String type = "Binary";
262: BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
263: ref.add(refAddr);
264:
265: assertNull(ref.get("String"));
266: }
267:
268: public void testGet_TypeNull() {
269: String type = "Binary";
270: BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
271: ref.add(refAddr);
272: try {
273: ref.get(null);
274: fail("Should throw NullPointerException.");
275: } catch (NullPointerException e) {
276: }
277: }
278:
279: public void testGetAll_Simple() {
280: String type = "Binary";
281: BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
282: ref.add(refAddr);
283:
284: Enumeration<?> allAddrs = ref.getAll();
285: assertTrue(allAddrs.hasMoreElements());
286: assertEquals(refAddr, allAddrs.nextElement());
287: }
288:
289: public void testGetAll_Empty() {
290: Enumeration<?> allAddrs = ref.getAll();
291: assertFalse(allAddrs.hasMoreElements());
292: }
293:
294: public void testRemove_Simple() {
295: String type = "Binary";
296: BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
297: ref.add(refAddr);
298:
299: assertEquals(1, ref.size());
300:
301: assertEquals(ref.remove(0), refAddr);
302:
303: assertEquals(0, ref.size());
304: }
305:
306: public void testRemove_Invalid() {
307: try {
308: ref.remove(0);
309: fail("This should throw a ArrayIndexOutOfBoundsException");
310: } catch (ArrayIndexOutOfBoundsException e) {
311: }
312: }
313:
314: public void testClear_Simple() {
315: String type = "Binary";
316: BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
317: int count = 10;
318: for (int i = 0; i < count; i++) {
319: ref.add(refAddr);
320: }
321: assertEquals(count, ref.size());
322: ref.clear();
323: assertEquals(0, ref.size());
324: }
325:
326: public void testClear_Empty() {
327: ref.clear();
328: assertEquals(0, ref.size());
329: }
330:
331: public void testEquals_Simple() {
332: String className = "java.lang.String";
333: String classFactory = "class factory";
334: String location = "/home/neuser";
335:
336: Reference reference0 = new Reference(className, classFactory,
337: location);
338: Reference reference1 = new Reference(className, classFactory,
339: location);
340: assertTrue(reference0.equals(reference1));
341: assertTrue(reference0.equals(reference0));
342: assertTrue(reference1.equals(reference0));
343: assertFalse(reference0.equals(null));
344: }
345:
346: public void testEquals_SimpleWithStrAddr() {
347: String className = "java.lang.String";
348: String classFactory = "class factory";
349: String location = "/home/neuser";
350: StringRefAddr addr = new StringRefAddr("String address",
351: "this is a string");
352: Reference reference0 = new Reference(className, addr,
353: classFactory, location);
354: Reference reference1 = new Reference(className, addr,
355: classFactory, location);
356: assertTrue(reference0.equals(reference1));
357: assertTrue(reference0.equals(reference0));
358: assertTrue(reference1.equals(reference0));
359: assertFalse(reference0.equals(null));
360: }
361:
362: public void testEquals_IgnoreFactory() {
363: String className = "java.lang.String";
364: String classFactory = "class factory";
365: String location = "/home/neuser";
366: StringRefAddr addr = new StringRefAddr("String address",
367: "this is a string");
368: Reference reference0 = new Reference(className, addr,
369: classFactory, location);
370: Reference reference1 = new Reference(className, addr, "",
371: location);
372: assertTrue(reference0.equals(reference1));
373: }
374:
375: public void testEquals_IgnoreFactoryLocation() {
376: String className = "java.lang.String";
377: String classFactory = "class factory";
378: String location = "/home/neuser";
379: StringRefAddr addr = new StringRefAddr("String address",
380: "this is a string");
381: Reference reference0 = new Reference(className, addr,
382: classFactory, location);
383: Reference reference1 = new Reference(className, addr,
384: classFactory, "");
385: assertTrue(reference0.equals(reference1));
386: }
387:
388: public void testEquals_NotEquals1() {
389: String className = "java.lang.String";
390: String classFactory = "class factory";
391: String location = "/home/neuser";
392: StringRefAddr addr = new StringRefAddr("String address",
393: "this is a string");
394: Reference reference0 = new Reference(className, addr,
395: classFactory, location);
396: Reference reference1 = new Reference("java.lang.StringBuffer",
397: addr, classFactory, location);
398: assertFalse(reference0.equals(reference1));
399: }
400:
401: public void testEquals_NotEquals2() {
402: String className = "java.lang.String";
403: String classFactory = "class factory";
404: String location = "/home/neuser";
405: StringRefAddr addr = new StringRefAddr("String address",
406: "this is a string");
407: BinaryRefAddr addr1 = new BinaryRefAddr("Binary address",
408: new byte[] { 1, 2, 3, 4, 5 });
409: Reference reference0 = new Reference(className, addr,
410: classFactory, location);
411: Reference reference1 = new Reference(className, addr1,
412: classFactory, location);
413: assertFalse(reference0.equals(reference1));
414: }
415:
416: public void testEquals_NotInstance() {
417: String className = "java.lang.String";
418: String classFactory = "class factory";
419: String location = "/home/neuser";
420:
421: Reference reference0 = new Reference(className, classFactory,
422: location);
423: assertFalse(reference0.equals("reference"));
424: }
425:
426: public void testEquals_NullClassName() {
427: String classFactory = "class factory";
428: String location = "/home/neuser";
429:
430: Reference reference0 = new Reference(null, classFactory,
431: location);
432: Reference reference1 = new Reference(null, classFactory,
433: location);
434:
435: try {
436: reference0.equals(reference1);
437: fail("Should throw NullPointerException.");
438: } catch (NullPointerException e) {
439: }
440: }
441:
442: public void testEquals_NullClassName2() {
443: String className = "java.lang.String";
444: String classFactory = "class factory";
445: String location = "/home/neuser";
446:
447: Reference reference0 = new Reference(null, classFactory,
448: location);
449: Reference reference2 = new Reference(className, classFactory,
450: location);
451:
452: // try {
453: assertFalse(reference0.equals(reference2));
454: // fail("Should throw NullPointerException.");
455: // } catch (NullPointerException e) {
456: // }
457: }
458:
459: public void testEquals_NullClassName3() {
460: String className = "java.lang.String";
461: String classFactory = "class factory";
462: String location = "/home/neuser";
463:
464: Reference reference0 = new Reference(null, classFactory,
465: location);
466: Reference reference2 = new Reference(className, classFactory,
467: location);
468:
469: try {
470: reference2.equals(reference0);
471: fail("Should throw NullPointerException.");
472: } catch (NullPointerException e) {
473: }
474: }
475:
476: public void testHashcode_Simple() {
477: String className = "java.lang.String";
478: String classFactory = "class factory";
479: String location = "/home/neuser";
480: StringRefAddr addr0 = new StringRefAddr("String address",
481: "this is a string");
482: StringRefAddr addr1 = new StringRefAddr("String address",
483: "this is another string");
484: Reference reference = new Reference(className, addr0,
485: classFactory, location);
486: reference.add(addr1);
487: assertEquals(className.hashCode() + addr0.hashCode()
488: + addr1.hashCode(), reference.hashCode());
489: }
490:
491: public void testHashcode_AddressNull() {
492: String className = "java.lang.String";
493: Reference reference = new Reference(className);
494: assertEquals(className.hashCode(), reference.hashCode());
495: }
496:
497: public void testToString_Simple() {
498: String className = "java.lang.String";
499: String classFactory = "class factory";
500: String location = "/home/neuser";
501: StringRefAddr addr0 = new StringRefAddr("String address",
502: "this is a string");
503: StringRefAddr addr1 = new StringRefAddr("String address",
504: "this is another string");
505: Reference reference = new Reference(className, addr0,
506: classFactory, location);
507: reference.add(addr1);
508:
509: /*
510: * assertEquals( "Reference class name: " + className + "\nReference
511: * addresses:\n\t" + addr0.toString() + "\n\t" + addr1.toString() +
512: * "\n", reference.toString());
513: */
514: assertNotNull(reference.toString());
515: }
516:
517: public void testToString_AddressNull() {
518: String className = "java.lang.String";
519: Reference reference = new Reference(className);
520: /*
521: * assertEquals( "Reference class name: " + className + "\nReference
522: * addresses:\n", reference.toString());
523: */
524: assertNotNull(reference.toString());
525: }
526:
527: public void testClone_Simple() {
528: String className = "java.lang.String";
529: String classFactory = "class factory";
530: String location = "/home/neuser";
531: StringRefAddr addr = new StringRefAddr("String address",
532: "this is a string");
533: Reference reference = new Reference(className, addr,
534: classFactory, location);
535:
536: Reference cloneRef = (Reference) reference.clone();
537: assertEquals(reference, cloneRef);
538: assertNotSame(reference, cloneRef);
539: }
540:
541: public void testClone_AddressNull() {
542: String className = "java.lang.String";
543: Reference reference = new Reference(className);
544:
545: Reference cloneRef = (Reference) reference.clone();
546: assertEquals(reference, cloneRef);
547: assertNotSame(reference, cloneRef);
548: }
549:
550: public void testClone_DiffAddress() {
551: String className = "java.lang.String";
552: StringRefAddr addr = new StringRefAddr("string address",
553: "/home/neuser");
554: Reference reference = new Reference(className);
555: reference.add(addr);
556: Reference cloneRef = (Reference) reference.clone();
557: reference.clear();
558: assertFalse(reference.equals(cloneRef));
559: }
560:
561: public void testSerializable_Simple()
562: throws ClassNotFoundException, IOException {
563: Reference reference = new Reference("dazzle.naming.Reference",
564: "dazzle.naming.factory.RefFactory",
565: "http://www.apache.org");
566: StringRefAddr addr = new StringRefAddr("StringRefAddr",
567: "This is a String RefAddr.");
568: BinaryRefAddr addr2 = new BinaryRefAddr("BinaryRefAddr",
569: new byte[] { 'a', 'b', 'c' });
570: reference.add(addr);
571: reference.add(addr2);
572:
573: // write to byte array
574: ByteArrayOutputStream baos = new ByteArrayOutputStream();
575: ObjectOutputStream oos = new ObjectOutputStream(baos);
576: oos.writeObject(reference);
577: byte[] buffer = baos.toByteArray();
578: oos.close();
579: baos.close();
580:
581: // read from byte array
582: ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
583: ObjectInputStream ois = new ObjectInputStream(bais);
584: Reference reference2 = (Reference) ois.readObject();
585: ois.close();
586: bais.close();
587:
588: assertEquals(reference, reference2);
589: }
590:
591: public void testSerializable_compatibility()
592: throws ClassNotFoundException, IOException {
593: ObjectInputStream ois = new ObjectInputStream(getClass()
594: .getClassLoader().getResourceAsStream(
595: "/serialization/javax/naming/Reference.ser"));
596: Reference reference2 = (Reference) ois.readObject();
597: ois.close();
598:
599: Reference reference = new Reference("dazzle.naming.Reference",
600: "dazzle.naming.factory.RefFactory",
601: "http://www.apache.org");
602: StringRefAddr addr = new StringRefAddr("StringRefAddr",
603: "This is a String RefAddr.");
604: BinaryRefAddr addr2 = new BinaryRefAddr("BinaryRefAddr",
605: new byte[] { 'a', 'b', 'c' });
606: reference.add(addr);
607: reference.add(addr2);
608:
609: assertEquals(reference, reference2);
610: }
611: }
|