1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
/*
* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4847959 6191402
* @summary Test newly-generified APIs
* @author Eamonn McManus
*
* @run clean GenericTest
* @run build GenericTest
* @run main GenericTest
*/
import java.lang.management.ManagementFactory;
import java.lang.reflect.*;
import java.util.*;
import javax.management.*;
import javax.management.openmbean.*;
import javax.management.relation.*;
import javax.management.timer.Timer;
import javax.management.timer.TimerMBean;
public class GenericTest {
private static int failures;
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// Check we are really using the generified version
boolean generic;
Method findmbs = MBeanServerFactory.class.getMethod("findMBeanServer",
String.class);
Type findmbstype = findmbs.getGenericReturnType();
if (!(findmbstype instanceof ParameterizedType)) {
System.out.println("FAILURE: API NOT GENERIC!");
System.out.println(" MBeanServerFactory.findMBeanServer -> " +
findmbstype);
failures++;
generic = false;
} else {
System.out.println("OK: this API is generic");
generic = true;
}
ArrayList<MBeanServer> mbsList1 =
MBeanServerFactory.findMBeanServer(null);
checked(mbsList1, MBeanServer.class);
ArrayList mbsList2 = MBeanServerFactory.findMBeanServer(null);
check("ArrayList<MBeanServer> findMBeanServer", mbsList1.size() == 1);
check("ArrayList findMBeanServer", mbsList1.equals(mbsList2));
Set<ObjectName> names1 =
checked(mbs.queryNames(null, null), ObjectName.class);
Set names2 = mbs.queryNames(null, null);
Set<ObjectName> names3 =
checked(((MBeanServerConnection) mbs).queryNames(null, null),
ObjectName.class);
check("Set<ObjectName> MBeanServer.queryNames", names1.size() >= 1);
check("Set MBeanServer.queryNames", names2.size() >= 1);
check("Set<ObjectName> MBeanServerConnection.queryNames",
names3.size() >= 1);
check("queryNames sets same",
names1.equals(names2) && names2.equals(names3));
Set<ObjectInstance> mbeans1 =
checked(mbs.queryMBeans(null, null), ObjectInstance.class);
Set mbeans2 = mbs.queryMBeans(null, null);
Set<ObjectInstance> mbeans3 =
checked(((MBeanServerConnection) mbs).queryMBeans(null, null),
ObjectInstance.class);
check("Set<ObjectInstsance> MBeanServer.queryMBeans",
mbeans1.size() >= 1);
check("Set MBeanServer.queryMBeans", mbeans2.size() >= 1);
check("Set<ObjectInstsance> MBeanServerConnection.queryMBeans",
mbeans3.size() >= 1);
check("queryMBeans sets same",
mbeans1.equals(mbeans2) && mbeans2.equals(mbeans3));
AttributeChangeNotificationFilter acnf =
new AttributeChangeNotificationFilter();
acnf.enableAttribute("foo");
Vector<String> acnfs = acnf.getEnabledAttributes();
checked(acnfs, String.class);
check("Vector<String> AttributeChangeNotificationFilter.getEnabled" +
"Attributes", acnfs.equals(Arrays.asList(new String[] {"foo"})));
if (generic) {
Attribute a = new Attribute("foo", "bar");
AttributeList al1 = new AttributeList();
al1.add(a);
AttributeList al2 =
new AttributeList(Arrays.asList(new Attribute[] {a}));
check("new AttributeList(List<Attribute>)", al1.equals(al2));
List<Attribute> al3 = checked(al1.asList(), Attribute.class);
al3.remove(a);
check("List<Attribute> AttributeList.asList()",
al1.equals(al3) && al1.isEmpty());
}
List<ObjectName> namelist1 = new ArrayList<ObjectName>(names1);
Role role = new Role("rolename", namelist1);
List<ObjectName> namelist2 =
checked(role.getRoleValue(), ObjectName.class);
check("new Role(String,List<ObjectName>).getRoleValue() -> " +
"List<ObjectName>", namelist1.equals(namelist2));
RoleList rl1 = new RoleList();
rl1.add(role);
RoleList rl2 = new RoleList(Arrays.asList(new Role[] {role}));
check("new RoleList(List<Role>)", rl1.equals(rl2));
if (generic) {
List<Role> rl3 = checked(rl1.asList(), Role.class);
rl3.remove(role);
check("List<Role> RoleList.asList()",
rl1.equals(rl3) && rl1.isEmpty());
}
RoleUnresolved ru =
new RoleUnresolved("rolename", namelist1,
RoleStatus.LESS_THAN_MIN_ROLE_DEGREE);
List<ObjectName> namelist3 =
checked(ru.getRoleValue(), ObjectName.class);
check("new RoleUnresolved(...List<ObjectName>...).getRoleValue() -> " +
"List<ObjectName>", namelist1.equals(namelist3));
RoleUnresolvedList rul1 = new RoleUnresolvedList();
rul1.add(ru);
RoleUnresolvedList rul2 =
new RoleUnresolvedList(Arrays.asList(new RoleUnresolved[] {ru}));
check("new RoleUnresolvedList(List<RoleUnresolved>", rul1.equals(rul2));
if (generic) {
List<RoleUnresolved> rul3 =
checked(rul1.asList(), RoleUnresolved.class);
rul3.remove(ru);
check("List<RoleUnresolved> RoleUnresolvedList.asList()",
rul1.equals(rul3) && rul1.isEmpty());
}
// This case basically just tests that we can compile this sort of thing
OpenMBeanAttributeInfo ombai1 =
new OpenMBeanAttributeInfoSupport("a", "a descr",
SimpleType.INTEGER,
true, true, false);
CompositeType ct =
new CompositeType("ct", "ct descr", new String[] {"item1"},
new String[] {"item1 descr"},
new OpenType[] {SimpleType.INTEGER});
OpenMBeanAttributeInfo ombai2 =
new OpenMBeanAttributeInfoSupport("a", "a descr",
ct, true, true, false);
TabularType tt =
new TabularType("tt", "tt descr", ct, new String[] {"item1"});
OpenMBeanAttributeInfo ombai3 =
new OpenMBeanAttributeInfoSupport("a", "a descr",
tt, true, true, false);
ArrayType<String[][]> at =
new ArrayType<String[][]>(2, SimpleType.STRING);
OpenMBeanAttributeInfo ombai4 =
new OpenMBeanAttributeInfoSupport("a", "a descr",
at, true, true, false);
OpenMBeanAttributeInfo ombai4a =
new OpenMBeanAttributeInfoSupport("a", "a descr",
(ArrayType) at,
true, true, false);
OpenMBeanAttributeInfo ombai5 =
new OpenMBeanAttributeInfoSupport("a", "a descr",
SimpleType.INTEGER,
true, true, false,
5, 1, 9);
OpenMBeanAttributeInfo ombai6 =
new OpenMBeanAttributeInfoSupport("a", "a descr",
SimpleType.INTEGER,
true, true, false,
5, new Integer[] {1, 5});
OpenMBeanInfo ombi =
new OpenMBeanInfoSupport("a.a", "a.a descr",
new OpenMBeanAttributeInfo[] {
ombai1, ombai2, ombai3, ombai4,
ombai5, ombai6,
},
null, null, null);
Map<String,Integer> itemMap =
checked(singletonMap("item1", 5),
String.class, Integer.class);
CompositeData cd =
new CompositeDataSupport(ct, itemMap);
check("CompositeDataSupport(CompositeType, Map<String,?>",
cd.get("item1").equals(5));
Set<String> ctkeys = checked(ct.keySet(), String.class);
check("Set<String> CompositeType.keySet()",
ctkeys.equals(singleton("item1")));
List<String> ttindex = checked(tt.getIndexNames(), String.class);
check("Set<String> TabularType.getIndexNames()",
ttindex.equals(singletonList("item1")));
TabularData td = new TabularDataSupport(tt);
td.putAll(new CompositeData[] {cd});
List<Integer> tdkey = checked(singletonList(5), Integer.class);
Set<List<Integer>> tdkeys = checked(singleton(tdkey),
(Class<List<Integer>>) tdkey.getClass());
Collection<CompositeData> tdvalues = checked(singleton(cd),
CompositeData.class);
check("Set<List<?>> TabularDataSupport.keySet()",
td.keySet().equals(tdkeys));
check("Collection<CompositeData> TabularDataSupport.values()",
td.values().iterator().next().equals(tdvalues.iterator().next()));
ObjectName stupidName = new ObjectName("stupid:a=b");
mbs.registerMBean(new Stupid(), stupidName);
StupidMBean proxy =
MBeanServerInvocationHandler.newProxyInstance(mbs,
stupidName,
StupidMBean.class,
false);
check("MBeanServerInvocationHandler.newProxyInstance",
proxy.getFive() == 5);
mbs.unregisterMBean(stupidName);
mbs.registerMBean(new StandardMBean(new Stupid(), StupidMBean.class),
stupidName);
check("<T> StandardMBean(T impl, Class<T> intf)",
proxy.getFive() == 5);
// Following is based on the package.html for javax.management.relation
// Create the Relation Service MBean
ObjectName relSvcName = new ObjectName(":type=RelationService");
RelationService relSvcObject = new RelationService(true);
mbs.registerMBean(relSvcObject, relSvcName);
// Create an MBean proxy for easier access to the Relation Service
RelationServiceMBean relSvc =
MBeanServerInvocationHandler.newProxyInstance(mbs, relSvcName,
RelationServiceMBean.class,
false);
// Define the DependsOn relation type
RoleInfo[] dependsOnRoles = {
new RoleInfo("dependent", Module.class.getName()),
new RoleInfo("dependedOn", Module.class.getName())
};
relSvc.createRelationType("DependsOn", dependsOnRoles);
// Now define a relation instance "moduleA DependsOn moduleB"
ObjectName moduleA = new ObjectName(":type=Module,name=A");
ObjectName moduleB = new ObjectName(":type=Module,name=B");
// Following two lines added to example:
mbs.registerMBean(new Module(), moduleA);
mbs.registerMBean(new Module(), moduleB);
Role dependent = new Role("dependent", singletonList(moduleA));
Role dependedOn = new Role("dependedOn", singletonList(moduleB));
Role[] roleArray = {dependent, dependedOn};
RoleList roles = new RoleList(Arrays.asList(roleArray));
relSvc.createRelation("A-DependsOn-B", "DependsOn", roles);
// Query the Relation Service to find what modules moduleA depends on
Map<ObjectName,List<String>> dependentAMap =
relSvc.findAssociatedMBeans(moduleA, "DependsOn", "dependent");
Set<ObjectName> dependentASet = dependentAMap.keySet();
dependentASet = checked(dependentASet, ObjectName.class);
// Set of ObjectName containing moduleB
check("Map<ObjectName,List<String>> RelationService.findAssociatedMBeans",
dependentAMap.size() == 1 &&
dependentASet.equals(singleton(moduleB)));
Map<String,List<String>> refRels =
relSvc.findReferencingRelations(moduleA, "DependsOn", "dependent");
List<String> refRoles =
checked(refRels.get("A-DependsOn-B"), String.class);
check("Map<String,List<String>> RelationService.findReferencingRelations",
refRoles.equals(singletonList("dependent")));
List<String> relsOfType = relSvc.findRelationsOfType("DependsOn");
relsOfType = checked(relsOfType, String.class);
check("List<String> RelationService.findRelationsOfType",
relsOfType.equals(singletonList("A-DependsOn-B")));
List<String> allRelIds = relSvc.getAllRelationIds();
allRelIds = checked(allRelIds, String.class);
check("List<String> RelationService.getAllRelationIds()",
allRelIds.equals(singletonList("A-DependsOn-B")));
List<String> allRelTypes = relSvc.getAllRelationTypeNames();
allRelTypes = checked(allRelTypes, String.class);
check("List<String> RelationService.getAllRelationTypeNames",
allRelTypes.equals(singletonList("DependsOn")));
Map<ObjectName,List<String>> refdMBeans =
relSvc.getReferencedMBeans("A-DependsOn-B");
check("Map<ObjectName,List<String>> RelationService.getReferencedMBeans",
refdMBeans.get(moduleA).equals(singletonList("dependent")) &&
refdMBeans.get(moduleB).equals(singletonList("dependedOn")));
List<ObjectName> roleContents =
checked(relSvc.getRole("A-DependsOn-B", "dependent"),
ObjectName.class);
check("List<ObjectName> RelationService.getRole",
roleContents.equals(singletonList(moduleA)));
RoleInfo roleInfoDependent =
relSvc.getRoleInfo("DependsOn", "dependent");
RoleInfo roleInfoDependedOn =
relSvc.getRoleInfo("DependsOn", "dependedOn");
List<RoleInfo> expectedRoleInfos =
Arrays.asList(new RoleInfo[] {roleInfoDependent, roleInfoDependedOn});
List<RoleInfo> roleInfos =
checked(relSvc.getRoleInfos("DependsOn"), RoleInfo.class);
check("List<RoleInfo> RelationService.getRoleInfos",
equalListContents(expectedRoleInfos, roleInfos));
RelationType relType =
new RelationTypeSupport("DependsOn", dependsOnRoles);
List<RoleInfo> relTypeRoleInfos =
checked(relType.getRoleInfos(), RoleInfo.class);
// Since there's no RoleInfo.equals and since the RelationTypeSupport
// constructor clones the RoleInfos passed to it, it's tricky to
// test equality here so we check type and size and have done with it
check("List<RoleInfo> RelationType.getRoleInfos",
relTypeRoleInfos.size() == 2);
MBeanServerNotificationFilter mbsnf =
new MBeanServerNotificationFilter();
mbsnf.enableObjectName(moduleA);
check("Vector<ObjectName> MBeanServerNotificationFilter." +
"getEnabledObjectNames",
mbsnf.getEnabledObjectNames().equals(Arrays.asList(moduleA)));
mbsnf.enableAllObjectNames();
mbsnf.disableObjectName(moduleB);
check("Vector<ObjectName> MBeanServerNotificationFilter." +
"getDisabledObjectNames",
mbsnf.getDisabledObjectNames().equals(Arrays.asList(moduleB)));
RelationService unusedRelSvc = new RelationService(false);
RelationNotification rn1 =
new RelationNotification(RelationNotification.RELATION_MBEAN_REMOVAL,
unusedRelSvc, 0L, 0L, "yo!",
"A-DependsOn-B", "DependsOn", null,
singletonList(moduleA));
List<ObjectName> toUnreg =
checked(rn1.getMBeansToUnregister(), ObjectName.class);
check("List<ObjectName> RelationNotification.getMBeansToUnregister",
toUnreg.equals(singletonList(moduleA)));
RelationNotification rn2 =
new RelationNotification(RelationNotification.RELATION_MBEAN_UPDATE,
unusedRelSvc, 0L, 0L, "yo!",
"A-DependsOn-B", "DependsOn", null,
"dependent", singletonList(moduleA),
singletonList(moduleB));
check("List<ObjectName> RelationNotification.getOldRoleValue",
checked(rn2.getOldRoleValue(), ObjectName.class)
.equals(singletonList(moduleB)));
check("List<ObjectName> RelationNotification.getNewRoleValue",
checked(rn2.getNewRoleValue(), ObjectName.class)
.equals(singletonList(moduleA)));
ObjectName timerName = new ObjectName(":type=timer");
mbs.registerMBean(new Timer(), timerName);
TimerMBean timer =
MBeanServerInvocationHandler.newProxyInstance(mbs,
timerName,
TimerMBean.class,
false);
Date doomsday = new Date(Long.MAX_VALUE);
int timer1 = timer.addNotification("one", "one", null, doomsday);
int timer2 = timer.addNotification("two", "two", null, doomsday);
Vector<Integer> idsOne = timer.getNotificationIDs("one");
check("Vector<Integer> TimerMBean.getNotificationIDs",
idsOne.equals(singletonList(timer1)));
Vector<Integer> allIds = timer.getAllNotificationIDs();
check("Vector<Integer> TimerMBean.getAllNotificationIDs",
equalListContents(allIds,
Arrays.asList(new Integer[]{timer1, timer2})));
// ADD NEW TEST CASES ABOVE THIS COMMENT
if (failures == 0)
System.out.println("All tests passed");
else {
System.out.println("TEST FAILURES: " + failures);
System.exit(1);
}
// DO NOT ADD NEW TEST CASES HERE, ADD THEM ABOVE THE PREVIOUS COMMENT
}
public static interface StupidMBean {
public int getFive();
}
public static class Stupid implements StupidMBean {
public int getFive() {
return 5;
}
}
public static class Module extends StandardMBean implements StupidMBean {
public Module() throws NotCompliantMBeanException {
super(StupidMBean.class);
}
public int getFive() {
return 5;
}
}
private static <E> List<E> singletonList(E value) {
return Collections.singletonList(value);
}
private static <E> Set<E> singleton(E value) {
return Collections.singleton(value);
}
private static <K,V> Map<K,V> singletonMap(K key, V value) {
return Collections.singletonMap(key, value);
}
private static <E> List<E> checked(List<E> c, Class<E> type) {
List<E> unchecked = new ArrayList<E>();
List<E> checked = Collections.checkedList(unchecked, type);
checked.addAll(c);
return Collections.checkedList(c, type);
}
private static <E> Set<E> checked(Set<E> c, Class<E> type) {
Set<E> unchecked = new HashSet<E>();
Set<E> checked = Collections.checkedSet(unchecked, type);
checked.addAll(c);
return Collections.checkedSet(c, type);
}
private static <K,V> Map<K,V> checked(Map<K,V> m,
Class<K> keyType,
Class<V> valueType) {
Map<K,V> unchecked = new HashMap<K,V>();
Map<K,V> checked = Collections.checkedMap(unchecked, keyType, valueType);
checked.putAll(m);
return Collections.checkedMap(m, keyType, valueType);
}
/* The fact that we have to call this method is a clear signal that
* the API says List where it means Set.
*/
private static <E> boolean equalListContents(List<E> l1, List<E> l2) {
return new HashSet<E>(l1).equals(new HashSet<E>(l2));
}
private static void check(String what, boolean cond) {
if (cond)
System.out.println("OK: " + what);
else {
System.out.println("FAILED: " + what);
failures++;
}
}
}
|