1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.util;
19
20import java.util.AbstractCollection;
21import java.util.Arrays;
22import java.util.Collection;
23import java.util.Iterator;
24import junit.framework.TestCase;
25
26public class AbstractCollectionTest extends TestCase {
27
28    /**
29     * java.util.AbstractCollection#add(java.lang.Object)
30     */
31    public void test_addLjava_lang_Object() {
32        AbstractCollection<Object> ac = new AbstractCollection<Object>() {
33
34            @Override
35            public Iterator<Object> iterator() {
36                fail("iterator should not get called");
37                return null;
38            }
39
40            @Override
41            public int size() {
42                fail("size should not get called");
43                return 0;
44            }
45
46        };
47        try {
48            ac.add(null);
49        } catch (UnsupportedOperationException e) {
50        }
51    }
52
53    /**
54     * java.util.AbstractCollection#addAll(java.util.Collection)
55     */
56    public void test_addAllLjava_util_Collection() {
57        final Collection<String> fixtures = Arrays.asList("0", "1", "2");
58        AbstractCollection<String> ac = new AbstractCollection<String>() {
59
60            @Override
61            public boolean add(String object) {
62                assertTrue(fixtures.contains(object));
63                return true;
64            }
65
66            @Override
67            public Iterator<String> iterator() {
68                fail("iterator should not get called");
69                return null;
70            }
71
72            @Override
73            public int size() {
74                fail("size should not get called");
75                return 0;
76            }
77
78        };
79        assertTrue(ac.addAll(fixtures));
80    }
81
82    /**
83     * java.util.AbstractCollection#containsAll(java.util.Collection)
84     */
85    public void test_containsAllLjava_util_Collection() {
86        final Collection<String> fixtures = Arrays.asList("0", "1", "2");
87        AbstractCollection<String> ac = new AbstractCollection<String>() {
88
89            @Override
90            public boolean contains(Object object) {
91                assertTrue(fixtures.contains(object));
92                return true;
93            }
94
95            @Override
96            public Iterator<String> iterator() {
97                fail("iterator should not get called");
98                return null;
99            }
100
101            @Override
102            public int size() {
103                fail("size should not get called");
104                return 0;
105            }
106
107        };
108        assertTrue(ac.containsAll(fixtures));
109    }
110
111    /**
112     * java.util.AbstractCollection#isEmpty()
113     */
114    public void test_isEmpty() {
115        final boolean[] sizeCalled = new boolean[1];
116        AbstractCollection<Object> ac = new AbstractCollection<Object>() {
117            @Override
118            public Iterator<Object> iterator() {
119                fail("iterator should not get called");
120                return null;
121            }
122
123            @Override
124            public int size() {
125                sizeCalled[0] = true;
126                return 0;
127            }
128        };
129        assertTrue(ac.isEmpty());
130        assertTrue(sizeCalled[0]);
131    }
132
133    /**
134     * java.util.AbstractCollection#removeAll(java.util.Collection)
135     */
136    public void test_removeAllLjava_util_Collection() {
137        final String[] removed = new String[3];
138        AbstractCollection<String> ac = new AbstractCollection<String>() {
139
140            @Override
141            public Iterator<String> iterator() {
142                return new Iterator<String>() {
143                    String[] values = new String[] { "0", "1", "2" };
144                    int index;
145
146                    public boolean hasNext() {
147                        return index < values.length;
148                    }
149
150                    public String next() {
151                        return values[index++];
152                    }
153
154                    public void remove() {
155                        removed[index - 1] = values[index - 1];
156                    }
157
158                };
159            }
160
161            @Override
162            public int size() {
163                fail("size should not get called");
164                return 0;
165            }
166
167        };
168        assertTrue(ac.removeAll(Arrays.asList("0", "1", "2")));
169        for (String r : removed) {
170            if (!"0".equals(r) && !"1".equals(r) && !"2".equals(r)) {
171                fail("an unexpected element was removed");
172            }
173        }
174    }
175
176    /**
177     * java.util.AbstractCollection#retainAll(java.util.Collection)
178     */
179    public void test_retainAllLjava_util_Collection() {
180        final String[] removed = new String[1];
181        AbstractCollection<String> ac = new AbstractCollection<String>() {
182
183            @Override
184            public Iterator<String> iterator() {
185                return new Iterator<String>() {
186                    String[] values = new String[] { "0", "1", "2" };
187                    int index;
188
189                    public boolean hasNext() {
190                        return index < values.length;
191                    }
192
193                    public String next() {
194                        return values[index++];
195                    }
196
197                    public void remove() {
198                        removed[index - 1] = values[index - 1];
199                    }
200
201                };
202            }
203
204            @Override
205            public int size() {
206                fail("size should not get called");
207                return 0;
208            }
209
210        };
211        assertTrue(ac.retainAll(Arrays.asList("1", "2")));
212        assertEquals("0", removed[0]);
213    }
214
215    /**
216     * java.util.AbstractCollection#toArray()
217     */
218    public void test_toArray() {
219        AbstractCollection<String> ac = new AbstractCollection<String>() {
220            @Override
221            public Iterator<String> iterator() {
222                return new Iterator<String>() {
223                    String[] values = new String[] { "0", "1", "2" };
224                    int index;
225
226                    public boolean hasNext() {
227                        return index < values.length;
228                    }
229
230                    public String next() {
231                        return values[index++];
232                    }
233
234                    public void remove() {
235                        fail("remove should not get called");
236                    }
237
238                };
239            }
240
241            @Override
242            public int size() {
243                return 3;
244            }
245        };
246
247        Object[] array = ac.toArray();
248        assertEquals(3, array.length);
249        for (Object o : array) {
250            if (!"0".equals(o) && !"1".equals(o) && !"2".equals(o)) {
251                fail("an unexpected element was removed");
252            }
253        }
254    }
255
256    /**
257     * java.util.AbstractCollection#toArray(java.lang.Object[])
258     */
259    public void test_toArray$Ljava_lang_Object() {
260        AbstractCollection<String> ac = new AbstractCollection<String>() {
261            @Override
262            public Iterator<String> iterator() {
263                return new Iterator<String>() {
264                    String[] values = new String[] { "0", "1", "2" };
265                    int index;
266
267                    public boolean hasNext() {
268                        return index < values.length;
269                    }
270
271                    public String next() {
272                        return values[index++];
273                    }
274
275                    public void remove() {
276                        fail("remove should not get called");
277                    }
278
279                };
280            }
281
282            @Override
283            public int size() {
284                return 3;
285            }
286        };
287        try {
288            ac.toArray(null);
289            fail("No expected NullPointerException");
290        } catch (NullPointerException e) {
291            // expected
292        }
293
294        try {
295            ac.toArray(new StringBuffer[ac.size()]);
296            fail("No expected ArrayStoreException");
297        } catch (ArrayStoreException e) {
298            // expected
299        }
300
301        String[] a = new String[3];
302        assertSame(a, ac.toArray(a));
303
304        a = new String[0];
305        assertNotSame(a, ac.toArray(a));
306        a = ac.toArray(a);
307        assertEquals(3, a.length);
308
309        CharSequence[] csa = new CharSequence[3];
310        ac.toArray(csa);
311        assertEquals(3, csa.length);
312        assertEquals("0", csa[0]);
313        assertEquals("1", csa[1]);
314        assertEquals("2", csa[2]);
315    }
316
317    /**
318     * java.util.AbstractCollection#toString()
319     */
320    public void test_toString() {
321        // see HARMONY-1522
322        // collection that returns null iterator(this is against the spec.)
323        AbstractCollection<?> c = new AbstractCollection<Object>() {
324            @Override
325            public int size() {
326                // return non-zero value to pass 'isEmpty' check
327                return 1;
328            }
329
330            @Override
331            public Iterator<Object> iterator() {
332                // this violates the spec.
333                return null;
334            }
335        };
336
337        try {
338            // AbstractCollection.toString() doesn't verify
339            // whether iterator() returns null value or not
340            c.toString();
341            fail("No expected NullPointerException");
342        } catch (NullPointerException e) {
343        }
344    }
345}
346