diff options
Diffstat (limited to 'sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder')
4 files changed, 582 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/MonitorInjectionTestCase.java b/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/MonitorInjectionTestCase.java new file mode 100644 index 0000000000..fe6bff635a --- /dev/null +++ b/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/MonitorInjectionTestCase.java @@ -0,0 +1,106 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * 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.builder; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.common.monitor.MonitorFactory; +import org.apache.tuscany.core.builder.ContextFactory; +import org.apache.tuscany.core.context.CompositeContext; +import org.apache.tuscany.core.context.Context; +import org.apache.tuscany.core.context.impl.CompositeContextImpl; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.DefaultScopeStrategy; +import org.apache.tuscany.core.mock.MockConfigContext; +import org.apache.tuscany.core.mock.MockFactory; +import org.apache.tuscany.core.system.annotation.Monitor; +import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory; +import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl; +import org.apache.tuscany.model.assembly.Component; +import org.apache.tuscany.model.assembly.Scope; + +/** + * @version $Rev$ $Date$ + */ +public class MonitorInjectionTestCase extends TestCase { + private SystemContextFactoryBuilder builder; + private Component component; + + public static interface TestService { + } + + public static class TestComponent implements TestService { + @Monitor + protected Monitor1 monitor1; + Monitor2 monitor2; + + @Monitor + public void setMonitor2(Monitor2 monitor2) { + this.monitor2 = monitor2; + } + } + + public static interface Monitor1 { + } + + public static interface Monitor2 { + } + + public void testMonitorInjection() { + builder.build(component); + ContextFactory<?> contextFactory = (ContextFactory<?>) component.getContextFactory(); + Assert.assertNotNull(contextFactory); + contextFactory.prepare(createContext()); + Context ctx = contextFactory.createContext(); + + ctx.start(); + TestComponent instance = (TestComponent) ctx.getInstance(null); + assertSame(MONITOR1, instance.monitor1); + assertSame(MONITOR2, instance.monitor2); + } + + protected void setUp() throws Exception { + super.setUp(); + SystemAssemblyFactory factory = new SystemAssemblyFactoryImpl(); + MockMonitorFactory monitorFactory = new MockMonitorFactory(); + builder = new SystemContextFactoryBuilder(monitorFactory); + component = factory.createSystemComponent("test", TestService.class, TestComponent.class, Scope.MODULE); + component.getImplementation().setComponentType(MockFactory.getIntrospector().introspect(TestComponent.class)); + } + + private static final Monitor1 MONITOR1 = new Monitor1() { + }; + private static final Monitor2 MONITOR2 = new Monitor2() { + }; + + public static class MockMonitorFactory implements MonitorFactory { + public <T> T getMonitor(Class<T> monitorInterface) { + if (Monitor1.class.equals(monitorInterface)) { + return monitorInterface.cast(MONITOR1); + } else if (Monitor2.class.equals(monitorInterface)) { + return monitorInterface.cast(MONITOR2); + } else { + throw new AssertionError(); + } + } + } + + private static CompositeContext createContext() { + return new CompositeContextImpl("test.parent", null, new DefaultScopeStrategy(), new EventContextImpl(), new MockConfigContext(null)); + } +} diff --git a/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/SystemComponentImpl.java b/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/SystemComponentImpl.java new file mode 100644 index 0000000000..3398a010e9 --- /dev/null +++ b/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/SystemComponentImpl.java @@ -0,0 +1,167 @@ +/** + * + * 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.builder; + +import org.apache.tuscany.core.context.CompositeContext; +import org.apache.tuscany.core.context.AutowireContext; +import org.apache.tuscany.core.context.ConfigurationContext; +import org.apache.tuscany.core.system.annotation.Autowire; +import org.apache.tuscany.core.system.annotation.ParentContext; +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Scope; + +/** + * A system component used for unit testing + * + * @version $Rev$ $Date$ + */ +@Scope("MODULE") +public class SystemComponentImpl { + + @Autowire + protected ConfigurationContext ctx; + + @ParentContext + protected CompositeContext parent; + + @Autowire + protected AutowireContext autowireCtx; + + private ConfigurationContext ctxSetter; + + private CompositeContext parentSetter; + + private AutowireContext autowireCtxSetter; + + public ConfigurationContext getConfigContext() { + return ctx; + } + + public CompositeContext getParentContext() { + return parent; + } + + public AutowireContext getAutowireContext() { + return autowireCtx; + } + + @Autowire + public void setConfigContext(ConfigurationContext configCtx) { + ctxSetter = configCtx; + } + + public ConfigurationContext getConfigContextSetter() { + return ctxSetter; + } + + @ParentContext + public void setParentContex(CompositeContext ctx) { + parentSetter = ctx; + } + + public CompositeContext getParentContextSetter() { + return parentSetter; + } + + @Autowire + public void setAutowireContext(AutowireContext ctx) { + autowireCtxSetter = ctx; + } + + public AutowireContext getAutowireContextSetter() { + return autowireCtx; + } + + private boolean inited; + + @Init + public void init(){ + inited=true; + } + + public boolean initialized(){ + return (inited); + } + + private boolean destroyed; + + @Destroy + public void destroy(){ + destroyed=true; + } + + public boolean destroyed(){ + return (destroyed); + } + + @Property + protected int testInt; + + public int getTestInt(){ + return testInt; + } + + @Property + protected double testDouble; + + public double getTestDouble(){ + return testDouble; + } + + @Property + protected float testFloat; + + public float getTestFloat(){ + return testFloat; + } + + @Property + protected short testShort; + + public short getTestShort(){ + return testShort; + } + + @Property + protected boolean testBoolean; + + public boolean getTestBoolean(){ + return testBoolean; + } + + @Property + protected byte testByte; + + public byte getTestByte(){ + return testByte; + } + + @Property + protected char testChar; + + public char getTestChar(){ + return testChar; + } + + @Property + protected String testString; + + public String getTestString(){ + return testString; + } + + +} diff --git a/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/SystemContextFactoryBuilderTestCase.java b/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/SystemContextFactoryBuilderTestCase.java new file mode 100644 index 0000000000..f7543911a6 --- /dev/null +++ b/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/SystemContextFactoryBuilderTestCase.java @@ -0,0 +1,173 @@ +/** + * + * 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.builder; + +import junit.framework.Assert; +import junit.framework.TestCase; +import org.apache.tuscany.core.builder.ContextFactory; +import org.apache.tuscany.core.context.CompositeContext; +import org.apache.tuscany.core.context.Context; +import org.apache.tuscany.core.context.AtomicContext; +import org.apache.tuscany.core.context.impl.CompositeContextImpl; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.DefaultScopeStrategy; +import org.apache.tuscany.core.mock.MockConfigContext; +import org.apache.tuscany.core.mock.MockFactory; +import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory; +import org.apache.tuscany.core.system.assembly.SystemImplementation; +import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl; +import org.apache.tuscany.model.assembly.Component; +import org.apache.tuscany.model.assembly.ConfiguredProperty; +import org.apache.tuscany.model.assembly.Property; +import org.apache.tuscany.model.assembly.Scope; +import org.apache.tuscany.model.assembly.Service; +import org.apache.tuscany.model.assembly.ComponentType; +import org.apache.tuscany.model.types.java.JavaServiceContract; + +/** + * Tests to system components are built properly + * + * @version $Rev$ $Date$ + */ +public class SystemContextFactoryBuilderTestCase extends TestCase { + + private SystemAssemblyFactory factory = new SystemAssemblyFactoryImpl(); + + public void testComponentContextBuilder() throws Exception { + SystemContextFactoryBuilder builder = new SystemContextFactoryBuilder(null); + Component component = factory.createSystemComponent("test", null, SystemComponentImpl.class, Scope.AGGREGATE); + component.getImplementation().setComponentType(MockFactory.getIntrospector().introspect(SystemComponentImpl.class)); + ConfiguredProperty cProp = factory.createConfiguredProperty(); + Property prop = factory.createProperty(); + prop.setName("testInt"); + cProp.setValue(1); + cProp.setProperty(prop); + component.getConfiguredProperties().add(cProp); + + cProp = factory.createConfiguredProperty(); + prop = factory.createProperty(); + prop.setName("testString"); + cProp.setValue("test"); + cProp.setProperty(prop); + component.getConfiguredProperties().add(cProp); + + cProp = factory.createConfiguredProperty(); + prop = factory.createProperty(); + prop.setName("testDouble"); + cProp.setValue(1d); + cProp.setProperty(prop); + component.getConfiguredProperties().add(cProp); + + cProp = factory.createConfiguredProperty(); + prop = factory.createProperty(); + prop.setName("testFloat"); + cProp.setValue(1f); + cProp.setProperty(prop); + component.getConfiguredProperties().add(cProp); + + cProp = factory.createConfiguredProperty(); + prop = factory.createProperty(); + prop.setName("testShort"); + cProp.setValue((short) 1); + cProp.setProperty(prop); + component.getConfiguredProperties().add(cProp); + + cProp = factory.createConfiguredProperty(); + prop = factory.createProperty(); + prop.setName("testByte"); + cProp.setValue((byte) 1); + cProp.setProperty(prop); + component.getConfiguredProperties().add(cProp); + + cProp = factory.createConfiguredProperty(); + prop = factory.createProperty(); + prop.setName("testBoolean"); + cProp.setValue(Boolean.TRUE); + cProp.setProperty(prop); + component.getConfiguredProperties().add(cProp); + + cProp = factory.createConfiguredProperty(); + prop = factory.createProperty(); + prop.setName("testChar"); + cProp.setValue('1'); + cProp.setProperty(prop); + component.getConfiguredProperties().add(cProp); + + builder.build(component); + ContextFactory<AtomicContext> contextFactory = (ContextFactory<AtomicContext>) component.getContextFactory(); + Assert.assertNotNull(contextFactory); + contextFactory.prepare(createContext()); + AtomicContext ctx = contextFactory.createContext(); + + ctx.start(); + SystemComponentImpl instance = (SystemComponentImpl) ctx.getInstance(null); + Assert.assertNotNull(instance.getConfigContext()); + Assert.assertNotNull(instance.getParentContext()); + Assert.assertNotNull(instance.getAutowireContext()); + Assert.assertNotNull(instance.getConfigContextSetter()); + Assert.assertNotNull(instance.getParentContextSetter()); + Assert.assertNotNull(instance.getAutowireContextSetter()); + Assert.assertEquals(1, instance.getTestInt()); + Assert.assertEquals(1d, instance.getTestDouble()); + Assert.assertEquals(1f, instance.getTestFloat()); + Assert.assertEquals((short) 1, instance.getTestShort()); + Assert.assertTrue(instance.getTestBoolean()); + Assert.assertEquals('1', instance.getTestChar()); + Assert.assertEquals((byte) 1, instance.getTestByte()); + Assert.assertEquals("test", instance.getTestString()); + + Assert.assertTrue(instance.initialized()); + ctx.destroy(); + ctx.stop(); + Assert.assertTrue(instance.destroyed()); + } + + + public void testDefaultScopeIsModuleScope() throws Exception { + SystemContextFactoryBuilder builder = new SystemContextFactoryBuilder(null); + Component component = createSystemComponentWithNoScope("test", null, SystemComponentImpl.class); + builder.build(component); + ContextFactory<AtomicContext> contextFactory = (ContextFactory<AtomicContext>) component.getContextFactory(); + Assert.assertEquals(Scope.MODULE, contextFactory.getScope()); + } + + + + + private static CompositeContext createContext() { + return new CompositeContextImpl("test.parent", null, new DefaultScopeStrategy(), new EventContextImpl(), + new MockConfigContext(null)); + } + + private <T> Component createSystemComponentWithNoScope(String name, Class<T> service, Class<? extends T> impl) { + JavaServiceContract jsc = factory.createJavaServiceContract(); + jsc.setInterface(service); + Service s = factory.createService(); + s.setServiceContract(jsc); + + ComponentType componentType = factory.createComponentType(); + componentType.getServices().add(s); + + SystemImplementation sysImpl = factory.createSystemImplementation(); + sysImpl.setImplementationClass(impl); + sysImpl.setComponentType(componentType); + + Component sc = factory.createSimpleComponent(); + sc.setName(name); + sc.setImplementation(sysImpl); + return sc; + } + + +} diff --git a/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/impl/AssemblyVisitorTestCase.java b/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/impl/AssemblyVisitorTestCase.java new file mode 100644 index 0000000000..3d63664848 --- /dev/null +++ b/sca-java-1.x/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/system/builder/impl/AssemblyVisitorTestCase.java @@ -0,0 +1,136 @@ +/** + * + * 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.builder.impl; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.ContextFactoryBuilder; +import org.apache.tuscany.core.builder.impl.AssemblyVisitorImpl; +import org.apache.tuscany.core.mock.component.ModuleScopeSystemComponent; +import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory; +import org.apache.tuscany.core.system.assembly.SystemBinding; +import org.apache.tuscany.core.system.assembly.SystemImplementation; +import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl; +import org.apache.tuscany.model.assembly.AssemblyContext; +import org.apache.tuscany.model.assembly.AssemblyObject; +import org.apache.tuscany.model.assembly.Component; +import org.apache.tuscany.model.assembly.ComponentType; +import org.apache.tuscany.model.assembly.ConfiguredPort; +import org.apache.tuscany.model.assembly.ConfiguredReference; +import org.apache.tuscany.model.assembly.ConfiguredService; +import org.apache.tuscany.model.assembly.ContextFactoryHolder; +import org.apache.tuscany.model.assembly.EntryPoint; +import org.apache.tuscany.model.assembly.Module; +import org.apache.tuscany.model.assembly.Reference; +import org.apache.tuscany.model.assembly.Service; +import org.apache.tuscany.model.assembly.impl.AssemblyContextImpl; +import org.apache.tuscany.model.types.java.JavaServiceContract; + +/** + * Tests decorating a logical configuration model + * + * @version $Rev$ $Date$ + */ +public class AssemblyVisitorTestCase extends TestCase { + + private static final Object MARKER = new Object(); + + private SystemAssemblyFactory factory = new SystemAssemblyFactoryImpl(); + private AssemblyContext assemblyContext = new AssemblyContextImpl(factory, null, null); + + public void testModelVisit() throws Exception { + ComponentType componentType; + Service service; + SystemImplementation impl; + Component component; + + Module module = factory.createModule(); + + // create target component + componentType = factory.createComponentType(); + service = factory.createService(); + service.setName("target"); + componentType.getServices().add(service); + impl = factory.createSystemImplementation(); + impl.setComponentType(componentType); + component = factory.createSimpleComponent(); + component.setName("target"); + component.setImplementation(impl); + component.initialize(assemblyContext); + module.getComponents().add(component); + + // create source component + componentType = factory.createComponentType(); + Reference ref = factory.createReference(); + ref.setName("ref"); + componentType.getReferences().add(ref); + impl = factory.createSystemImplementation(); + impl.setComponentType(componentType); + component = factory.createSimpleComponent(); + component.setName("source"); + component.setImplementation(impl); + ConfiguredReference cRef = factory.createConfiguredReference("ref", "target"); + component.getConfiguredReferences().add(cRef); + component.initialize(assemblyContext); + module.getComponents().add(component); + + EntryPoint ep = factory.createEntryPoint(); + JavaServiceContract contract = factory.createJavaServiceContract(); + contract.setInterface(ModuleScopeSystemComponent.class); + service = factory.createService(); + service.setServiceContract(contract); + ConfiguredService cService = factory.createConfiguredService(); + cService.setPort(service); + cService.initialize(assemblyContext); + ep.setConfiguredService(cService); + SystemBinding binding = factory.createSystemBinding(); + ep.getBindings().add(binding); + ConfiguredReference cEpRef = factory.createConfiguredReference(); + Reference epRef = factory.createReference(); + cEpRef.setPort(epRef); + ep.setConfiguredReference(cEpRef); + ep.initialize(assemblyContext); + module.getEntryPoints().add(ep); + + List<ContextFactoryBuilder> builders = new ArrayList<ContextFactoryBuilder>(); + builders.add(new TestBuilder()); + AssemblyVisitorImpl visitor = new AssemblyVisitorImpl(builders); + module.initialize(assemblyContext); + visitor.start(module); + + Assert.assertSame(MARKER, component.getContextFactory()); + Assert.assertSame(MARKER, cRef.getProxyFactory()); + Assert.assertSame(MARKER, ep.getContextFactory()); + Assert.assertSame(MARKER, cEpRef.getProxyFactory()); + + } + + private static class TestBuilder implements ContextFactoryBuilder { + public void build(AssemblyObject model) throws BuilderException { + if (model instanceof ConfiguredPort) { + ((ConfiguredPort) model).setProxyFactory(MARKER); + } + if (model instanceof ContextFactoryHolder) { + ((ContextFactoryHolder) model).setContextFactory(MARKER); + } + } + + } + +} |