MBeanConnector { public <T> T connect(final String contextName, final Class<T> mBeanInterface, final JMXConnector jmxConnector) { try { final ObjectName objectName = commandMBeanNameProvider.create(contextName); final MBeanServerConnection connection = jmxConnector.getMBeanServerConnection(); if(connection.isRegistered(objectName)) { return remoteMBeanFactory.createRemote(connection, objectName, mBeanInterface); } throw new MBeanClientException(format("No JMX bean found with name '%s'. Is your context name of '%s' correct?", objectName.getKeyProperty("type"), contextName)); } catch (final IOException e) { throw new MBeanClientConnectionException(format("Failed to get remote connection to MBean '%s'", mBeanInterface.getSimpleName()), e); } }  T connect(final String contextName, final Class<T> mBeanInterface, final JMXConnector jmxConnector);  }
@Test public void shouldConnectToARemoteInstanceOfTheJmxBean() throws Exception { final String contextName = "my-context"; final Class<SystemCommanderMBean> mBeanInterface = SystemCommanderMBean.class; final JMXConnector jmxConnector = mock(JMXConnector.class); final ObjectName objectName = mock(ObjectName.class); final MBeanServerConnection connection = mock(MBeanServerConnection.class); final SystemCommanderMBean systemCommanderMBean = mock(SystemCommanderMBean.class); when(commandMBeanNameProvider.create(contextName)).thenReturn(objectName); when(jmxConnector.getMBeanServerConnection()).thenReturn(connection); when(connection.isRegistered(objectName)).thenReturn(true); when(remoteMBeanFactory.createRemote(connection, objectName, mBeanInterface)).thenReturn(systemCommanderMBean); assertThat(mBeanConnector.connect(contextName, mBeanInterface, jmxConnector), is(systemCommanderMBean)); }
@Test public void shouldThrowExceptionIfConnectingToTheMBeanFails() throws Exception { final IOException ioException = new IOException("Ooops"); final String contextName = "my-context"; final Class<SystemCommanderMBean> mBeanInterface = SystemCommanderMBean.class; final JMXConnector jmxConnector = mock(JMXConnector.class); final ObjectName objectName = mock(ObjectName.class); when(commandMBeanNameProvider.create(contextName)).thenReturn(objectName); when(jmxConnector.getMBeanServerConnection()).thenThrow(ioException); try { mBeanConnector.connect(contextName, mBeanInterface, jmxConnector); fail(); } catch (final MBeanClientConnectionException expected) { assertThat(expected.getCause(), is(ioException)); assertThat(expected.getMessage(), is("Failed to get remote connection to MBean 'SystemCommanderMBean'")); } }
@Test public void shouldThrowExceptionIfMBeanNameNotCorrect() throws Exception { final String contextName = "people"; final ObjectName realObjectName = createARealObjectName(contextName); final Class<SystemCommanderMBean> mBeanInterface = SystemCommanderMBean.class; final JMXConnector jmxConnector = mock(JMXConnector.class); final MBeanServerConnection connection = mock(MBeanServerConnection.class); when(commandMBeanNameProvider.create(contextName)).thenReturn(realObjectName); when(jmxConnector.getMBeanServerConnection()).thenReturn(connection); when(connection.isRegistered(realObjectName)).thenReturn(false); try { mBeanConnector.connect(contextName, mBeanInterface, jmxConnector); fail(); } catch (final MBeanClientException expected) { assertThat(expected.getMessage(), is("No JMX bean found with name 'people-system-command-handler-mbean'. Is your context name of 'people' correct?")); } }