test/java/util/Collections/EmptyIterator.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5017904 6356890
  27  * @summary Test empty iterators, enumerations, and collections
  28  */
  29 
  30 import java.util.*;
  31 import static java.util.Collections.*;

  32 
  33 public class EmptyIterator {
  34 
  35     void test(String[] args) throws Throwable {
  36         testEmptyCollection(Collections.<Object>emptyList());
  37         testEmptyCollection(Collections.<Object>emptySet());
  38 
  39         testEmptyMap(Collections.<Object, Object>emptyMap());
  40 
  41         Hashtable<Object, Object> emptyTable = new Hashtable<Object, Object>();
  42         testEmptyEnumeration(emptyTable.keys());
  43         testEmptyEnumeration(emptyTable.elements());
  44         testEmptyIterator(emptyTable.keySet().iterator());
  45         testEmptyIterator(emptyTable.values().iterator());
  46         testEmptyIterator(emptyTable.entrySet().iterator());
  47 
  48         testEmptyEnumeration(javax.swing.tree.DefaultMutableTreeNode
  49                              .EMPTY_ENUMERATION);
  50         testEmptyEnumeration(javax.swing.text.SimpleAttributeSet
  51                              .EMPTY.getAttributeNames());



  52 
  53         @SuppressWarnings("unchecked") Iterator<?> x =
  54             new sun.tools.java.MethodSet()
  55             .lookupName(sun.tools.java.Identifier.lookup(""));
  56         testEmptyIterator(x);
  57     }
  58 
  59     <T> void testEmptyEnumeration(final Enumeration<T> e) {
  60         check(e == emptyEnumeration());
  61         check(! e.hasMoreElements());
  62         THROWS(NoSuchElementException.class,
  63                new F(){void f(){ e.nextElement(); }});
  64     }
  65 
  66     <T> void testEmptyIterator(final Iterator<T> it) {
  67         check(it == emptyIterator());
  68         check(! it.hasNext());
  69         THROWS(NoSuchElementException.class,
  70                new F(){void f(){ it.next(); }});
  71         THROWS(IllegalStateException.class,




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5017904 6356890 8004928
  27  * @summary Test empty iterators, enumerations, and collections
  28  */
  29 

  30 import static java.util.Collections.*;
  31 import java.util.*;
  32 
  33 public class EmptyIterator {
  34     
  35     void test(String[] args) throws Throwable {
  36         testEmptyCollection(Collections.<Object>emptyList());
  37         testEmptyCollection(Collections.<Object>emptySet());
  38 
  39         testEmptyMap(Collections.<Object, Object>emptyMap());
  40 
  41         Hashtable<Object, Object> emptyTable = new Hashtable<Object, Object>();
  42         testEmptyEnumeration(emptyTable.keys());
  43         testEmptyEnumeration(emptyTable.elements());
  44         testEmptyIterator(emptyTable.keySet().iterator());
  45         testEmptyIterator(emptyTable.values().iterator());
  46         testEmptyIterator(emptyTable.entrySet().iterator());
  47 
  48         final Enumeration<EmptyIterator> finalEmptyTyped = 
  49             Collections.emptyEnumeration();
  50         testEmptyEnumeration(finalEmptyTyped);
  51         
  52         final Enumeration finalEmptyAbstract =         
  53             Collections.emptyEnumeration();
  54         testEmptyEnumeration(finalEmptyAbstract);
  55 
  56         @SuppressWarnings("unchecked") Iterator<?> x =
  57             new sun.tools.java.MethodSet()
  58             .lookupName(sun.tools.java.Identifier.lookup(""));
  59         testEmptyIterator(x);
  60     }
  61 
  62     <T> void testEmptyEnumeration(final Enumeration<T> e) {
  63         check(e == emptyEnumeration());
  64         check(! e.hasMoreElements());
  65         THROWS(NoSuchElementException.class,
  66                new F(){void f(){ e.nextElement(); }});
  67     }
  68 
  69     <T> void testEmptyIterator(final Iterator<T> it) {
  70         check(it == emptyIterator());
  71         check(! it.hasNext());
  72         THROWS(NoSuchElementException.class,
  73                new F(){void f(){ it.next(); }});
  74         THROWS(IllegalStateException.class,