Package org.apache.felix.dm.test.components

Examples of org.apache.felix.dm.test.components.Ensure


   @Test
   public void testReplacementAutoConfig() {
       DependencyManager m = new DependencyManager(context);
       // helper class that ensures certain steps get executed in sequence
       Ensure e = new Ensure();
       // create a service provider and consumer
       Component provider = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
       Component provider2 = m.createComponent().setImplementation(new ServiceProvider2(e)).setInterface(ServiceInterface.class.getName(), null);
       Component consumer = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true));
       m.add(provider2);
       m.add(consumer);
       e.waitForStep(3, 15000);
       m.add(provider);
       m.remove(provider2);
       e.step(4);
       e.waitForStep(5, 15000);
       m.remove(provider);
       m.remove(consumer);
       e.waitForStep(6, 15000);
   }
View Full Code Here


   @Test
   public void testReplacementCallbacks() {
       DependencyManager m = new DependencyManager(context);
       // helper class that ensures certain steps get executed in sequence
       Ensure e = new Ensure();
       // create a service provider and consumer
       Component provider = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
       Component provider2 = m.createComponent().setImplementation(new ServiceProvider2(e)).setInterface(ServiceInterface.class.getName(), null);
       Component consumer = m.createComponent().setImplementation(new ServiceConsumer(e))
         .add(m.createServiceDependency()
              .setService(ServiceInterface.class)
              .setRequired(true)
              .setCallbacks("add", "remove"));
       m.add(provider2);
       m.add(consumer);
       e.waitForStep(3, 15000);
       m.add(provider);
       m.remove(provider2);
       e.step(4);
       e.waitForStep(5, 15000);
       m.remove(provider);
       m.remove(consumer);
       e.waitForStep(6, 15000);
   }
View Full Code Here

public class DynamicProxyAspectTest extends TestBase {
    @Test
    public void testImplementGenericAspectWithDynamicProxy() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
       
        // create two service providers, each providing a different service interface
        Component sp1 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
        Component sp2 = m.createComponent().setImplementation(new ServiceProvider2(e)).setInterface(ServiceInterface2.class.getName(), null);
       
        // create a dynamic proxy based aspect and hook it up to both services
        Component a1 = m.createAspectService(ServiceInterface.class, null, 10, "m_service")
            .setFactory(new Factory(e, ServiceInterface.class, "ServiceInterfaceProxy"), "create");
        Component a2 = m.createAspectService(ServiceInterface2.class, null, 10, "m_service")
            .setFactory(new Factory(e, ServiceInterface2.class, "ServiceInterfaceProxy2"), "create");

        // create a client that invokes a method on boths services, validate that it goes
        // through the proxy twice
        Component sc = m.createComponent()
            .setImplementation(new ServiceConsumer(e))
            .add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true))
            .add(m.createServiceDependency().setService(ServiceInterface2.class).setRequired(true))
            ;
       
        // register both producers, validate that both services are started
        m.add(sp1);
        e.waitForStep(1, 2000);
        m.add(sp2);
        e.waitForStep(2, 2000);
       
        // add both aspects, and validate that both instances have been created
        m.add(a1);
        m.add(a2);
        e.waitForStep(4, 4000);
       
        // add the client, which will automatically invoke both services
        m.add(sc);
       
        // wait until both services have been invoked
        e.waitForStep(6, 4000);
       
        // make sure the proxy has been called twice
        Assert.assertEquals("Proxy should have been invoked this many times.", 2, DynamicProxyHandler.getCounter());
       
        m.remove(sc);
View Full Code Here

public class ResourceAdapterTest extends TestBase {
    @Test
    public void testBasicResourceAdapter() throws Exception {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // create a resource provider
        ResourceProvider provider = new ResourceProvider(e);
        // activate it
        m.add(m.createComponent().setImplementation(provider).add(m.createServiceDependency().setService(ResourceHandler.class).setCallbacks("add", "remove")));
        // create a resource adapter for our single resource
        // note that we can provide an actual implementation instance here because there will be only one
        // adapter, normally you'd want to specify a Class here
        m.add(m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))", false, null, "changed")
              .setImplementation(new ResourceAdapter(e)));
        // wait until the single resource is available
        e.waitForStep(3, 5000);
        // trigger a 'change' in our resource
        provider.change();
        // wait until the changed callback is invoked
        e.waitForStep(4, 5000);
        m.clear();
     }
View Full Code Here

    @Test
    public void testAspectsWithPropagation() {
        System.out.println("----------- Running testAspectsWithPropagation ...");
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        m_invokeStep = new Ensure();
       
        // Create our original "S" service.
        Dictionary props = new Hashtable();
        props.put("foo", "bar");
        Component s = m.createComponent()
                .setImplementation(new SImpl())
                .setInterface(S.class.getName(), props);
       
        // Create an aspect aware client, depending on "S" service.
        Client clientImpl;
        Component client = m.createComponent()
                .setImplementation((clientImpl = new Client()))
                .add(m.createServiceDependency()
                     .setService(S.class)
                     .setRequired(true)
                     .setDebug("client")
                     .setCallbacks("add", "change", "remove", "swap"));

        // Create some "S" aspects
        Component[] aspects = new Component[ASPECTS];
        for (int rank = 1; rank <= ASPECTS; rank ++) {
            aspects[rank-1] = m.createAspectService(S.class, null, rank, "add", "change", "remove", "swap")
                    .setImplementation(new A("A" + rank, rank));
            props = new Hashtable();
            props.put("a" + rank, "v" + rank);
            aspects[rank-1].setServiceProperties(props);
        }                                   
             
        // Register client
        m.add(client);
       
        // Randomly register aspects and original service
        boolean originalServiceAdded = false;
        for (int i = 0; i < ASPECTS; i ++) {
            int index = getRandomAspect();
            m.add(aspects[index]);
            if (_rnd.nextBoolean()) {
                m.add(s);
                originalServiceAdded = true;
            }
        }
        if (! originalServiceAdded) {
            m.add(s);
        }
             
        // All set, check if client has inherited from top level aspect properties + original service properties
        Map check = new HashMap();
        check.put("foo", "bar");
        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, clientImpl.getServiceProperties());

        // Now invoke client, which orderly calls all aspects in the chain, and finally the original service "S".
        System.out.println("-------------------------- Invoking client.");
        clientImpl.invoke();
        m_invokeStep.waitForStep(ASPECTS+1, 5000);
       
        // Now, change original service "S" properties: this will orderly trigger "change" callbacks on aspects, and on client.
        System.out.println("-------------------------- Modifying original service properties.");
        m_changeStep = new Ensure();
        props = new Hashtable();
        props.put("foo", "barModified");
        s.setServiceProperties(props);
       
        // Check if aspects and client have been orderly called in their "changed" callback
        m_changeStep.waitForStep(ASPECTS+1, 5000);
       
        // Check if modified "foo" original service property has been propagated
        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); // we only see top-level aspect service properties
        checkServiceProperties(check, clientImpl.getServiceProperties());
       
        // Now, change the top-level ranked aspect: it must propagate to all upper aspects, as well as to the client
        System.out.println("-------------------------- Modifying top-level aspect service properties.");

        m_changeStep = new Ensure();
        for (int i = 1; i <= ASPECTS; i ++) {
            m_changeStep.step(i); // only client has to be changed.
        }
        props = new Hashtable();
        props.put("a" + ASPECTS, "v" + ASPECTS + "-Modified");
View Full Code Here

    @Test
    public void testAspectsWithPropagationAndNoCallbacks() {
        System.out.println("----------- Running testAspectsWithPropagation ...");
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        m_invokeStep = new Ensure();
       
        // Create our original "S" service.
        Dictionary props = new Hashtable();
        props.put("foo", "bar");
        Component s = m.createComponent()
                .setImplementation(new SImpl())
                .setInterface(S.class.getName(), props);
       
        // Create an aspect aware client, depending on "S" service.
        Client clientImpl;
        Component client = m.createComponent()
                .setImplementation((clientImpl = new Client()))
                .add(m.createServiceDependency()
                     .setService(S.class)
                     .setRequired(true)
                     .setDebug("client")
                     .setCallbacks("add", "change", "remove"));

        // Create some "S" aspects
        Component[] aspects = new Component[ASPECTS];
        for (int rank = 1; rank <= ASPECTS; rank ++) {
            aspects[rank-1] = m.createAspectService(S.class, null, rank)
                    .setImplementation(new A("A" + rank, rank));
            props = new Hashtable();
            props.put("a" + rank, "v" + rank);
            aspects[rank-1].setServiceProperties(props);
        }                                   
             
        // Register client
        m.add(client);
       
        // Randomly register aspects and original service
        boolean originalServiceAdded = false;
        for (int i = 0; i < ASPECTS; i ++) {
            int index = getRandomAspect();
            m.add(aspects[index]);
            if (_rnd.nextBoolean()) {
                m.add(s);
                originalServiceAdded = true;
            }
        }
        if (! originalServiceAdded) {
            m.add(s);
        }
             
        // All set, check if client has inherited from top level aspect properties + original service properties
        Map check = new HashMap();
        check.put("foo", "bar");
        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, clientImpl.getServiceProperties());

        // Now invoke client, which orderly calls all aspects in the chain, and finally the original service "S".
        System.out.println("-------------------------- Invoking client.");
        clientImpl.invoke();
        m_invokeStep.waitForStep(ASPECTS+1, 5000);
       
        // Now, change original service "S" properties: this will orderly trigger "change" callbacks on aspects, and on client.
        System.out.println("-------------------------- Modifying original service properties.");
        m_changeStep = new Ensure();
        for (int i = 1; i <= ASPECTS; i ++) {
            m_changeStep.step(i); // skip aspects, which have no "change" callbacks.
        }
        props = new Hashtable();
        props.put("foo", "barModified");
View Full Code Here

    @Test
    public void testAdapterWithAspectsAndPropagation() {
        System.out.println("----------- Running testAdapterWithAspectsAndPropagation ...");

        DependencyManager m = new DependencyManager(context);
        m_invokeStep = new Ensure();
       
        // Create our original "S" service.
        Dictionary props = new Hashtable();
        props.put("foo", "bar");
        Component s = m.createComponent()
                .setImplementation(new SImpl())
                .setInterface(S.class.getName(), props);
       
        // Create some "S" aspects
        Component[] aspects = new Component[ASPECTS];
        for (int rank = 1; rank <= ASPECTS; rank ++) {
            aspects[rank-1] = m.createAspectService(S.class, null, rank, "add", "change", "remove", "swap")
                    .setImplementation(new A("A" + rank, rank));
            props = new Hashtable();
            props.put("a" + rank, "v" + rank);
            aspects[rank-1].setServiceProperties(props);
        }
       
        // Create S2 adapter (which adapts S1 to S2 interface)
        Component adapter = m.createAdapterService(S.class, null, "add", "change", "remove", "swap")
                .setInterface(S2.class.getName(), null)
                .setImplementation(new S2Impl());
       
        // Create Client2, which depends on "S2" service.
        Client2 client2Impl;
        Component client2 = m.createComponent()
                .setImplementation((client2Impl = new Client2()))
                .add(m.createServiceDependency()
                     .setService(S2.class)
                     .setRequired(true)
                     .setDebug("client")                    
                     .setCallbacks("add", "change", null));
             
        // Register client2
        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 (_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();
        props = new Hashtable();
        props.put("foo", "barModified");
        s.setServiceProperties(props);
       
        // Check if aspects and Client2 have been orderly called in their "changed" callback
View Full Code Here

    @Test
    public void testAdapterWithAspectsAndPropagationNoCallbacks() {
        System.out.println("----------- Running testAdapterWithAspectsAndPropagationNoCallbacks ...");

        DependencyManager m = new DependencyManager(context);
        m_invokeStep = new Ensure();
       
        // Create our original "S" service.
        Dictionary props = new Hashtable();
        props.put("foo", "bar");
        Component s = m.createComponent()
                .setImplementation(new SImpl())
                .setInterface(S.class.getName(), props);
       
        // Create some "S" aspects
        Component[] aspects = new Component[ASPECTS];
        for (int rank = 1; rank <= ASPECTS; rank ++) {
            aspects[rank-1] = m.createAspectService(S.class, null, rank)
                    .setImplementation(new A("A" + rank, rank));
            props = new Hashtable();
            props.put("a" + rank, "v" + rank);
            aspects[rank-1].setServiceProperties(props);
        }
       
        // Create S2 adapter (which adapts S1 to S2 interface)
        Component adapter = m.createAdapterService(S.class, null)
                .setInterface(S2.class.getName(), null)
                .setImplementation(new S2Impl());
       
        // Create Client2, which depends on "S2" service.
        Client2 client2Impl;
        Component client2 = m.createComponent()
                .setImplementation((client2Impl = new Client2()))
                .add(m.createServiceDependency()
                     .setService(S2.class)
                     .setRequired(true)
                     .setDebug("client")                    
                     .setCallbacks("add", "change", "remove"));
             
        // Register client2
        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 (_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");
View Full Code Here

        // be the order in which they're invoked (which from a spec point of view is not true)
       
       
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
       
        // create a service provider
        Component provider = m.createComponent()
            .setImplementation(new ServiceProvider())
            .setInterface(OriginalService.class.getName(), null);

        // create a adapter on the provider
        Component adapter = m.createAdapterService(OriginalService.class, null)
        .setInterface(AdaptedService.class.getName(), null)
        .setImplementation(ServiceAdapter.class);
       
        // create a consumer for the adapted service
        Component consumer = m.createComponent()
            .setImplementation(new ServiceConsumer(e))
            .add(m.createServiceDependency()
                .setService(AdaptedService.class)
                .setCallbacks("add", "remove")
                .setRequired(true)
            );
       
        // create an aspect on the service provider
        Component aspect = m.createAspectService(OriginalService.class, null, 10, null)
            .setImplementation(ServiceAspect.class);

        // we first start the provider, the adapter and the consumer
        m.add(provider);
        m.add(adapter);
        m.add(consumer);
        // now wait until the callback method is invoked on the consumer
        e.waitForStep(1, 5000);
        // now we add an aspect on top of the provided service, which
        // should not affect our consumer at all
        m.add(aspect);
        m.remove(aspect);
        // now we remove the consumer, adapter and provider
        m.remove(consumer);
        m.remove(adapter);
        m.remove(provider);
        // that should have triggered step 2
        e.waitForStep(2, 5000);
        // make sure we don't have extra steps by explicitly going to step 3
        e.step(3);
    }
View Full Code Here

public class MultipleExtraDependencyTest extends TestBase {
    @Test
    public void testMultipleExtraDependencies()
    {
        DependencyManager m = new DependencyManager(context);
        Ensure e = new Ensure();
       
        Component sp2 = m.createComponent()
              .setImplementation(ServiceProvider2.class).setInterface(ServiceProvider2.class.getName(), null)
              .add(m.createServiceDependency()
                   .setService(Runnable.class, "(foo=bar)")
                   .setRequired(false)
                   .setAutoConfig("m_runnable"))
              .add(m.createServiceDependency()
                   .setService(Sequencer.class)
                   .setRequired(true)
                   .setCallbacks("bind", null))
              .setCallbacks(null, "start", "stop", null)
              .setComposition("getComposition");
       
        Component sp = m.createComponent()
              .setImplementation(ServiceProvider.class)
              .setInterface(ServiceInterface.class.getName(),
                            new Hashtable() {{ put("foo", "bar"); }})
              .add(m.createServiceDependency()
                   .setService(Sequencer.class)
                   .setRequired(true)
                   .setAutoConfig("m_sequencer"))
               .add(m.createServiceDependency()
                   .setService(ServiceProvider2.class)
                   .setRequired(true)
                   .setCallbacks("bind", "unbind"))
              .setCallbacks(null, "start", "stop", null);
       
        Component sc = m.createComponent()
              .setImplementation(ServiceConsumer.class)
              .add(m.createServiceDependency()
                   .setService(Sequencer.class)
                   .setRequired(true)
                   .setAutoConfig("m_sequencer"))
               .add(m.createServiceDependency()
                   .setService(ServiceInterface.class, "(foo=bar)")
                   .setRequired(true)
                   .setAutoConfig("m_service"))
              .setCallbacks(null, "start", "stop", null);
       
        // Provide the Sequencer service to the MultipleAnnotationsTest class.
        Component sequencer =
            m.createComponent().setImplementation(new SequencerImpl(e))
                             .setInterface(Sequencer.class.getName(), null);
        m.add(sp2);
        m.add(sp);
        m.add(sc);
        m.add(sequencer);
       
        // Check if the test.annotation components have been initialized orderly
        e.waitForStep(7, 10000);
       
        // Stop the test.annotation bundle
        m.remove(sequencer);
        m.remove(sp);
        m.remove(sp2);
        m.remove(sc);
       
        // And check if the test.annotation bundle has been deactivated orderly
        e.waitForStep(11, 10000);
    }
View Full Code Here

TOP

Related Classes of org.apache.felix.dm.test.components.Ensure

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.