diff options
Diffstat (limited to 'sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation')
66 files changed, 8895 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/IntrospectionRegistryTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/IntrospectionRegistryTestCase.java new file mode 100644 index 0000000000..fb5e7fbb1f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/IntrospectionRegistryTestCase.java @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation; + +import java.lang.reflect.Method; +import java.lang.reflect.Field; +import java.lang.reflect.Constructor; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessor; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +import junit.framework.TestCase; +import org.apache.tuscany.core.implementation.IntrospectionRegistryImpl.Monitor; +import org.apache.tuscany.core.monitor.NullMonitorFactory; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class IntrospectionRegistryTestCase extends TestCase { + + private Monitor monitor; + + public void testRegister() throws Exception { + IntrospectionRegistryImpl registry = new IntrospectionRegistryImpl(monitor); + ImplementationProcessor processor = EasyMock.createNiceMock(ImplementationProcessor.class); + registry.registerProcessor(processor); + } + + public void testUnegister() throws Exception { + IntrospectionRegistryImpl registry = new IntrospectionRegistryImpl(monitor); + ImplementationProcessor processor = EasyMock.createNiceMock(ImplementationProcessor.class); + registry.registerProcessor(processor); + registry.unregisterProcessor(processor); + } + + @SuppressWarnings("unchecked") + public void testWalk() throws Exception { + IntrospectionRegistryImpl registry = new IntrospectionRegistryImpl(monitor); + ImplementationProcessor processor = EasyMock.createMock(ImplementationProcessor.class); + processor.visitClass(EasyMock.isA(CompositeComponent.class), + EasyMock.eq(Bar.class), + EasyMock.isA(PojoComponentType.class), + EasyMock.isA(DeploymentContext.class)); + processor.visitConstructor(EasyMock.isA(CompositeComponent.class), + EasyMock.isA(Constructor.class), + EasyMock.isA(PojoComponentType.class), + EasyMock.isA(DeploymentContext.class)); + processor.visitMethod(EasyMock.isA(CompositeComponent.class), + EasyMock.isA(Method.class), + EasyMock.isA(PojoComponentType.class), + EasyMock.isA(DeploymentContext.class)); + processor.visitField(EasyMock.isA(CompositeComponent.class), + EasyMock.isA(Field.class), + EasyMock.isA(PojoComponentType.class), + EasyMock.isA(DeploymentContext.class)); + processor.visitSuperClass(EasyMock.isA(CompositeComponent.class), + EasyMock.isA(Class.class), + EasyMock.isA(PojoComponentType.class), + EasyMock.isA(DeploymentContext.class)); + processor.visitEnd(EasyMock.isA(CompositeComponent.class), + EasyMock.isA(Class.class), + EasyMock.isA(PojoComponentType.class), + EasyMock.isA(DeploymentContext.class)); + + // mock.expects(once()).method("visitClass"); +// mock.expects(once()).method("visitMethod"); +// mock.expects(once()).method("visitField"); +// mock.expects(once()).method("visitConstructor"); +// mock.expects(once()).method("visitSuperClass"); +// mock.expects(once()).method("visitEnd"); + EasyMock.replay(processor); + registry.registerProcessor(processor); + registry.introspect(EasyMock.createNiceMock(CompositeComponent.class), + Bar.class, + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(), + EasyMock.createNiceMock(DeploymentContext.class)); + EasyMock.verify(processor); + } + + + protected void setUp() throws Exception { + super.setUp(); + monitor = new NullMonitorFactory().getMonitor(Monitor.class); + } + + private class Baz { + + } + + private class Bar extends Baz { + + protected String bar; + + public Bar() { + } + + public void bar() { + } + + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/AutowireResolutionTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/AutowireResolutionTestCase.java new file mode 100644 index 0000000000..7d9e2a75ee --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/AutowireResolutionTestCase.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import org.apache.tuscany.spi.component.CompositeComponent; + +import junit.framework.TestCase; +import org.easymock.EasyMock; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +/** + * Verfies specific autowire resolution scenarios + * + * @version $Rev$ $Date$ + */ +public class AutowireResolutionTestCase extends TestCase { + + public void testConstruction() { + CompositeComponent parent = createMock(CompositeComponent.class); + replay(parent); + CompositeComponent component = new CompositeComponentImpl("test", parent, null, null); + assertEquals("test", component.getName()); + assertSame(parent, component.getParent()); + verify(parent); + } + + public void testResolveToSelf() { + CompositeComponent parent = createMock(CompositeComponent.class); + replay(parent); + CompositeComponent component = new CompositeComponentImpl("test", parent, null, null); + assertSame(component, component.resolveInstance(CompositeComponent.class)); + verify(parent); + } + + public void testSystemResolveToSelf() { + CompositeComponent parent = createMock(CompositeComponent.class); + replay(parent); + CompositeComponent component = new CompositeComponentImpl("test", parent, null, null); + assertSame(component, component.resolveSystemInstance(CompositeComponent.class)); + verify(parent); + } + + public void testResolvedByAutowire() { + Foo foo = new Foo() { + }; + CompositeComponent parent = createMock(CompositeComponent.class); + EasyMock.expect(parent.resolveInstance(eq(Foo.class))).andReturn(foo); + replay(parent); + CompositeComponent component = new CompositeComponentImpl("test", parent, null, null); + assertSame(foo, component.resolveInstance(Foo.class)); + verify(parent); + } + + public void testSystemResolvedByAutowire() { + Foo foo = new Foo() { + }; + CompositeComponent parent = createMock(CompositeComponent.class); + EasyMock.expect(parent.resolveSystemInstance(eq(Foo.class))).andReturn(foo); + replay(parent); + CompositeComponent component = new CompositeComponentImpl("test", parent, null, null); + assertSame(foo, component.resolveSystemInstance(Foo.class)); + verify(parent); + } + + /** + * Verify parent resolution strategy for application services + */ + public void testNamespaceIsolationAutowire() { + Foo foo = new Foo() { + }; + CompositeComponent parent = new CompositeComponentImpl("parent", null, null, null); + parent.registerJavaObject("foo", Foo.class, foo); + CompositeComponent component = new CompositeComponentImpl("test", parent, null, null); + assertNull(component.resolveInstance(Foo.class)); + } + + protected void setUp() throws Exception { + super.setUp(); + } + + public static interface Foo { + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/AutowireTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/AutowireTestCase.java new file mode 100644 index 0000000000..d565777cd2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/AutowireTestCase.java @@ -0,0 +1,231 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.Reference; +import org.apache.tuscany.spi.component.Service; +import org.apache.tuscany.spi.component.SystemAtomicComponent; + +import junit.framework.TestCase; +import org.apache.tuscany.core.implementation.system.component.SystemReference; +import org.apache.tuscany.core.implementation.system.component.SystemService; +import org.easymock.EasyMock; + +/** + * Performs basic autowiring tests to composite artifacts + * + * @version $$Rev$$ $$Date$$ + */ +public class AutowireTestCase extends TestCase { + + /** + * Tests autowiring to an system atomic component + */ + public void testSystemAtomicAutowire() throws Exception { + CompositeComponent parent = new CompositeComponentImpl("parent", null, null, null); + parent.start(); + + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + interfaces.add(Source2.class); + Source originalSource = new SourceImpl(); + SystemAtomicComponent component = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component.getServiceInstance()).andReturn(originalSource).atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(interfaces); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + + EasyMock.replay(component); + parent.register(component); + + Source source = parent.resolveSystemInstance(Source.class); + assertNotNull(source); + Source2 source2 = parent.resolveSystemInstance(Source2.class); + assertSame(source, source2); + assertNull(parent.resolveSystemExternalInstance(Source.class)); + EasyMock.verify(component); + } + + /** + * Tests autowiring to an system atomic component + */ + public void testAtomicAutowire() throws Exception { + CompositeComponent parent = new CompositeComponentImpl("parent", null, null, null); + parent.start(); + + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + interfaces.add(Source2.class); + Source originalSource = new SourceImpl(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component.getServiceInstance()).andReturn(originalSource).atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(interfaces); + EasyMock.expect(component.isSystem()).andReturn(false).atLeastOnce(); + + EasyMock.replay(component); + parent.register(component); + + Source source = parent.resolveInstance(Source.class); + assertNotNull(source); + Source2 source2 = parent.resolveInstance(Source2.class); + assertSame(source, source2); + assertNull(parent.resolveExternalInstance(Source.class)); + EasyMock.verify(component); + } + + /** + * Tests autowiring to a system service which is wired to an atomic component. + */ + public void testSystemServiceAutowire() throws Exception { + CompositeComponent parent = new CompositeComponentImpl("parent", null, null, null); + parent.start(); + + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + interfaces.add(Source2.class); + + Source serviceSource = new SourceImpl(); + SystemService component = EasyMock.createMock(SystemService.class); + EasyMock.expect(component.getName()).andReturn("service").atLeastOnce(); + component.getInterface(); + EasyMock.expectLastCall().andReturn(Source.class).atLeastOnce(); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.expect(component.getServiceInstance()).andReturn(serviceSource); + EasyMock.replay(component); + parent.register(component); + + + SystemAtomicComponent component2 = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(component2.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component2.getServiceInterfaces()).andReturn(interfaces).atLeastOnce(); + EasyMock.expect(component2.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.replay(component2); + parent.register(component2); + + Source source = parent.resolveSystemExternalInstance(Source.class); + assertSame(serviceSource, source); + Source2 source2 = parent.resolveSystemExternalInstance(Source2.class); + assertNull(source2); + EasyMock.verify(component); + EasyMock.verify(component2); + } + + /** + * Tests autowiring to a system service which is wired to an atomic component. + */ + public void testServiceAutowire() throws Exception { + CompositeComponent parent = new CompositeComponentImpl("parent", null, null, null); + parent.start(); + + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + interfaces.add(Source2.class); + + Source serviceSource = new SourceImpl(); + Service component = EasyMock.createMock(Service.class); + EasyMock.expect(component.getName()).andReturn("service").atLeastOnce(); + component.getInterface(); + EasyMock.expectLastCall().andReturn(Source.class).atLeastOnce(); + EasyMock.expect(component.isSystem()).andReturn(false).atLeastOnce(); + EasyMock.expect(component.getServiceInstance()).andReturn(serviceSource); + EasyMock.replay(component); + parent.register(component); + + + AtomicComponent component2 = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component2.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component2.getServiceInterfaces()).andReturn(interfaces).atLeastOnce(); + EasyMock.expect(component2.isSystem()).andReturn(false).atLeastOnce(); + EasyMock.replay(component2); + parent.register(component2); + + Source source = parent.resolveExternalInstance(Source.class); + assertSame(serviceSource, source); + Source2 source2 = parent.resolveExternalInstance(Source2.class); + assertNull(source2); + EasyMock.verify(component); + EasyMock.verify(component2); + } + + + /** + * Tests autowiring to a system reference + */ + public void testSystemReferenceAutowire() throws Exception { + CompositeComponent parent = new CompositeComponentImpl("parent", null, null, null); + parent.start(); + + Source refSource = new SourceImpl(); + SystemReference reference = EasyMock.createMock(SystemReference.class); + EasyMock.expect(reference.getName()).andReturn("service").atLeastOnce(); + EasyMock.expect(reference.getServiceInstance()).andReturn(refSource); + EasyMock.expect(reference.isSystem()).andReturn(true).atLeastOnce(); + reference.getInterface(); + EasyMock.expectLastCall().andReturn(Source.class); + EasyMock.replay(reference); + parent.register(reference); + + Source source = parent.resolveSystemInstance(Source.class); + assertNotNull(source); + assertNull(parent.resolveSystemExternalInstance(Source.class)); + EasyMock.verify(reference); + } + + /** + * Tests autowiring to a reference + */ + public void testReferenceAutowire() throws Exception { + CompositeComponent parent = new CompositeComponentImpl("parent", null, null, null); + parent.start(); + Source refSource = new SourceImpl(); + Reference reference = EasyMock.createMock(Reference.class); + EasyMock.expect(reference.getName()).andReturn("service").atLeastOnce(); + EasyMock.expect(reference.getServiceInstance()).andReturn(refSource); + EasyMock.expect(reference.isSystem()).andReturn(false).atLeastOnce(); + reference.getInterface(); + EasyMock.expectLastCall().andReturn(Source.class); + EasyMock.replay(reference); + parent.register(reference); + + Source source = parent.resolveInstance(Source.class); + assertNotNull(source); + assertNull(parent.resolveExternalInstance(Source.class)); + EasyMock.verify(reference); + } + + + public static class SourceImpl implements Source, Source2 { + public SourceImpl() { + } + } + + public static interface Source { + + } + + public static interface Source2 { + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeBuilderTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeBuilderTestCase.java new file mode 100644 index 0000000000..fb665a55f4 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeBuilderTestCase.java @@ -0,0 +1,235 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.BindlessServiceDefinition; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.CompositeComponentType; +import org.apache.tuscany.spi.model.CompositeImplementation; +import org.apache.tuscany.spi.model.ReferenceDefinition; +import org.apache.tuscany.spi.model.ReferenceTarget; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.model.ServiceDefinition; +import org.apache.tuscany.spi.wire.WireService; + +import junit.framework.TestCase; +import org.apache.tuscany.core.builder.BuilderRegistryImpl; +import org.apache.tuscany.core.builder.ConnectorImpl; +import org.apache.tuscany.core.deployer.RootDeploymentContext; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; +import org.apache.tuscany.core.implementation.java.JavaComponentBuilder; +import org.apache.tuscany.core.implementation.java.JavaImplementation; +import org.apache.tuscany.core.implementation.java.mock.components.OtherTarget; +import org.apache.tuscany.core.implementation.java.mock.components.Source; +import org.apache.tuscany.core.implementation.java.mock.components.SourceImpl; +import org.apache.tuscany.core.implementation.java.mock.components.Target; +import org.apache.tuscany.core.implementation.java.mock.components.TargetImpl; +import org.apache.tuscany.core.wire.jdk.JDKWireService; +import org.easymock.EasyMock; +import org.easymock.IAnswer; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class CompositeBuilderTestCase extends TestCase { + private DeploymentContext deploymentContext; + + protected void setUp() throws Exception { + super.setUp(); + ScopeContainer mock = createMock(); + deploymentContext = new RootDeploymentContext(null, null, mock, null); + } + + @SuppressWarnings("unchecked") + public void testBuildConnect() throws Exception { + CompositeComponent parent = new CompositeComponentImpl(null, null, null, null); + + CompositeBuilder builder = new CompositeBuilder(); + WireService wireService = new JDKWireService(); + builder.setWireService(wireService); + BuilderRegistryImpl builderRegistry = new BuilderRegistryImpl(); + builderRegistry.setWireService(wireService); + JavaComponentBuilder jBuilder = new JavaComponentBuilder(); + jBuilder.setWireService(wireService); + builderRegistry.register(JavaImplementation.class, jBuilder); + builderRegistry.register(CompositeImplementation.class, builder); + CompositeBindlessBuilder bindlessBuilder = new CompositeBindlessBuilder(); + bindlessBuilder.setWireService(wireService); + builderRegistry.register(bindlessBuilder); + builder.setBuilderRegistry(builderRegistry); + CompositeComponent component = + (CompositeComponent) builder.build(parent, createTopComponentDef(), deploymentContext); + + ConnectorImpl connector = new ConnectorImpl(); + connector.connect(component); + + deploymentContext.getModuleScope().start(); + component.start(); + CompositeComponent sourceComponent = (CompositeComponent) component.getChild("SourceComponent"); + Source source = (Source) sourceComponent.getServiceInstance("InnerSourceService"); + assertNotNull(source); + AtomicComponent innerSourceComponent = (AtomicComponent) sourceComponent.getChild("InnerSourceComponent"); + Source innerSourceInstance = (Source) deploymentContext.getModuleScope().getInstance(innerSourceComponent); + assertNotNull(innerSourceInstance); + component.stop(); + } + + private ComponentDefinition createTopComponentDef() throws Exception { + + CompositeComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> outerType = + new CompositeComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + outerType.add(createSourceComponentDef()); + outerType.add(createTargetComponentDef()); + + CompositeImplementation outerImpl = new CompositeImplementation(); + outerImpl.setComponentType(outerType); + + return new ComponentDefinition<CompositeImplementation>(outerImpl); + } + + private ComponentDefinition<CompositeImplementation> createSourceComponentDef() throws Exception { + + CompositeComponentType<ServiceDefinition, ReferenceDefinition, JavaMappedProperty<?>> innerType = + new CompositeComponentType<ServiceDefinition, ReferenceDefinition, JavaMappedProperty<?>>(); + innerType.add(createInnerSourceComponentDef()); + ReferenceDefinition reference = new ReferenceDefinition(); + reference.setName("TargetComponentRef"); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + JavaServiceContract targetContract = registry.introspect(Target.class); + reference.setServiceContract(targetContract); + innerType.add(reference); + BindlessServiceDefinition service = new BindlessServiceDefinition(); + service.setName("InnerSourceService"); + JavaServiceContract sourceContract = registry.introspect(Source.class); + service.setServiceContract(sourceContract); + service.setTarget(new URI("InnerSourceComponent")); + innerType.add(service); + + CompositeImplementation innerImpl = new CompositeImplementation(); + innerImpl.setComponentType(innerType); + + ComponentDefinition<CompositeImplementation> sourceComponentDefinition = + new ComponentDefinition<CompositeImplementation>("SourceComponent", innerImpl); + ReferenceTarget refTarget = new ReferenceTarget(); + refTarget.setReferenceName("TargetComponentRef"); + refTarget.addTarget(new URI("TargetComponent")); + sourceComponentDefinition.add(refTarget); + + return sourceComponentDefinition; + } + + private ComponentDefinition<JavaImplementation> createInnerSourceComponentDef() throws Exception { + + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> sourceType = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + sourceType.setImplementationScope(Scope.MODULE); + JavaMappedReference reference = new JavaMappedReference(); + reference.setName("targetReference"); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract<?> targetContract = registry.introspect(Target.class); + targetContract.setCallbackClass(OtherTarget.class); + targetContract.setCallbackName("OtherTarget"); + reference.setServiceContract(targetContract); + reference.setMember(SourceImpl.class.getMethod("setTarget", Target.class)); + sourceType.add(reference); + + ServiceContract<?> sourceContract = registry.introspect(Source.class); + + JavaMappedService sourceServiceDefinition = new JavaMappedService(); + sourceServiceDefinition.setName("Source"); + sourceServiceDefinition.setServiceContract(sourceContract); + + sourceType.add(sourceServiceDefinition); + sourceType.setConstructorDefinition(new ConstructorDefinition<SourceImpl>(SourceImpl.class.getConstructor())); + JavaImplementation sourceImpl = new JavaImplementation(); + sourceImpl.setComponentType(sourceType); + sourceImpl.setImplementationClass(SourceImpl.class); + ComponentDefinition<JavaImplementation> innerSourceComponentDefinition = + new ComponentDefinition<JavaImplementation>("InnerSourceComponent", sourceImpl); + ReferenceTarget refTarget = new ReferenceTarget(); + refTarget.setReferenceName("targetReference"); + refTarget.addTarget(new URI("TargetComponentRef")); + innerSourceComponentDefinition.add(refTarget); + + return innerSourceComponentDefinition; + } + + private ComponentDefinition<JavaImplementation> createTargetComponentDef() throws Exception { + + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> targetType = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + targetType.setImplementationScope(Scope.MODULE); + + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract<?> targetContract = registry.introspect(Target.class); + targetContract.setCallbackClass(OtherTarget.class); + targetContract.setCallbackName("OtherTarget"); + + JavaMappedService serviceDefinition = new JavaMappedService(); + serviceDefinition.setName("Target"); + serviceDefinition.setServiceContract(targetContract); + serviceDefinition.setCallbackReferenceName("otherTarget"); + + targetType.add(serviceDefinition); + targetType.setConstructorDefinition(new ConstructorDefinition<TargetImpl>(TargetImpl.class.getConstructor())); + JavaImplementation targetImpl = new JavaImplementation(); + targetImpl.setComponentType(targetType); + targetImpl.setImplementationClass(TargetImpl.class); + return new ComponentDefinition<JavaImplementation>("TargetComponent", targetImpl); + } + + private ScopeContainer createMock() { + ScopeContainer container = EasyMock.createMock(ScopeContainer.class); + container.start(); + container.register(EasyMock.isA(AtomicComponent.class)); + EasyMock.expectLastCall().anyTimes(); + EasyMock.expect(container.getScope()).andReturn(Scope.MODULE).anyTimes(); + EasyMock.expect(container.getInstance(EasyMock.isA(AtomicComponent.class))).andAnswer(new IAnswer<Object>() { + private Map<AtomicComponent, Object> cache = new HashMap<AtomicComponent, Object>(); + + public Object answer() throws Throwable { + AtomicComponent component = (AtomicComponent) EasyMock.getCurrentArguments()[0]; + Object instance = cache.get(component); + if (instance == null) { + instance = component.createInstance(); + cache.put(component, instance); + } + return instance; + } + }); + EasyMock.replay(container); + return container; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImplBasicTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImplBasicTestCase.java new file mode 100644 index 0000000000..8694d9ddc6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImplBasicTestCase.java @@ -0,0 +1,223 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.util.List; + +import org.apache.tuscany.spi.component.ComponentNotFoundException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.IllegalTargetException; +import org.apache.tuscany.spi.component.Reference; +import org.apache.tuscany.spi.component.Service; +import org.apache.tuscany.spi.component.TargetNotFoundException; +import org.apache.tuscany.spi.event.Event; +import org.apache.tuscany.spi.event.RuntimeEventListener; +import org.apache.tuscany.spi.extension.ServiceExtension; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.Assert; +import junit.framework.TestCase; +import org.apache.tuscany.core.component.event.CompositeStart; +import org.easymock.EasyMock; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeComponentImplBasicTestCase extends TestCase { + + public void testGetScope() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + Assert.assertEquals(Scope.COMPOSITE, composite.getScope()); + } + + public void testGetChildren() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + composite.register(new ServiceExtension("foo", null, null, null)); + Assert.assertEquals(1, composite.getChildren().size()); + } + + public void testGetServices() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + composite.register(new ServiceExtension("foo", null, null, null)); + composite.register(getReference("bar")); + Assert.assertEquals(1, composite.getServices().size()); + } + + public void testGetService() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + composite.register(new ServiceExtension("foo", null, null, null)); + composite.start(); + assertNotNull(composite.getService("foo")); + } + + public void testServiceNotFound() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + composite.register(new ServiceExtension("foo", null, null, null)); + composite.start(); + try { + composite.getService("bar"); + fail(); + } catch (ComponentNotFoundException e) { + // expected + } + } + + public void testNotService() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + composite.register(getReference("foo")); + composite.start(); + try { + composite.getService("foo"); + fail(); + } catch (ComponentNotFoundException e) { + // expected + } + } + + public void testTargetNotFound() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + composite.register(getReference("foo")); + composite.start(); + try { + composite.locateService(Foo.class, "foo1"); + fail(); + } catch (TargetNotFoundException e) { + // expected + } + } + + public void testReferencesServices() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + composite.register(new ServiceExtension("foo", null, null, null)); + composite.register(getReference("bar")); + Assert.assertEquals(1, composite.getReferences().size()); + } + + public void testServiceInterfaces() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + Service service1 = getService("foo", Foo.class); + composite.register(service1); + Service service2 = getService("bar", Bar.class); + composite.register(service2); + + List<Class<?>> interfaces = composite.getServiceInterfaces(); + assertEquals(2, interfaces.size()); + for (Class o : interfaces) { + if (!(Foo.class.isAssignableFrom(o)) && !(Bar.class.isAssignableFrom(o))) { + fail(); + } + } + } + + public void testGetServiceInstanceByName() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + Service service = createMock(Service.class); + EasyMock.expect(service.isSystem()).andReturn(false).atLeastOnce(); + service.getName(); + expectLastCall().andReturn("foo").anyTimes(); + service.getInterface(); + expectLastCall().andReturn(Foo.class); + service.getServiceInstance(); + expectLastCall().andReturn(new Foo() { + }); + replay(service); + composite.register(service); + assertNotNull(composite.getServiceInstance("foo")); + } + + public void testGetServiceInstanceNotFound() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + Service service = getService("foo", Foo.class); + composite.register(service); + try { + composite.getServiceInstance("bar"); + fail(); + } catch (TargetNotFoundException e) { + //expected + } + } + + public void testGetServiceInstanceNotService() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + Reference reference = getReference("foo"); + composite.register(reference); + try { + composite.getServiceInstance("foo"); + fail(); + } catch (IllegalTargetException e) { + //expected + } + } + + public void testOnEvent() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + Event event = new Event() { + public Object getSource() { + return null; + } + }; + RuntimeEventListener listener = createMock(RuntimeEventListener.class); + listener.onEvent(isA(CompositeStart.class)); + listener.onEvent(eq(event)); + expectLastCall(); + replay(listener); + composite.addListener(listener); + composite.start(); + composite.onEvent(event); + } + + public void testPrepare() { + CompositeComponent composite = new CompositeComponentImpl("parent", null, null, null); + composite.prepare(); + } + + private Reference getReference(String name) { + Reference reference = EasyMock.createNiceMock(Reference.class); + EasyMock.expect(reference.isSystem()).andReturn(false).atLeastOnce(); + reference.getName(); + expectLastCall().andReturn(name).anyTimes(); + reference.getInterface(); + expectLastCall().andReturn(Object.class).atLeastOnce(); + replay(reference); + return reference; + } + + private Service getService(String name, Class<?> interfaze) { + Service service = createMock(Service.class); + EasyMock.expect(service.isSystem()).andReturn(false).atLeastOnce(); + service.getName(); + expectLastCall().andReturn(name).anyTimes(); + service.getInterface(); + expectLastCall().andReturn(interfaze).atLeastOnce(); + replay(service); + return service; + } + + private interface Foo { + } + + private interface Bar { + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImplSystemWireTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImplSystemWireTestCase.java new file mode 100644 index 0000000000..c3bf846400 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImplSystemWireTestCase.java @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.spi.QualifiedName; +import org.apache.tuscany.spi.builder.BuilderConfigException; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.SystemAtomicComponent; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundWire; + +import junit.framework.TestCase; +import org.apache.tuscany.core.builder.ConnectorImpl; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeComponentImplSystemWireTestCase extends TestCase { + + /** + * Verifies system services in a CompositeComponentImpl are wired during the parent composite's prepare callback + */ + public void testSystemServiceWire() { + InboundWire inbound = EasyMock.createMock(InboundWire.class); + EasyMock.expect(inbound.getServiceContract()).andReturn(new JavaServiceContract(Foo.class)); + inbound.getInvocationChains(); + EasyMock.expectLastCall().andReturn(Collections.emptyMap()); + + List<Class<?>> services = new ArrayList<Class<?>>(); + services.add(Foo.class); + QualifiedName qName = new QualifiedName("target/bar"); + OutboundWire outbound = EasyMock.createMock(OutboundWire.class); + EasyMock.expect(outbound.getTargetName()).andReturn(qName).atLeastOnce(); + outbound.getInvocationChains(); + EasyMock.expectLastCall().andReturn(Collections.emptyMap()); + outbound.setTargetWire(EasyMock.eq(inbound)); + EasyMock.expect(outbound.getServiceContract()).andReturn(new JavaServiceContract(Foo.class)).atLeastOnce(); + List<OutboundWire> wires = new ArrayList<OutboundWire>(); + wires.add(outbound); + Map<String, List<OutboundWire>> wireMap = new HashMap<String, List<OutboundWire>>(); + wireMap.put("ref", wires); + CompositeComponent parent = new CompositeComponentImpl("foo", "foo", null, new ConnectorImpl(), null); + SystemAtomicComponent source = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(source.getScope()).andReturn(Scope.MODULE).atLeastOnce(); + EasyMock.expect(source.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(source.getServiceInterfaces()).andReturn(services); + EasyMock.expect(source.getOutboundWires()).andReturn(wireMap); + source.getInboundWires(); + EasyMock.expectLastCall().andReturn(Collections.emptyMap()); + EasyMock.expect(source.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.expect(source.getParent()).andReturn(parent).atLeastOnce(); + + source.prepare(); + EasyMock.replay(source); + + EasyMock.expect(outbound.getContainer()).andReturn(source); + EasyMock.replay(outbound); + + parent.register(source); + + SystemAtomicComponent target = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(target.getName()).andReturn("target").atLeastOnce(); + EasyMock.expect(target.getServiceInterfaces()).andReturn(services); + EasyMock.expect(target.getInboundWire("bar")).andReturn(inbound).atLeastOnce(); + EasyMock.expect(target.getScope()).andReturn(Scope.MODULE).atLeastOnce(); + EasyMock.expect(target.getParent()).andReturn(parent).atLeastOnce(); + target.getInboundWires(); + EasyMock.expectLastCall().andReturn(Collections.emptyMap()); + + target.prepare(); + target.getOutboundWires(); + EasyMock.expectLastCall().andReturn(Collections.emptyMap()); + EasyMock.expect(target.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.replay(target); + + EasyMock.expect(inbound.getContainer()).andReturn(target); + EasyMock.replay(inbound); + + parent.register(target); + parent.prepare(); + EasyMock.verify(source); + EasyMock.verify(target); + EasyMock.verify(inbound); + EasyMock.verify(outbound); + } + + + /** + * Verifies an application component cannot be wired to a system service in the same composite + */ + public void testSystemServiceIsolationWire() { + InboundWire inbound = EasyMock.createMock(InboundWire.class); + EasyMock.replay(inbound); + + List<Class<?>> services = new ArrayList<Class<?>>(); + services.add(Foo.class); + QualifiedName qName = new QualifiedName("target/bar"); + OutboundWire outbound = EasyMock.createMock(OutboundWire.class); + EasyMock.expect(outbound.getReferenceName()).andReturn("foo"); + EasyMock.expect(outbound.getTargetName()).andReturn(qName).atLeastOnce(); + EasyMock.replay(outbound); + + List<OutboundWire> wires = new ArrayList<OutboundWire>(); + wires.add(outbound); + Map<String, List<OutboundWire>> wireMap = new HashMap<String, List<OutboundWire>>(); + wireMap.put("ref", wires); + CompositeComponent parent = new CompositeComponentImpl("foo", "foo", null, new ConnectorImpl(), null); + SystemAtomicComponent source = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(source.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(source.getServiceInterfaces()).andReturn(services); + EasyMock.expect(source.getOutboundWires()).andReturn(wireMap); + EasyMock.expect(source.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.expect(source.getParent()).andReturn(parent).atLeastOnce(); + EasyMock.replay(source); + + parent.register(source); + + AtomicComponent target = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(target.getName()).andReturn("target").atLeastOnce(); + EasyMock.expect(target.getServiceInterfaces()).andReturn(services); + EasyMock.expect(target.isSystem()).andReturn(false).atLeastOnce(); + EasyMock.replay(target); + + parent.register(target); + try { + parent.prepare(); + fail(); + } catch (BuilderConfigException e) { + //expected + } + EasyMock.verify(source); + EasyMock.verify(target); + EasyMock.verify(inbound); + EasyMock.verify(outbound); + } + + + private class Foo { + + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImplTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImplTestCase.java new file mode 100644 index 0000000000..29df7f0f85 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImplTestCase.java @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.SystemAtomicComponent; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeComponentImplTestCase extends TestCase { + + public void testSetUri() throws Exception { + CompositeComponentImpl component = new CompositeComponentImpl("foo", "foo/bar", null, null, null); + assertEquals("foo/bar", component.getURI()); + } + + public void testRegisterSystemService() throws Exception { + List<Class<?>> services = new ArrayList<Class<?>>(); + services.add(Foo.class); + CompositeComponent parent = new CompositeComponentImpl("foo", "foo", null, null, null); + SystemAtomicComponent component = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("bar").atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(services); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.replay(component); + parent.register(component); + assertNull(parent.getChild("bar")); + assertNotNull(parent.getSystemChild("bar")); + EasyMock.verify(component); + } + + public void testRegister() throws Exception { + List<Class<?>> services = new ArrayList<Class<?>>(); + services.add(Foo.class); + CompositeComponent parent = new CompositeComponentImpl("foo", "foo", null, null, null); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("bar").atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(services); + EasyMock.expect(component.isSystem()).andReturn(false).atLeastOnce(); + EasyMock.replay(component); + parent.register(component); + assertNull(parent.getSystemChild("bar")); + assertNotNull(parent.getChild("bar")); + EasyMock.verify(component); + } + + /** + * Verifies a system service and application component can be registered with the same name in a composite + */ + public void testSystemServiceApplicationNamespaceIsolation() throws Exception { + List<Class<?>> services = new ArrayList<Class<?>>(); + services.add(Foo.class); + CompositeComponent parent = new CompositeComponentImpl("foo", "foo", null, null, null); + SystemAtomicComponent component = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("bar").atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(services); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.replay(component); + parent.register(component); + AtomicComponent component2 = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component2.getName()).andReturn("bar").atLeastOnce(); + EasyMock.expect(component2.getServiceInterfaces()).andReturn(services); + EasyMock.expect(component2.isSystem()).andReturn(false).atLeastOnce(); + EasyMock.replay(component2); + parent.register(component2); + EasyMock.verify(component); + EasyMock.verify(component2); + } + + public void testSystemServiceLifecycle() throws Exception { + List<Class<?>> services = new ArrayList<Class<?>>(); + services.add(Foo.class); + CompositeComponent parent = new CompositeComponentImpl("foo", "foo", null, null, null); + SystemAtomicComponent component = EasyMock.createMock(SystemAtomicComponent.class); + component.start(); + EasyMock.expect(component.getName()).andReturn("bar").atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(services); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + component.stop(); + EasyMock.replay(component); + parent.register(component); + parent.start(); + parent.stop(); + EasyMock.verify(component); + } + + public void testComponentLifecycle() throws Exception { + List<Class<?>> services = new ArrayList<Class<?>>(); + services.add(Foo.class); + CompositeComponent parent = new CompositeComponentImpl("foo", "foo", null, null, null); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + component.start(); + EasyMock.expect(component.getName()).andReturn("bar").atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(services); + EasyMock.expect(component.isSystem()).andReturn(false).atLeastOnce(); + component.stop(); + EasyMock.replay(component); + parent.register(component); + parent.start(); + parent.stop(); + EasyMock.verify(component); + } + + public void testSystemServiceAutowire() throws Exception { + List<Class<?>> services = new ArrayList<Class<?>>(); + services.add(Foo.class); + CompositeComponent parent = new CompositeComponentImpl("foo", "foo", null, null, null); + SystemAtomicComponent component = EasyMock.createMock(SystemAtomicComponent.class); + component.start(); + EasyMock.expect(component.getName()).andReturn("bar").atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(services); + EasyMock.expect(component.getServiceInstance()).andReturn(new Foo() { + }); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + component.stop(); + EasyMock.replay(component); + parent.register(component); + parent.start(); + assertNull(parent.resolveSystemExternalInstance(Foo.class)); + assertNotNull(parent.resolveSystemInstance(Foo.class)); + parent.stop(); + EasyMock.verify(component); + } + + + public void testAutowire() throws Exception { + List<Class<?>> services = new ArrayList<Class<?>>(); + services.add(Foo.class); + CompositeComponent parent = new CompositeComponentImpl("foo", "foo", null, null, null); + SystemAtomicComponent component = EasyMock.createMock(SystemAtomicComponent.class); + component.start(); + EasyMock.expect(component.getName()).andReturn("bar").atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(services); + EasyMock.expect(component.getServiceInstance()).andReturn(new Foo() { + }); + EasyMock.expect(component.isSystem()).andReturn(false).atLeastOnce(); + component.stop(); + EasyMock.replay(component); + parent.register(component); + parent.start(); + assertNull(parent.resolveExternalInstance(Foo.class)); + assertNotNull(parent.resolveInstance(Foo.class)); + parent.stop(); + EasyMock.verify(component); + } + + private class Foo { + + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentResolutionTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentResolutionTestCase.java new file mode 100644 index 0000000000..0ad1e6f649 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeComponentResolutionTestCase.java @@ -0,0 +1,167 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ComponentNotFoundException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.Service; +import org.apache.tuscany.spi.component.SystemAtomicComponent; + +import junit.framework.TestCase; +import org.apache.tuscany.core.mock.component.Source; +import org.apache.tuscany.core.mock.component.SourceImpl; +import org.easymock.EasyMock; + +/** + * Verifies an atomic component can be resolved from its parent + * + * @version $$Rev$$ $$Date$$ + */ +public class CompositeComponentResolutionTestCase extends TestCase { + + public void testSystemComponentResolution() throws NoSuchMethodException { + CompositeComponent parent = new CompositeComponentImpl("foo", null, null, null); + parent.start(); + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + Source originalSource = new SourceImpl(); + SystemAtomicComponent component = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component.getServiceInstance()).andReturn(originalSource); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(interfaces); + EasyMock.replay(component); + parent.register(component); + assertNull(parent.getChild("source")); + AtomicComponent target = (AtomicComponent) parent.getSystemChild("source"); + Source source = (Source) target.getServiceInstance(); + assertNotNull(source); + EasyMock.verify(component); + } + + public void testLocateSystemService() throws NoSuchMethodException { + CompositeComponent parent = new CompositeComponentImpl("foo", null, null, null); + parent.start(); + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + Source originalSource = new SourceImpl(); + SystemAtomicComponent component = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component.getServiceInstance()).andReturn(originalSource); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(interfaces); + EasyMock.replay(component); + parent.register(component); + Source source = parent.locateSystemService(Source.class, "source"); + assertNotNull(source); + EasyMock.verify(component); + } + + public void testLocateService() throws NoSuchMethodException { + CompositeComponent parent = new CompositeComponentImpl("foo", null, null, null); + parent.start(); + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + Source originalSource = new SourceImpl(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component.getServiceInstance()).andReturn(originalSource); + EasyMock.expect(component.isSystem()).andReturn(false).atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(interfaces); + EasyMock.replay(component); + parent.register(component); + Source source = parent.locateService(Source.class, "source"); + assertNotNull(source); + EasyMock.verify(component); + } + + public void testComponentResolution() throws NoSuchMethodException { + CompositeComponent parent = new CompositeComponentImpl("foo", null, null, null); + parent.start(); + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + Source originalSource = new SourceImpl(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component.getServiceInstance()).andReturn(originalSource); + EasyMock.expect(component.isSystem()).andReturn(false).atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(interfaces); + EasyMock.replay(component); + parent.register(component); + assertNull(parent.getSystemChild("source")); + AtomicComponent target = (AtomicComponent) parent.getChild("source"); + Source source = (Source) target.getServiceInstance(); + assertNotNull(source); + EasyMock.verify(component); + } + + + public void testGetService() throws NoSuchMethodException { + CompositeComponent parent = new CompositeComponentImpl("foo", null, null, null); + parent.start(); + Service service = EasyMock.createMock(Service.class); + EasyMock.expect(service.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(service.isSystem()).andReturn(false).atLeastOnce(); + service.getInterface(); + EasyMock.expectLastCall().andReturn(Source.class); + EasyMock.replay(service); + parent.register(service); + assertNotNull(parent.getService("source")); + try { + parent.getSystemService("source"); + fail(); + } catch (ComponentNotFoundException e) { + // expected + } + EasyMock.verify(service); + } + + public void testSystemGetService() throws NoSuchMethodException { + CompositeComponent parent = new CompositeComponentImpl("foo", null, null, null); + parent.start(); + Service service = EasyMock.createMock(Service.class); + EasyMock.expect(service.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(service.isSystem()).andReturn(true).atLeastOnce(); + service.getInterface(); + EasyMock.expectLastCall().andReturn(Source.class); + EasyMock.replay(service); + parent.register(service); + assertNotNull(parent.getSystemService("source")); + try { + parent.getService("source"); + fail(); + } catch (ComponentNotFoundException e) { + // expected + } + EasyMock.verify(service); + } + + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeLifecycleTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeLifecycleTestCase.java new file mode 100644 index 0000000000..5b0af72caa --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeLifecycleTestCase.java @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.SystemAtomicComponent; + +import junit.framework.TestCase; +import org.apache.tuscany.core.mock.component.Source; +import org.apache.tuscany.core.mock.component.SourceImpl; +import org.easymock.EasyMock; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class CompositeLifecycleTestCase extends TestCase { + + public void testLifecycle() throws Exception { + CompositeComponent composite = new CompositeComponentImpl("foo", null, null, null); + composite.start(); + assertNull(composite.getChild("nothtere")); + composite.stop(); + composite.start(); + assertNull(composite.getChild("nothtere")); + composite.stop(); + } + + public void testSystemRestart() throws NoSuchMethodException { + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + Source originalSource = new SourceImpl(); + SystemAtomicComponent component = EasyMock.createMock(SystemAtomicComponent.class); + component.start(); + component.stop(); + EasyMock.expectLastCall().times(2); + EasyMock.expect(component.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.expect(component.getServiceInstance()).andReturn(originalSource).atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(interfaces); + EasyMock.replay(component); + + CompositeComponent composite = new CompositeComponentImpl("foo", null, null, null); + composite.start(); + composite.register(component); + + AtomicComponent atomicComponent = (AtomicComponent) composite.getSystemChild("source"); + Source source = (Source) atomicComponent.getServiceInstance(); + assertNotNull(source); + composite.stop(); + composite.start(); + atomicComponent = (AtomicComponent) composite.getSystemChild("source"); + Source source2 = (Source) atomicComponent.getServiceInstance(); + assertNotNull(source2); + composite.stop(); + EasyMock.verify(component); + } + + public void testRestart() throws NoSuchMethodException { + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + Source originalSource = new SourceImpl(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + component.start(); + component.stop(); + EasyMock.expectLastCall().times(2); + EasyMock.expect(component.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component.isSystem()).andReturn(false).atLeastOnce(); + EasyMock.expect(component.getServiceInstance()).andReturn(originalSource).atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(interfaces); + EasyMock.replay(component); + + CompositeComponent composite = new CompositeComponentImpl("foo", null, null, null); + composite.start(); + composite.register(component); + + AtomicComponent atomicComponent = (AtomicComponent) composite.getChild("source"); + Source source = (Source) atomicComponent.getServiceInstance(); + assertNotNull(source); + composite.stop(); + composite.start(); + atomicComponent = (AtomicComponent) composite.getChild("source"); + Source source2 = (Source) atomicComponent.getServiceInstance(); + assertNotNull(source2); + composite.stop(); + EasyMock.verify(component); + } + + public void testChildStoppedBeforeParent() { + CompositeComponent parent = new CompositeComponentImpl("parent", null, null, null); + CompositeComponent child = new CompositeComponentImpl("child", null, null, null); + parent.register(child); + parent.start(); + child.stop(); + parent.stop(); + } + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositePropagationTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositePropagationTestCase.java new file mode 100644 index 0000000000..69b8c3f34f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositePropagationTestCase.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.SystemAtomicComponent; + +import junit.framework.TestCase; +import org.apache.tuscany.core.mock.component.Source; +import org.easymock.EasyMock; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +/** + * + * @version $Rev$ $Date$ + */ +public class CompositePropagationTestCase extends TestCase { + + private CompositeComponent parent; + private CompositeComponent child2; + + public void testSystemLifecyclePropagation() throws NoSuchMethodException { + parent.start(); + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + SystemAtomicComponent component = createMock(SystemAtomicComponent.class); + expect(component.getName()).andReturn("source").anyTimes(); + component.stop(); + expect(component.getServiceInterfaces()).andReturn(interfaces); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + replay(component); + child2.register(component); + parent.stop(); + verify(component); + } + + public void testLifecyclePropagation() throws NoSuchMethodException { + parent.start(); + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + SystemAtomicComponent component = createMock(SystemAtomicComponent.class); + expect(component.getName()).andReturn("source").anyTimes(); + component.stop(); + expect(component.getServiceInterfaces()).andReturn(interfaces); + EasyMock.expect(component.isSystem()).andReturn(false).atLeastOnce(); + replay(component); + child2.register(component); + parent.stop(); + verify(component); + } + + + protected void setUp() throws Exception { + super.setUp(); + parent = new CompositeComponentImpl("parent", null, null, null); + CompositeComponent child1 = new CompositeComponentImpl("child1", parent, null, null); + child2 = new CompositeComponentImpl("child2", child1, null, null); + child1.register(child2); + parent.register(child1); + } + + protected void tearDown() throws Exception { + parent.stop(); + super.tearDown(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvokerInvocationExceptionTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvokerInvocationExceptionTestCase.java new file mode 100644 index 0000000000..b1e850d703 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvokerInvocationExceptionTestCase.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.Interceptor; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.MessageImpl; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeReferenceCallbackTargetInvokerInvocationExceptionTestCase extends TestCase { + private InboundWire wire; + private WorkContext context; + private Message message; + private OutboundInvocationChain chain; + private CompositeReferenceCallbackTargetInvoker invoker; + + /** + * Verfies an InvocationTargetException thrown when invoking the target is propagated to the client correctly and + * the originating error is unwrapped + */ + public void testThrowableTargetInvocation() throws Exception { + Message response = invoker.invoke(message); + assertTrue(response.isFault()); + Object body = response.getBody(); + assertTrue(SomeException.class.equals(body.getClass())); + EasyMock.verify(wire); + EasyMock.verify(context); + EasyMock.verify(chain); + } + + protected void setUp() throws Exception { + super.setUp(); + Object id = new Object(); + Object corrId = new Object(); + Object targetAddress = new Object(); + message = new MessageImpl(); + message.setMessageId(id); + message.setCorrelationId(corrId); + message.setBody("foo"); + Message response = new MessageImpl(); + response.setBody("response"); + Operation<Type> operation = new Operation<Type>("echo", null, null, null); + Interceptor head = new ErrorInterceptor(); + chain = EasyMock.createMock(OutboundInvocationChain.class); + EasyMock.expect(chain.getTargetInvoker()).andReturn(null); + EasyMock.expect(chain.getHeadInterceptor()).andReturn(head); + EasyMock.replay(chain); + Map<Operation<?>, OutboundInvocationChain> chains = new HashMap<Operation<?>, OutboundInvocationChain>(); + chains.put(operation, chain); + wire = EasyMock.createMock(InboundWire.class); + EasyMock.expect(wire.retrieveMapping(corrId)).andReturn(targetAddress); + EasyMock.expect(wire.getSourceCallbackInvocationChains(targetAddress)).andReturn(chains); + EasyMock.expect(wire.getContainer()).andReturn(null); + EasyMock.replay(wire); + context = EasyMock.createMock(WorkContext.class); + context.setCurrentMessageId(EasyMock.eq(id)); + context.setCurrentMessageId(EasyMock.isNull()); + context.setCurrentCorrelationId(corrId); + context.setCurrentCorrelationId(EasyMock.isNull()); + EasyMock.expect(context.getCurrentMessageId()).andReturn(id); + EasyMock.expect(context.getCurrentCorrelationId()).andReturn(corrId); + EasyMock.replay(context); + invoker = new CompositeReferenceCallbackTargetInvoker(operation, wire, context); + } + + private class SomeException extends Exception { + + } + + private class ErrorInterceptor implements Interceptor { + + public Message invoke(Message msg) { + msg.setBodyWithFault(new SomeException()); + return msg; + } + + public void setNext(Interceptor next) { + + } + + public Interceptor getNext() { + return null; + } + + public boolean isOptimizable() { + return false; + } + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvokerTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvokerTestCase.java new file mode 100644 index 0000000000..8e047e3ad1 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvokerTestCase.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.Interceptor; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.MessageImpl; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeReferenceCallbackTargetInvokerTestCase extends TestCase { + private InboundWire wire; + private WorkContext context; + private Message message; + private OutboundInvocationChain chain; + private Interceptor head; + private CompositeReferenceCallbackTargetInvoker invoker; + + /** + * Verfies the normal execution path through a callback + */ + public void testNormalPathMessageInvocation() throws Exception { + Message response = invoker.invoke(message); + assertEquals("response", response.getBody()); + EasyMock.verify(wire); + EasyMock.verify(context); + EasyMock.verify(chain); + EasyMock.verify(head); + } + + protected void setUp() throws Exception { + super.setUp(); + Object id = new Object(); + Object corrId = new Object(); + Object targetAddress = new Object(); + message = new MessageImpl(); + message.setMessageId(id); + message.setCorrelationId(corrId); + message.setBody("foo"); + Message response = new MessageImpl(); + response.setBody("response"); + Operation<Type> operation = new Operation<Type>("echo", null, null, null); + head = EasyMock.createMock(Interceptor.class); + EasyMock.expect(head.invoke(EasyMock.isA(Message.class))).andReturn(response); + EasyMock.replay(head); + chain = EasyMock.createMock(OutboundInvocationChain.class); + EasyMock.expect(chain.getTargetInvoker()).andReturn(null); + EasyMock.expect(chain.getHeadInterceptor()).andReturn(head); + EasyMock.replay(chain); + Map<Operation<?>, OutboundInvocationChain> chains = new HashMap<Operation<?>, OutboundInvocationChain>(); + chains.put(operation, chain); + wire = EasyMock.createMock(InboundWire.class); + EasyMock.expect(wire.retrieveMapping(corrId)).andReturn(targetAddress); + EasyMock.expect(wire.getSourceCallbackInvocationChains(targetAddress)).andReturn(chains); + EasyMock.expect(wire.getContainer()).andReturn(null); + EasyMock.replay(wire); + context = EasyMock.createMock(WorkContext.class); + context.setCurrentMessageId(EasyMock.eq(id)); + context.setCurrentMessageId(EasyMock.isNull()); + context.setCurrentCorrelationId(corrId); + context.setCurrentCorrelationId(EasyMock.isNull()); + EasyMock.expect(context.getCurrentMessageId()).andReturn(id); + EasyMock.expect(context.getCurrentCorrelationId()).andReturn(corrId); + EasyMock.replay(context); + invoker = new CompositeReferenceCallbackTargetInvoker(operation, wire, context); + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvokerThrowableTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvokerThrowableTestCase.java new file mode 100644 index 0000000000..462b6a5680 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvokerThrowableTestCase.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.lang.reflect.Type; +import java.lang.reflect.UndeclaredThrowableException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.Interceptor; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.MessageImpl; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; + +import junit.framework.TestCase; +import org.easymock.EasyMock; +import org.easymock.IAnswer; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeReferenceCallbackTargetInvokerThrowableTestCase extends TestCase { + private InboundWire wire; + private WorkContext context; + private Message message; + private OutboundInvocationChain chain; + private Interceptor head; + private CompositeReferenceCallbackTargetInvoker invoker; + + /** + * Verfies an exception thrown in the target is propagated to the client correctly + */ + public void testThrowableTargetInvocation() throws Exception { + Message response = invoker.invoke(message); + assertTrue(response.isFault()); + Object body = response.getBody(); + if (!(body instanceof UndeclaredThrowableException)) { + fail(); // EasyMock wraps the Throwable in an UndeclaredThrowableException + } + UndeclaredThrowableException e = (UndeclaredThrowableException) body; + assertTrue(InsidiousException.class.equals(e.getUndeclaredThrowable().getClass())); + EasyMock.verify(wire); + EasyMock.verify(context); + EasyMock.verify(chain); + EasyMock.verify(head); + } + + protected void setUp() throws Exception { + super.setUp(); + Object id = new Object(); + Object corrId = new Object(); + Object targetAddress = new Object(); + message = new MessageImpl(); + message.setMessageId(id); + message.setCorrelationId(corrId); + message.setBody("foo"); + Message response = new MessageImpl(); + response.setBody("response"); + Operation<Type> operation = new Operation<Type>("echo", null, null, null); + head = EasyMock.createMock(Interceptor.class); + EasyMock.expect(head.invoke(EasyMock.isA(Message.class))).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + throw new InsidiousException(); // andThrow() does not seem to work here + } + }); + EasyMock.replay(head); + chain = EasyMock.createMock(OutboundInvocationChain.class); + EasyMock.expect(chain.getTargetInvoker()).andReturn(null); + EasyMock.expect(chain.getHeadInterceptor()).andReturn(head); + EasyMock.replay(chain); + Map<Operation<?>, OutboundInvocationChain> chains = new HashMap<Operation<?>, OutboundInvocationChain>(); + chains.put(operation, chain); + wire = EasyMock.createMock(InboundWire.class); + EasyMock.expect(wire.retrieveMapping(corrId)).andReturn(targetAddress); + EasyMock.expect(wire.getSourceCallbackInvocationChains(targetAddress)).andReturn(chains); + EasyMock.expect(wire.getContainer()).andReturn(null); + EasyMock.replay(wire); + context = EasyMock.createMock(WorkContext.class); + context.setCurrentMessageId(EasyMock.eq(id)); + context.setCurrentMessageId(EasyMock.isNull()); + context.setCurrentCorrelationId(corrId); + context.setCurrentCorrelationId(EasyMock.isNull()); + EasyMock.expect(context.getCurrentMessageId()).andReturn(id); + EasyMock.expect(context.getCurrentCorrelationId()).andReturn(corrId); + EasyMock.replay(context); + invoker = new CompositeReferenceCallbackTargetInvoker(operation, wire, context); + } + + private class InsidiousException extends Throwable { + + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceTestCase.java new file mode 100644 index 0000000000..fe62691820 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceTestCase.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.lang.reflect.Type; + +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.TargetInvoker; + +import junit.framework.TestCase; + +/** + * + */ +public class CompositeReferenceTestCase extends TestCase { + + public void testCreateTargetInvoker() throws Exception { + + MyServiceContract serviceContract = new MyServiceContract(); + CompositeReference compositeReference = new CompositeReference("testCompositeReferemce", + null, + null, + serviceContract, + null); + Operation operation = new Operation<Type>("sayHi", null, null, null, false, null); + TargetInvoker targetInvoker = compositeReference.createTargetInvoker(serviceContract, operation); + assertNotNull(targetInvoker); + } + + public void testCreateCallbackTargetInvoker() throws Exception { + + MyServiceContract serviceContract = new MyServiceContract(); + CompositeReference compositeReference = new CompositeReference("testCompositeReferemce", + null, + null, + serviceContract, + null); + Operation operation = new Operation<Type>("sayHi", null, null, null, false, null); + TargetInvoker targetInvoker = compositeReference.createCallbackTargetInvoker(serviceContract, operation); + assertNotNull(targetInvoker); + } + + class MyServiceContract extends ServiceContract { + + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/DuplicateRegistrationTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/DuplicateRegistrationTestCase.java new file mode 100644 index 0000000000..33235983b9 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/DuplicateRegistrationTestCase.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.DuplicateNameException; +import org.apache.tuscany.spi.component.SystemAtomicComponent; + +import junit.framework.TestCase; +import org.apache.tuscany.core.mock.component.Source; +import org.easymock.EasyMock; + +/** + * Verfies children with the same name cannot be registered in the same composite + * + * @version $Rev$ $Date$ + */ +public class DuplicateRegistrationTestCase extends TestCase { + + public void testDuplicateRegistration() throws Exception { + CompositeComponent parent = new CompositeComponentImpl(null, null, null, null); + parent.start(); + + List<Class<?>> interfaces = new ArrayList<Class<?>>(); + interfaces.add(Source.class); + SystemAtomicComponent component1 = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(component1.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component1.isSystem()).andReturn(true).atLeastOnce(); + component1.stop(); + EasyMock.expect(component1.getServiceInterfaces()).andReturn(interfaces); + EasyMock.replay(component1); + + SystemAtomicComponent component2 = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(component2.getName()).andReturn("source").atLeastOnce(); + EasyMock.expect(component2.isSystem()).andReturn(true).atLeastOnce(); + component2.stop(); + EasyMock.expect(component2.getServiceInterfaces()).andReturn(interfaces); + EasyMock.replay(component2); + + parent.register(component1); + try { + parent.register(component2); + fail(); + } catch (DuplicateNameException e) { + // ok + } + parent.stop(); + } + + public void testDuplicateNameSystemService() throws Exception { + List<Class<?>> services = new ArrayList<Class<?>>(); + services.add(Source.class); + CompositeComponent parent = new CompositeComponentImpl("foo", "foo", null, null, null); + SystemAtomicComponent component = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("bar").atLeastOnce(); + EasyMock.expect(component.getServiceInterfaces()).andReturn(services); + EasyMock.expect(component.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.replay(component); + parent.register(component); + SystemAtomicComponent component2 = EasyMock.createMock(SystemAtomicComponent.class); + EasyMock.expect(component2.getName()).andReturn("bar").atLeastOnce(); + EasyMock.expect(component2.getServiceInterfaces()).andReturn(services); + EasyMock.expect(component2.isSystem()).andReturn(true).atLeastOnce(); + EasyMock.replay(component2); + try { + parent.register(component2); + fail(); + } catch (DuplicateNameException e) { + // expected + } + } + + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/ImplementationCompositeLoaderTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/ImplementationCompositeLoaderTestCase.java new file mode 100644 index 0000000000..67efb6fd4c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/ImplementationCompositeLoaderTestCase.java @@ -0,0 +1,192 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import java.net.MalformedURLException; +import java.net.URL; +import javax.xml.namespace.QName; +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import junit.framework.TestCase; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.reportMatcher; +import static org.easymock.EasyMock.verify; +import org.easymock.IArgumentMatcher; +import static org.osoa.sca.Version.XML_NAMESPACE_1_0; + +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.deployer.CompositeClassLoader; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.model.CompositeImplementation; +import org.apache.tuscany.spi.services.artifact.Artifact; +import org.apache.tuscany.spi.services.artifact.ArtifactRepository; + +/** + * @version $Rev$ $Date$ + */ +public class ImplementationCompositeLoaderTestCase extends TestCase { + private static final QName IMPLEMENTATION_COMPOSITE = new QName(XML_NAMESPACE_1_0, "implementation.composite"); + + private ClassLoader cl; + private ImplementationCompositeLoader loader; + private XMLStreamReader reader; + private DeploymentContext context; + private ArtifactRepository artifactRepository; + + public void testName() throws LoaderException, XMLStreamException, MalformedURLException { + String name = "foo"; + expect(reader.getName()).andReturn(IMPLEMENTATION_COMPOSITE); + expect(reader.getAttributeValue(null, "name")).andReturn(name); + expect(reader.getAttributeValue(null, "group")).andReturn(null); + expect(reader.getAttributeValue(null, "version")).andReturn(null); + expect(reader.getAttributeValue(null, "scdlLocation")).andReturn(null); + expect(reader.getAttributeValue(null, "jarLocation")).andReturn(null); + expect(reader.next()).andReturn(END_ELEMENT); + replay(reader); + + replay(context); + replay(artifactRepository); + + CompositeImplementation impl = loader.load(null, reader, context); + verify(reader); + verify(context); + verify(artifactRepository); + assertEquals(name, impl.getName()); + assertNull(impl.getScdlLocation()); + assertNull(impl.getClassLoader()); + } + + public void testWithArtifact() throws LoaderException, XMLStreamException, MalformedURLException { + String name = "foo"; + expect(reader.getName()).andReturn(IMPLEMENTATION_COMPOSITE); + expect(reader.getAttributeValue(null, "name")).andReturn(name); + expect(reader.getAttributeValue(null, "group")).andReturn("com.example"); + expect(reader.getAttributeValue(null, "version")).andReturn("1.0"); + expect(reader.getAttributeValue(null, "scdlLocation")).andReturn(null); + expect(reader.getAttributeValue(null, "jarLocation")).andReturn(null); + expect(reader.next()).andReturn(END_ELEMENT); + replay(reader); + + expect(context.getClassLoader()).andReturn(cl); + replay(context); + URL url = new URL("http://www.example.com/sca/base.jar"); + artifactRepository.resolve(artifactMatcher(url, "com.example", name, "1.0")); + replay(artifactRepository); + + CompositeImplementation impl = loader.load(null, reader, context); + verify(reader); + verify(context); + verify(artifactRepository); + assertEquals(name, impl.getName()); + assertEquals(new URL("jar:http://www.example.com/sca/base.jar!/META-INF/sca/default.scdl"), impl.getScdlLocation()); + assertTrue(impl.getClassLoader() instanceof CompositeClassLoader); + } + + public void testWithScdlLocation() throws LoaderException, XMLStreamException, MalformedURLException { + String name = "foo"; + expect(reader.getName()).andReturn(IMPLEMENTATION_COMPOSITE); + expect(reader.getAttributeValue(null, "name")).andReturn(name); + expect(reader.getAttributeValue(null, "group")).andReturn(null); + expect(reader.getAttributeValue(null, "version")).andReturn(null); + expect(reader.getAttributeValue(null, "scdlLocation")).andReturn("bar.scdl"); + expect(reader.getAttributeValue(null, "jarLocation")).andReturn(null); + expect(reader.next()).andReturn(END_ELEMENT); + replay(reader); + + expect(context.getScdlLocation()).andReturn(new URL("http://www.example.com/sca/base.scdl")); + expect(context.getClassLoader()).andReturn(cl); + replay(context); + replay(artifactRepository); + + CompositeImplementation impl = loader.load(null, reader, context); + verify(reader); + verify(context); + verify(artifactRepository); + assertEquals(name, impl.getName()); + assertEquals(new URL("http://www.example.com/sca/bar.scdl"), impl.getScdlLocation()); + assertSame(cl, impl.getClassLoader()); + } + + public void testWithJarLocation() throws LoaderException, XMLStreamException, MalformedURLException { + String name = "foo"; + expect(reader.getName()).andReturn(IMPLEMENTATION_COMPOSITE); + expect(reader.getAttributeValue(null, "name")).andReturn(name); + expect(reader.getAttributeValue(null, "group")).andReturn(null); + expect(reader.getAttributeValue(null, "version")).andReturn(null); + expect(reader.getAttributeValue(null, "scdlLocation")).andReturn(null); + expect(reader.getAttributeValue(null, "jarLocation")).andReturn("bar.jar"); + expect(reader.next()).andReturn(END_ELEMENT); + replay(reader); + + expect(context.getScdlLocation()).andReturn(new URL("http://www.example.com/sca/base.scdl")); + expect(context.getClassLoader()).andReturn(cl); + replay(context); + replay(artifactRepository); + + CompositeImplementation impl = loader.load(null, reader, context); + verify(reader); + verify(context); + verify(artifactRepository); + assertEquals(name, impl.getName()); + assertEquals(new URL("jar:http://www.example.com/sca/bar.jar!/META-INF/sca/default.scdl"), + impl.getScdlLocation()); + } + + protected void setUp() throws Exception { + super.setUp(); + artifactRepository = createMock(ArtifactRepository.class); + reader = createMock(XMLStreamReader.class); + context = createMock(DeploymentContext.class); + cl = getClass().getClassLoader(); + loader = new ImplementationCompositeLoader(null, artifactRepository); + } + + protected static Artifact artifactMatcher(final URL url, + final String group, + final String name, + final String version) { + reportMatcher(new IArgumentMatcher() { + + public boolean matches(Object object) { + if (!(object instanceof Artifact)) { + return false; + } + + Artifact artifact = (Artifact) object; + boolean match = group.equals(artifact.getGroup()) && + name.equals(artifact.getName()) && + version.equals(artifact.getVersion()) && + "jar".equals(artifact.getType()); + if (match) { + artifact.setUrl(url); + } + return match; + } + + public void appendTo(StringBuffer stringBuffer) { + stringBuffer.append(group).append(':').append(name).append(':').append(version); + } + }); + return null; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/JavaObjectRegistrationTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/JavaObjectRegistrationTestCase.java new file mode 100644 index 0000000000..c3267f8ec6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/composite/JavaObjectRegistrationTestCase.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.composite; + +import javax.naming.ConfigurationException; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.DuplicateNameException; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.event.CompositeStart; +import org.apache.tuscany.core.component.event.CompositeStop; + +/** + * @version $Rev$ $Date$ + */ +public class JavaObjectRegistrationTestCase extends TestCase { + private CompositeComponent composite; + + public void testRegistration() throws Exception { + MockComponent instance = new MockComponent(); + composite.registerJavaObject("foo", MockComponent.class, instance); + MockComponent resolvedInstance = (MockComponent) composite.getSystemChild("foo").getServiceInstance(); + assertSame(instance, resolvedInstance); + } + + public void testDuplicateRegistration() throws ConfigurationException { + MockComponent instance = new MockComponent(); + composite.registerJavaObject("foo", MockComponent.class, instance); + try { + composite.registerJavaObject("foo", MockComponent.class, instance); + fail(); + } catch (DuplicateNameException e) { + // ok + } + } + + public void testSystemAutowireToObject() { + MockComponent instance = new MockComponent(); + composite.registerJavaObject("foo", MockComponent.class, instance); + assertSame(instance, composite.resolveSystemInstance(MockComponent.class)); + assertNull(composite.resolveSystemExternalInstance(MockComponent.class)); + } + + public void testApplicationAutowireToObject() { + MockComponent instance = new MockComponent(); + composite.registerJavaObject("foo", MockComponent.class, instance); + assertNull(composite.resolveInstance(MockComponent.class)); + assertNull(composite.resolveExternalInstance(MockComponent.class)); + } + + protected void setUp() throws Exception { + super.setUp(); + composite = new CompositeComponentImpl(null, null, null, null); + composite.start(); + composite.publish(new CompositeStart(this, null)); + } + + protected void tearDown() throws Exception { + composite.publish(new CompositeStop(this, null)); + composite.stop(); + super.tearDown(); + } + + private static class MockComponent { + public String hello(String message) { + return message; + } + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/AsyncJavaTargetInvokerTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/AsyncJavaTargetInvokerTestCase.java new file mode 100644 index 0000000000..77e48991d5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/AsyncJavaTargetInvokerTestCase.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java; + +import java.lang.reflect.Method; + +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.services.work.WorkScheduler; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.MessageImpl; + +import junit.framework.TestCase; +import static org.apache.tuscany.core.implementation.java.mock.MockFactory.createJavaComponent; +import org.apache.tuscany.core.implementation.java.mock.components.AsyncTarget; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.getCurrentArguments; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import org.easymock.IAnswer; + +/** + * @version $Rev$ $Date$ + */ +public class AsyncJavaTargetInvokerTestCase extends TestCase { + + public void testInvoke() throws Exception { + AsyncTarget target = createMock(AsyncTarget.class); + target.invoke(); + expectLastCall().once(); + replay(target); + JavaAtomicComponent component = createJavaComponent(target); + AsyncMonitor monitor = createMock(AsyncMonitor.class); + replay(monitor); + + Message msg = new MessageImpl(); + Object id = new Object(); + msg.setMessageId(id); + + WorkContext context = createMock(WorkContext.class); + context.setCurrentMessageId(null); + context.setCurrentCorrelationId(id); + replay(context); + Method method = AsyncTarget.class.getMethod("invoke"); + method.setAccessible(true); + InboundWire wire = createMock(InboundWire.class); + AsyncJavaTargetInvoker invoker = + new AsyncJavaTargetInvoker(method, wire, component, monitor, context); + invoker.invoke(msg); + verify(target); + } + + public void testClone() throws Exception { + AsyncTarget target = createMock(AsyncTarget.class); + target.invoke(); + expectLastCall().once(); + replay(target); + JavaAtomicComponent component = createJavaComponent(target); + AsyncMonitor monitor = createMock(AsyncMonitor.class); + replay(monitor); + WorkScheduler scheduler = createMock(WorkScheduler.class); + scheduler.scheduleWork(isA(Runnable.class)); + expectLastCall().andStubAnswer(new IAnswer<Object>() { + public Object answer() throws Throwable { + Runnable runnable = (Runnable) getCurrentArguments()[0]; + runnable.run(); + return null; + } + }); + replay(scheduler); + WorkContext context = createMock(WorkContext.class); + InboundWire wire = createMock(InboundWire.class); + Method method = AsyncTarget.class.getMethod("invoke"); + method.setAccessible(true); + AsyncJavaTargetInvoker invoker = + new AsyncJavaTargetInvoker(method, wire, component, monitor, context); + AsyncJavaTargetInvoker clone = invoker.clone(); + Message msg = new MessageImpl(); + clone.invoke(msg); + verify(target); + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/DifferentInterfaceWireTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/DifferentInterfaceWireTestCase.java new file mode 100644 index 0000000000..64a573c6e5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/DifferentInterfaceWireTestCase.java @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java; + +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.apache.tuscany.core.implementation.java.mock.MockFactory; +import org.apache.tuscany.core.implementation.java.mock.components.OtherTarget; +import org.apache.tuscany.core.implementation.java.mock.components.OtherTargetImpl; +import org.apache.tuscany.core.implementation.java.mock.components.Source; +import org.apache.tuscany.core.implementation.java.mock.components.SourceImpl; +import org.apache.tuscany.core.implementation.java.mock.components.Target; +import org.easymock.EasyMock; +import org.easymock.IAnswer; + + +/** + * Tests wires that have different interfaces on the source and target side + * + * @version $Rev$ $Date$ + */ +public class DifferentInterfaceWireTestCase extends TestCase { + + public void testDifferentInterfaceInjection() throws Exception { + Map<String, Member> members = new HashMap<String, Member>(); + Method m = SourceImpl.class.getMethod("setTarget", Target.class); + members.put("target", m); + ScopeContainer scope = createMock(); + scope.start(); + Map<String, AtomicComponent> contexts = + MockFactory.createWiredComponents("source", + SourceImpl.class, + Target.class, + scope, + members, + "target", + OtherTarget.class, + OtherTargetImpl.class, + scope); + AtomicComponent sourceComponent = contexts.get("source"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = source.getTarget(); + assertTrue(Proxy.isProxyClass(target.getClass())); + assertNotNull(target); + scope.stop(); + EasyMock.verify(scope); + } + + public void testDifferentInterfaceMultiplicityInjection() throws Exception { + Map<String, Member> members = new HashMap<String, Member>(); + Method m = SourceImpl.class.getMethod("setTargets", List.class); + members.put("target", m); + ScopeContainer scope = createMock(); + scope.start(); + Map<String, AtomicComponent> contexts = + MockFactory.createWiredMultiplicity("source", SourceImpl.class, Target.class, scope, + "target", OtherTarget.class, OtherTargetImpl.class, members, scope); + AtomicComponent sourceComponent = contexts.get("source"); + Source source = (Source) sourceComponent.getServiceInstance(); + List<Target> targets = source.getTargets(); + assertEquals(1, targets.size()); + Target target = targets.get(0); + target.setString("foo"); + assertEquals("foo", target.getString()); + assertTrue(Proxy.isProxyClass(target.getClass())); + scope.stop(); + EasyMock.verify(scope); + } + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + private ScopeContainer createMock() { + ScopeContainer scope = EasyMock.createMock(ScopeContainer.class); + scope.start(); + scope.stop(); + scope.register(EasyMock.isA(AtomicComponent.class)); + EasyMock.expectLastCall().atLeastOnce(); + EasyMock.expect(scope.getScope()).andReturn(Scope.MODULE).atLeastOnce(); + scope.getInstance(EasyMock.isA(AtomicComponent.class)); + EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { + private Map<AtomicComponent, Object> cache = new HashMap<AtomicComponent, Object>(); + + public Object answer() throws Throwable { + AtomicComponent component = (AtomicComponent) EasyMock.getCurrentArguments()[0]; + Object instance = cache.get(component); + if (instance == null) { + instance = component.createInstance(); + cache.put(component, instance); + } + return instance; + } + }).anyTimes(); + EasyMock.replay(scope); + return scope; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/GetServiceByNameTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/GetServiceByNameTestCase.java new file mode 100644 index 0000000000..4695866232 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/GetServiceByNameTestCase.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.wire.InboundInvocationChain; +import org.apache.tuscany.spi.wire.InboundWire; + +import junit.framework.TestCase; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.java.mock.components.Target; +import org.apache.tuscany.core.implementation.java.mock.components.TargetImpl; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.apache.tuscany.core.wire.jdk.JDKWireService; +import org.easymock.EasyMock; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class GetServiceByNameTestCase extends TestCase { + + public void testServiceLocate() throws Exception { + ScopeContainer scope = createMock(ScopeContainer.class); + scope.register(EasyMock.isA(JavaAtomicComponent.class)); + expect(scope.getScope()).andReturn(Scope.MODULE); + replay(scope); + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setScopeContainer(scope); + configuration.setInstanceFactory(new PojoObjectFactory<TargetImpl>(TargetImpl.class.getConstructor())); + configuration.addServiceInterface(Target.class); + configuration.setWireService(new JDKWireService()); + final JavaAtomicComponent component = new JavaAtomicComponent("target", configuration, null); + + InboundWire wire = createMock(InboundWire.class); + + JavaServiceContract contract = new JavaServiceContract(Target.class); + EasyMock.expect(wire.getServiceContract()).andReturn(contract).anyTimes(); + expect(wire.getServiceName()).andReturn("Target"); + Map<Operation<?>, InboundInvocationChain> chains = new HashMap<Operation<?>, InboundInvocationChain>(); + expect(wire.getInvocationChains()).andReturn(chains); + expect(wire.getCallbackReferenceName()).andReturn(null); + replay(wire); + component.addInboundWire(wire); + component.prepare(); + component.start(); + assertTrue(component.getServiceInstance("Target") instanceof Target); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaAtomicComponentMetadataInjectionTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaAtomicComponentMetadataInjectionTestCase.java new file mode 100644 index 0000000000..a907e047b8 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaAtomicComponentMetadataInjectionTestCase.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java; + +import junit.framework.TestCase; + +/** + * Tests SCA metadata such as <code>@ComponentName</code> and <code>@SCAObject</code> are handled properly + * + * @version $Rev$ $Date$ + */ +public class JavaAtomicComponentMetadataInjectionTestCase extends TestCase { + + public void testComponentNameSet() throws Exception { + } + + public void testModuleContextSet() throws Exception { + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaAtomicComponentNegativeMetadataTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaAtomicComponentNegativeMetadataTestCase.java new file mode 100644 index 0000000000..365de8e41f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaAtomicComponentNegativeMetadataTestCase.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java; + +import junit.framework.TestCase; + + +/** + * Performs rudimentary negative testing by using malformed metadata on a POJO + * + * @version $Rev $Date + */ +public class JavaAtomicComponentNegativeMetadataTestCase extends TestCase { + + /** + * Tests that a pojo with <code>@ComponentName</code> specified on a non-String type generates an error. + * <p/> + * <strong>NB:</strong> the test assumes an error with a message containing "@ComponentName" is generated + */ + public void testBadNameType() throws Exception { +// CompositeComponent mc = new CompositeComponentImpl(); +// mc.setName("mc"); +// try { +// MockFactory.createPojoContext("BadNamePojo", BadNamePojo.class, Scope.MODULE, mc); +// } catch (NoSuchMethodException e) { +// if (e.getMessage().indexOf("@ComponentName") < 0) { +// throw e; +// } +// } +// + } + + /** + * Tests that a pojo with <code>@Context</code> specified on a non-ModuleContext type generates an error. + * <p/> + * <strong>NB:</strong> the test assumes an error with a message containing "@Context" is generated + */ + public void testContextType() throws Exception { +// CompositeComponent mc = new CompositeComponentImpl(); +// mc.setName("mc"); +// try { +// MockFactory.createPojoContext("BadContextPojo", BadContextPojo.class, Scope.MODULE, mc); +// } catch (NoSuchMethodException e) { +// if (e.getMessage().indexOf("@SCAObject") < 0) { +// throw e; +// } +// } +// + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaComponentBuilderTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaComponentBuilderTestCase.java new file mode 100644 index 0000000000..4877d06aba --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaComponentBuilderTestCase.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java; + +import java.lang.reflect.Constructor; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceContract; + +import junit.framework.TestCase; +import org.apache.tuscany.core.deployer.RootDeploymentContext; +import org.apache.tuscany.core.implementation.composite.CompositeComponentImpl; +import org.apache.tuscany.core.implementation.java.mock.components.Source; +import org.apache.tuscany.core.implementation.java.mock.components.SourceImpl; +import org.apache.tuscany.core.implementation.java.mock.components.Target; +import org.apache.tuscany.core.wire.jdk.JDKWireService; +import org.easymock.EasyMock; +import org.easymock.IAnswer; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class JavaComponentBuilderTestCase extends TestCase { + private DeploymentContext deploymentContext; + + public void testBuild() throws Exception { + CompositeComponent parent = new CompositeComponentImpl(null, null, null, null); + + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> sourceType = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + sourceType.setImplementationScope(Scope.MODULE); + JavaMappedReference reference = new JavaMappedReference(); + reference.setName("target"); + reference.setMember(SourceImpl.class.getMethod("setTarget", Target.class)); + sourceType.add(reference); + + ServiceContract<?> sourceContract = new JavaServiceContract(Source.class); + JavaMappedService sourceServiceDefinition = new JavaMappedService(); + sourceServiceDefinition.setName("Source"); + sourceServiceDefinition.setServiceContract(sourceContract); + + sourceType.add(sourceServiceDefinition); + Constructor<SourceImpl> constructor = SourceImpl.class.getConstructor((Class[]) null); + sourceType.setConstructorDefinition(new ConstructorDefinition<SourceImpl>(constructor)); + JavaImplementation sourceImpl = new JavaImplementation(); + sourceImpl.setComponentType(sourceType); + sourceImpl.setImplementationClass(SourceImpl.class); + ComponentDefinition<JavaImplementation> sourceComponentDefinition = + new ComponentDefinition<JavaImplementation>(sourceImpl); + + JavaComponentBuilder builder = new JavaComponentBuilder(); + builder.setWireService(new JDKWireService()); + JavaAtomicComponent component = + (JavaAtomicComponent) builder.build(parent, sourceComponentDefinition, deploymentContext); + deploymentContext.getModuleScope().start(); + component.start(); + Source source = (Source) component.getServiceInstance(); + assertNotNull(source); + component.stop(); + } + + protected void setUp() throws Exception { + super.setUp(); + deploymentContext = new RootDeploymentContext(null, null, createMock(), null); + } + + private ScopeContainer createMock() { + ScopeContainer scope = EasyMock.createMock(ScopeContainer.class); + scope.start(); + scope.stop(); + scope.register(EasyMock.isA(AtomicComponent.class)); + EasyMock.expectLastCall().atLeastOnce(); + EasyMock.expect(scope.getScope()).andReturn(Scope.MODULE).atLeastOnce(); + scope.getInstance(EasyMock.isA(AtomicComponent.class)); + EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { + private Map<AtomicComponent, Object> cache = new HashMap<AtomicComponent, Object>(); + + public Object answer() throws Throwable { + AtomicComponent component = (AtomicComponent) EasyMock.getCurrentArguments()[0]; + Object instance = cache.get(component); + if (instance == null) { + instance = component.createInstance(); + cache.put(component, instance); + } + return instance; + } + }).anyTimes(); + EasyMock.replay(scope); + return scope; + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaReferenceWireTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaReferenceWireTestCase.java new file mode 100644 index 0000000000..5afc8b6511 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaReferenceWireTestCase.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java; + +import java.lang.reflect.Constructor; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.WireService; + +import junit.framework.TestCase; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.java.mock.components.Source; +import org.apache.tuscany.core.implementation.java.mock.components.SourceImpl; +import org.apache.tuscany.core.implementation.java.mock.components.Target; +import org.apache.tuscany.core.implementation.java.mock.components.TargetImpl; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.easymock.EasyMock; +import org.easymock.IAnswer; + +/** + * Validates wiring from a Java atomic context + * + * @version $$Rev$$ $$Date$$ + */ +public class JavaReferenceWireTestCase extends TestCase { + + public void testReferenceSet() throws Exception { + ScopeContainer scope = createMock(); + scope.start(); + final Target target = new TargetImpl(); + PojoConfiguration configuration = new PojoConfiguration(); + configuration.addReferenceSite("target", SourceImpl.class.getMethod("setTarget", Target.class)); + configuration.addServiceInterface(Source.class); + Constructor<SourceImpl> ctr = SourceImpl.class.getConstructor(); + configuration.setInstanceFactory(new PojoObjectFactory<SourceImpl>(ctr)); + configuration.setScopeContainer(scope); + OutboundWire wire = EasyMock.createMock(OutboundWire.class); + wire.getInvocationChains(); + EasyMock.expectLastCall().andReturn(new HashMap<Operation<?>, OutboundInvocationChain>()); + EasyMock.expect(wire.getReferenceName()).andReturn("target").atLeastOnce(); + EasyMock.replay(wire); + WireService service = EasyMock.createMock(WireService.class); + EasyMock.expect(service.createProxy(EasyMock.eq(wire))).andAnswer(new IAnswer<Object>() { + public Object answer() throws Throwable { + OutboundWire wire = (OutboundWire) EasyMock.getCurrentArguments()[0]; + wire.getInvocationChains(); + return target; + } + + }).atLeastOnce(); + EasyMock.replay(service); + configuration.setWireService(service); + JavaAtomicComponent sourceContext = new JavaAtomicComponent("source", configuration, null); + sourceContext.addOutboundWire(wire); + sourceContext.start(); + Source source = (Source) sourceContext.getServiceInstance(); + assertSame(target, source.getTarget()); + scope.stop(); + EasyMock.verify(wire); + EasyMock.verify(scope); + EasyMock.verify(service); + } + + private ScopeContainer createMock() { + ScopeContainer scope = EasyMock.createMock(ScopeContainer.class); + scope.start(); + scope.stop(); + scope.register(EasyMock.isA(AtomicComponent.class)); + EasyMock.expectLastCall().atLeastOnce(); + EasyMock.expect(scope.getScope()).andReturn(Scope.MODULE).atLeastOnce(); + scope.getInstance(EasyMock.isA(AtomicComponent.class)); + EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { + private Map<AtomicComponent, Object> cache = new HashMap<AtomicComponent, Object>(); + + public Object answer() throws Throwable { + AtomicComponent component = (AtomicComponent) EasyMock.getCurrentArguments()[0]; + Object instance = cache.get(component); + if (instance == null) { + instance = component.createInstance(); + cache.put(component, instance); + } + return instance; + } + }).anyTimes(); + EasyMock.replay(scope); + return scope; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaTargetInvokerTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaTargetInvokerTestCase.java new file mode 100644 index 0000000000..018273be28 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaTargetInvokerTestCase.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java; + +import java.lang.reflect.Method; + +import org.apache.tuscany.spi.component.ScopeContainer; + +import junit.framework.Assert; +import junit.framework.TestCase; +import org.apache.tuscany.core.component.scope.ModuleScopeContainer; +import org.apache.tuscany.core.implementation.java.mock.MockFactory; + +public class JavaTargetInvokerTestCase extends TestCase { + + private Method echoMethod; + + public JavaTargetInvokerTestCase() { + super(); + } + + public JavaTargetInvokerTestCase(String arg0) { + super(arg0); + } + + public void setUp() throws Exception { + echoMethod = Echo.class.getDeclaredMethod("echo", String.class); + Assert.assertNotNull(echoMethod); + } + + public void testScopedInvoke() throws Exception { + ScopeContainer scope = new ModuleScopeContainer(null); + scope.start(); + JavaAtomicComponent component = + MockFactory.createJavaComponent("foo", scope, Echo.class); + scope.register(component); + JavaTargetInvoker invoker = new JavaTargetInvoker(echoMethod, component); + invoker.setCacheable(false); + assertEquals("foo", invoker.invokeTarget("foo")); + scope.stop(); + } + + public void testClone() throws Exception { + ScopeContainer scope = new ModuleScopeContainer(null); + scope.start(); + JavaAtomicComponent component = + MockFactory.createJavaComponent("foo", scope, Echo.class); + scope.register(component); + JavaTargetInvoker invoker = new JavaTargetInvoker(echoMethod, component); + invoker.setCacheable(false); + JavaTargetInvoker clone = invoker.clone(); + assertEquals("foo", clone.invokeTarget("foo")); + scope.stop(); + } + + public static class Echo { + public String echo(String message) throws Exception { + return message; + } + + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/MultiplicityTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/MultiplicityTestCase.java new file mode 100644 index 0000000000..92c8c7acfa --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/MultiplicityTestCase.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java; + +import junit.framework.TestCase; + + +/** + * Tests wires that are configured with a multiplicity + * + * @version $Rev$ $Date$ + */ +public class MultiplicityTestCase extends TestCase { + + public void testMultiplicity() throws Exception { +// CompositeComponent context = createContext(); +// context.start(); +// context.registerModelObject(MockFactory.createModuleWithWiredComponents(Scope.MODULE, Scope.MODULE)); +// context.publish(new CompositeStart(this)); +// Source source = (Source) ((AtomicComponent) context.getContext("source")).getTargetInstance(); +// Assert.assertNotNull(source); +// Target target = (Target) ((AtomicComponent)context.getContext("target")).getTargetInstance(); +// Assert.assertNotNull(target); +// // test setter injection +// List<Target> targets = source.getTargets(); +// Assert.assertEquals(1, targets.size()); +// +// // test field injection +// targets = source.getTargetsThroughField(); +// Assert.assertEquals(1, targets.size()); + } + +// private CompositeComponent createContext() { +// CompositeComponentImpl context = new CompositeComponentImpl(); +// context.setName("system.context"); +// List<ContextFactoryBuilder>builders = MockFactory.createSystemBuilders(); +// WireService wireService = new DefaultWireFactoryService(new MessageFactoryImpl(), +// new JDKWireFactoryService(), new DefaultPolicyBuilderRegistry()); +// builders.add(new JavaContextFactoryBuilder(wireService)); +// List<WireBuilder> wireBuilders = new ArrayList<WireBuilder>(); +// wireBuilders.add(new JavaTargetWireBuilder()); +// context.setConfigurationContext(new MockConfigContext(builders,wireBuilders)); +// return context; +// } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/ReferenceInjectionTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/ReferenceInjectionTestCase.java new file mode 100644 index 0000000000..e6309e0c97 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/ReferenceInjectionTestCase.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java; + +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; + +import junit.framework.TestCase; +import org.apache.tuscany.core.implementation.java.mock.MockFactory; +import org.apache.tuscany.core.implementation.java.mock.components.Source; +import org.apache.tuscany.core.implementation.java.mock.components.SourceImpl; +import org.apache.tuscany.core.implementation.java.mock.components.Target; +import org.apache.tuscany.core.implementation.java.mock.components.TargetImpl; +import org.apache.tuscany.core.component.scope.ModuleScopeContainer; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class ReferenceInjectionTestCase extends TestCase { + + private Map<String, Member> members; + + public void testProxiedReferenceInjection() throws Exception { + ScopeContainer scope = new ModuleScopeContainer(null); + scope.start(); + Map<String, AtomicComponent> contexts = + MockFactory.createWiredComponents("source", SourceImpl.class, scope, + members, "target", Target.class, TargetImpl.class, scope); + AtomicComponent sourceComponent = contexts.get("source"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = source.getTarget(); + assertTrue(Proxy.isProxyClass(target.getClass())); + + assertNotNull(target); + scope.stop(); + } + + protected void setUp() throws Exception { + super.setUp(); + members = new HashMap<String, Member>(); + Method m = SourceImpl.class.getMethod("setTarget", Target.class); + members.put("target", m); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/CallbackInvocationTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/CallbackInvocationTestCase.java new file mode 100644 index 0000000000..718fd50845 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/CallbackInvocationTestCase.java @@ -0,0 +1,266 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.integration.component; + +import java.lang.reflect.Method; +import java.net.URI; +import java.net.URISyntaxException; + +import org.osoa.sca.annotations.Callback; + +import org.apache.tuscany.spi.builder.Connector; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.idl.InvalidServiceContractException; +import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry; +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.ReferenceTarget; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.services.work.WorkScheduler; +import org.apache.tuscany.spi.wire.WireService; + +import junit.framework.TestCase; +import org.apache.tuscany.core.builder.ConnectorImpl; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.scope.ModuleScopeContainer; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; +import org.apache.tuscany.core.implementation.java.JavaAtomicComponent; +import org.apache.tuscany.core.implementation.java.JavaComponentBuilder; +import org.apache.tuscany.core.implementation.java.JavaImplementation; +import org.apache.tuscany.core.wire.jdk.JDKWireService; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.getCurrentArguments; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; +import org.easymock.IAnswer; + +/** + * Verifies callback integration scenarios. + * + * @version $Rev$ $Date$ + */ +public class CallbackInvocationTestCase extends TestCase { + + private ScopeContainer container; + private DeploymentContext context; + private JavaComponentBuilder builder; + private WireService wireService; + private WorkScheduler scheduler; + private WorkContext workContext; + /** + * Verifies callback wires are built and callback invocations are handled properly + */ + public void testComponentToComponentCallback() throws Exception { + ComponentDefinition<JavaImplementation> targetDefinition = createTarget(); + JavaAtomicComponent fooComponent = + (JavaAtomicComponent) builder.build(null, targetDefinition, context); + wireService.createWires(fooComponent, targetDefinition); + container.register(fooComponent); + + CompositeComponent parent = createMock(CompositeComponent.class); + parent.getChild(isA(String.class)); + expectLastCall().andReturn(fooComponent).anyTimes(); + replay(parent); + + ComponentDefinition<JavaImplementation> sourceDefinition = createSource("fooClient"); + JavaAtomicComponent clientComponent = + (JavaAtomicComponent) builder.build(parent, sourceDefinition, context); + wireService.createWires(clientComponent, sourceDefinition); + container.register(clientComponent); + + Connector connector = new ConnectorImpl(new JDKWireService(), null, scheduler , workContext); + + connector.connect(clientComponent); + FooClient client = (FooClient) clientComponent.getServiceInstance(); + client.invoke(); + assertTrue(client.invoked); + } + + /** + * Verifies a callback in response to an invocation from two different client components is routed back to the + * appropriate client. + */ + public void testTwoSourceComponentToComponentCallback() throws Exception { + ComponentDefinition<JavaImplementation> targetDefinition = createTarget(); + JavaAtomicComponent fooComponent = + (JavaAtomicComponent) builder.build(null, targetDefinition, context); + wireService.createWires(fooComponent, targetDefinition); + container.register(fooComponent); + + CompositeComponent parent = createMock(CompositeComponent.class); + parent.getChild(isA(String.class)); + expectLastCall().andReturn(fooComponent).anyTimes(); + replay(parent); + + ComponentDefinition<JavaImplementation> sourceDefinition1 = createSource("fooCleint1"); + ComponentDefinition<JavaImplementation> sourceDefinition2 = createSource("fooCleint2"); + JavaAtomicComponent clientComponent1 = + (JavaAtomicComponent) builder.build(parent, sourceDefinition1, context); + wireService.createWires(clientComponent1, sourceDefinition1); + container.register(clientComponent1); + JavaAtomicComponent clientComponent2 = + (JavaAtomicComponent) builder.build(parent, sourceDefinition2, context); + wireService.createWires(clientComponent2, sourceDefinition2); + container.register(clientComponent2); + + Connector connector = new ConnectorImpl(new JDKWireService(), null, scheduler, workContext); + connector.connect(clientComponent1); + connector.connect(clientComponent2); + FooClient client1 = (FooClient) clientComponent1.getServiceInstance(); + client1.invoke(); + assertTrue(client1.invoked); + FooClient client2 = (FooClient) clientComponent2.getServiceInstance(); + client2.invoke(); + assertTrue(client2.invoked); + } + + + private ComponentDefinition<JavaImplementation> createTarget() throws NoSuchMethodException, + InvalidServiceContractException { + ConstructorDefinition<FooImpl> ctorDef = new ConstructorDefinition<FooImpl>(FooImpl.class.getConstructor()); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + type.setConstructorDefinition(ctorDef); + type.setImplementationScope(Scope.MODULE); + Method method = FooImpl.class.getMethod("setCallback", FooCallback.class); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract<?> contract = registry.introspect(Foo.class); + contract.setCallbackClass(FooCallback.class); + contract.setCallbackName("callback"); + JavaMappedService mappedService = new JavaMappedService("Foo", contract, false, "callback", method); + type.getServices().put("Foo", mappedService); + + JavaImplementation impl = new JavaImplementation(); + impl.setComponentType(type); + impl.setImplementationClass(FooImpl.class); + return new ComponentDefinition<JavaImplementation>("foo", impl); + } + + private ComponentDefinition<JavaImplementation> createSource(String name) + throws NoSuchMethodException, URISyntaxException, InvalidServiceContractException { + ConstructorDefinition<FooClient> ctorDef = + new ConstructorDefinition<FooClient>(FooClient.class.getConstructor()); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + type.setConstructorDefinition(ctorDef); + type.setImplementationScope(Scope.MODULE); + Method method = FooClient.class.getMethod("setFoo", Foo.class); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract<?> contract = registry.introspect(Foo.class); + contract.setCallbackClass(FooCallback.class); + contract.setCallbackName("callback"); + JavaMappedReference mappedReference = new JavaMappedReference("foo", contract, method); + type.getReferences().put("foo", mappedReference); + ReferenceTarget refTarget = new ReferenceTarget(); + refTarget.setReferenceName("foo"); + refTarget.getTargets().add(new URI("foo")); + JavaImplementation impl = new JavaImplementation(); + impl.setComponentType(type); + impl.setImplementationClass(FooClient.class); + ComponentDefinition<JavaImplementation> def = new ComponentDefinition<JavaImplementation>(name, impl); + def.getReferenceTargets().put("foo", refTarget); + return def; + } + + @Callback(FooCallback.class) + public static interface Foo { + void call(); + } + + public static class FooImpl implements Foo { + private FooCallback callback; + + public FooImpl() { + } + + @Callback + public void setCallback(FooCallback callback) { + this.callback = callback; + } + + public void call() { + callback.callback(); + } + } + + public static class FooClient implements FooCallback { + + private Foo foo; + private boolean invoked; + + public FooClient() { + } + + public void setFoo(Foo foo) { + this.foo = foo; + } + + public void callback() { + if (invoked) { + fail(); + } + invoked = true; + } + + public void invoke() { + foo.call(); + } + } + + public interface FooCallback { + void callback(); + } + + protected void setUp() throws Exception { + super.setUp(); + wireService = new JDKWireService(); + container = new ModuleScopeContainer(); + container.start(); + context = createMock(DeploymentContext.class); + context.getModuleScope(); + expectLastCall().andReturn(container).anyTimes(); + replay(context); + + scheduler = createMock(WorkScheduler.class); + scheduler.scheduleWork(isA(Runnable.class)); + expectLastCall().andStubAnswer(new IAnswer<Object>() { + public Object answer() throws Throwable { + Runnable runnable = (Runnable) getCurrentArguments()[0]; + runnable.run(); + return null; + } + }); + replay(scheduler); + + builder = new JavaComponentBuilder(); + workContext = new WorkContextImpl(); + builder.setWorkContext(workContext); + builder.setWireService(new JDKWireService(workContext, null)); + builder.setWorkScheduler(scheduler); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/OneWayWireInvocationTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/OneWayWireInvocationTestCase.java new file mode 100644 index 0000000000..91d31d2db2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/OneWayWireInvocationTestCase.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.integration.component; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.services.work.WorkScheduler; +import org.apache.tuscany.spi.wire.InboundInvocationChain; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.MessageImpl; +import org.apache.tuscany.spi.wire.WireService; + +import junit.framework.TestCase; +import org.apache.tuscany.core.implementation.java.AsyncJavaTargetInvoker; +import org.apache.tuscany.core.implementation.java.JavaAtomicComponent; +import org.apache.tuscany.core.implementation.java.mock.MockFactory; +import static org.apache.tuscany.core.implementation.java.mock.MockFactory.createServiceWire; +import org.apache.tuscany.core.implementation.java.mock.components.AsyncTarget; +import org.apache.tuscany.core.wire.jdk.JDKWireService; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.getCurrentArguments; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import org.easymock.IAnswer; + +/** + * Verifies non-blocking invocations are properly made through a wire + * + * @version $Rev$ $Date$ + */ +public class OneWayWireInvocationTestCase extends TestCase { + + WireService wireService = new JDKWireService(); + + public void testNoInterceptors() throws Exception { + AsyncTarget target = createMock(AsyncTarget.class); + target.invoke(); + expectLastCall().once(); + replay(target); + JavaAtomicComponent component = MockFactory.createJavaComponent(target); + WorkScheduler scheduler = createMock(WorkScheduler.class); + scheduler.scheduleWork(isA(Runnable.class)); + expectLastCall().andStubAnswer(new IAnswer<Object>() { + public Object answer() throws Throwable { + Runnable runnable = (Runnable) getCurrentArguments()[0]; + runnable.run(); + return null; + } + }); + replay(scheduler); + WorkContext context = createMock(WorkContext.class); + Method method = AsyncTarget.class.getMethod("invoke"); + method.setAccessible(true); + InboundWire inboundWire = createMock(InboundWire.class); + AsyncJavaTargetInvoker invoker = + new AsyncJavaTargetInvoker(method, inboundWire, component, null, context); + InboundWire wire = createServiceWire("foo", AsyncTarget.class, null); + Map<Operation<?>, InboundInvocationChain> chains = wire.getInvocationChains(); + InboundInvocationChain chain = chains.get(wire.getServiceContract().getOperations().get("invoke")); + chain.setTargetInvoker(invoker); + chain.prepare(); + MessageImpl msg = new MessageImpl(); + msg.setTargetInvoker(invoker); + chain.getHeadInterceptor().invoke(msg); + verify(target); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/OutboundWireToJavaTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/OutboundWireToJavaTestCase.java new file mode 100644 index 0000000000..9ea0cd99e3 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/OutboundWireToJavaTestCase.java @@ -0,0 +1,229 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.integration.component; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.concurrent.FutureTask; + +import org.apache.tuscany.spi.QualifiedName; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.idl.InvalidServiceContractException; +import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.WireService; + +import junit.framework.TestCase; +import org.apache.tuscany.core.builder.ConnectorImpl; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.event.CompositeStart; +import org.apache.tuscany.core.component.event.CompositeStop; +import org.apache.tuscany.core.component.event.HttpSessionEnd; +import org.apache.tuscany.core.component.event.HttpSessionStart; +import org.apache.tuscany.core.component.event.RequestEnd; +import org.apache.tuscany.core.component.event.RequestStart; +import org.apache.tuscany.core.component.scope.HttpSessionScopeContainer; +import org.apache.tuscany.core.component.scope.ModuleScopeContainer; +import org.apache.tuscany.core.component.scope.RequestScopeContainer; +import org.apache.tuscany.core.component.scope.StatelessScopeContainer; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.java.JavaAtomicComponent; +import org.apache.tuscany.core.implementation.java.mock.MockFactory; +import org.apache.tuscany.core.implementation.java.mock.components.Target; +import org.apache.tuscany.core.implementation.java.mock.components.TargetImpl; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.apache.tuscany.core.wire.OutboundInvocationChainImpl; +import org.apache.tuscany.core.wire.OutboundWireImpl; +import org.apache.tuscany.core.wire.jdk.JDKWireService; +import org.easymock.EasyMock; + +/** + * Validates wiring from a service context to Java atomic contexts by scope + * + * @version $$Rev$$ $$Date$$ + */ +public class OutboundWireToJavaTestCase extends TestCase { + private WorkContext workContext = new WorkContextImpl(); + private WireService wireService = new JDKWireService(new WorkContextImpl(), null); + + public void testToStatelessScope() throws Exception { + StatelessScopeContainer scope = new StatelessScopeContainer(workContext); + scope.start(); + final OutboundWire wire = getWire(scope); + Target service = (Target) wireService.createProxy(wire); + assertNotNull(service); + service.setString("foo"); + assertEquals(null, service.getString()); + scope.stop(); + } + + public void testToRequestScope() throws Exception { + final RequestScopeContainer scope = new RequestScopeContainer(workContext); + scope.start(); + + scope.onEvent(new RequestStart(this)); + + final OutboundWire wire = getWire(scope); + Target service = (Target) wireService.createProxy(wire); + assertNotNull(service); + service.setString("foo"); + + // another request + Executor executor = Executors.newSingleThreadExecutor(); + FutureTask<Void> future = new FutureTask<Void>(new Runnable() { + public void run() { + scope.onEvent(new RequestStart(this)); + Target service2 = (Target) wireService.createProxy(wire); + Target target2 = (Target) wireService.createProxy(wire); + assertEquals(null, service2.getString()); + service2.setString("bar"); + assertEquals("bar", service2.getString()); + assertEquals("bar", target2.getString()); + scope.onEvent(new RequestEnd(this)); + } + }, null); + executor.execute(future); + future.get(); + + assertEquals("foo", service.getString()); + scope.onEvent(new RequestEnd(this)); + scope.stop(); + } + + public void testToSessionScope() throws Exception { + HttpSessionScopeContainer scope = new HttpSessionScopeContainer(workContext); + scope.start(); + Object session1 = new Object(); + workContext.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session1); + scope.onEvent(new HttpSessionStart(this, session1)); + + final OutboundWire wire = getWire(scope); + Target service = (Target) wireService.createProxy(wire); + Target target = (Target) wireService.createProxy(wire); + assertNotNull(service); + service.setString("foo"); + assertEquals("foo", service.getString()); + assertEquals("foo", target.getString()); + + workContext.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + + //second session + Object session2 = new Object(); + workContext.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session2); + scope.onEvent(new HttpSessionStart(this, session2)); + + Target service2 = (Target) wireService.createProxy(wire); + assertNotNull(service2); + assertNull(service2.getString()); + Target target2 = (Target) wireService.createProxy(wire); + service2.setString("bar"); + assertEquals("bar", service2.getString()); + assertEquals("bar", target2.getString()); + + scope.onEvent(new HttpSessionEnd(this, session2)); + workContext.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + + workContext.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session1); + assertEquals("foo", service.getString()); + + scope.onEvent(new HttpSessionEnd(this, session1)); + + scope.stop(); + } + + public void testToModuleScope() throws Exception { + + ModuleScopeContainer scope = new ModuleScopeContainer(workContext); + scope.start(); + scope.onEvent(new CompositeStart(this, null)); + final OutboundWire wire = getWire(scope); + Target service = (Target) wireService.createProxy(wire); + Target target = (Target) wireService.createProxy(wire); + assertNotNull(service); + service.setString("foo"); + assertEquals("foo", service.getString()); + assertEquals("foo", target.getString()); + scope.onEvent(new CompositeStop(this, null)); + scope.stop(); + } + + private OutboundWire getWire(ScopeContainer scope) throws NoSuchMethodException, + InvalidServiceContractException { + ConnectorImpl connector = new ConnectorImpl(); + + CompositeComponent parent = EasyMock.createMock(CompositeComponent.class); + + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setScopeContainer(scope); + configuration.setInstanceFactory(new PojoObjectFactory<TargetImpl>(TargetImpl.class.getConstructor())); + configuration.addServiceInterface(Target.class); + configuration.setParent(parent); + + JavaAtomicComponent source = new JavaAtomicComponent("source", configuration, null); + OutboundWire outboundWire = createOutboundWire(new QualifiedName("target/Target"), Target.class); + outboundWire.setContainer(source); + source.addOutboundWire(outboundWire); + JavaAtomicComponent target = new JavaAtomicComponent("target", configuration, null); + InboundWire targetWire = MockFactory.createTargetWire("Target", Target.class); + targetWire.setContainer(target); + target.addInboundWire(targetWire); + InboundWire inboundWire = target.getInboundWire("Target"); + inboundWire.setContainer(target); + + EasyMock.expect(parent.getChild("target")).andReturn(target); + EasyMock.replay(parent); + + connector.connect(source); + target.start(); + return outboundWire; + } + + public static <T> OutboundWire createOutboundWire(QualifiedName targetName, Class<T> interfaze) + throws InvalidServiceContractException { + OutboundWire wire = new OutboundWireImpl(); + JavaServiceContract contract = new JavaServiceContract(interfaze); + wire.setServiceContract(contract); + wire.setTargetName(targetName); + wire.addInvocationChains(createInvocationChains(interfaze)); + return wire; + } + + private static Map<Operation<?>, OutboundInvocationChain> createInvocationChains(Class<?> interfaze) + throws InvalidServiceContractException { + Map<Operation<?>, OutboundInvocationChain> invocations = new HashMap<Operation<?>, OutboundInvocationChain>(); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract<?> contract = registry.introspect(interfaze); + for (Operation operation : contract.getOperations().values()) { + OutboundInvocationChain chain = new OutboundInvocationChainImpl(operation); + invocations.put(operation, chain); + } + return invocations; + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/ScopeReferenceTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/ScopeReferenceTestCase.java new file mode 100644 index 0000000000..87a8be3a01 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/integration/component/ScopeReferenceTestCase.java @@ -0,0 +1,746 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.integration.component; + +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.concurrent.FutureTask; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.WorkContext; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.event.CompositeStart; +import org.apache.tuscany.core.component.event.CompositeStop; +import org.apache.tuscany.core.component.event.HttpSessionEnd; +import org.apache.tuscany.core.component.event.HttpSessionStart; +import org.apache.tuscany.core.component.event.RequestEnd; +import org.apache.tuscany.core.component.event.RequestStart; +import org.apache.tuscany.core.component.scope.HttpSessionScopeContainer; +import org.apache.tuscany.core.component.scope.ModuleScopeContainer; +import org.apache.tuscany.core.component.scope.RequestScopeContainer; +import org.apache.tuscany.core.component.scope.StatelessScopeContainer; +import org.apache.tuscany.core.implementation.java.mock.MockFactory; +import org.apache.tuscany.core.implementation.java.mock.components.Source; +import org.apache.tuscany.core.implementation.java.mock.components.SourceImpl; +import org.apache.tuscany.core.implementation.java.mock.components.Target; +import org.apache.tuscany.core.implementation.java.mock.components.TargetImpl; +import org.apache.tuscany.core.util.JavaIntrospectionHelper; + +/** + * Tests scoping is properly handled for service references + * + * @version $Rev$ $Date$ + */ +public class ScopeReferenceTestCase extends TestCase { + + private Map<String, Member> members; + + /** + * Tests a module-to-module scoped wire + */ + public void testModuleToModule() throws Exception { + ScopeContainer scope = new ModuleScopeContainer(null); + scope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + scope, members, "target", Target.class, TargetImpl.class, scope); + scope.onEvent(new CompositeStart(this, null)); + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + scope.onEvent(new CompositeStop(this, null)); + scope.stop(); + } + + /** + * Tests a module-to-session scoped wire is setup properly by the runtime + */ + public void testModuleToSession() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ScopeContainer moduleScope = new ModuleScopeContainer(ctx); + moduleScope.start(); + ScopeContainer sessionScope = new HttpSessionScopeContainer(ctx); + sessionScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + moduleScope, members, "target", Target.class, TargetImpl.class, sessionScope); + moduleScope.onEvent(new CompositeStart(this, null)); + Object session1 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session1); + sessionScope.onEvent(new HttpSessionStart(this, session1)); + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + sessionScope.onEvent(new HttpSessionEnd(this, session1)); + + //second session + Object session2 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session2); + sessionScope.onEvent(new HttpSessionStart(this, session2)); + + Target target2 = (Target) targetComponent.getServiceInstance(); + assertFalse("foo".equals(target2.getString())); + + assertFalse("foo".equals(source.getTarget().getString())); + source.getTarget().setString("bar"); + assertEquals("bar", target2.getString()); + assertEquals("bar", source.getTarget().getString()); + sessionScope.onEvent(new HttpSessionEnd(this, session2)); + + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + moduleScope.onEvent(new CompositeStop(this, null)); + sessionScope.stop(); + moduleScope.stop(); + } + + /** + * Tests a module-to-request scoped wire + */ + public void testModuleToRequest() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ScopeContainer moduleScope = new ModuleScopeContainer(ctx); + moduleScope.start(); + final ScopeContainer requestScope = new RequestScopeContainer(ctx); + requestScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + moduleScope, members, "target", Target.class, TargetImpl.class, requestScope); + moduleScope.onEvent(new CompositeStart(this, null)); + requestScope.onEvent(new RequestStart(this)); + + AtomicComponent sourceComponent = contexts.get("source"); + final AtomicComponent targetComponent = contexts.get("target"); + final Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + + // spin off another request + Executor executor = Executors.newSingleThreadExecutor(); + FutureTask<Void> future = new FutureTask<Void>(new Runnable() { + public void run() { + requestScope.onEvent(new RequestStart(this)); + Target target2 = (Target) targetComponent.getServiceInstance(); + assertFalse("foo".equals(target2.getString())); + assertFalse("foo".equals(source.getTarget().getString())); + source.getTarget().setString("bar"); + assertEquals("bar", target2.getString()); + assertEquals("bar", source.getTarget().getString()); + requestScope.onEvent(new RequestEnd(this)); + } + }, null); + executor.execute(future); + future.get(); + assertEquals("foo", source.getTarget().getString()); + requestScope.onEvent(new RequestEnd(this)); + moduleScope.onEvent(new CompositeStop(this, null)); + requestScope.stop(); + moduleScope.stop(); + } + + /** + * Tests a module-to-stateless scoped wire is setup properly by the runtime + */ + public void testModuleToStateless() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ScopeContainer moduleScope = new ModuleScopeContainer(ctx); + moduleScope.start(); + ScopeContainer statelessScope = new StatelessScopeContainer(ctx); + statelessScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + moduleScope, members, "target", Target.class, TargetImpl.class, statelessScope); + moduleScope.onEvent(new CompositeStart(this, null)); + + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertFalse("foo".equals(source.getTarget().getString())); + Target target2 = (Target) targetComponent.getServiceInstance(); + assertFalse("foo".equals(target2.getString())); + source.getTarget().setString("bar"); + assertFalse("bar".equals(source.getTarget().getString())); + moduleScope.onEvent(new CompositeStop(this, null)); + moduleScope.stop(); + statelessScope.stop(); + } + + + /** + * Tests a session-to-session scoped wire + */ + public void testSessionToSession() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ScopeContainer sessionScope = new HttpSessionScopeContainer(ctx); + sessionScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + sessionScope, members, "target", Target.class, TargetImpl.class, sessionScope); + + Object session1 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session1); + sessionScope.onEvent(new HttpSessionStart(this, session1)); + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + source.getTarget().setString("foo"); + source.getTarget().setString("foo"); + assertEquals("foo", target.getString()); + + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + sessionScope.onEvent(new HttpSessionEnd(this, session1)); + + //second session + Object session2 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session2); + sessionScope.onEvent(new HttpSessionStart(this, session2)); + + Source source2 = (Source) sourceComponent.getServiceInstance(); + assertNotNull(source2); + Target target2 = (Target) targetComponent.getServiceInstance(); + + assertNotNull(target2); + assertNull(target2.getString()); + assertEquals(null, source2.getTarget().getString()); + source2.getTarget().setString("baz"); + assertEquals("baz", source2.getTarget().getString()); + assertEquals("baz", target2.getString()); + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + sessionScope.onEvent(new HttpSessionEnd(this, session2)); + sessionScope.stop(); + } + + + /** + * Tests a session-to-module scoped wire + */ + public void testSessionToModule() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ScopeContainer moduleScope = new ModuleScopeContainer(ctx); + moduleScope.start(); + ScopeContainer sessionScope = new HttpSessionScopeContainer(ctx); + sessionScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + sessionScope, members, "target", Target.class, TargetImpl.class, moduleScope); + moduleScope.onEvent(new CompositeStart(this, null)); + Object session1 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session1); + sessionScope.onEvent(new HttpSessionStart(this, session1)); + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + sessionScope.onEvent(new HttpSessionEnd(this, session1)); + + //second session + Object session2 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session2); + sessionScope.onEvent(new HttpSessionStart(this, session2)); + + Target target2 = (Target) targetComponent.getServiceInstance(); + Source source2 = (Source) sourceComponent.getServiceInstance(); + assertEquals("foo", target2.getString()); + assertEquals("foo", source2.getTarget().getString()); + source2.getTarget().setString("baz"); + assertEquals("baz", source2.getTarget().getString()); + assertEquals("baz", target2.getString()); + assertEquals("baz", target.getString()); + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + sessionScope.onEvent(new HttpSessionEnd(this, session2)); + moduleScope.stop(); + sessionScope.stop(); + } + + /** + * Tests a session-to-request scoped wire is setup properly by the runtime + */ + public void testSessionToRequest() throws Exception { + WorkContext ctx = new WorkContextImpl(); + final ScopeContainer requestScope = new RequestScopeContainer(ctx); + requestScope.start(); + ScopeContainer sessionScope = new HttpSessionScopeContainer(ctx); + sessionScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + sessionScope, members, "target", Target.class, TargetImpl.class, requestScope); + Object session1 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session1); + sessionScope.onEvent(new HttpSessionStart(this, session1)); + requestScope.onEvent(new RequestStart(this)); + AtomicComponent sourceComponent = contexts.get("source"); + final AtomicComponent targetComponent = contexts.get("target"); + final Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + + // spin off another request + Executor executor = Executors.newSingleThreadExecutor(); + FutureTask<Void> future = new FutureTask<Void>(new Runnable() { + public void run() { + requestScope.onEvent(new RequestStart(this)); + Target target2 = (Target) targetComponent.getServiceInstance(); + assertFalse("foo".equals(target2.getString())); + assertFalse("foo".equals(source.getTarget().getString())); + source.getTarget().setString("bar"); + assertEquals("bar", target2.getString()); + assertEquals("bar", source.getTarget().getString()); + requestScope.onEvent(new RequestEnd(this)); + } + }, null); + executor.execute(future); + future.get(); + assertEquals("foo", source.getTarget().getString()); + requestScope.onEvent(new RequestEnd(this)); + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + sessionScope.onEvent(new HttpSessionEnd(this, session1)); + requestScope.stop(); + sessionScope.stop(); + } + + + /** + * Tests a session-to-stateless scoped wire is setup properly by the runtime + */ + public void testSessionToStateless() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ScopeContainer sessionScope = new HttpSessionScopeContainer(ctx); + sessionScope.start(); + ScopeContainer statelessScope = new StatelessScopeContainer(ctx); + statelessScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + sessionScope, members, "target", Target.class, TargetImpl.class, statelessScope); + + Object session1 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session1); + sessionScope.onEvent(new HttpSessionStart(this, session1)); + + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertFalse("foo".equals(source.getTarget().getString())); + Target target2 = (Target) targetComponent.getServiceInstance(); + assertFalse("foo".equals(target2.getString())); + source.getTarget().setString("bar"); + assertFalse("bar".equals(source.getTarget().getString())); + + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + sessionScope.onEvent(new HttpSessionEnd(this, session1)); + sessionScope.stop(); + statelessScope.stop(); + } + + /** + * Tests a request-to-request scoped wire is setup properly by the runtime + */ + public void testRequestToRequest() throws Exception { + WorkContext ctx = new WorkContextImpl(); + final ScopeContainer requestScope = new RequestScopeContainer(ctx); + requestScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + requestScope, members, "target", Target.class, TargetImpl.class, requestScope); + requestScope.onEvent(new RequestStart(this)); + + final AtomicComponent sourceComponent = contexts.get("source"); + final AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + + // spin off another request + Executor executor = Executors.newSingleThreadExecutor(); + FutureTask<Void> future = new FutureTask<Void>(new Runnable() { + public void run() { + requestScope.onEvent(new RequestStart(this)); + Source source2 = (Source) sourceComponent.getServiceInstance(); + Target target2 = (Target) targetComponent.getServiceInstance(); + assertFalse("foo".equals(target2.getString())); + assertFalse("foo".equals(source2.getTarget().getString())); + source2.getTarget().setString("bar"); + assertEquals("bar", target2.getString()); + assertEquals("bar", source2.getTarget().getString()); + requestScope.onEvent(new RequestEnd(this)); + } + }, null); + executor.execute(future); + future.get(); + requestScope.onEvent(new RequestEnd(this)); + requestScope.stop(); + } + + /** + * Tests a request-to-module scoped wire + */ + public void testRequestToModule() throws Exception { + WorkContext ctx = new WorkContextImpl(); + final ScopeContainer requestScope = new RequestScopeContainer(ctx); + final ScopeContainer moduleScope = new ModuleScopeContainer(ctx); + requestScope.start(); + moduleScope.start(); + moduleScope.onEvent(new CompositeStart(this, null)); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + requestScope, members, "target", Target.class, TargetImpl.class, moduleScope); + requestScope.onEvent(new RequestStart(this)); + + final AtomicComponent sourceComponent = contexts.get("source"); + final AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + + // spin off another request + Executor executor = Executors.newSingleThreadExecutor(); + FutureTask<Void> future = new FutureTask<Void>(new Runnable() { + public void run() { + requestScope.onEvent(new RequestStart(this)); + Source source2 = (Source) sourceComponent.getServiceInstance(); + Target target2 = (Target) targetComponent.getServiceInstance(); + assertEquals("foo", target2.getString()); + assertEquals("foo", source2.getTarget().getString()); + source2.getTarget().setString("bar"); + assertEquals("bar", target2.getString()); + assertEquals("bar", source2.getTarget().getString()); + requestScope.onEvent(new RequestEnd(this)); + } + }, null); + executor.execute(future); + future.get(); + assertEquals("bar", target.getString()); + + requestScope.onEvent(new RequestEnd(this)); + requestScope.stop(); + moduleScope.onEvent(new CompositeStop(this, null)); + moduleScope.stop(); + } + + /** + * Tests a request-to-session scoped wire is setup properly by the runtime + */ + public void testRequestToSession() throws Exception { + WorkContext ctx = new WorkContextImpl(); + final ScopeContainer requestScope = new RequestScopeContainer(ctx); + final ScopeContainer sessionScope = new HttpSessionScopeContainer(ctx); + requestScope.start(); + sessionScope.start(); + + Object session1 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session1); + sessionScope.onEvent(new HttpSessionStart(this, session1)); + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + requestScope, members, "target", Target.class, TargetImpl.class, sessionScope); + + final AtomicComponent sourceComponent = contexts.get("source"); + final AtomicComponent targetComponent = contexts.get("target"); + requestScope.onEvent(new RequestStart(this)); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + + // spin off another request + Executor executor = Executors.newSingleThreadExecutor(); + FutureTask<Void> future = new FutureTask<Void>(new Runnable() { + public void run() { + requestScope.onEvent(new RequestStart(this)); + Source source2 = (Source) sourceComponent.getServiceInstance(); + Target target2 = (Target) targetComponent.getServiceInstance(); + assertEquals("foo", target2.getString()); + assertEquals("foo", source2.getTarget().getString()); + source2.getTarget().setString("bar"); + assertEquals("bar", target2.getString()); + assertEquals("bar", source2.getTarget().getString()); + requestScope.onEvent(new RequestEnd(this)); + } + }, null); + executor.execute(future); + future.get(); + assertEquals("bar", target.getString()); + + requestScope.onEvent(new RequestEnd(this)); + requestScope.stop(); + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + sessionScope.onEvent(new HttpSessionEnd(this, session1)); + sessionScope.stop(); + } + + + /** + * Tests a request-to-stateless scoped wire is setup properly by the runtime + */ + public void testRequestToStateless() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ScopeContainer requestScope = new RequestScopeContainer(ctx); + requestScope.start(); + ScopeContainer statelessScope = new StatelessScopeContainer(ctx); + statelessScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + requestScope, members, "target", Target.class, TargetImpl.class, statelessScope); + + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + requestScope.onEvent(new RequestStart(this)); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertFalse("foo".equals(source.getTarget().getString())); + Target target2 = (Target) targetComponent.getServiceInstance(); + assertFalse("foo".equals(target2.getString())); + source.getTarget().setString("bar"); + assertFalse("bar".equals(source.getTarget().getString())); + requestScope.onEvent(new RequestEnd(this)); + requestScope.stop(); + statelessScope.stop(); + } + + + /** + * Tests a stateless-to-stateless scoped wire is setup properly by the runtime + */ + public void testStatelessToStateless() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ScopeContainer statelessScope = new StatelessScopeContainer(ctx); + statelessScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + statelessScope, members, "target", Target.class, TargetImpl.class, statelessScope); + + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertFalse("foo".equals(source.getTarget().getString())); + Target target2 = (Target) targetComponent.getServiceInstance(); + assertFalse("foo".equals(target2.getString())); + source.getTarget().setString("bar"); + assertFalse("bar".equals(source.getTarget().getString())); + statelessScope.stop(); + } + + /** + * Tests a stateless-to-request scoped wire is setup properly by the runtime + */ + public void testStatelessToRequest() throws Exception { + WorkContext ctx = new WorkContextImpl(); + final ScopeContainer requestScope = new RequestScopeContainer(ctx); + requestScope.start(); + ScopeContainer statelessScope = new StatelessScopeContainer(ctx); + statelessScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + statelessScope, members, "target", Target.class, TargetImpl.class, requestScope); + requestScope.onEvent(new RequestStart(this)); + AtomicComponent sourceComponent = contexts.get("source"); + final AtomicComponent targetComponent = contexts.get("target"); + final Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + + // spin off another request + Executor executor = Executors.newSingleThreadExecutor(); + FutureTask<Void> future = new FutureTask<Void>(new Runnable() { + public void run() { + requestScope.onEvent(new RequestStart(this)); + Target target2 = (Target) targetComponent.getServiceInstance(); + assertFalse("foo".equals(target2.getString())); + assertFalse("foo".equals(source.getTarget().getString())); + source.getTarget().setString("bar"); + assertEquals("bar", target2.getString()); + assertEquals("bar", source.getTarget().getString()); + requestScope.onEvent(new RequestEnd(this)); + } + }, null); + executor.execute(future); + future.get(); + requestScope.stop(); + statelessScope.stop(); + } + + /** + * Tests a stateless-to-session scoped wire is setup properly by the runtime + */ + public void testStatelessToSession() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ScopeContainer statelessScope = new StatelessScopeContainer(ctx); + statelessScope.start(); + ScopeContainer sessionScope = new HttpSessionScopeContainer(ctx); + sessionScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + statelessScope, members, "target", Target.class, TargetImpl.class, sessionScope); + Object session1 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session1); + sessionScope.onEvent(new HttpSessionStart(this, session1)); + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + sessionScope.onEvent(new HttpSessionEnd(this, session1)); + + //second session + Object session2 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session2); + sessionScope.onEvent(new HttpSessionStart(this, session2)); + + Target target2 = (Target) targetComponent.getServiceInstance(); + assertFalse("foo".equals(target2.getString())); + + assertFalse("foo".equals(source.getTarget().getString())); + source.getTarget().setString("bar"); + assertEquals("bar", target2.getString()); + assertEquals("bar", source.getTarget().getString()); + sessionScope.onEvent(new HttpSessionEnd(this, session2)); + + ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER); + sessionScope.stop(); + statelessScope.stop(); + } + + + /** + * Tests a stateless-to-module scoped wire is setup properly by the runtime + */ + public void testStatelessToModule() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ScopeContainer statelessScope = new StatelessScopeContainer(ctx); + statelessScope.start(); + ScopeContainer moduleScope = new ModuleScopeContainer(ctx); + moduleScope.start(); + + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class, + statelessScope, members, "target", Target.class, TargetImpl.class, moduleScope); + moduleScope.onEvent(new CompositeStart(this, null)); + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + Source source = (Source) sourceComponent.getServiceInstance(); + Target target = (Target) targetComponent.getServiceInstance(); + assertNull(source.getTarget().getString()); + assertNull(target.getString()); + target.setString("foo"); + assertTrue(Proxy.isProxyClass(source.getTarget().getClass())); + assertEquals("foo", source.getTarget().getString()); + + //second session + Object session2 = new Object(); + ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session2); + moduleScope.onEvent(new HttpSessionStart(this, session2)); + + Target target2 = (Target) targetComponent.getServiceInstance(); + assertEquals("foo", target2.getString()); + + assertEquals("foo", source.getTarget().getString()); + source.getTarget().setString("bar"); + assertEquals("bar", target2.getString()); + assertEquals("bar", source.getTarget().getString()); + + moduleScope.onEvent(new CompositeStop(this, null)); + moduleScope.stop(); + statelessScope.stop(); + } + + protected void setUp() throws Exception { + super.setUp(); + members = new HashMap<String, Member>(); + Method[] methods = SourceImpl.class.getMethods(); + for (Method method : methods) { + if (method.getName().startsWith("set")) { + members.put(JavaIntrospectionHelper.toPropertyName(method.getName()), method); + } + } + } + + +} + diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/MockFactory.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/MockFactory.java new file mode 100644 index 0000000000..3c2093eee7 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/MockFactory.java @@ -0,0 +1,381 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.mock; + +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.spi.builder.BuilderConfigException; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.idl.InvalidServiceContractException; +import org.apache.tuscany.spi.idl.java.JavaIDLUtils; +import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundInvocationChain; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.Interceptor; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.WireService; + +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.java.JavaAtomicComponent; +import org.apache.tuscany.core.implementation.java.JavaTargetInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.apache.tuscany.core.wire.InboundInvocationChainImpl; +import org.apache.tuscany.core.wire.InboundWireImpl; +import org.apache.tuscany.core.wire.InvokerInterceptor; +import org.apache.tuscany.core.wire.OutboundInvocationChainImpl; +import org.apache.tuscany.core.wire.OutboundWireImpl; +import org.apache.tuscany.core.wire.jdk.JDKWireService; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; + +/** + * @version $$Rev$$ $$Date$$ + */ +public final class MockFactory { + + private static final WireService WIRE_SERVICE = new JDKWireService(new WorkContextImpl(), null); + private static final JavaInterfaceProcessorRegistry REGISTRY = new JavaInterfaceProcessorRegistryImpl(); + + private MockFactory() { + } + + /** + * Creates a JavaAtomicComponent which returns the given instance + */ + @SuppressWarnings("unchecked") + public static <T> JavaAtomicComponent createJavaComponent(T instance) { + ScopeContainer scope = createMock(ScopeContainer.class); + scope.getScope(); + expectLastCall().andReturn(Scope.MODULE); + scope.getInstance(isA(JavaAtomicComponent.class)); + expectLastCall().andReturn(instance).anyTimes(); + replay(scope); + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setScopeContainer(scope); + try { + configuration.setInstanceFactory(new PojoObjectFactory(DummyImpl.class.getConstructor())); + } catch (NoSuchMethodException e) { + throw new AssertionError(e); + } + configuration.addServiceInterface(DummyImpl.class); + configuration.setWireService(WIRE_SERVICE); + return new JavaAtomicComponent(instance.getClass().getName(), configuration, null); + } + + @SuppressWarnings("unchecked") + public static <T> JavaAtomicComponent createJavaComponent(String name, ScopeContainer scope, Class<T> clazz) + throws NoSuchMethodException { + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setScopeContainer(scope); + configuration.setInstanceFactory(new PojoObjectFactory(clazz.getConstructor())); + configuration.addServiceInterface(clazz); + configuration.setWireService(WIRE_SERVICE); + return new JavaAtomicComponent(name, configuration, null); + + } + + /** + * Wires two contexts together where the reference interface is the same as target service + * + * @param sourceName + * @param sourceClass + * @param sourceScope + * @param members + * @param targetName + * @param targetService + * @param targetClass + * @param targetScope + * @return + * @throws Exception + */ + public static Map<String, AtomicComponent> createWiredComponents(String sourceName, + Class<?> sourceClass, + ScopeContainer sourceScope, + Map<String, Member> members, + String targetName, + Class<?> targetService, + Class<?> targetClass, + ScopeContainer targetScope) throws Exception { + return createWiredComponents(sourceName, sourceClass, targetService, sourceScope, members, targetName, + targetService, targetClass, targetScope); + + } + + /** + * Wires two contexts together where the reference interface may be different from the target service + */ + public static Map<String, AtomicComponent> createWiredComponents(String sourceName, Class<?> sourceClass, + Class<?> sourceReferenceClass, + ScopeContainer sourceScope, + Map<String, Member> members, + String targetName, + Class<?> targetService, + Class<?> targetClass, + ScopeContainer targetScope) throws Exception { + return createWiredComponents(sourceName, + sourceClass, + sourceReferenceClass, + sourceScope, + null, + members, + targetName, + targetService, + targetClass, + targetScope, + null); + } + + @SuppressWarnings("unchecked") + public static Map<String, AtomicComponent> createWiredComponents(String sourceName, Class<?> sourceClass, + Class<?> sourceReferenceClass, + ScopeContainer sourceScope, + Interceptor sourceHeadInterceptor, + Map<String, Member> members, + String targetName, Class<?> targetService, + Class<?> targetClass, + ScopeContainer targetScope, + Interceptor targetHeadInterceptor) + throws Exception { + + JavaAtomicComponent targetContext = + createJavaComponent(targetName, targetScope, targetClass); + String serviceName = targetService.getName().substring(targetService.getName().lastIndexOf('.') + 1); + InboundWire inboundWire = createServiceWire(serviceName, targetService, targetHeadInterceptor); + targetContext.addInboundWire(inboundWire); + + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setScopeContainer(sourceScope); + configuration.setInstanceFactory(new PojoObjectFactory(sourceClass.getConstructor())); + configuration.addServiceInterface(sourceClass); + configuration.setWireService(WIRE_SERVICE); + for (Map.Entry<String, Member> entry : members.entrySet()) { + configuration.addReferenceSite(entry.getKey(), entry.getValue()); + } + JavaAtomicComponent sourceContext = new JavaAtomicComponent(sourceName, configuration, null); + OutboundWire outboundWire = createReferenceWire(targetName, sourceReferenceClass, sourceHeadInterceptor); + sourceContext.addOutboundWire(outboundWire); + targetScope.register(targetContext); + sourceScope.register(sourceContext); + connect(outboundWire, inboundWire, targetContext, false); + Map<String, AtomicComponent> contexts = new HashMap<String, AtomicComponent>(); + contexts.put(sourceName, sourceContext); + contexts.put(targetName, targetContext); + return contexts; + } + + + /** + * Wires two contexts using a multiplicity reference + * + * @param sourceName + * @param sourceClass + * @param sourceReferenceClass + * @param sourceScope + * @param targetName + * @param targetService + * @param targetClass + * @param members + * @param targetScope + * @return + * @throws Exception + */ + @SuppressWarnings("unchecked") + public static Map<String, AtomicComponent> createWiredMultiplicity(String sourceName, Class<?> sourceClass, + Class<?> sourceReferenceClass, + ScopeContainer sourceScope, + String targetName, Class<?> targetService, + Class<?> targetClass, + Map<String, Member> members, + ScopeContainer targetScope) throws Exception { + JavaAtomicComponent targetContext = + createJavaComponent(targetName, targetScope, targetClass); + String serviceName = targetService.getName().substring(targetService.getName().lastIndexOf('.') + 1); + InboundWire inboundWire = createServiceWire(serviceName, targetService, null); + targetContext.addInboundWire(inboundWire); + + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setScopeContainer(sourceScope); + configuration.setInstanceFactory(new PojoObjectFactory(sourceClass.getConstructor())); + configuration.addServiceInterface(sourceClass); + configuration.setWireService(WIRE_SERVICE); + for (Map.Entry<String, Member> entry : members.entrySet()) { + configuration.addReferenceSite(entry.getKey(), entry.getValue()); + } + JavaAtomicComponent sourceContext = new JavaAtomicComponent(sourceName, configuration, null); + OutboundWire outboundWire = createReferenceWire(targetName, sourceReferenceClass, null); + List<OutboundWire> factories = new ArrayList<OutboundWire>(); + factories.add(outboundWire); + sourceContext.addOutboundWires(sourceReferenceClass, factories); + targetScope.register(targetContext); + sourceScope.register(sourceContext); + connect(outboundWire, inboundWire, targetContext, false); + Map<String, AtomicComponent> contexts = new HashMap<String, AtomicComponent>(); + contexts.put(sourceName, sourceContext); + contexts.put(targetName, targetContext); + return contexts; + } + + public static <T> InboundWire createTargetWire(String serviceName, Class<T> interfaze) + throws InvalidServiceContractException { + return createServiceWire(serviceName, interfaze, null); + } + + + public static <T> InboundWire createServiceWire(String serviceName, Class<T> interfaze, Interceptor interceptor) + throws InvalidServiceContractException { + InboundWire wire = new InboundWireImpl(); + ServiceContract<?> contract = REGISTRY.introspect(interfaze); + wire.setServiceContract(contract); + wire.setServiceName(serviceName); + wire.addInvocationChains( + createInboundChains(interfaze, interceptor)); + return wire; + } + + public static <T> OutboundWire createReferenceWire(String refName, Class<T> interfaze, Interceptor interceptor) + throws InvalidServiceContractException { + + OutboundWire wire = new OutboundWireImpl(); + wire.setReferenceName(refName); + Map<Operation<?>, OutboundInvocationChain> outboundChains = createOutboundChains(interfaze, interceptor); + wire.addInvocationChains(outboundChains); + ServiceContract<?> contract = REGISTRY.introspect(interfaze); + wire.setServiceContract(contract); + return wire; + } + + public static <T> OutboundWire createReferenceWire(String refName, Class<T> interfaze) + throws InvalidServiceContractException { + OutboundWire wire = new OutboundWireImpl(); + wire.setReferenceName(refName); + wire.addInvocationChains(createOutboundChains(interfaze)); + ServiceContract<?> contract = REGISTRY.introspect(interfaze); + wire.setServiceContract(contract); + return wire; + } + + + /** + * @param outboundWire + * @param inboundWire + * @param targetContext + * @param cacheable + * @throws Exception + */ + public static void connect(OutboundWire outboundWire, + InboundWire inboundWire, + JavaAtomicComponent targetContext, + boolean cacheable) throws Exception { + if (inboundWire != null) { + // if null, the target side has no interceptors or handlers + Map<Operation<?>, InboundInvocationChain> targetInvocationConfigs = inboundWire.getInvocationChains(); + for (OutboundInvocationChain outboundInvocationConfig : outboundWire.getInvocationChains().values()) { + // match wire chains + InboundInvocationChain inboundInvocationConfig = + targetInvocationConfigs.get(outboundInvocationConfig.getOperation()); + if (inboundInvocationConfig == null) { + BuilderConfigException e = + new BuilderConfigException("Incompatible source and target interface types for reference"); + e.setIdentifier(outboundWire.getReferenceName()); + throw e; + } + if (inboundInvocationConfig.getHeadInterceptor() == null) { + BuilderConfigException e = + new BuilderConfigException("No target handler or interceptor for operation"); + e.setIdentifier(inboundInvocationConfig.getOperation().getName()); + throw e; + } + if (!(outboundInvocationConfig.getTailInterceptor() instanceof InvokerInterceptor + && inboundInvocationConfig.getHeadInterceptor() instanceof InvokerInterceptor)) { + // check that we do not have the case where the only interceptors are invokers since we just + // need one + outboundInvocationConfig.setTargetInterceptor(inboundInvocationConfig.getHeadInterceptor()); + } + } + + for (OutboundInvocationChain chain : outboundWire.getInvocationChains().values()) { + //FIXME should use target method, not outboundInvocationConfig.getMethod() + Method[] methods = outboundWire.getServiceContract().getInterfaceClass().getMethods(); + Method m = JavaIDLUtils.findMethod(chain.getOperation(), methods); + TargetInvoker invoker = new JavaTargetInvoker(m, targetContext); + invoker.setCacheable(cacheable); + chain.setTargetInvoker(invoker); + } + } + } + + private static Map<Operation<?>, OutboundInvocationChain> createOutboundChains(Class<?> interfaze) + throws InvalidServiceContractException { + return createOutboundChains(interfaze, null); + } + + private static Map<Operation<?>, OutboundInvocationChain> createOutboundChains(Class<?> interfaze, + Interceptor interceptor) + throws InvalidServiceContractException { + Map<Operation<?>, OutboundInvocationChain> invocations = new HashMap<Operation<?>, OutboundInvocationChain>(); + ServiceContract<?> contract = REGISTRY.introspect(interfaze); + for (Operation<?> operation : contract.getOperations().values()) { + OutboundInvocationChain chain = new OutboundInvocationChainImpl(operation); + if (interceptor != null) { + chain.addInterceptor(interceptor); + } + invocations.put(operation, chain); + } + return invocations; + } + + private static Map<Operation<?>, InboundInvocationChain> createInboundChains(Class<?> interfaze, + Interceptor interceptor) + throws InvalidServiceContractException { + + Map<Operation<?>, InboundInvocationChain> invocations = new HashMap<Operation<?>, InboundInvocationChain>(); + ServiceContract<?> contract = REGISTRY.introspect(interfaze); + for (Operation<?> method : contract.getOperations().values()) { + InboundInvocationChain chain = new InboundInvocationChainImpl(method); + if (interceptor != null) { + chain.addInterceptor(interceptor); + } + // add tail interceptor + chain.addInterceptor(new InvokerInterceptor()); + invocations.put(method, chain); + } + return invocations; + } + + private static class DummyImpl { + public DummyImpl() { + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/AsyncTarget.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/AsyncTarget.java new file mode 100644 index 0000000000..1abc9781e3 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/AsyncTarget.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.mock.components; + +import org.osoa.sca.annotations.OneWay; + +/** + * @version $Rev$ $Date$ + */ +public interface AsyncTarget { + @OneWay + void invoke(); + + int getCount(); +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/OtherTarget.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/OtherTarget.java new file mode 100644 index 0000000000..87b1fd87fa --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/OtherTarget.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.mock.components; + +/** + * Implementations are used in wiring tests + * + * @version $Rev$ $Date$ + */ +public interface OtherTarget { + + String getString(); + + void setString(String val); +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/OtherTargetImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/OtherTargetImpl.java new file mode 100644 index 0000000000..b32fc7547e --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/OtherTargetImpl.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.mock.components; + +/** + * A target used for testing wires with a different source and target interface + * + * @version $Rev$ $Date$ + */ +public class OtherTargetImpl implements OtherTarget { + + private String theString; + + public String getString() { + return theString; + } + + public void setString(String val) { + theString = val; + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/Source.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/Source.java new file mode 100644 index 0000000000..bffacc3c20 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/Source.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.mock.components; + +import java.util.List; + + +/** + * Implementations are used in wiring tests + * + * @version $Rev$ $Date$ + */ +public interface Source { + + Target getTarget(); + + List<Target> getTargets(); + + List<Target> getTargetsThroughField(); + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/SourceImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/SourceImpl.java new file mode 100644 index 0000000000..bf9e30b306 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/SourceImpl.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.mock.components; + +import java.util.List; + + +/** + * Mock system component implementation used in wiring tests + * + * @version $Rev$ $Date$ + */ +public class SourceImpl implements Source { + + private Target target; + private List<Target> targets; + private Target[] targetsArray; + private List<Target> targetsThroughField; + + public void setTarget(Target target) { + this.target = target; + } + + public Target getTarget() { + return target; + } + + public List<Target> getTargets() { + return targets; + } + + public void setTargets(List<Target> targets) { + this.targets = targets; + } + + public List<Target> getTargetsThroughField() { + return targetsThroughField; + } + + + public Target[] getArrayOfTargets() { + return targetsArray; + } + + public void setArrayOfTargets(Target[] targets) { + targetsArray = targets; + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/Target.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/Target.java new file mode 100644 index 0000000000..cafebc0801 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/Target.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.mock.components; + +/** + * Implementations are used in wiring tests + * + * @version $Rev$ $Date$ + */ +public interface Target { + + String getString(); + + void setString(String val); +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/TargetImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/TargetImpl.java new file mode 100644 index 0000000000..060ca292a9 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/mock/components/TargetImpl.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.java.mock.components; + +/** + * Mock system component implementation used in wiring tests + * + * @version $Rev$ $Date$ + */ +public class TargetImpl implements Target { + + private String theString; + + public String getString() { + return theString; + } + + public void setString(String val) { + theString = val; + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorAutowireTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorAutowireTestCase.java new file mode 100644 index 0000000000..c67d226b41 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorAutowireTestCase.java @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Constructor; +import java.util.List; + +import org.osoa.sca.annotations.Property; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorAutowireTestCase extends TestCase { + + ConstructorProcessor processor = + new ConstructorProcessor(new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl())); + + public void testAutowire() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor = Foo.class.getConstructor(Bar.class); + processor.visitConstructor(null, ctor, type, null); + assertNotNull(type.getReferences().get("myRef")); + } + + public void testNamesOnConstructor() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor = Foo.class.getConstructor(Bar.class, Bar.class); + processor.visitConstructor(null, ctor, type, null); + assertEquals(2, type.getConstructorDefinition().getInjectionNames().size()); + assertNotNull(type.getReferences().get("myRef1")); + assertNotNull(type.getReferences().get("myRef2")); + } + + public void testNoName() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo2> ctor = Foo2.class.getConstructor(Bar.class); + processor.visitConstructor(null, ctor, type, null); + assertNotNull(type.getReferences().get(Bar.class.getName() + "0")); + } + + public void testInvalidNumberOfNames() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(Bar.class, Bar.class); + try { + processor.visitConstructor(null, ctor, type, null); + fail(); + } catch (InvalidAutowireException e) { + // expected + } + } + + public void testNoMatchingNames() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(List.class, List.class); + try { + processor.visitConstructor(null, ctor, type, null); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + /** + * Verifies processing executes with additional extension annotations + */ + public void testRandomAnnotation() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo3> ctor = Foo3.class.getConstructor(String.class, String.class); + processor.visitConstructor(null, ctor, type, null); + assertEquals(1, type.getProperties().size()); + assertNotNull(type.getProperties().get("prop1")); + } + + private static interface Bar { + + } + + private static class Foo { + + @org.osoa.sca.annotations.Constructor() + public Foo(@Autowire(name = "myRef") Bar ref) { + + } + + @org.osoa.sca.annotations.Constructor({"myRef1", "myRef2"}) + public Foo(@Autowire Bar ref1, @Autowire Bar ref2) { + + } + + } + + private static class Foo2 { + @org.osoa.sca.annotations.Constructor() + public Foo2(@Autowire Bar ref) { + + } + } + + private static class BadFoo { + + @org.osoa.sca.annotations.Constructor({"ref1"}) + public BadFoo(@Autowire Bar ref1, @Autowire Bar ref2) { + + } + + @org.osoa.sca.annotations.Constructor({"myRef", "myRef2"}) + public BadFoo(@Autowire List ref, @Autowire(name = "myOtherRef") List ref2) { + + } + + } + + public static class Foo3 { + + @org.osoa.sca.annotations.Constructor + public Foo3(@Property(name = "prop1") String prop, @Baz String baz) { + } + } + + public @interface Baz { + + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorExtensibilityTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorExtensibilityTestCase.java new file mode 100644 index 0000000000..25a8ab9a1a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorExtensibilityTestCase.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Constructor; + +import org.osoa.sca.annotations.Property; + +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * Verifies the constructor processor works when parameters are marked with custom extension annotations + * + * @version $Rev$ $Date$ + */ +public class ConstructorProcessorExtensibilityTestCase extends TestCase { + private ConstructorProcessor processor = + new ConstructorProcessor(new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl())); + + public void testProcessFirst() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor1 = Foo.class.getConstructor(String.class, String.class); + processor.visitConstructor(null, ctor1, type, null); + assertEquals("foo", type.getConstructorDefinition().getInjectionNames().get(0)); + } + + /** + * Verifies the constructor processor can be called after another processor has evaluated the constructor and found + * an annotation + * + * @throws Exception + */ + public void testProcessLast() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor1 = Foo.class.getConstructor(String.class, String.class); + ConstructorDefinition<Foo> definition = new ConstructorDefinition<Foo>(ctor1); + definition.getInjectionNames().add(""); + definition.getInjectionNames().add("mybar"); + type.setConstructorDefinition(definition); + processor.visitConstructor(null, ctor1, type, null); + assertEquals("foo", type.getConstructorDefinition().getInjectionNames().get(0)); + } + + + private @interface Bar { + + } + + private static class Foo { + @org.osoa.sca.annotations.Constructor + public Foo(@Property(name = "foo") String foo, @Bar String bar) { + + } + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java new file mode 100644 index 0000000000..364bfcc274 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Constructor; + +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorProcessorTestCase extends TestCase { + private ConstructorProcessor processor = + new ConstructorProcessor(new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl())); + + public void testDuplicateConstructor() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + try { + processor.visitClass(null, BadFoo.class, type, null); + fail(); + } catch (DuplicateConstructorException e) { + // expected + } + } + + public void testConstructorAnnotation() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor1 = Foo.class.getConstructor(String.class); + processor.visitConstructor(null, ctor1, type, null); + assertEquals("foo", type.getConstructorDefinition().getInjectionNames().get(0)); + } + + public void testNoAnnotation() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<NoAnnotation> ctor1 = NoAnnotation.class.getConstructor(); + processor.visitConstructor(null, ctor1, type, null); + assertNull(type.getConstructorDefinition()); + } + + public void testBadAnnotation() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadAnnotation> ctor1 = BadAnnotation.class.getConstructor(String.class, Foo.class); + try { + processor.visitConstructor(null, ctor1, type, null); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + public void testMixedParameters() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Mixed> ctor1 = Mixed.class.getConstructor(String.class, String.class, String.class); + processor.visitConstructor(null, ctor1, type, null); + assertEquals("java.lang.String0", type.getConstructorDefinition().getInjectionNames().get(0)); + assertEquals("foo", type.getConstructorDefinition().getInjectionNames().get(1)); + assertEquals("bar", type.getConstructorDefinition().getInjectionNames().get(2)); + } + + public void testAllAutowireWithNoNames() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<AllAutowireNoName> ctor1 = + AllAutowireNoName.class.getConstructor(String.class, String.class, String.class); + processor.visitConstructor(null, ctor1, type, null); + assertEquals("java.lang.String0", type.getConstructorDefinition().getInjectionNames().get(0)); + assertEquals("java.lang.String1", type.getConstructorDefinition().getInjectionNames().get(1)); + assertEquals("java.lang.String2", type.getConstructorDefinition().getInjectionNames().get(2)); + } + + private static class BadFoo { + + @org.osoa.sca.annotations.Constructor("foo") + public BadFoo(String foo) { + + } + + @org.osoa.sca.annotations.Constructor({"foo", "bar"}) + public BadFoo(String foo, String bar) { + + } + } + + private static class Foo { + @org.osoa.sca.annotations.Constructor("foo") + public Foo(String foo) { + + } + } + + private static class NoAnnotation { + public NoAnnotation() { + } + } + + private static class BadAnnotation { + @org.osoa.sca.annotations.Constructor("foo") + public BadAnnotation(String foo, Foo ref) { + } + } + + + public static final class Mixed { + @org.osoa.sca.annotations.Constructor + public Mixed(@Autowire String param1, + @Property(name = "foo") String param2, + @Reference(name = "bar") String param3) { + } + } + + public static final class AllAutowireNoName { + @org.osoa.sca.annotations.Constructor + public AllAutowireNoName(@Autowire String param1, @Autowire String param2, @Autowire String param3) { + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorPropertyTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorPropertyTestCase.java new file mode 100644 index 0000000000..ee461aead7 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorPropertyTestCase.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Constructor; +import java.util.List; + +import org.osoa.sca.annotations.Property; + +import org.apache.tuscany.spi.implementation.java.DuplicatePropertyException; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.OverrideOptions; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorPropertyTestCase extends TestCase { + + ConstructorProcessor processor = + new ConstructorProcessor(new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl())); + + public void testProperty() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class); + processor.visitConstructor(null, ctor, type, null); + JavaMappedProperty<?> property = type.getProperties().get("myProp"); + //assertTrue(property.isRequired()); + assertEquals(property.getOverride(), OverrideOptions.MAY); + assertEquals("myProp", property.getName()); + } + + public void testTwoPropertiesSameType() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + processor.visitConstructor(null, ctor, type, null); + assertNotNull(type.getProperties().get("myProp1")); + assertNotNull(type.getProperties().get("myProp2")); + } + + public void testDuplicateProperty() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class, String.class); + try { + processor.visitConstructor(null, ctor, type, null); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + public void testNoName() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class); + try { + processor.visitConstructor(null, ctor, type, null); + fail(); + } catch (InvalidPropertyException e) { + // expected + } + } + + public void testNamesOnConstructor() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor = Foo.class.getConstructor(Integer.class); + processor.visitConstructor(null, ctor, type, null); + assertNotNull(type.getProperties().get("myProp")); + } + + public void testInvalidNumberOfNames() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(Integer.class, Integer.class); + try { + processor.visitConstructor(null, ctor, type, null); + fail(); + } catch (InvalidPropertyException e) { + // expected + } + } + + public void testNoMatchingNames() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(List.class, List.class); + try { + processor.visitConstructor(null, ctor, type, null); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + +// public void testMultiplicityRequired() throws Exception { + // TODO multiplicity +// } + + private static class Foo { + + @org.osoa.sca.annotations.Constructor() + public Foo(@Property(name = "myProp", override = "may") String prop) { + + } + + @org.osoa.sca.annotations.Constructor("myProp") + public Foo(@Property Integer prop) { + + } + + @org.osoa.sca.annotations.Constructor() + public Foo(@Property(name = "myProp1") String prop1, @Property(name = "myProp2") String prop2) { + + } + + @org.osoa.sca.annotations.Constructor() + public Foo(@Property List prop) { + + } + } + + private static class BadFoo { + + @org.osoa.sca.annotations.Constructor() + public BadFoo(@Property(name = "myProp") String prop1, @Property(name = "myProp") String prop2) { + + } + + @org.osoa.sca.annotations.Constructor() + public BadFoo(@Property String prop) { + + } + + @org.osoa.sca.annotations.Constructor("myProp") + public BadFoo(@Property Integer prop, @Property Integer prop2) { + + } + + @org.osoa.sca.annotations.Constructor({"myRef", "myRef2"}) + public BadFoo(@Property List ref, @Property(name = "myOtherRef") List ref2) { + + } + + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorReferenceTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorReferenceTestCase.java new file mode 100644 index 0000000000..687c95e860 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorReferenceTestCase.java @@ -0,0 +1,173 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Constructor; +import java.util.List; + +import org.osoa.sca.annotations.Reference; + +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorReferenceTestCase extends TestCase { + + ConstructorProcessor processor; + + public void testReference() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class); + processor.visitConstructor(null, ctor, type, null); + JavaMappedReference reference = type.getReferences().get("myRef"); + assertTrue(reference.isRequired()); + assertEquals("myRef", reference.getName()); + } + + public void testTwoReferencesSameType() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + processor.visitConstructor(null, ctor, type, null); + assertNotNull(type.getReferences().get("myRef1")); + assertNotNull(type.getReferences().get("myRef2")); + } + + public void testDuplicateProperty() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class, String.class); + try { + processor.visitConstructor(null, ctor, type, null); + fail(); + } catch (DuplicateReferenceException e) { + // expected + } + } + + public void testNoName() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class); + try { + processor.visitConstructor(null, ctor, type, null); + fail(); + } catch (InvalidReferenceException e) { + // expected + } + } + + public void testNamesOnConstructor() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor = Foo.class.getConstructor(Integer.class); + processor.visitConstructor(null, ctor, type, null); + assertNotNull(type.getReferences().get("myRef")); + } + + public void testInvalidNumberOfNames() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(Integer.class, Integer.class); + try { + processor.visitConstructor(null, ctor, type, null); + fail(); + } catch (InvalidReferenceException e) { + // expected + } + } + + public void testNoMatchingNames() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(List.class, List.class); + try { + processor.visitConstructor(null, ctor, type, null); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + protected void setUp() throws Exception { + super.setUp(); + processor = + new ConstructorProcessor(new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl())); + } + +// public void testMultiplicityRequired() throws Exception { + // TODO multiplicity +// } + + private static class Foo { + + @org.osoa.sca.annotations.Constructor() + public Foo(@Reference(name = "myRef", required = true) String prop) { + + } + + @org.osoa.sca.annotations.Constructor() + public Foo(@Reference(name = "myRef1") String prop1, @Reference(name = "myRef2") String prop2) { + + } + + @org.osoa.sca.annotations.Constructor("myRef") + public Foo(@Reference Integer prop) { + + } + + @org.osoa.sca.annotations.Constructor() + public Foo(@Reference List prop) { + + } + } + + private static class BadFoo { + + @org.osoa.sca.annotations.Constructor + public BadFoo(@Reference(name = "myRef") String prop1, @Reference(name = "myRef") String prop2) { + + } + + @org.osoa.sca.annotations.Constructor + public BadFoo(@Reference String prop) { + + } + + @org.osoa.sca.annotations.Constructor("myRef") + public BadFoo(@Reference Integer ref, @Reference Integer ref2) { + + } + + @org.osoa.sca.annotations.Constructor({"myRef", "myRef2"}) + public BadFoo(@Reference List ref, @Reference(name = "myOtherRef") List ref2) { + + } + + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/DestroyProcessorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/DestroyProcessorTestCase.java new file mode 100644 index 0000000000..6929250298 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/DestroyProcessorTestCase.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Method; + +import org.osoa.sca.annotations.Destroy; + +import org.apache.tuscany.spi.implementation.java.JavaMappedService; + +import junit.framework.TestCase; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; + +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; + +/** + * @version $Rev$ $Date$ + */ +public class DestroyProcessorTestCase extends TestCase { + + public void testDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Method method = Foo.class.getMethod("destroy"); + processor.visitMethod(null, method, type, null); + assertNotNull(type.getDestroyMethod()); + } + + public void testBadDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Method method = Bar.class.getMethod("badDestroy", String.class); + try { + processor.visitMethod(null, method, type, null); + fail(); + } catch (IllegalDestructorException e) { + // expected + } + } + + public void testTwoDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Method method = Bar.class.getMethod("destroy"); + Method method2 = Bar.class.getMethod("destroy2"); + processor.visitMethod(null, method, type, null); + try { + processor.visitMethod(null, method2, type, null); + fail(); + } catch (DuplicateDestructorException e) { + // expected + } + } + + + private class Foo { + + @Destroy + public void destroy() { + } + } + + + private class Bar { + + @Destroy + public void destroy() { + } + + @Destroy + public void destroy2() { + } + + @Destroy + public void badDestroy(String foo) { + } + + + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicAndPropertyTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicAndPropertyTestCase.java new file mode 100644 index 0000000000..93de605e33 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicAndPropertyTestCase.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Constructor; + +import org.osoa.sca.annotations.Property; + +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class HeuristicAndPropertyTestCase extends TestCase { + + private PropertyProcessor propertyProcessor; + private HeuristicPojoProcessor heuristicProcessor; + + /** + * Verifies the property and heuristic processors don't collide + */ + @SuppressWarnings("unchecked") + public void testPropertyProcessorWithHeuristicProcessor() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor ctor = Foo.class.getConstructor(String.class); + type.setConstructorDefinition(new ConstructorDefinition(ctor)); + propertyProcessor.visitConstructor(null, ctor, type, null); + heuristicProcessor.visitEnd(null, Foo.class, type, null); + assertEquals(1, type.getProperties().size()); + assertNotNull(type.getProperties().get("foo")); + } + + protected void setUp() throws Exception { + super.setUp(); + ImplementationProcessorServiceImpl service = + new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl()); + propertyProcessor = new PropertyProcessor(service); + heuristicProcessor = new HeuristicPojoProcessor(service); + } + + public static class Foo { + public Foo(@Property(name = "foo") String prop) { + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java new file mode 100644 index 0000000000..92f6d54366 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java @@ -0,0 +1,330 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; +import org.osoa.sca.annotations.Remotable; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.ServiceContract; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class HeuristicConstructorTestCase extends TestCase { + + private HeuristicPojoProcessor processor = + new HeuristicPojoProcessor(new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl())); + + /** + * Verifies a single constructor is chosen with a parameter as the type + */ + public void testSingleConstructorWithParam() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + JavaMappedProperty<String> prop = new JavaMappedProperty<String>(); + prop.setName("foo"); + prop.setJavaType(String.class); + type.getProperties().put("foo", prop); + processor.visitEnd(null, Foo1.class, type, null); + assertNotNull(type.getConstructorDefinition().getConstructor()); + assertEquals("foo", type.getConstructorDefinition().getInjectionNames().get(0)); + } + + /** + * Verifies a single constructor is chosen with a reference as the type + */ + public void testSingleConstructorWithRef() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + JavaMappedReference ref = new JavaMappedReference(); + ref.setName("foo"); + ServiceContract contract = new JavaServiceContract(String.class); + ref.setServiceContract(contract); + type.getReferences().put("foo", ref); + processor.visitEnd(null, Foo1.class, type, null); + assertNotNull(type.getConstructorDefinition().getConstructor()); + assertEquals("foo", type.getConstructorDefinition().getInjectionNames().get(0)); + } + + /** + * Verifies a single constructor is chosen with a property and a reference as the type + */ + public void testSingleConstructorWithPropRef() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + + JavaMappedProperty<String> prop = new JavaMappedProperty<String>(); + prop.setName("foo"); + prop.setJavaType(String.class); + type.getProperties().put("foo", prop); + + JavaMappedReference ref = new JavaMappedReference(); + ref.setName("ref"); + ServiceContract contract = new JavaServiceContract(Foo1.class); + ref.setServiceContract(contract); + type.getReferences().put("ref", ref); + processor.visitEnd(null, Foo2.class, type, null); + assertNotNull(type.getConstructorDefinition().getConstructor()); + assertEquals(2, type.getConstructorDefinition().getInjectionNames().size()); + } + + + public void testSingleConstructorResolvableParam() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo5.class, type, null); + assertEquals(String.class, type.getProperties().get("string").getJavaType()); + } + + public void testSingleConstructorResolvableRef() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo6.class, type, null); + assertEquals(Ref.class, + type.getReferences().get("heuristicconstructortestcase$ref").getServiceContract().getInterfaceClass()); + } + + public void testSingleConstructorAmbiguousRef() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + JavaMappedReference ref = new JavaMappedReference(); + ref.setName("ref"); + ServiceContract contract = new JavaServiceContract(Foo1.class); + ref.setServiceContract(contract); + type.getReferences().put("ref", ref); + JavaMappedReference ref2 = new JavaMappedReference(); + ref2.setName("ref2"); + ref2.setServiceContract(contract); + type.getReferences().put("ref2", ref2); + try { + processor.visitEnd(null, Foo4.class, type, null); + fail(); + } catch (AmbiguousConstructorException e) { + // expected + } + } + + public void testConstructorPropertyAnnotatedParamsOnly() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo7.class, type, null); + assertNotNull(type.getProperties().get("myProp")); + } + + public void testConstructorReferenceAnnotatedParamsOnly() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo8.class, type, null); + assertNotNull(type.getReferences().get("myRef")); + } + + public void testConstructorAutowireAnnotatedParamsOnly() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo9.class, type, null); + assertNotNull(type.getReferences().get("myAutowire")); + } + + @SuppressWarnings("unchecked") + public void testDefaultConstructor() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo3.class, type, null); + assertNotNull(type.getConstructorDefinition().getConstructor()); + } + + public void testSameTypesButAnnotated() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo12.class, type, null); + assertEquals(2, type.getProperties().size()); + assertNotNull(type.getProperties().get("prop1")); + assertNotNull(type.getProperties().get("prop2")); + } + + /** + * Verifies processing executes with additional extension annotations + */ + public void testRandomAnnotation() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo11.class, type, null); + assertEquals(1, type.getProperties().size()); + assertNotNull(type.getProperties().get("prop1")); + } + + public void testAutowire() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo13.class, type, null); + assertEquals(1, type.getReferences().size()); + assertNotNull(type.getReferences().get(String.class.getName() + "0")); + } + + public void testMultipleAutowire() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo15.class, type, null); + assertEquals(2, type.getReferences().size()); + assertNotNull(type.getReferences().get(String.class.getName() + "0")); + assertNotNull(type.getReferences().get(String.class.getName() + "1")); + } + + public void testNoAutowireNameInConstructor() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo16.class, type, null); + assertEquals(2, type.getReferences().size()); + assertNotNull(type.getReferences().get(String.class.getName() + "0")); + assertNotNull(type.getReferences().get("bar")); + assertNotNull(type.getProperties().get("foo")); + } + + public void testPrivateConstructor() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + try { + processor.visitEnd(null, Foo14.class, type, null); + fail(); + } catch (NoConstructorException e) { + // expected + } + } + + + public void testMultipleConstructors() throws Exception { + // throw new UnsupportedOperationException("Finish heuristic multiple constructors - Foo10"); + } + + + public static class Foo1 { + public Foo1(String val) { + } + } + + public static class Foo2 { + public Foo2(String val, Foo1 ref) { + } + } + + public static class Foo3 { + } + + public static class Foo4 { + public Foo4(Foo1 ref) { + } + } + + public static class Prop { + + } + + @Remotable + public static interface Ref { + + } + + public static class Foo5 { + public Foo5(String val) { + } + } + + public static class Foo6 { + public Foo6(Ref ref) { + } + } + + public static class Foo7 { + public Foo7(@Property(name = "myProp") String prop) { + } + } + + + public static class Foo8 { + public Foo8(@Reference(name = "myRef") String ref) { + } + } + + public static class Foo9 { + public Foo9(@Autowire(name = "myAutowire") String autowire) { + } + } + + public static class Foo10 { + + public Foo10() { + } + + public Foo10(String prop) { + } + + public Foo10(@Property(name = "prop1") String prop1, @Property(name = "prop2") String prop2) { + + } + } + + public static class Foo11 { + + public Foo11(@Property(name = "prop1") String prop, @Baz String baz) { + } + } + + public static class Foo12 { + + public Foo12(@Property(name = "prop1") String prop, @Property(name = "prop2") String baz) { + } + } + + public @interface Baz { + + } + + public static class Foo13 { + public Foo13(@Autowire String foo) { + } + } + + public static final class Foo14 { + private Foo14() { + } + } + + public static final class Foo15 { + public Foo15(@Autowire String param1, @Autowire String param2) { + } + } + + public static final class Foo16 { + public Foo16(@Autowire String param1, + @Property(name = "foo") String param2, + @Reference(name = "bar") String param3) { + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessorTestCase.java new file mode 100644 index 0000000000..659cbbd115 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessorTestCase.java @@ -0,0 +1,367 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Constructor; +import java.util.Collection; +import java.util.List; + +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; +import org.osoa.sca.annotations.Remotable; +import org.osoa.sca.annotations.Service; + +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * Verfies component type information is properly introspected from an unadorned POJO according to the SCA Java Client + * and Implementation Model Specification + * + * @version $Rev$ $Date$ + */ +public class HeuristicPojoProcessorTestCase extends TestCase { + + private HeuristicPojoProcessor processor = + new HeuristicPojoProcessor(new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl())); + + /** + * Verifies a single service interface is computed when only one interface is implemented + */ + public void testSingleInterface() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<SingleInterfaceImpl> ctor = SingleInterfaceImpl.class.getConstructor(); + type.setConstructorDefinition(new ConstructorDefinition<SingleInterfaceImpl>(ctor)); + processor.visitEnd(null, SingleInterfaceImpl.class, type, null); + assertEquals(1, type.getServices().size()); + assertEquals(PropertyInterface.class, + type.getServices().get("HeuristicPojoProcessorTestCase$PropertyInterface") + .getServiceContract().getInterfaceClass()); + assertTrue(type.getProperties().isEmpty()); + assertTrue(type.getReferences().isEmpty()); + } + + /** + * Verifies property and reference setters are computed + */ + public void testPropertyReference() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<SingleInterfaceWithPropertyReferenceImpl> ctor = + SingleInterfaceWithPropertyReferenceImpl.class.getConstructor(); + type.setConstructorDefinition(new ConstructorDefinition<SingleInterfaceWithPropertyReferenceImpl>(ctor)); + processor.visitEnd(null, SingleInterfaceWithPropertyReferenceImpl.class, type, null); + assertEquals(1, type.getServices().size()); + assertEquals(Interface1.class, + type.getServices().get("HeuristicPojoProcessorTestCase$Interface1") + .getServiceContract().getInterfaceClass()); + assertEquals(1, type.getProperties().size()); + assertEquals(ComplexProperty.class, type.getProperties().get("property").getJavaType()); + assertEquals(1, type.getReferences().size()); + assertEquals(Ref.class, type.getReferences().get("reference").getServiceContract().getInterfaceClass()); + } + + /** + * Verifies that a property setter is not introspected if an analogous operation is in the service interface + */ + public void testPropertySetterInInterface() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<SingleInterfaceImpl> ctor = SingleInterfaceImpl.class.getConstructor(); + type.setConstructorDefinition(new ConstructorDefinition<SingleInterfaceImpl>(ctor)); + processor.visitEnd(null, SingleInterfaceImpl.class, type, null); + assertEquals(0, type.getProperties().size()); + } + + /** + * Verifies that a reference setter is not introspected if an analogous operation is in the service interface + */ + public void testReferenceSetterInInterface() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<RefInterfaceImpl> ctor = RefInterfaceImpl.class.getConstructor(); + type.setConstructorDefinition(new ConstructorDefinition<RefInterfaceImpl>(ctor)); + processor.visitEnd(null, RefInterfaceImpl.class, type, null); + assertEquals(0, type.getReferences().size()); + } + + /** + * Verifies collection generic types or array types are introspected as references according to spec rules + */ + public void testReferenceCollectionType() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<ReferenceCollectionImpl> ctor = ReferenceCollectionImpl.class.getConstructor(); + type.setConstructorDefinition(new ConstructorDefinition<ReferenceCollectionImpl>(ctor)); + processor.visitEnd(null, ReferenceCollectionImpl.class, type, null); + assertEquals(0, type.getProperties().size()); + assertEquals(4, type.getReferences().size()); + } + + /** + * Verifies collection generic types or array types are introspected as properties according to spec rules + */ + public void testPropertyCollectionType() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<PropertyCollectionImpl> ctor = PropertyCollectionImpl.class.getConstructor(); + type.setConstructorDefinition(new ConstructorDefinition<PropertyCollectionImpl>(ctor)); + processor.visitEnd(null, PropertyCollectionImpl.class, type, null); + assertEquals(0, type.getReferences().size()); + assertEquals(4, type.getProperties().size()); + } + + /** + * Verifies references are calculated when the type marked with is @Remotable + */ + public void testRemotableRef() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<RemotableRefImpl> ctor = RemotableRefImpl.class.getConstructor(); + type.setConstructorDefinition(new ConstructorDefinition<RemotableRefImpl>(ctor)); + processor.visitEnd(null, RemotableRefImpl.class, type, null); + assertEquals(2, type.getReferences().size()); + assertEquals(0, type.getProperties().size()); + } + + public void testParentInterface() throws ProcessingException, NoSuchMethodException { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Child> ctor = Child.class.getConstructor(); + type.setConstructorDefinition(new ConstructorDefinition<Child>(ctor)); + processor.visitEnd(null, Child.class, type, null); + assertTrue(type.getServices().containsKey("HeuristicPojoProcessorTestCase$Interface1")); + } + + /** + * Verifies a service inteface is calculated when only props and refs are given + */ + public void testExcludedPropertyAndReference() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + JavaMappedReference ref = new JavaMappedReference(); + ref.setName("reference"); + type.add(ref); + JavaMappedReference ref2 = new JavaMappedReference(); + ref2.setName("reference2"); + type.add(ref2); + JavaMappedProperty<?> prop1 = new JavaMappedProperty(); + prop1.setName("string1"); + type.add(prop1); + JavaMappedProperty<?> prop2 = new JavaMappedProperty(); + prop2.setName("string2"); + type.add(prop2); + processor.visitEnd(null, ServiceImpl.class, type, null); + assertEquals(1, type.getServices().size()); + } + + public void testProtectedRemotableRefField() throws ProcessingException, NoSuchMethodException { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<ProtectedRemotableRefFieldImpl> ctor = ProtectedRemotableRefFieldImpl.class.getConstructor(); + type.setConstructorDefinition(new ConstructorDefinition<ProtectedRemotableRefFieldImpl>(ctor)); + processor.visitEnd(null, ProtectedRemotableRefFieldImpl.class, type, null); + assertNotNull(type.getReferences().get("otherRef")); + } + + public void testProtectedRemotableRefMethod() throws ProcessingException, NoSuchMethodException { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<ProtectedRemotableRefMethodImpl> ctor = ProtectedRemotableRefMethodImpl.class.getConstructor(); + type.setConstructorDefinition(new ConstructorDefinition<ProtectedRemotableRefMethodImpl>(ctor)); + processor.visitEnd(null, ProtectedRemotableRefMethodImpl.class, type, null); + assertNotNull(type.getReferences().get("otherRef")); + } + + private interface PropertyInterface { + void setString1(String val); + } + + private interface Interface1 { + } + + private static class Parent implements Interface1 { + + } + + private static class Child extends Parent { + public Child() { + } + + } + + private static class SingleInterfaceImpl implements PropertyInterface { + public SingleInterfaceImpl() { + } + + public void setString1(String val) { + } + + } + + private interface HeuristicServiceInterface { + void fooOperation(String ref); + void setInvalid1(); // No parameter + void setInvalid2(String str, int i); // More than one parameter + String setInvalid3(String str); // return should be void + } + + public static class ServiceImpl implements PropertyInterface, RefInterface, HeuristicServiceInterface { + + @Property + public void setString1(String val) { + } + + @Property + public void setString2(String val) { + } + + @Reference + public void setReference(Ref ref) { + } + + @Reference + public void setReference2(Ref ref) { + } + + public void fooOperation(String ref) { + + } + public void setInvalid1() { + } + + public void setInvalid2(String str, int i) { + } + + public String setInvalid3(String str) { + return null; + } + + } + + @Service + private interface Ref { + } + + private class ComplexProperty { + } + + private interface RefInterface { + void setReference(Ref ref); + } + + private static class RefInterfaceImpl implements RefInterface { + public RefInterfaceImpl() { + } + + public void setReference(Ref ref) { + } + } + + private static class SingleInterfaceWithPropertyReferenceImpl implements Interface1 { + public SingleInterfaceWithPropertyReferenceImpl() { + } + + public void setReference(Ref ref) { + } + + public void setProperty(ComplexProperty prop) { + } + } + + private static class ReferenceCollectionImpl implements Interface1 { + public ReferenceCollectionImpl() { + } + + public void setCollectionReference(Collection<Ref> ref) { + } + + public void setNonGenericCollectionReference(Collection ref) { + } + + public void setListReference(List<Ref> ref) { + } + + public void setArrayReference(Ref[] ref) { + } + } + + private static class PropertyCollectionImpl implements Interface1 { + public PropertyCollectionImpl() { + } + + public void setCollectionProperty(Collection<ComplexProperty> prop) { + } + + public void setCollectionProperty2(Collection<String> prop) { + } + + public void setArrayProperty(ComplexProperty[] prop) { + } + + public void setArrayProperty2(String[] prop) { + } + } + + @Remotable + private interface RemotableRef { + } + + private static class RemotableRefImpl implements Interface1 { + protected RemotableRef otherRef; + + public RemotableRefImpl() { + } + + public void setRef(RemotableRef ref) { + + } + } + + private static class ProtectedRemotableRefFieldImpl implements Interface1 { + protected RemotableRef otherRef; + + public ProtectedRemotableRefFieldImpl() { + } + + public ProtectedRemotableRefFieldImpl(RemotableRef otherRef) { + this.otherRef = otherRef; + } + + } + + private static class ProtectedRemotableRefMethodImpl implements Interface1 { + public ProtectedRemotableRefMethodImpl() { + } + + protected void setOtherRef(RemotableRef otherRef) { + } + + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeutisticExtensibleConstructorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeutisticExtensibleConstructorTestCase.java new file mode 100644 index 0000000000..4e6c1063d8 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeutisticExtensibleConstructorTestCase.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Constructor; +import java.util.List; + +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * Verifies constructors that have extensible annotation types, i.e. that have parameters marked by annotations which + * are themselves processed by some other implementation processor + * + * @version $Rev$ $Date$ + */ +public class HeutisticExtensibleConstructorTestCase extends TestCase { + + private HeuristicPojoProcessor processor = + new HeuristicPojoProcessor(new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl())); + + /** + * Verifies heuristic processing can be called priot to an extension annotation processors being called. + */ + public void testBarAnnotationProcessedFirst() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + ConstructorDefinition<Foo> definition = new ConstructorDefinition<Foo>(ctor); + type.setConstructorDefinition(definition); + JavaMappedProperty property = new JavaMappedProperty(); + property.setName("myBar"); + definition.getInjectionNames().add("myBar"); + type.getProperties().put("myBar", property); + processor.visitEnd(null, Foo.class, type, null); + assertEquals(2, type.getProperties().size()); + } + + /** + * Verifies heuristic processing can be called before an extension annotation processors is called. + * <p/> + * For example, given: + * <pre> Foo(@Bar String prop, @org.osoa.sca.annotations.Property(name = "foo") String prop2)</pre> + * <p/> + * Heuristic evaluation of @Property can occur prior to another implementation processor evaluating @Bar + * + * @throws Exception + */ + public void testBarAnnotationProcessedLast() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitEnd(null, Foo.class, type, null); + + // now simulate process the bar impl + ConstructorDefinition<?> definition = type.getConstructorDefinition(); + List<String> injectionNames = definition.getInjectionNames(); + injectionNames.remove(0); + injectionNames.add(0, "mybar"); + type.getProperties().put("mybar", new JavaMappedProperty<String>()); + + assertEquals(2, type.getProperties().size()); + assertEquals("foo", definition.getInjectionNames().get(1)); + } + + /** + * Verifies heuristic processing can be called before an extension annotation processors is called with the + * extension parameter in a middle position. Specifically, verifies that the heuristic processor updates injection + * names and preserves their ordering. + */ + public void testBarAnnotationProcessedFirstInMiddle() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Foo2> ctor = Foo2.class.getConstructor(String.class, String.class, String.class); + ConstructorDefinition<Foo2> definition = new ConstructorDefinition<Foo2>(ctor); + type.setConstructorDefinition(definition); + // insert placeholder for first param, which would be done by a processor + definition.getInjectionNames().add(""); + JavaMappedProperty property = new JavaMappedProperty(); + property.setName("myBar"); + definition.getInjectionNames().add("myBar"); + type.getProperties().put("myBar", property); + processor.visitEnd(null, Foo2.class, type, null); + assertEquals("baz", definition.getInjectionNames().get(0)); + assertEquals(2, type.getProperties().size()); + assertEquals(1, type.getReferences().size()); + } + + public @interface Bar { + + } + + public static class Foo { + public Foo(@Bar String prop, @org.osoa.sca.annotations.Property(name = "foo") String prop2) { + } + } + + public static class Foo2 { + public Foo2(@org.osoa.sca.annotations.Reference(name = "baz") String prop1, + @Bar String prop2, + @org.osoa.sca.annotations.Property(name = "foo") String prop3) { + } + } + + +} + + diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceTestCase.java new file mode 100644 index 0000000000..3fbe5a8350 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceTestCase.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import org.osoa.sca.annotations.Callback; +import org.osoa.sca.annotations.Remotable; +import org.osoa.sca.annotations.Scope; + +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService; +import org.apache.tuscany.spi.model.InteractionScope; +import org.apache.tuscany.spi.model.ServiceContract; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class ImplementationProcessorServiceTestCase extends TestCase { + + private ImplementationProcessorService implService = + new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl()); + + public void testCreateConversationalService() throws Exception { + JavaMappedService service = implService.createService(Foo.class); + assertTrue(Foo.class.equals(service.getServiceContract().getInterfaceClass())); + assertTrue(service.isRemotable()); + assertEquals(InteractionScope.CONVERSATIONAL, service.getServiceContract().getInteractionScope()); + ServiceContract serviceContract = service.getServiceContract(); + assertTrue(Bar.class.equals(serviceContract.getCallbackClass())); + assertTrue("ImplementationProcessorServiceTestCase$Bar".equals(serviceContract.getCallbackName())); + } + + public void testCreateDefaultService() throws Exception { + JavaMappedService service = implService.createService(Baz.class); + assertTrue(Baz.class.equals(service.getServiceContract().getInterfaceClass())); + assertTrue(!service.isRemotable()); + assertEquals(InteractionScope.NONCONVERSATIONAL, service.getServiceContract().getInteractionScope()); + } + + + @Callback(Bar.class) + @Remotable + @Scope("CONVERSATIONAL") + public interface Foo { + + } + + public interface Bar { + + } + + public interface Baz { + + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceUniqueTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceUniqueTestCase.java new file mode 100644 index 0000000000..da45ed2d46 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceUniqueTestCase.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class ImplementationProcessorServiceUniqueTestCase extends TestCase { + + private ImplementationProcessorService service = + new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl()); + + public void testUniquess1() throws Exception { + Class[] classes = new Class[2]; + classes[0] = String.class; + classes[1] = Integer.class; + assertTrue(service.areUnique(classes)); + } + + public void testUniquess2() throws Exception { + Class[] classes = new Class[2]; + classes[0] = String.class; + classes[1] = String.class; + assertFalse(service.areUnique(classes)); + } + + public void testUniquess3() throws Exception { + Class[] classes = new Class[1]; + classes[0] = String.class; + assertTrue(service.areUnique(classes)); + } + + public void testUniquess4() throws Exception { + Class[] classes = new Class[3]; + classes[0] = String.class; + classes[1] = Integer.class; + classes[2] = String.class; + assertFalse(service.areUnique(classes)); + } + + public void testUniquess5() throws Exception { + Class[] classes = new Class[0]; + assertTrue(service.areUnique(classes)); + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/InitProcessorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/InitProcessorTestCase.java new file mode 100644 index 0000000000..059094f922 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/InitProcessorTestCase.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Method; + +import org.osoa.sca.annotations.Init; + +import org.apache.tuscany.spi.implementation.java.JavaMappedService; + +import junit.framework.TestCase; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +/** + * @version $Rev$ $Date$ + */ +public class InitProcessorTestCase extends TestCase { + + public void testInit() throws Exception { + InitProcessor processor = new InitProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Method method = InitProcessorTestCase.Foo.class.getMethod("init"); + processor.visitMethod(null, method, type, null); + assertNotNull(type.getInitMethod()); + assertEquals(50, type.getInitLevel()); + } + + public void testBadInit() throws Exception { + InitProcessor processor = new InitProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Method method = InitProcessorTestCase.Bar.class.getMethod("badInit", String.class); + try { + processor.visitMethod(null, method, type, null); + fail(); + } catch (IllegalInitException e) { + // expected + } + } + + public void testTwoInit() throws Exception { + InitProcessor processor = new InitProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Method method = InitProcessorTestCase.Bar.class.getMethod("init"); + Method method2 = InitProcessorTestCase.Bar.class.getMethod("init2"); + processor.visitMethod(null, method, type, null); + try { + processor.visitMethod(null, method2, type, null); + fail(); + } catch (DuplicateInitException e) { + // expected + } + } + + + private class Foo { + @Init(eager = true) + public void init() { + } + } + + + private class Bar { + @Init + public void init() { + } + + @Init + public void init2() { + } + + @Init + public void badInit(String foo) { + } + + + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/MonitorProcessorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/MonitorProcessorTestCase.java new file mode 100644 index 0000000000..8982fa3991 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/MonitorProcessorTestCase.java @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Map; + +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.IllegalPropertyException; + +import junit.framework.TestCase; +import org.apache.tuscany.api.annotation.Monitor; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; +import org.apache.tuscany.core.injection.SingletonObjectFactory; +import org.apache.tuscany.host.MonitorFactory; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class MonitorProcessorTestCase extends TestCase { + + private MonitorProcessor processor; + private MonitorFactory monitorFactory; + + public void testSetter() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Method method = Foo.class.getMethod("setMonitor", Foo.class); + EasyMock.expect(monitorFactory.getMonitor(EasyMock.eq(Foo.class))).andReturn(null); + EasyMock.replay(monitorFactory); + processor.visitMethod(null, method, type, null); + Map<String, JavaMappedProperty<?>> properties = type.getProperties(); + assertTrue(properties.get("monitor").getDefaultValueFactory() instanceof SingletonObjectFactory); + EasyMock.verify(monitorFactory); + } + + + public void testBadSetter() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Method method = BadMonitor.class.getMethod("setMonitor"); + try { + processor.visitMethod(null, method, type, null); + fail(); + } catch (IllegalPropertyException e) { + // expected + } + } + + public void testField() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Field field = Foo.class.getDeclaredField("bar"); + EasyMock.expect(monitorFactory.getMonitor(EasyMock.eq(Foo.class))).andReturn(null); + EasyMock.replay(monitorFactory); + processor.visitField(null, field, type, null); + Map<String, JavaMappedProperty<?>> properties = type.getProperties(); + assertTrue(properties.get("bar").getDefaultValueFactory() instanceof SingletonObjectFactory); + EasyMock.verify(monitorFactory); + } + + public void testConstructor() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Bar> ctor = Bar.class.getConstructor(BazMonitor.class); + EasyMock.expect(monitorFactory.getMonitor(EasyMock.eq(BazMonitor.class))).andReturn(null); + EasyMock.replay(monitorFactory); + processor.visitConstructor(null, ctor, type, null); + Map<String, JavaMappedProperty<?>> properties = type.getProperties(); + assertTrue( + properties.get(BazMonitor.class.getName()).getDefaultValueFactory() instanceof SingletonObjectFactory); + EasyMock.verify(monitorFactory); + } + + /** + * Verifies calling the monitor processor to evaluate a constructor can be done after a property parameter is + * processed + */ + public void testConstructorAfterProperty() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Bar> ctor = Bar.class.getConstructor(String.class, BazMonitor.class); + EasyMock.expect(monitorFactory.getMonitor(EasyMock.eq(BazMonitor.class))).andReturn(null); + EasyMock.replay(monitorFactory); + ConstructorDefinition<Bar> definition = new ConstructorDefinition<Bar>(ctor); + JavaMappedProperty prop = new JavaMappedProperty(); + definition.getInjectionNames().add("prop"); + type.setConstructorDefinition(definition); + type.getProperties().put("prop", prop); + processor.visitConstructor(null, ctor, type, null); + Map<String, JavaMappedProperty<?>> properties = type.getProperties(); + assertEquals(BazMonitor.class.getName(), definition.getInjectionNames().get(1)); + assertEquals(2, type.getProperties().size()); + String name = BazMonitor.class.getName(); + assertTrue(properties.get(name).getDefaultValueFactory() instanceof SingletonObjectFactory); + EasyMock.verify(monitorFactory); + } + + /** + * Verifies calling the monitor processor to evaluate a constructor can be done before a property parameter is + * processed + */ + public void testConstructorBeforeProperty() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Constructor<Bar> ctor = Bar.class.getConstructor(String.class, BazMonitor.class); + EasyMock.expect(monitorFactory.getMonitor(EasyMock.eq(BazMonitor.class))).andReturn(null); + EasyMock.replay(monitorFactory); + processor.visitConstructor(null, ctor, type, null); + Map<String, JavaMappedProperty<?>> properties = type.getProperties(); + ConstructorDefinition definition = type.getConstructorDefinition(); + assertEquals(2, definition.getInjectionNames().size()); + assertEquals(BazMonitor.class.getName(), definition.getInjectionNames().get(1)); + String name = BazMonitor.class.getName(); + assertTrue(properties.get(name).getDefaultValueFactory() instanceof SingletonObjectFactory); + EasyMock.verify(monitorFactory); + } + + protected void setUp() throws Exception { + super.setUp(); + monitorFactory = EasyMock.createMock(MonitorFactory.class); + JavaInterfaceProcessorRegistryImpl registry = new JavaInterfaceProcessorRegistryImpl(); + ImplementationProcessorServiceImpl processor = new ImplementationProcessorServiceImpl(registry); + this.processor = new MonitorProcessor(monitorFactory, processor); + } + + private class Foo { + + @Monitor + protected Foo bar; + + @Monitor + public void setMonitor(Foo foo) { + } + } + + + private class BadMonitor { + + @Monitor + public void setMonitor() { + } + } + + private interface BazMonitor { + + } + + private static class Bar { + + public Bar(@Monitor BazMonitor monitor) { + } + + public Bar(String prop, @Monitor BazMonitor monitor) { + } + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/PropertyProcessorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/PropertyProcessorTestCase.java new file mode 100644 index 0000000000..412c9f9d36 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/PropertyProcessorTestCase.java @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import static org.apache.tuscany.spi.model.OverrideOptions.MUST; +import junit.framework.TestCase; + +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; +import org.apache.tuscany.spi.implementation.java.DuplicatePropertyException; +import org.apache.tuscany.spi.implementation.java.IllegalPropertyException; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.OverrideOptions; +import org.osoa.sca.annotations.Property; + +/** + * @version $Rev$ $Date$ + */ +public class PropertyProcessorTestCase extends TestCase { + + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type; + PropertyProcessor processor; + + public void testMethodAnnotation() throws Exception { + processor.visitMethod(null, Foo.class.getMethod("setFoo", String.class), type, null); + assertNotNull(type.getProperties().get("foo")); + } + + public void testMethodRequired() throws Exception { + processor.visitMethod(null, Foo.class.getMethod("setFooRequired", String.class), type, null); + JavaMappedProperty prop = type.getProperties().get("fooRequired"); + assertNotNull(prop); + assertEquals(prop.getOverride(), MUST); + } + + public void testMethodName() throws Exception { + processor.visitMethod(null, Foo.class.getMethod("setBarMethod", String.class), type, null); + assertNotNull(type.getProperties().get("bar")); + } + + public void testFieldAnnotation() throws Exception { + processor.visitField(null, Foo.class.getDeclaredField("baz"), type, null); + assertNotNull(type.getProperties().get("baz")); + } + + public void testFieldRequired() throws Exception { + processor.visitField(null, Foo.class.getDeclaredField("bazRequired"), type, null); + JavaMappedProperty prop = type.getProperties().get("bazRequired"); + assertNotNull(prop); + assertEquals(prop.getOverride(), OverrideOptions.MUST); + } + + public void testFieldName() throws Exception { + processor.visitField(null, Foo.class.getDeclaredField("bazField"), type, null); + assertNotNull(type.getProperties().get("theBaz")); + } + + public void testDuplicateFields() throws Exception { + processor.visitField(null, Bar.class.getDeclaredField("dup"), type, null); + try { + processor.visitField(null, Bar.class.getDeclaredField("baz"), type, null); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + public void testDuplicateMethods() throws Exception { + processor.visitMethod(null, Bar.class.getMethod("dupMethod", String.class), type, null); + try { + processor.visitMethod(null, Bar.class.getMethod("dupSomeMethod", String.class), type, null); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + public void testInvalidProperty() throws Exception { + try { + processor.visitMethod(null, Bar.class.getMethod("badMethod"), type, null); + fail(); + } catch (IllegalPropertyException e) { + // expected + } + } + + protected void setUp() throws Exception { + super.setUp(); + type = new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + JavaInterfaceProcessorRegistryImpl registry = new JavaInterfaceProcessorRegistryImpl(); + processor = new PropertyProcessor(new ImplementationProcessorServiceImpl(registry)); + } + + private class Foo { + + @Property + protected String baz; + @Property(override = "must") + protected String bazRequired; + @Property(name = "theBaz") + protected String bazField; + + @Property + public void setFoo(String string) { + } + + @Property(override = "must") + public void setFooRequired(String string) { + } + + @Property(name = "bar") + public void setBarMethod(String string) { + } + + } + + private class Bar { + + @Property + protected String dup; + + @Property(name = "dup") + protected String baz; + + @Property + public void dupMethod(String s) { + } + + @Property(name = "dupMethod") + public void dupSomeMethod(String s) { + } + + @Property + public void badMethod() { + } + + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessorTestCase.java new file mode 100644 index 0000000000..e60cf58fb5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessorTestCase.java @@ -0,0 +1,173 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import org.osoa.sca.annotations.Reference; + +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.ServiceContract; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class ReferenceProcessorTestCase extends TestCase { + + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + ReferenceProcessor processor = + new ReferenceProcessor(new JavaInterfaceProcessorRegistryImpl()); + + public void testMethodAnnotation() throws Exception { + processor.visitMethod(null, ReferenceProcessorTestCase.Foo.class.getMethod("setFoo", Ref.class), type, null); + JavaMappedReference reference = type.getReferences().get("foo"); + assertNotNull(reference); + ServiceContract contract = reference.getServiceContract(); + assertEquals(Ref.class, contract.getInterfaceClass()); + assertEquals("ReferenceProcessorTestCase$Ref", contract.getInterfaceName()); + } + + public void testMethodRequired() throws Exception { + processor + .visitMethod(null, ReferenceProcessorTestCase.Foo.class.getMethod("setFooRequired", Ref.class), type, null); + JavaMappedReference prop = type.getReferences().get("fooRequired"); + assertNotNull(prop); + assertTrue(prop.isRequired()); + } + + public void testMethodName() throws Exception { + processor + .visitMethod(null, ReferenceProcessorTestCase.Foo.class.getMethod("setBarMethod", Ref.class), type, null); + assertNotNull(type.getReferences().get("bar")); + } + + public void testFieldAnnotation() throws Exception { + processor.visitField(null, ReferenceProcessorTestCase.Foo.class.getDeclaredField("baz"), type, null); + JavaMappedReference reference = type.getReferences().get("baz"); + assertNotNull(reference); + ServiceContract contract = reference.getServiceContract(); + assertEquals(Ref.class, contract.getInterfaceClass()); + assertEquals("ReferenceProcessorTestCase$Ref", contract.getInterfaceName()); + } + + public void testFieldRequired() throws Exception { + processor.visitField(null, ReferenceProcessorTestCase.Foo.class.getDeclaredField("bazRequired"), type, null); + JavaMappedReference prop = type.getReferences().get("bazRequired"); + assertNotNull(prop); + assertTrue(prop.isRequired()); + } + + public void testFieldName() throws Exception { + processor.visitField(null, ReferenceProcessorTestCase.Foo.class.getDeclaredField("bazField"), type, null); + assertNotNull(type.getReferences().get("theBaz")); + } + + public void testDuplicateFields() throws Exception { + processor.visitField(null, ReferenceProcessorTestCase.Bar.class.getDeclaredField("dup"), type, null); + try { + processor.visitField(null, ReferenceProcessorTestCase.Bar.class.getDeclaredField("baz"), type, null); + fail(); + } catch (DuplicateReferenceException e) { + //expected + } + } + + public void testDuplicateMethods() throws Exception { + processor.visitMethod(null, ReferenceProcessorTestCase.Bar.class.getMethod("dupMethod", Ref.class), type, null); + try { + processor + .visitMethod(null, ReferenceProcessorTestCase.Bar.class.getMethod("dupSomeMethod", Ref.class), type, + null); + fail(); + } catch (DuplicateReferenceException e) { + //expected + } + } + + public void testInvalidProperty() throws Exception { + try { + processor.visitMethod(null, ReferenceProcessorTestCase.Bar.class.getMethod("badMethod"), type, null); + fail(); + } catch (IllegalReferenceException e) { + //expected + } + } + + protected void setUp() throws Exception { + super.setUp(); + type = new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor = new ReferenceProcessor(new JavaInterfaceProcessorRegistryImpl()); + } + + private interface Ref { + } + + private class Foo { + + @Reference + protected Ref baz; + @Reference(required = true) + protected Ref bazRequired; + @Reference(name = "theBaz") + protected Ref bazField; + + + @Reference + public void setFoo(Ref ref) { + } + + @Reference(required = true) + public void setFooRequired(Ref ref) { + } + + @Reference(name = "bar") + public void setBarMethod(Ref ref) { + } + + } + + + private class Bar { + + @Reference + protected Ref dup; + + @Reference(name = "dup") + protected Ref baz; + + @Reference + public void dupMethod(Ref s) { + } + + @Reference(name = "dupMethod") + public void dupSomeMethod(Ref s) { + } + + @Reference + public void badMethod() { + } + + + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ScopeProcessorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ScopeProcessorTestCase.java new file mode 100644 index 0000000000..09152c9009 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ScopeProcessorTestCase.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class ScopeProcessorTestCase extends TestCase { + + CompositeComponent parent; + + public void testModuleScope() throws ProcessingException { + ScopeProcessor processor = new ScopeProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + + processor.visitClass(parent, Module.class, type, null); + assertEquals(Scope.MODULE, type.getImplementationScope()); + } + + public void testSessionScope() throws ProcessingException { + ScopeProcessor processor = new ScopeProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitClass(parent, Session.class, type, null); + assertEquals(Scope.SESSION, type.getImplementationScope()); + } + + public void testRequestScope() throws ProcessingException { + ScopeProcessor processor = new ScopeProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitClass(parent, Request.class, type, null); + assertEquals(Scope.REQUEST, type.getImplementationScope()); + } + + public void testCompositeScope() throws ProcessingException { + ScopeProcessor processor = new ScopeProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitClass(parent, Composite.class, type, null); + assertEquals(Scope.COMPOSITE, type.getImplementationScope()); + } + + public void testStatelessScope() throws ProcessingException { + ScopeProcessor processor = new ScopeProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitClass(parent, Stateless.class, type, null); + assertEquals(Scope.STATELESS, type.getImplementationScope()); + } + + public void testNoScope() throws ProcessingException { + ScopeProcessor processor = new ScopeProcessor(); + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitClass(parent, None.class, type, null); + assertEquals(Scope.STATELESS, type.getImplementationScope()); + } + + protected void setUp() throws Exception { + super.setUp(); + parent = EasyMock.createNiceMock(CompositeComponent.class); + } + + @org.osoa.sca.annotations.Scope("MODULE") + private class Module { + } + + @org.osoa.sca.annotations.Scope("SESSION") + private class Session { + } + + @org.osoa.sca.annotations.Scope("REQUEST") + private class Request { + } + + @org.osoa.sca.annotations.Scope("COMPOSITE") + private class Composite { + } + + @org.osoa.sca.annotations.Scope("STATELESS") + private class Stateless { + } + + private class None { + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ServiceCallbackTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ServiceCallbackTestCase.java new file mode 100644 index 0000000000..e5b5698e23 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ServiceCallbackTestCase.java @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.osoa.sca.annotations.Callback; +import org.osoa.sca.annotations.Service; + +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; +import org.apache.tuscany.core.idl.java.IllegalCallbackException; + +/** + * @version $Rev$ $Date$ + */ +public class ServiceCallbackTestCase extends TestCase { + + ServiceProcessor processor = + new ServiceProcessor(new ImplementationProcessorServiceImpl(new JavaInterfaceProcessorRegistryImpl())); + + public void testMethodCallbackInterface() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitClass(null, FooImpl.class, type, null); + JavaMappedService service = type.getServices().get("ServiceCallbackTestCase$Foo"); + assertNotNull(service); + Method method = FooImpl.class.getMethod("setCallback", FooCallback.class); + processor.visitMethod(null, method, type, null); + assertEquals(method, service.getCallbackMember()); + } + + public void testFieldCallbackInterface() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitClass(null, FooImpl.class, type, null); + JavaMappedService service = type.getServices().get("ServiceCallbackTestCase$Foo"); + assertNotNull(service); + Field field = FooImpl.class.getDeclaredField("callback"); + processor.visitField(null, field, type, null); + assertEquals(field, service.getCallbackMember()); + } + + public void testMethodDoesNotMatchCallback() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitClass(null, BadBarImpl.class, type, null); + Method method = BadBarImpl.class.getMethod("setWrongInterfaceCallback", String.class); + try { + processor.visitMethod(null, method, type, null); + fail(); + } catch (IllegalCallbackReferenceException e) { + // expected + } + } + + public void testNoParamCallback() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitClass(null, BadBarImpl.class, type, null); + Method method = BadBarImpl.class.getMethod("setNoParamCallback"); + try { + processor.visitMethod(null, method, type, null); + fail(); + } catch (IllegalCallbackReferenceException e) { + // expected + } + } + + public void testFieldDoesNotMatchCallback() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + processor.visitClass(null, BadBarImpl.class, type, null); + Field field = BadBarImpl.class.getDeclaredField("wrongInterfaceCallback"); + try { + processor.visitField(null, field, type, null); + fail(); + } catch (IllegalCallbackReferenceException e) { + // expected + } + } + + public void testBadCallbackInterfaceAnnotation() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + try { + processor.visitClass(null, BadFooImpl.class, type, null); + fail(); + } catch (ProcessingException e) { + // expected + assertTrue(e.getCause() instanceof IllegalCallbackException); + } + } + + @Callback(FooCallback.class) + private interface Foo { + + } + + private interface FooCallback { + + } + + @Service(Foo.class) + private static class FooImpl implements Foo { + + @Callback + protected FooCallback callback; + + @Callback + public void setCallback(FooCallback cb) { + + } + } + + private static class BadBarImpl implements Foo { + @Callback + protected String wrongInterfaceCallback; + + @Callback + public void setWrongInterfaceCallback(String cb) { + + } + + @Callback + public void setNoParamCallback() { + + } + + } + + @Callback + private interface BadFoo { + + } + + @Service(BadFoo.class) + private static class BadFooImpl implements BadFoo { + + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ServiceProcessorTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ServiceProcessorTestCase.java new file mode 100644 index 0000000000..f2a22fcb85 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ServiceProcessorTestCase.java @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.processor; + +import org.osoa.sca.annotations.Callback; +import org.osoa.sca.annotations.Remotable; +import org.osoa.sca.annotations.Service; + +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.ServiceContract; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class ServiceProcessorTestCase extends TestCase { + + private ServiceProcessor processor; + private PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type; + + public void testMultipleInterfaces() throws Exception { + processor.visitClass(null, FooMultiple.class, type, null); + assertEquals(2, type.getServices().size()); + JavaMappedService service = type.getServices().get("ServiceProcessorTestCase$Baz"); + ServiceContract contract = service.getServiceContract(); + assertEquals(Baz.class, contract.getInterfaceClass()); + assertEquals(Bar.class, contract.getCallbackClass()); + assertEquals("ServiceProcessorTestCase$Bar", contract.getCallbackName()); + assertNotNull(type.getServices().get("ServiceProcessorTestCase$Bar")); + } + + public void testSingleInterfaces() throws Exception { + processor.visitClass(null, FooSingle.class, type, null); + assertEquals(1, type.getServices().size()); + assertNotNull(type.getServices().get("ServiceProcessorTestCase$Baz")); + } + + public void testMultipleNoService() throws Exception { + processor.visitClass(null, FooMultipleNoService.class, type, null); + assertEquals(0, type.getServices().size()); + } + + public void testRemotableNoService() throws Exception { + processor.visitClass(null, FooRemotableNoService.class, type, null); + assertEquals(1, type.getServices().size()); + JavaMappedService service = type.getServices().get("ServiceProcessorTestCase$BazRemotable"); + ServiceContract contract = service.getServiceContract(); + assertEquals(BazRemotable.class, contract.getInterfaceClass()); + } + + public void testNonInterface() throws Exception { + try { + processor.visitClass(null, BadImpl.class, type, null); + fail(); + } catch (InvalidServiceType e) { + //expected + } + } + + public void testNoInterfaces() throws Exception { + try { + processor.visitClass(null, BadDefinition.class, type, null); + fail(); + } catch (IllegalServiceDefinitionException e) { + //expected + } + } + + protected void setUp() throws Exception { + super.setUp(); + JavaInterfaceProcessorRegistryImpl registry = new JavaInterfaceProcessorRegistryImpl(); + processor = new ServiceProcessor(new ImplementationProcessorServiceImpl(registry)); + type = new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + } + + @Callback(Bar.class) + private interface Baz { + } + + private interface Bar { + } + + @Remotable + private interface BazRemotable { + } + + @Service(interfaces = {Baz.class, Bar.class}) + private class FooMultiple implements Baz, Bar { + + } + + @Service(Baz.class) + private class FooSingle implements Baz, Bar { + + } + + private class FooMultipleNoService implements Baz, Bar { + + } + + private class FooRemotableNoService implements BazRemotable, Bar { + + } + + @Service(FooSingle.class) + private class BadImpl extends FooSingle { + + } + + + @Service() + private class BadDefinition extends FooSingle { + + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/builder/SystemComponentBuilderTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/builder/SystemComponentBuilderTestCase.java new file mode 100644 index 0000000000..97d69f976e --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/builder/SystemComponentBuilderTestCase.java @@ -0,0 +1,303 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.system.builder; + +import java.lang.reflect.Method; +import java.net.URI; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.PropertyValue; +import org.apache.tuscany.spi.model.ReferenceTarget; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.OutboundWire; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.event.CompositeStart; +import org.apache.tuscany.core.component.event.CompositeStop; +import org.apache.tuscany.core.component.scope.ModuleScopeContainer; +import org.apache.tuscany.core.implementation.system.model.SystemImplementation; +import org.apache.tuscany.core.implementation.system.wire.SystemInboundWire; +import org.apache.tuscany.core.injection.SingletonObjectFactory; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class SystemComponentBuilderTestCase extends TestCase { + + CompositeComponent parent; + DeploymentContext deploymentContext; + SystemComponentBuilder builder = new SystemComponentBuilder(); + ModuleScopeContainer container; + + /** + * Verifies lifecycle callbacks are made + */ + public void testLifecycleBuild() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + type.setEagerInit(true); + Method initMethod = FooImpl.class.getMethod("init"); + initMethod.setAccessible(true); + type.setInitMethod(initMethod); + Method destroyMethod = FooImpl.class.getMethod("destroy"); + destroyMethod.setAccessible(true); + type.setDestroyMethod(destroyMethod); + type.setImplementationScope(Scope.MODULE); + ConstructorDefinition<FooImpl> ctorDef = new ConstructorDefinition<FooImpl>(FooImpl.class.getConstructor()); + type.setConstructorDefinition(ctorDef); + SystemImplementation impl = new SystemImplementation(); + impl.setComponentType(type); + impl.setImplementationClass(FooImpl.class); + ComponentDefinition<SystemImplementation> definition = new ComponentDefinition<SystemImplementation>(impl); + AtomicComponent component = builder.build(parent, definition, deploymentContext); + component.start(); + container.onEvent(new CompositeStart(this, null)); + FooImpl foo = (FooImpl) component.getServiceInstance(); + assertTrue(foo.initialized); + container.onEvent(new CompositeStop(this, null)); + assertTrue(foo.destroyed); + } + + /** + * Verifies properties are built properly + */ + public void testPropertyBuild() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + type.setEagerInit(true); + Method initMethod = FooImpl.class.getMethod("init"); + initMethod.setAccessible(true); + type.setInitMethod(initMethod); + Method destroyMethod = FooImpl.class.getMethod("destroy"); + destroyMethod.setAccessible(true); + type.setDestroyMethod(destroyMethod); + type.setImplementationScope(Scope.MODULE); + JavaMappedProperty mappedProp = new JavaMappedProperty(); + mappedProp.setName("prop"); + Method propMethod = FooImpl.class.getMethod("setProp", String.class); + propMethod.setAccessible(true); + mappedProp.setMember(propMethod); + type.add(mappedProp); + ConstructorDefinition<FooImpl> ctorDef = new ConstructorDefinition<FooImpl>(FooImpl.class.getConstructor()); + type.setConstructorDefinition(ctorDef); + SystemImplementation impl = new SystemImplementation(); + impl.setComponentType(type); + impl.setImplementationClass(FooImpl.class); + ComponentDefinition<SystemImplementation> definition = new ComponentDefinition<SystemImplementation>(impl); + PropertyValue<String> propVal = new PropertyValue<String>(); + propVal.setName("prop"); + propVal.setValueFactory(new SingletonObjectFactory<String>("value")); + definition.add(propVal); + AtomicComponent component = builder.build(parent, definition, deploymentContext); + component.start(); + FooImpl foo = (FooImpl) component.getServiceInstance(); + assertEquals("value", foo.prop); + container.onEvent(new CompositeStop(this, null)); + } + + /** + * Verifies references are built properly + */ + public void testRefBuild() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + type.setImplementationScope(Scope.MODULE); + JavaMappedReference mappedReference = new JavaMappedReference(); + mappedReference.setName("ref"); + Method refMethod = FooImpl.class.getMethod("setRef", Foo.class); + refMethod.setAccessible(true); + mappedReference.setMember(refMethod); + ServiceContract contract = new JavaServiceContract(Foo.class); + mappedReference.setServiceContract(contract); + type.add(mappedReference); + ConstructorDefinition<FooImpl> ctorDef = new ConstructorDefinition<FooImpl>(FooImpl.class.getConstructor()); + type.setConstructorDefinition(ctorDef); + SystemImplementation impl = new SystemImplementation(); + impl.setComponentType(type); + impl.setImplementationClass(FooImpl.class); + ComponentDefinition<SystemImplementation> definition = new ComponentDefinition<SystemImplementation>(impl); + ReferenceTarget target = new ReferenceTarget(); + target.setReferenceName("ref"); + target.addTarget(new URI("foo")); + definition.add(target); + AtomicComponent component = builder.build(parent, definition, deploymentContext); + OutboundWire wire = component.getOutboundWires().get("ref").get(0); + SystemInboundWire inbound = EasyMock.createMock(SystemInboundWire.class); + FooImpl targetFoo = new FooImpl(); + EasyMock.expect(inbound.getTargetService()).andReturn(targetFoo); + EasyMock.replay(inbound); + wire.setTargetWire(inbound); + component.start(); + FooImpl foo = (FooImpl) component.getServiceInstance(); + assertNotNull(foo.ref); + container.onEvent(new CompositeStop(this, null)); + EasyMock.verify(inbound); + } + + /** + * Verifies autowires are built properly + */ + public void testAutowireBuild() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + type.setImplementationScope(Scope.MODULE); + JavaMappedReference mappedReference = new JavaMappedReference(); + mappedReference.setName("ref"); + mappedReference.setAutowire(true); + Method refMethod = FooImpl.class.getMethod("setRef", Foo.class); + refMethod.setAccessible(true); + mappedReference.setMember(refMethod); + ServiceContract contract = new JavaServiceContract(Foo.class); + mappedReference.setServiceContract(contract); + type.add(mappedReference); + ConstructorDefinition<FooImpl> ctorDef = new ConstructorDefinition<FooImpl>(FooImpl.class.getConstructor()); + type.setConstructorDefinition(ctorDef); + SystemImplementation impl = new SystemImplementation(); + impl.setComponentType(type); + impl.setImplementationClass(FooImpl.class); + ComponentDefinition<SystemImplementation> definition = new ComponentDefinition<SystemImplementation>(impl); + CompositeComponent parent = EasyMock.createNiceMock(CompositeComponent.class); + FooImpl targetFoo = new FooImpl(); + EasyMock.expect(parent.resolveSystemInstance(EasyMock.eq(Foo.class))).andReturn(targetFoo); + EasyMock.replay(parent); + AtomicComponent component = builder.build(parent, definition, deploymentContext); + component.start(); + FooImpl foo = (FooImpl) component.getServiceInstance(); + assertNotNull(foo.ref); + container.onEvent(new CompositeStop(this, null)); + EasyMock.verify(parent); + } + + /** + * Verifies constructor-based autowiring + */ + public void testAutowireConstructorBuild() throws Exception { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + type.setImplementationScope(Scope.MODULE); + ConstructorDefinition<FooImpl2> ctorDef = + new ConstructorDefinition<FooImpl2>(FooImpl2.class.getConstructor(Foo.class)); + ctorDef.getInjectionNames().add("ref"); + type.setConstructorDefinition(ctorDef); + SystemImplementation impl = new SystemImplementation(); + impl.setComponentType(type); + impl.setImplementationClass(FooImpl2.class); + JavaMappedReference mappedReference = new JavaMappedReference(); + mappedReference.setName("ref"); + mappedReference.setAutowire(true); + ServiceContract contract = new JavaServiceContract(Foo.class); + mappedReference.setServiceContract(contract); + type.add(mappedReference); + ComponentDefinition<SystemImplementation> definition = new ComponentDefinition<SystemImplementation>(impl); + CompositeComponent parent = EasyMock.createNiceMock(CompositeComponent.class); + FooImpl targetFoo = new FooImpl(); + EasyMock.expect(parent.resolveSystemInstance(EasyMock.eq(Foo.class))).andReturn(targetFoo); + EasyMock.replay(parent); + AtomicComponent component = builder.build(parent, definition, deploymentContext); + component.start(); + container.onEvent(new CompositeStart(this, null)); + FooImpl2 foo = (FooImpl2) component.getServiceInstance(); + assertNotNull(foo.getRef()); + container.onEvent(new CompositeStop(this, null)); + EasyMock.verify(parent); + } + + protected void setUp() throws Exception { + super.setUp(); + parent = EasyMock.createNiceMock(CompositeComponent.class); + container = new ModuleScopeContainer(); + container.start(); + deploymentContext = EasyMock.createMock(DeploymentContext.class); + EasyMock.expect(deploymentContext.getModuleScope()).andReturn(container).atLeastOnce(); + EasyMock.replay(deploymentContext); + } + + private static interface Foo { + + } + + private static class FooImpl implements Foo { + private boolean initialized; + private boolean destroyed; + private String prop; + private Foo ref; + + public FooImpl() { + } + + public void init() { + if (initialized) { + fail(); + } + initialized = true; + } + + public void destroy() { + if (destroyed) { + fail(); + } + destroyed = true; + } + + public boolean isInitialized() { + return initialized; + } + + public String getProp() { + return prop; + } + + public void setProp(String prop) { + this.prop = prop; + } + + public Foo getRef() { + return ref; + } + + public void setRef(Foo ref) { + this.ref = ref; + } + } + + private static class FooImpl2 implements Foo { + private Foo ref; + + public FooImpl2(@Autowire Foo ref) { + this.ref = ref; + } + + public Foo getRef() { + return ref; + } + + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/component/SystemAtomicComponentTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/component/SystemAtomicComponentTestCase.java new file mode 100644 index 0000000000..29c37ba47a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/component/SystemAtomicComponentTestCase.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.system.component; + +import junit.framework.TestCase; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.wire.SystemOutboundWire; +import org.apache.tuscany.core.injection.EventInvoker; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.apache.tuscany.core.injection.SingletonObjectFactory; +import org.easymock.EasyMock; + +/** + * Verifies a system atomic component can be started and initialized + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemAtomicComponentTestCase extends TestCase { + + private EventInvoker<Object> initInvoker; + + public void testDefaultCreationAndInit() throws Exception { + PojoObjectFactory<Foo> factory = new PojoObjectFactory<Foo>(Foo.class.getConstructor((Class[]) null)); + PojoConfiguration configuration = new PojoConfiguration(); + configuration.addServiceInterface(Foo.class); + configuration.setInstanceFactory(factory); + configuration.setInitInvoker(initInvoker); + SystemAtomicComponentImpl component = new SystemAtomicComponentImpl("foo", configuration); + Foo foo = (Foo) component.createInstance(); + component.init(foo); + assertTrue(foo.initialized); + } + + public void testReferenceAndPropertyConstructor() throws Exception { + PojoObjectFactory<Bar> factory = new PojoObjectFactory<Bar>(Bar.class.getConstructor(String.class, Foo.class)); + PojoConfiguration configuration = new PojoConfiguration(); + configuration.addServiceInterface(Foo.class); + configuration.setInstanceFactory(factory); + configuration.setInitInvoker(initInvoker); + configuration.addConstructorParamName("foo"); + configuration.addConstructorParamName("ref"); + SystemAtomicComponentImpl component = new SystemAtomicComponentImpl("foo", configuration); + component.addPropertyFactory("foo", new SingletonObjectFactory<String>("baz")); + Foo target = new Foo(); + SystemOutboundWire wire = EasyMock.createMock(SystemOutboundWire.class); + EasyMock.expect(wire.getTargetService()).andReturn(target); + EasyMock.expect(wire.getReferenceName()).andReturn("ref").anyTimes(); + EasyMock.replay(wire); + component.addOutboundWire(wire); + Bar bar = (Bar) component.createInstance(); + assertEquals("baz", bar.foo); + assertEquals(target, bar.ref); + EasyMock.verify(wire); + } + + protected void setUp() throws Exception { + super.setUp(); + initInvoker = new MethodEventInvoker<Object>(Foo.class.getMethod("init")); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public static class Foo { + + private boolean initialized; + + public void init() { + initialized = true; + } + } + + public static class Bar { + + private String foo; + private Foo ref; + + public Bar(String foo, Foo ref) { + this.foo = foo; + this.ref = ref; + } + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/loader/SystemComponentTypeLoaderTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/loader/SystemComponentTypeLoaderTestCase.java new file mode 100644 index 0000000000..e4eec8cae2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/loader/SystemComponentTypeLoaderTestCase.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.system.loader; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.model.Property; +import org.apache.tuscany.spi.model.ReferenceDefinition; +import org.apache.tuscany.spi.model.ServiceDefinition; + +import junit.framework.TestCase; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; +import org.apache.tuscany.core.implementation.IntrospectionRegistryImpl; +import org.apache.tuscany.core.implementation.processor.ConstructorProcessor; +import org.apache.tuscany.core.implementation.processor.DestroyProcessor; +import org.apache.tuscany.core.implementation.processor.HeuristicPojoProcessor; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService; +import org.apache.tuscany.core.implementation.processor.ImplementationProcessorServiceImpl; +import org.apache.tuscany.core.implementation.processor.InitProcessor; +import org.apache.tuscany.core.implementation.processor.PropertyProcessor; +import org.apache.tuscany.core.implementation.processor.ReferenceProcessor; +import org.apache.tuscany.core.implementation.processor.ScopeProcessor; +import org.apache.tuscany.core.implementation.processor.ServiceProcessor; +import org.apache.tuscany.core.implementation.system.model.SystemImplementation; +import org.apache.tuscany.core.mock.component.BasicInterface; +import org.apache.tuscany.core.mock.component.BasicInterfaceImpl; +import org.apache.tuscany.core.monitor.NullMonitorFactory; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class SystemComponentTypeLoaderTestCase extends TestCase { + private SystemComponentTypeLoader loader; + + public void testIntrospectUnannotatedClass() throws ProcessingException { + CompositeComponent parent = EasyMock.createNiceMock(CompositeComponent.class); + SystemImplementation impl = new SystemImplementation(BasicInterfaceImpl.class); + PojoComponentType<?, ?, ?> componentType = loader.loadByIntrospection(parent, impl, null); + ServiceDefinition service = componentType.getServices().get("BasicInterface"); + assertEquals(BasicInterface.class, service.getServiceContract().getInterfaceClass()); + Property<?> property = componentType.getProperties().get("publicProperty"); + assertEquals(String.class, property.getJavaType()); + ReferenceDefinition referenceDefinition = componentType.getReferences().get("protectedReference"); + assertEquals(BasicInterface.class, referenceDefinition.getServiceContract().getInterfaceClass()); + } + + protected void setUp() throws Exception { + super.setUp(); + JavaInterfaceProcessorRegistryImpl interfaceProcessorRegistry = new JavaInterfaceProcessorRegistryImpl(); + ImplementationProcessorService service = + new ImplementationProcessorServiceImpl(interfaceProcessorRegistry); + IntrospectionRegistryImpl registry = new IntrospectionRegistryImpl(); + registry.setMonitor(new NullMonitorFactory().getMonitor(IntrospectionRegistryImpl.Monitor.class)); + registry.registerProcessor(new ConstructorProcessor(service)); + registry.registerProcessor(new DestroyProcessor()); + registry.registerProcessor(new InitProcessor()); + registry.registerProcessor(new ScopeProcessor()); + registry.registerProcessor(new PropertyProcessor(service)); + registry.registerProcessor(new ReferenceProcessor(interfaceProcessorRegistry)); + registry.registerProcessor(new ServiceProcessor(service)); + registry.registerProcessor(new HeuristicPojoProcessor(service)); + loader = new SystemComponentTypeLoader(registry); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/AtomicComponentWireInvocationTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/AtomicComponentWireInvocationTestCase.java new file mode 100644 index 0000000000..017274e455 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/AtomicComponentWireInvocationTestCase.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.system.wire; + +import org.apache.tuscany.spi.QualifiedName; +import org.apache.tuscany.spi.component.SystemAtomicComponent; +import org.apache.tuscany.spi.wire.OutboundWire; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.scope.ModuleScopeContainer; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl; +import org.apache.tuscany.core.injection.PojoObjectFactory; +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.easymock.EasyMock; + +/** + * Tests wiring from an system atomic context + * + * @version $$Rev$$ $$Date$$ + */ +public class AtomicComponentWireInvocationTestCase extends TestCase { + + public void testWireResolution() throws NoSuchMethodException { + ModuleScopeContainer scope = new ModuleScopeContainer(null); + scope.start(); + Target target = new TargetImpl(); + SystemInboundWire inboundWire = EasyMock.createMock(SystemInboundWire.class); + EasyMock.expect(inboundWire.getTargetService()).andReturn(target); + EasyMock.replay(inboundWire); + + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setScopeContainer(scope); + configuration.addReferenceSite("setTarget", SourceImpl.class.getMethod("setTarget", Target.class)); + configuration.addServiceInterface(Source.class); + configuration.setInstanceFactory(new PojoObjectFactory<SourceImpl>(SourceImpl.class.getConstructor())); + SystemAtomicComponent sourceContext = new SystemAtomicComponentImpl("source", configuration); + QualifiedName qName = new QualifiedName("service"); + OutboundWire outboundWire = new SystemOutboundWireImpl("setTarget", qName, Target.class); + outboundWire.setTargetWire(inboundWire); + sourceContext.addOutboundWire(outboundWire); + sourceContext.start(); + assertSame(((Source) sourceContext.getServiceInstance()).getTarget(), target); + // wires should pass back direct ref + EasyMock.verify(inboundWire); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemInboundtoOutboundTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemInboundtoOutboundTestCase.java new file mode 100644 index 0000000000..ed89fb6aae --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemInboundtoOutboundTestCase.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.system.wire; + +import junit.framework.TestCase; +import org.apache.tuscany.core.mock.component.Target; +import org.apache.tuscany.core.mock.component.TargetImpl; +import org.easymock.EasyMock; + +/** + * Tests connecting an inbound system wire to an outbound system wire + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemInboundtoOutboundTestCase extends TestCase { + + public void testWire() throws NoSuchMethodException { + Target target = new TargetImpl(); + SystemOutboundWire outboundWire = EasyMock.createMock(SystemOutboundWire.class); + EasyMock.expect(outboundWire.getTargetService()).andReturn(target); + EasyMock.replay(outboundWire); + SystemInboundWire inboundWire = new SystemInboundWireImpl("service", Target.class); + inboundWire.setTargetWire(outboundWire); + assertSame(inboundWire.getTargetService(), target); + EasyMock.verify(outboundWire); + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundAutowireTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundAutowireTestCase.java new file mode 100644 index 0000000000..8c00af6e9e --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundAutowireTestCase.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.system.wire; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.TargetNotFoundException; + +import junit.framework.TestCase; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +/** + * @version $Rev$ $Date$ + */ +public class SystemOutboundAutowireTestCase extends TestCase { + + public void testAutowire() { + CompositeComponent component = createMock(CompositeComponent.class); + expect(component.resolveSystemInstance(Object.class)).andReturn(new Object()); + replay(component); + SystemOutboundAutowire wire = new SystemOutboundAutowire("foo", Object.class, component, false); + assertNotNull(wire.getTargetService()); + verify(component); + } + + + public void testNonExistentAutowire() { + CompositeComponent component = createMock(CompositeComponent.class); + expect(component.resolveSystemInstance(Object.class)).andReturn(null); + replay(component); + SystemOutboundAutowire wire = new SystemOutboundAutowire("foo", Object.class, component, true); + try { + wire.getTargetService(); + fail(); + } catch (TargetNotFoundException e) { + // expected + } + verify(component); + } + + + public void testNonExistentAutowireNotRequired() { + CompositeComponent component = createMock(CompositeComponent.class); + expect(component.resolveSystemInstance(Object.class)).andReturn(null); + replay(component); + SystemOutboundAutowire wire = new SystemOutboundAutowire("foo", Object.class, component, false); + try { + assertNull(wire.getTargetService()); + } catch (TargetNotFoundException e) { + fail(); + } + verify(component); + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundToInboundTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundToInboundTestCase.java new file mode 100644 index 0000000000..8f41a33c12 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundToInboundTestCase.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.system.wire; + +import org.apache.tuscany.spi.QualifiedName; + +import junit.framework.TestCase; +import org.apache.tuscany.core.mock.component.Target; +import org.apache.tuscany.core.mock.component.TargetImpl; +import org.easymock.EasyMock; + +/** + * Tests connecting an outbound system wire to an inbound system wire + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemOutboundToInboundTestCase extends TestCase { + + public void testWire() throws NoSuchMethodException { + Target target = new TargetImpl(); + SystemInboundWire inboundWire = EasyMock.createMock(SystemInboundWire.class); + EasyMock.expect(inboundWire.getTargetService()).andReturn(target); + EasyMock.replay(inboundWire); + QualifiedName qName = new QualifiedName("service"); + SystemOutboundWire outboundWire = new SystemOutboundWireImpl("setTarget", qName, Target.class); + outboundWire.setTargetWire(inboundWire); + assertSame(outboundWire.getTargetService(), target); + EasyMock.verify(inboundWire); + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemServiceComponentWireTestCase.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemServiceComponentWireTestCase.java new file mode 100644 index 0000000000..222b02f6a1 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/wire/SystemServiceComponentWireTestCase.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.implementation.system.wire; + +import junit.framework.TestCase; +import org.apache.tuscany.core.implementation.system.component.SystemService; +import org.apache.tuscany.core.implementation.system.component.SystemServiceImpl; +import org.apache.tuscany.core.mock.component.Target; +import org.apache.tuscany.core.mock.component.TargetImpl; +import org.easymock.EasyMock; + +/** + * Verifies that a system context interacts correctly with configured, connected inbound and outbound system wires + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemServiceComponentWireTestCase extends TestCase { + + public void testServiceContext() throws NoSuchMethodException { + Target target = new TargetImpl(); + SystemOutboundWire outboundWire = EasyMock.createMock(SystemOutboundWire.class); + EasyMock.expect(outboundWire.getTargetService()).andReturn(target); + EasyMock.replay(outboundWire); + SystemInboundWire wire = new SystemInboundWireImpl("Target", Target.class); + SystemService serviceContext = new SystemServiceImpl("service", null); + serviceContext.setInboundWire(wire); + serviceContext.setOutboundWire(outboundWire); + wire.setTargetWire(outboundWire); + assertSame(target, serviceContext.getServiceInstance()); + EasyMock.verify(outboundWire); + } +} |