use of org.apache.sling.discovery.PropertyProvider in project sling by apache.
the class NoClusterDiscoveryServiceTest method testPropertyChanges.
@Test
public void testPropertyChanges() throws Exception {
final DiscoveryService service = this.createService(true);
final List<TopologyEvent> events = new ArrayList<TopologyEvent>();
final TopologyEventListener listener = new TopologyEventListener() {
@Override
public void handleTopologyEvent(final TopologyEvent event) {
events.add(event);
}
};
invoke(service, "bindTopologyEventListener", new Class[] { TopologyEventListener.class }, new Object[] { listener });
events.clear();
final PropertyProvider provider = new PropertyProvider() {
@Override
public String getProperty(final String name) {
if ("a".equals(name)) {
return "1";
}
if ("b".equals(name)) {
return "2";
}
if ("c".equals(name)) {
return "3";
}
return null;
}
};
final Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyProvider.PROPERTY_PROPERTIES, new String[] { "a", "b", "c" });
properties.put(Constants.SERVICE_ID, 1L);
invoke(service, "bindPropertyProvider", new Class[] { PropertyProvider.class, Map.class }, new Object[] { provider, properties });
assertEquals(1, events.size());
assertEquals(TopologyEvent.Type.PROPERTIES_CHANGED, events.get(0).getType());
assertNotNull(events.get(0).getNewView());
assertTrue(events.get(0).getNewView().isCurrent());
assertNotNull(events.get(0).getOldView());
assertFalse(events.get(0).getOldView().isCurrent());
// test properties
assertEquals("1", events.get(0).getNewView().getLocalInstance().getProperty("a"));
assertEquals("2", events.get(0).getNewView().getLocalInstance().getProperty("b"));
assertEquals("3", events.get(0).getNewView().getLocalInstance().getProperty("c"));
assertNull(events.get(0).getOldView().getLocalInstance().getProperty("a"));
assertNull(events.get(0).getOldView().getLocalInstance().getProperty("b"));
assertNull(events.get(0).getOldView().getLocalInstance().getProperty("c"));
events.clear();
invoke(service, "unbindPropertyProvider", new Class[] { PropertyProvider.class, Map.class }, new Object[] { provider, properties });
assertEquals(1, events.size());
assertEquals(TopologyEvent.Type.PROPERTIES_CHANGED, events.get(0).getType());
assertNotNull(events.get(0).getNewView());
assertTrue(events.get(0).getNewView().isCurrent());
assertNotNull(events.get(0).getOldView());
assertFalse(events.get(0).getOldView().isCurrent());
assertEquals("1", events.get(0).getOldView().getLocalInstance().getProperty("a"));
assertEquals("2", events.get(0).getOldView().getLocalInstance().getProperty("b"));
assertEquals("3", events.get(0).getOldView().getLocalInstance().getProperty("c"));
assertNull(events.get(0).getNewView().getLocalInstance().getProperty("a"));
assertNull(events.get(0).getNewView().getLocalInstance().getProperty("b"));
assertNull(events.get(0).getNewView().getLocalInstance().getProperty("c"));
}
Aggregations