summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-final/java/sca/core/src/test/java/org/apache/tuscany/core/mock/MockFactory.java
blob: 87e92d783cd0245f92fd8a38396f9f2bba3ce6d6 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
 * 
 * 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.mock;

import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Method;

import org.apache.tuscany.common.monitor.impl.NullMonitorFactory;
import org.apache.tuscany.core.builder.ContextFactoryBuilder;
import org.apache.tuscany.core.client.BootstrapHelper;
import org.apache.tuscany.core.config.ComponentTypeIntrospector;
import org.apache.tuscany.core.config.ConfigurationLoadException;
import org.apache.tuscany.core.config.impl.Java5ComponentTypeIntrospector;
import org.apache.tuscany.core.config.processor.ProcessorUtils;
import org.apache.tuscany.core.context.impl.CompositeContextImpl;
import org.apache.tuscany.core.extension.config.ImplementationProcessor;
import org.apache.tuscany.core.extension.config.extensibility.DestroyInvokerExtensibilityElement;
import org.apache.tuscany.core.mock.component.ModuleScopeSystemComponent;
import org.apache.tuscany.core.mock.component.ModuleScopeSystemComponentImpl;
import org.apache.tuscany.core.mock.component.Source;
import org.apache.tuscany.core.mock.component.SourceImpl;
import org.apache.tuscany.core.mock.component.Target;
import org.apache.tuscany.core.mock.component.TargetImpl;
import org.apache.tuscany.core.runtime.RuntimeContext;
import org.apache.tuscany.core.runtime.RuntimeContextImpl;
import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory;
import org.apache.tuscany.core.system.assembly.SystemBinding;
import org.apache.tuscany.core.system.assembly.SystemModule;
import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl;
import org.apache.tuscany.core.system.builder.SystemContextFactoryBuilder;
import org.apache.tuscany.core.system.builder.SystemEntryPointBuilder;
import org.apache.tuscany.core.system.builder.SystemExternalServiceBuilder;
import org.apache.tuscany.core.system.context.SystemCompositeContextImpl;
import org.apache.tuscany.core.injection.MethodEventInvoker;
import org.apache.tuscany.model.assembly.AssemblyContext;
import org.apache.tuscany.model.assembly.Component;
import org.apache.tuscany.model.assembly.ComponentType;
import org.apache.tuscany.model.assembly.ConfiguredReference;
import org.apache.tuscany.model.assembly.ConfiguredService;
import org.apache.tuscany.model.assembly.EntryPoint;
import org.apache.tuscany.model.assembly.ExternalService;
import org.apache.tuscany.model.assembly.Module;
import org.apache.tuscany.model.assembly.ModuleComponent;
import org.apache.tuscany.model.assembly.Multiplicity;
import org.apache.tuscany.model.assembly.Part;
import org.apache.tuscany.model.assembly.Reference;
import org.apache.tuscany.model.assembly.Scope;
import org.apache.tuscany.model.assembly.Service;
import org.apache.tuscany.model.assembly.impl.AssemblyContextImpl;
import org.apache.tuscany.model.types.java.JavaServiceContract;

/**
 * Generates test components, modules, and runtime artifacts
 *
 * @version $Rev$ $Date$
 */
public class MockFactory {

    private static SystemAssemblyFactory systemFactory = new SystemAssemblyFactoryImpl();
    private static AssemblyContext assemblyContext = new AssemblyContextImpl(systemFactory, null, null);
    private static ComponentTypeIntrospector introspector;
    private static ComponentType systemComponentType;
    private static ComponentType compositeComponentType;

    private MockFactory() {
    }

    public static ComponentType getComponentType() throws ConfigurationLoadException {
        if (systemComponentType == null) {
            systemComponentType = getIntrospector().introspect(SystemCompositeContextImpl.class);
        }
        return systemComponentType;
    }

    public static ComponentType getCompositeComponentType() throws ConfigurationLoadException {
        if (compositeComponentType == null) {
            compositeComponentType = getIntrospector().introspect(CompositeContextImpl.class);
        }
        return compositeComponentType;
    }

    public static ComponentTypeIntrospector getIntrospector() {
        if (introspector == null) {
            introspector = ProcessorUtils.createCoreIntrospector(systemFactory);
        }
        return introspector;
    }

    /**
     * Creates an composite component with the given name
     */
    public static ModuleComponent createCompositeComponent(String name) throws ConfigurationLoadException {
        ModuleComponent sc = systemFactory.createModuleComponent();
        Module impl = systemFactory.createModule();
        impl.setName(name);
        //impl.setImplementationClass(CompositeContextImpl.class);
        sc.setImplementation(impl);
        impl.setImplementationClass(CompositeContextImpl.class);
        impl.setComponentType(getCompositeComponentType());
        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;
    }

    /**
     * Creates an composite component with the given name
     */
    public static ModuleComponent createSystemCompositeComponent(String name) throws ConfigurationLoadException {
        ModuleComponent sc = systemFactory.createModuleComponent();
        SystemModule impl = systemFactory.createSystemModule();
        impl.setName(name);
        impl.setImplementationClass(SystemCompositeContextImpl.class);
        impl.setComponentType(getComponentType());
        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;
    }

    /**
     * Creates a basic entry point with no configured reference using the system binding
     *
     * @param name     the name of the entry point
     * @param interfaz the inteface exposed by the entry point
     * @param refName  the name of the entry point reference
     */
    public static EntryPoint createEPSystemBinding(String name, Class interfaz, String refName) {
        return createEPSystemBinding(name, interfaz, refName, null);
    }

    /**
     * Creates an entry point wired to the given target (e.g. component, external service) using the system
     * binding
     *
     * @param name     the name of the entry point
     * @param interfaz the inteface exposed by the entry point
     * @param refName  the name of the entry point reference
     * @param target   the target the entry point is wired to
     */
    public static EntryPoint createEPSystemBinding(String name, Class interfaz, String refName, Part target) {
        JavaServiceContract contract = systemFactory.createJavaServiceContract();
        contract.setInterface(interfaz);

        EntryPoint ep = systemFactory.createEntryPoint();
        ep.setName(name);

        Reference ref = systemFactory.createReference();
        ref.setName(refName);
        ref.setServiceContract(contract);
        ConfiguredReference configuredReference = systemFactory.createConfiguredReference();
        configuredReference.setPort(ref);
        Service service = systemFactory.createService();
        service.setServiceContract(contract);

        ConfiguredService cService = systemFactory.createConfiguredService();
        cService.setPort(service);
        cService.initialize(assemblyContext);

        configuredReference.getTargetConfiguredServices().add(cService);
        ep.setConfiguredReference(configuredReference);

        Service epService = systemFactory.createService();
        epService.setServiceContract(contract);

        ConfiguredService epCService = systemFactory.createConfiguredService();
        epCService.initialize(assemblyContext);
        epCService.setPort(epService);

        ep.setConfiguredService(epCService);
        SystemBinding binding = systemFactory.createSystemBinding();
        ep.getBindings().add(binding);
        if (target != null) {
            if (target instanceof Component) {
                ((Component) target).getConfiguredServices().add(cService);
                // cService.
            } else if (target instanceof ExternalService) {
                ((ExternalService) target).setConfiguredService(cService);
            }
            target.initialize(assemblyContext);
        }
        ep.initialize(null);
        return ep;
    }

    /**
     * Creates an entry point that should be wired to the given target (e.g. component, external service)
     * using the system binding. The system assembly process should resolve the target name to an actual
     * target configuration.
     *
     * @param name          the name of the entry point
     * @param interfaz      the inteface exposed by the entry point
     * @param refName       the name of the entry point reference
     * @param componentName the name of the target to resolve
     */
    public static EntryPoint createEntryPointWithStringRef(String name, Class interfaz, String refName, String componentName) {
        EntryPoint ep = createEPSystemBinding(name, interfaz, refName, null);
        ConfiguredReference cRef = systemFactory.createConfiguredReference();
        Reference ref = systemFactory.createReference();
        cRef.setPort(ref);
        Service service = systemFactory.createService();
        service.setName(componentName);
        ConfiguredService cService = systemFactory.createConfiguredService();
        cService.setPort(service);
        cRef.getTargetConfiguredServices().add(cService);
        cRef.initialize(assemblyContext);
        cService.initialize(assemblyContext);
        JavaServiceContract contract = systemFactory.createJavaServiceContract();
        contract.setInterface(interfaz);
        ref.setServiceContract(contract);
        ep.setConfiguredReference(cRef);
        ep.initialize(assemblyContext);
        return ep;
    }

    /**
     * Creates an external service configured with a {@link SystemBinding}
     */
    public static ExternalService createESSystemBinding(String name, String refName) {
        ExternalService es = systemFactory.createExternalService();
        es.setName(name);
        ConfiguredService configuredService = systemFactory.createConfiguredService();
        es.setConfiguredService(configuredService);
        SystemBinding binding = systemFactory.createSystemBinding();
        binding.setTargetName(refName);
        es.getBindings().add(binding);
        es.initialize(null);
        return es;
    }

    /**
     * Creates an external service that specifies an autowire of the given type
     */
    public static ExternalService createAutowirableExternalService(String name, Class type) {
        ExternalService es = systemFactory.createExternalService();
        es.setName(name);
        JavaServiceContract inter = systemFactory.createJavaServiceContract();
        inter.setInterface(type);
        Service service = systemFactory.createService();
        service.setServiceContract(inter);
        ConfiguredService cService = systemFactory.createConfiguredService();
        cService.setPort(service);
        cService.initialize(assemblyContext);
        es.setConfiguredService(cService);
        es.getBindings().add(systemFactory.createSystemBinding());
        es.initialize(null);
        return es;
    }

    /**
     * Creates a test system module with a module-scoped component and entry point
     */
    public static Module createSystemModule() throws ConfigurationLoadException {
        Module module = systemFactory.createSystemModule();
        module.setName("system.module");

        // create test component
        Component component = systemFactory.createSystemComponent("TestService1", ModuleScopeSystemComponent.class,
                ModuleScopeSystemComponentImpl.class, Scope.MODULE);
        component.getImplementation().setComponentType(getIntrospector().introspect(ModuleScopeSystemComponent.class));
        module.getComponents().add(component);

        // create the entry point
        EntryPoint ep = createEPSystemBinding("TestService1EP", ModuleScopeSystemComponent.class, "target", component);
        module.getEntryPoints().add(ep);

        module.initialize(assemblyContext);
        module.setImplementationClass(SystemCompositeContextImpl.class);
        module.setComponentType(getComponentType());
        return module;
    }

    public static <T> Component createSystemComponent(String name, Class<T> service, Class<? extends T> impl, Scope scope) throws ConfigurationLoadException {
        Component c = systemFactory.createSystemComponent(name, service, impl, scope);
        c.getImplementation().setComponentType(getIntrospector().introspect(impl));
        for (Service s : c.getImplementation().getComponentType().getServices()) {
             s.getServiceContract().setScope(scope);    //hack
        }

        return c;
    }

    /**
     * Creates a test system module with source and target components wired together.
     *
     * @see org.apache.tuscany.core.mock.component.Source
     * @see org.apache.tuscany.core.mock.component.Target
     */
    public static Module createSystemModuleWithWiredComponents(String moduleName, Scope sourceScope, Scope targetScope) throws ConfigurationLoadException {

        // create the target component
        Component target = systemFactory.createSystemComponent("target", Target.class, TargetImpl.class, targetScope);
        target.initialize(assemblyContext);

        // create the source componentType
        Component source = systemFactory.createSystemComponent("source", Source.class, SourceImpl.class, sourceScope);
        ComponentType sourceComponentType = source.getImplementation().getComponentType();
        List<Reference> references = sourceComponentType.getReferences();
        List<ConfiguredReference> configuredReferences = source.getConfiguredReferences();

        // wire source to target
        references.add(systemFactory.createReference("setTarget", Target.class));
        ConfiguredReference configuredReference = systemFactory.createConfiguredReference("setTarget", "target");
        configuredReferences.add(configuredReference);

        // wire multiplicity using a setter
        references.add(systemFactory.createReference("setTargets", Target.class, Multiplicity.ONE_N));
        configuredReference = systemFactory.createConfiguredReference("setTargets", "target");
        configuredReferences.add(configuredReference);

        // wire multiplicity using a field
        references.add(systemFactory.createReference("targetsThroughField", Target.class, Multiplicity.ONE_N));
        configuredReference = systemFactory.createConfiguredReference("targetsThroughField", "target");
        configuredReferences.add(configuredReference);

        // wire multiplicity using a setter
        references.add(systemFactory.createReference("setArrayOfTargets", Target.class, Multiplicity.ONE_N));
        configuredReference = systemFactory.createConfiguredReference("setArrayOfTargets", "target");
        configuredReferences.add(configuredReference);

        source.initialize(assemblyContext);

        Module module = systemFactory.createSystemModule();
        module.setImplementationClass(SystemCompositeContextImpl.class);
        module.setComponentType(getComponentType());
        module.setName(moduleName);
        module.getComponents().add(source);
        module.getComponents().add(target);
        module.initialize(assemblyContext);
        return module;
    }

    /**
     * Creates a test system module component with source and target components wired together.
     *
     * @see org.apache.tuscany.core.mock.component.Source
     * @see org.apache.tuscany.core.mock.component.Target
     */
    public static ModuleComponent createSystemModuleComponentWithWiredComponents(String moduleComponentName, Scope sourceScope,
                                                                                 Scope targetScope) throws ConfigurationLoadException {
        ModuleComponent mc = systemFactory.createModuleComponent();
        mc.setName(moduleComponentName);
        mc.setImplementation(createSystemModuleWithWiredComponents(moduleComponentName + ".module", sourceScope, targetScope));
        return mc;
    }

    /**
     * Creates a test system module component with a module-scoped component and entry point
     */
    public static Module createSystemChildModule() throws ConfigurationLoadException {
        Module module = systemFactory.createSystemModule();
        module.setName("system.test.module");
        module.setImplementationClass(SystemCompositeContextImpl.class);
        module.setComponentType(getComponentType());

        // create test component
        Component component = systemFactory.createSystemComponent("TestService2", ModuleScopeSystemComponent.class,
                ModuleScopeSystemComponentImpl.class, Scope.MODULE);
        module.getComponents().add(component);

        // create the entry point
        EntryPoint ep = createEPSystemBinding("TestService2EP", ModuleScopeSystemComponent.class, "target", component);
        module.getEntryPoints().add(ep);

        module.initialize(assemblyContext);
        return module;
    }

    /**
     * Returns a collection of bootstrap configuration builders
     */
    public static List<ContextFactoryBuilder> createSystemBuilders() {
        List<ContextFactoryBuilder> builders = new ArrayList<ContextFactoryBuilder>();
        builders.add((new SystemContextFactoryBuilder(null)));
        builders.add(new SystemEntryPointBuilder());
        builders.add(new SystemExternalServiceBuilder());
        return builders;
    }

    /**
     * Creates a default {@link RuntimeContext} configured with support for Java component implementations
     */
    public static RuntimeContext createCoreRuntime() {
        NullMonitorFactory monitorFactory = new NullMonitorFactory();
        RuntimeContext runtime = new RuntimeContextImpl(monitorFactory, BootstrapHelper.bootstrapContextFactoryBuilders(monitorFactory), null);
        runtime.start();
        return runtime;
    }

}