7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
authorjbachorik
Thu, 21 Aug 2014 15:22:07 +0200
changeset 26208 a581ee8890b1
parent 26207 a02f6165d5be
child 26209 f7860e8501d8
7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22 Reviewed-by: dfuchs, sjiang
jdk/test/javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java
--- a/jdk/test/javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java	Tue Aug 26 10:19:56 2014 +0100
+++ b/jdk/test/javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java	Thu Aug 21 15:22:07 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -37,6 +37,8 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.Semaphore;
 import javax.management.MBeanServer;
 import javax.management.MBeanServerConnection;
 import javax.management.MBeanServerFactory;
@@ -56,10 +58,12 @@
 public class NotificationAccessControllerTest {
 
     public class NAC implements NotificationAccessController {
-        private boolean throwException;
+        private final boolean throwException;
         public NAC(boolean throwException) {
             this.throwException = throwException;
         }
+
+        @Override
         public void addNotificationListener(
             String connectionId,
             ObjectName name,
@@ -73,9 +77,13 @@
             if (throwException)
                 if (name.getCanonicalName().equals("domain:name=1,type=NB")
                     &&
+                    subject != null
+                    &&
                     subject.getPrincipals().contains(new JMXPrincipal("role")))
                     throw new SecurityException();
         }
+
+        @Override
         public void removeNotificationListener(
             String connectionId,
             ObjectName name,
@@ -89,9 +97,13 @@
             if (throwException)
                 if (name.getCanonicalName().equals("domain:name=2,type=NB")
                     &&
+                    subject != null
+                    &&
                     subject.getPrincipals().contains(new JMXPrincipal("role")))
                     throw new SecurityException();
         }
+
+        @Override
         public void fetchNotification(
             String connectionId,
             ObjectName name,
@@ -105,13 +117,17 @@
             echo("\tsubject: " +
                  (subject == null ? null : subject.getPrincipals()));
             if (!throwException)
-                if (name.getCanonicalName().equals("domain:name=2,type=NB") &&
+                if (name.getCanonicalName().equals("domain:name=2,type=NB")
+                    &&
+                    subject != null
+                    &&
                     subject.getPrincipals().contains(new JMXPrincipal("role")))
                     throw new SecurityException();
         }
     }
 
     public class CustomJMXAuthenticator implements JMXAuthenticator {
+        @Override
         public Subject authenticate(Object credentials) {
             String role = ((String[]) credentials)[0];
             echo("\nCreate principal with name = " + role);
@@ -129,6 +145,7 @@
     public static class NB
         extends NotificationBroadcasterSupport
         implements NBMBean {
+        @Override
         public void emitNotification(int seqnum, ObjectName name) {
             if (name == null) {
                 sendNotification(new Notification("nb", this, seqnum));
@@ -139,13 +156,20 @@
     }
 
     public class Listener implements NotificationListener {
-        public List<Notification> notifs = new ArrayList<Notification>();
+        public final List<Notification> notifs = new CopyOnWriteArrayList<>();
+
+        private final Semaphore s;
+        public Listener(Semaphore s) {
+            this.s = s;
+        }
+        @Override
         public void handleNotification(Notification n, Object h) {
             echo("handleNotification:");
             echo("\tNotification = " + n);
             echo("\tNotification.SeqNum = " + n.getSequenceNumber());
             echo("\tHandback = " + h);
             notifs.add(n);
+            s.release();
         }
     }
 
@@ -192,6 +216,17 @@
         JMXConnectorServer server = null;
         JMXConnector client = null;
 
+        /*
+        * (!enableChecks)
+        * - List must contain three notifs from sources nb1, nb2 and nb3
+        * (enableChecks && !throwException)
+        * - List must contain one notif from source nb1
+        * (enableChecks && throwException)
+        * - List must contain two notifs from sources nb2 and nb3
+        */
+        final int expected_notifs =
+            (!enableChecks ? 3 : (throwException ? 2 : 1));
+
         // Create a new MBeanServer
         //
         final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
@@ -199,7 +234,7 @@
         try {
             // Create server environment map
             //
-            final Map<String,Object> env = new HashMap<String,Object>();
+            final Map<String,Object> env = new HashMap<>();
             env.put("jmx.remote.authenticator", new CustomJMXAuthenticator());
             if (enableChecks) {
                 env.put("com.sun.jmx.remote.notification.access.controller",
@@ -222,7 +257,7 @@
 
             // Create server environment map
             //
-            final Map<String,Object> cenv = new HashMap<String,Object>();
+            final Map<String,Object> cenv = new HashMap<>();
             String[] credentials = new String[] { "role" , "password" };
             cenv.put("jmx.remote.credentials", credentials);
 
@@ -246,7 +281,9 @@
 
             // Add notification listener
             //
-            Listener li = new Listener();
+            Semaphore s = new Semaphore(0);
+
+            Listener li = new Listener(s);
             try {
                 mbsc.addNotificationListener(nb1, li, null, null);
                 if (enableChecks && throwException) {
@@ -263,6 +300,9 @@
             }
             mbsc.addNotificationListener(nb2, li, null, null);
 
+            System.out.println("\n+++ Expecting to receive " + expected_notifs +
+                               " notification" + (expected_notifs > 1 ? "s" : "") +
+                               " +++");
             // Invoke the "sendNotification" method
             //
             mbsc.invoke(nb1, "emitNotification",
@@ -277,7 +317,7 @@
 
             // Wait for notifications to be emitted
             //
-            Thread.sleep(2000);
+            s.acquire(expected_notifs);
 
             // Remove notification listener
             //
@@ -303,21 +343,7 @@
             sources.add(nb1);
             sources.add(nb2);
             sources.add(nb3);
-            if (!enableChecks) {
-                // List must contain three notifs from sources nb1, nb2 and nb3
-                //
-                result = checkNotifs(3, li.notifs, sources);
-            }
-            if (enableChecks && !throwException) {
-                // List must contain one notif from source nb1
-                //
-                result = checkNotifs(1, li.notifs, sources);
-            }
-            if (enableChecks && throwException) {
-                // List must contain two notifs from sources nb2 and nb3
-                //
-                result = checkNotifs(2, li.notifs, sources);
-            }
+            result = checkNotifs(expected_notifs, li.notifs, sources);
             if (result > 0) {
                 return result;
             }