summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/integration/IntraCompositeWireIntegrationTestCase.java
blob: 44be759b69c624a020c838b04f18b583b6bef44e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 * 
 * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.apache.tuscany.core.integration;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.apache.tuscany.core.config.ConfigurationException;
import org.apache.tuscany.core.context.AtomicContext;
import org.apache.tuscany.core.context.CompositeContext;
import org.apache.tuscany.core.context.event.ModuleStop;
import org.apache.tuscany.core.context.event.ModuleStart;
import org.apache.tuscany.core.mock.MockFactory;
import org.apache.tuscany.core.mock.component.Source;
import org.apache.tuscany.core.mock.component.Target;
import org.apache.tuscany.core.runtime.RuntimeContext;
import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory;
import org.apache.tuscany.core.system.assembly.SystemModule;
import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl;
import org.apache.tuscany.core.system.context.SystemCompositeContextImpl;
import org.apache.tuscany.model.assembly.Module;
import org.apache.tuscany.model.assembly.ModuleComponent;
import org.apache.tuscany.model.assembly.Scope;
import org.apache.tuscany.model.assembly.Service;
import org.apache.tuscany.model.types.java.JavaServiceContract;

/**
 * Tests intra-composite system wires are properly constructed in the runtime
 * 
 * @version $Rev$ $Date$
 */
public class IntraCompositeWireIntegrationTestCase extends TestCase {

  
    public void testWireConstruction2() throws Exception {
        RuntimeContext runtime = MockFactory.createCoreRuntime();
        ModuleComponent moduleComponent = createSystemCompositeComponent("test.system");
        Module module = MockFactory.createSystemModuleWithWiredComponents("system.module",Scope.MODULE, Scope.MODULE);
        moduleComponent.setImplementation(module);
        runtime.getSystemContext().registerModelObject(moduleComponent);
        CompositeContext context = (CompositeContext) runtime.getSystemContext().getContext("test.system");
        context.publish(new ModuleStart(this));
        //context.registerModelObject(module);
        Source source = (Source) ((AtomicContext)context.getContext("source")).getTargetInstance();
        Assert.assertNotNull(source);
        Target targetRef = source.getTarget();
        Assert.assertNotNull(targetRef);
        Target target = (Target) ((AtomicContext)context.getContext("target")).getTargetInstance();
        Assert.assertSame(target, targetRef);
        Source source2 = (Source) ((AtomicContext)context.getContext("source")).getTargetInstance();
        Assert.assertSame(target, source2.getTarget());
        context.publish(new ModuleStop(this));
        context.stop();
    }

    private static SystemAssemblyFactory systemFactory = new SystemAssemblyFactoryImpl();

    /**
     * Creates an composite component with the given name
     */
    public static ModuleComponent createSystemCompositeComponent(String name) {
        ModuleComponent sc = systemFactory.createModuleComponent();
        SystemModule impl = systemFactory.createSystemModule();
        impl.setImplementationClass(SystemCompositeContextImpl.class);
        sc.setImplementation(impl);
        Service s = systemFactory.createService();
        JavaServiceContract ji = systemFactory.createJavaServiceContract();
        s.setServiceContract(ji);
        ji.setScope(Scope.AGGREGATE);
        impl.setComponentType(systemFactory.createComponentType());
        impl.getComponentType().getServices().add(s);
        sc.setName(name);
        sc.setImplementation(impl);
        return sc;
    }
    
    
    public void testWireConstruction() throws ConfigurationException {
        RuntimeContext runtime = MockFactory.createCoreRuntime();
        runtime.getSystemContext().registerModelObject(MockFactory.createSystemCompositeComponent("test.system"));
        CompositeContext context = (CompositeContext) runtime.getSystemContext().getContext("test.system");

        context.publish(new ModuleStart(this));
        context.registerModelObject(MockFactory.createSystemModuleWithWiredComponents("system.module",Scope.MODULE,Scope.MODULE));
        Source source = (Source) ((AtomicContext)context.getContext("source")).getTargetInstance();
        Assert.assertNotNull(source);
        Target targetRef = source.getTarget();
        Assert.assertNotNull(targetRef);
        Target target = (Target) ((AtomicContext)context.getContext("target")).getTargetInstance();
        Assert.assertSame(target, targetRef);
        Source source2 = (Source) ((AtomicContext)context.getContext("source")).getTargetInstance();
        Assert.assertSame(target, source2.getTarget());
        context.publish(new ModuleStop(this));
        context.stop();
    }
}