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