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.Collection;
27import java.util.HashMap;
28import java.util.Iterator;
29import java.util.Map;
30import java.util.Set;
31
32@TestTargetClass(Map.Entry.class)
33public class MapEntryTest extends TestCase {
34    Map.Entry me = null;
35    HashMap   hm = null;
36    Iterator  i  = null;
37
38    @TestTargetNew(
39        level = TestLevel.COMPLETE,
40        notes = "",
41        method = "getKey",
42        args = {}
43    )
44    public void testGetKey() {
45        assertTrue(hm.containsKey(me.getKey()));
46        hm.clear();
47        try {
48            me.getKey();
49            //expected
50        } catch (IllegalStateException e) {
51            //expected
52        }
53    }
54
55    @TestTargetNew(
56        level = TestLevel.COMPLETE,
57        notes = "",
58        method = "getValue",
59        args = {}
60    )
61    public void testGetValue() {
62        assertTrue(hm.containsValue(me.getValue()));
63        hm.clear();
64        try {
65            me.getValue();
66            //expected
67        } catch (IllegalStateException e) {
68            //expected
69        }
70    }
71
72    class Mock_HashMap extends HashMap {
73        @Override
74        public Object put(Object key, Object val) {
75            if (val == null) throw new NullPointerException();
76            if (val.getClass() == Double.class) throw new ClassCastException();
77            if (((String)val).equals("Wrong element")) throw new IllegalArgumentException();
78            throw new UnsupportedOperationException();
79        }
80
81        public Object fakePut(Object key, Object val) {
82            return super.put(key, val);
83        }
84    }
85
86    @TestTargetNew(
87        level = TestLevel.COMPLETE,
88        notes = "RI does not throw following exceptions: UnsupportedOperationException,ClassCastException, IllegalArgumentException and NullPointerException",
89        method = "setValue",
90        args = {java.lang.Object.class}
91    )
92    public void testSetValue() {
93        Mock_HashMap mhm = new Mock_HashMap();
94
95        mhm.fakePut(new Integer(1), "One");
96        mhm.fakePut(new Integer(2), "Two");
97
98        i = mhm.entrySet().iterator();
99        me = (Map.Entry)i.next();
100
101        me.setValue("Wrong element");
102
103        hm.clear();
104        try {
105            me.setValue("");
106            //expected
107        } catch (IllegalStateException e) {
108            //expected
109        }
110    }
111
112    @TestTargetNew(
113        level = TestLevel.COMPLETE,
114        notes = "",
115        method = "equals",
116        args = {java.lang.Object.class}
117    )
118    public void testEquals() {
119        Map.Entry me1 = (Map.Entry)i.next();
120        assertFalse(me.equals(me1));
121        assertFalse(me.equals(this));
122        me1 = me;
123        assertTrue(me.equals(me1));
124    }
125
126    @TestTargetNew(
127        level = TestLevel.COMPLETE,
128        notes = "",
129        method = "hashCode",
130        args = {}
131    )
132    public void testHashCode() {
133        Map.Entry me1 = (Map.Entry)i.next();
134        assertTrue(me.hashCode() != me1.hashCode());
135    }
136
137    protected void setUp() throws Exception {
138        hm = new HashMap();
139
140        hm.put(new Integer(1), "one");
141        hm.put(new Integer(2), "two");
142
143        i = hm.entrySet().iterator();
144        me = (Map.Entry)i.next();
145
146        super.setUp();
147    }
148}
149