ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueueTest.java
Revision: 1.3
Committed: Sat Nov 21 10:25:05 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +9 -18 lines
Log Message:
improve exception handling

File Contents

# User Rev Content
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     junit.textui.TestRunner.run (suite());
19     }
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     * addAll(null) throws NPE
117     */
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     * addAll of a collection with null elements throws NPE
140     */
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     /**
150     * addAll of a collection with any null elements throws NPE after
151     * possibly adding some elements
152     */
153     public void testAddAll3() {
154     try {
155     Succeed q = new Succeed();
156     Integer[] ints = new Integer[SIZE];
157     for (int i = 0; i < SIZE-1; ++i)
158     ints[i] = new Integer(i);
159     q.addAll(Arrays.asList(ints));
160     shouldThrow();
161 jsr166 1.3 } catch (NullPointerException success) {}
162 dl 1.1 }
163     /**
164     * addAll throws ISE if an add fails
165     */
166     public void testAddAll4() {
167     try {
168     Fail q = new Fail();
169     Integer[] ints = new Integer[SIZE];
170     for (int i = 0; i < SIZE; ++i)
171     ints[i] = new Integer(i);
172     q.addAll(Arrays.asList(ints));
173     shouldThrow();
174 jsr166 1.3 } catch (IllegalStateException success) {}
175 dl 1.1 }
176    
177     }