AbstractSetTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.luni.tests.java.util;
18
19import dalvik.annotation.TestTargets;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestTargetClass;
23
24import junit.framework.TestCase;
25
26import java.util.AbstractSequentialList;
27import java.util.AbstractSet;
28import java.util.Collection;
29import java.util.Iterator;
30import java.util.Vector;
31
32@TestTargetClass(AbstractSet.class)
33public class AbstractSetTest extends TestCase {
34
35    class Mock_AbstractSet extends AbstractSet{
36
37        @Override
38        public Iterator iterator() {
39            return new Iterator() {
40                public boolean hasNext() {
41                    return false;
42                }
43
44                public Object next() {
45                    return null;
46                }
47
48                public void remove() {
49                }
50            };
51        }
52
53        @Override
54        public int size() {
55            return 0;
56        }
57    }
58
59    @TestTargetNew(
60        level = TestLevel.COMPLETE,
61        notes = "",
62        method = "hashCode",
63        args = {}
64    )
65    public void testHashCode() {
66        AbstractSet as = new Mock_AbstractSet();
67        assertNotNull(as.hashCode());
68    }
69
70    @TestTargets({
71        @TestTargetNew(
72            level = TestLevel.COMPLETE,
73            notes = "",
74            method = "equals",
75            args = {java.lang.Object.class}
76        ),
77        @TestTargetNew(
78            level = TestLevel.COMPLETE,
79            notes = "",
80            method = "AbstractSet",
81            args = {}
82        )
83    })
84    public void testEquals() {
85        AbstractSet as1 = new Mock_AbstractSet();
86        AbstractSet as2 = new Mock_AbstractSet();
87
88        assertTrue(as1.equals(as2));
89    }
90
91    @TestTargetNew(
92        level = TestLevel.COMPLETE,
93        notes = "",
94        method = "removeAll",
95        args = {java.util.Collection.class}
96    )
97    public void testRemoveAll() {
98        AbstractSet as = new AbstractSet(){
99            @Override
100            public Iterator iterator() {
101                return new Iterator() {
102                    public boolean hasNext() {
103                        return true;
104                    }
105
106                    public Object next() {
107                        return null;
108                    }
109
110                    public void remove() {
111                        throw new UnsupportedOperationException();
112                    }
113
114                };
115            }
116
117            @Override
118            public int size() {
119                return 10;
120            }
121        };
122
123        try {
124            as.removeAll(null);
125            fail("NullPointerException expected");
126        } catch (NullPointerException e) {
127            //expected
128        }
129        Collection c = new Vector();
130        c.add(null);
131        try {
132            as.removeAll(c);
133            fail("UnsupportedOperationException expected");
134        } catch (UnsupportedOperationException e) {
135            //expected
136        }
137
138        as = new Mock_AbstractSet();
139
140        as.removeAll(c);
141    }
142}
143