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