1 /* |
|
2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6336968 |
|
27 * @summary Test AttributeList.toMap |
|
28 * @author Eamonn McManus |
|
29 */ |
|
30 |
|
31 import java.math.BigInteger; |
|
32 import java.util.Arrays; |
|
33 import java.util.HashMap; |
|
34 import java.util.HashSet; |
|
35 import java.util.Map; |
|
36 import javax.management.Attribute; |
|
37 import javax.management.AttributeList; |
|
38 |
|
39 public class AttributeListMapTest { |
|
40 |
|
41 private static String failure; |
|
42 |
|
43 public static void main(String[] args) throws Exception { |
|
44 AttributeList attrs = new AttributeList(Arrays.asList( |
|
45 new Attribute("Str", "Five"), |
|
46 new Attribute("Int", 5), |
|
47 new Attribute("Flt", 5.0))); |
|
48 |
|
49 Map<String, Object> map = attrs.toMap(); |
|
50 final Map<String, Object> expectMap = new HashMap<String, Object>(); |
|
51 for (Attribute attr : attrs.asList()) |
|
52 expectMap.put(attr.getName(), attr.getValue()); |
|
53 assertEquals("Initial map", expectMap, map); |
|
54 assertEquals("Initial map size", 3, map.size()); |
|
55 assertEquals("Name set", expectMap.keySet(), map.keySet()); |
|
56 assertEquals("Values", new HashSet<Object>(expectMap.values()), |
|
57 new HashSet<Object>(map.values())); |
|
58 assertEquals("Entry set", expectMap.entrySet(), map.entrySet()); |
|
59 |
|
60 AttributeList attrs2 = new AttributeList(map); |
|
61 assertEquals("AttributeList from Map", attrs, attrs2); |
|
62 // This assumes that the Map conserves the order of the attributes, |
|
63 // which is not specified but true because we use LinkedHashMap. |
|
64 |
|
65 // Check that toMap fails if the list contains non-Attribute elements. |
|
66 AttributeList attrs3 = new AttributeList(attrs); |
|
67 attrs3.add("Hello"); // allowed but curious |
|
68 try { |
|
69 map = attrs3.toMap(); |
|
70 fail("toMap succeeded on list with non-Attribute elements"); |
|
71 } catch (Exception e) { |
|
72 assertEquals("Exception for toMap with non-Atttribute elements", |
|
73 IllegalArgumentException.class, e.getClass()); |
|
74 } |
|
75 |
|
76 // Check that the Map does not reflect changes made to the list after |
|
77 // the Map was obtained. |
|
78 AttributeList attrs4 = new AttributeList(attrs); |
|
79 map = attrs4.toMap(); |
|
80 attrs4.add(new Attribute("Big", new BigInteger("5"))); |
|
81 assertEquals("Map after adding element to list", expectMap, map); |
|
82 |
|
83 // Check that if there is more than one Attribute with the same name |
|
84 // then toMap() chooses the last of them. |
|
85 AttributeList attrs5 = new AttributeList(attrs); |
|
86 attrs5.add(new Attribute("Str", "Cinq")); |
|
87 map = attrs5.toMap(); |
|
88 assertEquals("Size of Map for list with duplicate attribute name", |
|
89 3, map.size()); |
|
90 Object value = map.get("Str"); |
|
91 assertEquals("Value of Str in Map for list with two values for it", |
|
92 "Cinq", value); |
|
93 |
|
94 if (failure == null) |
|
95 System.out.println("TEST PASSED"); |
|
96 else |
|
97 throw new Exception("TEST FAILED: " + failure); |
|
98 } |
|
99 |
|
100 private static void assertEquals(String what, Object expect, Object actual) { |
|
101 if (eq(expect, actual)) |
|
102 System.out.println("OK: " + what); |
|
103 else |
|
104 fail(what + ": expected " + expect + ", got " + actual); |
|
105 } |
|
106 |
|
107 private static boolean eq(Object x, Object y) { |
|
108 return (x == null) ? (y == null) : x.equals(y); |
|
109 } |
|
110 |
|
111 private static void fail(String why) { |
|
112 System.out.println("FAIL: " + why); |
|
113 failure = why; |
|
114 } |
|
115 } |
|