summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/runtime/RuntimeContextImplTestCase.java
blob: d9596239fade05e44da850ffd6df4971d1174441 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
 * 
 * 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.runtime;

import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.tuscany.common.monitor.MonitorFactory;
import org.apache.tuscany.common.monitor.impl.NullMonitorFactory;
import org.apache.tuscany.core.builder.ContextFactoryBuilder;
import org.apache.tuscany.core.builder.ContextFactoryBuilderRegistry;
import org.apache.tuscany.core.client.BootstrapHelper;
import org.apache.tuscany.core.config.ConfigurationException;
import org.apache.tuscany.core.context.CompositeContext;
import org.apache.tuscany.core.context.Lifecycle;
import org.apache.tuscany.core.context.ServiceNotFoundException;
import org.apache.tuscany.core.context.event.ModuleStart;
import org.apache.tuscany.core.context.event.ModuleStop;
import org.apache.tuscany.core.context.impl.CompositeContextImpl;
import org.apache.tuscany.core.mock.MockFactory;
import org.apache.tuscany.core.mock.component.ModuleScopeSystemComponent;
import org.apache.tuscany.core.mock.component.ModuleScopeSystemComponentImpl;
import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory;
import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl;
import org.apache.tuscany.core.system.context.TestBuilder;
import org.apache.tuscany.model.assembly.Component;
import org.apache.tuscany.model.assembly.EntryPoint;
import org.apache.tuscany.model.assembly.ExternalService;
import org.apache.tuscany.model.assembly.Scope;
import org.apache.tuscany.model.assembly.Service;
import org.apache.tuscany.model.types.java.JavaServiceContract;

/**
 * Performs basic tests on the runtime context
 *
 * @version $Rev$ $Date$
 */
public class RuntimeContextImplTestCase extends TestCase {

    private SystemAssemblyFactory systemFactory = new SystemAssemblyFactoryImpl();

    private ContextFactoryBuilderRegistry builderRegistry;

    private SystemAssemblyFactory factory;
    private MonitorFactory monitorFactory;
    private RuntimeContext runtime;

    /**
     * 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();
        service.setName("system.child/TestService2EP");
        ep.getConfiguredReference().getTargetConfiguredServices().get(0).setPort(service);
        JavaServiceContract inter = systemFactory.createJavaServiceContract();
        inter.setInterface(ModuleScopeSystemComponentImpl.class);
        service.setServiceContract(inter);
        system.registerModelObject(ep);
        system.publish(new ModuleStart(this));
        Assert.assertNotNull(system.getContext("TestService1").getInstance(null));
        Assert.assertNotNull(system.getContext("TestService2EP").getInstance(null));

        // 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.createESSystemBinding("TestService2ES", "tuscany.system/TestService2EP");
        moduleContext.registerModelObject(es);
        moduleContext.publish(new ModuleStart(this));
        Assert.assertNotNull(moduleContext.getContext("TestService2ES").getInstance(null));

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

    /**
     * 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));
    }

    public void testServiceNotFound() throws Exception {
        // create a test module
        Component moduleComponent = MockFactory.createCompositeComponent("module");
        runtime.registerModelObject(moduleComponent);
        CompositeContextImpl moduleContext = (CompositeContextImpl) runtime.getContext("module");
        moduleContext.publish(new ModuleStart(this));
        try {
            moduleContext.locateService("TestService");
            fail("Expected " + ServiceNotFoundException.class.getName());
        } catch (ServiceNotFoundException e) {
            // expected
        }
        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));
    }

    public void testEntryPointReferenceNotFound() throws Exception {
        // create a test module
        Component moduleComponent = MockFactory.createCompositeComponent("module");
        runtime.registerModelObject(moduleComponent);

        Component component = factory.createSystemComponent("NoService", ModuleScopeSystemComponent.class, ModuleScopeSystemComponentImpl.class, Scope.MODULE);
        // do not register the above component!

        CompositeContextImpl moduleContext = (CompositeContextImpl) runtime.getContext("module");
        EntryPoint epSystemBinding = MockFactory.createEPSystemBinding("TestServiceEP", ModuleScopeSystemComponent.class, "NoReference", component);
        moduleContext.registerModelObject(epSystemBinding);

        moduleContext.publish(new ModuleStart(this));
        try {
            moduleContext.locateService("TestServiceEP");
            fail("Expected " + ServiceNotFoundException.class.getName());
        } catch (ServiceNotFoundException e) {
            // expected
        }
        moduleContext.publish(new ModuleStop(this));
    }

    /**
     * Test two module components that have external services wired to entry points contained in each
     */
    public void testCircularWires() throws Exception {
        // create a test modules
        Component module1 = MockFactory.createCompositeComponent("module1");
        runtime.registerModelObject(module1);
        Component module2 = MockFactory.createCompositeComponent("module2");
        runtime.registerModelObject(module2);

        CompositeContextImpl moduleContext1 = (CompositeContextImpl) runtime.getContext("module1");
        CompositeContextImpl moduleContext2 = (CompositeContextImpl) runtime.getContext("module2");

        Component component1 = factory.createSystemComponent("Component1", ModuleScopeSystemComponent.class, ModuleScopeSystemComponentImpl.class, Scope.MODULE);
        EntryPoint entryPoint1 = MockFactory.createEPSystemBinding("EntryPoint1", ModuleScopeSystemComponent.class, "Component1", component1);
        ExternalService externalService1 = MockFactory.createESSystemBinding("ExternalService1", "module2/EntryPoint2");
        moduleContext1.registerModelObject(component1);
        moduleContext1.registerModelObject(entryPoint1);
        moduleContext1.registerModelObject(externalService1);

        Component component2 = factory.createSystemComponent("Component2", ModuleScopeSystemComponent.class, ModuleScopeSystemComponentImpl.class, Scope.MODULE);
        EntryPoint entryPoint2 = MockFactory.createEPSystemBinding("EntryPoint2", ModuleScopeSystemComponent.class, "Component2", component2);
        ExternalService externalService2 = MockFactory.createESSystemBinding("ExternalService2", "module1/EntryPoint1");
        moduleContext2.registerModelObject(component2);
        moduleContext2.registerModelObject(entryPoint2);
        moduleContext2.registerModelObject(externalService2);

        moduleContext1.publish(new ModuleStart(this));
        moduleContext2.publish(new ModuleStart(this));
        Assert.assertNotNull(moduleContext2.getContext("ExternalService2").getInstance(null));
        Assert.assertNotNull(moduleContext1.getContext("ExternalService1").getInstance(null));
    }

    /**
     * Tests that a circular reference between an external service in one module and an entry point in another
     * is caught as an error condition FIXME this must be implemented
     */
    public void testInterModuleCircularReference() throws Exception {
        // create a test modules
        Component module1 = MockFactory.createCompositeComponent("module1");
        runtime.registerModelObject(module1);
        Component module2 = MockFactory.createCompositeComponent("module2");
        runtime.registerModelObject(module2);

        CompositeContextImpl moduleContext1 = (CompositeContextImpl) runtime.getContext("module1");
        CompositeContextImpl moduleContext2 = (CompositeContextImpl) runtime.getContext("module2");
        ExternalService externalService1 = MockFactory.createESSystemBinding("ExternalService1", "module2/EntryPoint2");
        EntryPoint entryPoint1 = MockFactory.createEPSystemBinding("EntryPoint1", ModuleScopeSystemComponent.class,
                "ExternalService1", externalService1);
        ExternalService externalService2 = MockFactory.createESSystemBinding("ExternalService2", "module1/EntryPoint1");
        EntryPoint entryPoint2 = MockFactory.createEPSystemBinding("EntryPoint2", ModuleScopeSystemComponent.class,
                "ExternalService2", externalService2);
        try {
            // FIXME this should throw a circular reference exception
            moduleContext1.registerModelObject(externalService1);
            moduleContext1.registerModelObject(entryPoint1);
            moduleContext2.registerModelObject(externalService2);
            moduleContext2.registerModelObject(entryPoint2);
            // FIXME implement fail("Expected " + ConfigurationException.class.getName());
        } catch (ConfigurationException e) {
            // expected
        }
    }

    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));
    }

    protected void setUp() throws Exception {
        super.setUp();
        monitorFactory = new NullMonitorFactory();
        builderRegistry = BootstrapHelper.bootstrapContextFactoryBuilders(monitorFactory);
        factory = new SystemAssemblyFactoryImpl();

        runtime = new RuntimeContextImpl(monitorFactory, builderRegistry, null);
        runtime.start();
    }

    protected void tearDown() throws Exception {
        runtime.stop();
        super.tearDown();
    }
}