OSDN Git Service

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