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.3 |
import java.util.function.Consumer; |
13 |
jsr166 |
1.1 |
|
14 |
|
|
import junit.framework.Test; |
15 |
|
|
|
16 |
|
|
/** |
17 |
|
|
* Contains tests applicable to all Collection implementations. |
18 |
|
|
*/ |
19 |
|
|
public class CollectionTest extends JSR166TestCase { |
20 |
|
|
final CollectionImplementation impl; |
21 |
|
|
|
22 |
|
|
/** Tests are parameterized by a Collection implementation. */ |
23 |
|
|
CollectionTest(CollectionImplementation impl, String methodName) { |
24 |
|
|
super(methodName); |
25 |
|
|
this.impl = impl; |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
public static Test testSuite(CollectionImplementation impl) { |
29 |
|
|
return newTestSuite |
30 |
|
|
(parameterizedTestSuite(CollectionTest.class, |
31 |
|
|
CollectionImplementation.class, |
32 |
|
|
impl), |
33 |
|
|
jdk8ParameterizedTestSuite(CollectionTest.class, |
34 |
|
|
CollectionImplementation.class, |
35 |
|
|
impl)); |
36 |
|
|
} |
37 |
|
|
|
38 |
jsr166 |
1.2 |
/** Checks properties of empty collections. */ |
39 |
jsr166 |
1.1 |
public void testEmptyMeansEmpty() { |
40 |
jsr166 |
1.2 |
Collection c = impl.emptyCollection(); |
41 |
|
|
assertTrue(c.isEmpty()); |
42 |
|
|
assertEquals(0, c.size()); |
43 |
|
|
assertEquals("[]", c.toString()); |
44 |
|
|
assertEquals(0, c.toArray().length); |
45 |
|
|
{ |
46 |
|
|
Object[] a = new Object[0]; |
47 |
|
|
assertSame(a, c.toArray(a)); |
48 |
|
|
} |
49 |
jsr166 |
1.3 |
Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; |
50 |
|
|
c.forEach(alwaysThrows); |
51 |
|
|
c.iterator().forEachRemaining(alwaysThrows); |
52 |
|
|
c.spliterator().forEachRemaining(alwaysThrows); |
53 |
|
|
assertFalse(c.spliterator().tryAdvance(alwaysThrows)); |
54 |
jsr166 |
1.2 |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
55 |
|
|
Queue q = (Queue) c; |
56 |
|
|
assertNull(q.peek()); |
57 |
|
|
assertNull(q.poll()); |
58 |
|
|
} |
59 |
|
|
if (Deque.class.isAssignableFrom(impl.klazz())) { |
60 |
|
|
Deque d = (Deque) c; |
61 |
|
|
assertNull(d.peekFirst()); |
62 |
|
|
assertNull(d.peekLast()); |
63 |
|
|
assertNull(d.pollFirst()); |
64 |
|
|
assertNull(d.pollLast()); |
65 |
|
|
} |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
public void testNullPointerExceptions() { |
69 |
|
|
Collection c = impl.emptyCollection(); |
70 |
|
|
assertThrows( |
71 |
|
|
NullPointerException.class, |
72 |
|
|
() -> c.addAll(null), |
73 |
|
|
() -> c.containsAll(null), |
74 |
|
|
() -> c.retainAll(null), |
75 |
|
|
() -> c.removeAll(null), |
76 |
|
|
() -> c.removeIf(null)); |
77 |
|
|
|
78 |
|
|
if (!impl.permitsNulls()) { |
79 |
|
|
assertThrows( |
80 |
|
|
NullPointerException.class, |
81 |
|
|
() -> c.add(null)); |
82 |
|
|
} |
83 |
|
|
if (!impl.permitsNulls() |
84 |
|
|
&& Queue.class.isAssignableFrom(impl.klazz())) { |
85 |
|
|
Queue q = (Queue) c; |
86 |
|
|
assertThrows( |
87 |
|
|
NullPointerException.class, |
88 |
|
|
() -> q.offer(null)); |
89 |
|
|
} |
90 |
|
|
if (!impl.permitsNulls() |
91 |
|
|
&& Deque.class.isAssignableFrom(impl.klazz())) { |
92 |
|
|
Deque d = (Deque) c; |
93 |
|
|
assertThrows( |
94 |
|
|
NullPointerException.class, |
95 |
|
|
() -> d.addFirst(null), |
96 |
|
|
() -> d.addLast(null), |
97 |
|
|
() -> d.offerFirst(null), |
98 |
|
|
() -> d.offerLast(null), |
99 |
|
|
() -> d.push(null)); |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
public void testNoSuchElementExceptions() { |
104 |
|
|
Collection c = impl.emptyCollection(); |
105 |
jsr166 |
1.3 |
assertThrows( |
106 |
|
|
NoSuchElementException.class, |
107 |
|
|
() -> c.iterator().next()); |
108 |
jsr166 |
1.2 |
|
109 |
|
|
if (Queue.class.isAssignableFrom(impl.klazz())) { |
110 |
|
|
Queue q = (Queue) c; |
111 |
|
|
assertThrows( |
112 |
|
|
NoSuchElementException.class, |
113 |
|
|
() -> q.element(), |
114 |
|
|
() -> q.remove()); |
115 |
|
|
} |
116 |
|
|
if (Deque.class.isAssignableFrom(impl.klazz())) { |
117 |
|
|
Deque d = (Deque) c; |
118 |
|
|
assertThrows( |
119 |
|
|
NoSuchElementException.class, |
120 |
|
|
() -> d.getFirst(), |
121 |
|
|
() -> d.getLast(), |
122 |
|
|
() -> d.removeFirst(), |
123 |
|
|
() -> d.removeLast(), |
124 |
|
|
() -> d.pop()); |
125 |
|
|
} |
126 |
jsr166 |
1.1 |
} |
127 |
|
|
|
128 |
jsr166 |
1.4 |
// public void testCollectionDebugFail() { |
129 |
|
|
// fail(impl.klazz().getSimpleName()); |
130 |
|
|
// } |
131 |
jsr166 |
1.1 |
} |