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