Path: blob/master/test/jdk/javax/management/relation/RelationNotificationSeqNoTest.java
41149 views
/*1* Copyright (c) 2005, 2015, 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 670145926* @summary Test sequence numbers in RelationService notifications.27* @author Eamonn McManus28*/2930/*31* Bug 6701459 is for a synchronization problem that is very unlikely to occur32* in practice and it would be very hard to test it. Instead we just check that33* the fix has not introduced any obviously-wrong behavior in the sequence34* numbers.35*/3637import java.util.Arrays;38import java.util.concurrent.ArrayBlockingQueue;39import java.util.concurrent.BlockingQueue;40import javax.management.JMX;41import javax.management.MBeanServer;42import javax.management.MBeanServerFactory;43import javax.management.Notification;44import javax.management.NotificationListener;45import javax.management.ObjectName;46import javax.management.relation.RelationServiceMBean;47import javax.management.relation.Role;48import javax.management.relation.RoleInfo;49import javax.management.relation.RoleList;5051public class RelationNotificationSeqNoTest {52public static void main(String[] args) throws Exception {53MBeanServer mbs = MBeanServerFactory.newMBeanServer();54ObjectName relSvcName = new ObjectName("a:type=relationService");55RelationServiceMBean relSvc =56JMX.newMBeanProxy(mbs, relSvcName, RelationServiceMBean.class);57mbs.createMBean("javax.management.relation.RelationService",58relSvcName,59new Object[] {Boolean.TRUE},60new String[] {"boolean"});6162final BlockingQueue<Notification> q =63new ArrayBlockingQueue<Notification>(100);64NotificationListener qListener = new NotificationListener() {65public void handleNotification(Notification notification,66Object handback) {67q.add(notification);68}69};70mbs.addNotificationListener(relSvcName, qListener, null, null);7172RoleInfo leftInfo =73new RoleInfo("left", "javax.management.timer.TimerMBean");74RoleInfo rightInfo =75new RoleInfo("right", "javax.management.timer.Timer");76relSvc.createRelationType("typeName", new RoleInfo[] {leftInfo, rightInfo});77ObjectName timer1 = new ObjectName("a:type=timer,number=1");78ObjectName timer2 = new ObjectName("a:type=timer,number=2");79mbs.createMBean("javax.management.timer.Timer", timer1);80mbs.createMBean("javax.management.timer.Timer", timer2);8182Role leftRole =83new Role("left", Arrays.asList(new ObjectName[] {timer1}));84Role rightRole =85new Role("right", Arrays.asList(new ObjectName[] {timer2}));86RoleList roles =87new RoleList(Arrays.asList(new Role[] {leftRole, rightRole}));8889final int NREPEAT = 10;9091for (int i = 0; i < NREPEAT; i++) {92relSvc.createRelation("relationName", "typeName", roles);93relSvc.removeRelation("relationName");94}9596Notification firstNotif = q.remove();97long seqNo = firstNotif.getSequenceNumber();98for (int i = 0; i < NREPEAT * 2 - 1; i++) {99Notification n = q.remove();100long nSeqNo = n.getSequenceNumber();101if (nSeqNo != seqNo + 1) {102throw new Exception(103"TEST FAILED: expected seqNo " + (seqNo + 1) + "; got " +104nSeqNo);105}106seqNo++;107}108System.out.println("TEST PASSED: got " + (NREPEAT * 2) + " notifications " +109"with contiguous sequence numbers");110}111}112113114