JMXConnectorFactory { public JMXConnector createJmxConnector(final JmxParameters jmxParameters) { final Map<String, Object> environment = environmentFactory.create(jmxParameters); final String host = jmxParameters.getHost(); final int port = jmxParameters.getPort(); final JMXServiceURL serviceURL = jmxUrlFactory.createUrl(host, port); try { return connectorWrapper.connect(serviceURL, environment); } catch (final SaslException e) { throw new JmxAuthenticationException("Jmx authentication failed", e); } catch (final IOException e) { throw new MBeanClientConnectionException(format("Failed to connect to JMX using url '%s'", serviceURL), e); } }  JMXConnector createJmxConnector(final JmxParameters jmxParameters);  }
@Test public void shouldCreateJMXConnectorWithTheCorrectNameAndPort() throws Exception { final String host = "localhost"; final int port = 2384; final Map<String, Object> environment = of("name", "value"); final JmxParameters jmxParameters = jmxParameters() .withHost(host) .withPort(port) .build(); final JMXServiceURL serviceURL = mock(JMXServiceURL.class); final JMXConnector jmxConnector = mock(JMXConnector.class); when(jmxUrlFactory.createUrl(host, port)).thenReturn(serviceURL); when(connectorWrapper.connect(serviceURL, environment)).thenReturn(jmxConnector); when(environmentFactory.create(jmxParameters)).thenReturn(environment); assertThat(jmxConnectorFactory.createJmxConnector(jmxParameters), is(jmxConnector)); }
@Test public void shouldThrowAuthenticationExceptionIfAuthenticationFails() throws Exception { final SaslException saslException = new SaslException("Ooops"); final String host = "localhost"; final int port = 2384; final String urlString = "service:jmx:remote+http: final Map<String, Object> environment = of("name", "value"); final JmxParameters jmxParameters = jmxParameters() .withHost(host) .withPort(port) .build(); final JMXServiceURL serviceURL = new JMXServiceURL(urlString); when(jmxUrlFactory.createUrl(host, port)).thenReturn(serviceURL); when(environmentFactory.create(jmxParameters)).thenReturn(environment); when(connectorWrapper.connect(serviceURL, environment)).thenThrow(saslException); try { jmxConnectorFactory.createJmxConnector(jmxParameters); fail(); } catch (final JmxAuthenticationException expected) { assertThat(expected.getCause(), is(saslException)); assertThat(expected.getMessage(), is("Jmx authentication failed")); } }
@Test public void shouldThrowExceptionIfConnectionFails() throws Exception { final IOException ioException = new IOException("Ooops"); final String host = "localhost"; final int port = 2384; final String urlString = "service:jmx:remote+http: final Map<String, Object> environment = of("name", "value"); final JmxParameters jmxParameters = jmxParameters() .withHost(host) .withPort(port) .build(); final JMXServiceURL serviceURL = new JMXServiceURL(urlString); when(jmxUrlFactory.createUrl(host, port)).thenReturn(serviceURL); when(connectorWrapper.connect(serviceURL, environment)).thenThrow(ioException); when(environmentFactory.create(jmxParameters)).thenReturn(environment); try { jmxConnectorFactory.createJmxConnector(jmxParameters); fail(); } catch (final MBeanClientConnectionException expected) { assertThat(expected.getCause(), is(ioException)); assertThat(expected.getMessage(), is("Failed to connect to JMX using url 'service:jmx:remote+http: } }