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 junit.framework.*; 12 import java.util.AbstractQueue; 13 import java.util.Arrays; 14 import java.util.Iterator; 15 import java.util.NoSuchElementException; 16 17 public class AbstractQueueTest extends JSR166TestCase { 18 19 static class Succeed extends AbstractQueue<Integer> { offer(Integer x)20 public boolean offer(Integer x) { 21 if (x == null) throw new NullPointerException(); 22 return true; 23 } peek()24 public Integer peek() { return one; } poll()25 public Integer poll() { return one; } size()26 public int size() { return 0; } iterator()27 public Iterator iterator() { return null; } // not needed 28 } 29 30 static class Fail extends AbstractQueue<Integer> { offer(Integer x)31 public boolean offer(Integer x) { 32 if (x == null) throw new NullPointerException(); 33 return false; 34 } peek()35 public Integer peek() { return null; } poll()36 public Integer poll() { return null; } size()37 public int size() { return 0; } iterator()38 public Iterator iterator() { return null; } // not needed 39 } 40 41 /** 42 * add returns true if offer succeeds 43 */ testAddS()44 public void testAddS() { 45 Succeed q = new Succeed(); 46 assertTrue(q.add(two)); 47 } 48 49 /** 50 * add throws ISE true if offer fails 51 */ testAddF()52 public void testAddF() { 53 Fail q = new Fail(); 54 try { 55 q.add(one); 56 shouldThrow(); 57 } catch (IllegalStateException success) {} 58 } 59 60 /** 61 * add throws NPE if offer does 62 */ testAddNPE()63 public void testAddNPE() { 64 Succeed q = new Succeed(); 65 try { 66 q.add(null); 67 shouldThrow(); 68 } catch (NullPointerException success) {} 69 } 70 71 /** 72 * remove returns normally if poll succeeds 73 */ testRemoveS()74 public void testRemoveS() { 75 Succeed q = new Succeed(); 76 q.remove(); 77 } 78 79 /** 80 * remove throws NSEE if poll returns null 81 */ testRemoveF()82 public void testRemoveF() { 83 Fail q = new Fail(); 84 try { 85 q.remove(); 86 shouldThrow(); 87 } catch (NoSuchElementException success) {} 88 } 89 90 /** 91 * element returns normally if peek succeeds 92 */ testElementS()93 public void testElementS() { 94 Succeed q = new Succeed(); 95 q.element(); 96 } 97 98 /** 99 * element throws NSEE if peek returns null 100 */ testElementF()101 public void testElementF() { 102 Fail q = new Fail(); 103 try { 104 q.element(); 105 shouldThrow(); 106 } catch (NoSuchElementException success) {} 107 } 108 109 /** 110 * addAll(null) throws NPE 111 */ testAddAll1()112 public void testAddAll1() { 113 try { 114 Succeed q = new Succeed(); 115 q.addAll(null); 116 shouldThrow(); 117 } catch (NullPointerException success) {} 118 } 119 120 /** 121 * addAll(this) throws IAE 122 */ testAddAllSelf()123 public void testAddAllSelf() { 124 try { 125 Succeed q = new Succeed(); 126 q.addAll(q); 127 shouldThrow(); 128 } catch (IllegalArgumentException success) {} 129 } 130 131 /** 132 * addAll of a collection with null elements throws NPE 133 */ testAddAll2()134 public void testAddAll2() { 135 try { 136 Succeed q = new Succeed(); 137 Integer[] ints = new Integer[SIZE]; 138 q.addAll(Arrays.asList(ints)); 139 shouldThrow(); 140 } catch (NullPointerException success) {} 141 } 142 143 /** 144 * addAll of a collection with any null elements throws NPE after 145 * possibly adding some elements 146 */ testAddAll3()147 public void testAddAll3() { 148 try { 149 Succeed q = new Succeed(); 150 Integer[] ints = new Integer[SIZE]; 151 for (int i = 0; i < SIZE-1; ++i) 152 ints[i] = new Integer(i); 153 q.addAll(Arrays.asList(ints)); 154 shouldThrow(); 155 } catch (NullPointerException success) {} 156 } 157 158 /** 159 * addAll throws ISE if an add fails 160 */ testAddAll4()161 public void testAddAll4() { 162 try { 163 Fail q = new Fail(); 164 Integer[] ints = new Integer[SIZE]; 165 for (int i = 0; i < SIZE; ++i) 166 ints[i] = new Integer(i); 167 q.addAll(Arrays.asList(ints)); 168 shouldThrow(); 169 } catch (IllegalStateException success) {} 170 } 171 172 } 173