Home | History | Annotate | Download | only in jsr166
      1 /*
      2  * Written by Doug Lea with assistance from members of JCP JSR-166
      3  * Expert Group and released to the public domain, as explained at
      4  * http://creativecommons.org/publicdomain/zero/1.0/
      5  * Other contributors include Andrew Wright, Jeffrey Hayes,
      6  * Pat Fisher, Mike Judd.
      7  */
      8 
      9 package jsr166;
     10 
     11 import java.util.AbstractQueue;
     12 import java.util.Arrays;
     13 import java.util.Iterator;
     14 import java.util.NoSuchElementException;
     15 
     16 import junit.framework.Test;
     17 import junit.framework.TestSuite;
     18 
     19 public class AbstractQueueTest extends JSR166TestCase {
     20     // android-note: Removed because the CTS runner does a bad job of
     21     // retrying tests that have suite() declarations.
     22     //
     23     // public static void main(String[] args) {
     24     //     main(suite(), args);
     25     // }
     26     // public static Test suite() {
     27     //     return new TestSuite(AbstractQueueTest.class);
     28     // }
     29 
     30     static class Succeed extends AbstractQueue<Integer> {
     31         public boolean offer(Integer x) {
     32             if (x == null) throw new NullPointerException();
     33             return true;
     34         }
     35         public Integer peek() { return one; }
     36         public Integer poll() { return one; }
     37         public int size() { return 0; }
     38         public Iterator iterator() { return null; } // not needed
     39     }
     40 
     41     static class Fail extends AbstractQueue<Integer> {
     42         public boolean offer(Integer x) {
     43             if (x == null) throw new NullPointerException();
     44             return false;
     45         }
     46         public Integer peek() { return null; }
     47         public Integer poll() { return null; }
     48         public int size() { return 0; }
     49         public Iterator iterator() { return null; } // not needed
     50     }
     51 
     52     /**
     53      * add returns true if offer succeeds
     54      */
     55     public void testAddS() {
     56         Succeed q = new Succeed();
     57         assertTrue(q.add(two));
     58     }
     59 
     60     /**
     61      * add throws ISE true if offer fails
     62      */
     63     public void testAddF() {
     64         Fail q = new Fail();
     65         try {
     66             q.add(one);
     67             shouldThrow();
     68         } catch (IllegalStateException success) {}
     69     }
     70 
     71     /**
     72      * add throws NPE if offer does
     73      */
     74     public void testAddNPE() {
     75         Succeed q = new Succeed();
     76         try {
     77             q.add(null);
     78             shouldThrow();
     79         } catch (NullPointerException success) {}
     80     }
     81 
     82     /**
     83      * remove returns normally if poll succeeds
     84      */
     85     public void testRemoveS() {
     86         Succeed q = new Succeed();
     87         q.remove();
     88     }
     89 
     90     /**
     91      * remove throws NSEE if poll returns null
     92      */
     93     public void testRemoveF() {
     94         Fail q = new Fail();
     95         try {
     96             q.remove();
     97             shouldThrow();
     98         } catch (NoSuchElementException success) {}
     99     }
    100 
    101     /**
    102      * element returns normally if peek succeeds
    103      */
    104     public void testElementS() {
    105         Succeed q = new Succeed();
    106         q.element();
    107     }
    108 
    109     /**
    110      * element throws NSEE if peek returns null
    111      */
    112     public void testElementF() {
    113         Fail q = new Fail();
    114         try {
    115             q.element();
    116             shouldThrow();
    117         } catch (NoSuchElementException success) {}
    118     }
    119 
    120     /**
    121      * addAll(null) throws NPE
    122      */
    123     public void testAddAll1() {
    124         Succeed q = new Succeed();
    125         try {
    126             q.addAll(null);
    127             shouldThrow();
    128         } catch (NullPointerException success) {}
    129     }
    130 
    131     /**
    132      * addAll(this) throws IAE
    133      */
    134     public void testAddAllSelf() {
    135         Succeed q = new Succeed();
    136         try {
    137             q.addAll(q);
    138             shouldThrow();
    139         } catch (IllegalArgumentException success) {}
    140     }
    141 
    142     /**
    143      * addAll of a collection with null elements throws NPE
    144      */
    145     public void testAddAll2() {
    146         Succeed q = new Succeed();
    147         Integer[] ints = new Integer[SIZE];
    148         try {
    149             q.addAll(Arrays.asList(ints));
    150             shouldThrow();
    151         } catch (NullPointerException success) {}
    152     }
    153 
    154     /**
    155      * addAll of a collection with any null elements throws NPE after
    156      * possibly adding some elements
    157      */
    158     public void testAddAll3() {
    159         Succeed q = new Succeed();
    160         Integer[] ints = new Integer[SIZE];
    161         for (int i = 0; i < SIZE - 1; ++i)
    162             ints[i] = new Integer(i);
    163         try {
    164             q.addAll(Arrays.asList(ints));
    165             shouldThrow();
    166         } catch (NullPointerException success) {}
    167     }
    168 
    169     /**
    170      * addAll throws ISE if an add fails
    171      */
    172     public void testAddAll4() {
    173         Fail q = new Fail();
    174         Integer[] ints = new Integer[SIZE];
    175         for (int i = 0; i < SIZE; ++i)
    176             ints[i] = new Integer(i);
    177         try {
    178             q.addAll(Arrays.asList(ints));
    179             shouldThrow();
    180         } catch (IllegalStateException success) {}
    181     }
    182 
    183 }
    184