Search in sources :

Example 1 with PropertyProvider

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"));
}
Also used : PropertyProvider(org.apache.sling.discovery.PropertyProvider) HashMap(java.util.HashMap) TopologyEvent(org.apache.sling.discovery.TopologyEvent) ArrayList(java.util.ArrayList) DiscoveryService(org.apache.sling.discovery.DiscoveryService) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DiscoveryService (org.apache.sling.discovery.DiscoveryService)1 PropertyProvider (org.apache.sling.discovery.PropertyProvider)1 TopologyEvent (org.apache.sling.discovery.TopologyEvent)1 TopologyEventListener (org.apache.sling.discovery.TopologyEventListener)1 Test (org.junit.Test)1