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:
018: package org.apache.harmony.jndi.provider.ldap;
019:
020: import java.util.ArrayList;
021: import java.util.NoSuchElementException;
022:
023: import javax.naming.NamingException;
024:
025: import junit.framework.TestCase;
026:
027: public class LdapNamingEnumerationTest extends TestCase {
028: private LdapNamingEnumeration<Object> enu;
029:
030: public void test_constructor() {
031: enu = new LdapNamingEnumeration<Object>(null, null);
032: }
033:
034: public void test_Enumeration() throws Exception {
035: ArrayList<Object> list = new ArrayList<Object>();
036: enu = new LdapNamingEnumeration<Object>(list, null);
037: assertFalse(enu.hasMore());
038:
039: list = new ArrayList<Object>();
040: list.add(new Object());
041: list.add(new Object());
042:
043: enu = new LdapNamingEnumeration<Object>(list, null);
044: assertTrue(enu.hasMore());
045: assertNotNull(enu.next());
046: assertTrue(enu.hasMore());
047: assertNotNull(enu.next());
048: assertFalse(enu.hasMore());
049: try {
050: enu.next();
051: fail("Should throws NoSuchElementException");
052: } catch (NoSuchElementException e) {
053: // expected
054: }
055:
056: NamingException ex = new NamingException();
057: enu = new LdapNamingEnumeration<Object>(list, ex);
058: assertTrue(enu.hasMore());
059: assertNotNull(enu.next());
060: assertTrue(enu.hasMore());
061: assertNotNull(enu.next());
062: try {
063: enu.hasMore();
064: fail("Should throws NamingException");
065: } catch (NamingException e) {
066: // expected
067: assertEquals(ex, e);
068: }
069: try {
070: enu.next();
071: fail("Should throws NoSuchElementException");
072: } catch (NoSuchElementException e) {
073: // expected
074: }
075: }
076:
077: /**
078: * uses hasMoreElements() and nextElement() to iterate
079: *
080: * @throws Exception
081: */
082: public void test_Enumeration_01() throws Exception {
083: ArrayList<Object> list = new ArrayList<Object>();
084: enu = new LdapNamingEnumeration<Object>(list, null);
085: assertFalse(enu.hasMoreElements());
086: try {
087: enu.nextElement();
088: fail("Should throws NoSuchElementException");
089: } catch (NoSuchElementException e) {
090: // expected;
091: }
092:
093: list = new ArrayList<Object>();
094: list.add(new Object());
095: list.add(new Object());
096:
097: enu = new LdapNamingEnumeration<Object>(list, null);
098: assertTrue(enu.hasMoreElements());
099: assertNotNull(enu.nextElement());
100: assertTrue(enu.hasMoreElements());
101: assertNotNull(enu.nextElement());
102: assertFalse(enu.hasMoreElements());
103: try {
104: enu.nextElement();
105: fail("Should throws NoSuchElementException");
106: } catch (NoSuchElementException e) {
107: // expected
108: }
109:
110: NamingException ex = new NamingException();
111: enu = new LdapNamingEnumeration<Object>(list, ex);
112: assertTrue(enu.hasMoreElements());
113: assertNotNull(enu.nextElement());
114: assertTrue(enu.hasMoreElements());
115: assertNotNull(enu.nextElement());
116: assertFalse(enu.hasMoreElements());
117: try {
118: enu.nextElement();
119: fail("Should throws NoSuchElementException");
120: } catch (NoSuchElementException e) {
121: // expected
122: }
123: }
124:
125: public void test_close() throws Exception {
126: ArrayList<Object> list = new ArrayList<Object>();
127: list.add(new Object());
128: list.add(new Object());
129: enu = new LdapNamingEnumeration<Object>(list, null);
130: assertTrue(enu.hasMore());
131: enu.close();
132: assertFalse(enu.hasMore());
133: assertFalse(enu.hasMoreElements());
134:
135: try {
136: enu.next();
137: fail("Should throws NoSuchElementException");
138: } catch (NoSuchElementException e) {
139: // expected
140: }
141:
142: try {
143: enu.nextElement();
144: fail("Should throws NoSuchElementException");
145: } catch (NoSuchElementException e) {
146: // expected
147: }
148: }
149: }
|