1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4716675
  27  * @summary Test that relation type checking uses isInstanceOf
  28  * @author Eamonn McManus
  29  *
  30  * @run clean RelationTypeTest
  31  * @run build RelationTypeTest
  32  * @run main RelationTypeTest
  33  */
  34 
  35 import java.util.*;
  36 import javax.management.*;
  37 import javax.management.relation.*;
  38 
  39 public class RelationTypeTest {
  40     public static void main(String[] args) throws Exception {
  41         System.out.println("Test that relation type checking uses " +
  42                            "isInstanceOf");
  43         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
  44         ObjectName relSvc = new ObjectName("a:type=relationService");
  45         mbs.createMBean("javax.management.relation.RelationService",
  46                         relSvc,
  47                         new Object[] {Boolean.TRUE},
  48                         new String[] {"boolean"});
  49         RoleInfo leftInfo =
  50             new RoleInfo("left", "javax.management.timer.TimerMBean");
  51         RoleInfo rightInfo =
  52             new RoleInfo("right", "javax.management.timer.Timer");
  53         mbs.invoke(relSvc, "createRelationType",
  54                    new Object[] {
  55                         "typeName",
  56                         new RoleInfo[] {leftInfo, rightInfo},
  57                    },
  58                    new String[] {
  59                        "java.lang.String",
  60                        "[Ljavax.management.relation.RoleInfo;",
  61                    });
  62         ObjectName timer1 = new ObjectName("a:type=timer,number=1");
  63         ObjectName timer2 = new ObjectName("a:type=timer,number=2");
  64         mbs.createMBean("javax.management.timer.Timer", timer1);
  65         mbs.createMBean("javax.management.timer.Timer", timer2);
  66         Role leftRole =
  67             new Role("left", Arrays.asList(new ObjectName[] {timer1}));
  68         Role rightRole =
  69             new Role("right", Arrays.asList(new ObjectName[] {timer2}));
  70         RoleList roles =
  71             new RoleList(Arrays.asList(new Role[] {leftRole, rightRole}));
  72         mbs.invoke(relSvc, "createRelation",
  73                    new Object[] {
  74                         "relationName",
  75                         "typeName",
  76                         roles,
  77                    },
  78                    new String[] {
  79                        "java.lang.String",
  80                        "java.lang.String",
  81                        "javax.management.relation.RoleList",
  82                    });
  83         Map assoc =
  84             (Map) mbs.invoke(relSvc, "findAssociatedMBeans",
  85                              new Object[] {
  86                                  timer1,
  87                                  "typeName",
  88                                  "left",
  89                              },
  90                              new String[] {
  91                                  "javax.management.ObjectName",
  92                                  "java.lang.String",
  93                                  "java.lang.String",
  94                              });
  95 
  96         if (assoc.size() != 1) {
  97             System.out.println("TEST FAILS: findAssociatedMBeans should " +
  98                                "return one association: " + assoc);
  99             System.exit(1);
 100         }
 101 
 102         ArrayList list = (ArrayList) assoc.get(timer2);
 103         if (list.size() != 1 || !list.get(0).equals("relationName")) {
 104             System.out.println("TEST FAILS: MBean not associated as " +
 105                                "expected: " + list);
 106             System.exit(1);
 107         }
 108         System.out.println("Test passes");
 109     }
 110 }