/** * {@inheritDoc} */ public void init(JmxSensorTypeConfig sensorTypeConfig) { this.sensorTypeConfig = sensorTypeConfig; // check for forcing server creation Map<String, Object> parameters = sensorTypeConfig.getParameters(); if (MapUtils.isNotEmpty(parameters)) { if (Boolean.TRUE.equals(parameters.get("forceMBeanServer"))) { // create only, get it via hook MBeanServer server = ManagementFactory.getPlatformMBeanServer(); mbeanServerAdded(server); } } }
@Test public void nullServer() { jmxSensor.mbeanServerAdded(null); verifyZeroInteractions(connection); }
@Test public void listenerRegistered() throws Exception { ArgumentCaptor<MBeanServerHolder> captor = ArgumentCaptor.forClass(MBeanServerHolder.class); jmxSensor.mbeanServerAdded(mBeanServer); verify(mBeanServer).addNotificationListener(Matchers.<ObjectName> any(), captor.capture(), Matchers.<NotificationFilter> any(), eq(null)); assertThat(captor.getValue().mBeanServer, is(mBeanServer)); }
@Test public void wrongNotification() throws Exception { jmxSensor.mbeanServerAdded(mBeanServer); ArgumentCaptor<MBeanServerHolder> notificationListener = ArgumentCaptor.forClass(MBeanServerHolder.class); verify(mBeanServer).addNotificationListener(Matchers.<ObjectName> any(), notificationListener.capture(), Matchers.<NotificationFilter> any(), eq(null)); Notification notification = mock(Notification.class); when(connection.isConnected()).thenReturn(true); notificationListener.getValue().handleNotification(notification, null); verifyZeroInteractions(mBeanServer); } }
@Test public void listenerUnregistered() throws Exception { ArgumentCaptor<MBeanServerHolder> captor = ArgumentCaptor.forClass(MBeanServerHolder.class); jmxSensor.mbeanServerAdded(mBeanServer); jmxSensor.mbeanServerRemoved(mBeanServer); verify(mBeanServer).removeNotificationListener(Matchers.<ObjectName> any(), captor.capture(), Matchers.<NotificationFilter> any(), eq(null)); assertThat(captor.getValue().mBeanServer, is(mBeanServer)); } }
@Test public void connectionOff() throws Exception { when(connection.isConnected()).thenReturn(false); jmxSensor.mbeanServerAdded(mBeanServer); verify(mBeanServer).addNotificationListener(Matchers.<ObjectName> any(), Matchers.<NotificationListener> any(), Matchers.<NotificationFilter> any(), eq(null)); verifyNoMoreInteractions(mBeanServer); }
@Test public void noClassCacheWarningShouldBeLoggedOnce() throws Exception { when(connection.isConnected()).thenReturn(true); BusinessException businessException = new BusinessException(AgentManagementErrorCodeEnum.AGENT_DOES_NOT_EXIST); doThrow(businessException).when(connection).analyzeJmxAttributes(anyLong(), Matchers.anyList()); jmxSensor.mbeanServerAdded(mBeanServer); verify(agentStatisticsLogger, times(1)).noClassCacheAvailable(); } }
when(connection.analyzeJmxAttributes(eq(platformIdent), Matchers.<Collection<JmxAttributeDescriptor>> any())).thenReturn(Collections.<JmxAttributeDescriptor> emptyList()); when(mBeanServer.getAttribute(objectName, testAttributeName)).thenReturn(value); jmxSensor.mbeanServerAdded(mBeanServer);
jmxSensor.mbeanServerAdded(mBeanServer);
MBeanServerNotification notification = new MBeanServerNotification(MBeanServerNotification.UNREGISTRATION_NOTIFICATION, this, 1, objectName); jmxSensor.mbeanServerAdded(mBeanServer); ArgumentCaptor<MBeanServerHolder> notificationListener = ArgumentCaptor.forClass(MBeanServerHolder.class); verify(mBeanServer).addNotificationListener(Matchers.<ObjectName> any(), notificationListener.capture(), Matchers.<NotificationFilter> any(), eq(null));
jmxSensor.mbeanServerAdded(mBeanServer);
jmxSensor.mbeanServerAdded(mBeanServer);
jmxSensor.mbeanServerAdded(mBeanServer);
jmxSensor.mbeanServerAdded(mBeanServer);
jmxSensor.mbeanServerAdded(mBeanServer);
ObjectName objectName = new ObjectName(testObjectName); jmxSensor.mbeanServerAdded(mBeanServer); ArgumentCaptor<MBeanServerHolder> notificationListener = ArgumentCaptor.forClass(MBeanServerHolder.class); verify(mBeanServer).addNotificationListener(Matchers.<ObjectName> any(), notificationListener.capture(), Matchers.<NotificationFilter> any(), eq(null));