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.jar;
19
20import java.util.Collection;
21import java.util.Collections;
22import java.util.HashSet;
23import java.util.Iterator;
24import java.util.Map;
25import java.util.Set;
26import java.util.jar.Attributes;
27import junit.framework.TestCase;
28
29public class AttributesTest extends TestCase {
30    private Attributes a;
31
32    @Override
33    protected void setUp() {
34        a = new Attributes();
35        a.putValue("1", "one");
36        a.putValue("2", "two");
37        a.putValue("3", "three");
38        a.putValue("4", "four");
39    }
40
41    /**
42     * java.util.jar.Attributes#Attributes(java.util.jar.Attributes)
43     */
44    public void test_ConstructorLjava_util_jar_Attributes() {
45        Attributes a2 = new Attributes(a);
46        assertEquals(a, a2);
47        a.putValue("1", "one(1)");
48        assertTrue("equal", !a.equals(a2));
49    }
50
51    /**
52     * java.util.jar.Attributes#clear()
53     */
54    public void test_clear() {
55        a.clear();
56        assertNull("a) All entries should be null after clear", a.get("1"));
57        assertNull("b) All entries should be null after clear", a.get("2"));
58        assertNull("c) All entries should be null after clear", a.get("3"));
59        assertNull("d) All entries should be null after clear", a.get("4"));
60        assertTrue("Should not contain any keys", !a.containsKey("1"));
61    }
62
63    /**
64     * java.util.jar.Attributes#containsKey(java.lang.Object)
65     */
66    public void test_containsKeyLjava_lang_Object() {
67        assertTrue("a) Should have returned false", !a.containsKey(new Integer(1)));
68        assertTrue("b) Should have returned false", !a.containsKey("0"));
69        assertTrue("Should have returned true", a.containsKey(new Attributes.Name("1")));
70    }
71
72    /**
73     * java.util.jar.Attributes#containsValue(java.lang.Object)
74     */
75    public void test_containsValueLjava_lang_Object() {
76        assertTrue("Should have returned false", !a.containsValue("One"));
77        assertTrue("Should have returned true", a.containsValue("one"));
78    }
79
80    /**
81     * java.util.jar.Attributes#entrySet()
82     */
83    public void test_entrySet() {
84        Set<Map.Entry<Object, Object>> entrySet = a.entrySet();
85        Set<Object> keySet = new HashSet<Object>();
86        Set<Object> valueSet = new HashSet<Object>();
87        Iterator<?> i;
88        assertEquals(4, entrySet.size());
89        i = entrySet.iterator();
90        while (i.hasNext()) {
91            java.util.Map.Entry<?, ?> e;
92            e = (Map.Entry<?, ?>) i.next();
93            keySet.add(e.getKey());
94            valueSet.add(e.getValue());
95        }
96        assertTrue("a) Should contain entry", valueSet.contains("one"));
97        assertTrue("b) Should contain entry", valueSet.contains("two"));
98        assertTrue("c) Should contain entry", valueSet.contains("three"));
99        assertTrue("d) Should contain entry", valueSet.contains("four"));
100        assertTrue("a) Should contain key", keySet.contains(new Attributes.Name("1")));
101        assertTrue("b) Should contain key", keySet.contains(new Attributes.Name("2")));
102        assertTrue("c) Should contain key", keySet.contains(new Attributes.Name("3")));
103        assertTrue("d) Should contain key", keySet.contains(new Attributes.Name("4")));
104    }
105
106    /**
107     * java.util.jar.Attributes#get(java.lang.Object)
108     */
109    public void test_getLjava_lang_Object() {
110        assertEquals("a) Incorrect value returned", "one", a.getValue("1"));
111        assertNull("b) Incorrect value returned", a.getValue("0"));
112    }
113
114    /**
115     * java.util.jar.Attributes#isEmpty()
116     */
117    public void test_isEmpty() {
118        assertTrue("Should not be empty", !a.isEmpty());
119        a.clear();
120        assertTrue("a) Should be empty", a.isEmpty());
121        a = new Attributes();
122        assertTrue("b) Should be empty", a.isEmpty());
123    }
124
125    /**
126     * java.util.jar.Attributes#keySet()
127     */
128    public void test_keySet() {
129        Set<?> s = a.keySet();
130        assertEquals(4, s.size());
131        assertTrue("a) Should contain entry", s.contains(new Attributes.Name("1")));
132        assertTrue("b) Should contain entry", s.contains(new Attributes.Name("2")));
133        assertTrue("c) Should contain entry", s.contains(new Attributes.Name("3")));
134        assertTrue("d) Should contain entry", s.contains(new Attributes.Name("4")));
135    }
136
137    /**
138     * java.util.jar.Attributes#putAll(java.util.Map)
139     */
140    public void test_putAllLjava_util_Map() {
141        Attributes b = new Attributes();
142        b.putValue("3", "san");
143        b.putValue("4", "shi");
144        b.putValue("5", "go");
145        b.putValue("6", "roku");
146        a.putAll(b);
147        assertEquals("Should not have been replaced", "one", a.getValue("1"));
148        assertEquals("Should have been replaced", "san", a.getValue("3"));
149        assertEquals("Should have been added", "go", a.getValue("5"));
150        Attributes atts = new Attributes();
151        assertNull("Assert 0: ", atts.put(Attributes.Name.CLASS_PATH, "tools.jar"));
152        assertNull("Assert 1: ", atts.put(Attributes.Name.MANIFEST_VERSION, "1"));
153        Attributes atts2 = new Attributes();
154        atts2.putAll(atts);
155        assertEquals("Assert 2:", "tools.jar", atts2.get(Attributes.Name.CLASS_PATH));
156        assertEquals("Assert 3: ", "1", atts2.get(Attributes.Name.MANIFEST_VERSION));
157        try {
158            atts.putAll(Collections.EMPTY_MAP);
159            fail("Assert 4: no class cast from attrib parameter");
160        } catch (ClassCastException e) {
161            // Expected
162        }
163    }
164
165    /**
166     * java.util.jar.Attributes#remove(java.lang.Object)
167     */
168    public void test_removeLjava_lang_Object() {
169        a.remove(new Attributes.Name("1"));
170        a.remove(new Attributes.Name("3"));
171        assertNull("Should have been removed", a.getValue("1"));
172        assertEquals("Should not have been removed", "four", a.getValue("4"));
173    }
174
175    /**
176     * java.util.jar.Attributes#size()
177     */
178    public void test_size() {
179        assertEquals("Incorrect size returned", 4, a.size());
180        a.clear();
181        assertEquals(0, a.size());
182    }
183
184    /**
185     * java.util.jar.Attributes#values()
186     */
187    public void test_values() {
188        Collection<?> valueCollection = a.values();
189        assertTrue("a) Should contain entry", valueCollection.contains("one"));
190        assertTrue("b) Should contain entry", valueCollection.contains("two"));
191        assertTrue("c) Should contain entry", valueCollection.contains("three"));
192        assertTrue("d) Should contain entry", valueCollection.contains("four"));
193    }
194
195    /**
196     * java.util.jar.Attributes#clone()
197     */
198    public void test_clone() {
199        Attributes a2 = (Attributes) a.clone();
200        assertEquals(a, a2);
201        a.putValue("1", "one(1)");
202        assertTrue("equal", !a.equals(a2));
203    }
204
205    /**
206     * java.util.jar.Attributes#equals(java.lang.Object)
207     */
208    public void test_equalsLjava_lang_Object() {
209        Attributes.Name n1 = new Attributes.Name("name"), n2 = new Attributes.Name("Name");
210        assertEquals(n1, n2);
211        Attributes a1 = new Attributes();
212        a1.putValue("one", "1");
213        a1.putValue("two", "2");
214        Attributes a2 = new Attributes();
215        a2.putValue("One", "1");
216        a2.putValue("TWO", "2");
217        assertEquals(a1, a2);
218        assertEquals(a1, a1);
219        a2 = null;
220        assertFalse(a1.equals(a2));
221    }
222
223    /**
224     * java.util.jar.Attributes.put(java.lang.Object, java.lang.Object)
225     */
226    public void test_putLjava_lang_ObjectLjava_lang_Object() {
227        Attributes atts = new Attributes();
228        assertNull("Assert 0: ", atts.put(Attributes.Name.CLASS_PATH, "tools.jar"));
229        assertEquals("Assert 1: ", "tools.jar", atts.getValue(Attributes.Name.CLASS_PATH));
230        // Regression for HARMONY-79
231        try {
232            atts.put("not a name", "value");
233            fail("Assert 2: no class cast from key parameter");
234        } catch (ClassCastException e) {
235            // Expected
236        }
237        try {
238            atts.put(Attributes.Name.CLASS_PATH, Boolean.TRUE);
239            fail("Assert 3: no class cast from value parameter");
240        } catch (ClassCastException e) {
241            // Expected
242        }
243    }
244
245    /**
246     * java.util.jar.Attributes.put(java.lang.Object, java.lang.Object)
247     */
248    public void test_putLjava_lang_ObjectLjava_lang_Object_Null() {
249
250        Attributes attribute = new Attributes();
251
252        assertFalse(attribute.containsKey(null));
253        assertFalse(attribute.containsValue(null));
254        attribute.put(null, null);
255        attribute.put(null, null);
256        assertEquals(1, attribute.size());
257        assertTrue(attribute.containsKey(null));
258        assertTrue(attribute.containsValue(null));
259        assertNull(attribute.get(null));
260
261        String value = "It's null";
262        attribute.put(null, value);
263        assertEquals(1, attribute.size());
264        assertEquals(value, attribute.get(null));
265
266        Attributes.Name name = new Attributes.Name("null");
267        attribute.put(name, null);
268        assertEquals(2, attribute.size());
269        assertNull(attribute.get(name));
270    }
271
272    /**
273     * java.util.jar.Attributes.hashCode()
274     */
275    public void test_hashCode() {
276        MockAttributes mockAttr = new MockAttributes();
277        mockAttr.putValue("1", "one");
278        assertEquals(mockAttr.getMap().hashCode(), mockAttr.hashCode());
279    }
280
281    private static class MockAttributes extends Attributes {
282        public Map<Object, Object> getMap() {
283            return map;
284        }
285    }
286}
287