JndiAddressProvider extends DefaultAddressProvider implements InitializingBean { @Override public List<InetSocketAddress> getAddresses() { Object ips = null; List<InetSocketAddress> addrs = null; try { if (jndiKey != null && (ips = lookup(jndiKey)) != null) { getLogger().info("Addresses from JNDI will be used to connect to memcached servers. Addresses: {}", ips); addrs = getAddresses((String) ips); } else { addrs = super.getAddresses(); } } catch (NamingException ex) { getLogger() .warn(String.format("Name of the JNDI key with memcached addresses is set but wrong value is bound to this key: %s", jndiKey), ex); addrs = super.getAddresses(); } return addrs; } JndiAddressProvider();  JndiAddressProvider(final String jndiKey, final String address); @Override void afterPropertiesSet(); @Override List<InetSocketAddress> getAddresses();  }
@Test public void getAddresses() throws NamingException { JndiAddressProvider spy = Mockito.spy(jndiAdressProvider); Mockito.doReturn("127.0.0.1:11311").when(spy).lookup(jndiKey); List<InetSocketAddress> result = spy.getAddresses(); assertNotNull(result); assertEquals(1, result.size()); assertEquals("127.0.0.1", result.get(0).getAddress().getHostAddress()); assertEquals(11311, result.get(0).getPort()); }
@Test public void getAddressesNullKey() { jndiAdressProvider.setJndiKey(null); List<InetSocketAddress> result = jndiAdressProvider.getAddresses(); assertNotNull(result); assertEquals(1, result.size()); assertEquals("127.0.0.1", result.get(0).getAddress().getHostAddress()); assertEquals(11211, result.get(0).getPort()); jndiAdressProvider = new JndiAddressProvider(null, "127.0.0.1:11211"); result = jndiAdressProvider.getAddresses(); assertNotNull(result); assertEquals(1, result.size()); assertEquals("127.0.0.1", result.get(0).getAddress().getHostAddress()); assertEquals(11211, result.get(0).getPort()); }