1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
|
|
* members of JCP JSR-166 Expert Group and released to the public |
4 |
|
|
* domain, as explained at |
5 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import java.util.Collection; |
9 |
jsr166 |
1.2 |
import java.util.Deque; |
10 |
|
|
import java.util.NoSuchElementException; |
11 |
|
|
import java.util.Queue; |
12 |
jsr166 |
1.1 |
|
13 |
|
|
import junit.framework.Test; |
14 |
|
|
|
15 |
|
|
/** |
16 |
|
|
* Contains tests applicable to all Collection implementations. |
17 |
|
|
*/ |
18 |
|
|
public class CollectionTest extends JSR166TestCase { |
19 |
|
|
final CollectionImplementation impl; |
20 |
|
|
|
21 |
|
|
/** Tests are parameterized by a Collection implementation. */ |
22 |
|
|
CollectionTest(CollectionImplementation impl, String methodName) { |
23 |
|
|
super(methodName); |
24 |
|
|
this.impl = impl; |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
public static Test testSuite(CollectionImplementation impl) { |
28 |
|
|
return newTestSuite |
29 |
|
|
(parameterizedTestSuite(CollectionTest.class, |
30 |
|
|
CollectionImplementation.class, |
31 |
|
|
impl), |
32 |
|
|
jdk8ParameterizedTestSuite(CollectionTest.class, |
33 |
|
|
CollectionImplementation.class, |
34 |
|
|
impl)); |
35 |
|
|
} |
36 |
|
|
|
37 |
jsr166 |
1.2 |
/** Checks properties of empty collections. */ |
38 |
jsr166 |
1.1 |
public void testEmptyMeansEmpty() { |
39 |
jsr166 |
1.2 |
Collection c = impl.emptyCollection(); |
40 |
|
|
assertTrue(c.isEmpty()); |
41 |
|
|
assertEquals(0, c.size()); |
42 |
|
|
assertEquals("[]", c.toString()); |
43 |
|
|
assertEquals(0, c.toArray().length); |
44 |
|
|
{ |
45 |
|
|
Object[] a = new Object[0]; |
46 |
|
|
assertSame(a, c.toArray(a)); |
47 |
|
|
} |
48 |
|
|
c.forEach((e) -> { throw new AssertionError(); }); |
49 |
|
|
if (Queue.class.isAssignableFrom(impl.klazz())) { |
50 |
|
|
Queue q = (Queue) c; |
51 |
|
|
assertNull(q.peek()); |
52 |
|
|
assertNull(q.poll()); |
53 |
|
|
} |
54 |
|
|
if (Deque.class.isAssignableFrom(impl.klazz())) { |
55 |
|
|
Deque d = (Deque) c; |
56 |
|
|
assertNull(d.peekFirst()); |
57 |
|
|
assertNull(d.peekLast()); |
58 |
|
|
assertNull(d.pollFirst()); |
59 |
|
|
assertNull(d.pollLast()); |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
public void testNullPointerExceptions() { |
64 |
|
|
Collection c = impl.emptyCollection(); |
65 |
|
|
assertThrows( |
66 |
|
|
NullPointerException.class, |
67 |
|
|
() -> c.addAll(null), |
68 |
|
|
() -> c.containsAll(null), |
69 |
|
|
() -> c.retainAll(null), |
70 |
|
|
() -> c.removeAll(null), |
71 |
|
|
() -> c.removeIf(null)); |
72 |
|
|
|
73 |
|
|
if (!impl.permitsNulls()) { |
74 |
|
|
assertThrows( |
75 |
|
|
NullPointerException.class, |
76 |
|
|
() -> c.add(null)); |
77 |
|
|
} |
78 |
|
|
if (!impl.permitsNulls() |
79 |
|
|
&& Queue.class.isAssignableFrom(impl.klazz())) { |
80 |
|
|
Queue q = (Queue) c; |
81 |
|
|
assertThrows( |
82 |
|
|
NullPointerException.class, |
83 |
|
|
() -> q.offer(null)); |
84 |
|
|
} |
85 |
|
|
if (!impl.permitsNulls() |
86 |
|
|
&& Deque.class.isAssignableFrom(impl.klazz())) { |
87 |
|
|
Deque d = (Deque) c; |
88 |
|
|
assertThrows( |
89 |
|
|
NullPointerException.class, |
90 |
|
|
() -> d.addFirst(null), |
91 |
|
|
() -> d.addLast(null), |
92 |
|
|
() -> d.offerFirst(null), |
93 |
|
|
() -> d.offerLast(null), |
94 |
|
|
() -> d.push(null)); |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
public void testNoSuchElementExceptions() { |
99 |
|
|
Collection c = impl.emptyCollection(); |
100 |
|
|
|
101 |
|
|
if (Queue.class.isAssignableFrom(impl.klazz())) { |
102 |
|
|
Queue q = (Queue) c; |
103 |
|
|
assertThrows( |
104 |
|
|
NoSuchElementException.class, |
105 |
|
|
() -> q.element(), |
106 |
|
|
() -> q.remove()); |
107 |
|
|
} |
108 |
|
|
if (Deque.class.isAssignableFrom(impl.klazz())) { |
109 |
|
|
Deque d = (Deque) c; |
110 |
|
|
assertThrows( |
111 |
|
|
NoSuchElementException.class, |
112 |
|
|
() -> d.getFirst(), |
113 |
|
|
() -> d.getLast(), |
114 |
|
|
() -> d.removeFirst(), |
115 |
|
|
() -> d.removeLast(), |
116 |
|
|
() -> d.pop()); |
117 |
|
|
} |
118 |
jsr166 |
1.1 |
} |
119 |
|
|
|
120 |
|
|
// public void testCollectionDebugFail() { fail(); } |
121 |
|
|
} |