dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/AspectWithPropagationTest.java [504:759]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        m.add(client2);
        
        // Register S2 adapter
        m.add(adapter);
        
        // Randomly register aspects, original service
        boolean originalServiceAdded = false;
        for (int i = 0; i < ASPECTS; i ++) {
            int index = getRandomAspect();
            m.add(aspects[index]);
            if (! originalServiceAdded && _rnd.nextBoolean()) {
                m.add(s);
                originalServiceAdded = true;
            }
        }
        if (! originalServiceAdded) {
            m.add(s);
        }
             
        // Now invoke client2, which orderly calls all S1 aspects, then S1Impl, and finally S2 service
        System.out.println("-------------------------- Invoking client2.");
        client2Impl.invoke2();
        m_invokeStep.waitForStep(ASPECTS+2, 5000);
        
        // Now, change original service "S" properties: this will orderly trigger "change" callbacks on aspects, S2Impl, and Client2.
        System.out.println("-------------------------- Modifying original service properties.");
        m_changeStep = new Ensure();
        for (int i = 1; i <= ASPECTS+1; i ++) {
            m_changeStep.step(i); // skip all aspects and the adapter
        }
        props = new Hashtable();
        props.put("foo", "barModified");
        s.setServiceProperties(props);
        
        // Check if Client2 has been called in its "changed" callback
        m_changeStep.waitForStep(ASPECTS+2, 5000);
        
        // Check if modified "foo" original service property has been propagated to Client2
        Map check = new HashMap();
        check.put("foo", "barModified");
        for (int i = 1; i < (ASPECTS - 1); i ++) {
            check.put("a" + i, null); // we must not inherit from lower ranks, only from the top-level aspect.
        }
        check.put("a" + ASPECTS, "v" + ASPECTS);
        checkServiceProperties(check, client2Impl.getServiceProperties());
        
        // Clear all components.
        m_changeStep = null;
        m.clear();
    }    
    
   private void checkServiceProperties(Map<?, ?> check, Dictionary properties) {
        for (Object key : check.keySet()) {
            Object val = check.get(key);   
            if (val == null) {
                Assert.assertNull(properties.get(key));
            } else {
                Assert.assertEquals(val, properties.get(key));
            }
        }
    }
    
    private int getRandomAspect() {
        int index = 0;  
        do {
            index = _rnd.nextInt(ASPECTS);            
        } while (_randoms.contains(new Integer(index)));
        _randoms.add(new Integer(index));
        return index;
    }

    // S Service
    public static interface S {
        public void invoke();
    }
    
    // S ServiceImpl
    static class SImpl implements S {
        public SImpl() {
        }
        
        public String toString() {
            return "S";
        }
        
        public void invoke() {
             m_invokeStep.step(ASPECTS+1);
        }
    }
    
    // S Aspect
    static class A implements S {
        private final String m_name;
        private volatile ServiceRegistration m_registration;
        private volatile S m_next;
        private final int m_rank;

        public A(String name, int rank) {
            m_name = name;
            m_rank = rank;
        }
        
        public String toString() {
            return m_name;
        }
        
        public void invoke() {
            int rank = ServiceUtil.getRanking(m_registration.getReference());
            m_invokeStep.step(ASPECTS - rank + 1);
            m_next.invoke();
        }
               
        public void add(ServiceReference ref, S s) {
            System.out.println("+++ A" + m_rank + ".add:" + s + "/" + ServiceUtil.toString(ref));   
            m_next = s;
        }

        public void swap(ServiceReference oldSRef, S oldS, ServiceReference newSRef, S newS) {
            System.out.println("+++ A" + m_rank + ".swap: new=" + newS + ", props=" + ServiceUtil.toString(newSRef));
            Assert.assertTrue(m_next == oldS);
            m_next = newS;
        }

        public void change(ServiceReference props, S s) {   
            System.out.println("+++ A" + m_rank + ".change: s=" + s + ", props=" + ServiceUtil.toString(props));
            if (m_changeStep != null) {
                int rank = ServiceUtil.getRanking(m_registration.getReference());
                m_changeStep.step(rank);
            }
        }
        
        public void remove(ServiceReference props, S s) {
            System.out.println("+++ A" + m_rank + ".remove: " + s + ", props=" + ServiceUtil.toString(props));
        }
    }
    
    // Aspect aware client, depending of "S" service aspects.
    static class Client {
        private volatile S m_s;
        private volatile ServiceReference m_sRef;

        public Client() {
        }
        
        public Dictionary getServiceProperties() {
            Dictionary props = new Hashtable();
            for (String key : m_sRef.getPropertyKeys()) {
                props.put(key, m_sRef.getProperty(key));
            }
            return props;
        }

        public void invoke() {
            m_s.invoke();           
        }

        public String toString() {
            return "Client";
        }
        
        public void add(ServiceReference ref, S s) {
            System.out.println("+++ Client.add: " + s + "/" + ServiceUtil.toString(ref));
            m_s = s;
            m_sRef = ref;
        }
              
        public void swap(ServiceReference oldSRef, S oldS, ServiceReference newSRef, S newS) {
            System.out.println("+++ Client.swap: m_s = " + m_s + ", old=" + oldS + ", oldProps=" + ServiceUtil.toString(oldSRef) + ", new=" + newS + ", props=" + ServiceUtil.toString(newSRef));
            Assert.assertTrue(m_s == oldS);
            m_s = newS;
            m_sRef = newSRef;
        }

        public void change(ServiceReference properties, S s) {
            System.out.println("+++ Client.change: s=" + s + ", props=" + ServiceUtil.toString(properties));
            if (m_changeStep != null) {
                m_changeStep.step(ASPECTS+1);
            }
        }
        
        public void remove(ServiceReference props, S s) {
            System.out.println("+++ Client.remove: " + s + ", props=" + ServiceUtil.toString(props));
        }
    }
    
    // S2 Service
    public static interface S2 {
        public void invoke2();
    }

    // S2 impl, which adapts S1 interface to S2 interface
    static class S2Impl implements S2 {
        private volatile S m_s; // we shall see top-level aspect on S service
        
        public void add(ServiceReference ref, S s) {
            System.out.println("+++ S2Impl.add: " + s + "/" + ServiceUtil.toString(ref));
            m_s = s;
        }
              
        public void swap(ServiceReference oldSRef, S oldS, ServiceReference newSRef, S newS) {
            System.out.println("+++ S2Impl.swap: new=" + newS + ", props=" + ServiceUtil.toString(newSRef));
            m_s = newS;
        }

        public void change(ServiceReference properties, S s) {
            System.out.println("+++ S2Impl.change: s=" + s + ", props=" + ServiceUtil.toString(properties));
            if (m_changeStep != null) {
                m_changeStep.step(ASPECTS+1);
            }
        }
        
        public void remove(ServiceReference props, S s) {
            System.out.println("+++ S2Impl.remove: " + s + ", props=" + ServiceUtil.toString(props));
        }
        
        public void invoke2() {
            m_s.invoke();
            m_invokeStep.step(ASPECTS + 2); // All aspects, and S1Impl have been invoked
        }

        public String toString() {
            return "S2";
        }        
    }
    
    // Client2 depending on S2.
    static class Client2 {
        private volatile S2 m_s2;
        private volatile ServiceReference m_s2Ref;

        public Dictionary getServiceProperties() {
            Dictionary props = new Hashtable();
            for (String key : m_s2Ref.getPropertyKeys()) {
                props.put(key, m_s2Ref.getProperty(key));
            }
            return props;
        }

        public void invoke2() {
            m_s2.invoke2();           
        }

        public String toString() {
            return "Client2";
        }  
                
        public void add(ServiceReference ref, S2 s2) {
            System.out.println("+++ Client2.add: " + s2 + "/" + ServiceUtil.toString(ref));
            m_s2 = s2;
            m_s2Ref = ref;
        }

        public void change(ServiceReference props, S2 s2) {   
            System.out.println("+++ Client2.change: s2=" + s2 + ", props=" + ServiceUtil.toString(props));
            if (m_changeStep != null) {
                m_changeStep.step(ASPECTS + 2); // S1Impl, all aspects, and S2 adapters have been changed before us.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



dependencymanager/org.apache.felix.dependencymanager.lambda.itest/src/org/apache/felix/dm/lambda/itest/AspectWithPropagationTest.java [457:712]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        m.add(client2);
        
        // Register S2 adapter
        m.add(adapter);
        
        // Randomly register aspects, original service
        boolean originalServiceAdded = false;
        for (int i = 0; i < ASPECTS; i ++) {
            int index = getRandomAspect();
            m.add(aspects[index]);
            if (! originalServiceAdded && _rnd.nextBoolean()) {
                m.add(s);
                originalServiceAdded = true;
            }
        }
        if (! originalServiceAdded) {
            m.add(s);
        }
             
        // Now invoke client2, which orderly calls all S1 aspects, then S1Impl, and finally S2 service
        System.out.println("-------------------------- Invoking client2.");
        client2Impl.invoke2();
        m_invokeStep.waitForStep(ASPECTS+2, 5000);
        
        // Now, change original service "S" properties: this will orderly trigger "change" callbacks on aspects, S2Impl, and Client2.
        System.out.println("-------------------------- Modifying original service properties.");
        m_changeStep = new Ensure();
        for (int i = 1; i <= ASPECTS+1; i ++) {
            m_changeStep.step(i); // skip all aspects and the adapter
        }
        props = new Hashtable();
        props.put("foo", "barModified");
        s.setServiceProperties(props);
        
        // Check if Client2 has been called in its "changed" callback
        m_changeStep.waitForStep(ASPECTS+2, 5000);
        
        // Check if modified "foo" original service property has been propagated to Client2
        Map check = new HashMap();
        check.put("foo", "barModified");
        for (int i = 1; i < (ASPECTS - 1); i ++) {
            check.put("a" + i, null); // we must not inherit from lower ranks, only from the top-level aspect.
        }
        check.put("a" + ASPECTS, "v" + ASPECTS);
        checkServiceProperties(check, client2Impl.getServiceProperties());
        
        // Clear all components.
        m_changeStep = null;
        m.clear();
    }    
    
   private void checkServiceProperties(Map<?, ?> check, Dictionary properties) {
        for (Object key : check.keySet()) {
            Object val = check.get(key);   
            if (val == null) {
                Assert.assertNull(properties.get(key));
            } else {
                Assert.assertEquals(val, properties.get(key));
            }
        }
    }
    
    private int getRandomAspect() {
        int index = 0;  
        do {
            index = _rnd.nextInt(ASPECTS);            
        } while (_randoms.contains(new Integer(index)));
        _randoms.add(new Integer(index));
        return index;
    }

    // S Service
    public static interface S {
        public void invoke();
    }
    
    // S ServiceImpl
    static class SImpl implements S {
        public SImpl() {
        }
        
        public String toString() {
            return "S";
        }
        
        public void invoke() {
             m_invokeStep.step(ASPECTS+1);
        }
    }
    
    // S Aspect
    static class A implements S {
        private final String m_name;
        private volatile ServiceRegistration m_registration;
        private volatile S m_next;
        private final int m_rank;

        public A(String name, int rank) {
            m_name = name;
            m_rank = rank;
        }
        
        public String toString() {
            return m_name;
        }
        
        public void invoke() {
            int rank = ServiceUtil.getRanking(m_registration.getReference());
            m_invokeStep.step(ASPECTS - rank + 1);
            m_next.invoke();
        }
               
        public void add(ServiceReference ref, S s) {
            System.out.println("+++ A" + m_rank + ".add:" + s + "/" + ServiceUtil.toString(ref));   
            m_next = s;
        }

        public void swap(ServiceReference oldSRef, S oldS, ServiceReference newSRef, S newS) {
            System.out.println("+++ A" + m_rank + ".swap: new=" + newS + ", props=" + ServiceUtil.toString(newSRef));
            Assert.assertTrue(m_next == oldS);
            m_next = newS;
        }

        public void change(ServiceReference props, S s) {   
            System.out.println("+++ A" + m_rank + ".change: s=" + s + ", props=" + ServiceUtil.toString(props));
            if (m_changeStep != null) {
                int rank = ServiceUtil.getRanking(m_registration.getReference());
                m_changeStep.step(rank);
            }
        }
        
        public void remove(ServiceReference props, S s) {
            System.out.println("+++ A" + m_rank + ".remove: " + s + ", props=" + ServiceUtil.toString(props));
        }
    }
    
    // Aspect aware client, depending of "S" service aspects.
    static class Client {
        private volatile S m_s;
        private volatile ServiceReference m_sRef;

        public Client() {
        }
        
        public Dictionary getServiceProperties() {
            Dictionary props = new Hashtable();
            for (String key : m_sRef.getPropertyKeys()) {
                props.put(key, m_sRef.getProperty(key));
            }
            return props;
        }

        public void invoke() {
            m_s.invoke();           
        }

        public String toString() {
            return "Client";
        }
        
        public void add(ServiceReference ref, S s) {
            System.out.println("+++ Client.add: " + s + "/" + ServiceUtil.toString(ref));
            m_s = s;
            m_sRef = ref;
        }
              
        public void swap(ServiceReference oldSRef, S oldS, ServiceReference newSRef, S newS) {
            System.out.println("+++ Client.swap: m_s = " + m_s + ", old=" + oldS + ", oldProps=" + ServiceUtil.toString(oldSRef) + ", new=" + newS + ", props=" + ServiceUtil.toString(newSRef));
            Assert.assertTrue(m_s == oldS);
            m_s = newS;
            m_sRef = newSRef;
        }

        public void change(ServiceReference properties, S s) {
            System.out.println("+++ Client.change: s=" + s + ", props=" + ServiceUtil.toString(properties));
            if (m_changeStep != null) {
                m_changeStep.step(ASPECTS+1);
            }
        }
        
        public void remove(ServiceReference props, S s) {
            System.out.println("+++ Client.remove: " + s + ", props=" + ServiceUtil.toString(props));
        }
    }
    
    // S2 Service
    public static interface S2 {
        public void invoke2();
    }

    // S2 impl, which adapts S1 interface to S2 interface
    static class S2Impl implements S2 {
        private volatile S m_s; // we shall see top-level aspect on S service
        
        public void add(ServiceReference ref, S s) {
            System.out.println("+++ S2Impl.add: " + s + "/" + ServiceUtil.toString(ref));
            m_s = s;
        }
              
        public void swap(ServiceReference oldSRef, S oldS, ServiceReference newSRef, S newS) {
            System.out.println("+++ S2Impl.swap: new=" + newS + ", props=" + ServiceUtil.toString(newSRef));
            m_s = newS;
        }

        public void change(ServiceReference properties, S s) {
            System.out.println("+++ S2Impl.change: s=" + s + ", props=" + ServiceUtil.toString(properties));
            if (m_changeStep != null) {
                m_changeStep.step(ASPECTS+1);
            }
        }
        
        public void remove(ServiceReference props, S s) {
            System.out.println("+++ S2Impl.remove: " + s + ", props=" + ServiceUtil.toString(props));
        }
        
        public void invoke2() {
            m_s.invoke();
            m_invokeStep.step(ASPECTS + 2); // All aspects, and S1Impl have been invoked
        }

        public String toString() {
            return "S2";
        }        
    }
    
    // Client2 depending on S2.
    static class Client2 {
        private volatile S2 m_s2;
        private volatile ServiceReference m_s2Ref;

        public Dictionary getServiceProperties() {
            Dictionary props = new Hashtable();
            for (String key : m_s2Ref.getPropertyKeys()) {
                props.put(key, m_s2Ref.getProperty(key));
            }
            return props;
        }

        public void invoke2() {
            m_s2.invoke2();           
        }

        public String toString() {
            return "Client2";
        }  
                
        public void add(ServiceReference ref, S2 s2) {
            System.out.println("+++ Client2.add: " + s2 + "/" + ServiceUtil.toString(ref));
            m_s2 = s2;
            m_s2Ref = ref;
        }

        public void change(ServiceReference props, S2 s2) {   
            System.out.println("+++ Client2.change: s2=" + s2 + ", props=" + ServiceUtil.toString(props));
            if (m_changeStep != null) {
                m_changeStep.step(ASPECTS + 2); // S1Impl, all aspects, and S2 adapters have been changed before us.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



