summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-M1-final/java/sca/core/src/test/java/org/apache/tuscany/core/system/context/CompositeNestingTestCase.java
blob: 27ffc5794eee83a99bbdc49f6d1b05438351ef35 (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
/**
 * 
 * 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.system.context;

import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.tuscany.core.context.CompositeContext;
import org.apache.tuscany.core.context.event.ModuleStart;
import org.apache.tuscany.core.context.event.ModuleStop;
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.config.ConfigurationLoadException;
import org.apache.tuscany.model.assembly.ModuleComponent;
import org.apache.tuscany.model.assembly.Scope;

/**
 * Tests registering arbirarily deep child composite contexts
 * 
 * @version $Rev$ $Date$
 */
public class CompositeNestingTestCase extends TestCase {

    /**
     * Tests registration of a 3-level deep hierarchy under the top-level system composite context
     */
    public void testSystemContext() throws Exception {
        RuntimeContext runtime = MockFactory.createCoreRuntime();
        ModuleComponent child1 = createHierarchy();
        runtime.getSystemContext().registerModelObject(child1);
        CompositeContext child1Ctx = (CompositeContext) runtime.getSystemContext().getContext("child1");
        Assert.assertNotNull(child1Ctx);
        child1Ctx.publish(new ModuleStart(this));
        analyzeLeafComponents(child1Ctx);
        CompositeContext child2Ctx = (CompositeContext) child1Ctx.getContext("child2");
        Assert.assertNotNull(child2Ctx);
        child2Ctx.publish(new ModuleStart(this));
        analyzeLeafComponents(child2Ctx);
        CompositeContext child3Ctx = (CompositeContext) child2Ctx.getContext("child3");
        Assert.assertNotNull(child3Ctx);
        child3Ctx.publish(new ModuleStart(this));
        analyzeLeafComponents(child3Ctx);
        
        Assert.assertNull(child1Ctx.getContext("child3")); // sanity check
    }

    /**
     * Tests registration of a 3-level deep hierarchy under the root application composite context
     */
    public void testRootContext() throws Exception {
        RuntimeContext runtime = MockFactory.createCoreRuntime();
        ModuleComponent child1 = createHierarchy();
        runtime.getRootContext().registerModelObject(child1);
        CompositeContext child1Ctx = (CompositeContext) runtime.getRootContext().getContext("child1");
        Assert.assertNotNull(child1Ctx);
        child1Ctx.publish(new ModuleStart(this));
        analyzeLeafComponents(child1Ctx);
        CompositeContext child2Ctx = (CompositeContext) child1Ctx.getContext("child2");
        Assert.assertNotNull(child2Ctx);
        child2Ctx.publish(new ModuleStart(this));
        analyzeLeafComponents(child2Ctx);
        CompositeContext child3Ctx = (CompositeContext) child2Ctx.getContext("child3");
        Assert.assertNotNull(child3Ctx);
        child3Ctx.publish(new ModuleStart(this));
        analyzeLeafComponents(child3Ctx);
        
        Assert.assertNull(child1Ctx.getContext("child3")); // sanity check
        child1Ctx.publish(new ModuleStop(this));
    }

    private ModuleComponent createHierarchy() throws ConfigurationLoadException {
        ModuleComponent child3 = MockFactory.createSystemModuleComponentWithWiredComponents("child3", Scope.MODULE, Scope.MODULE);
        ModuleComponent child2 = MockFactory.createSystemModuleComponentWithWiredComponents("child2", Scope.MODULE, Scope.MODULE);
        child2.getImplementation().getComponents().add(child3);
        ModuleComponent child1 = MockFactory.createSystemModuleComponentWithWiredComponents("child1", Scope.MODULE, Scope.MODULE);
        child1.getImplementation().getComponents().add(child2);
        return child1;
    }
    
    private void analyzeLeafComponents(CompositeContext ctx) throws Exception {
        Source source = (Source) ctx.getContext("source").getInstance(null);
        Assert.assertNotNull(source);
        Target target = source.getTarget();
        Assert.assertNotNull(target);
    }
}