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.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.util.Enumeration;
024: import java.util.Properties;
025:
026: import javax.naming.CompositeName;
027: import javax.naming.CompoundName;
028: import javax.naming.InvalidNameException;
029: import javax.naming.Name;
030:
031: import junit.framework.TestCase;
032: import org.apache.harmony.jndi.tests.javax.naming.util.Log;
033:
034: /**
035: * unit test for composite name
036: *
037: */
038: public class CompositeNameTest extends TestCase {
039:
040: /*
041: * -------------------------------------------------------------------
042: * Constants
043: * -------------------------------------------------------------------
044: */
045: private static final Log log = new Log(CompositeNameTest.class);
046:
047: private static final char SEPARATOR = '/';
048:
049: /*
050: * -------------------------------------------------------------------
051: * Instance variables (Should be private)
052: * -------------------------------------------------------------------
053: */
054: private CompositeName name;
055:
056: private final String[] elements = { "www.apache.org", "gbank" };
057:
058: private String initName;
059:
060: private Properties props;
061:
062: /**
063: * Constructor for TestCompositeName.
064: *
065: * @param arg0
066: */
067: public CompositeNameTest(String arg0) {
068: super (arg0);
069: StringBuffer sb = new StringBuffer();
070: for (String element : elements) {
071: sb.append(element).append(SEPARATOR);
072: }
073: initName = sb.toString();
074: }
075:
076: /*
077: * -------------------------------------------------------------------
078: * Methods
079: * -------------------------------------------------------------------
080: */
081:
082: @Override
083: protected void setUp() throws Exception {
084: super .setUp();
085: name = new CompositeName(initName);
086: props = new Properties();
087: props.put("jndi.syntax.separator", "/");
088: props.put("jndi.syntax.direction", "left_to_right");
089: props.put("jndi.syntax.escape", "\\");
090: props.put("jndi.syntax.beginquote", "<");
091: props.put("jndi.syntax.endquote", ">");
092: props.put("jndi.syntax.beginquote2", "'");
093: props.put("jndi.syntax.endquote2", "'");
094: props.put("jndi.syntax.ignorecase", "false");
095: props.put("jndi.syntax.trimblanks", "false");
096: }
097:
098: @Override
099: protected void tearDown() throws Exception {
100: super .tearDown();
101: }
102:
103: /**
104: * test constructor with null string parameter
105: *
106: * @throws InvalidNameException
107: */
108: public void TestConstructorNull() throws InvalidNameException {
109: log.setMethod("TestConstructorNull");
110: try {
111: name = new CompositeName((String) null);
112: log
113: .log("fail: try to construct a compositename with null succeed");
114: fail();
115: } catch (NullPointerException e) {
116: }
117: }
118:
119: public void testConstructorSimple() throws InvalidNameException {
120: assertNameEquals(new CompositeName("a/b/c"), new String[] {
121: "a", "b", "c" });
122: }
123:
124: public void testConstructorException() throws InvalidNameException {
125: try {
126: name = new CompositeName("'abc'd/ef");
127: fail();
128: } catch (InvalidNameException e) {
129: }
130: name = new CompositeName("abc'abc'/ab");
131: assertNameEquals(name, new String[] { "abc'abc'", "ab" });
132: }
133:
134: /**
135: * test toString()
136: *
137: * @throws InvalidNameException
138: */
139: public void testToString() throws InvalidNameException {
140: log.setMethod("testToString()");
141: assertEquals("", new CompositeName("").toString());
142: assertEquals("/", new CompositeName("/").toString());
143: assertEquals("//", new CompositeName("//").toString());
144: assertEquals("/a/", new CompositeName("/a/").toString());
145: name.add("abc\"abc");
146: assertEquals(4, name.size());
147: name.add("abc/abc");
148: assertEquals(5, name.size());
149: name.add("abc\\abc");
150: assertEquals(6, name.size());
151: assertEquals(new CompositeName(name.toString()), name);
152: assertNameEquals(name, new String[] { "www.apache.org",
153: "gbank", "", "abc\"abc", "abc/abc", "abc\\abc" });
154: }
155:
156: /**
157: * test getAll()
158: *
159: */
160: public void testGetAll() {
161: log.setMethod("testGetAll()");
162: Enumeration<?> enumeration = name.getAll();
163: for (String element : elements) {
164: assertTrue(element.equals(enumeration.nextElement()));
165: }
166: assertTrue("".equals(enumeration.nextElement()));
167: }
168:
169: /**
170: * test get()
171: *
172: */
173: public void testGet() {
174: log.setMethod("testGet()");
175: for (int j = 0; j < elements.length; j++) {
176: assertEquals(elements[j], name.get(j));
177: }
178: try {
179: name.get(-1);
180: fail();
181: } catch (ArrayIndexOutOfBoundsException e) {
182: }
183: try {
184: name.get(name.size());
185: fail();
186: } catch (ArrayIndexOutOfBoundsException e) {
187: }
188: }
189:
190: /**
191: * test getPrefix()
192: *
193: */
194: public void testGetPrefix() {
195: log.setMethod("testGetPrefix()");
196: Name prefix = name.getPrefix(0);
197: assertEquals("", prefix.toString());
198: try {
199: name.getPrefix(elements.length + 2);
200: fail("name.getPrefix(elements.length + 2)");
201: } catch (IndexOutOfBoundsException e) {
202: }
203: try {
204: name.getPrefix(-1);
205: fail("name.getPrefix(-1)");
206: } catch (IndexOutOfBoundsException e) {
207: }
208: prefix = name.getPrefix(1);
209: assertEquals(elements[0], prefix.toString());
210: }
211:
212: /**
213: * test getSuffix
214: *
215: */
216: public void testGetSuffix() {
217: log.setMethod("testGetSuffix()");
218: Name suffix = name.getSuffix(elements.length + 1);
219: assertEquals("", suffix.toString());
220: try {
221: name.getSuffix(elements.length + 2);
222: fail("name.getSuffix(elements.length + 2)");
223: } catch (ArrayIndexOutOfBoundsException e) {
224: }
225: try {
226: name.getSuffix(-1);
227: fail("name.getSuffix(-1)");
228: } catch (ArrayIndexOutOfBoundsException e) {
229: }
230: suffix = name.getSuffix(2);
231: assertEquals("/", suffix.toString());
232: }
233:
234: /**
235: * Test addAll(Name), include exceptional case
236: */
237: public void testAddAllName() throws InvalidNameException {
238: log.setMethod("testAddAllName()");
239: int oldSize = name.size();
240: name.addAll(new CompositeName(initName));
241: assertEquals(name.size(), oldSize * 2);
242: assertEquals(name.getPrefix(3).toString(), name.getSuffix(3)
243: .toString());
244: assertEquals(name.getPrefix(3).toString(), initName);
245: name = new CompositeName("a");
246: try {
247: name.addAll(new CompoundName("b/c", props));
248: fail("name.addAll(new CompoundName(\"b/c\", props));");
249: } catch (InvalidNameException t) {
250: }
251: try {
252: name.addAll(null);
253: fail("Add null should throw NullPointerException");
254: } catch (NullPointerException e) {
255: }
256: }
257:
258: /**
259: * test addAll(int), include exceptional case
260: */
261: public void testAddAllintName() throws InvalidNameException {
262: log.setMethod("testAddAllintName()");
263: int oldSize = name.size();
264: name.addAll(1, new CompositeName(initName));
265: assertEquals(name.size(), oldSize * 2);
266: assertEquals(name.getSuffix(1).getPrefix(3).toString(),
267: initName);
268: name = new CompositeName("a");
269: try {
270: name.addAll(0, new CompoundName("b/c", props));
271: fail("name.addAll(0, new CompoundName(\"b/c\", props));");
272: } catch (InvalidNameException t) {
273: }
274: try {
275: name.addAll(0, null);
276: fail("Add null should throw NullPointerException");
277: } catch (NullPointerException e) {
278: }
279: try {
280: name.addAll(-1, new CompositeName(initName));
281: fail("-1 should be out of bound.");
282: } catch (ArrayIndexOutOfBoundsException e) {
283: }
284: try {
285: name.addAll(name.size() + 1, new CompositeName(initName));
286: fail((name.size() + 1) + " should out of bound.");
287: } catch (ArrayIndexOutOfBoundsException e) {
288: }
289: }
290:
291: /**
292: * test add(String), include exceptional case of null parameter
293: */
294: public void testAddString() throws InvalidNameException {
295: log.setMethod("testAddString()");
296: int oldSize = name.size();
297: name.add(elements[0]);
298: assertEquals(name.size(), oldSize + 1);
299: assertEquals(elements[0], name.getSuffix(3).toString());
300: name.add(null);
301: assertEquals(name.size(), oldSize + 2);
302: try {
303: assertNull(name.getSuffix(4).toString());
304: fail();
305: } catch (NullPointerException e) {
306: }
307:
308: }
309:
310: /**
311: * Test add(int, String), include boundary case
312: */
313: public void testAddintString() throws InvalidNameException {
314: log.setMethod("testAddintString()");
315: int oldSize = name.size();
316: name.add(1, elements[0]);
317: assertEquals(name.size(), oldSize + 1);
318: assertEquals(name.getSuffix(1).toString(), initName);
319: try {
320: name.add(oldSize + 2, elements[0]);
321: fail();
322: } catch (ArrayIndexOutOfBoundsException e) {
323: }
324: try {
325: name.add(-1, elements[0]);
326: fail();
327: } catch (ArrayIndexOutOfBoundsException e) {
328: }
329: }
330:
331: /**
332: * test remove, include boundary case
333: *
334: * @throws InvalidNameException
335: */
336: public void testRemove() throws InvalidNameException {
337: log.setMethod("testRemove()");
338: int oldSize = name.size();
339: name.remove(1);
340: assertEquals(name.size(), oldSize - 1);
341: assertEquals(name.toString(), elements[0] + "/");
342: try {
343: name.remove(-1);
344: fail();
345: } catch (ArrayIndexOutOfBoundsException e) {
346: }
347: try {
348: name.remove(oldSize - 1);
349: fail();
350: } catch (ArrayIndexOutOfBoundsException e) {
351: }
352:
353: }
354:
355: /**
356: * test size()
357: *
358: * @throws InvalidNameException
359: */
360: public void testSize() throws InvalidNameException {
361: log.setMethod("testSize()");
362: assertEquals(elements.length + 1, name.size());
363: name = new CompositeName("/");
364: assertEquals(1, name.size());
365: }
366:
367: /**
368: * test isEmpty()
369: *
370: * @throws InvalidNameException
371: */
372: @SuppressWarnings("unused")
373: public void testIsEmpty() throws InvalidNameException {
374: log.setMethod("testIsEmpty()");
375: assertFalse(name.isEmpty());
376: for (String element : elements) {
377: name.remove(0);
378: }
379: name.remove(0);
380: assertTrue(name.isEmpty());
381: name = new CompositeName("");
382: assertTrue(name.isEmpty());
383:
384: }
385:
386: /**
387: * test startWith(), include exceptional case of null and CompoundName
388: *
389: * @throws InvalidNameException
390: */
391: public void testStartsWith() throws InvalidNameException {
392: log.setMethod("testStartsWith()");
393: CompositeName start = new CompositeName(elements[0]);
394: assertTrue(name.startsWith(start));
395: start = new CompositeName(elements[1]);
396: assertFalse(name.startsWith(start));
397: try {
398: assertFalse(name.startsWith(null));
399: } catch (Throwable e) {
400: log.log("start with null?", e);
401: }
402: try {
403: assertFalse(name.startsWith(new CompoundName(elements[0],
404: props)));
405: } catch (Throwable e) {
406: log.log("start with compoundName?", e);
407: }
408: }
409:
410: /**
411: * test endsWith(), include exceptional case of null and CompoundName
412: *
413: * @throws InvalidNameException
414: */
415: public void testEndsWith() throws InvalidNameException {
416: log.setMethod("testEndsWith()");
417: CompositeName end = new CompositeName("");
418: assertTrue(name.endsWith(end));
419: end = new CompositeName("12345");
420: assertFalse(name.endsWith(end));
421: try {
422: name.endsWith(null);
423: } catch (Throwable e) {
424: log.log("end with null?", e);
425: }
426: try {
427: assertFalse(name.endsWith(new CompoundName("", props)));
428: } catch (Throwable e) {
429: log.log("end with compoundName?", e);
430: }
431: }
432:
433: /**
434: * Special characters are as follows: The separator is / The escape
435: * character is \ Quotes can be used - both single quotes and double quotes
436: * are allowed. This allows you to quote strings which contain chars such as /
437: * which are part of a CompositeName element to avoid them being read as a
438: * separator.
439: *
440: * @throws InvalidNameException
441: */
442: public void testSpecialCharacter() throws InvalidNameException {
443: log.setMethod("testSpecialCharacter()");
444: // The name "a//a" has 3 elements. The middle element is empty and the
445: // first & third elements are both "a".
446: name = new CompositeName("a//a");
447: Enumeration<?> enumeration = name.getAll();
448: assertEquals("a", enumeration.nextElement());
449: assertEquals("", enumeration.nextElement());
450: assertEquals("a", enumeration.nextElement());
451:
452: // The name "a/'b/c'/a" has 3 elements. The middle element is b/c.
453: name = new CompositeName("a/'b/c'/a");
454: enumeration = name.getAll();
455: assertEquals("a", enumeration.nextElement());
456: assertEquals("b/c", enumeration.nextElement());
457: assertEquals("a", enumeration.nextElement());
458:
459: name = new CompositeName("a/a'b/c'c/a");
460: enumeration = name.getAll();
461: assertEquals("a", enumeration.nextElement());
462: assertEquals("a'b", enumeration.nextElement());
463: assertEquals("c'c", enumeration.nextElement());
464: assertEquals("a", enumeration.nextElement());
465: name = new CompositeName("a/a'b/c'/a/\\abc/ab\\\"c");
466: enumeration = name.getAll();
467: assertEquals("a", enumeration.nextElement());
468: assertEquals("a'b", enumeration.nextElement());
469: assertEquals("c'", enumeration.nextElement());
470: assertEquals("a", enumeration.nextElement());
471: assertEquals("\\abc", enumeration.nextElement());
472: assertEquals("ab\"c", enumeration.nextElement());
473:
474: name = new CompositeName("\"ab/c\"/ab");
475: assertNameEquals(name, new String[] { "ab/c", "ab" });
476:
477: // The name "a/'b/a" is invalid as there is no closing quote for the '
478: // character.
479: try {
480: name = new CompositeName("a/'b/a");
481: fail("a/'b/a");
482: } catch (InvalidNameException e) {
483: }
484:
485: // The name "a/\"b/a" is interpreted as a/"b/a and is invalid as there
486: // is no closing quote for the embedded escaped " character.
487: try {
488: name = new CompositeName("a/\"'b/a");
489: fail("a/\"'b/a");
490: } catch (InvalidNameException e) {
491: }
492: try {
493: name = new CompositeName("a/bcd/a\\");
494: fail("a/bcd/a\\");
495: } catch (InvalidNameException e) {
496: }
497: }
498:
499: /**
500: * test equals, include exceptional case, i.e, CompoundName, null, etc.
501: *
502: * @throws InvalidNameException
503: */
504: public void testEquals() throws InvalidNameException {
505: log.setMethod("testEquals()");
506: Name name2 = null;
507: name2 = new CompositeName(initName);
508: assertTrue(name.equals(name2));
509: assertTrue(name2.equals(name));
510: name2.add("abc");
511: assertFalse(name2.equals(name));
512: name2 = new CompoundName("abc", props);
513: name = new CompositeName("abc");
514: assertFalse(name.equals(name2));
515: assertFalse(name.equals(null));
516: assertTrue(name.equals(name));
517: }
518:
519: /**
520: * test compareTo(), include exceptional case, i.e, CompoundName, null, etc.
521: *
522: * @throws InvalidNameException
523: */
524: public void testCompareTo() throws InvalidNameException {
525: log.setMethod("testCompareTo()");
526: Name name1 = new CompositeName("a/b/c/d");
527: Name name2 = new CompositeName("ab/b/c/d");
528: Name name3 = new CompositeName("a/b/c/d/e");
529: Name name4 = new CompositeName("b/b/c/d");
530: assertEquals(-1, name1.compareTo(name2));
531: assertEquals(1, name3.compareTo(name1));
532: assertEquals(-1, name1.compareTo(name4));
533: assertEquals(name1.compareTo(name1), 0);
534: try {
535: name1.compareTo(null);
536: fail();
537: } catch (ClassCastException e) {
538: }
539: name2 = new CompoundName("a/b/c/d", props);
540: try {
541: name1.compareTo(name2);
542: fail();
543: } catch (ClassCastException e) {
544: }
545: }
546:
547: /**
548: * test hashCode()
549: *
550: * @throws InvalidNameException
551: */
552: public void testHashCode() throws InvalidNameException {
553: log.setMethod("testHashCode()");
554: Name name2 = new CompositeName(initName);
555: assertEquals(name.hashCode(), name2.hashCode());
556: assertEquals(767782430, name2.hashCode());
557: name2 = new CompositeName("a");
558: assertEquals(97, name2.hashCode());
559: }
560:
561: /**
562: * test serialization
563: *
564: * @throws Exception
565: */
566: public void testWriteReadObject() throws Exception {
567: log.setMethod("testWriteReadObject()");
568: CompositeName name = new CompositeName("a/b/c/d");
569: ByteArrayOutputStream bout = new ByteArrayOutputStream();
570: ObjectOutputStream stream = new ObjectOutputStream(bout);
571: stream.writeObject(name);
572: stream.close();
573: ObjectInputStream in = new ObjectInputStream(
574: new ByteArrayInputStream(bout.toByteArray()));
575: CompositeName name2 = (CompositeName) in.readObject();
576: assertTrue(name.equals(name2));
577: in.close();
578: }
579:
580: /**
581: * utility method for name assertion
582: *
583: * @param n
584: * @param elems
585: */
586: private void assertNameEquals(Name n, String[] elems) {
587:
588: try {
589: // compare
590: assertEquals(elems.length, n.size());
591:
592: for (int i = 0; i < n.size(); i++) {
593: assertEquals(elems[i], n.get(i));
594: }
595:
596: int i = 0;
597: Enumeration<?> enumeration = n.getAll();
598: while (enumeration.hasMoreElements()) {
599: assertEquals(elems[i++], enumeration.nextElement());
600: }
601: } catch (RuntimeException e) {
602: // log
603: StringBuffer buf = new StringBuffer();
604: buf.append("Assert name ");
605: buf.append(n.toString());
606: buf.append(" has elements [" + elems.length + "]{");
607: for (int i = 0; i < elems.length; i++) {
608: if (i > 0) {
609: buf.append(",");
610: }
611: buf.append(elems[i]);
612: }
613: buf.append("}");
614: log.log(buf.toString());
615:
616: throw e;
617: }
618: }
619:
620: /**
621: * test serialization compatibility
622: *
623: * @throws Exception
624: */
625: public void testSerializationCompatibility() throws Exception {
626: log.setMethod("testSerializationCompatibility()");
627:
628: try {
629: ObjectInputStream in = new ObjectInputStream(
630: getClass()
631: .getResourceAsStream(
632: "/serialization/javax/naming/CompositeName.ser"));
633: CompositeName name = (CompositeName) in.readObject();
634: assertEquals(new CompositeName("a/b/c/d"), name);
635: in.close();
636: } catch (Exception e) {
637: log.log(e);
638: throw e;
639: }
640: }
641:
642: /**
643: * test clone
644: *
645: */
646: public void testClone() throws InvalidNameException {
647: CompositeName name = new CompositeName("a/b/c/d");
648: CompositeName name2 = (CompositeName) name.clone();
649: assertEquals(name, name2);
650: name.add(1, elements[0]);
651: assertFalse(name.equals(name2));
652: }
653:
654: public void testConstructorEnum() {
655: log.setMethod("testConstructorEnum");
656: CompositeName name2 = new MockCompositeName(name.getAll());
657: assertEquals(name2, name);
658: try {
659: name2 = new MockCompositeName((Enumeration<String>) null);
660: fail();
661: } catch (NullPointerException e) {
662: }
663: }
664:
665: // mock class to test protected methods
666: public class MockCompositeName extends CompositeName {
667: private static final long serialVersionUID = 1L;
668:
669: public MockCompositeName(Enumeration<String> enumeration) {
670: super(enumeration);
671: }
672: }
673: }
|