tests/libjava-mauve/src/gnu/testlet/java/util/Collection/Test.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Tags: JDK1.5
       
     2 
       
     3 // Copyright (C) 2010 Andrew John Hughes <gnu_andrew@member.fsf.org>
       
     4 
       
     5 // This file is part of Mauve.
       
     6 
       
     7 // Mauve is free software; you can redistribute it and/or modify
       
     8 // it under the terms of the GNU General Public License as published by
       
     9 // the Free Software Foundation; either version 2, or (at your option)
       
    10 // any later version.
       
    11 
       
    12 // Mauve is distributed in the hope that it will be useful,
       
    13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 // GNU General Public License for more details.
       
    16 
       
    17 // You should have received a copy of the GNU General Public License
       
    18 // along with Mauve; see the file COPYING.  If not, write to
       
    19 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    20 // Boston, MA 02111-1307, USA.
       
    21 
       
    22 package gnu.testlet.java.util.Collection;
       
    23 
       
    24 import gnu.testlet.TestHarness;
       
    25 import gnu.testlet.Testlet;
       
    26 
       
    27 import java.util.Collection;
       
    28 
       
    29 import java.util.concurrent.BlockingQueue;
       
    30 import java.util.concurrent.Delayed;
       
    31 import java.util.concurrent.TimeUnit;
       
    32 
       
    33 /**
       
    34  * Some tests for the remove() method in the {@link List} interface.
       
    35  */
       
    36 public class Test implements Testlet
       
    37 {
       
    38 
       
    39   private TestHarness harness;
       
    40 
       
    41   /**
       
    42    * Runs the test using the specified harness.
       
    43    *
       
    44    * @param harness  the test harness (<code>null</code> not permitted).
       
    45    */
       
    46   public void test(TestHarness harness)
       
    47   {
       
    48     this.harness = harness;
       
    49     testClass(java.util.concurrent.ArrayBlockingQueue.class);
       
    50     testClass(java.util.ArrayDeque.class);
       
    51     testClass(java.util.ArrayList.class);
       
    52     testClass(java.util.concurrent.ConcurrentLinkedQueue.class);
       
    53     testClass(java.util.concurrent.ConcurrentSkipListSet.class);
       
    54     testClass(java.util.concurrent.CopyOnWriteArrayList.class);
       
    55     testClass(java.util.concurrent.CopyOnWriteArraySet.class);
       
    56     testClass(java.util.concurrent.DelayQueue.class);
       
    57     testClass(java.util.EnumSet.class);
       
    58     testClass(java.util.HashSet.class);
       
    59     testClass(java.util.concurrent.LinkedBlockingQueue.class);
       
    60     testClass(java.util.LinkedHashSet.class);
       
    61     testClass(java.util.LinkedList.class);
       
    62     testClass(java.util.concurrent.PriorityBlockingQueue.class);
       
    63     testClass(java.util.PriorityQueue.class);
       
    64     testClass(java.util.Stack.class);
       
    65     testClass(java.util.concurrent.SynchronousQueue.class);
       
    66     testClass(java.util.TreeSet.class);
       
    67     testClass(java.util.Vector.class);
       
    68   }
       
    69 
       
    70   /**
       
    71    * Tests the given {@link java.util.Collection} class.
       
    72    *
       
    73    * @param cls the class to test.
       
    74    */
       
    75   public void testClass(Class<? extends Collection> cls)
       
    76   {
       
    77     harness.checkPoint(cls.getName());
       
    78     Collection result = null;
       
    79     try
       
    80     {
       
    81       result = (Collection) cls.newInstance();
       
    82     }
       
    83     catch (Exception e)
       
    84     {
       
    85       harness.debug(e);
       
    86     }
       
    87     if (result != null)
       
    88       {
       
    89         testRemove(result);
       
    90       }
       
    91   }
       
    92 
       
    93   /**
       
    94    * Test the {@link Collection#remove(Object)} method of
       
    95    * the given collection.
       
    96    *
       
    97    * @param coll the collection to test.
       
    98    */
       
    99   public void testRemove(Collection coll)
       
   100   {
       
   101     /**
       
   102      * Use Delayed Object so DelayQueue
       
   103      * and sorted collections work.
       
   104      */
       
   105     Delayed obj = new Delayed()
       
   106       {
       
   107         public long getDelay(TimeUnit unit)
       
   108         {
       
   109           return unit.convert(10, TimeUnit.MINUTES);
       
   110         }
       
   111         public int compareTo(Delayed o)
       
   112         {
       
   113           Long other = o.getDelay(TimeUnit.NANOSECONDS);
       
   114           return other.compareTo(getDelay(TimeUnit.NANOSECONDS));
       
   115         }
       
   116       };
       
   117     String type = coll.getClass().getName();
       
   118 
       
   119     harness.check(coll.remove(obj) == false,
       
   120                   "Object is not present in empty " + type);
       
   121 
       
   122     boolean result = false;
       
   123     try
       
   124       {
       
   125         result = coll.remove(null);
       
   126       }
       
   127     catch (NullPointerException e)
       
   128       {
       
   129         /* Collection does not support null elements */
       
   130       }
       
   131     harness.check(result == false, "Null is not present in empty " + type);
       
   132 
       
   133     /* Can't do post-addition tests if no capacity */
       
   134     if (coll instanceof BlockingQueue &&
       
   135         ((BlockingQueue) coll).remainingCapacity() == 0)
       
   136       return;
       
   137 
       
   138     coll.add(obj);
       
   139     harness.check(coll.remove(obj) == true,
       
   140                   "Object is present in non-empty " + type);
       
   141 
       
   142     result = true;
       
   143     try
       
   144       {
       
   145         coll.add(null);
       
   146         result = coll.remove(null);
       
   147       }
       
   148     catch (NullPointerException e)
       
   149       {
       
   150         /* Collection does not support null elements */
       
   151       }
       
   152     harness.check(result == true, "Null is present in non-empty " + type);
       
   153   }
       
   154 
       
   155 }