Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/generified/GenericTest.java
38838 views
/*1* Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4847959 619140226* @summary Test newly-generified APIs27* @author Eamonn McManus28* @run clean GenericTest29* @run build GenericTest30* @run main GenericTest31*/3233import java.lang.management.ManagementFactory;34import java.lang.reflect.*;35import java.util.*;36import javax.management.*;37import javax.management.openmbean.*;38import javax.management.relation.*;39import javax.management.timer.Timer;40import javax.management.timer.TimerMBean;4142public class GenericTest {43private static int failures;4445public static void main(String[] args) throws Exception {46MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();4748// Check we are really using the generified version49boolean generic;50Method findmbs = MBeanServerFactory.class.getMethod("findMBeanServer",51String.class);52Type findmbstype = findmbs.getGenericReturnType();53if (!(findmbstype instanceof ParameterizedType)) {54System.out.println("FAILURE: API NOT GENERIC!");55System.out.println(" MBeanServerFactory.findMBeanServer -> " +56findmbstype);57failures++;58generic = false;59} else {60System.out.println("OK: this API is generic");61generic = true;62}6364ArrayList<MBeanServer> mbsList1 =65MBeanServerFactory.findMBeanServer(null);66checked(mbsList1, MBeanServer.class);67ArrayList mbsList2 = MBeanServerFactory.findMBeanServer(null);68check("ArrayList<MBeanServer> findMBeanServer", mbsList1.size() == 1);69check("ArrayList findMBeanServer", mbsList1.equals(mbsList2));7071Set<ObjectName> names1 =72checked(mbs.queryNames(null, null), ObjectName.class);73Set names2 = mbs.queryNames(null, null);74Set<ObjectName> names3 =75checked(((MBeanServerConnection) mbs).queryNames(null, null),76ObjectName.class);77check("Set<ObjectName> MBeanServer.queryNames", names1.size() >= 1);78check("Set MBeanServer.queryNames", names2.size() >= 1);79check("Set<ObjectName> MBeanServerConnection.queryNames",80names3.size() >= 1);81check("queryNames sets same",82names1.equals(names2) && names2.equals(names3));8384Set<ObjectInstance> mbeans1 =85checked(mbs.queryMBeans(null, null), ObjectInstance.class);86Set mbeans2 = mbs.queryMBeans(null, null);87Set<ObjectInstance> mbeans3 =88checked(((MBeanServerConnection) mbs).queryMBeans(null, null),89ObjectInstance.class);90check("Set<ObjectInstsance> MBeanServer.queryMBeans",91mbeans1.size() >= 1);92check("Set MBeanServer.queryMBeans", mbeans2.size() >= 1);93check("Set<ObjectInstsance> MBeanServerConnection.queryMBeans",94mbeans3.size() >= 1);95check("queryMBeans sets same",96mbeans1.equals(mbeans2) && mbeans2.equals(mbeans3));979899AttributeChangeNotificationFilter acnf =100new AttributeChangeNotificationFilter();101acnf.enableAttribute("foo");102Vector<String> acnfs = acnf.getEnabledAttributes();103checked(acnfs, String.class);104check("Vector<String> AttributeChangeNotificationFilter.getEnabled" +105"Attributes", acnfs.equals(Arrays.asList(new String[] {"foo"})));106107if (generic) {108Attribute a = new Attribute("foo", "bar");109AttributeList al1 = new AttributeList();110al1.add(a);111AttributeList al2 =112new AttributeList(Arrays.asList(new Attribute[] {a}));113check("new AttributeList(List<Attribute>)", al1.equals(al2));114List<Attribute> al3 = checked(al1.asList(), Attribute.class);115al3.remove(a);116check("List<Attribute> AttributeList.asList()",117al1.equals(al3) && al1.isEmpty());118}119120List<ObjectName> namelist1 = new ArrayList<ObjectName>(names1);121Role role = new Role("rolename", namelist1);122List<ObjectName> namelist2 =123checked(role.getRoleValue(), ObjectName.class);124check("new Role(String,List<ObjectName>).getRoleValue() -> " +125"List<ObjectName>", namelist1.equals(namelist2));126127RoleList rl1 = new RoleList();128rl1.add(role);129RoleList rl2 = new RoleList(Arrays.asList(new Role[] {role}));130check("new RoleList(List<Role>)", rl1.equals(rl2));131if (generic) {132List<Role> rl3 = checked(rl1.asList(), Role.class);133rl3.remove(role);134check("List<Role> RoleList.asList()",135rl1.equals(rl3) && rl1.isEmpty());136}137138RoleUnresolved ru =139new RoleUnresolved("rolename", namelist1,140RoleStatus.LESS_THAN_MIN_ROLE_DEGREE);141List<ObjectName> namelist3 =142checked(ru.getRoleValue(), ObjectName.class);143check("new RoleUnresolved(...List<ObjectName>...).getRoleValue() -> " +144"List<ObjectName>", namelist1.equals(namelist3));145146RoleUnresolvedList rul1 = new RoleUnresolvedList();147rul1.add(ru);148RoleUnresolvedList rul2 =149new RoleUnresolvedList(Arrays.asList(new RoleUnresolved[] {ru}));150check("new RoleUnresolvedList(List<RoleUnresolved>", rul1.equals(rul2));151if (generic) {152List<RoleUnresolved> rul3 =153checked(rul1.asList(), RoleUnresolved.class);154rul3.remove(ru);155check("List<RoleUnresolved> RoleUnresolvedList.asList()",156rul1.equals(rul3) && rul1.isEmpty());157}158159// This case basically just tests that we can compile this sort of thing160OpenMBeanAttributeInfo ombai1 =161new OpenMBeanAttributeInfoSupport("a", "a descr",162SimpleType.INTEGER,163true, true, false);164CompositeType ct =165new CompositeType("ct", "ct descr", new String[] {"item1"},166new String[] {"item1 descr"},167new OpenType[] {SimpleType.INTEGER});168OpenMBeanAttributeInfo ombai2 =169new OpenMBeanAttributeInfoSupport("a", "a descr",170ct, true, true, false);171TabularType tt =172new TabularType("tt", "tt descr", ct, new String[] {"item1"});173OpenMBeanAttributeInfo ombai3 =174new OpenMBeanAttributeInfoSupport("a", "a descr",175tt, true, true, false);176ArrayType<String[][]> at =177new ArrayType<String[][]>(2, SimpleType.STRING);178OpenMBeanAttributeInfo ombai4 =179new OpenMBeanAttributeInfoSupport("a", "a descr",180at, true, true, false);181OpenMBeanAttributeInfo ombai4a =182new OpenMBeanAttributeInfoSupport("a", "a descr",183(ArrayType) at,184true, true, false);185OpenMBeanAttributeInfo ombai5 =186new OpenMBeanAttributeInfoSupport("a", "a descr",187SimpleType.INTEGER,188true, true, false,1895, 1, 9);190OpenMBeanAttributeInfo ombai6 =191new OpenMBeanAttributeInfoSupport("a", "a descr",192SimpleType.INTEGER,193true, true, false,1945, new Integer[] {1, 5});195196OpenMBeanInfo ombi =197new OpenMBeanInfoSupport("a.a", "a.a descr",198new OpenMBeanAttributeInfo[] {199ombai1, ombai2, ombai3, ombai4,200ombai5, ombai6,201},202null, null, null);203204Map<String,Integer> itemMap =205checked(singletonMap("item1", 5),206String.class, Integer.class);207CompositeData cd =208new CompositeDataSupport(ct, itemMap);209check("CompositeDataSupport(CompositeType, Map<String,?>",210cd.get("item1").equals(5));211212Set<String> ctkeys = checked(ct.keySet(), String.class);213check("Set<String> CompositeType.keySet()",214ctkeys.equals(singleton("item1")));215216List<String> ttindex = checked(tt.getIndexNames(), String.class);217check("Set<String> TabularType.getIndexNames()",218ttindex.equals(singletonList("item1")));219220TabularData td = new TabularDataSupport(tt);221td.putAll(new CompositeData[] {cd});222List<Integer> tdkey = checked(singletonList(5), Integer.class);223Set<List<Integer>> tdkeys = checked(singleton(tdkey),224(Class<List<Integer>>) tdkey.getClass());225Collection<CompositeData> tdvalues = checked(singleton(cd),226CompositeData.class);227check("Set<List<?>> TabularDataSupport.keySet()",228td.keySet().equals(tdkeys));229check("Collection<CompositeData> TabularDataSupport.values()",230td.values().iterator().next().equals(tdvalues.iterator().next()));231232ObjectName stupidName = new ObjectName("stupid:a=b");233mbs.registerMBean(new Stupid(), stupidName);234StupidMBean proxy =235MBeanServerInvocationHandler.newProxyInstance(mbs,236stupidName,237StupidMBean.class,238false);239check("MBeanServerInvocationHandler.newProxyInstance",240proxy.getFive() == 5);241mbs.unregisterMBean(stupidName);242243mbs.registerMBean(new StandardMBean(new Stupid(), StupidMBean.class),244stupidName);245check("<T> StandardMBean(T impl, Class<T> intf)",246proxy.getFive() == 5);247248// Following is based on the package.html for javax.management.relation249// Create the Relation Service MBean250ObjectName relSvcName = new ObjectName(":type=RelationService");251RelationService relSvcObject = new RelationService(true);252mbs.registerMBean(relSvcObject, relSvcName);253254// Create an MBean proxy for easier access to the Relation Service255RelationServiceMBean relSvc =256MBeanServerInvocationHandler.newProxyInstance(mbs, relSvcName,257RelationServiceMBean.class,258false);259260// Define the DependsOn relation type261RoleInfo[] dependsOnRoles = {262new RoleInfo("dependent", Module.class.getName()),263new RoleInfo("dependedOn", Module.class.getName())264};265relSvc.createRelationType("DependsOn", dependsOnRoles);266267// Now define a relation instance "moduleA DependsOn moduleB"268269ObjectName moduleA = new ObjectName(":type=Module,name=A");270ObjectName moduleB = new ObjectName(":type=Module,name=B");271272// Following two lines added to example:273mbs.registerMBean(new Module(), moduleA);274mbs.registerMBean(new Module(), moduleB);275276Role dependent = new Role("dependent", singletonList(moduleA));277Role dependedOn = new Role("dependedOn", singletonList(moduleB));278Role[] roleArray = {dependent, dependedOn};279RoleList roles = new RoleList(Arrays.asList(roleArray));280relSvc.createRelation("A-DependsOn-B", "DependsOn", roles);281282// Query the Relation Service to find what modules moduleA depends on283Map<ObjectName,List<String>> dependentAMap =284relSvc.findAssociatedMBeans(moduleA, "DependsOn", "dependent");285Set<ObjectName> dependentASet = dependentAMap.keySet();286dependentASet = checked(dependentASet, ObjectName.class);287// Set of ObjectName containing moduleB288check("Map<ObjectName,List<String>> RelationService.findAssociatedMBeans",289dependentAMap.size() == 1 &&290dependentASet.equals(singleton(moduleB)));291292Map<String,List<String>> refRels =293relSvc.findReferencingRelations(moduleA, "DependsOn", "dependent");294List<String> refRoles =295checked(refRels.get("A-DependsOn-B"), String.class);296check("Map<String,List<String>> RelationService.findReferencingRelations",297refRoles.equals(singletonList("dependent")));298299List<String> relsOfType = relSvc.findRelationsOfType("DependsOn");300relsOfType = checked(relsOfType, String.class);301check("List<String> RelationService.findRelationsOfType",302relsOfType.equals(singletonList("A-DependsOn-B")));303304List<String> allRelIds = relSvc.getAllRelationIds();305allRelIds = checked(allRelIds, String.class);306check("List<String> RelationService.getAllRelationIds()",307allRelIds.equals(singletonList("A-DependsOn-B")));308309List<String> allRelTypes = relSvc.getAllRelationTypeNames();310allRelTypes = checked(allRelTypes, String.class);311check("List<String> RelationService.getAllRelationTypeNames",312allRelTypes.equals(singletonList("DependsOn")));313314Map<ObjectName,List<String>> refdMBeans =315relSvc.getReferencedMBeans("A-DependsOn-B");316check("Map<ObjectName,List<String>> RelationService.getReferencedMBeans",317refdMBeans.get(moduleA).equals(singletonList("dependent")) &&318refdMBeans.get(moduleB).equals(singletonList("dependedOn")));319320List<ObjectName> roleContents =321checked(relSvc.getRole("A-DependsOn-B", "dependent"),322ObjectName.class);323check("List<ObjectName> RelationService.getRole",324roleContents.equals(singletonList(moduleA)));325326RoleInfo roleInfoDependent =327relSvc.getRoleInfo("DependsOn", "dependent");328RoleInfo roleInfoDependedOn =329relSvc.getRoleInfo("DependsOn", "dependedOn");330List<RoleInfo> expectedRoleInfos =331Arrays.asList(new RoleInfo[] {roleInfoDependent, roleInfoDependedOn});332List<RoleInfo> roleInfos =333checked(relSvc.getRoleInfos("DependsOn"), RoleInfo.class);334check("List<RoleInfo> RelationService.getRoleInfos",335equalListContents(expectedRoleInfos, roleInfos));336337RelationType relType =338new RelationTypeSupport("DependsOn", dependsOnRoles);339List<RoleInfo> relTypeRoleInfos =340checked(relType.getRoleInfos(), RoleInfo.class);341// Since there's no RoleInfo.equals and since the RelationTypeSupport342// constructor clones the RoleInfos passed to it, it's tricky to343// test equality here so we check type and size and have done with it344check("List<RoleInfo> RelationType.getRoleInfos",345relTypeRoleInfos.size() == 2);346347MBeanServerNotificationFilter mbsnf =348new MBeanServerNotificationFilter();349mbsnf.enableObjectName(moduleA);350check("Vector<ObjectName> MBeanServerNotificationFilter." +351"getEnabledObjectNames",352mbsnf.getEnabledObjectNames().equals(Arrays.asList(moduleA)));353mbsnf.enableAllObjectNames();354mbsnf.disableObjectName(moduleB);355check("Vector<ObjectName> MBeanServerNotificationFilter." +356"getDisabledObjectNames",357mbsnf.getDisabledObjectNames().equals(Arrays.asList(moduleB)));358359RelationService unusedRelSvc = new RelationService(false);360RelationNotification rn1 =361new RelationNotification(RelationNotification.RELATION_MBEAN_REMOVAL,362unusedRelSvc, 0L, 0L, "yo!",363"A-DependsOn-B", "DependsOn", null,364singletonList(moduleA));365List<ObjectName> toUnreg =366checked(rn1.getMBeansToUnregister(), ObjectName.class);367check("List<ObjectName> RelationNotification.getMBeansToUnregister",368toUnreg.equals(singletonList(moduleA)));369370RelationNotification rn2 =371new RelationNotification(RelationNotification.RELATION_MBEAN_UPDATE,372unusedRelSvc, 0L, 0L, "yo!",373"A-DependsOn-B", "DependsOn", null,374"dependent", singletonList(moduleA),375singletonList(moduleB));376check("List<ObjectName> RelationNotification.getOldRoleValue",377checked(rn2.getOldRoleValue(), ObjectName.class)378.equals(singletonList(moduleB)));379check("List<ObjectName> RelationNotification.getNewRoleValue",380checked(rn2.getNewRoleValue(), ObjectName.class)381.equals(singletonList(moduleA)));382383ObjectName timerName = new ObjectName(":type=timer");384mbs.registerMBean(new Timer(), timerName);385TimerMBean timer =386MBeanServerInvocationHandler.newProxyInstance(mbs,387timerName,388TimerMBean.class,389false);390Date doomsday = new Date(Long.MAX_VALUE);391int timer1 = timer.addNotification("one", "one", null, doomsday);392int timer2 = timer.addNotification("two", "two", null, doomsday);393Vector<Integer> idsOne = timer.getNotificationIDs("one");394check("Vector<Integer> TimerMBean.getNotificationIDs",395idsOne.equals(singletonList(timer1)));396Vector<Integer> allIds = timer.getAllNotificationIDs();397check("Vector<Integer> TimerMBean.getAllNotificationIDs",398equalListContents(allIds,399Arrays.asList(new Integer[]{timer1, timer2})));400401// ADD NEW TEST CASES ABOVE THIS COMMENT402403if (failures == 0)404System.out.println("All tests passed");405else {406System.out.println("TEST FAILURES: " + failures);407System.exit(1);408}409410// DO NOT ADD NEW TEST CASES HERE, ADD THEM ABOVE THE PREVIOUS COMMENT411}412413public static interface StupidMBean {414public int getFive();415}416417public static class Stupid implements StupidMBean {418public int getFive() {419return 5;420}421}422423public static class Module extends StandardMBean implements StupidMBean {424public Module() throws NotCompliantMBeanException {425super(StupidMBean.class);426}427428public int getFive() {429return 5;430}431}432433private static <E> List<E> singletonList(E value) {434return Collections.singletonList(value);435}436437private static <E> Set<E> singleton(E value) {438return Collections.singleton(value);439}440441private static <K,V> Map<K,V> singletonMap(K key, V value) {442return Collections.singletonMap(key, value);443}444445private static <E> List<E> checked(List<E> c, Class<E> type) {446List<E> unchecked = new ArrayList<E>();447List<E> checked = Collections.checkedList(unchecked, type);448checked.addAll(c);449return Collections.checkedList(c, type);450}451452private static <E> Set<E> checked(Set<E> c, Class<E> type) {453Set<E> unchecked = new HashSet<E>();454Set<E> checked = Collections.checkedSet(unchecked, type);455checked.addAll(c);456return Collections.checkedSet(c, type);457}458459private static <K,V> Map<K,V> checked(Map<K,V> m,460Class<K> keyType,461Class<V> valueType) {462Map<K,V> unchecked = new HashMap<K,V>();463Map<K,V> checked = Collections.checkedMap(unchecked, keyType, valueType);464checked.putAll(m);465return Collections.checkedMap(m, keyType, valueType);466}467468/* The fact that we have to call this method is a clear signal that469* the API says List where it means Set.470*/471private static <E> boolean equalListContents(List<E> l1, List<E> l2) {472return new HashSet<E>(l1).equals(new HashSet<E>(l2));473}474475private static void check(String what, boolean cond) {476if (cond)477System.out.println("OK: " + what);478else {479System.out.println("FAILED: " + what);480failures++;481}482}483}484485486