}
@Test
public void testLocationUtils_shouldLoadServiceByLocation() throws Exception {
ExampleDomain service = new DummyService("test");
Hashtable<String, Object> properties = new Hashtable<String, Object>();
properties.put("id", "test");
properties.put(Constants.SERVICE_RANKING, -1);
properties.put("location.root", new String[]{ "foo" });
getBundleContext().registerService(ExampleDomain.class.getName(), service, properties);
service = new DummyService("test2");
properties = new Hashtable<String, Object>();
properties.put("id", "test2");
properties.put("location.foo", new String[]{ "foo" });
properties.put(Constants.SERVICE_RANKING, 1);
getBundleContext().registerService(ExampleDomain.class.getName(), service, properties);
ExampleDomain service2 = getServiceUtils().getService(ExampleDomain.class);
assertThat(service2.getInstanceId(), is("test2"));
ContextHolder.get().setCurrentContextId("foo");
ExampleDomain serviceForLocation =
(ExampleDomain) getServiceUtils().getServiceForLocation("foo");
assertThat(serviceForLocation.getInstanceId(), is("test2"));
ContextHolder.get().setCurrentContextId("foo2");
serviceForLocation = (ExampleDomain) getServiceUtils().getServiceForLocation("foo");
assertThat(serviceForLocation.getInstanceId(), is("test"));
serviceForLocation =
(ExampleDomain) getServiceUtils().getServiceForLocation("foo", "foo");
assertThat(serviceForLocation.getInstanceId(), is("test2"));
serviceForLocation = getServiceUtils().getServiceForLocation(ExampleDomain.class, "foo");
assertThat(serviceForLocation.getInstanceId(), is("test"));
}