< prev index next >

application/tests/org.openjdk.jmc.rjmx.test/src/test/java/org/openjdk/jmc/rjmx/test/synthetic/SyntheticNotificationTest.java

Print this page




  66                 private Notification lastNotification;
  67 
  68                 @Override
  69                 public void handleNotification(Notification notification, Object handback) {
  70                         lastNotification = notification;
  71                         gotNotification = true;
  72                         synchronized (SyntheticNotificationTest.this) {
  73                                 SyntheticNotificationTest.this.notify();
  74                         }
  75                 }
  76 
  77                 public Notification getLastNotification() {
  78                         return lastNotification;
  79                 }
  80         }
  81 
  82         @Override
  83         @Before
  84         public void setUp() throws Exception {
  85                 super.setUp();
  86                 handle = getDefaultServer().connect("Test"); //$NON-NLS-1$
  87                 connection = handle.getServiceOrThrow(MBeanServerConnection.class);
  88                 gotNotification = false;
  89         }
  90 
  91         @Override
  92         public void tearDown() throws Exception {
  93                 handle.close();
  94         }
  95 
  96         /**
  97          * Tests that we can add synthetic notifications.
  98          */
  99         @Test
 100         public void testGetNotificationMetadata() throws InstanceNotFoundException, IntrospectionException,
 101                         MalformedObjectNameException, ReflectionException, NullPointerException, IOException {
 102                 MBeanInfo info = connection.getMBeanInfo(new ObjectName("org.openjdk.jmc.test:type=Test")); //$NON-NLS-1$
 103                 assertTrue(info.getNotifications().length > 0);
 104         }
 105 
 106         /**
 107          * Tests that we can have attributes AND notifications on the same synthetic.
 108          */
 109         @Test
 110         public void testCombinedMetadata() throws InstanceNotFoundException, IntrospectionException,
 111                         MalformedObjectNameException, ReflectionException, NullPointerException, IOException {
 112                 MBeanInfo info = connection.getMBeanInfo(new ObjectName("org.openjdk.jmc.test:type=Test")); //$NON-NLS-1$
 113                 assertTrue(info.getNotifications().length > 0);
 114                 assertTrue(info.getAttributes().length > 0);
 115         }
 116 
 117         /**
 118          * Tests that we can overload existing real MBean with notification and still get values.
 119          */
 120         @Test
 121         public void testOverloadMetadata() throws InstanceNotFoundException, IntrospectionException,
 122                         MalformedObjectNameException, ReflectionException, NullPointerException, IOException {
 123                 MBeanInfo info = connection.getMBeanInfo(new ObjectName("java.lang:type=ClassLoading")); //$NON-NLS-1$
 124                 assertTrue(info.getNotifications().length > 0);
 125                 assertTrue(info.getAttributes().length > 0);
 126         }
 127 
 128         /**
 129          * Tests that we can shadow an existing notification.
 130          */
 131         @Test
 132         public void testShadowMetadata() throws InstanceNotFoundException, IntrospectionException,
 133                         MalformedObjectNameException, ReflectionException, NullPointerException, IOException {
 134                 Assume.assumeTrue("FIXME: Shadowing does not work yet!", false); //$NON-NLS-1$
 135                 MBeanInfo info = connection.getMBeanInfo(new ObjectName("java.lang:type=Memory")); //$NON-NLS-1$
 136                 assertTrue(info.getNotifications().length > 0);
 137                 for (MBeanNotificationInfo notificationInfo : info.getNotifications()) {
 138                         if (notificationInfo.getName().equals("java.management.memory.collection.threshold.exceeded")) { //$NON-NLS-1$
 139                                 assertTrue("Failed to shadow description", notificationInfo.getDescription().contains("shadow")); //$NON-NLS-1$ //$NON-NLS-2$
 140                                 assertTrue("Got the wrong type:" + notificationInfo.getNotifTypes()[0], "int".equals(notificationInfo //$NON-NLS-1$ //$NON-NLS-2$
 141                                                 .getNotifTypes()[0]));
 142                         } else {
 143                                 assertTrue("Should NOT contain shadow!", !notificationInfo.getDescription().contains("shadow")); //$NON-NLS-1$ //$NON-NLS-2$
 144                                 assertTrue("Should not be int!", !"int".equals(notificationInfo.getNotifTypes()[0])); //$NON-NLS-1$ //$NON-NLS-2$
 145                         }
 146                 }
 147         }
 148 
 149         @Test
 150         public void testNotificationListener() throws InstanceNotFoundException, MalformedObjectNameException,
 151                         NullPointerException, IOException, InterruptedException, ListenerNotFoundException {
 152                 Notification notif = null;
 153                 SyntheticNotificationListener listener = new SyntheticNotificationListener();
 154                 ObjectName testMBean = new ObjectName("org.openjdk.jmc.test:type=Test"); //$NON-NLS-1$
 155                 connection.addNotificationListener(testMBean, listener, null, null);
 156                 synchronized (this) {
 157                         this.wait(30000);
 158                         notif = listener.getLastNotification();
 159                 }
 160                 assertTrue("Never got any notification!", gotNotification); //$NON-NLS-1$
 161                 assertNotNull(notif);
 162                 assertTrue("Expected a user data > 0!", ((Integer) notif.getUserData()) > 0); //$NON-NLS-1$
 163                 assertTrue("Expected Woho!", notif.getMessage().startsWith("Woho!")); //$NON-NLS-1$ //$NON-NLS-2$
 164                 connection.removeNotificationListener(testMBean, listener, null, null);
 165         }
 166 }


  66                 private Notification lastNotification;
  67 
  68                 @Override
  69                 public void handleNotification(Notification notification, Object handback) {
  70                         lastNotification = notification;
  71                         gotNotification = true;
  72                         synchronized (SyntheticNotificationTest.this) {
  73                                 SyntheticNotificationTest.this.notify();
  74                         }
  75                 }
  76 
  77                 public Notification getLastNotification() {
  78                         return lastNotification;
  79                 }
  80         }
  81 
  82         @Override
  83         @Before
  84         public void setUp() throws Exception {
  85                 super.setUp();
  86                 handle = getDefaultServer().connect("Test");
  87                 connection = handle.getServiceOrThrow(MBeanServerConnection.class);
  88                 gotNotification = false;
  89         }
  90 
  91         @Override
  92         public void tearDown() throws Exception {
  93                 handle.close();
  94         }
  95 
  96         /**
  97          * Tests that we can add synthetic notifications.
  98          */
  99         @Test
 100         public void testGetNotificationMetadata() throws InstanceNotFoundException, IntrospectionException,
 101                         MalformedObjectNameException, ReflectionException, NullPointerException, IOException {
 102                 MBeanInfo info = connection.getMBeanInfo(new ObjectName("org.openjdk.jmc.test:type=Test"));
 103                 assertTrue(info.getNotifications().length > 0);
 104         }
 105 
 106         /**
 107          * Tests that we can have attributes AND notifications on the same synthetic.
 108          */
 109         @Test
 110         public void testCombinedMetadata() throws InstanceNotFoundException, IntrospectionException,
 111                         MalformedObjectNameException, ReflectionException, NullPointerException, IOException {
 112                 MBeanInfo info = connection.getMBeanInfo(new ObjectName("org.openjdk.jmc.test:type=Test"));
 113                 assertTrue(info.getNotifications().length > 0);
 114                 assertTrue(info.getAttributes().length > 0);
 115         }
 116 
 117         /**
 118          * Tests that we can overload existing real MBean with notification and still get values.
 119          */
 120         @Test
 121         public void testOverloadMetadata() throws InstanceNotFoundException, IntrospectionException,
 122                         MalformedObjectNameException, ReflectionException, NullPointerException, IOException {
 123                 MBeanInfo info = connection.getMBeanInfo(new ObjectName("java.lang:type=ClassLoading"));
 124                 assertTrue(info.getNotifications().length > 0);
 125                 assertTrue(info.getAttributes().length > 0);
 126         }
 127 
 128         /**
 129          * Tests that we can shadow an existing notification.
 130          */
 131         @Test
 132         public void testShadowMetadata() throws InstanceNotFoundException, IntrospectionException,
 133                         MalformedObjectNameException, ReflectionException, NullPointerException, IOException {
 134                 Assume.assumeTrue("FIXME: Shadowing does not work yet!", false);
 135                 MBeanInfo info = connection.getMBeanInfo(new ObjectName("java.lang:type=Memory"));
 136                 assertTrue(info.getNotifications().length > 0);
 137                 for (MBeanNotificationInfo notificationInfo : info.getNotifications()) {
 138                         if (notificationInfo.getName().equals("java.management.memory.collection.threshold.exceeded")) {
 139                                 assertTrue("Failed to shadow description", notificationInfo.getDescription().contains("shadow"));
 140                                 assertTrue("Got the wrong type:" + notificationInfo.getNotifTypes()[0], "int".equals(notificationInfo
 141                                                 .getNotifTypes()[0]));
 142                         } else {
 143                                 assertTrue("Should NOT contain shadow!", !notificationInfo.getDescription().contains("shadow"));
 144                                 assertTrue("Should not be int!", !"int".equals(notificationInfo.getNotifTypes()[0]));
 145                         }
 146                 }
 147         }
 148 
 149         @Test
 150         public void testNotificationListener() throws InstanceNotFoundException, MalformedObjectNameException,
 151                         NullPointerException, IOException, InterruptedException, ListenerNotFoundException {
 152                 Notification notif = null;
 153                 SyntheticNotificationListener listener = new SyntheticNotificationListener();
 154                 ObjectName testMBean = new ObjectName("org.openjdk.jmc.test:type=Test");
 155                 connection.addNotificationListener(testMBean, listener, null, null);
 156                 synchronized (this) {
 157                         this.wait(30000);
 158                         notif = listener.getLastNotification();
 159                 }
 160                 assertTrue("Never got any notification!", gotNotification);
 161                 assertNotNull(notif);
 162                 assertTrue("Expected a user data > 0!", ((Integer) notif.getUserData()) > 0);
 163                 assertTrue("Expected Woho!", notif.getMessage().startsWith("Woho!"));
 164                 connection.removeNotificationListener(testMBean, listener, null, null);
 165         }
 166 }
< prev index next >