DeferredJMXConnector implements Deferred<JMXConnector> { @Override public JMXConnector get() throws TemporarilyUnavailableException, PermanentlyUnavailableException { String username = ""; String password = ""; Map<String, Object> env = new HashMap<String, Object>(); String[] credentials = new String[] {username, password}; env.put(JMXConnector.CREDENTIALS, credentials); try { JMXConnector connector; connector = jmxConnectorBuilder.realize(new JMXServiceURL(jmxConnectionURL), env); connector.connect(); return connector; } catch (IOException e) { throw new PermanentlyUnavailableException(this, e); } catch (Exception e) { throw new TemporarilyUnavailableException(this, e); } } DeferredJMXConnector(String jmxConnectionURL);  DeferredJMXConnector(String              jmxConnectionURL,
                                JMXConnectorBuilder jmxConnectorBuilder); String getJMXConnectionURL(); @Override JMXConnector get(); @Override Class<JMXConnector> getDeferredClass(); @Override String toString();  }
@SuppressWarnings({"unchecked"}) @Test public void shouldGetConnector() throws Exception { String url = "service:jmx:rmi: DeferredJMXConnector.JMXConnectorBuilder builder = mock(DeferredJMXConnector.JMXConnectorBuilder.class); JMXConnector connector = mock(JMXConnector.class); JMXServiceURL serviceURL = new JMXServiceURL(url); when(builder.realize(eq(serviceURL), anyMap())).thenReturn(connector); DeferredJMXConnector deferred = new DeferredJMXConnector(url, builder); assertThat(deferred.get(), is(sameInstance(connector))); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Test public void shouldGetConnectorUsingEmptyCredentials() throws Exception { String url = "service:jmx:rmi: DeferredJMXConnector.JMXConnectorBuilder builder = mock(DeferredJMXConnector.JMXConnectorBuilder.class); JMXConnector connector = mock(JMXConnector.class); JMXServiceURL serviceURL = new JMXServiceURL(url); when(builder.realize(eq(serviceURL), anyMap())).thenReturn(connector); DeferredJMXConnector deferred = new DeferredJMXConnector(url, builder); deferred.get(); ArgumentCaptor<Map> captor = ArgumentCaptor.forClass(Map.class); verify(builder).realize(eq(serviceURL), captor.capture()); Map env = captor.getValue(); assertThat((String[]) env.get(JMXConnector.CREDENTIALS), is(arrayContaining("", ""))); }