ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.4 by dl, Sat Sep 20 18:20:08 2003 UTC

# Line 10 | Line 10 | import java.util.*;
10   import java.util.concurrent.*;
11   import java.io.*;
12  
13 < public class LinkedBlockingQueueTest extends TestCase {
14 <
15 <    private static int N = 10;
16 <    private static long SHORT_DELAY_MS = 100;
17 <    private static long MEDIUM_DELAY_MS = 1000;
18 <    private static long LONG_DELAY_MS = 10000;
13 > public class LinkedBlockingQueueTest extends JSR166TestCase {
14  
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
# Line 25 | Line 20 | public class LinkedBlockingQueueTest ext
20          return new TestSuite(LinkedBlockingQueueTest.class);
21      }
22  
23 +
24      /**
25       * Create a queue of given size containing consecutive
26       * Integers 0 ... n.
27       */
28 <    private LinkedBlockingQueue fullQueue(int n) {
28 >    private LinkedBlockingQueue populatedQueue(int n) {
29          LinkedBlockingQueue q = new LinkedBlockingQueue(n);
30          assertTrue(q.isEmpty());
31          for(int i = 0; i < n; i++)
# Line 40 | Line 36 | public class LinkedBlockingQueueTest ext
36          return q;
37      }
38  
39 <    public void testConstructor1(){
40 <        assertEquals(N, new LinkedBlockingQueue(N).remainingCapacity());
39 >    /**
40 >     *
41 >     */
42 >    public void testConstructor1() {
43 >        assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
44      }
45  
46 <    public void testConstructor2(){
46 >    /**
47 >     *
48 >     */
49 >    public void testConstructor2() {
50          try {
51              LinkedBlockingQueue q = new LinkedBlockingQueue(0);
52 <            fail("Cannot make zero-sized");
52 >            shouldThrow();
53          }
54          catch (IllegalArgumentException success) {}
55      }
56  
57 <    public void testConstructor3(){
57 >    /**
58 >     *
59 >     */
60 >    public void testConstructor3() {
61  
62          try {
63              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
64 <            fail("Cannot make from null collection");
64 >            shouldThrow();
65          }
66          catch (NullPointerException success) {}
67      }
68  
69 <    public void testConstructor4(){
69 >    /**
70 >     *
71 >     */
72 >    public void testConstructor4() {
73          try {
74 <            Integer[] ints = new Integer[N];
74 >            Integer[] ints = new Integer[SIZE];
75              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
76 <            fail("Cannot make with null elements");
76 >            shouldThrow();
77          }
78          catch (NullPointerException success) {}
79      }
80  
81 <    public void testConstructor5(){
81 >    /**
82 >     *
83 >     */
84 >    public void testConstructor5() {
85          try {
86 <            Integer[] ints = new Integer[N];
87 <            for (int i = 0; i < N-1; ++i)
86 >            Integer[] ints = new Integer[SIZE];
87 >            for (int i = 0; i < SIZE-1; ++i)
88                  ints[i] = new Integer(i);
89              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
90 <            fail("Cannot make with null elements");
90 >            shouldThrow();
91          }
92          catch (NullPointerException success) {}
93      }
94  
95 <    public void testConstructor6(){
95 >    /**
96 >     *
97 >     */
98 >    public void testConstructor6() {
99          try {
100 <            Integer[] ints = new Integer[N];
101 <            for (int i = 0; i < N; ++i)
100 >            Integer[] ints = new Integer[SIZE];
101 >            for (int i = 0; i < SIZE; ++i)
102                  ints[i] = new Integer(i);
103              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
104 <            for (int i = 0; i < N; ++i)
104 >            for (int i = 0; i < SIZE; ++i)
105                  assertEquals(ints[i], q.poll());
106          }
107          finally {}
108      }
109  
110 +    /**
111 +     *
112 +     */
113      public void testEmptyFull() {
114          LinkedBlockingQueue q = new LinkedBlockingQueue(2);
115          assertTrue(q.isEmpty());
116          assertEquals("should have room for 2", 2, q.remainingCapacity());
117 <        q.add(new Integer(1));
117 >        q.add(one);
118          assertFalse(q.isEmpty());
119 <        q.add(new Integer(2));
119 >        q.add(two);
120          assertFalse(q.isEmpty());
121 <        assertEquals("queue should be full", 0, q.remainingCapacity());
122 <        assertFalse("offer should be rejected", q.offer(new Integer(3)));
121 >        assertEquals(0, q.remainingCapacity());
122 >        assertFalse(q.offer(three));
123      }
124  
125 <    public void testRemainingCapacity(){
126 <        LinkedBlockingQueue q = fullQueue(N);
127 <        for (int i = 0; i < N; ++i) {
125 >    /**
126 >     *
127 >     */
128 >    public void testRemainingCapacity() {
129 >        LinkedBlockingQueue q = populatedQueue(SIZE);
130 >        for (int i = 0; i < SIZE; ++i) {
131              assertEquals(i, q.remainingCapacity());
132 <            assertEquals(N-i, q.size());
132 >            assertEquals(SIZE-i, q.size());
133              q.remove();
134          }
135 <        for (int i = 0; i < N; ++i) {
136 <            assertEquals(N-i, q.remainingCapacity());
135 >        for (int i = 0; i < SIZE; ++i) {
136 >            assertEquals(SIZE-i, q.remainingCapacity());
137              assertEquals(i, q.size());
138              q.add(new Integer(i));
139          }
140      }
141  
142 <    public void testOfferNull(){
142 >    /**
143 >     *
144 >     */
145 >    public void testOfferNull() {
146          try {
147              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
148              q.offer(null);
149 <            fail("should throw NPE");
149 >            shouldThrow();
150          } catch (NullPointerException success) { }  
151      }
152  
153 <    public void testOffer(){
153 >    /**
154 >     *
155 >     */
156 >    public void testOffer() {
157          LinkedBlockingQueue q = new LinkedBlockingQueue(1);
158 <        assertTrue(q.offer(new Integer(0)));
159 <        assertFalse(q.offer(new Integer(1)));
158 >        assertTrue(q.offer(zero));
159 >        assertFalse(q.offer(one));
160      }
161  
162 <    public void testAdd(){
162 >    /**
163 >     *
164 >     */
165 >    public void testAdd() {
166          try {
167 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
168 <            for (int i = 0; i < N; ++i) {
167 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
168 >            for (int i = 0; i < SIZE; ++i) {
169                  assertTrue(q.add(new Integer(i)));
170              }
171              assertEquals(0, q.remainingCapacity());
172 <            q.add(new Integer(N));
172 >            q.add(new Integer(SIZE));
173          } catch (IllegalStateException success){
174          }  
175      }
176  
177 <    public void testAddAll1(){
177 >    /**
178 >     *
179 >     */
180 >    public void testAddAll1() {
181          try {
182              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
183              q.addAll(null);
184 <            fail("Cannot add null collection");
184 >            shouldThrow();
185          }
186          catch (NullPointerException success) {}
187      }
188 <    public void testAddAll2(){
188 >    /**
189 >     *
190 >     */
191 >    public void testAddAll2() {
192          try {
193 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
194 <            Integer[] ints = new Integer[N];
193 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
194 >            Integer[] ints = new Integer[SIZE];
195              q.addAll(Arrays.asList(ints));
196 <            fail("Cannot add null elements");
196 >            shouldThrow();
197          }
198          catch (NullPointerException success) {}
199      }
200 <    public void testAddAll3(){
200 >    /**
201 >     *
202 >     */
203 >    public void testAddAll3() {
204          try {
205 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
206 <            Integer[] ints = new Integer[N];
207 <            for (int i = 0; i < N-1; ++i)
205 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
206 >            Integer[] ints = new Integer[SIZE];
207 >            for (int i = 0; i < SIZE-1; ++i)
208                  ints[i] = new Integer(i);
209              q.addAll(Arrays.asList(ints));
210 <            fail("Cannot add null elements");
210 >            shouldThrow();
211          }
212          catch (NullPointerException success) {}
213      }
214 <    public void testAddAll4(){
214 >    /**
215 >     *
216 >     */
217 >    public void testAddAll4() {
218          try {
219              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
220 <            Integer[] ints = new Integer[N];
221 <            for (int i = 0; i < N; ++i)
220 >            Integer[] ints = new Integer[SIZE];
221 >            for (int i = 0; i < SIZE; ++i)
222                  ints[i] = new Integer(i);
223              q.addAll(Arrays.asList(ints));
224 <            fail("Cannot add with insufficient capacity");
224 >            shouldThrow();
225          }
226          catch (IllegalStateException success) {}
227      }
228 <    public void testAddAll5(){
228 >    /**
229 >     *
230 >     */
231 >    public void testAddAll5() {
232          try {
233              Integer[] empty = new Integer[0];
234 <            Integer[] ints = new Integer[N];
235 <            for (int i = 0; i < N; ++i)
234 >            Integer[] ints = new Integer[SIZE];
235 >            for (int i = 0; i < SIZE; ++i)
236                  ints[i] = new Integer(i);
237 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
237 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
238              assertFalse(q.addAll(Arrays.asList(empty)));
239              assertTrue(q.addAll(Arrays.asList(ints)));
240 <            for (int i = 0; i < N; ++i)
240 >            for (int i = 0; i < SIZE; ++i)
241                  assertEquals(ints[i], q.poll());
242          }
243          finally {}
244      }
245  
246 +    /**
247 +     *
248 +     */
249       public void testPutNull() {
250          try {
251 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
251 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
252              q.put(null);
253 <            fail("put should throw NPE");
253 >            shouldThrow();
254          }
255          catch (NullPointerException success){
256          }  
257          catch (InterruptedException ie) {
258 <            fail("Unexpected exception");
258 >            unexpectedException();
259          }
260       }
261  
262 +    /**
263 +     *
264 +     */
265       public void testPut() {
266           try {
267 <             LinkedBlockingQueue q = new LinkedBlockingQueue(N);
268 <             for (int i = 0; i < N; ++i) {
267 >             LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
268 >             for (int i = 0; i < SIZE; ++i) {
269                   Integer I = new Integer(i);
270                   q.put(I);
271                   assertTrue(q.contains(I));
# Line 223 | Line 273 | public class LinkedBlockingQueueTest ext
273               assertEquals(0, q.remainingCapacity());
274           }
275          catch (InterruptedException ie) {
276 <            fail("Unexpected exception");
276 >            unexpectedException();
277          }
278      }
279  
280 <    public void testBlockingPut(){
280 >    /**
281 >     *
282 >     */
283 >    public void testBlockingPut() {
284          Thread t = new Thread(new Runnable() {
285                  public void run() {
286                      int added = 0;
287                      try {
288 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(N);
289 <                        for (int i = 0; i < N; ++i) {
288 >                        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
289 >                        for (int i = 0; i < SIZE; ++i) {
290                              q.put(new Integer(i));
291                              ++added;
292                          }
293 <                        q.put(new Integer(N));
294 <                        fail("put should block");
293 >                        q.put(new Integer(SIZE));
294 >                        threadShouldThrow();
295                      } catch (InterruptedException ie){
296 <                        assertEquals(added, N);
296 >                        threadAssertEquals(added, SIZE);
297                      }  
298                  }});
299          t.start();
# Line 250 | Line 303 | public class LinkedBlockingQueueTest ext
303             t.join();
304          }
305          catch (InterruptedException ie) {
306 <            fail("Unexpected exception");
306 >            unexpectedException();
307          }
308      }
309  
310 +    /**
311 +     *
312 +     */
313      public void testPutWithTake() {
314          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
315          Thread t = new Thread(new Runnable() {
316 <                public void run(){
316 >                public void run() {
317                      int added = 0;
318                      try {
319                          q.put(new Object());
# Line 268 | Line 324 | public class LinkedBlockingQueueTest ext
324                          ++added;
325                          q.put(new Object());
326                          ++added;
327 <                        fail("Should block");
327 >                        threadShouldThrow();
328                      } catch (InterruptedException e){
329 <                        assertTrue(added >= 2);
329 >                        threadAssertTrue(added >= 2);
330                      }
331                  }
332              });
# Line 281 | Line 337 | public class LinkedBlockingQueueTest ext
337              t.interrupt();
338              t.join();
339          } catch (Exception e){
340 <            fail("Unexpected exception");
340 >            unexpectedException();
341          }
342      }
343  
344 +    /**
345 +     *
346 +     */
347      public void testTimedOffer() {
348          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
349          Thread t = new Thread(new Runnable() {
350 <                public void run(){
350 >                public void run() {
351                      try {
352                          q.put(new Object());
353                          q.put(new Object());
354 <                        assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
354 >                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
355                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
356 <                        fail("Should block");
356 >                        threadShouldThrow();
357                      } catch (InterruptedException success){}
358                  }
359              });
360          
361          try {
362              t.start();
363 <            Thread.sleep(SHORT_DELAY_MS);
363 >            Thread.sleep(SMALL_DELAY_MS);
364              t.interrupt();
365              t.join();
366          } catch (Exception e){
367 <            fail("Unexpected exception");
367 >            unexpectedException();
368          }
369      }
370  
371 <    public void testTake(){
371 >    /**
372 >     *
373 >     */
374 >    public void testTake() {
375          try {
376 <            LinkedBlockingQueue q = fullQueue(N);
377 <            for (int i = 0; i < N; ++i) {
376 >            LinkedBlockingQueue q = populatedQueue(SIZE);
377 >            for (int i = 0; i < SIZE; ++i) {
378                  assertEquals(i, ((Integer)q.take()).intValue());
379              }
380          } catch (InterruptedException e){
381 <            fail("Unexpected exception");
381 >            unexpectedException();
382          }  
383      }
384  
385 +    /**
386 +     *
387 +     */
388      public void testTakeFromEmpty() {
389          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
390          Thread t = new Thread(new Runnable() {
391 <                public void run(){
391 >                public void run() {
392                      try {
393                          q.take();
394 <                        fail("Should block");
394 >                        threadShouldThrow();
395                      } catch (InterruptedException success){ }                
396                  }
397              });
# Line 336 | Line 401 | public class LinkedBlockingQueueTest ext
401              t.interrupt();
402              t.join();
403          } catch (Exception e){
404 <            fail("Unexpected exception");
404 >            unexpectedException();
405          }
406      }
407  
408 <    public void testBlockingTake(){
408 >    /**
409 >     *
410 >     */
411 >    public void testBlockingTake() {
412          Thread t = new Thread(new Runnable() {
413                  public void run() {
414                      try {
415 <                        LinkedBlockingQueue q = fullQueue(N);
416 <                        for (int i = 0; i < N; ++i) {
415 >                        LinkedBlockingQueue q = populatedQueue(SIZE);
416 >                        for (int i = 0; i < SIZE; ++i) {
417                              assertEquals(i, ((Integer)q.take()).intValue());
418                          }
419                          q.take();
420 <                        fail("take should block");
420 >                        threadShouldThrow();
421                      } catch (InterruptedException success){
422                      }  
423                  }});
# Line 360 | Line 428 | public class LinkedBlockingQueueTest ext
428             t.join();
429          }
430          catch (InterruptedException ie) {
431 <            fail("Unexpected exception");
431 >            unexpectedException();
432          }
433      }
434  
435  
436 <    public void testPoll(){
437 <        LinkedBlockingQueue q = fullQueue(N);
438 <        for (int i = 0; i < N; ++i) {
436 >    /**
437 >     *
438 >     */
439 >    public void testPoll() {
440 >        LinkedBlockingQueue q = populatedQueue(SIZE);
441 >        for (int i = 0; i < SIZE; ++i) {
442              assertEquals(i, ((Integer)q.poll()).intValue());
443          }
444          assertNull(q.poll());
445      }
446  
447 +    /**
448 +     *
449 +     */
450      public void testTimedPoll0() {
451          try {
452 <            LinkedBlockingQueue q = fullQueue(N);
453 <            for (int i = 0; i < N; ++i) {
452 >            LinkedBlockingQueue q = populatedQueue(SIZE);
453 >            for (int i = 0; i < SIZE; ++i) {
454                  assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
455              }
456              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
457          } catch (InterruptedException e){
458 <            fail("Unexpected exception");
458 >            unexpectedException();
459          }  
460      }
461  
462 +    /**
463 +     *
464 +     */
465      public void testTimedPoll() {
466          try {
467 <            LinkedBlockingQueue q = fullQueue(N);
468 <            for (int i = 0; i < N; ++i) {
467 >            LinkedBlockingQueue q = populatedQueue(SIZE);
468 >            for (int i = 0; i < SIZE; ++i) {
469                  assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
470              }
471              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
472          } catch (InterruptedException e){
473 <            fail("Unexpected exception");
473 >            unexpectedException();
474          }  
475      }
476  
477 <    public void testInterruptedTimedPoll(){
477 >    /**
478 >     *
479 >     */
480 >    public void testInterruptedTimedPoll() {
481          Thread t = new Thread(new Runnable() {
482                  public void run() {
483                      try {
484 <                        LinkedBlockingQueue q = fullQueue(N);
485 <                        for (int i = 0; i < N; ++i) {
486 <                            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
484 >                        LinkedBlockingQueue q = populatedQueue(SIZE);
485 >                        for (int i = 0; i < SIZE; ++i) {
486 >                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
487                          }
488 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
488 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
489                      } catch (InterruptedException success){
490                      }  
491                  }});
# Line 416 | Line 496 | public class LinkedBlockingQueueTest ext
496             t.join();
497          }
498          catch (InterruptedException ie) {
499 <            fail("Unexpected exception");
499 >            unexpectedException();
500          }
501      }
502  
503 <    public void testTimedPollWithOffer(){
503 >    /**
504 >     *
505 >     */
506 >    public void testTimedPollWithOffer() {
507          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
508          Thread t = new Thread(new Runnable() {
509 <                public void run(){
509 >                public void run() {
510                      try {
511 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
511 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
512                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
513                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
514 <                        fail("Should block");
514 >                        threadShouldThrow();
515                      } catch (InterruptedException success) { }                
516                  }
517              });
518          try {
519              t.start();
520 <            Thread.sleep(SHORT_DELAY_MS * 2);
521 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
520 >            Thread.sleep(SMALL_DELAY_MS);
521 >            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
522              t.interrupt();
523              t.join();
524          } catch (Exception e){
525 <            fail("Unexpected exception");
525 >            unexpectedException();
526          }
527      }  
528  
529  
530 <    public void testPeek(){
531 <        LinkedBlockingQueue q = fullQueue(N);
532 <        for (int i = 0; i < N; ++i) {
530 >    /**
531 >     *
532 >     */
533 >    public void testPeek() {
534 >        LinkedBlockingQueue q = populatedQueue(SIZE);
535 >        for (int i = 0; i < SIZE; ++i) {
536              assertEquals(i, ((Integer)q.peek()).intValue());
537              q.poll();
538              assertTrue(q.peek() == null ||
# Line 455 | Line 541 | public class LinkedBlockingQueueTest ext
541          assertNull(q.peek());
542      }
543  
544 <    public void testElement(){
545 <        LinkedBlockingQueue q = fullQueue(N);
546 <        for (int i = 0; i < N; ++i) {
544 >    /**
545 >     *
546 >     */
547 >    public void testElement() {
548 >        LinkedBlockingQueue q = populatedQueue(SIZE);
549 >        for (int i = 0; i < SIZE; ++i) {
550              assertEquals(i, ((Integer)q.element()).intValue());
551              q.poll();
552          }
553          try {
554              q.element();
555 <            fail("no such element");
555 >            shouldThrow();
556          }
557          catch (NoSuchElementException success) {}
558      }
559  
560 <    public void testRemove(){
561 <        LinkedBlockingQueue q = fullQueue(N);
562 <        for (int i = 0; i < N; ++i) {
560 >    /**
561 >     *
562 >     */
563 >    public void testRemove() {
564 >        LinkedBlockingQueue q = populatedQueue(SIZE);
565 >        for (int i = 0; i < SIZE; ++i) {
566              assertEquals(i, ((Integer)q.remove()).intValue());
567          }
568          try {
569              q.remove();
570 <            fail("remove should throw");
570 >            shouldThrow();
571          } catch (NoSuchElementException success){
572          }  
573      }
574  
575 <    public void testRemoveElement(){
576 <        LinkedBlockingQueue q = fullQueue(N);
577 <        for (int i = 1; i < N; i+=2) {
575 >    /**
576 >     *
577 >     */
578 >    public void testRemoveElement() {
579 >        LinkedBlockingQueue q = populatedQueue(SIZE);
580 >        for (int i = 1; i < SIZE; i+=2) {
581              assertTrue(q.remove(new Integer(i)));
582          }
583 <        for (int i = 0; i < N; i+=2) {
583 >        for (int i = 0; i < SIZE; i+=2) {
584              assertTrue(q.remove(new Integer(i)));
585              assertFalse(q.remove(new Integer(i+1)));
586          }
587          assertTrue(q.isEmpty());
588      }
589          
590 <    public void testContains(){
591 <        LinkedBlockingQueue q = fullQueue(N);
592 <        for (int i = 0; i < N; ++i) {
590 >    /**
591 >     *
592 >     */
593 >    public void testContains() {
594 >        LinkedBlockingQueue q = populatedQueue(SIZE);
595 >        for (int i = 0; i < SIZE; ++i) {
596              assertTrue(q.contains(new Integer(i)));
597              q.poll();
598              assertFalse(q.contains(new Integer(i)));
599          }
600      }
601  
602 <    public void testClear(){
603 <        LinkedBlockingQueue q = fullQueue(N);
602 >    /**
603 >     *
604 >     */
605 >    public void testClear() {
606 >        LinkedBlockingQueue q = populatedQueue(SIZE);
607          q.clear();
608          assertTrue(q.isEmpty());
609          assertEquals(0, q.size());
610 <        assertEquals(N, q.remainingCapacity());
611 <        q.add(new Integer(1));
610 >        assertEquals(SIZE, q.remainingCapacity());
611 >        q.add(one);
612          assertFalse(q.isEmpty());
613          q.clear();
614          assertTrue(q.isEmpty());
615      }
616  
617 <    public void testContainsAll(){
618 <        LinkedBlockingQueue q = fullQueue(N);
619 <        LinkedBlockingQueue p = new LinkedBlockingQueue(N);
620 <        for (int i = 0; i < N; ++i) {
617 >    /**
618 >     *
619 >     */
620 >    public void testContainsAll() {
621 >        LinkedBlockingQueue q = populatedQueue(SIZE);
622 >        LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
623 >        for (int i = 0; i < SIZE; ++i) {
624              assertTrue(q.containsAll(p));
625              assertFalse(p.containsAll(q));
626              p.add(new Integer(i));
# Line 524 | Line 628 | public class LinkedBlockingQueueTest ext
628          assertTrue(p.containsAll(q));
629      }
630  
631 <    public void testRetainAll(){
632 <        LinkedBlockingQueue q = fullQueue(N);
633 <        LinkedBlockingQueue p = fullQueue(N);
634 <        for (int i = 0; i < N; ++i) {
631 >    /**
632 >     *
633 >     */
634 >    public void testRetainAll() {
635 >        LinkedBlockingQueue q = populatedQueue(SIZE);
636 >        LinkedBlockingQueue p = populatedQueue(SIZE);
637 >        for (int i = 0; i < SIZE; ++i) {
638              boolean changed = q.retainAll(p);
639              if (i == 0)
640                  assertFalse(changed);
# Line 535 | Line 642 | public class LinkedBlockingQueueTest ext
642                  assertTrue(changed);
643  
644              assertTrue(q.containsAll(p));
645 <            assertEquals(N-i, q.size());
645 >            assertEquals(SIZE-i, q.size());
646              p.remove();
647          }
648      }
649  
650 <    public void testRemoveAll(){
651 <        for (int i = 1; i < N; ++i) {
652 <            LinkedBlockingQueue q = fullQueue(N);
653 <            LinkedBlockingQueue p = fullQueue(i);
650 >    /**
651 >     *
652 >     */
653 >    public void testRemoveAll() {
654 >        for (int i = 1; i < SIZE; ++i) {
655 >            LinkedBlockingQueue q = populatedQueue(SIZE);
656 >            LinkedBlockingQueue p = populatedQueue(i);
657              assertTrue(q.removeAll(p));
658 <            assertEquals(N-i, q.size());
658 >            assertEquals(SIZE-i, q.size());
659              for (int j = 0; j < i; ++j) {
660                  Integer I = (Integer)(p.remove());
661                  assertFalse(q.contains(I));
# Line 554 | Line 664 | public class LinkedBlockingQueueTest ext
664      }
665  
666  
667 <    public void testToArray(){
668 <        LinkedBlockingQueue q = fullQueue(N);
667 >    /**
668 >     *
669 >     */
670 >    public void testToArray() {
671 >        LinkedBlockingQueue q = populatedQueue(SIZE);
672          Object[] o = q.toArray();
673          try {
674          for(int i = 0; i < o.length; i++)
675              assertEquals(o[i], q.take());
676          } catch (InterruptedException e){
677 <            fail("Unexpected exception");
677 >            unexpectedException();
678          }    
679      }
680  
681 <    public void testToArray2(){
682 <        LinkedBlockingQueue q = fullQueue(N);
683 <        Integer[] ints = new Integer[N];
681 >    /**
682 >     *
683 >     */
684 >    public void testToArray2() {
685 >        LinkedBlockingQueue q = populatedQueue(SIZE);
686 >        Integer[] ints = new Integer[SIZE];
687          ints = (Integer[])q.toArray(ints);
688          try {
689              for(int i = 0; i < ints.length; i++)
690                  assertEquals(ints[i], q.take());
691          } catch (InterruptedException e){
692 <            fail("Unexpected exception");
692 >            unexpectedException();
693          }    
694      }
695      
696 <    public void testIterator(){
697 <        LinkedBlockingQueue q = fullQueue(N);
696 >    /**
697 >     *
698 >     */
699 >    public void testIterator() {
700 >        LinkedBlockingQueue q = populatedQueue(SIZE);
701          Iterator it = q.iterator();
702          try {
703              while(it.hasNext()){
704                  assertEquals(it.next(), q.take());
705              }
706          } catch (InterruptedException e){
707 <            fail("Unexpected exception");
707 >            unexpectedException();
708          }    
709      }
710  
711 +    /**
712 +     *
713 +     */
714      public void testIteratorOrdering() {
715  
716          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
717  
718 <        q.add(new Integer(1));
719 <        q.add(new Integer(2));
720 <        q.add(new Integer(3));
718 >        q.add(one);
719 >        q.add(two);
720 >        q.add(three);
721  
722 <        assertEquals("queue should be full", 0, q.remainingCapacity());
722 >        assertEquals(0, q.remainingCapacity());
723  
724          int k = 0;
725          for (Iterator it = q.iterator(); it.hasNext();) {
726              int i = ((Integer)(it.next())).intValue();
727 <            assertEquals("items should come out in order", ++k, i);
727 >            assertEquals(++k, i);
728          }
729  
730 <        assertEquals("should go through 3 elements", 3, k);
730 >        assertEquals(3, k);
731      }
732  
733 +    /**
734 +     *
735 +     */
736      public void testWeaklyConsistentIteration () {
737  
738          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
739  
740 <        q.add(new Integer(1));
741 <        q.add(new Integer(2));
742 <        q.add(new Integer(3));
740 >        q.add(one);
741 >        q.add(two);
742 >        q.add(three);
743  
744          try {
745              for (Iterator it = q.iterator(); it.hasNext();) {
# Line 623 | Line 748 | public class LinkedBlockingQueueTest ext
748              }
749          }
750          catch (ConcurrentModificationException e) {
751 <            fail("weakly consistent iterator; should not get CME");
751 >            unexpectedException();
752          }
753  
754 <        assertEquals("queue should be empty again", 0, q.size());
754 >        assertEquals(0, q.size());
755      }
756  
757  
758 <    public void testToString(){
759 <        LinkedBlockingQueue q = fullQueue(N);
758 >    /**
759 >     *
760 >     */
761 >    public void testToString() {
762 >        LinkedBlockingQueue q = populatedQueue(SIZE);
763          String s = q.toString();
764 <        for (int i = 0; i < N; ++i) {
764 >        for (int i = 0; i < SIZE; ++i) {
765              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
766          }
767      }        
768  
769  
770 +    /**
771 +     *
772 +     */
773      public void testOfferInExecutor() {
774  
775          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
776  
777 <        q.add(new Integer(1));
778 <        q.add(new Integer(2));
777 >        q.add(one);
778 >        q.add(two);
779  
780          ExecutorService executor = Executors.newFixedThreadPool(2);
781  
782          executor.execute(new Runnable() {
783              public void run() {
784 <                assertFalse("offer should be rejected", q.offer(new Integer(3)));
784 >                threadAssertFalse(q.offer(three));
785                  try {
786 <                    assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
787 <                    assertEquals(0, q.remainingCapacity());
786 >                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
787 >                    threadAssertEquals(0, q.remainingCapacity());
788                  }
789                  catch (InterruptedException e) {
790 <                    fail("should not be interrupted");
790 >                    threadUnexpectedException();
791                  }
792              }
793          });
# Line 664 | Line 795 | public class LinkedBlockingQueueTest ext
795          executor.execute(new Runnable() {
796              public void run() {
797                  try {
798 <                    Thread.sleep(MEDIUM_DELAY_MS);
799 <                    assertEquals("first item in queue should be 1", new Integer(1), q.take());
798 >                    Thread.sleep(SMALL_DELAY_MS);
799 >                    threadAssertEquals(one, q.take());
800                  }
801                  catch (InterruptedException e) {
802 <                    fail("should not be interrupted");
802 >                    threadUnexpectedException();
803                  }
804              }
805          });
806          
807 <        executor.shutdown();
807 >        joinPool(executor);
808  
809      }
810  
811 +    /**
812 +     *
813 +     */
814      public void testPollInExecutor() {
815  
816          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
# Line 685 | Line 819 | public class LinkedBlockingQueueTest ext
819  
820          executor.execute(new Runnable() {
821              public void run() {
822 <                assertNull("poll should fail", q.poll());
822 >                threadAssertNull(q.poll());
823                  try {
824 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
825 <                    assertTrue(q.isEmpty());
824 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
825 >                    threadAssertTrue(q.isEmpty());
826                  }
827                  catch (InterruptedException e) {
828 <                    fail("should not be interrupted");
828 >                    threadUnexpectedException();
829                  }
830              }
831          });
# Line 699 | Line 833 | public class LinkedBlockingQueueTest ext
833          executor.execute(new Runnable() {
834              public void run() {
835                  try {
836 <                    Thread.sleep(MEDIUM_DELAY_MS);
837 <                    q.put(new Integer(1));
836 >                    Thread.sleep(SMALL_DELAY_MS);
837 >                    q.put(one);
838                  }
839                  catch (InterruptedException e) {
840 <                    fail("should not be interrupted");
840 >                    threadUnexpectedException();
841                  }
842              }
843          });
844          
845 <        executor.shutdown();
712 <
845 >        joinPool(executor);
846      }
847  
848 +    /**
849 +     *
850 +     */
851      public void testSerialization() {
852 <        LinkedBlockingQueue q = fullQueue(N);
852 >        LinkedBlockingQueue q = populatedQueue(SIZE);
853  
854          try {
855              ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
# Line 728 | Line 864 | public class LinkedBlockingQueueTest ext
864              while (!q.isEmpty())
865                  assertEquals(q.remove(), r.remove());
866          } catch(Exception e){
867 <            e.printStackTrace();
732 <            fail("unexpected exception");
867 >            unexpectedException();
868          }
869      }
870  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines