JmxUrlFactory { public JMXServiceURL createUrl(final String hostName, final int port) { final String urlString = "service:jmx:remote+http: try { return new JMXServiceURL(urlString); } catch (final MalformedURLException e) { throw new MBeanClientConnectionException(format("Failed to create JMX service url using host '%s' and port %d", hostName, port), e); } }  JMXServiceURL createUrl(final String hostName, final int port);  }
@Test public void shouldCreateACorrectlyFormedJmxUrl() throws Exception { final String host = "localhost"; final int port = 9009; final JMXServiceURL jmxServiceURL = jmxUrlFactory.createUrl(host, port); assertThat(jmxServiceURL.toString(), is("service:jmx:remote+http: }
@Test public void shouldThrowExceptionOfHostnameIsMalformed() throws Exception { final String dodglyHostName = "{}\\"; final int port = 9009; try { jmxUrlFactory.createUrl(dodglyHostName, port); fail(); } catch (final MBeanClientConnectionException expected) { assertThat(expected.getMessage(), is("Failed to create JMX service url using host '{}\\' and port 9009")); assertThat(expected.getCause(), is(instanceOf(MalformedURLException.class))); } }