jdk/test/java/util/Collections/EmptySortedSet.java
author mduigou
Thu, 03 Nov 2011 13:26:57 -0700
changeset 10896 8b5d56140b70
permissions -rw-r--r--
4533691: Add Collections.emptySortedSet() Reviewed-by: mduigou, alanb, dholmes Contributed-by: darryl.mocek@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10896
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
     1
/*
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
     4
 *
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
     8
 *
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    13
 * accompanied this code).
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    14
 *
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    18
 *
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    21
 * questions.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    22
 */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    23
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    24
/*
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    25
 * @test
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    26
 * @bug 4533691
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    27
 * @summary Unit test for Collections.emptySortedSet
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    28
 */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    29
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    30
import java.lang.reflect.Method;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    31
import java.math.BigInteger;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    32
import java.util.Collections;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    33
import java.util.Comparator;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    34
import java.util.Iterator;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    35
import java.util.NoSuchElementException;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    36
import java.util.SortedSet;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    37
import java.util.TreeSet;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    38
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    39
public class EmptySortedSet {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    40
    static int status = 0;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    41
    private static final String FAILED = " failed. ";
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    42
    private static final String PERIOD = ".";
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    43
    private final String thisClassName = this.getClass().getName();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    44
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    46
        new EmptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    47
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    48
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    49
    public EmptySortedSet() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    50
        run();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    51
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    52
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    53
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    54
     * Returns {@code true} if the {@link Object} passed in is an empty
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    55
     * {@link SortedSet}.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    56
     *
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    57
     * @param obj the object to test
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    58
     * @return {@code true} if the {@link Object} is an empty {@link SortedSet}
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    59
     *         otherwise {@code false}.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    60
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    61
    private boolean isEmptySortedSet(Object obj) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    62
        boolean isEmptySortedSet = false;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    63
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    64
        // We determine if the object is an empty sorted set by testing if it's
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    65
        // an instance of SortedSet, and if so, if it's empty.  Currently the
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    66
        // testing doesn't include checks of the other methods.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    67
        if (obj instanceof SortedSet) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    68
            SortedSet ss = (SortedSet) obj;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    69
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    70
            if ((ss.isEmpty()) && (ss.size() == 0)) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    71
                isEmptySortedSet = true;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    72
            }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    73
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    74
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    75
        return isEmptySortedSet;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    76
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    77
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    78
    private void run() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    79
        Method[] methods = this.getClass().getDeclaredMethods();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    80
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    81
        for (int i = 0; i < methods.length; i++) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    82
            Method method = methods[i];
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    83
            String methodName = method.getName();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    84
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    85
            if (methodName.startsWith("test")) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    86
                try {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    87
                    Object obj = method.invoke(this, new Object[0]);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    88
                } catch(Exception e) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    89
                    throw new Exception(this.getClass().getName() + "." +
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    90
                            methodName + " test failed, test exception "
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    91
                            + "follows\n" + e.getCause());
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    92
                }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    93
            }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    94
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    95
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    96
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    97
    private void throwException(String methodName, String reason)
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    98
            throws Exception
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
    99
    {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   100
        StringBuilder sb = new StringBuilder(thisClassName);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   101
        sb.append(PERIOD);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   102
        sb.append(methodName);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   103
        sb.append(FAILED);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   104
        sb.append(reason);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   105
        throw new Exception(sb.toString());
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   106
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   107
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   108
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   109
     *
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   110
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   111
    private void test00() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   112
        //throwException("test00", "This test has not been implemented yet.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   113
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   114
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   115
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   116
     * Tests that the comparator is {@code null}.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   117
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   118
    private void testComparatorIsNull() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   119
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   120
        Comparator comparator = sortedSet.comparator();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   121
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   122
        if (comparator != null) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   123
            throwException("testComparatorIsNull", "Comparator is not null.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   124
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   125
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   126
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   127
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   128
     * Tests that the contains method returns {@code false}.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   129
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   130
    private void testContains() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   131
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   132
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   133
        if (sortedSet.contains(new Object())) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   134
            throwException("testContains", "Should not contain any elements.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   135
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   136
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   137
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   138
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   139
     * Tests that the containsAll method returns {@code false}.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   140
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   141
    private void testContainsAll() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   142
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   143
        TreeSet treeSet = new TreeSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   144
        treeSet.add("1");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   145
        treeSet.add("2");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   146
        treeSet.add("3");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   147
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   148
        if (sortedSet.containsAll(treeSet)) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   149
            throwException("testContainsAll",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   150
                    "Should not contain any elements.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   151
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   152
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   153
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   154
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   155
     * Tests that the iterator is empty.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   156
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   157
    private void testEmptyIterator() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   158
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   159
        Iterator emptyIterator = sortedSet.iterator();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   160
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   161
        if ((emptyIterator != null) && (emptyIterator.hasNext())) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   162
            throwException("testEmptyIterator", "The iterator is not empty.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   163
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   164
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   165
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   166
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   167
     * Tests that the set is empty.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   168
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   169
    private void testIsEmpty() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   170
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   171
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   172
        if ((sortedSet != null) && (!sortedSet.isEmpty())) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   173
            throwException("testSizeIsZero", "The set is not empty.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   174
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   175
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   176
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   177
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   178
     * Tests that the first() method throws NoSuchElementException
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   179
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   180
    private void testFirst() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   181
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   182
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   183
        try {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   184
            sortedSet.first();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   185
            throwException("testFirst",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   186
                    "NoSuchElemenException was not thrown.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   187
        } catch(NoSuchElementException nsee) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   188
            // Do nothing
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   189
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   190
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   191
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   192
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   193
     * Tests the headSet() method.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   194
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   195
    private void testHeadSet() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   196
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   197
        SortedSet ss;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   198
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   199
        try {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   200
            ss = sortedSet.headSet(null);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   201
            throwException("testHeadSet",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   202
                    "Must throw NullPointerException for null element");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   203
        } catch(NullPointerException npe) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   204
            // Do nothing
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   205
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   206
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   207
        try {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   208
            ss = sortedSet.headSet(new Object());
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   209
            throwException("testHeadSet",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   210
                    "Must throw ClassCastException for non-Comparable element");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   211
        } catch(ClassCastException cce) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   212
            // Do nothing.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   213
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   214
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   215
        ss = sortedSet.headSet("1");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   216
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   217
        if ((ss == null) || !isEmptySortedSet(ss)) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   218
            throwException("testHeadSet",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   219
                    "Returned value is null or not an EmptySortedSet.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   220
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   221
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   222
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   223
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   224
     * Tests that the last() method throws NoSuchElementException
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   225
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   226
    private void testLast() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   227
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   228
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   229
        try {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   230
            sortedSet.last();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   231
            throwException("testLast",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   232
                    "NoSuchElemenException was not thrown.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   233
        } catch(NoSuchElementException nsee) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   234
            // Do nothing
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   235
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   236
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   237
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   238
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   239
     * Tests that the size is 0.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   240
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   241
    private void testSizeIsZero() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   242
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   243
        int size = sortedSet.size();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   244
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   245
        if (size > 0) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   246
            throwException("testSizeIsZero",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   247
                    "The size of the set is greater then 0.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   248
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   249
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   250
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   251
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   252
     * Tests the subSet() method.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   253
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   254
    private void testSubSet() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   255
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   256
        SortedSet ss = sortedSet.headSet("1");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   257
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   258
        try {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   259
            ss = sortedSet.subSet(null, BigInteger.TEN);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   260
            ss = sortedSet.subSet(BigInteger.ZERO, null);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   261
            ss = sortedSet.subSet(null, null);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   262
            throwException("testSubSet",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   263
                    "Must throw NullPointerException for null element");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   264
        } catch(NullPointerException npe) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   265
            // Do nothing
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   266
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   267
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   268
        try {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   269
            Object obj1 = new Object();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   270
            Object obj2 = new Object();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   271
            ss = sortedSet.subSet(obj1, BigInteger.TEN);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   272
            ss = sortedSet.subSet(BigInteger.ZERO, obj2);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   273
            ss = sortedSet.subSet(obj1, obj2);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   274
            throwException("testSubSet",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   275
                    "Must throw ClassCastException for parameter which is "
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   276
                    + "not Comparable.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   277
        } catch(ClassCastException cce) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   278
            // Do nothing.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   279
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   280
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   281
        try {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   282
            ss = sortedSet.subSet(BigInteger.ZERO, BigInteger.ZERO);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   283
            ss = sortedSet.subSet(BigInteger.TEN, BigInteger.ZERO);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   284
            throwException("testSubSet",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   285
                    "Must throw IllegalArgumentException when fromElement is "
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   286
                    + "not less then then toElement.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   287
        } catch(IllegalArgumentException iae) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   288
            // Do nothing.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   289
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   290
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   291
        ss = sortedSet.subSet(BigInteger.ZERO, BigInteger.TEN);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   292
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   293
        if (!isEmptySortedSet(ss)) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   294
            throw new Exception("Returned value is not empty sorted set.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   295
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   296
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   297
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   298
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   299
     * Tests the tailSet() method.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   300
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   301
    private void testTailSet() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   302
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   303
        SortedSet ss;
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   304
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   305
        try {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   306
            ss = sortedSet.tailSet(null);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   307
            throwException("testTailSet",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   308
                    "Must throw NullPointerException for null element");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   309
        } catch(NullPointerException npe) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   310
            // Do nothing
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   311
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   312
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   313
        try {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   314
            SortedSet ss2 = sortedSet.tailSet(new Object());
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   315
            throwException("testTailSet",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   316
                    "Must throw ClassCastException for non-Comparable element");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   317
        } catch(ClassCastException cce) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   318
            // Do nothing.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   319
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   320
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   321
        ss = sortedSet.tailSet("1");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   322
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   323
        if ((ss == null) || !isEmptySortedSet(ss)) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   324
            throwException("testTailSet",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   325
                    "Returned value is null or not an EmptySortedSet.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   326
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   327
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   328
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   329
    /**
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   330
     * Tests that the array has a size of 0.
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   331
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   332
    private void testToArray() throws Exception {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   333
        SortedSet sortedSet = Collections.emptySortedSet();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   334
        Object[] emptySortedSetArray = sortedSet.toArray();
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   335
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   336
        if ((emptySortedSetArray == null) || (emptySortedSetArray.length > 0)) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   337
            throwException("testToArray",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   338
                    "Returned null array or array with length > 0.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   339
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   340
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   341
        String[] strings = new String[2];
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   342
        strings[0] = "1";
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   343
        strings[1] = "2";
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   344
        emptySortedSetArray = sortedSet.toArray(strings);
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   345
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   346
        if ((emptySortedSetArray == null) || (emptySortedSetArray[0] != null)) {
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   347
            throwException("testToArray",
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   348
                    "Returned null array or array with length > 0.");
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   349
        }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   350
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents:
diff changeset
   351
}