ServiceLocator { public static Service getService(String serviceJndiName) { Service serviceObj = serviceCache.getService(serviceJndiName); if (serviceObj != null) { return serviceObj; } else { InitContext ctx = new InitContext(); serviceObj = (Service) ctx.lookup(serviceJndiName); if (serviceObj != null) { serviceCache.addService(serviceObj); } return serviceObj; } } private  ServiceLocator(); static Service getService(String serviceJndiName);  }
@Test public void testGetNonExistentService() { assertNull(ServiceLocator.getService("fantastic/unicorn/service")); assertNull(ServiceLocator.getService("another/fantastic/unicorn/service")); }
@Test public void testServiceCache() { final String[] serviceNames = new String[]{ "jndi/serviceA", "jndi/serviceB" }; for (final String serviceName : serviceNames) { final Service service = ServiceLocator.getService(serviceName); assertNotNull(service); assertEquals(serviceName, service.getName()); assertTrue(service.getId() > 0); assertSame(service, ServiceLocator.getService(serviceName)); } }