OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / archive / tests / java / util / jar / AttributesTest.java
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
18 package org.apache.harmony.archive.tests.java.util.jar;
19
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.jar.Attributes;
27 import junit.framework.TestCase;
28
29 public 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      * @tests 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      * @tests 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      * @tests 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(
68                 1)));
69         assertTrue("b) Should have returned false", !a.containsKey("0"));
70         assertTrue("Should have returned true", a
71                 .containsKey(new Attributes.Name("1")));
72     }
73
74     /**
75      * @tests java.util.jar.Attributes#containsValue(java.lang.Object)
76      */
77     public void test_containsValueLjava_lang_Object() {
78         assertTrue("Should have returned false", !a.containsValue("One"));
79         assertTrue("Should have returned true", a.containsValue("one"));
80     }
81
82     /**
83      * @tests java.util.jar.Attributes#entrySet()
84      */
85     public void test_entrySet() {
86         Set<Map.Entry<Object, Object>> entrySet = a.entrySet();
87         Set<Object> keySet = new HashSet<Object>();
88         Set<Object> valueSet = new HashSet<Object>();
89         Iterator<?> i;
90         assertEquals(4, entrySet.size());
91         i = entrySet.iterator();
92         while (i.hasNext()) {
93             java.util.Map.Entry<?, ?> e;
94             e = (Map.Entry<?, ?>) i.next();
95             keySet.add(e.getKey());
96             valueSet.add(e.getValue());
97         }
98         assertTrue("a) Should contain entry", valueSet.contains("one"));
99         assertTrue("b) Should contain entry", valueSet.contains("two"));
100         assertTrue("c) Should contain entry", valueSet.contains("three"));
101         assertTrue("d) Should contain entry", valueSet.contains("four"));
102         assertTrue("a) Should contain key", keySet
103                 .contains(new Attributes.Name("1")));
104         assertTrue("b) Should contain key", keySet
105                 .contains(new Attributes.Name("2")));
106         assertTrue("c) Should contain key", keySet
107                 .contains(new Attributes.Name("3")));
108         assertTrue("d) Should contain key", keySet
109                 .contains(new Attributes.Name("4")));
110     }
111
112     /**
113      * @tests java.util.jar.Attributes#get(java.lang.Object)
114      */
115     public void test_getLjava_lang_Object() {
116         assertEquals("a) Incorrect value returned", "one", a.getValue("1"));
117         assertNull("b) Incorrect value returned", a.getValue("0"));
118
119         try {
120             a.getValue("IllegalArgumentException expected");
121         } catch (IllegalArgumentException ee) {
122             // expected
123         }
124     }
125
126     /**
127      * @tests java.util.jar.Attributes#isEmpty()
128      */
129     public void test_isEmpty() {
130         assertTrue("Should not be empty", !a.isEmpty());
131         a.clear();
132         assertTrue("a) Should be empty", a.isEmpty());
133         a = new Attributes();
134         assertTrue("b) Should be empty", a.isEmpty());
135     }
136
137     /**
138      * @tests java.util.jar.Attributes#keySet()
139      */
140     public void test_keySet() {
141         Set<?> s = a.keySet();
142         assertEquals(4, s.size());
143         assertTrue("a) Should contain entry", s.contains(new Attributes.Name(
144                 "1")));
145         assertTrue("b) Should contain entry", s.contains(new Attributes.Name(
146                 "2")));
147         assertTrue("c) Should contain entry", s.contains(new Attributes.Name(
148                 "3")));
149         assertTrue("d) Should contain entry", s.contains(new Attributes.Name(
150                 "4")));
151     }
152
153     /**
154      * @tests java.util.jar.Attributes#putAll(java.util.Map)
155      */
156     public void test_putAllLjava_util_Map() {
157         Attributes b = new Attributes();
158         b.putValue("3", "san");
159         b.putValue("4", "shi");
160         b.putValue("5", "go");
161         b.putValue("6", "roku");
162         a.putAll(b);
163         assertEquals("Should not have been replaced", "one", a.getValue("1"));
164         assertEquals("Should have been replaced", "san", a.getValue("3"));
165         assertEquals("Should have been added", "go", a.getValue("5"));
166         Attributes atts = new Attributes();
167         assertNull("Assert 0: ", atts.put(Attributes.Name.CLASS_PATH,
168                 "tools.jar"));
169         assertNull("Assert 1: ", atts
170                 .put(Attributes.Name.MANIFEST_VERSION, "1"));
171         Attributes atts2 = new Attributes();
172         atts2.putAll(atts);
173         assertEquals("Assert 2:", "tools.jar", atts2
174                 .get(Attributes.Name.CLASS_PATH));
175         assertEquals("Assert 3: ", "1", atts2
176                 .get(Attributes.Name.MANIFEST_VERSION));
177         try {
178             atts.putAll(Collections.EMPTY_MAP);
179             fail("Assert 4: no class cast from attrib parameter");
180         } catch (ClassCastException e) {
181             // Expected
182         }
183     }
184
185     /**
186      * @tests java.util.jar.Attributes#putAll(java.util.Map)
187      */
188     public void test_putAllLjava_util_Map2() {
189         // Regression for HARMONY-464
190         try {
191             new Attributes().putAll((Map) null);
192             fail("ClassCastException expected");
193         } catch (ClassCastException e) {
194         }
195         // verify that special care for null is done in the Attributes.putAll()
196         // method
197         try {
198             new Attributes() {
199                 @Override
200                 public void putAll(Map<?, ?> attrib) {
201                     map.putAll(attrib);
202                 }
203             }.putAll((Map<?, ?>) null);
204             fail("NullPointerException expected");
205         } catch (NullPointerException e) {
206         }
207     }
208
209     /**
210      * @tests java.util.jar.Attributes#remove(java.lang.Object)
211      */
212     public void test_removeLjava_lang_Object() {
213         a.remove(new Attributes.Name("1"));
214         a.remove(new Attributes.Name("3"));
215         assertNull("Should have been removed", a.getValue("1"));
216         assertEquals("Should not have been removed", "four", a.getValue("4"));
217     }
218
219     /**
220      * @tests java.util.jar.Attributes#size()
221      */
222     public void test_size() {
223         assertEquals("Incorrect size returned", 4, a.size());
224         a.clear();
225         assertEquals(0, a.size());
226     }
227
228     /**
229      * @tests java.util.jar.Attributes#values()
230      */
231     public void test_values() {
232         Collection<?> valueCollection = a.values();
233         assertTrue("a) Should contain entry", valueCollection.contains("one"));
234         assertTrue("b) Should contain entry", valueCollection.contains("two"));
235         assertTrue("c) Should contain entry", valueCollection.contains("three"));
236         assertTrue("d) Should contain entry", valueCollection.contains("four"));
237     }
238
239     /**
240      * @tests java.util.jar.Attributes#clone()
241      */
242     public void test_clone() {
243         Attributes a2 = (Attributes) a.clone();
244         assertEquals(a, a2);
245         a.putValue("1", "one(1)");
246         assertTrue("equal", !a.equals(a2));
247     }
248
249     /**
250      * @tests java.util.jar.Attributes#equals(java.lang.Object)
251      */
252     public void test_equalsLjava_lang_Object() {
253         Attributes.Name n1 = new Attributes.Name("name"), n2 = new Attributes.Name(
254                 "Name");
255         assertEquals(n1, n2);
256         Attributes a1 = new Attributes();
257         a1.putValue("one", "1");
258         a1.putValue("two", "2");
259         Attributes a2 = new Attributes();
260         a2.putValue("One", "1");
261         a2.putValue("TWO", "2");
262         assertEquals(a1, a2);
263     }
264
265     /**
266      * @tests java.util.jar.Attributes.put(java.lang.Object, java.lang.Object)
267      */
268     public void test_putLjava_lang_ObjectLjava_lang_Object() {
269         Attributes atts = new Attributes();
270         assertNull("Assert 0: ", atts.put(Attributes.Name.CLASS_PATH,
271                 "tools.jar"));
272         assertEquals("Assert 1: ", "tools.jar", atts
273                 .getValue(Attributes.Name.CLASS_PATH));
274         // Regression for HARMONY-79
275         try {
276             atts.put("not a name", "value");
277             fail("Assert 2: no class cast from key parameter");
278         } catch (ClassCastException e) {
279             // Expected
280         }
281         try {
282             atts.put(Attributes.Name.CLASS_PATH, Boolean.TRUE);
283             fail("Assert 3: no class cast from value parameter");
284         } catch (ClassCastException e) {
285             // Expected
286         }
287     }
288
289     /**
290      * @tests java.util.jar.Attributes.put(java.lang.Object, java.lang.Object)
291      */
292     public void test_putLjava_lang_ObjectLjava_lang_Object_Null() {
293
294         Attributes attribute = new Attributes();
295
296         assertFalse(attribute.containsKey(null));
297         assertFalse(attribute.containsValue(null));
298         attribute.put(null, null);
299         attribute.put(null, null);
300         assertEquals(1, attribute.size());
301         assertTrue(attribute.containsKey(null));
302         assertTrue(attribute.containsValue(null));
303         assertNull(attribute.get(null));
304
305         String value = "It's null";
306         attribute.put(null, value);
307         assertEquals(1, attribute.size());
308         assertEquals(value, attribute.get(null));
309
310         Attributes.Name name = new Attributes.Name("null");
311         attribute.put(name, null);
312         assertEquals(2, attribute.size());
313         assertNull(attribute.get(name));
314     }
315
316     /**
317      * @tests java.util.jar.Attributes.hashCode()
318      */
319     public void test_hashCode_consistent_with_map() {
320         MockAttributes mockAttr = new MockAttributes();
321         mockAttr.putValue("1", "one");
322         assertEquals(mockAttr.getMap().hashCode(), mockAttr.hashCode());
323     }
324
325     private static class MockAttributes extends Attributes {
326         public Map<Object, Object> getMap() {
327             return map;
328         }
329     }
330
331     public void test_Constructor() {
332         Attributes attr = new Attributes();
333         assertTrue(attr.size() >= 0);
334     }
335
336     public void test_ConstructorI() {
337         Attributes attr = new Attributes(10);
338         assertTrue(attr.size() >= 0);
339     }
340
341     public void test_getLjava_lang_Object_true() {
342         assertEquals("a) Incorrect value returned", "one", a
343                 .get(new Attributes.Name("1")));
344         assertNull("b) Incorrect value returned", a.get("0"));
345         assertNull("b) Incorrect value returned", a.get("1"));
346     }
347
348     public void test_getValueLjava_util_jar_Attributes_Name() {
349         assertEquals("a) Incorrect value returned", "one", a
350                 .getValue(new Attributes.Name("1")));
351         assertNull("b) Incorrect value returned", a
352                 .getValue(new Attributes.Name("0")));
353     }
354
355     public void test_hashCode() {
356         Attributes b = (Attributes) a.clone();
357         b.putValue("33", "Thirty three");
358         assertNotSame(a.hashCode(), b.hashCode());
359         b = (Attributes) a.clone();
360         b.clear();
361         assertNotSame(a.hashCode(), b.hashCode());
362     }
363
364     public void test_putValueLjava_lang_StringLjava_lang_String() {
365         Attributes b = new Attributes();
366         b.put(new Attributes.Name("1"), "one");
367         b.putValue("2", "two");
368         b.put(new Attributes.Name("3"), "three");
369         b.putValue("4", "four");
370
371         assertTrue(a.equals(b));
372
373         try {
374             b.putValue(null, "null");
375             fail("NullPointerException expected");
376         } catch (NullPointerException ee) {
377             // expected
378         }
379
380         StringBuffer sb = new StringBuffer();
381         for (int i = 0; i < 0x10000; i++) {
382             sb.append('3');
383         }
384         try {
385             b.putValue(new String(sb), "wrong name");
386             fail("IllegalArgumentException expected");
387         } catch (IllegalArgumentException ee) {
388             // expected
389         }
390     }
391 }