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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.harmony.archive.tests.java.util.jar;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.Iterator;
26 import java.util.jar.Attributes;
27 import junit.framework.TestCase;
29 public class AttributesTest extends TestCase {
33 protected void setUp() {
35 a.putValue("1", "one");
36 a.putValue("2", "two");
37 a.putValue("3", "three");
38 a.putValue("4", "four");
42 * @tests java.util.jar.Attributes#Attributes(java.util.jar.Attributes)
44 public void test_ConstructorLjava_util_jar_Attributes() {
45 Attributes a2 = new Attributes(a);
47 a.putValue("1", "one(1)");
48 assertTrue("equal", !a.equals(a2));
52 * @tests java.util.jar.Attributes#clear()
54 public void test_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"));
64 * @tests java.util.jar.Attributes#containsKey(java.lang.Object)
66 public void test_containsKeyLjava_lang_Object() {
67 assertTrue("a) Should have returned false", !a.containsKey(new Integer(
69 assertTrue("b) Should have returned false", !a.containsKey("0"));
70 assertTrue("Should have returned true", a
71 .containsKey(new Attributes.Name("1")));
75 * @tests java.util.jar.Attributes#containsValue(java.lang.Object)
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"));
83 * @tests java.util.jar.Attributes#entrySet()
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>();
90 assertEquals(4, entrySet.size());
91 i = entrySet.iterator();
93 java.util.Map.Entry<?, ?> e;
94 e = (Map.Entry<?, ?>) i.next();
95 keySet.add(e.getKey());
96 valueSet.add(e.getValue());
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")));
113 * @tests java.util.jar.Attributes#get(java.lang.Object)
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"));
120 a.getValue("IllegalArgumentException expected");
121 } catch (IllegalArgumentException ee) {
127 * @tests java.util.jar.Attributes#isEmpty()
129 public void test_isEmpty() {
130 assertTrue("Should not be empty", !a.isEmpty());
132 assertTrue("a) Should be empty", a.isEmpty());
133 a = new Attributes();
134 assertTrue("b) Should be empty", a.isEmpty());
138 * @tests java.util.jar.Attributes#keySet()
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(
145 assertTrue("b) Should contain entry", s.contains(new Attributes.Name(
147 assertTrue("c) Should contain entry", s.contains(new Attributes.Name(
149 assertTrue("d) Should contain entry", s.contains(new Attributes.Name(
154 * @tests java.util.jar.Attributes#putAll(java.util.Map)
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");
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,
169 assertNull("Assert 1: ", atts
170 .put(Attributes.Name.MANIFEST_VERSION, "1"));
171 Attributes atts2 = new Attributes();
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));
178 atts.putAll(Collections.EMPTY_MAP);
179 fail("Assert 4: no class cast from attrib parameter");
180 } catch (ClassCastException e) {
186 * @tests java.util.jar.Attributes#putAll(java.util.Map)
188 public void test_putAllLjava_util_Map2() {
189 // Regression for HARMONY-464
191 new Attributes().putAll((Map) null);
192 fail("ClassCastException expected");
193 } catch (ClassCastException e) {
195 // verify that special care for null is done in the Attributes.putAll()
200 public void putAll(Map<?, ?> attrib) {
203 }.putAll((Map<?, ?>) null);
204 fail("NullPointerException expected");
205 } catch (NullPointerException e) {
210 * @tests java.util.jar.Attributes#remove(java.lang.Object)
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"));
220 * @tests java.util.jar.Attributes#size()
222 public void test_size() {
223 assertEquals("Incorrect size returned", 4, a.size());
225 assertEquals(0, a.size());
229 * @tests java.util.jar.Attributes#values()
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"));
240 * @tests java.util.jar.Attributes#clone()
242 public void test_clone() {
243 Attributes a2 = (Attributes) a.clone();
245 a.putValue("1", "one(1)");
246 assertTrue("equal", !a.equals(a2));
250 * @tests java.util.jar.Attributes#equals(java.lang.Object)
252 public void test_equalsLjava_lang_Object() {
253 Attributes.Name n1 = new Attributes.Name("name"), n2 = new Attributes.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);
266 * @tests java.util.jar.Attributes.put(java.lang.Object, java.lang.Object)
268 public void test_putLjava_lang_ObjectLjava_lang_Object() {
269 Attributes atts = new Attributes();
270 assertNull("Assert 0: ", atts.put(Attributes.Name.CLASS_PATH,
272 assertEquals("Assert 1: ", "tools.jar", atts
273 .getValue(Attributes.Name.CLASS_PATH));
274 // Regression for HARMONY-79
276 atts.put("not a name", "value");
277 fail("Assert 2: no class cast from key parameter");
278 } catch (ClassCastException e) {
282 atts.put(Attributes.Name.CLASS_PATH, Boolean.TRUE);
283 fail("Assert 3: no class cast from value parameter");
284 } catch (ClassCastException e) {
290 * @tests java.util.jar.Attributes.put(java.lang.Object, java.lang.Object)
292 public void test_putLjava_lang_ObjectLjava_lang_Object_Null() {
294 Attributes attribute = new Attributes();
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));
305 String value = "It's null";
306 attribute.put(null, value);
307 assertEquals(1, attribute.size());
308 assertEquals(value, attribute.get(null));
310 Attributes.Name name = new Attributes.Name("null");
311 attribute.put(name, null);
312 assertEquals(2, attribute.size());
313 assertNull(attribute.get(name));
317 * @tests java.util.jar.Attributes.hashCode()
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());
325 private static class MockAttributes extends Attributes {
326 public Map<Object, Object> getMap() {
331 public void test_Constructor() {
332 Attributes attr = new Attributes();
333 assertTrue(attr.size() >= 0);
336 public void test_ConstructorI() {
337 Attributes attr = new Attributes(10);
338 assertTrue(attr.size() >= 0);
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"));
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")));
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();
361 assertNotSame(a.hashCode(), b.hashCode());
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");
371 assertTrue(a.equals(b));
374 b.putValue(null, "null");
375 fail("NullPointerException expected");
376 } catch (NullPointerException ee) {
380 StringBuffer sb = new StringBuffer();
381 for (int i = 0; i < 0x10000; i++) {
385 b.putValue(new String(sb), "wrong name");
386 fail("IllegalArgumentException expected");
387 } catch (IllegalArgumentException ee) {