278N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
278N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
278N/A *
278N/A * This code is free software; you can redistribute it and/or modify it
278N/A * under the terms of the GNU General Public License version 2 only, as
278N/A * published by the Free Software Foundation.
278N/A *
278N/A * This code is distributed in the hope that it will be useful, but WITHOUT
278N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
278N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
278N/A * version 2 for more details (a copy is included in the LICENSE file that
278N/A * accompanied this code).
278N/A *
278N/A * You should have received a copy of the GNU General Public License version
278N/A * 2 along with this work; if not, write to the Free Software Foundation,
278N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
278N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
278N/A */
278N/A
278N/A/*
278N/A * @test
278N/A * @bug 6701459
278N/A * @summary Test sequence numbers in RelationService notifications.
278N/A * @author Eamonn McManus
278N/A */
278N/A
278N/A/*
278N/A * Bug 6701459 is for a synchronization problem that is very unlikely to occur
278N/A * in practice and it would be very hard to test it. Instead we just check that
278N/A * the fix has not introduced any obviously-wrong behavior in the sequence
278N/A * numbers.
278N/A */
278N/A
278N/Aimport java.util.Arrays;
278N/Aimport java.util.concurrent.ArrayBlockingQueue;
278N/Aimport java.util.concurrent.BlockingQueue;
278N/Aimport javax.management.JMX;
278N/Aimport javax.management.MBeanServer;
278N/Aimport javax.management.MBeanServerFactory;
278N/Aimport javax.management.Notification;
278N/Aimport javax.management.NotificationListener;
278N/Aimport javax.management.ObjectName;
278N/Aimport javax.management.relation.RelationServiceMBean;
278N/Aimport javax.management.relation.Role;
278N/Aimport javax.management.relation.RoleInfo;
278N/Aimport javax.management.relation.RoleList;
278N/A
278N/Apublic class RelationNotificationSeqNoTest {
278N/A public static void main(String[] args) throws Exception {
278N/A MBeanServer mbs = MBeanServerFactory.newMBeanServer();
278N/A ObjectName relSvcName = new ObjectName("a:type=relationService");
278N/A RelationServiceMBean relSvc =
278N/A JMX.newMBeanProxy(mbs, relSvcName, RelationServiceMBean.class);
278N/A mbs.createMBean("javax.management.relation.RelationService",
278N/A relSvcName,
278N/A new Object[] {Boolean.TRUE},
278N/A new String[] {"boolean"});
278N/A
278N/A final BlockingQueue<Notification> q =
278N/A new ArrayBlockingQueue<Notification>(100);
278N/A NotificationListener qListener = new NotificationListener() {
278N/A public void handleNotification(Notification notification,
278N/A Object handback) {
278N/A q.add(notification);
278N/A }
278N/A };
278N/A mbs.addNotificationListener(relSvcName, qListener, null, null);
278N/A
278N/A RoleInfo leftInfo =
278N/A new RoleInfo("left", "javax.management.timer.TimerMBean");
278N/A RoleInfo rightInfo =
278N/A new RoleInfo("right", "javax.management.timer.Timer");
278N/A relSvc.createRelationType("typeName", new RoleInfo[] {leftInfo, rightInfo});
278N/A ObjectName timer1 = new ObjectName("a:type=timer,number=1");
278N/A ObjectName timer2 = new ObjectName("a:type=timer,number=2");
278N/A mbs.createMBean("javax.management.timer.Timer", timer1);
278N/A mbs.createMBean("javax.management.timer.Timer", timer2);
278N/A
278N/A Role leftRole =
278N/A new Role("left", Arrays.asList(new ObjectName[] {timer1}));
278N/A Role rightRole =
278N/A new Role("right", Arrays.asList(new ObjectName[] {timer2}));
278N/A RoleList roles =
278N/A new RoleList(Arrays.asList(new Role[] {leftRole, rightRole}));
278N/A
278N/A final int NREPEAT = 10;
278N/A
278N/A for (int i = 0; i < NREPEAT; i++) {
278N/A relSvc.createRelation("relationName", "typeName", roles);
278N/A relSvc.removeRelation("relationName");
278N/A }
278N/A
278N/A Notification firstNotif = q.remove();
278N/A long seqNo = firstNotif.getSequenceNumber();
278N/A for (int i = 0; i < NREPEAT * 2 - 1; i++) {
278N/A Notification n = q.remove();
278N/A long nSeqNo = n.getSequenceNumber();
278N/A if (nSeqNo != seqNo + 1) {
278N/A throw new Exception(
278N/A "TEST FAILED: expected seqNo " + (seqNo + 1) + "; got " +
278N/A nSeqNo);
278N/A }
278N/A seqNo++;
278N/A }
278N/A System.out.println("TEST PASSED: got " + (NREPEAT * 2) + " notifications " +
278N/A "with contiguous sequence numbers");
278N/A }
278N/A}