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 4848474
  27  * @summary Test that relation service doesn't require List params to be ArrayList
  28  * @author Eamonn McManus
  29  *
  30  * @run clean NonArrayListTest
  31  * @run build NonArrayListTest
  32  * @run main NonArrayListTest
  33  */
  34 
  35 import java.util.*;
  36 import javax.management.*;
  37 import javax.management.relation.*;
  38 import javax.management.loading.MLet;
  39 
  40 public class NonArrayListTest {
  41     public static void main(String[] args) throws Exception {
  42         MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  43         RelationService rs = new RelationService(true);
  44         ObjectName rsName = new ObjectName("r:type=RelationService");
  45         mbs.registerMBean(rs, rsName);
  46         RelationServiceMBean rsProxy = (RelationServiceMBean)
  47             MBeanServerInvocationHandler.newProxyInstance(mbs,
  48                                                           rsName,
  49                                                           RelationServiceMBean.class,
  50                                                           false);
  51 
  52         ObjectName mlet1Name = new ObjectName("r:type=MLet,instance=1");
  53         ObjectName mlet2Name = new ObjectName("r:type=MLet,instance=2");
  54         mbs.createMBean(MLet.class.getName(), mlet1Name);
  55         mbs.createMBean(MLet.class.getName(), mlet2Name);
  56 
  57         RoleInfo leftRoleInfo = new RoleInfo("left", MLet.class.getName());
  58         RoleInfo rightRoleInfo = new RoleInfo("right", MLet.class.getName());
  59 
  60         ArrayList leftRoleValues =
  61             new ArrayList(Arrays.asList(new ObjectName[] {mlet1Name}));
  62         ArrayList rightRoleValues =
  63             new ArrayList(Arrays.asList(new ObjectName[] {mlet2Name}));
  64         Role leftRole = new Role("left", leftRoleValues);
  65         Role rightRole = new Role("right", rightRoleValues);
  66 
  67         RelationType leftRightType =
  68             new RelationTypeSupport("leftRight",
  69                                     new RoleInfo[] {leftRoleInfo,
  70                                                     rightRoleInfo});
  71         RoleList roleList =
  72             new RoleList(new ArrayList(Arrays.asList(new Role[] {
  73                 leftRole, rightRole,
  74             })));
  75         rsProxy.addRelationType(leftRightType);
  76         rsProxy.createRelation("relId", "leftRight", roleList);
  77 
  78         boolean ok = true;
  79         ObjectName oname = new ObjectName("a:b=c");
  80         List onameList =
  81             new Vector(Arrays.asList(new ObjectName[] {oname}));
  82 
  83         String testName;
  84 
  85         testName = "RelationNotification constructor with only 9 arguments";
  86         try {
  87             RelationNotification notif =
  88                 new RelationNotification(RelationNotification.RELATION_BASIC_CREATION,
  89                                          rs, // theSrcObj
  90                                          0L, // TheSeqNbr
  91                                          0L, // theTimeStamp
  92                                          "theMsg",
  93                                          "theRelId",
  94                                          "theRelTypeName",
  95                                          oname,
  96                                          onameList);
  97             System.out.println("OK: " + testName);
  98         } catch (Exception e) {
  99             System.err.println("Exception for " + testName);
 100             e.printStackTrace();
 101             ok = false;
 102         }
 103 
 104         testName = "RelationNotification constructor with 11 arguments";
 105         try {
 106             RelationNotification notif =
 107                 new RelationNotification(RelationNotification.RELATION_BASIC_UPDATE,
 108                                          rs, // theSrcObj
 109                                          0L, // TheSeqNbr
 110                                          0L, // theTimeStamp
 111                                          "theMsg",
 112                                          "theRelId",
 113                                          "theRelTypeName",
 114                                          oname,
 115                                          "theRoleName",
 116                                          onameList,
 117                                          onameList);
 118             System.out.println("OK: " + testName);
 119         } catch (Exception e) {
 120             System.err.println("Exception for " + testName);
 121             e.printStackTrace();
 122             ok = false;
 123         }
 124 
 125         testName = "RelationService.sendNotification";
 126         try {
 127             rsProxy.sendRoleUpdateNotification("relId", leftRole, onameList);
 128             System.out.println("OK: " + testName);
 129         } catch (Exception e) {
 130             System.err.println("Exception for " + testName);
 131             e.printStackTrace();
 132             ok = false;
 133         }
 134 
 135         testName = "RelationService.updateRoleMap";
 136         try {
 137             rsProxy.updateRoleMap("relId", leftRole, onameList);
 138             System.out.println("OK: " + testName);
 139         } catch (Exception e) {
 140             System.err.println("Exception for " + testName);
 141             e.printStackTrace();
 142             ok = false;
 143         }
 144 
 145         if (ok)
 146             System.out.println("Tests passed");
 147         else
 148             System.err.println("SOME TESTS FAILED");
 149     }
 150 }