Package org.apache.tuscany.core.context

Examples of org.apache.tuscany.core.context.CompositeContext


    private NullMonitorFactory monitorFactory;
    private ContextFactoryBuilderRegistry builderRegistry;
    private DefaultWireBuilder wireBuilder;

    public void testContextParents() {
        CompositeContext rootContext = runtime.getRootContext();
        assertNotNull(rootContext);
        assertEquals("tuscany.root", rootContext.getName());
        assertSame(runtime, rootContext.getParent());
        assertSame(rootContext, runtime.getContext("tuscany.root"));

        CompositeContext systemContext = runtime.getSystemContext();
        assertNotNull(systemContext);
        assertEquals("tuscany.system", systemContext.getName());
        assertSame(runtime, systemContext.getParent());
        assertSame(systemContext, runtime.getContext("tuscany.system"));
    }
View Full Code Here


        // start the runtime context
        RuntimeContext runtimeContext = new RuntimeContextImpl(monitorFactory, builderRegistry, wireBuilder);
        runtimeContext.start();

        CompositeContext system = runtimeContext.getSystemContext();
        Assert.assertNotNull(system);
        // register system components
        system.registerModelObject(MockFactory.createSystemModule());
        // start the module scope
        system.publish(new ModuleStart(this));
        // register the first module
       
        // register the second module

        // start the modules

        system.publish(new ModuleStop(this));
        runtimeContext.stop();
        Assert.assertEquals(Lifecycle.STOPPED,system.getLifecycleState());
    }
View Full Code Here

    /**
     * Tests explicit wiring of an external service to a system entry point that is wired to a child system
     * module entry point
     */
    public void testSystemExplicitWiring() throws Exception {
        CompositeContext root = runtime.getRootContext();
        Assert.assertNotNull(root);
        Assert.assertTrue(root.getLifecycleState() == Lifecycle.RUNNING);

        CompositeContext system = runtime.getSystemContext();
        Assert.assertNotNull(system);
        system.registerModelObject(MockFactory.createSystemModule());

        // register a child system context
        system.registerModelObject(MockFactory.createSystemCompositeComponent("system.child"));
        CompositeContext systemChild = (CompositeContext) system.getContext("system.child");
        systemChild.registerModelObject(MockFactory.createSystemChildModule());

        // register a top-level system entry point that exposes the child entry point
        EntryPoint ep = MockFactory.createEPSystemBinding("TestService2EP", ModuleScopeSystemComponent.class, "ref");
        ep.getBindings().add(systemFactory.createSystemBinding());
        Service service = systemFactory.createService();
View Full Code Here

    /**
     * Tests autowiring an external service to a system entry point
     */
    public void testSystemAutoWiring() throws Exception {
        CompositeContext root = runtime.getRootContext();
        Assert.assertNotNull(root);
        Assert.assertTrue(root.getLifecycleState() == Lifecycle.RUNNING);

        CompositeContext system = runtime.getSystemContext();
        Assert.assertNotNull(system);
        system.registerModelObject(MockFactory.createSystemModule());

        // create a test module and wire an external service to the system entry point
        Component moduleComponent = MockFactory.createCompositeComponent("test.module");
        runtime.registerModelObject(moduleComponent);
        CompositeContextImpl moduleContext = (CompositeContextImpl) runtime.getContext("test.module");
        Assert.assertNotNull(moduleContext);
        ExternalService es = MockFactory.createAutowirableExternalService("TestService2ES", ModuleScopeSystemComponent.class);
        moduleContext.registerModelObject(es);

        system.publish(new ModuleStart(this));
        moduleContext.publish(new ModuleStart(this));
        // test that the autowire was resolved
        Assert.assertNotNull(moduleContext.getContext("TestService2ES").getInstance(null));

        moduleContext.publish(new ModuleStop(this));
        system.publish(new ModuleStop(this));
    }
View Full Code Here

        }
        moduleContext.publish(new ModuleStop(this));
    }

    public void testExternalServiceReferenceNotFound() throws Exception {
        CompositeContext system = runtime.getSystemContext();

        // create a test module
        Component moduleComponent = MockFactory.createCompositeComponent("module");
        runtime.registerModelObject(moduleComponent);
        CompositeContextImpl moduleContext = (CompositeContextImpl) runtime.getContext("module");
        ExternalService es = MockFactory.createESSystemBinding("TestServiceES", "tuscany.system/TestService1xEP");
        moduleContext.registerModelObject(es);

        // start the modules and test inter-module system wires
        system.publish(new ModuleStart(this));
        moduleContext.publish(new ModuleStart(this));
        try {
            moduleContext.locateService("TestServiceES");
            fail("Expected " + ServiceNotFoundException.class.getName());
        } catch (ServiceNotFoundException e) {
            // expected
        }
        moduleContext.publish(new ModuleStop(this));
        system.publish(new ModuleStop(this));
    }
View Full Code Here

    }

    public void testRuntimeBuilderAutowire() throws Exception {


        CompositeContext system = runtime.getSystemContext();
        Component builder = factory.createSystemComponent("TestBuilder", ContextFactoryBuilder.class, TestBuilder.class, Scope.MODULE);
        builder.getImplementation().setComponentType(MockFactory.getIntrospector().introspect(TestBuilder.class));
        system.registerModelObject(builder);
        system.publish(new ModuleStart(this));
        Component module1 = MockFactory.createCompositeComponent("module1");
        runtime.registerModelObject(module1);
        runtime.getContext("module1");
        Assert.assertTrue(((TestBuilder) system.getContext("TestBuilder").getInstance(null)).invoked());
        system.publish(new ModuleStop(this));
    }
View Full Code Here

                .getContext(MockFactory.POLICY_BUILDER_REGISTRY).getInstance(null);
        MockSyncInterceptor mockInterceptor = new MockSyncInterceptor();
        MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false);
        registry.registerTargetBuilder(interceptorBuilder);
        runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test.module"));
        CompositeContext child = (CompositeContext) runtime.getRootContext().getContext("test.module");
        child.registerModelObject(MockFactory.createModuleWithEntryPoint(Scope.SESSION));
        child.publish(new ModuleStart(this));

        // first session
        Object session = new Object();
        Object id = new Object();
        child.publish(new RequestStart(this, id));
        child.publish(new HttpSessionBound(this, session));

        EntryPointContext ctx = (EntryPointContext) child.getContext("source");
        Assert.assertNotNull(ctx);
        InvocationHandler handler = (InvocationHandler) ctx.getHandler();
        Assert.assertEquals(0, mockInterceptor.getCount());
        Object response = handler.invoke(null, hello, new Object[]{"foo"});
        Assert.assertEquals("Hello foo", response);
        Assert.assertEquals(1, mockInterceptor.getCount());
        child.publish(new RequestEnd(this, id));

        Object id2 = new Object();
        child.publish(new RequestStart(this, id2));
        child.publish(new HttpSessionBound(this, session));
        EntryPointContext ctx2 = (EntryPointContext) child.getContext("source");
        Assert.assertNotNull(ctx2);
        response = handler.invoke(null, hello, new Object[]{"foo"});
        Assert.assertEquals("Hello foo", response);
        Assert.assertEquals(2, mockInterceptor.getCount());
        HelloWorldService service1 = (HelloWorldService) child.getContext("target").getInstance(null);
        Assert.assertEquals(2, service1.count());
        child.publish(new RequestEnd(this, id2));
        child.publish(new HttpSessionEnd(this, session));

        // second session
        Object session2 = new Object();
        child.publish(new RequestStart(this, new Object()));
        child.publish(new HttpSessionBound(this, session2));

        ctx = (EntryPointContext) child.getContext("source");
        Assert.assertNotNull(ctx);
        Assert.assertEquals(2, mockInterceptor.getCount());
        response = handler.invoke(null, hello, new Object[]{"foo"});
        Assert.assertEquals("Hello foo", response);
        Assert.assertEquals(3, mockInterceptor.getCount());
        child.publish(new HttpSessionBound(this, session2));

        Object id3 = new Object();
        child.publish(new RequestStart(this, id3));
        child.publish(new HttpSessionBound(this, session2));
        ctx2 = (EntryPointContext) child.getContext("source");
        Assert.assertNotNull(ctx2);
        response = handler.invoke(null, hello, new Object[]{"foo"});
        Assert.assertEquals("Hello foo", response);
        Assert.assertEquals(4, mockInterceptor.getCount());
        HelloWorldService service2 = (HelloWorldService) child.getContext("target").getInstance(null);
        Assert.assertEquals(2, service2.count());
        Assert.assertEquals(2, service1.count()); //ensure sessions not crossed
        child.publish(new RequestEnd(this, session2));
        child.publish(new HttpSessionBound(this, session2));

        child.publish(new ModuleStop(this));
        runtime.stop();
    }
View Full Code Here

    public void testStatelessToStateless() throws Exception{
        RuntimeContext runtime = MockFactory.createJavaRuntime();
        Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
        Assert.assertNotNull(ctx);
        runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
        CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
        Assert.assertNotNull(testCtx);
        testCtx.registerModelObject(MockFactory.createModule(Scope.INSTANCE,Scope.INSTANCE));
        testCtx.publish(new ModuleStart(this));
       
        // first request
        Object id = new Object();
        testCtx.publish(new RequestStart(this,id));
        GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
        Assert.assertNotNull(source);
        GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
        Assert.assertNotNull(target);
        source.getGenericComponent().setString("foo");
        Assert.assertEquals(null,target.getString());
        testCtx.publish(new RequestEnd(this,new Object()));

        //second request
        Object id2 = new Object();
        testCtx.publish(new RequestStart(this,id2));
        GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
        Assert.assertNotNull(source2);
        GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);

        Assert.assertNotNull(target2);
        Assert.assertEquals(null,target2.getString());
        Assert.assertEquals(null,source2.getGenericComponent().getString());
        source2.getGenericComponent().setString("baz");
        Assert.assertEquals(null,source2.getGenericComponent().getString());
        Assert.assertEquals(null,target2.getString());
       
        testCtx.publish(new RequestEnd(this,new Object()));
    }
View Full Code Here

    public void testStatelessToRequest() throws Exception{
        RuntimeContext runtime = MockFactory.createJavaRuntime();
        Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
        Assert.assertNotNull(ctx);
        runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
        CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
        Assert.assertNotNull(testCtx);
        testCtx.registerModelObject(MockFactory.createModule(Scope.INSTANCE,Scope.REQUEST));
        testCtx.publish(new ModuleStart(this));
       
        // first request
        Object id = new Object();
        testCtx.publish(new RequestStart(this,id));
        GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
        Assert.assertNotNull(source);
        GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
        Assert.assertNotNull(target);
        source.getGenericComponent().setString("foo");
        Assert.assertEquals("foo",target.getString());
        testCtx.publish(new RequestEnd(this,new Object()));

        GenericComponent targetR1 = (GenericComponent)testCtx.getContext("target").getInstance(null);
        Assert.assertNotNull(targetR1);
        Assert.assertEquals("foo",target.getString());

        //second request
        Object id2 = new Object();
        testCtx.publish(new RequestStart(this,id2));
        GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
        Assert.assertNotNull(source2);
        GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);

        Assert.assertNotNull(target2);
        Assert.assertEquals(null,target2.getString());
        Assert.assertEquals(null,source2.getGenericComponent().getString());
        source2.getGenericComponent().setString("baz");
        Assert.assertEquals("baz",source2.getGenericComponent().getString());
        Assert.assertEquals("baz",target2.getString());
       
        testCtx.publish(new RequestEnd(this,new Object()));
      }
View Full Code Here

                .getContext(MockFactory.POLICY_BUILDER_REGISTRY).getInstance(null);
        MockSyncInterceptor mockInterceptor = new MockSyncInterceptor();
        MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false);
        registry.registerTargetBuilder(interceptorBuilder);
        runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test.module"));
        CompositeContext child = (CompositeContext) runtime.getRootContext().getContext("test.module");
        child.registerModelObject(MockFactory.createModuleWithEntryPoint(Scope.INSTANCE));
        child.publish(new ModuleStart(this));
        Object id = new Object();
        child.publish(new RequestStart(this, id));
        EntryPointContext ctx = (EntryPointContext) child.getContext("source");
        Assert.assertNotNull(ctx);
        InvocationHandler handler = (InvocationHandler) ctx.getHandler();
        Assert.assertEquals(0, mockInterceptor.getCount());
        Object response = handler.invoke(null, hello, new Object[]{"foo"});
        Assert.assertEquals("Hello foo", response);
        Assert.assertEquals(1, mockInterceptor.getCount());
        child.publish(new RequestEnd(this, id));

        // second request
        Object id2 = new Object();
        child.publish(new RequestStart(this, id2));
        ctx = (EntryPointContext) child.getContext("source");
        Assert.assertNotNull(ctx);
        handler = (InvocationHandler) ctx.getHandler();
        Assert.assertEquals(1, mockInterceptor.getCount());
        response = handler.invoke(null, hello, new Object[]{"foo"});
        Assert.assertEquals("Hello foo", response);
        Assert.assertEquals(2, mockInterceptor.getCount());
        HelloWorldService service1 = (HelloWorldService) child.getContext("target").getInstance(null);
        Assert.assertEquals(0, service1.count());
        child.publish(new RequestEnd(this, id));

        child.publish(new ModuleStop(this));
        runtime.stop();
    }
View Full Code Here

TOP

Related Classes of org.apache.tuscany.core.context.CompositeContext

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.