diff options
Diffstat (limited to 'sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org')
91 files changed, 7551 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/impl/JavaImplementationTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/impl/JavaImplementationTestCase.java new file mode 100644 index 0000000000..d916f67050 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/impl/JavaImplementationTestCase.java @@ -0,0 +1,137 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.impl; + +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.common.resource.impl.ResourceLoaderImpl; +import org.apache.tuscany.container.java.assembly.mock.HelloWorldImpl; +import org.apache.tuscany.container.java.assembly.mock.HelloWorldWithFieldProperties; +import org.apache.tuscany.container.java.assembly.mock.NakedHelloWorld; +import org.apache.tuscany.container.java.assembly.mock.NakedHelloWorldWithInterface; +import org.apache.tuscany.model.assembly.ComponentType; +import org.apache.tuscany.model.assembly.Property; +import org.apache.tuscany.model.assembly.Service; +import org.apache.tuscany.model.assembly.impl.AssemblyFactoryImpl; +import org.apache.tuscany.model.assembly.impl.AssemblyModelContextImpl; +import org.apache.tuscany.model.scdl.loader.impl.SCDLAssemblyModelLoaderImpl; + +/** + * @version $Rev$ $Date$ + */ +public class JavaImplementationTestCase extends TestCase { + private JavaImplementationImpl impl = (JavaImplementationImpl) new JavaAssemblyFactoryImpl().createJavaImplementation(); + + public void testFoo() { + impl.setImplementationClass(HelloWorldImpl.class); + + // this is not needed anymore + //assertEquals("org/apache/tuscany/container/java/assembly/mock/HelloWorldImpl.componentType", impl.getComponentTypeName()); + } + + public void testNoImplementationClass() { + //FIXME this test fails with NPE +// impl.setImplementationClass(null); +// try { +// impl.initialize(new AssemblyModelContextImpl(new AssemblyLoaderImpl(), ResourceLoaderFactory.getResourceLoader(Thread.currentThread().getContextClassLoader()))); +// impl.getComponentType(); +// fail("Expected IllegalArgumentException"); +// } catch (IllegalArgumentException e) { +// // ok +// } + } + + public void testNakedHelloWorld() { + impl.setImplementationClass(NakedHelloWorld.class); + impl.initialize(new AssemblyModelContextImpl(new AssemblyFactoryImpl(), new SCDLAssemblyModelLoaderImpl(null), new ResourceLoaderImpl(Thread.currentThread().getContextClassLoader()))); + ComponentType type = impl.getComponentType(); + Assert.assertNotNull(type); + Assert.assertTrue(type.getProperties().isEmpty()); + Assert.assertTrue(type.getReferences().isEmpty()); + List<Service> services = type.getServices(); + Assert.assertEquals(1, services.size()); + Assert.assertTrue(services.get(0).getName().equals("NakedHelloWorld")); + } + + public void testNakedHelloWorldWithInterface() { + impl.setImplementationClass(NakedHelloWorldWithInterface.class); + impl.initialize(new AssemblyModelContextImpl(new AssemblyFactoryImpl(), new SCDLAssemblyModelLoaderImpl(null), new ResourceLoaderImpl(Thread.currentThread().getContextClassLoader()))); + ComponentType type = impl.getComponentType(); + Assert.assertNotNull(type); + Assert.assertTrue(type.getProperties().isEmpty()); + Assert.assertTrue(type.getReferences().isEmpty()); + List<Service> services = type.getServices(); + Assert.assertEquals(1, services.size()); + Assert.assertTrue(services.get(0).getName().equals("NakedHelloWorldWithInterface")); + } + + public void testHelloWorldWithSidefile() { + impl.setImplementationClass(HelloWorldImpl.class); + impl.initialize(new AssemblyModelContextImpl(new AssemblyFactoryImpl(), new SCDLAssemblyModelLoaderImpl(null), new ResourceLoaderImpl(Thread.currentThread().getContextClassLoader()))); + ComponentType type = impl.getComponentType(); + Assert.assertNotNull(type); + List<Property> props = type.getProperties(); + Assert.assertEquals(1, props.size()); + Assert.assertTrue(props.get(0).getName().equals("text")); + + Assert.assertTrue(type.getReferences().isEmpty()); + + List<Service> services = type.getServices(); + Assert.assertEquals(1, services.size()); + Assert.assertTrue(services.get(0).getName().equals("HelloWorldService")); + } + + public void testHelloWorldWithFieldProperties() { + impl.setImplementationClass(HelloWorldWithFieldProperties.class); + impl.initialize(new AssemblyModelContextImpl(new AssemblyFactoryImpl(), new SCDLAssemblyModelLoaderImpl(null), new ResourceLoaderImpl(Thread.currentThread().getContextClassLoader()))); + ComponentType type = impl.getComponentType(); + Assert.assertNotNull(type); + List<Property> props = type.getProperties(); + Assert.assertEquals(3, props.size()); + + Property prop = type.getProperty("text"); + Assert.assertNotNull(prop); + Assert.assertEquals("text", prop.getName()); + Assert.assertEquals(false, prop.isRequired()); + Assert.assertEquals(String.class, prop.getType()); + + prop = type.getProperty("text2"); + Assert.assertNotNull(prop); + Assert.assertEquals("text2", prop.getName()); + Assert.assertEquals(true, prop.isRequired()); + Assert.assertEquals(Integer.class, prop.getType()); + + prop = type.getProperty("foo"); + Assert.assertNotNull(prop); + Assert.assertEquals("foo", prop.getName()); + Assert.assertEquals(false, prop.isRequired()); + Assert.assertEquals(Integer.TYPE, prop.getType()); + } + + protected void setUp() throws Exception { + super.setUp(); + Thread.currentThread().setContextClassLoader(JavaImplementationTestCase.class.getClassLoader()); + } + + protected void tearDown() throws Exception { + Thread.currentThread().setContextClassLoader(null); + super.tearDown(); + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldImpl.componentType b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldImpl.componentType new file mode 100644 index 0000000000..784d8bf728 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldImpl.componentType @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="ASCII"?>
+<!--
+ Copyright (c) 2005 The Apache Software Foundation or its licensors, as applicable.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+<componentType xmlns="http://www.osoa.org/xmlns/sca/0.9" + xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+ <service name="HelloWorldService">
+ <interface.java interface="org.apache.tuscany.container.java.assembly.mock.HelloWorldService" remoteable="true"/>
+ </service>
+ <property name="text" type="xsd:string"/>
+</componentType>
\ No newline at end of file diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldImpl.java new file mode 100644 index 0000000000..74f4c658ce --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldImpl.java @@ -0,0 +1,36 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.mock; + +/** + * @version $Rev$ $Date$ + */ +public class HelloWorldImpl implements HelloWorldService { + + private String text; + + private int count; + + public String hello(String name) { + ++count; + return "Hello " + name; + } + + public int count() { + return count; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldService.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldService.java new file mode 100644 index 0000000000..a7d0fe7788 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldService.java @@ -0,0 +1,27 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.mock; + +/** + * @version $Rev$ $Date$ + */ +public interface HelloWorldService { + String hello(String name); + + public int count(); + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldWithFieldProperties.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldWithFieldProperties.java new file mode 100644 index 0000000000..e015e611ae --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/HelloWorldWithFieldProperties.java @@ -0,0 +1,40 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.mock; + +import org.osoa.sca.annotations.Property; + +/** + * @version $Rev$ $Date$ + */ +public class HelloWorldWithFieldProperties { + @Property + private String text; + + @Property(required = true) + public Integer text2; + + @Property(name = "foo") + public int text3; + + @Property(name = "not exposed") + protected int field4; + + public String hello(String name) { + return "Hello " + name; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/NakedHelloWorld.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/NakedHelloWorld.java new file mode 100644 index 0000000000..2f1ae5d17b --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/NakedHelloWorld.java @@ -0,0 +1,26 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.mock; + +/** + * @version $Rev$ $Date$ + */ +public class NakedHelloWorld { + public String hello(String name) { + return "Hello " + name; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/NakedHelloWorldWithInterface.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/NakedHelloWorldWithInterface.java new file mode 100644 index 0000000000..9d5fe5e969 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/mock/NakedHelloWorldWithInterface.java @@ -0,0 +1,28 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.mock; + +import java.io.Serializable; + +/** + * @version $Rev$ $Date$ + */ +public class NakedHelloWorldWithInterface implements Serializable { + public String hello(String name) { + return "Hello " + name; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/JavaAssemblyLoaderTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/JavaAssemblyLoaderTestCase.java new file mode 100644 index 0000000000..d5cea8bea2 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/JavaAssemblyLoaderTestCase.java @@ -0,0 +1,88 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.common.resource.ResourceLoader; +import org.apache.tuscany.common.resource.impl.ResourceLoaderImpl; +import org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.accountdata.AccountDataService; +import org.apache.tuscany.container.java.loader.JavaSCDLModelLoader; +import org.apache.tuscany.model.assembly.AssemblyFactory; +import org.apache.tuscany.model.assembly.AssemblyModelContext; +import org.apache.tuscany.model.assembly.Component; +import org.apache.tuscany.model.assembly.EntryPoint; +import org.apache.tuscany.model.assembly.Module; +import org.apache.tuscany.model.assembly.impl.AssemblyFactoryImpl; +import org.apache.tuscany.model.assembly.impl.AssemblyModelContextImpl; +import org.apache.tuscany.model.assembly.loader.AssemblyModelLoader; +import org.apache.tuscany.model.scdl.loader.SCDLModelLoader; +import org.apache.tuscany.model.scdl.loader.impl.SCDLAssemblyModelLoaderImpl; + +/** + */ +public class JavaAssemblyLoaderTestCase extends TestCase { + + /** + * + */ + public JavaAssemblyLoaderTestCase() { + super(); + } + + public void testLoader() { + + ResourceLoader resourceLoader=new ResourceLoaderImpl(Thread.currentThread().getContextClassLoader()); + JavaSCDLModelLoader javaLoader=new JavaSCDLModelLoader(); + List<SCDLModelLoader> scdlLoaders=new ArrayList<SCDLModelLoader>(); + scdlLoaders.add(javaLoader); + AssemblyModelLoader assemblyLoader=new SCDLAssemblyModelLoaderImpl(scdlLoaders); + AssemblyFactory assemblyFactory=new AssemblyFactoryImpl(); + AssemblyModelContext modelContext=new AssemblyModelContextImpl(assemblyFactory, assemblyLoader, resourceLoader); + + Module module = assemblyLoader.loadModule(getClass().getResource("sca.module").toString()); + module.initialize(modelContext); + Assert.assertTrue(module.getName().equals("tuscany.container.java.assembly.tests.bigbank.account")); + + Component component=module.getComponent("AccountServiceComponent"); + Assert.assertTrue(component!= null); + + EntryPoint entryPoint=module.getEntryPoint("AccountService"); + Assert.assertTrue(entryPoint!= null); + + Object value = component.getConfiguredProperty("currency").getValue(); + Assert.assertTrue(value.equals("EURO")); + + //ConfiguredService configuredService = component.getConfiguredReference("accountDataService").getTargetConfiguredServices().get(0); + //Assert.assertTrue(configuredService.getAggregatePart().getName().equals("AccountDataServiceComponent")); + + Class interfaceClass = component.getConfiguredReference("accountDataService").getReference().getServiceContract().getInterface(); + Assert.assertTrue(interfaceClass == AccountDataService.class); + + } + + protected void setUp() throws Exception { + super.setUp(); + + Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountReport.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountReport.java new file mode 100644 index 0000000000..1770c2b011 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountReport.java @@ -0,0 +1,32 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.account; + +import java.util.List; + +/** + * @model + */ + +public interface AccountReport { + + /** + * @model type="services.account.AccountSummary" + */ + + List getAccountSummaries(); +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountService.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountService.java new file mode 100644 index 0000000000..1bb938655f --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountService.java @@ -0,0 +1,25 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.account; + +import org.osoa.sca.annotations.Remotable; + +@Remotable +public interface AccountService { + + public AccountReport getAccountReport(String customerID); +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountService.wsdl b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountService.wsdl new file mode 100644 index 0000000000..921218c5b9 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountService.wsdl @@ -0,0 +1,78 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (c) 2005 The Apache Software Foundation or its licensors, as applicable.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
+ xmlns:tns="http://www.bigbank.com/AccountService/"
+ xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ targetNamespace="http://www.bigbank.com/AccountService/"
+
+ name="AccountService">
+
+ <wsdl:types>
+ <xsd:schema targetNamespace="http://www.bigbank.com/AccountService/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
+ <xsd:element name="customerID" type="xsd:string"/>
+ <xsd:element name="getAccountReportResponse" type="tns:AccountReport"/>
+
+ <xsd:complexType name="AccountReport">
+ <xsd:sequence>
+ <xsd:element name="accountSummary" type="tns:AccountSummary" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ </xsd:complexType>
+ <xsd:complexType name="AccountSummary">
+ <xsd:sequence>
+ <xsd:element name="accountNumber" type="xsd:string"/>
+ <xsd:element name="accountType" type="xsd:string"/>
+ <xsd:element name="balance" type="xsd:float"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ </xsd:schema>
+ </wsdl:types>
+ <wsdl:message name="getAccountReportRequest">
+ <wsdl:part element="tns:customerID" name="getAccountReportRequest"/>
+ </wsdl:message>
+ <wsdl:message name="getAccountReportResponse">
+ <wsdl:part element="tns:getAccountReportResponse" name="getAccountReportResponse"/>
+ </wsdl:message>
+ <wsdl:portType name="AccountService">
+ <wsdl:operation name="getAccountReport">
+ <wsdl:input message="tns:getAccountReportRequest"/>
+ <wsdl:output message="tns:getAccountReportResponse"/>
+ </wsdl:operation>
+ </wsdl:portType>
+ <wsdl:binding name="AccountServiceSOAP" type="tns:AccountService">
+ <soap:binding style="document"
+ transport="http://schemas.xmlsoap.org/soap/http"/>
+ <wsdl:operation name="getAccountReport">
+ <soap:operation
+ soapAction="http://www.bigbank.com/AccountService/getAccountReport"/>
+ <wsdl:input>
+ <soap:body use="literal"/>
+ </wsdl:input>
+ <wsdl:output>
+ <soap:body use="literal"/>
+ </wsdl:output>
+ </wsdl:operation>
+ </wsdl:binding>
+ <wsdl:service name="AccountService">
+ <wsdl:port binding="tns:AccountServiceSOAP"
+ name="AccountServiceSOAP">
+ <soap:address location=""/>
+ </wsdl:port>
+ </wsdl:service>
+</wsdl:definitions>
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountServiceImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountServiceImpl.java new file mode 100644 index 0000000000..9a5ddb0005 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountServiceImpl.java @@ -0,0 +1,42 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.account; + +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; + +import org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.accountdata.AccountDataService; +import org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.stockquote.StockQuoteService; + +public class AccountServiceImpl implements AccountService { + + @Property + public String currency = "USD"; + + @Reference + public AccountDataService accountDataService; + @Reference + public StockQuoteService stockQuoteService; + + public AccountServiceImpl() { + } + + public AccountReport getAccountReport(String customerID) { + return null; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountSummary.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountSummary.java new file mode 100644 index 0000000000..2b11e23425 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/account/AccountSummary.java @@ -0,0 +1,46 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.account; + +/** + * @model + */ + +public interface AccountSummary { + + /** + * @model + */ + String getAccountNumber(); + + void setAccountNumber(String accountNumber); + + /** + * @model + */ + String getAccountType(); + + void setAccountType(String accountType); + + /** + * @model + */ + float getBalance(); + + void setBalance(float balance); +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/AccountDataService.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/AccountDataService.java new file mode 100644 index 0000000000..56f24ea4ac --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/AccountDataService.java @@ -0,0 +1,26 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.accountdata; + +public interface AccountDataService { + + CheckingAccount getCheckingAccount(String customerID); + + SavingsAccount getSavingsAccount(String customerID); + + StockAccount getStockAccount(String customerID); +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/AccountDataServiceImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/AccountDataServiceImpl.java new file mode 100644 index 0000000000..a7a40f954d --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/AccountDataServiceImpl.java @@ -0,0 +1,48 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.accountdata; + +public class AccountDataServiceImpl implements AccountDataService { + + public CheckingAccount getCheckingAccount(String customerID) { + + CheckingAccount checkingAccount = new CheckingAccount(); + checkingAccount.setAccountNumber(customerID + "_" + "CHA12345"); + checkingAccount.setBalance(1500.0f); + + return checkingAccount; + } + + public SavingsAccount getSavingsAccount(String customerID) { + + SavingsAccount savingsAccount = new SavingsAccount(); + savingsAccount.setAccountNumber(customerID + "_" + "SAA12345"); + savingsAccount.setBalance(1500.0f); + + return savingsAccount; + } + + public StockAccount getStockAccount(String customerID) { + + StockAccount stockAccount = new StockAccount(); + stockAccount.setAccountNumber(customerID + "_" + "STA12345"); + stockAccount.setSymbol("IBM"); + stockAccount.setQuantity(100); + + return stockAccount; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/CheckingAccount.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/CheckingAccount.java new file mode 100644 index 0000000000..ff84e9968a --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/CheckingAccount.java @@ -0,0 +1,39 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.accountdata; + +public class CheckingAccount { + + private String accountNumber; + private float balance; + + public String getAccountNumber() { + return accountNumber; + } + + public void setAccountNumber(String accountNumber) { + this.accountNumber = accountNumber; + } + + public float getBalance() { + return balance; + } + + public void setBalance(float balance) { + this.balance = balance; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/SavingsAccount.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/SavingsAccount.java new file mode 100644 index 0000000000..b39d9051de --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/SavingsAccount.java @@ -0,0 +1,39 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.accountdata; + +public class SavingsAccount { + + private String accountNumber; + private float balance; + + public String getAccountNumber() { + return accountNumber; + } + + public void setAccountNumber(String accountNumber) { + this.accountNumber = accountNumber; + } + + public float getBalance() { + return balance; + } + + public void setBalance(float balance) { + this.balance = balance; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/StockAccount.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/StockAccount.java new file mode 100644 index 0000000000..bce0821b25 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/accountdata/StockAccount.java @@ -0,0 +1,48 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.accountdata; + +public class StockAccount { + + private String accountNumber; + private String symbol; + private int quantity; + + public String getAccountNumber() { + return accountNumber; + } + + public void setAccountNumber(String accountNumber) { + this.accountNumber = accountNumber; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public String getSymbol() { + return symbol; + } + + public void setSymbol(String symbol) { + this.symbol = symbol; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/stockquote/StockQuoteService.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/stockquote/StockQuoteService.java new file mode 100644 index 0000000000..78ece12b41 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/stockquote/StockQuoteService.java @@ -0,0 +1,24 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.stockquote; + +public interface StockQuoteService { + + public float getQuote(String symbol); +} + +
\ No newline at end of file diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/stockquote/StockQuoteServiceImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/stockquote/StockQuoteServiceImpl.java new file mode 100644 index 0000000000..fa180d0ceb --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/stockquote/StockQuoteServiceImpl.java @@ -0,0 +1,28 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.assembly.tests.bigbank.account.services.stockquote; + +/* Dummy'd up StockQuoteService, override webservice for now */ + +public class StockQuoteServiceImpl implements StockQuoteService { + + public float getQuote(String symbol) { + // Just hardcode for now + return 83.00f; + } + +}
\ No newline at end of file diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/stockquote/StockQuoteWebService.wsdl b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/stockquote/StockQuoteWebService.wsdl new file mode 100644 index 0000000000..9678d9d6ea --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/assembly/tests/bigbank/account/services/stockquote/StockQuoteWebService.wsdl @@ -0,0 +1,98 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (c) 2005 The Apache Software Foundation or its licensors, as applicable.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+<wsdl:definitions targetNamespace="http://webservice.stockquote" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://webservice.stockquote" xmlns:intf="http://webservice.stockquote" xmlns:tns1="http://stockquote" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="">
+ <!--WSDL created by Apache Axis version: 1.2.1
+Built on Jun 14, 2005 (09:15:57 EDT)-->
+ <wsdl:types>
+ <schema elementFormDefault="qualified" targetNamespace="http://stockquote" xmlns="http://www.w3.org/2001/XMLSchema">
+ <complexType name="GetQuoteRequest">
+ <sequence>
+ <element name="symbol" nillable="true" type="xsd:string"/>
+ </sequence>
+ </complexType>
+ <complexType name="GetQuoteResponse">
+ <sequence>
+ <element name="price" type="xsd:float"/>
+ </sequence>
+ </complexType>
+ </schema>
+ <schema elementFormDefault="qualified" targetNamespace="http://webservice.stockquote" xmlns="http://www.w3.org/2001/XMLSchema">
+ <import namespace="http://stockquote"/>
+ <element name="request" type="tns1:GetQuoteRequest"/>
+ <element name="getQuoteReturn" type="tns1:GetQuoteResponse"/>
+ </schema>
+ </wsdl:types>
+
+ <wsdl:message name="getQuoteResponse">
+
+ <wsdl:part element="impl:getQuoteReturn" name="getQuoteReturn"/>
+
+ </wsdl:message>
+
+ <wsdl:message name="getQuoteRequest">
+
+ <wsdl:part element="impl:request" name="request"/>
+
+ </wsdl:message>
+
+ <wsdl:portType name="StockQuoteWebService">
+
+ <wsdl:operation name="getQuote" parameterOrder="request">
+
+ <wsdl:input message="impl:getQuoteRequest" name="getQuoteRequest"/>
+
+ <wsdl:output message="impl:getQuoteResponse" name="getQuoteResponse"/>
+
+ </wsdl:operation>
+
+ </wsdl:portType>
+
+ <wsdl:binding name="StockQuoteWebServiceSoapBinding" type="impl:StockQuoteWebService">
+
+ <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
+
+ <wsdl:operation name="getQuote">
+
+ <wsdlsoap:operation soapAction=""/>
+
+ <wsdl:input name="getQuoteRequest">
+
+ <wsdlsoap:body use="literal"/>
+
+ </wsdl:input>
+
+ <wsdl:output name="getQuoteResponse">
+
+ <wsdlsoap:body use="literal"/>
+
+ </wsdl:output>
+
+ </wsdl:operation>
+
+ </wsdl:binding>
+
+ <wsdl:service name="StockQuoteWebServiceService">
+
+ <wsdl:port binding="impl:StockQuoteWebServiceSoapBinding" name="StockQuoteWebService">
+
+ <wsdlsoap:address location="http://localhost:8123/StockQuoteWebService/services/StockQuoteWebService"/>
+
+ </wsdl:port>
+
+ </wsdl:service>
+
+</wsdl:definitions>
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/JavaComponentContextBuilderTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/JavaComponentContextBuilderTestCase.java new file mode 100644 index 0000000000..4459e9d01d --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/JavaComponentContextBuilderTestCase.java @@ -0,0 +1,111 @@ +package org.apache.tuscany.container.java.builder; + +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.common.monitor.impl.NullMonitorFactory; +import org.apache.tuscany.container.java.invocation.mock.MockSyncInterceptor; +import org.apache.tuscany.container.java.mock.MockConfigContext; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.GenericComponent; +import org.apache.tuscany.container.java.mock.components.ModuleScopeComponent; +import org.apache.tuscany.core.builder.RuntimeConfiguration; +import org.apache.tuscany.core.builder.impl.DefaultWireBuilder; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.ScopeContext; +import org.apache.tuscany.core.context.ScopeStrategy; +import org.apache.tuscany.core.context.impl.AggregateContextImpl; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.DefaultScopeStrategy; +import org.apache.tuscany.core.context.scope.ModuleScopeContext; +import org.apache.tuscany.core.invocation.ProxyConfiguration; +import org.apache.tuscany.core.invocation.jdk.JDKProxyFactoryFactory; +import org.apache.tuscany.core.invocation.spi.ProxyFactory; +import org.apache.tuscany.core.message.impl.MessageFactoryImpl; +import org.apache.tuscany.model.assembly.AssemblyFactory; +import org.apache.tuscany.model.assembly.AssemblyModelContext; +import org.apache.tuscany.model.assembly.Component; +import org.apache.tuscany.model.assembly.Module; +import org.apache.tuscany.model.assembly.impl.AssemblyFactoryImpl; +import org.apache.tuscany.model.assembly.impl.AssemblyModelContextImpl; + +public class JavaComponentContextBuilderTestCase extends TestCase { + + private AssemblyFactory factory = new AssemblyFactoryImpl(); + + private AssemblyModelContext assemblyContext = new AssemblyModelContextImpl(null,null); + + public JavaComponentContextBuilderTestCase() { + } + + public void testBuilder() throws Exception { + JavaComponentContextBuilder builder = new JavaComponentContextBuilder(); + builder.setMessageFactory(new MessageFactoryImpl()); + // HierarchicalBuilder refBuilder = new HierarchicalBuilder(); + MockSyncInterceptor interceptor = new MockSyncInterceptor(); + builder.addPolicyBuilder(new MockInterceptorBuilder(interceptor, true)); + //builder.setPolicyBuilder(refBuilder); + AggregateContext ctx = createContext(); + builder.setProxyFactoryFactory(new JDKProxyFactoryFactory()); + JavaTargetWireBuilder javaWireBuilder = new JavaTargetWireBuilder(); + ScopeStrategy strategy = new DefaultScopeStrategy(); + DefaultWireBuilder wireBuilder = new DefaultWireBuilder(); + wireBuilder.addWireBuilder(javaWireBuilder); + Module module = MockFactory.createModule(); + EventContext eCtx = new EventContextImpl(); + ScopeContext scopeContext = new ModuleScopeContext(eCtx); + scopeContext.start(); + scopeContext.onEvent(EventContext.MODULE_START, null); + List<Component> components = module.getComponents(); + Map<String, Component> compMap = new HashMap(components.size()); + + for (Component component : components) { + compMap.put(component.getName(), component); + builder.build(component, ctx); + RuntimeConfiguration config = (RuntimeConfiguration) component.getComponentImplementation().getRuntimeConfiguration(); + Assert.assertNotNull(config); + } + for (Component component : components) { + RuntimeConfiguration source = (RuntimeConfiguration) component.getComponentImplementation().getRuntimeConfiguration(); + Assert.assertNotNull(source); + for (ProxyFactory pFactory : (Collection<ProxyFactory>) source.getSourceProxyFactories().values()) { + ProxyConfiguration pConfig = pFactory.getProxyConfiguration(); + Component target = compMap.get(pConfig.getTargetName().getPartName()); + + if (target != null) { + RuntimeConfiguration targetConfig = (RuntimeConfiguration) target.getComponentImplementation() + .getRuntimeConfiguration(); + boolean downScope = strategy.downScopeReference(source.getScope(), targetConfig.getScope()); + wireBuilder.connect(pFactory, targetConfig.getTargetProxyFactory(pFactory.getProxyConfiguration().getTargetName() + .getPortName()), targetConfig.getClass(), downScope, scopeContext); + } + pFactory.initialize(); + } + + scopeContext.registerConfiguration(source); + } + for (Component component : components) { + RuntimeConfiguration config = (RuntimeConfiguration) component.getComponentImplementation().getRuntimeConfiguration(); + InstanceContext context = (InstanceContext) config.createInstanceContext(); + if ("source".equals(component.getName())) { + ModuleScopeComponent source = (ModuleScopeComponent) context.getInstance(null); + Assert.assertNotNull(source); + GenericComponent gComp = (GenericComponent) source.getGenericComponent(); + gComp.getString(); + } + } + } + + private static AggregateContext createContext() { + return new AggregateContextImpl("test.parent", null, new DefaultScopeStrategy(), new EventContextImpl(), + new MockConfigContext(null), new NullMonitorFactory()); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/JavaTargetWireBuilderTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/JavaTargetWireBuilderTestCase.java new file mode 100644 index 0000000000..c5b1ecd4fd --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/JavaTargetWireBuilderTestCase.java @@ -0,0 +1,115 @@ +package org.apache.tuscany.container.java.builder; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.config.JavaComponentRuntimeConfiguration; +import org.apache.tuscany.container.java.invocation.mock.MockHandler; +import org.apache.tuscany.container.java.invocation.mock.MockSyncInterceptor; +import org.apache.tuscany.container.java.invocation.mock.SimpleTarget; +import org.apache.tuscany.container.java.invocation.mock.SimpleTargetImpl; +import org.apache.tuscany.container.java.mock.MockScopeContext; +import org.apache.tuscany.core.builder.impl.DefaultWireBuilder; +import org.apache.tuscany.core.context.QualifiedName; +import org.apache.tuscany.core.invocation.InvocationConfiguration; +import org.apache.tuscany.core.invocation.MethodHashMap; +import org.apache.tuscany.core.invocation.ProxyConfiguration; +import org.apache.tuscany.core.invocation.impl.InvokerInterceptor; +import org.apache.tuscany.core.invocation.jdk.JDKProxyFactory; +import org.apache.tuscany.core.invocation.spi.ProxyFactory; +import org.apache.tuscany.core.message.Message; +import org.apache.tuscany.core.message.MessageFactory; +import org.apache.tuscany.core.message.impl.MessageFactoryImpl; + +public class JavaTargetWireBuilderTestCase extends TestCase { + + private Method hello; + private Method goodbye; + + public JavaTargetWireBuilderTestCase() { + } + + public JavaTargetWireBuilderTestCase(String arg0) { + super(arg0); + } + + public void setUp() throws Exception { + hello = SimpleTarget.class.getMethod("hello", new Class[] { String.class }); + goodbye = SimpleTarget.class.getMethod("goodbye", new Class[] { String.class }); + } + + + /** + * Tests basic wiring of a source to a target, including handlers and interceptors + */ + public void testInvocation() throws Exception { + MessageFactory msgFactory = new MessageFactoryImpl(); + + InvocationConfiguration source = new InvocationConfiguration(hello); + MockHandler sourceRequestHandler = new MockHandler(); + MockHandler sourceResponseHandler = new MockHandler(); + MockSyncInterceptor sourceInterceptor = new MockSyncInterceptor(); + source.addRequestHandler(sourceRequestHandler); + source.addResponseHandler(sourceResponseHandler); + source.addSourceInterceptor(sourceInterceptor); + + ProxyFactory sourceFactory = new JDKProxyFactory(); + Map<Method, InvocationConfiguration> sourceInvocationConfigs = new MethodHashMap(); + sourceInvocationConfigs.put(hello, source); + ProxyConfiguration sourceConfig = new ProxyConfiguration(new QualifiedName("target/SimpleTarget"), + sourceInvocationConfigs, Thread.currentThread().getContextClassLoader(), msgFactory); + sourceFactory.setProxyConfiguration(sourceConfig); + sourceFactory.setBusinessInterface(SimpleTarget.class); + + InvocationConfiguration target = new InvocationConfiguration(hello); + MockHandler targetRequestHandler = new MockHandler(); + MockHandler targetResponseHandler = new MockHandler(); + MockSyncInterceptor targetInterceptor = new MockSyncInterceptor(); + target.addRequestHandler(targetRequestHandler); + target.addResponseHandler(targetResponseHandler); + target.addTargetInterceptor(targetInterceptor); + target.addTargetInterceptor(new InvokerInterceptor()); + + ProxyFactory targetFactory = new JDKProxyFactory(); + Map<Method, InvocationConfiguration> targetInvocationConfigs = new MethodHashMap(); + targetInvocationConfigs.put(hello, target); + ProxyConfiguration targetConfig = new ProxyConfiguration(new QualifiedName("target/SimpleTarget"), + targetInvocationConfigs, Thread.currentThread().getContextClassLoader(), msgFactory); + targetFactory.setProxyConfiguration(targetConfig); + targetFactory.setBusinessInterface(SimpleTarget.class); + + // bootstrap a scope container with the target in it + Map<String,Object> instances = new HashMap(); + SimpleTarget simpleTarget = new SimpleTargetImpl(); + instances.put("target",simpleTarget); + MockScopeContext scopeCtx = new MockScopeContext(instances); + + // connect the source to the target + DefaultWireBuilder builder = new DefaultWireBuilder(); + builder.addWireBuilder(new JavaTargetWireBuilder()); + + builder.connect(sourceFactory, targetFactory, JavaComponentRuntimeConfiguration.class, true, scopeCtx); + source.build(); + target.build(); + Assert.assertNotNull(source.getTargetInvoker()); + + Message msg = msgFactory.createMessage(); + msg.setBody("foo"); + msg.setTargetInvoker(source.getTargetInvoker()); + Message response = (Message) source.getSourceInterceptor().invoke(msg); + Assert.assertEquals("foo", response.getBody()); + Assert.assertEquals(1, sourceRequestHandler.getCount()); + Assert.assertEquals(1, sourceResponseHandler.getCount()); + Assert.assertEquals(1, sourceInterceptor.getCount()); + Assert.assertEquals(1, targetRequestHandler.getCount()); + Assert.assertEquals(1, targetResponseHandler.getCount()); + Assert.assertEquals(1, targetInterceptor.getCount()); + } + + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/MockHandlerBuilder.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/MockHandlerBuilder.java new file mode 100644 index 0000000000..939d780e0f --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/MockHandlerBuilder.java @@ -0,0 +1,86 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.builder; + +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.RuntimeConfigurationBuilder; +import org.apache.tuscany.core.context.Context; +import org.apache.tuscany.core.invocation.InvocationConfiguration; +import org.apache.tuscany.core.invocation.MessageHandler; +import org.apache.tuscany.core.invocation.spi.ProxyFactory; +import org.apache.tuscany.model.assembly.AssemblyModelObject; +import org.apache.tuscany.model.assembly.ConfiguredReference; +import org.apache.tuscany.model.assembly.ConfiguredService; + +/** + * Adds a handler to a source or target proxy configuration + * + * @version $Rev$ $Date$ + */ +public class MockHandlerBuilder implements RuntimeConfigurationBuilder { + + private MessageHandler handler; + + private boolean source; + + private boolean request; + + /** + * Creates the builder. + * + * @param handler the handler to add to the source or target proxy configuration + * @param source true if the handler should be added on the source side; false if the handler should be added to the + * target side + * @param request true if the handler is a request handler; false if the handler is a response handler + */ + public MockHandlerBuilder(MessageHandler handler, boolean source, boolean request) { + this.handler = handler; + this.source = source; + this.request = request; + } + + public void build(AssemblyModelObject modelObject, Context context) throws BuilderException { + if (source) { + if (!(modelObject instanceof ConfiguredReference)) { + return; + } else { + ConfiguredReference cref = (ConfiguredReference) modelObject; + ProxyFactory pFactory = (ProxyFactory) cref.getProxyFactory(); + for (InvocationConfiguration config : pFactory.getProxyConfiguration().getInvocationConfigurations().values()) { + if (request) { + config.addRequestHandler(handler); + } else { + config.addResponseHandler(handler); + } + } + } + } else { + if (!(modelObject instanceof ConfiguredService)) { + return; + } else { + ConfiguredService cservice = (ConfiguredService) modelObject; + ProxyFactory pFactory = (ProxyFactory) cservice.getProxyFactory(); + for (InvocationConfiguration config : pFactory.getProxyConfiguration().getInvocationConfigurations().values()) { + if (request) { + config.addRequestHandler(handler); + } else { + config.addResponseHandler(handler); + } + } + } + + } + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/MockInterceptorBuilder.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/MockInterceptorBuilder.java new file mode 100644 index 0000000000..8bf6ed9e27 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/builder/MockInterceptorBuilder.java @@ -0,0 +1,74 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.builder; + +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.RuntimeConfigurationBuilder; +import org.apache.tuscany.core.context.Context; +import org.apache.tuscany.core.invocation.Interceptor; +import org.apache.tuscany.core.invocation.InvocationConfiguration; +import org.apache.tuscany.core.invocation.spi.ProxyFactory; +import org.apache.tuscany.model.assembly.AssemblyModelObject; +import org.apache.tuscany.model.assembly.ConfiguredReference; +import org.apache.tuscany.model.assembly.ConfiguredService; + +/** + * Adds an interceptor to a source or target proxy configuration + * + * @version $Rev$ $Date$ + */ +public class MockInterceptorBuilder implements RuntimeConfigurationBuilder { + + private Interceptor interceptor; + + private boolean source; + + /** + * Creates the builder + * + * @param interceptor the interceptor ot add + * @param source true if the interceptor should be added to the source side; false if the interceptor should be + * added to the target side + */ + public MockInterceptorBuilder(Interceptor interceptor, boolean source) { + this.interceptor = interceptor; + this.source = source; + } + + public void build(AssemblyModelObject modelObject, Context context) throws BuilderException { + if (source) { + if (!(modelObject instanceof ConfiguredReference)) { + return; + } else { + ConfiguredReference cref = (ConfiguredReference) modelObject; + ProxyFactory pFactory = (ProxyFactory) cref.getProxyFactory(); + for (InvocationConfiguration config : pFactory.getProxyConfiguration().getInvocationConfigurations().values()) { + config.addSourceInterceptor(interceptor); + } + } + } else { + if (!(modelObject instanceof ConfiguredService)) { + return; + } else { + ConfiguredService cservice = (ConfiguredService) modelObject; + ProxyFactory pFactory = (ProxyFactory) cservice.getProxyFactory(); + for (InvocationConfiguration config : pFactory.getProxyConfiguration().getInvocationConfigurations().values()) { + config.addTargetInterceptor(interceptor); + } + } + + } + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/config/ModuleComponentConfigurationLoaderTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/config/ModuleComponentConfigurationLoaderTestCase.java new file mode 100644 index 0000000000..1e8d7ebb9b --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/config/ModuleComponentConfigurationLoaderTestCase.java @@ -0,0 +1,80 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.config; + +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.common.resource.impl.ResourceLoaderImpl; +import org.apache.tuscany.container.java.assembly.impl.JavaImplementationImpl; +import org.apache.tuscany.container.java.loader.JavaSCDLModelLoader; +import org.apache.tuscany.core.config.ConfigurationException; +import org.apache.tuscany.core.config.ModuleComponentConfigurationLoader; +import org.apache.tuscany.core.config.impl.ModuleComponentConfigurationLoaderImpl; +import org.apache.tuscany.model.assembly.AssemblyModelContext; +import org.apache.tuscany.model.assembly.Component; +import org.apache.tuscany.model.assembly.ComponentImplementation; +import org.apache.tuscany.model.assembly.Module; +import org.apache.tuscany.model.assembly.ModuleComponent; +import org.apache.tuscany.model.assembly.impl.AssemblyFactoryImpl; +import org.apache.tuscany.model.assembly.impl.AssemblyModelContextImpl; +import org.apache.tuscany.model.assembly.loader.AssemblyModelLoader; +import org.apache.tuscany.model.scdl.loader.SCDLModelLoader; +import org.apache.tuscany.model.scdl.loader.impl.SCDLAssemblyModelLoaderImpl; + +/** + * @version $Rev: 379821 $ $Date: 2006-02-22 09:25:32 -0800 (Wed, 22 Feb 2006) $ + */ +public class ModuleComponentConfigurationLoaderTestCase extends TestCase { + private ModuleComponentConfigurationLoader loader; + private AssemblyModelContext modelContext; + + public void testFoo() throws ConfigurationException { + URL xml = ModuleComponentConfigurationLoaderTestCase.class.getResource("ModuleComponentLoaderTest1.module"); + ModuleComponent moduleComponent = loader.loadModuleComponent("test", "test", xml.toString()); + + Assert.assertEquals("test", moduleComponent.getName()); + Module module = moduleComponent.getModuleImplementation(); + Assert.assertEquals("ModuleComponentLoaderTest1", module.getName()); + List<Component> components = module.getComponents(); + Assert.assertEquals(1, components.size()); + Component component = components.get(0); + Assert.assertEquals("HelloWorldServiceComponent", component.getName()); + + component = module.getComponent("HelloWorldServiceComponent"); + Assert.assertEquals("HelloWorldServiceComponent", component.getName()); + + ComponentImplementation implementation = component.getComponentImplementation(); + Assert.assertTrue(implementation instanceof JavaImplementationImpl); + } + + protected void setUp() throws Exception { + super.setUp(); + Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); + List<SCDLModelLoader> scdlLoaders=new ArrayList<SCDLModelLoader>(); + JavaSCDLModelLoader javaLoader=new JavaSCDLModelLoader(); + scdlLoaders.add(javaLoader); + AssemblyModelLoader modelLoader=new SCDLAssemblyModelLoaderImpl(scdlLoaders); + modelContext=new AssemblyModelContextImpl(new AssemblyFactoryImpl(), modelLoader, + new ResourceLoaderImpl(this.getClass().getClassLoader())); + loader = new ModuleComponentConfigurationLoaderImpl(modelContext); + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/context/BadPojoTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/context/BadPojoTestCase.java new file mode 100644 index 0000000000..56567e9535 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/context/BadPojoTestCase.java @@ -0,0 +1,70 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.context; + +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.BadContextPojo; +import org.apache.tuscany.container.java.mock.components.BadNamePojo; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.impl.AggregateContextImpl; +import org.apache.tuscany.model.assembly.Scope; + +public class BadPojoTestCase 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 { + AggregateContext mc = new AggregateContextImpl(); + mc.setName("mc"); + try { + JavaComponentContext context = 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 { + AggregateContext mc = new AggregateContextImpl(); + mc.setName("mc"); + try { + JavaComponentContext context = MockFactory.createPojoContext("BadContextPojo", BadContextPojo.class, + Scope.MODULE, mc); + } catch (NoSuchMethodException e) { + if (e.getMessage().indexOf("@Context") < 0) { + throw e; + } + } + + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/context/PojoLifecycleTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/context/PojoLifecycleTestCase.java new file mode 100644 index 0000000000..113351c592 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/context/PojoLifecycleTestCase.java @@ -0,0 +1,71 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.context; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.ModuleScopeInitOnlyComponent; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.impl.AggregateContextImpl; +import org.apache.tuscany.model.assembly.Scope; + +/** + * Tests <code>@Init</code> method called, <code>@Context</code> set for ModuleContext, <code>@ComponentName</code> set + * + * @version $Rev$ $Date$ + */ +public class PojoLifecycleTestCase extends TestCase { + + public void testComponentNameSet() throws Exception { + AggregateContext mc = new AggregateContextImpl(); + mc.setName("mc"); + JavaComponentContext context = MockFactory.createPojoContext("TestServiceInit", + ModuleScopeInitOnlyComponent.class, Scope.MODULE, mc); + context.start(); + ModuleScopeInitOnlyComponent instance = (ModuleScopeInitOnlyComponent) context.getInstance(null); + Assert.assertNotNull(instance); + Assert.assertEquals("TestServiceInit", instance.getName()); + context.stop(); + } + + public void testModuleContextSet() throws Exception { + AggregateContext mc = new AggregateContextImpl(); + mc.setName("mc"); + JavaComponentContext context = MockFactory.createPojoContext("TestServiceInit", + ModuleScopeInitOnlyComponent.class, Scope.MODULE, mc); + context.start(); + ModuleScopeInitOnlyComponent instance = (ModuleScopeInitOnlyComponent) context.getInstance(null); + Assert.assertNotNull(instance); + Assert.assertEquals(mc, instance.getModuleContext()); + context.stop(); + } + + public void testInit() throws Exception { + AggregateContext mc = new AggregateContextImpl(); + mc.setName("mc"); + JavaComponentContext context = MockFactory.createPojoContext("TestServiceInit", + ModuleScopeInitOnlyComponent.class, Scope.MODULE, mc); + context.start(); + ModuleScopeInitOnlyComponent instance = (ModuleScopeInitOnlyComponent) context.getInstance(null); + Assert.assertNotNull(instance); + Assert.assertTrue(instance.isInitialized()); + context.stop(); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/context/PojoTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/context/PojoTestCase.java new file mode 100644 index 0000000000..5c30ad02a7 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/context/PojoTestCase.java @@ -0,0 +1,94 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.context; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.assembly.JavaAssemblyFactory; +import org.apache.tuscany.container.java.assembly.impl.JavaAssemblyFactoryImpl; +import org.apache.tuscany.container.java.context.JavaComponentContext; +import org.apache.tuscany.container.java.mock.components.GenericComponent; +import org.apache.tuscany.container.java.mock.components.ModuleScopeComponentImpl; +import org.apache.tuscany.container.java.mock.components.RequestScopeComponentImpl; +import org.apache.tuscany.container.java.mock.components.SessionScopeComponentImpl; +import org.apache.tuscany.container.java.mock.components.StatelessComponentImpl; +import org.apache.tuscany.core.config.JavaIntrospectionHelper; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.impl.AggregateContextImpl; +import org.apache.tuscany.core.injection.PojoObjectFactory; + +/** + * @version $Rev$ $Date$ + */ +public class PojoTestCase extends TestCase { + + JavaAssemblyFactory factory = new JavaAssemblyFactoryImpl(); + + public void testGetModuleInstance() throws Exception { + AggregateContext mc = new AggregateContextImpl(); + mc.setName("mc"); + JavaComponentContext c = new JavaComponentContext("foo", new PojoObjectFactory(JavaIntrospectionHelper + .getDefaultConstructor(ModuleScopeComponentImpl.class), null, null), false, null, null, false); + GenericComponent service = (GenericComponent) c.getInstance(null); + Assert.assertNotNull(service); + service.setString("foo"); + GenericComponent service2 = (GenericComponent) c.getInstance(null); + Assert.assertNotNull(service2); + Assert.assertSame(service, service2); + } + + public void testGetSessionInstance() throws Exception { + AggregateContext mc = new AggregateContextImpl(); + mc.setName("mc"); + JavaComponentContext c = new JavaComponentContext("foo", new PojoObjectFactory(JavaIntrospectionHelper + .getDefaultConstructor(SessionScopeComponentImpl.class), null, null), false, null, null, false); + GenericComponent service = (GenericComponent) c.getInstance(null); + Assert.assertNotNull(service); + service.setString("foo"); + GenericComponent service2 = (GenericComponent) c.getInstance(null); + Assert.assertNotNull(service2); + Assert.assertSame(service, service2); + } + + public void testGetRequestInstance() throws Exception { + AggregateContext mc = new AggregateContextImpl(); + mc.setName("mc"); + JavaComponentContext c = new JavaComponentContext("foo", new PojoObjectFactory(JavaIntrospectionHelper + .getDefaultConstructor(RequestScopeComponentImpl.class), null, null), false, null, null, false); + GenericComponent service = (GenericComponent) c.getInstance(null); + Assert.assertNotNull(service); + service.setString("foo"); + GenericComponent service2 = (GenericComponent) c.getInstance(null); + Assert.assertNotNull(service2); + Assert.assertSame(service, service2); + } + + public void testGetStatelessInstance() throws Exception { + AggregateContext mc = new AggregateContextImpl(); + mc.setName("fooContext"); + JavaComponentContext c = new JavaComponentContext("foo", new PojoObjectFactory(JavaIntrospectionHelper + .getDefaultConstructor(StatelessComponentImpl.class), null, null), false, null, null, true); + GenericComponent service = (GenericComponent) c.getInstance(null); + Assert.assertNotNull(service); + service.setString("foo"); + GenericComponent service2 = (GenericComponent) c.getInstance(null); + Assert.assertNotNull(service2); + Assert.assertTrue(!"foo".equals(service2.getString())); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/JavaBuilderContextIntegrationTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/JavaBuilderContextIntegrationTestCase.java new file mode 100644 index 0000000000..b935f19214 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/JavaBuilderContextIntegrationTestCase.java @@ -0,0 +1,240 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.integration; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.builder.JavaComponentContextBuilder; +import org.apache.tuscany.container.java.builder.JavaTargetWireBuilder; +import org.apache.tuscany.container.java.builder.MockHandlerBuilder; +import org.apache.tuscany.container.java.builder.MockInterceptorBuilder; +import org.apache.tuscany.container.java.invocation.mock.MockHandler; +import org.apache.tuscany.container.java.invocation.mock.MockSyncInterceptor; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.GenericComponent; +import org.apache.tuscany.core.builder.RuntimeConfigurationBuilder; +import org.apache.tuscany.core.builder.impl.DefaultWireBuilder; +import org.apache.tuscany.core.builder.impl.HierarchicalBuilder; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.invocation.jdk.JDKProxyFactoryFactory; +import org.apache.tuscany.core.invocation.spi.ProxyFactoryFactory; +import org.apache.tuscany.core.message.MessageFactory; +import org.apache.tuscany.core.message.impl.MessageFactoryImpl; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.core.runtime.RuntimeContextImpl; +import org.apache.tuscany.core.system.builder.SystemComponentContextBuilder; +import org.apache.tuscany.core.system.builder.SystemEntryPointBuilder; +import org.apache.tuscany.core.system.builder.SystemExternalServiceBuilder; + +/** + * Verifies that the aggregate context implementation and java component builders construct references properly + * + * @version $Rev$ $Date$ + */ +public class JavaBuilderContextIntegrationTestCase extends TestCase { + + public JavaBuilderContextIntegrationTestCase(String arg0) { + super(arg0); + } + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testRefWithSourceInterceptor() throws Exception { + MessageFactory msgFactory = new MessageFactoryImpl(); + + List<RuntimeConfigurationBuilder> builders = new ArrayList(); + builders.add((new SystemComponentContextBuilder())); + builders.add(new SystemEntryPointBuilder()); + builders.add(new SystemExternalServiceBuilder()); + + ProxyFactoryFactory proxyFactoryFactory =new JDKProxyFactoryFactory(); + + JavaComponentContextBuilder javaBuilder = new JavaComponentContextBuilder(); + javaBuilder.setMessageFactory(msgFactory); + javaBuilder.setProxyFactoryFactory(proxyFactoryFactory); + + MockSyncInterceptor mockInterceptor = new MockSyncInterceptor(); + MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, true); + //HierarchicalBuilder refBuilder = new HierarchicalBuilder(); + //refBuilder.addBuilder(interceptorBuilder); + javaBuilder.addPolicyBuilder(interceptorBuilder); + builders.add(javaBuilder); + + DefaultWireBuilder defaultWireBuilder = new DefaultWireBuilder(); + + RuntimeContext runtime = new RuntimeContextImpl(null, null, builders, defaultWireBuilder); + runtime.addBuilder(new JavaTargetWireBuilder()); + runtime.start(); + runtime.getRootContext().registerModelObject( + MockFactory.createAggregateComponent("test.module")); + AggregateContext child = (AggregateContext) runtime.getRootContext().getContext("test.module"); + child.registerModelObject(MockFactory.createModule()); + child.fireEvent(EventContext.MODULE_START, null); + GenericComponent source = (GenericComponent) child.locateInstance("source"); + Assert.assertNotNull(source); + source.getGenericComponent().getString(); + Assert.assertEquals(1, mockInterceptor.getCount()); + source.getGenericComponent().getString(); + Assert.assertEquals(2, mockInterceptor.getCount()); + child.fireEvent(EventContext.MODULE_STOP, null); + runtime.stop(); + } + + public void testRefWithSourceInterceptorHandler() throws Exception { + MessageFactory msgFactory = new MessageFactoryImpl(); + + List<RuntimeConfigurationBuilder> builders = new ArrayList(); + builders.add((new SystemComponentContextBuilder())); + builders.add(new SystemEntryPointBuilder()); + builders.add(new SystemExternalServiceBuilder()); + + JavaComponentContextBuilder javaBuilder = new JavaComponentContextBuilder(); + javaBuilder.setMessageFactory(msgFactory); + javaBuilder.setProxyFactoryFactory(new JDKProxyFactoryFactory()); + + MockSyncInterceptor mockInterceptor = new MockSyncInterceptor(); + MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, true); + //HierarchicalBuilder refBuilder = new HierarchicalBuilder(); + //refBuilder.addBuilder(interceptorBuilder); + MockHandler mockHandler = new MockHandler(); + MockHandlerBuilder handlerBuilder = new MockHandlerBuilder(mockHandler, true, true); + //refBuilder.addBuilder(handlerBuilder); + javaBuilder.addPolicyBuilder(interceptorBuilder); + javaBuilder.addPolicyBuilder(handlerBuilder); + + //javaBuilder.setPolicyBuilder(refBuilder); + builders.add(javaBuilder); + + DefaultWireBuilder defaultWireBuilder = new DefaultWireBuilder(); + RuntimeContext runtime = new RuntimeContextImpl(null, null, builders, defaultWireBuilder); + runtime.addBuilder(new JavaTargetWireBuilder()); + runtime.start(); + runtime.getRootContext().registerModelObject( + MockFactory.createAggregateComponent("test.module")); + AggregateContext child = (AggregateContext) runtime.getRootContext().getContext("test.module"); + child.registerModelObject(MockFactory.createModule()); + child.fireEvent(EventContext.MODULE_START, null); + GenericComponent source = (GenericComponent) child.locateInstance("source"); + Assert.assertNotNull(source); + source.getGenericComponent().getString(); + Assert.assertEquals(1, mockInterceptor.getCount()); + Assert.assertEquals(1, mockHandler.getCount()); + source.getGenericComponent().getString(); + Assert.assertEquals(2, mockInterceptor.getCount()); + Assert.assertEquals(2, mockHandler.getCount()); + child.fireEvent(EventContext.MODULE_STOP, null); + runtime.stop(); + } + + public void testRefWithTargetInterceptorHandler() throws Exception { + MessageFactory msgFactory = new MessageFactoryImpl(); + + List<RuntimeConfigurationBuilder> builders = new ArrayList(); + builders.add((new SystemComponentContextBuilder())); + builders.add(new SystemEntryPointBuilder()); + builders.add(new SystemExternalServiceBuilder()); + + JavaComponentContextBuilder javaBuilder = new JavaComponentContextBuilder(); + javaBuilder.setMessageFactory(msgFactory); + javaBuilder.setProxyFactoryFactory(new JDKProxyFactoryFactory()); + + MockSyncInterceptor mockInterceptor = new MockSyncInterceptor(); + MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false); + //HierarchicalBuilder refBuilder = new HierarchicalBuilder(); + //refBuilder.addBuilder(interceptorBuilder); + MockHandler mockHandler = new MockHandler(); + MockHandlerBuilder handlerBuilder = new MockHandlerBuilder(mockHandler, false, true); + //refBuilder.addBuilder(handlerBuilder); + javaBuilder.addPolicyBuilder(interceptorBuilder); + javaBuilder.addPolicyBuilder(handlerBuilder); + + // javaBuilder.setPolicyBuilder(refBuilder); + builders.add(javaBuilder); + + DefaultWireBuilder defaultWireBuilder = new DefaultWireBuilder(); + + RuntimeContext runtime = new RuntimeContextImpl(null, null, builders, defaultWireBuilder); + runtime.addBuilder(new JavaTargetWireBuilder()); + runtime.start(); + runtime.getRootContext().registerModelObject( + MockFactory.createAggregateComponent("test.module")); + AggregateContext child = (AggregateContext) runtime.getRootContext().getContext("test.module"); + child.registerModelObject(MockFactory.createModule()); + child.fireEvent(EventContext.MODULE_START, null); + GenericComponent source = (GenericComponent) child.locateInstance("source"); + Assert.assertNotNull(source); + source.getGenericComponent().getString(); + Assert.assertEquals(1, mockInterceptor.getCount()); + Assert.assertEquals(1, mockHandler.getCount()); + source.getGenericComponent().getString(); + Assert.assertEquals(2, mockInterceptor.getCount()); + Assert.assertEquals(2, mockHandler.getCount()); + child.fireEvent(EventContext.MODULE_STOP, null); + runtime.stop(); + } + + public void testRefWithTargetInterceptor() throws Exception { + MessageFactory msgFactory = new MessageFactoryImpl(); + + List<RuntimeConfigurationBuilder> builders = new ArrayList(); + builders.add((new SystemComponentContextBuilder())); + builders.add(new SystemEntryPointBuilder()); + builders.add(new SystemExternalServiceBuilder()); + + JavaComponentContextBuilder javaBuilder = new JavaComponentContextBuilder(); + javaBuilder.setMessageFactory(msgFactory); + javaBuilder.setProxyFactoryFactory(new JDKProxyFactoryFactory()); + + MockSyncInterceptor mockInterceptor = new MockSyncInterceptor(); + MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false); + //HierarchicalBuilder refBuilder = new HierarchicalBuilder(); + //refBuilder.addBuilder(interceptorBuilder); + javaBuilder.addPolicyBuilder(interceptorBuilder); + + //javaBuilder.setPolicyBuilder(refBuilder); + builders.add(javaBuilder); + + DefaultWireBuilder defaultWireBuilder = new DefaultWireBuilder(); + + RuntimeContext runtime = new RuntimeContextImpl(null, null, builders, defaultWireBuilder); + runtime.addBuilder(new JavaTargetWireBuilder()); + + runtime.start(); + runtime.getRootContext().registerModelObject( + MockFactory.createAggregateComponent("test.module")); + AggregateContext child = (AggregateContext) runtime.getRootContext().getContext("test.module"); + child.registerModelObject(MockFactory.createModule()); + child.fireEvent(EventContext.MODULE_START, null); + GenericComponent source = (GenericComponent) child.locateInstance("source"); + Assert.assertNotNull(source); + source.getGenericComponent().getString(); + Assert.assertEquals(1, mockInterceptor.getCount()); + source.getGenericComponent().getString(); + Assert.assertEquals(2, mockInterceptor.getCount()); + child.fireEvent(EventContext.MODULE_STOP, null); + runtime.stop(); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/JavaIntegrationTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/JavaIntegrationTestCase.java new file mode 100644 index 0000000000..795a6fe908 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/JavaIntegrationTestCase.java @@ -0,0 +1,64 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.integration; + +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.assembly.JavaAssemblyFactory; +import org.apache.tuscany.container.java.assembly.impl.JavaAssemblyFactoryImpl; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.core.runtime.RuntimeContextImpl; +import org.apache.tuscany.model.assembly.Module; +import org.apache.tuscany.model.assembly.ModuleComponent; + +/** + * Integration test that verifies container.java can be used to host components. + * + * @version $Rev$ $Date$ + */ +public class JavaIntegrationTestCase extends TestCase { + private JavaAssemblyFactory factory; + private RuntimeContext runtime; + + public void testModuleWithOneComponent() throws Exception { + Module module = factory.createModule(); + ModuleComponent moduleComponent = factory.createModuleComponent(); + moduleComponent.setModuleImplementation(module); + +// runtime.registerModelObject(moduleComponent); + } + + protected void setUp() throws Exception { + super.setUp(); + + // Create a factory for model objects + factory = new JavaAssemblyFactoryImpl(); + + // Create and bootstrap an empty Tuscany runtime + this.runtime = new RuntimeContextImpl(); + this.runtime.start(); + +// Component component = MockSystemAssemblyFactory.createComponent(RuntimeContext.SYSTEM, SystemAggregateContextImpl.class.getName(), ContextConstants.AGGREGATE_SCOPE_ENUM); +// runtime.registerModelObject(component); + } + + protected void tearDown() throws Exception { + runtime.stop(); + super.tearDown(); + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/JavaRuntimeBootstrapTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/JavaRuntimeBootstrapTestCase.java new file mode 100644 index 0000000000..d474d9cb4f --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/JavaRuntimeBootstrapTestCase.java @@ -0,0 +1,54 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.integration; + +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.GenericComponent; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.runtime.RuntimeContext; + +import junit.framework.Assert; +import junit.framework.TestCase; + +/** + * Ensures basic runtime with Java support boots properly + * + * @version $Rev$ $Date$ + */ +public class JavaRuntimeBootstrapTestCase extends TestCase { + + /** + * Tests the runtime can be bootstrapped with Java builders and two module-scoped Java-based components can be wired + */ + public void testRuntimeBoot() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext(MockFactory.SYSTEM_CHILD); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule()); + testCtx.fireEvent(EventContext.MODULE_START,null); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().getString(); + + } + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/binding/EntryPointToJavaTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/binding/EntryPointToJavaTestCase.java new file mode 100644 index 0000000000..a9408ef9ee --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/binding/EntryPointToJavaTestCase.java @@ -0,0 +1,248 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.integration.binding; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.assembly.mock.HelloWorldService; +import org.apache.tuscany.container.java.builder.MockInterceptorBuilder; +import org.apache.tuscany.container.java.invocation.mock.MockSyncInterceptor; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.binding.foo.FooBindingBuilder; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.EntryPointContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.model.assembly.Scope; + +/** + * Tests basic entry point functionality with Java components + * + * @version $Rev$ $Date$ + */ +public class EntryPointToJavaTestCase extends TestCase { + + private Method hello; + + public void setUp() throws Exception { + hello = HelloWorldService.class.getMethod("hello", new Class[] { String.class }); + } + + /** + * Tests creation and invocation of an entry point wired to a module-scoped service offered by a Java component + */ + public void testEPtoJavaModuleScopeInvoke() throws Throwable { + RuntimeContext runtime = MockFactory.registerFooBinding(MockFactory.createJavaRuntime()); + FooBindingBuilder builder = (FooBindingBuilder) ((AggregateContext) runtime.getSystemContext().getContext(MockFactory.SYSTEM_CHILD)) + .getContext(MockFactory.FOO_BUILDER).getInstance(null); + MockSyncInterceptor mockInterceptor = new MockSyncInterceptor(); + MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false); + builder.addPolicyBuilder(interceptorBuilder); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test.module")); + AggregateContext child = (AggregateContext) runtime.getRootContext().getContext("test.module"); + child.registerModelObject(MockFactory.createModuleWithEntryPoint(Scope.MODULE)); + child.fireEvent(EventContext.MODULE_START, null); + child.fireEvent(EventContext.REQUEST_START, null); + EntryPointContext ctx = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx); + InvocationHandler handler = (InvocationHandler) ctx.getImplementationInstance(); + Assert.assertEquals(0, mockInterceptor.getCount()); + Object response = handler.invoke(null, hello, new Object[] { "foo" }); + Assert.assertEquals("Hello foo", response); + Assert.assertEquals(1, mockInterceptor.getCount()); + child.fireEvent(EventContext.REQUEST_END, null); + + // second request + child.fireEvent(EventContext.REQUEST_START, null); + ctx = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx); + handler = (InvocationHandler) ctx.getImplementationInstance(); + Assert.assertEquals(1, mockInterceptor.getCount()); + response = handler.invoke(null, hello, new Object[] { "foo" }); + Assert.assertEquals("Hello foo", response); + Assert.assertEquals(2, mockInterceptor.getCount()); + HelloWorldService service1 = (HelloWorldService)child.getContext("target").getInstance(null); + Assert.assertEquals(2, service1.count()); + child.fireEvent(EventContext.REQUEST_END, null); + + child.fireEvent(EventContext.MODULE_STOP, null); + runtime.stop(); + } + + /** + * Tests creation and invocation of an entry point wired to a session-scoped service offered by a Java component + */ + public void testEPtoJavaSessionScopeInvoke() throws Throwable { + RuntimeContext runtime = MockFactory.registerFooBinding(MockFactory.createJavaRuntime()); + FooBindingBuilder builder = (FooBindingBuilder) ((AggregateContext) runtime.getSystemContext().getContext(MockFactory.SYSTEM_CHILD)) + .getContext(MockFactory.FOO_BUILDER).getInstance(null); + MockSyncInterceptor mockInterceptor = new MockSyncInterceptor(); + MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false); + builder.addPolicyBuilder(interceptorBuilder); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test.module")); + AggregateContext child = (AggregateContext) runtime.getRootContext().getContext("test.module"); + child.registerModelObject(MockFactory.createModuleWithEntryPoint(Scope.SESSION)); + child.fireEvent(EventContext.MODULE_START, null); + + // first session + Object session = new Object(); + child.fireEvent(EventContext.REQUEST_START, null); + child.fireEvent(EventContext.SESSION_NOTIFY, session); + + EntryPointContext ctx = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx); + InvocationHandler handler = (InvocationHandler) ctx.getImplementationInstance(); + Assert.assertEquals(0, mockInterceptor.getCount()); + Object response = handler.invoke(null, hello, new Object[] { "foo" }); + Assert.assertEquals("Hello foo", response); + Assert.assertEquals(1, mockInterceptor.getCount()); + child.fireEvent(EventContext.REQUEST_END, session); + + child.fireEvent(EventContext.REQUEST_START, null); + child.fireEvent(EventContext.SESSION_NOTIFY, session); + EntryPointContext ctx2 = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx2); + response = handler.invoke(null, hello, new Object[] { "foo" }); + Assert.assertEquals("Hello foo", response); + Assert.assertEquals(2, mockInterceptor.getCount()); + HelloWorldService service1 = (HelloWorldService)child.getContext("target").getInstance(null); + Assert.assertEquals(2, service1.count()); + child.fireEvent(EventContext.REQUEST_END, session); + child.fireEvent(EventContext.SESSION_END, session); + + + // second session + Object session2 = new Object(); + child.fireEvent(EventContext.REQUEST_START, null); + child.fireEvent(EventContext.SESSION_NOTIFY, session2); + + ctx = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx); + Assert.assertEquals(2, mockInterceptor.getCount()); + response = handler.invoke(null, hello, new Object[] { "foo" }); + Assert.assertEquals("Hello foo", response); + Assert.assertEquals(3, mockInterceptor.getCount()); + child.fireEvent(EventContext.REQUEST_END, session2); + + child.fireEvent(EventContext.REQUEST_START, null); + child.fireEvent(EventContext.SESSION_NOTIFY, session2); + ctx2 = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx2); + response = handler.invoke(null, hello, new Object[] { "foo" }); + Assert.assertEquals("Hello foo", response); + Assert.assertEquals(4, mockInterceptor.getCount()); + HelloWorldService service2 = (HelloWorldService)child.getContext("target").getInstance(null); + Assert.assertEquals(2, service2.count()); + Assert.assertEquals(2, service1.count()); //ensure sessions not crossed + child.fireEvent(EventContext.REQUEST_END, session2); + child.fireEvent(EventContext.SESSION_NOTIFY, session2); + + + child.fireEvent(EventContext.MODULE_STOP, null); + runtime.stop(); + } + + + /** + * Tests creation and invocation of an entry point wired to a module-scoped service offered by a Java component + */ + public void testEPtoJavaStatelessInvoke() throws Throwable { + RuntimeContext runtime = MockFactory.registerFooBinding(MockFactory.createJavaRuntime()); + FooBindingBuilder builder = (FooBindingBuilder) ((AggregateContext) runtime.getSystemContext().getContext(MockFactory.SYSTEM_CHILD)) + .getContext(MockFactory.FOO_BUILDER).getInstance(null); + MockSyncInterceptor mockInterceptor = new MockSyncInterceptor(); + MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false); + builder.addPolicyBuilder(interceptorBuilder); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test.module")); + AggregateContext child = (AggregateContext) runtime.getRootContext().getContext("test.module"); + child.registerModelObject(MockFactory.createModuleWithEntryPoint(Scope.INSTANCE)); + child.fireEvent(EventContext.MODULE_START, null); + child.fireEvent(EventContext.REQUEST_START, null); + EntryPointContext ctx = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx); + InvocationHandler handler = (InvocationHandler) ctx.getImplementationInstance(); + Assert.assertEquals(0, mockInterceptor.getCount()); + Object response = handler.invoke(null, hello, new Object[] { "foo" }); + Assert.assertEquals("Hello foo", response); + Assert.assertEquals(1, mockInterceptor.getCount()); + child.fireEvent(EventContext.REQUEST_END, null); + + // second request + child.fireEvent(EventContext.REQUEST_START, null); + ctx = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx); + handler = (InvocationHandler) ctx.getImplementationInstance(); + Assert.assertEquals(1, mockInterceptor.getCount()); + response = handler.invoke(null, hello, new Object[] { "foo" }); + Assert.assertEquals("Hello foo", response); + Assert.assertEquals(2, mockInterceptor.getCount()); + HelloWorldService service1 = (HelloWorldService)child.getContext("target").getInstance(null); + Assert.assertEquals(0, service1.count()); + child.fireEvent(EventContext.REQUEST_END, null); + + child.fireEvent(EventContext.MODULE_STOP, null); + runtime.stop(); + } + + public void testEPtoJavaRequestInvoke() throws Throwable { + RuntimeContext runtime = MockFactory.registerFooBinding(MockFactory.createJavaRuntime()); + FooBindingBuilder builder = (FooBindingBuilder) ((AggregateContext) runtime.getSystemContext().getContext(MockFactory.SYSTEM_CHILD)) + .getContext(MockFactory.FOO_BUILDER).getInstance(null); + MockSyncInterceptor mockInterceptor = new MockSyncInterceptor(); + MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false); + builder.addPolicyBuilder(interceptorBuilder); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test.module")); + AggregateContext child = (AggregateContext) runtime.getRootContext().getContext("test.module"); + child.registerModelObject(MockFactory.createModuleWithEntryPoint(Scope.REQUEST)); + child.fireEvent(EventContext.MODULE_START, null); + child.fireEvent(EventContext.REQUEST_START, null); + EntryPointContext ctx = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx); + InvocationHandler handler = (InvocationHandler) ctx.getImplementationInstance(); + Assert.assertEquals(0, mockInterceptor.getCount()); + Object response = handler.invoke(null, hello, new Object[] { "foo" }); + Assert.assertEquals("Hello foo", response); + Assert.assertEquals(1, mockInterceptor.getCount()); + + ctx = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx); + handler = (InvocationHandler) ctx.getImplementationInstance(); + response = handler.invoke(null, hello, new Object[] { "foo" }); + HelloWorldService service1 = (HelloWorldService)child.getContext("target").getInstance(null); + Assert.assertEquals(2, service1.count()); + + child.fireEvent(EventContext.REQUEST_END, null); + + // second request + child.fireEvent(EventContext.REQUEST_START, null); + ctx = (EntryPointContext) child.getContext("source"); + Assert.assertNotNull(ctx); + handler = (InvocationHandler) ctx.getImplementationInstance(); + Assert.assertEquals(2, mockInterceptor.getCount()); + response = handler.invoke(null, hello, new Object[] { "foo" }); + Assert.assertEquals("Hello foo", response); + Assert.assertEquals(3, mockInterceptor.getCount()); + HelloWorldService service2 = (HelloWorldService)child.getContext("target").getInstance(null); + Assert.assertEquals(1, service2.count()); + child.fireEvent(EventContext.REQUEST_END, null); + + child.fireEvent(EventContext.MODULE_STOP, null); + runtime.stop(); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/binding/JavaToExternalServiceTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/binding/JavaToExternalServiceTestCase.java new file mode 100644 index 0000000000..a2c88e315a --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/binding/JavaToExternalServiceTestCase.java @@ -0,0 +1,61 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.integration.binding; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.assembly.mock.HelloWorldService; +import org.apache.tuscany.container.java.builder.MockInterceptorBuilder; +import org.apache.tuscany.container.java.invocation.mock.MockSyncInterceptor; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.binding.foo.FooBindingBuilder; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.runtime.RuntimeContext; + +/** + * Tests basic Java to external service interaction + * + * @version $Rev$ $Date$ + */ +public class JavaToExternalServiceTestCase extends TestCase { + + /** + * Tests an invocation of an external service configured with the + * {@link org.apache.tuscany.container.java.mock.binding.foo.FooBinding} from a Java component + * + * @throws Exception + */ + public void testJavaToESInvoke() throws Exception { + RuntimeContext runtime = MockFactory.registerFooBinding(MockFactory.createJavaRuntime()); + FooBindingBuilder builder = (FooBindingBuilder) ((AggregateContext) runtime.getSystemContext().getContext( + MockFactory.SYSTEM_CHILD)).getContext(MockFactory.FOO_BUILDER).getInstance(null); + MockSyncInterceptor mockInterceptor = new MockSyncInterceptor(); + MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false); + builder.addPolicyBuilder(interceptorBuilder); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test.module")); + AggregateContext child = (AggregateContext) runtime.getRootContext().getContext("test.module"); + child.registerModelObject(MockFactory.createModuleWithExternalService()); + child.fireEvent(EventContext.MODULE_START, null); + HelloWorldService source = (HelloWorldService) child.locateInstance("source"); + Assert.assertNotNull(source); + Assert.assertEquals(0, mockInterceptor.getCount()); + Assert.assertEquals("foo", source.hello("foo")); + Assert.assertEquals(1, mockInterceptor.getCount()); + child.fireEvent(EventContext.MODULE_STOP, null); + runtime.stop(); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java new file mode 100644 index 0000000000..fc9bab4ca3 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java @@ -0,0 +1,709 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.integration.context; + +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.GenericComponent; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.model.assembly.Scope; + +import junit.framework.Assert; +import junit.framework.TestCase; + +/** + * Tests scoping is properly handled for service references + * + * @version $Rev$ $Date$ + */ +public class ScopeReferenceTestCase extends TestCase { + + /** + * Tests a module-to-module scoped wire is setup properly by the runtime + */ + public void testModuleToModule() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule()); + testCtx.fireEvent(EventContext.MODULE_START,null); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().getString(); + } + + /** + * Tests a module-to-session scoped wire is setup properly by the runtime + */ + public void testModuleToSession() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.MODULE,Scope.SESSION)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first session + Object session = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,session); + + //second session + Object session2 = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target2); + Assert.assertTrue(!"foo".equals(target2.getString())); + + Assert.assertTrue(!"foo".equals(source.getGenericComponent().getString())); + source.getGenericComponent().setString("bar"); + Assert.assertEquals("bar",target2.getString()); + Assert.assertEquals("bar",source.getGenericComponent().getString()); + //testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + + } + + /** + * Tests a module-to-request scoped wire is setup properly by the runtime + */ + public void testModuleToRequest() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.MODULE,Scope.REQUEST)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,null); + + //second request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target2); + Assert.assertTrue(!"foo".equals(target2.getString())); + + Assert.assertTrue(!"foo".equals(source.getGenericComponent().getString())); + source.getGenericComponent().setString("bar"); + Assert.assertEquals("bar",target2.getString()); + Assert.assertEquals("bar",source.getGenericComponent().getString()); + + } + + /** + * Tests a module-to-stateless scoped wire is setup properly by the runtime + */ + public void testModuleToStateless() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.MODULE,Scope.INSTANCE)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertTrue(!"foo".equals(target.getString())); + testCtx.fireEvent(EventContext.REQUEST_END,null); + + //second request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target2); + Assert.assertTrue(!"foo".equals(target2.getString())); + + Assert.assertTrue(!"foo".equals(source.getGenericComponent().getString())); + source.getGenericComponent().setString("bar"); + Assert.assertTrue(!"bar".equals(target2.getString())); + } + + /** + * Tests a session-to-session scoped wire is setup properly by the runtime + */ + public void testSessionToSession() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.SESSION,Scope.SESSION)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first session + Object session = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,session); + + //second session + Object session2 = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session2); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals(null,target2.getString()); + Assert.assertEquals(null,source2.getGenericComponent().getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals("baz",source2.getGenericComponent().getString()); + Assert.assertEquals("baz",target2.getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,session2); + + } + + + /** + * Tests a session-to-module scoped wire is setup properly by the runtime + */ + public void testSessionToModule() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.SESSION,Scope.MODULE)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first session + Object session = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,session); + + //second session + Object session2 = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session2); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals("foo",target2.getString()); + Assert.assertEquals("foo",source2.getGenericComponent().getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals("baz",source2.getGenericComponent().getString()); + Assert.assertEquals("baz",target2.getString()); + Assert.assertEquals("baz",target.getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,session2); + + } + + /** + * Tests a session-to-request scoped wire is setup properly by the runtime + */ + public void testSessionToRequest() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.SESSION,Scope.REQUEST)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first session + Object session = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,session); + + //second session + Object session2 = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session2); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals(null,target2.getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals("baz",target2.getString()); + Assert.assertEquals("baz",source2.getGenericComponent().getString()); + + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,session); + + } + + + /** + * Tests a session-to-stateless scoped wire is setup properly by the runtime + */ + public void testSessionToStateless() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.SESSION,Scope.INSTANCE)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first session + Object session = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals(null,target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,session); + + //second session + Object session2 = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session2); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals(null,target2.getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals(null,target2.getString()); //Note assumes no pooling + Assert.assertEquals(null,source2.getGenericComponent().getString()); + + Assert.assertEquals(null,target.getString()); //Note assumes no pooling + testCtx.fireEvent(EventContext.REQUEST_END,session); + + } + + /** + * Tests a request-to-request scoped wire is setup properly by the runtime + */ + public void testRequestToRequest() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.REQUEST,Scope.REQUEST)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,null); + + //second request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals(null,target2.getString()); + Assert.assertEquals(null,source2.getGenericComponent().getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals("baz",source2.getGenericComponent().getString()); + Assert.assertEquals("baz",target2.getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,null); + + } + + /** + * Tests a request-to-module scoped wire is setup properly by the runtime + */ + public void testRequestToModule() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.REQUEST,Scope.MODULE)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,null); + + //second request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals("foo",target2.getString()); + Assert.assertEquals("foo",source2.getGenericComponent().getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals("baz",source2.getGenericComponent().getString()); + Assert.assertEquals("baz",target2.getString()); + Assert.assertEquals("baz",target.getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,null); + + } + + /** + * Tests a request-to-session scoped wire is setup properly by the runtime + */ + public void testRequestToSession() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.REQUEST,Scope.SESSION)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first session + Object session = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,session); + + //second request for session + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + GenericComponent targetR2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertEquals("foo",targetR2.getString()); + GenericComponent sourceR2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(sourceR2); + Assert.assertEquals("foo",sourceR2.getGenericComponent().getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,session); + + //second session + Object session2 = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session2); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals(null,target2.getString()); + Assert.assertEquals(null,source2.getGenericComponent().getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals("baz",source2.getGenericComponent().getString()); + Assert.assertEquals("baz",target2.getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,session2); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + testCtx.fireEvent(EventContext.REQUEST_END,session); + + } + + + /** + * Tests a request-to-stateless scoped wire is setup properly by the runtime + */ + public void testRequestToStateless() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.REQUEST,Scope.INSTANCE)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals(null,target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,null); + + //second request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals(null,target2.getString()); + Assert.assertEquals(null,source2.getGenericComponent().getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals(null,source2.getGenericComponent().getString()); + Assert.assertEquals(null,target2.getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,null); + + } + + + /** + * Tests a stateless-to-stateless scoped wire is setup properly by the runtime + */ + public void testStatelessToStateless() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.INSTANCE,Scope.INSTANCE)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals(null,target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,null); + + //second request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals(null,target2.getString()); + Assert.assertEquals(null,source2.getGenericComponent().getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals(null,source2.getGenericComponent().getString()); + Assert.assertEquals(null,target2.getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,null); + + } + + /** + * Tests a stateless-to-request scoped wire is setup properly by the runtime + */ + public void testStatelessToRequest() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.INSTANCE,Scope.REQUEST)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,null); + + GenericComponent targetR1 = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(targetR1); + Assert.assertEquals("foo",target.getString()); + + //second request + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals(null,target2.getString()); + Assert.assertEquals(null,source2.getGenericComponent().getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals("baz",source2.getGenericComponent().getString()); + Assert.assertEquals("baz",target2.getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,null); + + } + + /** + * Tests a stateless-to-session scoped wire is setup properly by the runtime + */ + public void testStatelessToSession() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.INSTANCE,Scope.SESSION)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + // first session + Object session = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,session); + + //second request for session + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + GenericComponent targetR2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertEquals("foo",targetR2.getString()); + GenericComponent sourceR2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(sourceR2); + Assert.assertEquals("foo",sourceR2.getGenericComponent().getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,session); + + //second session + Object session2 = new Object(); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session2); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals(null,target2.getString()); + Assert.assertEquals(null,source2.getGenericComponent().getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals("baz",source2.getGenericComponent().getString()); + Assert.assertEquals("baz",target2.getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,session2); + testCtx.fireEvent(EventContext.REQUEST_START,null); + testCtx.fireEvent(EventContext.SESSION_NOTIFY,session); + testCtx.fireEvent(EventContext.REQUEST_END,session); + + } + + + /** + * Tests a stateless-to-module scoped wire is setup properly by the runtime + */ + public void testStatelessToModule() throws Exception{ + RuntimeContext runtime = MockFactory.createJavaRuntime(); + InstanceContext ctx = runtime.getSystemContext().getContext("tuscany.system.child"); + Assert.assertNotNull(ctx); + runtime.getRootContext().registerModelObject(MockFactory.createAggregateComponent("test")); + AggregateContext testCtx = (AggregateContext) runtime.getRootContext().getContext("test"); + Assert.assertNotNull(testCtx); + testCtx.registerModelObject(MockFactory.createModule(Scope.INSTANCE,Scope.MODULE)); + testCtx.fireEvent(EventContext.MODULE_START,null); + + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source); + GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null); + Assert.assertNotNull(target); + source.getGenericComponent().setString("foo"); + Assert.assertEquals("foo",target.getString()); + testCtx.fireEvent(EventContext.REQUEST_END,null); + + //second session + testCtx.fireEvent(EventContext.REQUEST_START,null); + GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null); + Assert.assertNotNull(source2); + GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null); + + Assert.assertNotNull(target2); + Assert.assertEquals("foo",target2.getString()); + Assert.assertEquals("foo",source2.getGenericComponent().getString()); + source2.getGenericComponent().setString("baz"); + Assert.assertEquals("baz",source2.getGenericComponent().getString()); + Assert.assertEquals("baz",target2.getString()); + + testCtx.fireEvent(EventContext.REQUEST_END,null); + + } + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/MediationTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/MediationTestCase.java new file mode 100644 index 0000000000..6266f18b87 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/MediationTestCase.java @@ -0,0 +1,53 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.invocation; + +import java.lang.reflect.Method; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.invocation.mock.SimpleTargetImpl; +import org.apache.tuscany.core.message.Message; +import org.apache.tuscany.core.message.MessageFactory; +import org.apache.tuscany.core.message.impl.MessageFactoryImpl; + +/** + * Tests invoking on a different interface from the one actually implemented by the target + * + * @version $Rev$ $Date$ + */ +public class MediationTestCase extends TestCase { + + private Method hello; + + private MessageFactory msgFactory = new MessageFactoryImpl(); + + public void setUp() throws Exception { + hello = Hello.class.getMethod("hello", new Class[] { String.class }); + } + + public void testMediation() throws Exception { + StaticJavaComponentTargetInvoker invoker = new StaticJavaComponentTargetInvoker(hello, new SimpleTargetImpl()); + Message msg = msgFactory.createMessage(); + msg.setBody("foo"); + Assert.assertEquals("foo", invoker.invoke(msg).getBody()); + } + + public interface Hello { + + public String hello(String message) throws Exception; + + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/ScopedPojoInvokerTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/ScopedPojoInvokerTestCase.java new file mode 100644 index 0000000000..6f532bc76a --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/ScopedPojoInvokerTestCase.java @@ -0,0 +1,54 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.invocation; + +import java.lang.reflect.Method; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.invocation.ScopedJavaComponentInvoker; +import org.apache.tuscany.container.java.invocation.mock.SimpleTarget; +import org.apache.tuscany.container.java.mock.MockScopeContext; +import org.apache.tuscany.core.context.QualifiedName; +import org.apache.tuscany.core.context.ScopeContext; + +public class ScopedPojoInvokerTestCase extends TestCase { + + private Method echoMethod; + + public ScopedPojoInvokerTestCase() { + super(); + } + + public ScopedPojoInvokerTestCase(String arg0) { + super(arg0); + } + + public void setUp() throws Exception { + echoMethod = SimpleTarget.class.getDeclaredMethod("echo", new Class[]{String.class}); + Assert.assertNotNull(echoMethod); + } + + public void testScopedInvoke() throws Exception { + ScopeContext container = new MockScopeContext(); + ScopedJavaComponentInvoker invoker = new ScopedJavaComponentInvoker(new QualifiedName("foo"), echoMethod, container); + Object ret = invoker.invokeTarget("foo"); + Assert.assertEquals("foo", ret); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/StaticPojoInvokerTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/StaticPojoInvokerTestCase.java new file mode 100644 index 0000000000..e90b7623e8 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/StaticPojoInvokerTestCase.java @@ -0,0 +1,155 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.invocation; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.apache.tuscany.container.java.invocation.StaticJavaComponentTargetInvoker; + +import junit.framework.Assert; +import junit.framework.TestCase; + +public class StaticPojoInvokerTestCase extends TestCase { + + private Method echoMethod; + private Method arrayMethod; + private Method nullParamMethod; + private Method primitiveMethod; + private Method checkedMethod; + private Method runtimeMethod; + + public StaticPojoInvokerTestCase() { + + } + + public StaticPojoInvokerTestCase(String arg0) { + super(arg0); + } + + public void setUp() throws Exception { + echoMethod = TestBean.class.getDeclaredMethod("echo", new Class[]{String.class}); + arrayMethod = TestBean.class.getDeclaredMethod("arrayEcho", new Class[]{String[].class}); + nullParamMethod = TestBean.class.getDeclaredMethod("nullParam", (Class[]) null); + primitiveMethod = TestBean.class.getDeclaredMethod("primitiveEcho", new Class[]{Integer.TYPE}); + checkedMethod = TestBean.class.getDeclaredMethod("checkedException", (Class[]) null); + runtimeMethod = TestBean.class.getDeclaredMethod("runtimeException", (Class[]) null); + Assert.assertNotNull(echoMethod); + Assert.assertNotNull(checkedMethod); + Assert.assertNotNull(runtimeMethod); + } + + public void testObjectInvoke() throws Throwable { + TestBean bean = new TestBean(); + StaticJavaComponentTargetInvoker invoker = new StaticJavaComponentTargetInvoker(echoMethod, bean); + Object ret = invoker.invokeTarget("foo"); + Assert.assertEquals("foo", ret); + } + + public void testArrayInvoke() throws Throwable { + TestBean bean = new TestBean(); + StaticJavaComponentTargetInvoker invoker = new StaticJavaComponentTargetInvoker(arrayMethod, bean); + String[] args = new String[]{"foo", "bar"}; + Object ret = invoker.invokeTarget(new Object[]{args}); + String[] retA = (String[]) ret; + Assert.assertNotNull(retA); + Assert.assertEquals(2, retA.length); + Assert.assertEquals("foo", retA[0]); + Assert.assertEquals("bar", retA[1]); + } + + public void testNullInvoke() throws Throwable { + TestBean bean = new TestBean(); + StaticJavaComponentTargetInvoker invoker = new StaticJavaComponentTargetInvoker(nullParamMethod, bean); + Object ret = invoker.invokeTarget(null); + String retS = (String) ret; + Assert.assertEquals("foo", retS); + } + + public void testPrimitiveInvoke() throws Throwable { + TestBean bean = new TestBean(); + StaticJavaComponentTargetInvoker invoker = new StaticJavaComponentTargetInvoker(primitiveMethod, bean); + Object ret = invoker.invokeTarget(new Integer[]{new Integer(1)}); + Integer retI = (Integer) ret; + Assert.assertEquals(1, retI.intValue()); + } + + public void testInvokeCheckedException() throws Throwable { + TestBean bean = new TestBean(); + StaticJavaComponentTargetInvoker invoker = new StaticJavaComponentTargetInvoker(checkedMethod, bean); + try { + invoker.invokeTarget(null); + } catch (InvocationTargetException e) { + if (e.getCause() != null && TestException.class.equals(e.getCause().getClass())) { + return; + } + } catch (Throwable e) { + } + fail(TestException.class.getName() + " should have been thrown"); + } + + public void testInvokeRuntimeException() throws Throwable { + TestBean bean = new TestBean(); + StaticJavaComponentTargetInvoker invoker = new StaticJavaComponentTargetInvoker(runtimeMethod, bean); + try { + invoker.invokeTarget(null); + } catch (InvocationTargetException e) { + if (e.getCause() != null && e.getCause() instanceof TestRuntimeException) { + return; + } + } + fail(TestException.class.getName() + " should have been thrown"); + } + + private class TestBean { + + public String echo(String msg) throws Exception { + Assert.assertEquals("foo", msg); + return msg; + } + + public String[] arrayEcho(String[] msg) throws Exception { + Assert.assertNotNull(msg); + Assert.assertEquals(2, msg.length); + Assert.assertEquals("foo", msg[0]); + Assert.assertEquals("bar", msg[1]); + return msg; + } + + public String nullParam() throws Exception { + return "foo"; + } + + public int primitiveEcho(int i) throws Exception { + return i; + } + + public void checkedException() throws TestException { + throw new TestException(); + } + + public void runtimeException() throws TestRuntimeException { + throw new TestRuntimeException(); + } + } + + public class TestException extends Exception { + } + + public class TestRuntimeException extends RuntimeException { + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/MockHandler.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/MockHandler.java new file mode 100644 index 0000000000..da862b1bc1 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/MockHandler.java @@ -0,0 +1,25 @@ +/** + * + */ +package org.apache.tuscany.container.java.invocation.mock; + +import org.apache.tuscany.core.invocation.MessageHandler; +import org.apache.tuscany.core.message.Message; + +/** + * + */ +public class MockHandler implements MessageHandler { + + private int count =0; + + public boolean processMessage(Message message) { + //System.out.println("Invoking handler"); + count++; + return true; + } + + public int getCount(){ + return count; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/MockSyncInterceptor.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/MockSyncInterceptor.java new file mode 100644 index 0000000000..4a07f32e2c --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/MockSyncInterceptor.java @@ -0,0 +1,45 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.invocation.mock; + +import org.apache.tuscany.core.invocation.Interceptor; +import org.apache.tuscany.core.message.Message; + +public class MockSyncInterceptor implements Interceptor { + + private int count; + + private Interceptor next; + + public MockSyncInterceptor() { + } + + public Message invoke(Message msg) { + ++count; + //System.out.println("Invoking interceptor"); + return next.invoke(msg); + } + + public int getCount() { + return count; + } + + public void setNext(Interceptor next) { + this.next=next; + } +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleSource.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleSource.java new file mode 100644 index 0000000000..213caab7e9 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleSource.java @@ -0,0 +1,25 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.invocation.mock; + +public interface SimpleSource { + + public void invokeHello() throws Exception; + + public void invokeGoodbye() throws Exception; +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleSourceImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleSourceImpl.java new file mode 100644 index 0000000000..2e7bd75f11 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleSourceImpl.java @@ -0,0 +1,36 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.invocation.mock; + +public class SimpleSourceImpl implements SimpleSource { + + private SimpleTarget proxy; + + public SimpleSourceImpl(SimpleTarget proxy) { + this.proxy = proxy; + } + + public void invokeHello() throws Exception { + proxy.hello("hello"); + } + + public void invokeGoodbye() throws Exception { + proxy.goodbye("hello"); + } + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleTarget.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleTarget.java new file mode 100644 index 0000000000..cbd3037529 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleTarget.java @@ -0,0 +1,28 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.invocation.mock; + +public interface SimpleTarget { + + public String hello(String message) throws Exception; + + public String goodbye(String message) throws Exception; + + public String echo(String message) throws Exception; + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleTargetImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleTargetImpl.java new file mode 100644 index 0000000000..18abf6108a --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/invocation/mock/SimpleTargetImpl.java @@ -0,0 +1,39 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.invocation.mock; + +public class SimpleTargetImpl implements SimpleTarget { + + public SimpleTargetImpl() { + super(); + } + + public String hello(String message) throws Exception { + return message; + } + + public String goodbye(String message) throws Exception { + return message; + } + + public String echo(String message) throws Exception { + return message; + } + + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockConfigContext.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockConfigContext.java new file mode 100644 index 0000000000..413f4a5a5b --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockConfigContext.java @@ -0,0 +1,56 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.mock; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.core.builder.BuilderConfigException; +import org.apache.tuscany.core.builder.RuntimeConfigurationBuilder; +import org.apache.tuscany.core.builder.impl.AssemblyVisitor; +import org.apache.tuscany.core.config.ConfigurationException; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.ConfigurationContext; +import org.apache.tuscany.core.context.ScopeContext; +import org.apache.tuscany.core.invocation.spi.ProxyFactory; +import org.apache.tuscany.model.assembly.Extensible; + +/** + * A mock configuration context + * + * @version $Rev: 368822 $ $Date: 2006-01-13 10:54:38 -0800 (Fri, 13 Jan 2006) $ + */ +public class MockConfigContext implements ConfigurationContext { + + private List<RuntimeConfigurationBuilder> builders = new ArrayList(); + + public MockConfigContext(List<RuntimeConfigurationBuilder> builders) { + this.builders=builders; + } + + public void configure(Extensible model) throws ConfigurationException { + } + + public void build(AggregateContext parent, Extensible model) throws BuilderConfigException { + AssemblyVisitor visitor = new AssemblyVisitor(parent, builders); + visitor.start(model); + } + + public void wire(ProxyFactory sourceFactory, ProxyFactory targetFactory, Class targetType, boolean downScope, ScopeContext targetScopeContext) throws BuilderConfigException { + } + + public void wire(ProxyFactory targetFactory, Class targetType, ScopeContext targetScopeContext) throws BuilderConfigException { + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java new file mode 100644 index 0000000000..192ce328fb --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java @@ -0,0 +1,548 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import junit.framework.Assert; + +import org.apache.tuscany.container.java.assembly.JavaAssemblyFactory; +import org.apache.tuscany.container.java.assembly.JavaImplementation; +import org.apache.tuscany.container.java.assembly.impl.JavaAssemblyFactoryImpl; +import org.apache.tuscany.container.java.assembly.mock.HelloWorldImpl; +import org.apache.tuscany.container.java.assembly.mock.HelloWorldService; +import org.apache.tuscany.container.java.builder.JavaComponentContextBuilder; +import org.apache.tuscany.container.java.builder.JavaTargetWireBuilder; +import org.apache.tuscany.container.java.context.JavaComponentContext; +import org.apache.tuscany.container.java.invocation.mock.MockSyncInterceptor; +import org.apache.tuscany.container.java.mock.binding.foo.FooBinding; +import org.apache.tuscany.container.java.mock.binding.foo.FooBindingBuilder; +import org.apache.tuscany.container.java.mock.binding.foo.FooBindingWireBuilder; +import org.apache.tuscany.container.java.mock.components.GenericComponent; +import org.apache.tuscany.container.java.mock.components.HelloWorldClient; +import org.apache.tuscany.container.java.mock.components.ModuleScopeComponentImpl; +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.RuntimeConfiguration; +import org.apache.tuscany.core.builder.RuntimeConfigurationBuilder; +import org.apache.tuscany.core.config.ConfigurationException; +import org.apache.tuscany.core.config.JavaIntrospectionHelper; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.SystemAggregateContext; +import org.apache.tuscany.core.context.impl.AggregateContextImpl; +import org.apache.tuscany.core.injection.EventInvoker; +import org.apache.tuscany.core.injection.FieldInjector; +import org.apache.tuscany.core.injection.Injector; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.MethodInjector; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.apache.tuscany.core.injection.SingletonObjectFactory; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.core.runtime.RuntimeContextImpl; +import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory; +import org.apache.tuscany.core.system.assembly.SystemBinding; +import org.apache.tuscany.core.system.assembly.SystemImplementation; +import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl; +import org.apache.tuscany.core.system.builder.SystemComponentContextBuilder; +import org.apache.tuscany.core.system.builder.SystemEntryPointBuilder; +import org.apache.tuscany.core.system.builder.SystemExternalServiceBuilder; +import org.apache.tuscany.core.system.context.SystemAggregateContextImpl; +import org.apache.tuscany.model.assembly.AggregatePart; +import org.apache.tuscany.model.assembly.AssemblyModelContext; +import org.apache.tuscany.model.assembly.Component; +import org.apache.tuscany.model.assembly.ConfiguredReference; +import org.apache.tuscany.model.assembly.ConfiguredService; +import org.apache.tuscany.model.assembly.EntryPoint; +import org.apache.tuscany.model.assembly.ExternalService; +import org.apache.tuscany.model.assembly.Module; +import org.apache.tuscany.model.assembly.Reference; +import org.apache.tuscany.model.assembly.Scope; +import org.apache.tuscany.model.assembly.Service; +import org.apache.tuscany.model.assembly.SimpleComponent; +import org.apache.tuscany.model.assembly.impl.AssemblyModelContextImpl; +import org.apache.tuscany.model.types.java.JavaServiceContract; +import org.osoa.sca.annotations.ComponentName; +import org.osoa.sca.annotations.Context; +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Init; + +/** + * Generates test components, modules, and runtime artifacts + * + * @version $Rev$ $Date$ + */ +public class MockFactory { + + public static final String JAVA_BUILDER = "java.runtime.builder"; + + public static final String JAVA_WIRE_BUILDER = "java.wire.builder"; + + public static final String FOO_BUILDER = "foo.binding.builder"; + + public static final String FOO_WIRE_BUILDER = "foo.binding.wire.builder"; + + public static final String SYSTEM_CHILD = "tuscany.system.child"; + + private static JavaAssemblyFactory factory = new JavaAssemblyFactoryImpl(); + + private static SystemAssemblyFactory systemFactory = new SystemAssemblyFactoryImpl(); + + private static AssemblyModelContext assemblyContext = new AssemblyModelContextImpl(null, null); + + /** + * Creates an initialized simple component + * + * @param name the component name + * @param type the implementation type + * @param scope the component scope + */ + public static SimpleComponent createComponent(String name, Class type, Scope scope) { + SimpleComponent sc = factory.createSimpleComponent(); + JavaImplementation impl = factory.createJavaImplementation(); + impl.setComponentType(factory.createComponentType()); + impl.setImplementationClass(type); + sc.setComponentImplementation(impl); + Service s = factory.createService(); + JavaServiceContract ji = factory.createJavaServiceContract(); + s.setServiceContract(ji); + ji.setScope(scope); + impl.getComponentType().getServices().add(s); + sc.setName(name); + sc.setComponentImplementation(impl); + return sc; + } + + /** + * Creates a system component of the given type with the given name and scope + */ + public static Component createSystemComponent(String name, Class type, Scope scope) { + + Component sc = null; + if (AggregateContext.class.isAssignableFrom(type)) { + sc = systemFactory.createModuleComponent(); + } else { + sc = systemFactory.createSimpleComponent(); + } + SystemImplementation impl = systemFactory.createSystemImplementation(); + impl.setImplementationClass(type); + sc.setComponentImplementation(impl); + Service s = systemFactory.createService(); + JavaServiceContract ji = systemFactory.createJavaServiceContract(); + s.setServiceContract(ji); + ji.setScope(scope); + impl.setComponentType(systemFactory.createComponentType()); + impl.getComponentType().getServices().add(s); + sc.setName(name); + sc.setComponentImplementation(impl); + return sc; + } + + /** + * Creates an aggregate component with the given name + */ + public static Component createAggregateComponent(String name) { + Component sc = sc = systemFactory.createModuleComponent(); + SystemImplementation impl = systemFactory.createSystemImplementation(); + impl.setImplementationClass(AggregateContextImpl.class); + sc.setComponentImplementation(impl); + Service s = systemFactory.createService(); + JavaServiceContract ji = systemFactory.createJavaServiceContract(); + s.setServiceContract(ji); + ji.setScope(Scope.AGGREGATE); + impl.setComponentType(systemFactory.createComponentType()); + impl.getComponentType().getServices().add(s); + sc.setName(name); + sc.setComponentImplementation(impl); + return sc; + } + + /** + * Creates a system aggregate component with the given name + */ + public static Component createSystemAggregateComponent(String name) { + Component sc = sc = systemFactory.createModuleComponent(); + SystemImplementation impl = systemFactory.createSystemImplementation(); + impl.setImplementationClass(SystemAggregateContextImpl.class); + sc.setComponentImplementation(impl); + Service s = systemFactory.createService(); + JavaServiceContract ji = systemFactory.createJavaServiceContract(); + s.setServiceContract(ji); + ji.setScope(Scope.AGGREGATE); + impl.setComponentType(systemFactory.createComponentType()); + impl.getComponentType().getServices().add(s); + sc.setName(name); + sc.setComponentImplementation(impl); + return sc; + } + + /** + * Creates an external service configured with the 'Foo' test binding + */ + public static ExternalService createFooBindingExternalService(String name, Class interfaz) { + ExternalService es = factory.createExternalService(); + es.setName(name); + Service s = factory.createService(); + JavaServiceContract ji = factory.createJavaServiceContract(); + ji.setScope(Scope.MODULE); + ji.setInterface(interfaz); + s.setServiceContract(ji); + ConfiguredService configuredService = factory.createConfiguredService(); + es.setConfiguredService(configuredService); + + FooBinding binding = new FooBinding(); + es.getBindings().add(binding); + return es; + } + + /** + * Creates an entry point with the given name configured with the given interface and the {@link FooBinding} + */ + public static EntryPoint createFooBindingEntryPoint(String name, Class interfaz) { + EntryPoint ep = factory.createEntryPoint(); + ep.setName(name); + Service s = factory.createService(); + JavaServiceContract ji = factory.createJavaServiceContract(); + ji.setScope(Scope.MODULE); + ji.setInterface(interfaz); + s.setServiceContract(ji); + ConfiguredService configuredService = factory.createConfiguredService(); + configuredService.setService(s); + ep.setConfiguredService(configuredService); + FooBinding binding = new FooBinding(); + ep.getBindings().add(binding); + return ep; + } + + /** + * Creates an entry point wired to the given target (e.g. component, external service) using the system binding + * + * @param name the name of the entry point + * @param interfaz the inteface exposed by the entry point + * @param refName the name of the entry point reference + * @param target the target the entry point is wired to + */ + public static EntryPoint createEPSystemBinding(String name, Class interfaz, String refName, AggregatePart target) { + JavaServiceContract contract = systemFactory.createJavaServiceContract(); + contract.setInterface(interfaz); + + EntryPoint ep = systemFactory.createEntryPoint(); + ep.setName(name); + + Reference ref = systemFactory.createReference(); + ref.setName(refName); + ref.setServiceContract(contract); + ConfiguredReference configuredReference = systemFactory.createConfiguredReference(); + configuredReference.setReference(ref); + Service service = systemFactory.createService(); + service.setServiceContract(contract); + + ConfiguredService cService = systemFactory.createConfiguredService(); + cService.setService(service); + cService.initialize(MockFactory.assemblyContext); + + configuredReference.getTargetConfiguredServices().add(cService); + ep.setConfiguredReference(configuredReference); + + Service epService = systemFactory.createService(); + epService.setServiceContract(contract); + + ConfiguredService epCService = systemFactory.createConfiguredService(); + epCService.initialize(MockFactory.assemblyContext); + epCService.setService(epService); + + ep.setConfiguredService(epCService); + SystemBinding binding = systemFactory.createSystemBinding(); + ep.getBindings().add(binding); + if (target != null) { + if (target instanceof Component) { + ((Component) target).getConfiguredServices().add(cService); + } else if (target instanceof ExternalService) { + ((ExternalService) target).setConfiguredService(cService); + } + target.initialize(MockFactory.assemblyContext); + } + ep.initialize(null); + return ep; + } + + /** + * Creates an external service configured with a {@link SystemBinding} + */ + public static ExternalService createESSystemBinding(String name, String refName) { + ExternalService es = systemFactory.createExternalService(); + es.setName(name); + ConfiguredService configuredService = systemFactory.createConfiguredService(); + es.setConfiguredService(configuredService); + SystemBinding binding = systemFactory.createSystemBinding(); + binding.setTargetName(refName); + es.getBindings().add(binding); + es.initialize(null); + return es; + } + + /** + * Creates a module with a Java-based "target" module-scoped component wired to a module-scoped "source" + */ + public static Module createModule() { + return createModule(Scope.MODULE, Scope.MODULE); + } + + /** + * Creates a module with a Java-based "target" component wired to a "source" + */ + public static Module createModule(Scope sourceScope, Scope targetScope) { + Component sourceComponent = createComponent("source", ModuleScopeComponentImpl.class, sourceScope); + Component targetComponent = createComponent("target", ModuleScopeComponentImpl.class, targetScope); + + Service targetService = factory.createService(); + JavaServiceContract targetContract = factory.createJavaServiceContract(); + targetContract.setInterface(GenericComponent.class); + targetService.setServiceContract(targetContract); + targetService.setName("GenericComponent"); + ConfiguredService cTargetService = factory.createConfiguredService(); + cTargetService.setService(targetService); + cTargetService.initialize(assemblyContext); + targetComponent.getConfiguredServices().add(cTargetService); + targetComponent.initialize(assemblyContext); + + Reference ref = factory.createReference(); + ConfiguredReference cref = factory.createConfiguredReference(); + ref.setName("setGenericComponent"); + JavaServiceContract inter = factory.createJavaServiceContract(); + inter.setInterface(GenericComponent.class); + ref.setServiceContract(inter); + cref.setReference(ref); + cref.getTargetConfiguredServices().add(cTargetService); + cref.initialize(assemblyContext); + sourceComponent.getConfiguredReferences().add(cref); + sourceComponent.initialize(assemblyContext); + + Module module = factory.createModule(); + module.setName("test.module"); + module.getComponents().add(sourceComponent); + module.getComponents().add(targetComponent); + module.initialize(assemblyContext); + return module; + } + + /** + * Creates a module with a Java-based source component wired to a "target" external service configured with the + * {@link FooBinding} + */ + public static Module createModuleWithExternalService() { + Component sourceComponent = createComponent("source", HelloWorldClient.class, Scope.MODULE); + ExternalService targetES = createFooBindingExternalService("target", HelloWorldService.class); + + Service targetService = factory.createService(); + JavaServiceContract targetContract = factory.createJavaServiceContract(); + targetContract.setInterface(HelloWorldService.class); + targetService.setServiceContract(targetContract); + targetService.setName("HelloWorld"); + ConfiguredService cTargetService = factory.createConfiguredService(); + cTargetService.setService(targetService); + targetES.setConfiguredService(cTargetService); + targetES.initialize(assemblyContext); + + Reference ref = factory.createReference(); + ConfiguredReference cref = factory.createConfiguredReference(); + ref.setName("setHelloWorldService"); + JavaServiceContract inter = factory.createJavaServiceContract(); + inter.setInterface(HelloWorldService.class); + ref.setServiceContract(inter); + cref.setReference(ref); + cref.getTargetConfiguredServices().add(cTargetService); + cref.initialize(assemblyContext); + sourceComponent.getConfiguredReferences().add(cref); + sourceComponent.initialize(assemblyContext); + + Module module = factory.createModule(); + module.setName("test.module"); + module.getComponents().add(sourceComponent); + module.getExternalServices().add(targetES); + module.initialize(assemblyContext); + return module; + } + + /** + * Creates a module with an entry point named "source" configured with the {@link FooBinding} wired to a service + * offered by a Java-based component named "target" + * + * @param scope the scope of the target service + */ + public static Module createModuleWithEntryPoint(Scope scope) { + EntryPoint sourceEP = createFooBindingEntryPoint("source", HelloWorldService.class); + Component targetComponent = createComponent("target", HelloWorldImpl.class, scope); + + Service targetService = factory.createService(); + JavaServiceContract targetContract = factory.createJavaServiceContract(); + targetContract.setInterface(HelloWorldService.class); + targetService.setServiceContract(targetContract); + targetService.setName("HelloWorldService"); + ConfiguredService cTargetService = factory.createConfiguredService(); + cTargetService.setService(targetService); + targetComponent.getConfiguredServices().add(cTargetService); + targetComponent.initialize(assemblyContext); + + Reference ref = factory.createReference(); + ConfiguredReference cref = factory.createConfiguredReference(); + ref.setName("setHelloWorldService"); + JavaServiceContract inter = factory.createJavaServiceContract(); + inter.setInterface(HelloWorldService.class); + ref.setServiceContract(inter); + cref.setReference(ref); + cref.getTargetConfiguredServices().add(cTargetService); + cref.initialize(assemblyContext); + sourceEP.setConfiguredReference(cref); + sourceEP.getConfiguredService().getService().setName("HelloWorldService"); + sourceEP.initialize(assemblyContext); + + Module module = factory.createModule(); + module.setName("test.module"); + module.getEntryPoints().add(sourceEP); + module.getComponents().add(targetComponent); + module.initialize(assemblyContext); + return module; + } + + /** + * Returns a collection of bootstrap configuration builders + */ + public static List<RuntimeConfigurationBuilder> createSystemBuilders() { + List<RuntimeConfigurationBuilder> builders = new ArrayList(); + builders.add((new SystemComponentContextBuilder())); + builders.add(new SystemEntryPointBuilder()); + builders.add(new SystemExternalServiceBuilder()); + return builders; + } + + /** + * Creates an aggregate runtime configuration + * + * @param name the name of the component + * @param aggregateContext the containing aggregate context + * @throws BuilderException + * @see RuntimeConfiguration + */ + public static RuntimeConfiguration<InstanceContext> createAggregateConfiguration(String name, + AggregateContext aggregateContext) throws BuilderException { + + Component sc = createAggregateComponent(name); + SystemComponentContextBuilder builder = new SystemComponentContextBuilder(); + builder.build(sc, aggregateContext); + return (RuntimeConfiguration<InstanceContext>) sc.getComponentImplementation().getRuntimeConfiguration(); + } + + /** + * Creates a Java POJO component context + * + * @param name the name of the context + * @param implType the POJO class + * @param scope the component scope + * @param moduleComponentContext the containing aggregate context + * @throws NoSuchMethodException if the POJO does not have a default noi-args constructor + */ + public static JavaComponentContext createPojoContext(String name, Class implType, Scope scope, + AggregateContext moduleComponentContext) throws NoSuchMethodException { + SimpleComponent component = createComponent(name, implType, scope); + + Set<Field> fields = JavaIntrospectionHelper.getAllFields(implType); + Set<Method> methods = JavaIntrospectionHelper.getAllUniqueMethods(implType); + List<Injector> injectors = new ArrayList(); + EventInvoker initInvoker = null; + boolean eagerInit = false; + EventInvoker destroyInvoker = null; + for (Field field : fields) { + ComponentName compName = field.getAnnotation(ComponentName.class); + if (compName != null) { + Injector injector = new FieldInjector(field, new SingletonObjectFactory(name)); + injectors.add(injector); + } + Context context = field.getAnnotation(Context.class); + if (context != null) { + Injector injector = new FieldInjector(field, new SingletonObjectFactory(moduleComponentContext)); + injectors.add(injector); + } + } + for (Method method : methods) { + // FIXME Java5 + Init init = method.getAnnotation(Init.class); + if (init != null && initInvoker == null) { + initInvoker = new MethodEventInvoker(method); + eagerInit = init.eager(); + continue; + } + Destroy destroy = method.getAnnotation(Destroy.class); + if (destroy != null && destroyInvoker == null) { + destroyInvoker = new MethodEventInvoker(method); + continue; + } + ComponentName compName = method.getAnnotation(ComponentName.class); + if (compName != null) { + Injector injector = new MethodInjector(method, new SingletonObjectFactory(name)); + injectors.add(injector); + } + Context context = method.getAnnotation(Context.class); + if (context != null) { + Injector injector = new MethodInjector(method, new SingletonObjectFactory(moduleComponentContext)); + injectors.add(injector); + } + } + boolean stateless = (scope == Scope.INSTANCE); + JavaComponentContext context = new JavaComponentContext("foo", new PojoObjectFactory(JavaIntrospectionHelper + .getDefaultConstructor(implType), null, injectors), eagerInit, initInvoker, destroyInvoker, stateless); + return context; + } + + /** + * Creates a default {@link RuntimeContext} configured with support for Java component implementations + * + * @throws ConfigurationException + */ + public static RuntimeContext createJavaRuntime() throws ConfigurationException { + RuntimeContext runtime = new RuntimeContextImpl(null, null, MockFactory.createSystemBuilders(), null); + runtime.start(); + runtime.getSystemContext().registerModelObject(createSystemAggregateComponent(SYSTEM_CHILD)); + SystemAggregateContext ctx = (SystemAggregateContext) runtime.getSystemContext().getContext(SYSTEM_CHILD); + ctx.registerModelObject(createSystemComponent(JAVA_BUILDER, JavaComponentContextBuilder.class, Scope.MODULE)); + ctx.registerModelObject(createSystemComponent(JAVA_WIRE_BUILDER, JavaTargetWireBuilder.class, Scope.MODULE)); + ctx.fireEvent(EventContext.MODULE_START, null); + return runtime; + } + + /** + * Registers the {@link FooBinding} builders with a given runtime + * + * @throws ConfigurationException + */ + public static RuntimeContext registerFooBinding(RuntimeContext runtime) throws ConfigurationException { + AggregateContext child = (AggregateContext) runtime.getSystemContext().getContext(MockFactory.SYSTEM_CHILD); + JavaComponentContextBuilder javaBuilder = (JavaComponentContextBuilder) child.getContext(MockFactory.JAVA_BUILDER) + .getInstance(null); + MockSyncInterceptor mockInterceptor = new MockSyncInterceptor(); + child.registerModelObject(MockFactory.createSystemComponent(FOO_BUILDER, FooBindingBuilder.class, Scope.MODULE)); + child.registerModelObject(MockFactory.createSystemComponent(FOO_WIRE_BUILDER, FooBindingWireBuilder.class, Scope.MODULE)); + // since the child context is already started, we need to manually retrieve the components to init them + Assert.assertNotNull(child.getContext(FOO_BUILDER).getInstance(null)); + Assert.assertNotNull(child.getContext(FOO_WIRE_BUILDER).getInstance(null)); + return runtime; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockScopeContext.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockScopeContext.java new file mode 100644 index 0000000000..a6d9c0b2c5 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockScopeContext.java @@ -0,0 +1,137 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.container.java.invocation.mock.SimpleTargetImpl; +import org.apache.tuscany.core.builder.RuntimeConfiguration; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.EventException; +import org.apache.tuscany.core.context.QualifiedName; +import org.apache.tuscany.core.context.LifecycleEventListener; +import org.apache.tuscany.core.context.ScopeContext; +import org.apache.tuscany.core.context.ScopeRuntimeException; +import org.apache.tuscany.core.context.SimpleComponentContext; +import org.apache.tuscany.core.context.TargetException; +import org.apache.tuscany.model.assembly.SimpleComponent; + +public class MockScopeContext implements ScopeContext { + + Map<String, Object> components; + + public MockScopeContext() { + components = new HashMap(); + components.put("foo", new SimpleTargetImpl()); + components.put("bar", new SimpleTargetImpl()); + } + + public MockScopeContext(Map<String,Object> instances) { + components = instances; + } + + + public void start() { + } + + public void stop() { + } + + public String getName() { + return "Mock Scope Container"; + } + + public boolean isCacheable() { + return false; + } + + public int[] getEventTypes() { + return null; + } + + public SimpleComponentContext getContext(String name) { + return null; + } + + public Object getInstance(QualifiedName name) throws ScopeRuntimeException { + return components.get(name.getPartName()); + } + + public Object getInstance(QualifiedName componentName, boolean notify) throws TargetException { + return getInstance(componentName); + } + + public SimpleComponentContext getContextByKey(String name, Object key) { + return null; + } + + public void setComponent(SimpleComponent component) throws ScopeRuntimeException { + } + + public void removeContext(String name) throws ScopeRuntimeException { + } + + public void removeContextByKey(String name, Object key) throws ScopeRuntimeException { + } + + public SimpleComponent[] getComponents() { + return null; + } + + public void onEvent(int type, Object message) throws EventException { + } + + + public void registerConfigurations(List<RuntimeConfiguration<InstanceContext>> configurations) { + } + + public void registerConfiguration(RuntimeConfiguration<InstanceContext> configuration) { + } + + public int getLifecycleState(){ + return RUNNING; + } + + + public void setLifecycleState(int state) { + } + + + public void setName(String name) { + } + + + public void addContextListener(LifecycleEventListener listener) { + } + + + public void removeContextListener(LifecycleEventListener listener) { + } + + public Object getImplementationInstance() throws TargetException{ + return this; + } + + public Object getImplementationInstance(boolean notify) throws TargetException{ + return this; + } + + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBinding.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBinding.java new file mode 100644 index 0000000000..75aabff105 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBinding.java @@ -0,0 +1,59 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.mock.binding.foo; + +import org.apache.tuscany.model.assembly.AssemblyModelContext; +import org.apache.tuscany.model.assembly.AssemblyModelVisitor; +import org.apache.tuscany.model.assembly.Binding; +import org.apache.tuscany.model.assembly.ModelInitException; + +/** + * Represents a mock binding that echoes back a single parameter + * + * @version $Rev$ $Date$ + */ +public class FooBinding implements Binding { + + public FooBinding() { + } + + public String getURI() { + return null; + } + + public void setURI(String value) { + } + + public void initialize(AssemblyModelContext modelContext) throws ModelInitException { + } + + public void freeze() { + } + + public boolean accept(AssemblyModelVisitor visitor) { + return false; + } + + private Object config; + + public void setRuntimeConfiguration(Object configuration) { + config = configuration; + } + + public Object getRuntimeConfiguration() { + System.out.println("retting"); + return config; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingBuilder.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingBuilder.java new file mode 100644 index 0000000000..248f7e54bc --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingBuilder.java @@ -0,0 +1,192 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.mock.binding.foo; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.ObjectFactory; +import org.apache.tuscany.core.builder.RuntimeConfigurationBuilder; +import org.apache.tuscany.core.builder.impl.EntryPointRuntimeConfiguration; +import org.apache.tuscany.core.builder.impl.HierarchicalBuilder; +import org.apache.tuscany.core.config.JavaIntrospectionHelper; +import org.apache.tuscany.core.context.Context; +import org.apache.tuscany.core.context.QualifiedName; +import org.apache.tuscany.core.injection.ObjectCreationException; +import org.apache.tuscany.core.invocation.InvocationConfiguration; +import org.apache.tuscany.core.invocation.ProxyConfiguration; +import org.apache.tuscany.core.invocation.impl.InvokerInterceptor; +import org.apache.tuscany.core.invocation.spi.ProxyFactory; +import org.apache.tuscany.core.invocation.spi.ProxyFactoryFactory; +import org.apache.tuscany.core.message.MessageFactory; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.core.system.annotation.Autowire; +import org.apache.tuscany.model.assembly.AssemblyModelObject; +import org.apache.tuscany.model.assembly.ConfiguredService; +import org.apache.tuscany.model.assembly.EntryPoint; +import org.apache.tuscany.model.assembly.ExternalService; +import org.apache.tuscany.model.assembly.Service; +import org.apache.tuscany.model.assembly.ServiceContract; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Scope; + +/** + * Creates a <code>RuntimeConfigurationBuilder</code> for an entry point or external service configured with the + * {@link FooBinding} + * + * @version $Rev$ $Date$ + */ +@Scope("MODULE") +public class FooBindingBuilder implements RuntimeConfigurationBuilder { + + @Autowire + private RuntimeContext runtimeContext; + + private ProxyFactoryFactory proxyFactoryFactory; + + private MessageFactory messageFactory; + + /* the top-level builder responsible for evaluating policies */ + private HierarchicalBuilder policyBuilder = new HierarchicalBuilder(); + + public FooBindingBuilder() { + } + + @Init(eager = true) + public void init() { + runtimeContext.addBuilder(this); + } + + /** + * @param runtimeContext The runtimeContext to set. + */ + @Autowire + public void setRuntimeContext(RuntimeContext runtimeContext) { + this.runtimeContext = runtimeContext; + } + + /** + * Sets the factory used to construct proxies implmementing the business interface required by a reference + */ + @Autowire + public void setProxyFactoryFactory(ProxyFactoryFactory factory) { + this.proxyFactoryFactory = factory; + } + + /** + * Sets the factory used to construct invocation messages + * + * @param msgFactory + */ + @Autowire + public void setMessageFactory(MessageFactory msgFactory) { + this.messageFactory = msgFactory; + } + + /** + * Adds a builder responsible for creating source-side and target-side invocation chains for a reference. The + * reference builder may be hierarchical, containing other child reference builders that operate on specific + * metadata used to construct and invocation chain. + */ + public void addPolicyBuilder(RuntimeConfigurationBuilder builder) { + policyBuilder.addBuilder(builder); + } + + public void build(AssemblyModelObject object, Context context) throws BuilderException { + if (object instanceof EntryPoint) { + EntryPoint ep = (EntryPoint) object; + if (ep.getBindings().size() < 1 || !(ep.getBindings().get(0) instanceof FooBinding)) { + return; + } + EntryPointRuntimeConfiguration config = new FooEntryPointRuntimeConfiguration(ep.getName(), ep.getConfiguredService() + .getService().getName(), messageFactory); + + ConfiguredService configuredService = ep.getConfiguredService(); + Service service = configuredService.getService(); + ServiceContract serviceContract = service.getServiceContract(); + Map<Method, InvocationConfiguration> iConfigMap = new HashMap(); + ProxyFactory proxyFactory = proxyFactoryFactory.createProxyFactory(); + Set<Method> javaMethods = JavaIntrospectionHelper.getAllUniqueMethods(serviceContract.getInterface()); + for (Method method : javaMethods) { + InvocationConfiguration iConfig = new InvocationConfiguration(method); + iConfigMap.put(method, iConfig); + } + QualifiedName qName = new QualifiedName(ep.getConfiguredReference().getTargetConfiguredServices().get(0).getAggregatePart().getName() + "/" + service.getName()); + ProxyConfiguration pConfiguration = new ProxyConfiguration(qName, iConfigMap, serviceContract.getInterface().getClassLoader(), messageFactory); + proxyFactory.setBusinessInterface(serviceContract.getInterface()); + proxyFactory.setProxyConfiguration(pConfiguration); + config.addSourceProxyFactory(service.getName(), proxyFactory); + configuredService.setProxyFactory(proxyFactory); + if (policyBuilder != null) { + // invoke the reference builder to handle additional policy metadata + policyBuilder.build(configuredService, context); + } + // add tail interceptor + for (InvocationConfiguration iConfig : (Collection<InvocationConfiguration>) iConfigMap.values()) { + iConfig.addTargetInterceptor(new InvokerInterceptor()); + } + ep.getConfiguredReference().setRuntimeConfiguration(config); + + } else if (object instanceof ExternalService) { + ExternalService es = (ExternalService) object; + if (es.getBindings().size() < 1 || !(es.getBindings().get(0) instanceof FooBinding)) { + return; + } + + FooExternalServiceRuntimeConfiguration config = new FooExternalServiceRuntimeConfiguration(es.getName(), + new FooClientFactory()); + + ConfiguredService configuredService = es.getConfiguredService(); + Service service = configuredService.getService(); + ServiceContract serviceContract = service.getServiceContract(); + Map<Method, InvocationConfiguration> iConfigMap = new HashMap(); + ProxyFactory proxyFactory = proxyFactoryFactory.createProxyFactory(); + Set<Method> javaMethods = JavaIntrospectionHelper.getAllUniqueMethods(serviceContract.getInterface()); + for (Method method : javaMethods) { + InvocationConfiguration iConfig = new InvocationConfiguration(method); + iConfigMap.put(method, iConfig); + } + QualifiedName qName = new QualifiedName(es.getName() + "/" + service.getName()); + ProxyConfiguration pConfiguration = new ProxyConfiguration(qName, iConfigMap, serviceContract.getInterface().getClassLoader(), messageFactory); + proxyFactory.setBusinessInterface(serviceContract.getInterface()); + proxyFactory.setProxyConfiguration(pConfiguration); + config.addTargetProxyFactory(service.getName(), proxyFactory); + configuredService.setProxyFactory(proxyFactory); + if (policyBuilder != null) { + // invoke the reference builder to handle additional policy metadata + policyBuilder.build(configuredService, context); + } + // add tail interceptor + for (InvocationConfiguration iConfig : (Collection<InvocationConfiguration>) iConfigMap.values()) { + iConfig.addTargetInterceptor(new InvokerInterceptor()); + } + + es.getConfiguredService().setRuntimeConfiguration(config); + } else { + return; + + } + } + + private class FooClientFactory implements ObjectFactory { + + public Object getInstance() throws ObjectCreationException { + return new FooClient(); + } + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingWireBuilder.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingWireBuilder.java new file mode 100644 index 0000000000..bba70e4c84 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingWireBuilder.java @@ -0,0 +1,56 @@ +package org.apache.tuscany.container.java.mock.binding.foo; + +import org.apache.tuscany.core.builder.BuilderConfigException; +import org.apache.tuscany.core.builder.WireBuilder; +import org.apache.tuscany.core.context.ScopeContext; +import org.apache.tuscany.core.invocation.InvocationConfiguration; +import org.apache.tuscany.core.invocation.spi.ProxyFactory; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.core.system.annotation.Autowire; +import org.osoa.sca.annotations.Init; + +public class FooBindingWireBuilder implements WireBuilder { + + public FooBindingWireBuilder() { + super(); + } + + private RuntimeContext runtimeContext; + + @Autowire + public void setRuntimeContext(RuntimeContext context) { + runtimeContext = context; + } + + @Init(eager=true) + public void init() { + runtimeContext.addBuilder(this); + } + + + public void connect(ProxyFactory sourceFactory, ProxyFactory targetFactory, Class targetType, boolean downScope, + ScopeContext targetScopeContext) throws BuilderConfigException { + if (!FooExternalServiceRuntimeConfiguration.class.isAssignableFrom(targetType)) { + return; + } + for (InvocationConfiguration sourceInvocationConfig : sourceFactory.getProxyConfiguration().getInvocationConfigurations() + .values()) { + FooESTargetInvoker invoker = new FooESTargetInvoker(sourceFactory.getProxyConfiguration().getTargetName() + .getPartName(), targetScopeContext); + sourceInvocationConfig.setTargetInvoker(invoker); + // if (downScope) { + // // the source scope is shorter than the target, so the invoker can cache the target instance + // invoker.setCacheable(true); + // } else { + // invoker.setCacheable(false); + // } + } + + } + + public void completeTargetChain(ProxyFactory targetFactory, Class targetType, ScopeContext targetScopeContext) + throws BuilderConfigException { + // TODO implement + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooClient.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooClient.java new file mode 100644 index 0000000000..9f67eb7786 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooClient.java @@ -0,0 +1,37 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.mock.binding.foo; + +import java.lang.reflect.Array; + +import org.apache.tuscany.core.context.TargetException; + +/** + * A mock client for a transport binding + * + * @version $Rev$ $Date$ + */ +public class FooClient { + + public FooClient() { + } + + public Object invoke(Object msg) { + if (msg!=null && msg.getClass().isArray() && Array.getLength(msg) == 1){ + return Array.get(msg,0); + }else{ + throw new TargetException("This binding only understands operations with a single parameter"); + } + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooESTargetInvoker.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooESTargetInvoker.java new file mode 100644 index 0000000000..6dd42ef4d6 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooESTargetInvoker.java @@ -0,0 +1,97 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.mock.binding.foo; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.core.context.ExternalServiceContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.ScopeContext; +import org.apache.tuscany.core.context.TargetException; +import org.apache.tuscany.core.invocation.Interceptor; +import org.apache.tuscany.core.invocation.TargetInvoker; +import org.apache.tuscany.core.message.Message; + +/** + * Responsible for invoking a mock transport binding client configured for an external service + * + * @version $Rev$ $Date$ + */ +public class FooESTargetInvoker implements TargetInvoker { + + private String name; + + private ScopeContext container; + + private ExternalServiceContext context; + + public FooESTargetInvoker(String esName, ScopeContext container) { + assert (esName != null) : "No external service name specified"; + assert (container != null) : "No scope container specified"; + name = esName; + this.container = container; + } + + public Object invokeTarget(Object payload) throws InvocationTargetException { + if (context == null) { + InstanceContext iContext = container.getContext(name); + if (!(iContext instanceof ExternalServiceContext)) { + TargetException te = new TargetException("Unexpected target context type"); + te.setIdentifier(iContext.getClass().getName()); + te.addContextName(iContext.getName()); + throw te; + } + context = (ExternalServiceContext) iContext; + } + FooClient client = (FooClient) context.getImplementationInstance(true); + if (payload != null) { + return client.invoke(payload); + } else { + return client.invoke(null); + } + } + + public boolean isCacheable() { + return false; + } + + public Message invoke(Message msg) { + try { + Object resp = invokeTarget(msg.getBody()); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBody(e.getCause()); + } catch (Throwable e) { + msg.setBody(e); + } + return msg; + } + + public void setNext(Interceptor next) { + throw new UnsupportedOperationException(); + } + + public Object clone() { + try { + FooESTargetInvoker invoker = (FooESTargetInvoker) super.clone(); + invoker.container = container; + invoker.context = this.context; + invoker.name = this.name; + return invoker; + } catch (CloneNotSupportedException e) { + return null; // will not happen + } + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooEntryPointRuntimeConfiguration.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooEntryPointRuntimeConfiguration.java new file mode 100644 index 0000000000..bee388127e --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooEntryPointRuntimeConfiguration.java @@ -0,0 +1,31 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.mock.binding.foo; + +import org.apache.tuscany.core.builder.impl.EntryPointRuntimeConfiguration; +import org.apache.tuscany.core.message.MessageFactory; + +/** + * + * + * @version $Rev$ $Date$ + */ +public class FooEntryPointRuntimeConfiguration extends EntryPointRuntimeConfiguration { + + public FooEntryPointRuntimeConfiguration(String name, String serviceName, MessageFactory msgFactory) { + super(name, serviceName, msgFactory); + } + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooExternalServiceRuntimeConfiguration.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooExternalServiceRuntimeConfiguration.java new file mode 100644 index 0000000000..e06a78d529 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooExternalServiceRuntimeConfiguration.java @@ -0,0 +1,32 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.mock.binding.foo; + +import org.apache.tuscany.core.builder.ObjectFactory; +import org.apache.tuscany.core.builder.impl.BaseExternalServiceRuntimeConfiguration; + +/** + * Creates instances of {@link org.apache.tuscany.core.context.ExternalServiceContext} configured with the appropriate + * invocation chains and bindings. This implementation serves as a marker for + * {@link org.apache.tuscany.container.java.mock.binding.foo.FooBindingWireBuilder} + * + * @version $Rev$ $Date$ + */ +public class FooExternalServiceRuntimeConfiguration extends BaseExternalServiceRuntimeConfiguration { + + public FooExternalServiceRuntimeConfiguration(String name, ObjectFactory objectFactory) { + super(name, objectFactory); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/AbstractGenericComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/AbstractGenericComponent.java new file mode 100644 index 0000000000..0c9d8172f8 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/AbstractGenericComponent.java @@ -0,0 +1,296 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import java.util.List; +import java.util.Map; + +import org.osoa.sca.ModuleContext; +import org.osoa.sca.annotations.ComponentName; +import org.osoa.sca.annotations.Context; +import org.osoa.sca.annotations.Property; + +/** + * Base test component implementation + * + * @version $Rev$ $Date$ + */ +public class AbstractGenericComponent implements GenericComponent { + + @ComponentName + private String name; + + public String getName() { + return name; + } + + @Context + private ModuleContext context; + + public ModuleContext getModuleContext() { + return context; + } + + //---------------------------------- + // Collections + //---------------------------------- + + Map testMap; + + public Map getTestMap() { + return testMap; + } + + public void setTestMap(Map testMap) { + this.testMap = testMap; + } + + List testList; + + public List getTestList() { + return testList; + } + + public void setTestList(List testList) { + this.testList = testList; + } + + //---------------------------------- + // Arrays + //---------------------------------- + + private int[] arrayInt; + + public int[] getArrayInt() { + return arrayInt; + } + + public void setArrayInt(int[] arrayInt) { + this.arrayInt = arrayInt; + } + + private float[] arrayFloat; + + public float[] getArrayFloat() { + return arrayFloat; + } + + public void setArrayFloat(float[] pArrayFloat) { + arrayFloat = pArrayFloat; + } + + private double[] arrayDouble; + + public double[] getArrayDouble() { + return arrayDouble; + } + + public void setArrayDouble(double[] pArrayDouble) { + arrayDouble = pArrayDouble; + } + + private long[] arrayLong; + + public long[] getArrayLong() { + return arrayLong; + } + + public void setArrayLong(long[] arrayLong) { + this.arrayLong = arrayLong; + } + + private short[] arrayShort; + + public short[] getArrayShort() { + return arrayShort; + } + + public void setArrayShort(short[] arrayShort) { + this.arrayShort = arrayShort; + } + + private boolean[] arrayBoolean; + + public boolean[] getArrayBoolean() { + return arrayBoolean; + } + + public void setArrayBoolean(boolean[] arrayBoolean) { + this.arrayBoolean = arrayBoolean; + } + + private String[] arrayString; + + public String[] getArrayString() { + return arrayString; + } + + public void setArrayString(String[] arrayString) { + this.arrayString = arrayString; + } + + //---------------------------------- + // Primitives + //---------------------------------- + + private boolean mBoolean; + + public boolean getBoolean() { + return mBoolean; + } + + public void setBoolean(boolean pBoolean) { + mBoolean = pBoolean; + } + + private short mShort; + + public short getShort() { + return mShort; + } + + public void setShort(short pShort) { + mShort = pShort; + } + + private int mInt; + + public int getInt() { + return mInt; + } + + public void setInt(int pInt) { + mInt = pInt; + } + + private long mLong; + + public long getLong() { + return mLong; + } + + public void setLong(long pLong) { + mLong = pLong; + } + + private double mDouble; + + public double getDouble() { + return mDouble; + } + + public void setDouble(double pDouble) { + mDouble = pDouble; + } + + private float mFloat; + + public float getFloat() { + return mFloat; + } + + public void setFloat(float pFloat) { + mFloat = pFloat; + } + + private char mChar; + + public char getChar() { + return mChar; + } + + public void setChar(char pChar) { + mChar = pChar; + } + + //---------------------------------- + // Object types + //---------------------------------- + + private String mString; + + public String getString() { + return mString; + } + + public void setString(String pString) { + mString = pString; + } + + private Short mOShort; + + public Short getOShort() { + return mOShort; + } + + public void setOShort(Short pOShort) { + mOShort = pOShort; + } + + private Integer mOInteger; + + public Integer getOInteger() { + return mOInteger; + } + + public void setOInteger(Integer pOInteger) { + mOInteger = pOInteger; + } + + private Long mOLong; + + public Long getOLong() { + return mOLong; + } + + public void setOLong(Long pOLong) { + mOLong = pOLong; + } + + private Float mOFloat; + + public Float getOFloat() { + return mOFloat; + } + + public void setOFloat(Float pOFloat) { + mOFloat = pOFloat; + } + + private Double mODouble; + + public Double getODouble() { + return mODouble; + } + + public void setODouble(Double pODouble) { + mODouble = pODouble; + } + + @Property(name = "genericComponent") + private GenericComponent mGenericComponent; + + public GenericComponent getGenericComponent() { + return mGenericComponent; + } + + public void setGenericComponent(GenericComponent pGenericComponent) { + mGenericComponent = pGenericComponent; + } + + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/BadContextPojo.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/BadContextPojo.java new file mode 100644 index 0000000000..7840b57dea --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/BadContextPojo.java @@ -0,0 +1,26 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Context; + +public class BadContextPojo { + + @Context + String moduleContext; + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/BadNamePojo.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/BadNamePojo.java new file mode 100644 index 0000000000..d9753fa9f0 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/BadNamePojo.java @@ -0,0 +1,24 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.ComponentName; + +public class BadNamePojo { + @ComponentName + private int name; +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/DataObject.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/DataObject.java new file mode 100644 index 0000000000..695cc2a5a1 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/DataObject.java @@ -0,0 +1,38 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import java.io.Serializable; + +/** + * A complex type for testing + * + * @version $Rev$ $Date$ + */ +public class DataObject implements Serializable { + + private String stringValue; + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(String stringValue) { + this.stringValue = stringValue; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/GenericComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/GenericComponent.java new file mode 100644 index 0000000000..44f1c02bfd --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/GenericComponent.java @@ -0,0 +1,142 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import java.util.List; +import java.util.Map; + +import org.osoa.sca.ModuleContext; + +/** + * Generic test component + * + * @version $Rev$ $Date$ + */ +public interface GenericComponent { + + public String getName(); + + public ModuleContext getModuleContext(); + + // ---------------------------------- + // Collections + // ---------------------------------- + + public Map getTestMap(); + + public void setTestMap(Map pTestMap); + + public List getTestList(); + + public void setTestList(List pTestList); + + // ---------------------------------- + // Arrays + // ---------------------------------- + + public int[] getArrayInt(); + + public void setArrayInt(int[] pArrayInt); + + public float[] getArrayFloat(); + + public void setArrayFloat(float[] pArrayFloat); + + public double[] getArrayDouble(); + + public void setArrayDouble(double[] pArrayDouble); + + public long[] getArrayLong(); + + public void setArrayLong(long[] pArrayLong); + + public short[] getArrayShort(); + + public void setArrayShort(short[] pArrayShort); + + public boolean[] getArrayBoolean(); + + public void setArrayBoolean(boolean[] pArrayBoolean); + + public String[] getArrayString(); + + public void setArrayString(String[] pArrayString); + + // ---------------------------------- + // Primitives + // ---------------------------------- + + public boolean getBoolean(); + + public void setBoolean(boolean pBoolean); + + public short getShort(); + + public void setShort(short pShort); + + public int getInt(); + + public void setInt(int pInt); + + public long getLong(); + + public void setLong(long pLong); + + public double getDouble(); + + public void setDouble(double pDouble); + + public float getFloat(); + + public void setFloat(float pFloat); + + public char getChar(); + + public void setChar(char pChar); + + // ---------------------------------- + // Object types + // ---------------------------------- + + public String getString(); + + public void setString(String pString); + + public Short getOShort(); + + public void setOShort(Short pOShort); + + public Integer getOInteger(); + + public void setOInteger(Integer pOInteger); + + public Long getOLong(); + + public void setOLong(Long pOLong); + + public Float getOFloat(); + + public void setOFloat(Float pOFloat); + + public Double getODouble(); + + public void setODouble(Double pODouble); + + public GenericComponent getGenericComponent(); + + public void setGenericComponent(GenericComponent pGenericComponent); +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/HelloWorldClient.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/HelloWorldClient.java new file mode 100644 index 0000000000..1e7ca9f36b --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/HelloWorldClient.java @@ -0,0 +1,46 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.apache.tuscany.container.java.assembly.mock.HelloWorldService; + +/** + * + * + * @version $Rev$ $Date$ + */ +public class HelloWorldClient implements HelloWorldService{ + + private int count; + + public HelloWorldClient() { + } + + private HelloWorldService service; + + public void setHelloWorldService(HelloWorldService service){ + this.service = service; + } + + public String hello(String name) { + ++count; + return service.hello(name); + } + + public int count() { + return count; + } + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/LocalComponentImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/LocalComponentImpl.java new file mode 100644 index 0000000000..4b57c7f301 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/LocalComponentImpl.java @@ -0,0 +1,97 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.ModuleContext; +import org.osoa.sca.annotations.ComponentName; +import org.osoa.sca.annotations.Context; +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; + +/** + * A test local service + * + * @version $Rev$ $Date$ + */ + +public class LocalComponentImpl { + + @ComponentName + protected String name; + + public String getName() { + return name; + } + + @Context + protected ModuleContext moduleCtx; + + public ModuleContext getModuleContext() { + return moduleCtx; + } + + @Property(name = "fieldSetter", required = true) + private String mfieldSetter; + + public String getfieldSetter() { + return mfieldSetter; + } + + public void setfieldSetter(String pfieldSetter) throws Exception { + throw new Exception("Set method instead of field"); + } + + private String mMethodSetter; + boolean mSetByMethod; + + public String getMethodSetter() throws Exception { + if (mSetByMethod) { + return mMethodSetter; + } + throw new Exception("Property method setter failed"); + + } + + @Property(required = true) + public void setMethodSetter(String pMethodSetter) { + mSetByMethod = true; + mMethodSetter = pMethodSetter; + } + + @Reference(name = "requiredDataObject", required = true) + private DataObject mRequiredDataObject; + + public DataObject getRequiredDataObject() { + return mRequiredDataObject; + } + + public void setRequiredDataObject(DataObject pRequiredDataObject) { + mRequiredDataObject = pRequiredDataObject; + } + + @Reference(name = "optionalDataObject") + private DataObject mOptionalDataObject; + + public DataObject getOptionalDataObject() { + return mOptionalDataObject; + } + + public void setOptionalDataObject(DataObject pOptionalDataObject) { + mOptionalDataObject = pOptionalDataObject; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeComponent.java new file mode 100644 index 0000000000..7787e8cf5b --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeComponent.java @@ -0,0 +1,29 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Scope; + +/** + * @version $Rev$ $Date$ + */ +@Scope("MODULE") +public interface ModuleScopeComponent extends GenericComponent { + + //public boolean isInit(); +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeComponentImpl.componentType b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeComponentImpl.componentType new file mode 100644 index 0000000000..ca4c633e59 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeComponentImpl.componentType @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="ASCII"?>
+<!--
+ Copyright (c) 2005 The Apache Software Foundation or its licensors, as applicable.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+<!-- remove this once ImplementationImpl is fixed not to requrie this -->
+<componentType xmlns="http://www.osoa.org/xmlns/sca/0.9">
+
+ <service name="ModuleScopeComponent" scope="module">
+ <interface.java interface="janus.test.component.ModuleScopeComponent"/>
+ </service> + <!-- Not supported, not in the spec
+ <property.java name="mInt" class="java.lang.Integer" default="default"/>
+ <property.java name="foo" class="java.lang.String" default="default"/> + -->
+ <reference name="mGenericComponent" multiplicity="0..1">
+ <interface.java interface="test.org.apache.tuscany.container.java.components.AbstractGenericComponent"/>
+ </reference>
+
+</componentType>
+
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeComponentImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeComponentImpl.java new file mode 100644 index 0000000000..ca65be4bb0 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeComponentImpl.java @@ -0,0 +1,47 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + + +/** + * @version $Rev$ $Date$ + */ +public class ModuleScopeComponentImpl extends AbstractGenericComponent implements + ModuleScopeComponent { + +// private boolean init; +// +// @Init(eager=false) +// public void init(){ +// init= true; +// } +// +// public boolean isInit(){ +// return init; +// } + + + private String foo; + + public void setFoo(String foo) { + this.foo = foo; + } + + public String getFoo() { + return foo; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeDestroyOnlyComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeDestroyOnlyComponent.java new file mode 100644 index 0000000000..76218c2d6c --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeDestroyOnlyComponent.java @@ -0,0 +1,34 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Destroy; + +public class ModuleScopeDestroyOnlyComponent extends ModuleScopeComponentImpl { + + boolean destroyed = false; + + public boolean isDestroyed() { + return destroyed; + } + + @Destroy + public void destroy() { + destroyed = true; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeEagerInitComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeEagerInitComponent.java new file mode 100644 index 0000000000..56a0421f24 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeEagerInitComponent.java @@ -0,0 +1,39 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Init; + +public class ModuleScopeEagerInitComponent extends ModuleScopeComponentImpl { + + boolean initialized = false; + + public boolean isInitialized() { + return initialized; + } + + // this value tests to ensure introspection can find the init() method even + // if a field is named the same. Ultimately, this should be in the + // introspection tests + private boolean init; + + @Init(eager = true) + public void init() { + initialized = true; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeEagerInitDestroyComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeEagerInitDestroyComponent.java new file mode 100644 index 0000000000..dae156737a --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeEagerInitDestroyComponent.java @@ -0,0 +1,33 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Destroy; + +public class ModuleScopeEagerInitDestroyComponent extends ModuleScopeEagerInitComponent { + + boolean destroyed = false; + + public boolean isDestroyed() { + return destroyed; + } + + @Destroy + public void destroy() { + destroyed = true; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeInitDestroyComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeInitDestroyComponent.java new file mode 100644 index 0000000000..d9b1303677 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeInitDestroyComponent.java @@ -0,0 +1,34 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Destroy; + +public class ModuleScopeInitDestroyComponent extends ModuleScopeInitOnlyComponent { + + boolean destroyed = false; + + public boolean isDestroyed() { + return destroyed; + } + + @Destroy + public void destroy() { + destroyed = true; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeInitOnlyComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeInitOnlyComponent.java new file mode 100644 index 0000000000..a9b465d79e --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/ModuleScopeInitOnlyComponent.java @@ -0,0 +1,38 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Init; + +public class ModuleScopeInitOnlyComponent extends ModuleScopeComponentImpl { + + boolean initialized = false; + + public boolean isInitialized() { + return initialized; + } + + // this value tests to ensure introspection can find the init() method even + // if a field is named the same. Ultimately, this should be in the introspection tests + private boolean init; + + @Init + public void init() { + initialized = true; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RemotableService.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RemotableService.java new file mode 100644 index 0000000000..645fd7f47c --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RemotableService.java @@ -0,0 +1,40 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Remotable; + +/** + * Used for testing basic remoting operations + * + * @version $Rev$ $Date$ + */ + +@Remotable +public interface RemotableService { + + public void syncOneWay(String msg); + + public String syncTwoWay(String msg); + + public DataObject syncTwoWayCustomType(DataObject val); + + public String getString(); + + public void setString(String string); + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RemotableServiceImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RemotableServiceImpl.java new file mode 100644 index 0000000000..34a4273323 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RemotableServiceImpl.java @@ -0,0 +1,61 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import junit.framework.Assert; +import org.osoa.sca.annotations.Property; + + +/** + * @version $Rev$ $Date$ + */ +public class RemotableServiceImpl implements RemotableService { + + // ---------------------------------- + // Properties + // ---------------------------------- + + @Property(name = "string", required = true) + private String mString; + + public String getString() { + return mString; + } + + public void setString(String string) { + mString = string; + } + + // ---------------------------------- + // Methods + // ---------------------------------- + + public void syncOneWay(String msg) { + Assert.assertEquals("hello", msg); + } + + public String syncTwoWay(String msg) { + return "response"; + } + + public DataObject syncTwoWayCustomType(DataObject val) { + Assert.assertEquals("hello", val.getStringValue()); + DataObject dto = new DataObject(); + dto.setStringValue("return"); + return dto; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RequestScopeComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RequestScopeComponent.java new file mode 100644 index 0000000000..ac7931bd2c --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RequestScopeComponent.java @@ -0,0 +1,28 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Scope; + +/** + * @version $Rev$ $Date$ + */ +@Scope("REQUEST") +public interface RequestScopeComponent extends GenericComponent { + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RequestScopeComponentImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RequestScopeComponentImpl.java new file mode 100644 index 0000000000..5b20519d90 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/RequestScopeComponentImpl.java @@ -0,0 +1,25 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +/** + * @version $Rev$ $Date$ + */ +public class RequestScopeComponentImpl extends AbstractGenericComponent implements + RequestScopeComponent { + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeComponent.java new file mode 100644 index 0000000000..236e083a2b --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeComponent.java @@ -0,0 +1,28 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Scope; + +/** + * @version $Rev$ $Date$ + */ +@Scope("SESSION") +public interface SessionScopeComponent extends GenericComponent { + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeComponentImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeComponentImpl.java new file mode 100644 index 0000000000..41ffeccdfc --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeComponentImpl.java @@ -0,0 +1,25 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +/** + * @version $Rev$ $Date$ + */ +public class SessionScopeComponentImpl extends AbstractGenericComponent implements + SessionScopeComponent { + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeDestroyOnlyComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeDestroyOnlyComponent.java new file mode 100644 index 0000000000..be20a23d29 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeDestroyOnlyComponent.java @@ -0,0 +1,34 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Destroy; + +public class SessionScopeDestroyOnlyComponent extends SessionScopeComponentImpl { + + boolean destroyed = false; + + public boolean isDestroyed() { + return destroyed; + } + + @Destroy + public void destroy() { + destroyed = true; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeInitDestroyComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeInitDestroyComponent.java new file mode 100644 index 0000000000..32aa0142a4 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeInitDestroyComponent.java @@ -0,0 +1,34 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Destroy; + +public class SessionScopeInitDestroyComponent extends SessionScopeInitOnlyComponent { + + boolean destroyed = false; + + public boolean isDestroyed() { + return destroyed; + } + + @Destroy + public void destroy() { + destroyed = true; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeInitOnlyComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeInitOnlyComponent.java new file mode 100644 index 0000000000..a518a9b244 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/SessionScopeInitOnlyComponent.java @@ -0,0 +1,35 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Init; + +public class SessionScopeInitOnlyComponent extends SessionScopeComponentImpl { + + boolean initialized = false; + + public boolean isInitialized() { + return initialized; + } + + @Init + public void init() { + initialized = true; + } + + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/StatelessComponent.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/StatelessComponent.java new file mode 100644 index 0000000000..3fe84dd785 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/StatelessComponent.java @@ -0,0 +1,28 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +import org.osoa.sca.annotations.Scope; + +/** + * @version $Rev$ $Date$ + */ +@Scope("STATELESS") +public interface StatelessComponent extends GenericComponent { + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/StatelessComponentImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/StatelessComponentImpl.java new file mode 100644 index 0000000000..8b806d6a2e --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/components/StatelessComponentImpl.java @@ -0,0 +1,25 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.mock.components; + +/** + * @version $Rev$ $Date$ + */ +public class StatelessComponentImpl extends AbstractGenericComponent implements + StatelessComponent { + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/AggregateScopeTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/AggregateScopeTestCase.java new file mode 100644 index 0000000000..77130f6334 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/AggregateScopeTestCase.java @@ -0,0 +1,178 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" + * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.builder.JavaComponentContextBuilder; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.GenericComponent; +import org.apache.tuscany.container.java.mock.components.ModuleScopeComponentImpl; +import org.apache.tuscany.container.java.mock.components.SessionScopeComponentImpl; +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.QualifiedName; +import org.apache.tuscany.core.context.impl.AggregateContextImpl; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.AggregateScopeContext; +import org.apache.tuscany.model.assembly.Extensible; +import org.apache.tuscany.model.assembly.Scope; +import org.apache.tuscany.model.assembly.SimpleComponent; + +/** + * Tests component nesting. This test need to be in the container.java progject since it relies on Java POJOs for scope + * testing. + * + * @version $Rev$ $Date$ + */ +public class AggregateScopeTestCase extends TestCase { + + /** + * Ensures scope events are propagated in an aggregate scope + */ + public void testAggregateScopePropagation() throws Exception { + EventContext ctx = new EventContextImpl(); + AggregateContext moduleComponentCtx = new AggregateContextImpl(); + moduleComponentCtx.setName("testMC"); + AggregateScopeContext scopeContainer = new AggregateScopeContext(ctx); + scopeContainer.registerConfiguration(MockFactory.createAggregateConfiguration("AggregateComponent", moduleComponentCtx)); + scopeContainer.start(); + AggregateContext child = (AggregateContext) scopeContainer.getContext("AggregateComponent"); + List<Extensible> models = createAssembly(moduleComponentCtx); + for (Extensible model : models) { + child.registerModelObject(model); + } + + scopeContainer.onEvent(EventContext.MODULE_START, null); + Object session = new Object(); + scopeContainer.onEvent(EventContext.REQUEST_START, null); + scopeContainer.onEvent(EventContext.SESSION_NOTIFY, session); + AggregateContext componentCtx = (AggregateContext) scopeContainer.getContext("AggregateComponent"); + GenericComponent testService1 = (GenericComponent) componentCtx.locateInstance("TestService1"); + GenericComponent testService2 = (GenericComponent) componentCtx.locateInstance("TestService2"); + GenericComponent testService3 = (GenericComponent) componentCtx.locateInstance("TestService3"); + Assert.assertNotNull(testService1); + Assert.assertNotNull(testService2); + Assert.assertNotNull(testService3); + scopeContainer.onEvent(EventContext.REQUEST_END, null); + scopeContainer.onEvent(EventContext.REQUEST_START, null); + scopeContainer.onEvent(EventContext.SESSION_NOTIFY, session); + + GenericComponent testService2a = (GenericComponent) componentCtx.locateInstance("TestService2"); + Assert.assertNotNull(testService2a); + GenericComponent testService3a = (GenericComponent) componentCtx.locateInstance("TestService3"); + Assert.assertNotNull(testService3a); + Assert.assertEquals(testService2, testService2a); + Assert.assertNotSame(testService3, testService3a); + scopeContainer.onEvent(EventContext.REQUEST_END, null); + scopeContainer.onEvent(EventContext.SESSION_END, session); + + Object session2 = new Object(); + scopeContainer.onEvent(EventContext.REQUEST_START, null); + scopeContainer.onEvent(EventContext.SESSION_NOTIFY, session2); + GenericComponent testService2b = (GenericComponent) componentCtx.locateInstance("TestService2"); + Assert.assertNotNull(testService2b); + Assert.assertNotSame(testService2, testService2b); + + scopeContainer.onEvent(EventContext.REQUEST_END, null); + scopeContainer.onEvent(EventContext.SESSION_END, session2); + + } + + /** + * Ensures only child entry points (and not components) are accessible from parents + */ + public void testAggregateNoEntryPoint() throws Exception { + EventContext ctx = new EventContextImpl(); + AggregateContext moduleComponentCtx = new AggregateContextImpl(); + moduleComponentCtx.setName("testMC"); + AggregateScopeContext scopeContainer = new AggregateScopeContext(ctx); + scopeContainer.registerConfiguration(MockFactory.createAggregateConfiguration("AggregateComponent", moduleComponentCtx)); + scopeContainer.start(); + AggregateContext child = (AggregateContext) scopeContainer.getContext("AggregateComponent"); + List<Extensible> parts = createAssembly(moduleComponentCtx); + for (Extensible part : parts) { + child.registerModelObject(part); + } + + // aggregate.onEvent(EventContext.SYSTEM_START, null); + scopeContainer.onEvent(EventContext.MODULE_START, null); + QualifiedName name = new QualifiedName("AggregateComponent/TestService1"); + AggregateContext componentCtx = (AggregateContext) scopeContainer.getContext("AggregateComponent"); + } + + /** + * Tests adding a context before its parent has been started + */ + public void testRegisterContextBeforeStart() throws Exception { + EventContext ctx = new EventContextImpl(); + AggregateContext moduleComponentCtx = new AggregateContextImpl(); + moduleComponentCtx.setName("testMC"); + AggregateScopeContext scopeContainer = new AggregateScopeContext(ctx); + scopeContainer.registerConfiguration(MockFactory.createAggregateConfiguration("AggregateComponent", moduleComponentCtx)); + scopeContainer.start(); + scopeContainer.onEvent(EventContext.MODULE_START, null); + QualifiedName name = new QualifiedName("AggregateComponent/TestService1"); + AggregateContext componentCtx = (AggregateContext) scopeContainer.getContext("AggregateComponent"); + scopeContainer.onEvent(EventContext.MODULE_STOP, null); + scopeContainer.stop(); + } + + /** + * Tests adding a context after its parent has been started + */ + public void testRegisterContextAfterStart() throws Exception { + EventContext ctx = new EventContextImpl(); + AggregateContext moduleComponentCtx = new AggregateContextImpl(); + moduleComponentCtx.setName("testMC"); + AggregateScopeContext scopeContainer = new AggregateScopeContext(ctx); + scopeContainer.start(); + + scopeContainer.onEvent(EventContext.MODULE_START, null); + scopeContainer.registerConfiguration(MockFactory.createAggregateConfiguration("AggregateComponent", moduleComponentCtx)); + QualifiedName name = new QualifiedName("AggregateComponent/TestService1"); + AggregateContext componentCtx = (AggregateContext) scopeContainer.getContext("AggregateComponent"); + scopeContainer.onEvent(EventContext.MODULE_STOP, null); + scopeContainer.stop(); + } + + /** + * Creats an assembly containing a module-scoped component definition, a session-scoped component definition, and a + * request-scoped component definition + * + * @param ctx the parent module context + */ + private List<Extensible> createAssembly(AggregateContext ctx) throws BuilderException { + JavaComponentContextBuilder builder = new JavaComponentContextBuilder(); + SimpleComponent component = MockFactory.createComponent("TestService1", ModuleScopeComponentImpl.class, Scope.MODULE); + SimpleComponent sessionComponent = MockFactory.createComponent("TestService2", SessionScopeComponentImpl.class, + Scope.SESSION); + SimpleComponent requestComponent = MockFactory.createComponent("TestService3", SessionScopeComponentImpl.class, + Scope.REQUEST); + builder.build(component, ctx); + builder.build(sessionComponent, ctx); + builder.build(requestComponent, ctx); + List<Extensible> configs = new ArrayList(); + configs.add(component); + configs.add(sessionComponent); + configs.add(requestComponent); + return configs; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicHttpSessionScopeTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicHttpSessionScopeTestCase.java new file mode 100644 index 0000000000..a56589c4d1 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicHttpSessionScopeTestCase.java @@ -0,0 +1,223 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.builder.JavaComponentContextBuilder; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.SessionScopeComponent; +import org.apache.tuscany.container.java.mock.components.SessionScopeComponentImpl; +import org.apache.tuscany.container.java.mock.components.SessionScopeInitDestroyComponent; +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.RuntimeConfiguration; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.HttpSessionScopeContext; +import org.apache.tuscany.model.assembly.Scope; +import org.apache.tuscany.model.assembly.SimpleComponent; + +/** + * Unit tests for the Http session scope container + * + * @version $Rev$ $Date$ + */ +public class BasicHttpSessionScopeTestCase extends TestCase { + + /** + * Tests instance identity is properly maintained + */ + public void testInstanceManagement() throws Exception { + EventContext ctx = new EventContextImpl(); + HttpSessionScopeContext scope = new HttpSessionScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + Object session = new Object(); + Object session2 = new Object(); + // first request + ctx.setIdentifier(EventContext.HTTP_SESSION, session); + SessionScopeComponent comp1 = (SessionScopeComponent) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + // second request + ctx.setIdentifier(EventContext.HTTP_SESSION, session); + SessionScopeComponent comp2 = (SessionScopeComponent) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp2); + Assert.assertSame(comp1, comp2); + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + // third request, different session + ctx.setIdentifier(EventContext.HTTP_SESSION, session2); + SessionScopeComponent comp3 = (SessionScopeComponent) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp3); + Assert.assertNotSame(comp1, comp3); // should be different instances + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + scope.onEvent(EventContext.SESSION_END, session); + scope.onEvent(EventContext.SESSION_END, session2); + + scope.stop(); + } + + /** + * Tests setting no components in the scope + */ + public void testSetNullComponents() throws Exception { + EventContext ctx = new EventContextImpl(); + HttpSessionScopeContext scope = new HttpSessionScopeContext(ctx); + scope.registerConfigurations(new ArrayList<RuntimeConfiguration<InstanceContext>>()); + scope.start(); + scope.stop(); + } + + public void testGetContextByKey() throws Exception { + EventContext ctx = new EventContextImpl(); + HttpSessionScopeContext scope = new HttpSessionScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + Object session = new Object(); + Object session2 = new Object(); + + // first request + ctx.setIdentifier(EventContext.HTTP_SESSION, session); + SessionScopeComponent comp1 = (SessionScopeComponent) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + // second request, different session + ctx.setIdentifier(EventContext.HTTP_SESSION, session2); + SessionScopeComponent comp2 = (SessionScopeComponent) scope.getContextByKey("TestService1", session) + .getInstance(null); + SessionScopeComponent comp3 = (SessionScopeComponent) scope.getContextByKey("TestService1", session) + .getInstance(null); + Assert.assertNotNull(comp2); + scope.onEvent(EventContext.REQUEST_END, session2); + Assert.assertSame(comp1, comp2); // should be same instances + Assert.assertSame(comp2, comp3); // should not be same instances + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + // shutdown sessions + scope.onEvent(EventContext.SESSION_END, session); + scope.onEvent(EventContext.SESSION_END, session2); + + scope.stop(); + } + + public void testRegisterContextBeforeSession() throws Exception { + EventContext ctx = new EventContextImpl(); + HttpSessionScopeContext scope = new HttpSessionScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + Object session = new Object(); + scope.registerConfiguration(createConfiguration("NewTestService")); + + // first request + ctx.setIdentifier(EventContext.HTTP_SESSION, session); + + SessionScopeInitDestroyComponent comp2 = (SessionScopeInitDestroyComponent) scope.getContext("NewTestService") + .getInstance(null); + Assert.assertNotNull(comp2); + Assert.assertTrue(comp2.isInitialized()); + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + // second request different session + Object session2 = new Object(); + ctx.setIdentifier(EventContext.HTTP_SESSION, session2); + SessionScopeInitDestroyComponent comp3 = (SessionScopeInitDestroyComponent) scope.getContext("NewTestService") + .getInstance(null); + Assert.assertNotNull(comp3); + Assert.assertNotSame(comp2, comp3); + Assert.assertTrue(comp3.isInitialized()); + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + scope.onEvent(EventContext.SESSION_END, session); + Assert.assertTrue(comp2.isDestroyed()); + + scope.onEvent(EventContext.SESSION_END, session2); + Assert.assertTrue(comp3.isDestroyed()); + scope.stop(); + } + + /** + * Tests runtime context registration + */ + public void testRegisterContextAfterSession() throws Exception { + EventContext ctx = new EventContextImpl(); + HttpSessionScopeContext scope = new HttpSessionScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + Object session = new Object(); + + // first request + ctx.setIdentifier(EventContext.HTTP_SESSION, session); + SessionScopeComponent comp1 = (SessionScopeComponent) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + scope.registerConfiguration(createConfiguration("NewTestService")); + + // second request + ctx.setIdentifier(EventContext.HTTP_SESSION, session); + SessionScopeInitDestroyComponent comp2 = (SessionScopeInitDestroyComponent) scope.getContext("NewTestService") + .getInstance(null); + Assert.assertNotNull(comp2); + Assert.assertTrue(comp2.isInitialized()); + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + // third request different session + Object session2 = new Object(); + ctx.setIdentifier(EventContext.HTTP_SESSION, session2); + SessionScopeInitDestroyComponent comp3 = (SessionScopeInitDestroyComponent) scope.getContext("NewTestService") + .getInstance(null); + Assert.assertNotNull(comp3); + Assert.assertNotSame(comp2, comp3); + Assert.assertTrue(comp3.isInitialized()); + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + scope.onEvent(EventContext.SESSION_END, session); + Assert.assertTrue(comp2.isDestroyed()); + + scope.onEvent(EventContext.SESSION_END, session2); + Assert.assertTrue(comp3.isDestroyed()); + scope.stop(); + } + + // ---------------------------------- + // Private methods + // ---------------------------------- + JavaComponentContextBuilder builder = new JavaComponentContextBuilder(); + + private List<RuntimeConfiguration<InstanceContext>> createConfigurations() throws NoSuchMethodException, BuilderException { + SimpleComponent component = MockFactory.createComponent("TestService1", SessionScopeComponentImpl.class, Scope.SESSION); + builder.build(component, null); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + configs.add((RuntimeConfiguration<InstanceContext>) component.getComponentImplementation().getRuntimeConfiguration()); + return configs; + } + + private RuntimeConfiguration<InstanceContext> createConfiguration(String name) throws NoSuchMethodException, BuilderException { + SimpleComponent component = MockFactory.createComponent(name, SessionScopeInitDestroyComponent.class, Scope.SESSION); + builder.build(component, null); + return (RuntimeConfiguration<InstanceContext>) component.getComponentImplementation().getRuntimeConfiguration(); + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicModuleScopeTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicModuleScopeTestCase.java new file mode 100644 index 0000000000..e1dab000ce --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicModuleScopeTestCase.java @@ -0,0 +1,126 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.builder.JavaComponentContextBuilder; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.ModuleScopeComponentImpl; +import org.apache.tuscany.container.java.mock.components.ModuleScopeInitDestroyComponent; +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.RuntimeConfiguration; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.ModuleScopeContext; +import org.apache.tuscany.model.assembly.Scope; +import org.apache.tuscany.model.assembly.SimpleComponent; + +/** + * Unit tests for the module scope container + * + * @version $Rev$ $Date$ + */ +public class BasicModuleScopeTestCase extends TestCase { + + /** + * Tests instance identity is properly maintained + */ + public void testInstanceManagement() throws Exception { + EventContext ctx = new EventContextImpl(); + ModuleScopeContext scope = new ModuleScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + // first request + scope.onEvent(EventContext.MODULE_START, null); + ModuleScopeComponentImpl comp1 = (ModuleScopeComponentImpl) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + // second request + ModuleScopeComponentImpl comp2 = (ModuleScopeComponentImpl) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp2); + Assert.assertSame(comp1, comp2); + scope.onEvent(EventContext.MODULE_STOP, null); + scope.stop(); + } + + public void testSetNullComponents() throws Exception { + EventContext ctx = new EventContextImpl(); + ModuleScopeContext scope = new ModuleScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + scope.onEvent(EventContext.MODULE_START, null); + scope.onEvent(EventContext.MODULE_STOP, null); + scope.stop(); + } + + public void testRegisterContextBeforeStart() throws Exception { + EventContext ctx = new EventContextImpl(); + ModuleScopeContext scope = new ModuleScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + scope.registerConfiguration(createConfiguration("NewTestService")); + scope.onEvent(EventContext.MODULE_START,null); + ModuleScopeInitDestroyComponent comp2 = (ModuleScopeInitDestroyComponent) scope.getContext("NewTestService").getInstance(null); + Assert.assertNotNull(comp2); + Assert.assertTrue(comp2.isInitialized()); + scope.onEvent(EventContext.MODULE_STOP,null); + Assert.assertTrue(comp2.isDestroyed()); + scope.stop(); + } + + public void testRegisterContextAfterStart() throws Exception { + EventContext ctx = new EventContextImpl(); + ModuleScopeContext scope = new ModuleScopeContext(ctx); + scope.start(); + scope.registerConfiguration(createConfiguration("NewTestService")); + scope.onEvent(EventContext.MODULE_START,null); + scope.registerConfigurations(createConfigurations()); + ModuleScopeInitDestroyComponent comp2 = (ModuleScopeInitDestroyComponent) scope.getContext("NewTestService").getInstance(null); + Assert.assertNotNull(comp2); + Assert.assertTrue(comp2.isInitialized()); + scope.onEvent(EventContext.MODULE_STOP,null); + Assert.assertTrue(comp2.isDestroyed()); + scope.stop(); + } + + // ---------------------------------- + // Private methods + // ---------------------------------- + + JavaComponentContextBuilder builder = new JavaComponentContextBuilder(); + + private List<RuntimeConfiguration<InstanceContext>> createConfigurations() throws NoSuchMethodException, BuilderException { + SimpleComponent component = MockFactory.createComponent("TestService1", ModuleScopeComponentImpl.class, Scope.MODULE); + builder.build(component, null); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + configs.add((RuntimeConfiguration<InstanceContext>) component.getComponentImplementation().getRuntimeConfiguration()); + return configs; + } + + private RuntimeConfiguration<InstanceContext> createConfiguration(String name) + throws NoSuchMethodException, BuilderException { + SimpleComponent component = MockFactory.createComponent(name, ModuleScopeInitDestroyComponent.class, + Scope.MODULE); + builder.build(component, null); + return (RuntimeConfiguration<InstanceContext>) component.getComponentImplementation().getRuntimeConfiguration(); + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicRequestScopeTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicRequestScopeTestCase.java new file mode 100644 index 0000000000..b3a6e5f64b --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicRequestScopeTestCase.java @@ -0,0 +1,148 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.builder.JavaComponentContextBuilder; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.RequestScopeComponent; +import org.apache.tuscany.container.java.mock.components.RequestScopeComponentImpl; +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.RuntimeConfiguration; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.RequestScopeContext; +import org.apache.tuscany.model.assembly.Scope; +import org.apache.tuscany.model.assembly.SimpleComponent; + +/** + * Unit tests for the request scope container + * + * @version $Rev$ $Date$ + */ +public class BasicRequestScopeTestCase extends TestCase { + + /** + * Tests instance identity is properly maintained + */ + public void testInstanceManagement() throws Exception { + EventContext ctx = new EventContextImpl(); + RequestScopeContext scope = new RequestScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + + // first request + RequestScopeComponentImpl comp1 = (RequestScopeComponentImpl) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + scope.onEvent(EventContext.REQUEST_END, null); + + // second request + RequestScopeComponentImpl comp2 = (RequestScopeComponentImpl) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp2); + Assert.assertNotSame(comp1, comp2); + scope.onEvent(EventContext.REQUEST_END, null); + + scope.stop(); + } + + public void testRegisterContextBeforeRequest() throws Exception { + EventContext ctx = new EventContextImpl(); + RequestScopeContext scope = new RequestScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.registerConfiguration(createConfiguration("NewTestService")); + scope.start(); + RequestScopeComponent comp1 = (RequestScopeComponent) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + RequestScopeComponent comp2 = (RequestScopeComponent) scope.getContext("NewTestService").getInstance(null); + Assert.assertNotNull(comp2); + scope.onEvent(EventContext.REQUEST_END, null); + scope.stop(); + } + + public void testRegisterContextAfterRequest() throws Exception { + EventContext ctx = new EventContextImpl(); + RequestScopeContext scope = new RequestScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + RequestScopeComponent comp1 = (RequestScopeComponent) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + scope.registerConfiguration(createConfiguration("NewTestService")); + RequestScopeComponent comp2 = (RequestScopeComponent) scope.getContext("NewTestService").getInstance(null); + Assert.assertNotNull(comp2); + scope.onEvent(EventContext.REQUEST_END, null); + scope.stop(); + } + + /** + * Tests setting no components in the scope + */ + public void testSetNullComponents() throws Exception { + EventContext ctx = new EventContextImpl(); + RequestScopeContext scope = new RequestScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + scope.stop(); + } + + public void testGetComponentByKey() throws Exception { + EventContext ctx = new EventContextImpl(); + RequestScopeContext scope = new RequestScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + + RequestScopeComponentImpl comp1 = (RequestScopeComponentImpl) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + scope.onEvent(EventContext.REQUEST_END, null); + + // second request + // should be null since the other context (thread) expired w/ onEvent(..) + Assert.assertNull((RequestScopeComponentImpl) scope.getContextByKey("TestService1", Thread.currentThread())); + // Note should test better using concurrent threads to pull the instance + + scope.stop(); + } + + // ---------------------------------- + // Private methods + // ---------------------------------- + + JavaComponentContextBuilder builder = new JavaComponentContextBuilder(); + + private List<RuntimeConfiguration<InstanceContext>> createConfigurations() throws NoSuchMethodException, BuilderException { + SimpleComponent component = MockFactory.createComponent("TestService1", RequestScopeComponentImpl.class, + Scope.REQUEST); + builder.build(component, null); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + configs.add((RuntimeConfiguration<InstanceContext>) component.getComponentImplementation().getRuntimeConfiguration()); + return configs; + } + + private RuntimeConfiguration<InstanceContext> createConfiguration(String name) throws NoSuchMethodException, + BuilderException { + SimpleComponent component = MockFactory.createComponent(name, RequestScopeComponentImpl.class, + Scope.REQUEST); + builder.build(component, null); + return (RuntimeConfiguration<InstanceContext>) component.getComponentImplementation().getRuntimeConfiguration(); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicStatelessScopeTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicStatelessScopeTestCase.java new file mode 100644 index 0000000000..807a0901ce --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/BasicStatelessScopeTestCase.java @@ -0,0 +1,125 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.builder.JavaComponentContextBuilder; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.StatelessComponent; +import org.apache.tuscany.container.java.mock.components.StatelessComponentImpl; +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.RuntimeConfiguration; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.StatelessScopeContext; +import org.apache.tuscany.model.assembly.Scope; +import org.apache.tuscany.model.assembly.SimpleComponent; + +/** + * Unit tests for the module scope container + * + * @version $Rev$ $Date$ + */ +public class BasicStatelessScopeTestCase extends TestCase { + + /** + * Tests instance identity is properly maintained + */ + public void testInstanceManagement() throws Exception { + EventContext ctx = new EventContextImpl(); + StatelessScopeContext scope = new StatelessScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + // first request + StatelessComponentImpl comp1 = (StatelessComponentImpl) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + // second request + StatelessComponentImpl comp2 = (StatelessComponentImpl) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp2); + Assert.assertNotSame(comp1, comp2); + scope.stop(); + } + + public void testRegisterContextBeforeRequest() throws Exception { + EventContext ctx = new EventContextImpl(); + StatelessScopeContext scope = new StatelessScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.registerConfiguration(createConfiguration("NewTestService")); + scope.start(); + StatelessComponent comp1 = (StatelessComponent) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + StatelessComponent comp2 = (StatelessComponent) scope.getContext("NewTestService").getInstance(null); + Assert.assertNotNull(comp2); + scope.stop(); + } + + public void testRegisterContextAfterRequest() throws Exception { + EventContext ctx = new EventContextImpl(); + StatelessScopeContext scope = new StatelessScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + StatelessComponent comp1 = (StatelessComponent) scope.getContext("TestService1").getInstance(null); + Assert.assertNotNull(comp1); + scope.registerConfiguration(createConfiguration("NewTestService")); + StatelessComponent comp2 = (StatelessComponent) scope.getContext("NewTestService").getInstance(null); + Assert.assertNotNull(comp2); + scope.stop(); + } + + + /** + * Tests setting no components in the scope + */ + public void testSetNullComponents() throws Exception { + EventContext ctx = new EventContextImpl(); + StatelessScopeContext scope = new StatelessScopeContext(ctx); + scope.registerConfigurations(createConfigurations()); + scope.start(); + scope.stop(); + } + + // ---------------------------------- + // Private methods + // ---------------------------------- + + JavaComponentContextBuilder builder = new JavaComponentContextBuilder(); + + private List<RuntimeConfiguration<InstanceContext>> createConfigurations() + throws NoSuchMethodException, BuilderException { + SimpleComponent component = MockFactory.createComponent("TestService1", StatelessComponentImpl.class, + Scope.INSTANCE); + builder.build(component, null); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + configs.add((RuntimeConfiguration<InstanceContext>) component.getComponentImplementation().getRuntimeConfiguration()); + return configs; + } + + private RuntimeConfiguration<InstanceContext> createConfiguration(String name) + throws NoSuchMethodException, BuilderException { + SimpleComponent component = MockFactory.createComponent(name, StatelessComponentImpl.class, + Scope.INSTANCE); + builder.build(component, null); + return (RuntimeConfiguration<InstanceContext>) component.getComponentImplementation().getRuntimeConfiguration(); + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/HttpSessionScopeLifecycleTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/HttpSessionScopeLifecycleTestCase.java new file mode 100644 index 0000000000..1d5780dac4 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/HttpSessionScopeLifecycleTestCase.java @@ -0,0 +1,157 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.builder.JavaComponentContextBuilder; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.SessionScopeDestroyOnlyComponent; +import org.apache.tuscany.container.java.mock.components.SessionScopeInitDestroyComponent; +import org.apache.tuscany.container.java.mock.components.SessionScopeInitOnlyComponent; +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.RuntimeConfiguration; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.HttpSessionScopeContext; +import org.apache.tuscany.model.assembly.Scope; +import org.apache.tuscany.model.assembly.SimpleComponent; + +/** + * Lifecycle unit tests for the Http session scope container + * + * @version $Rev$ $Date$ + */ +public class HttpSessionScopeLifecycleTestCase extends TestCase { + + /** + * Tests instance identity is properly maintained + */ + public void testInitDestroy() throws Exception { + EventContext ctx = new EventContextImpl(); + HttpSessionScopeContext scope = new HttpSessionScopeContext(ctx); + scope.registerConfigurations(createComponents()); + scope.start(); + Object session = new Object(); + Object session2 = new Object(); + // first request, no need to notify scope container since sessions are + // evaluated lazily + ctx.setIdentifier(EventContext.HTTP_SESSION, session); + SessionScopeInitDestroyComponent initDestroy = (SessionScopeInitDestroyComponent) scope.getContext( + "TestServiceInitDestroy").getInstance(null); + Assert.assertNotNull(initDestroy); + SessionScopeInitOnlyComponent initOnly = (SessionScopeInitOnlyComponent) scope.getContext("TestServiceInitOnly") + .getInstance(null); + Assert.assertNotNull(initOnly); + SessionScopeDestroyOnlyComponent destroyOnly = (SessionScopeDestroyOnlyComponent) scope.getContext( + "TestServiceDestroyOnly").getInstance(null); + Assert.assertNotNull(destroyOnly); + + Assert.assertTrue(initDestroy.isInitialized()); + Assert.assertTrue(initOnly.isInitialized()); + Assert.assertFalse(initDestroy.isDestroyed()); + Assert.assertFalse(destroyOnly.isDestroyed()); + // end request + ctx.clearIdentifier(EventContext.HTTP_SESSION); + // expire session + scope.onEvent(EventContext.SESSION_END, session); + Assert.assertTrue(initDestroy.isDestroyed()); + Assert.assertTrue(destroyOnly.isDestroyed()); + + scope.stop(); + } + + /** + * Test instances destroyed in proper (i.e. reverse) order + */ + public void testDestroyOrder() throws Exception { + EventContext ctx = new EventContextImpl(); + HttpSessionScopeContext scope = new HttpSessionScopeContext(ctx); + scope.registerConfigurations(createOrderedInitComponents()); + scope.start(); + Object session = new Object(); + Object session2 = new Object(); + // request start + ctx.setIdentifier(EventContext.HTTP_SESSION, session); + + OrderedInitPojo one = (OrderedInitPojo) scope.getContext("one").getInstance(null); + Assert.assertNotNull(one); + Assert.assertEquals(1, one.getNumberInstantiated()); + Assert.assertEquals(1, one.getInitOrder()); + + OrderedInitPojo two = (OrderedInitPojo) scope.getContext("two").getInstance(null); + Assert.assertNotNull(two); + Assert.assertEquals(2, two.getNumberInstantiated()); + Assert.assertEquals(2, two.getInitOrder()); + + OrderedInitPojo three = (OrderedInitPojo) scope.getContext("three").getInstance(null); + Assert.assertNotNull(three); + Assert.assertEquals(3, three.getNumberInstantiated()); + Assert.assertEquals(3, three.getInitOrder()); + + // end request + ctx.clearIdentifier(EventContext.HTTP_SESSION); + + // expire session + scope.onEvent(EventContext.SESSION_END, session); + Assert.assertEquals(0, one.getNumberInstantiated()); + scope.stop(); + } + + // ---------------------------------- + // Private methods + // ---------------------------------- + + JavaComponentContextBuilder builder = new JavaComponentContextBuilder(); + + private List<RuntimeConfiguration<InstanceContext>> createComponents() throws NoSuchMethodException, BuilderException { + SimpleComponent[] ca = new SimpleComponent[3]; + ca[0] = MockFactory.createComponent("TestServiceInitDestroy", SessionScopeInitDestroyComponent.class, + Scope.SESSION); + ca[1] = MockFactory.createComponent("TestServiceInitOnly", SessionScopeInitOnlyComponent.class, Scope.SESSION); + ca[2] = MockFactory.createComponent("TestServiceDestroyOnly", SessionScopeDestroyOnlyComponent.class, + Scope.SESSION); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + for (int i = 0; i < ca.length; i++) { + builder.build(ca[i], null); + configs.add((RuntimeConfiguration<InstanceContext>) ca[i].getComponentImplementation().getRuntimeConfiguration()); + + } + return configs; + } + + private List<RuntimeConfiguration<InstanceContext>> createOrderedInitComponents() throws NoSuchMethodException, + BuilderException { + SimpleComponent[] ca = new SimpleComponent[3]; + ca[0] = MockFactory.createComponent("one", OrderedInitPojo.class, Scope.SESSION); + ca[1] = MockFactory.createComponent("two", OrderedInitPojo.class, Scope.SESSION); + ca[2] = MockFactory.createComponent("three", OrderedInitPojo.class, Scope.SESSION); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + for (int i = 0; i < ca.length; i++) { + builder.build(ca[i], null); + configs.add((RuntimeConfiguration<InstanceContext>) ca[i].getComponentImplementation().getRuntimeConfiguration()); + + } + return configs; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/ModuleScopeLifecycleTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/ModuleScopeLifecycleTestCase.java new file mode 100644 index 0000000000..2c8a79de14 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/ModuleScopeLifecycleTestCase.java @@ -0,0 +1,224 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.builder.JavaComponentContextBuilder; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.ModuleScopeDestroyOnlyComponent; +import org.apache.tuscany.container.java.mock.components.ModuleScopeEagerInitComponent; +import org.apache.tuscany.container.java.mock.components.ModuleScopeEagerInitDestroyComponent; +import org.apache.tuscany.container.java.mock.components.ModuleScopeInitDestroyComponent; +import org.apache.tuscany.container.java.mock.components.ModuleScopeInitOnlyComponent; +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.RuntimeConfiguration; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.ModuleScopeContext; +import org.apache.tuscany.model.assembly.Scope; +import org.apache.tuscany.model.assembly.SimpleComponent; + +/** + * Lifecycle unit tests for the Http session scope container + * + * @version $Rev$ $Date$ + */ +public class ModuleScopeLifecycleTestCase extends TestCase { + + public void testInitDestroy() throws Exception { + EventContext ctx = new EventContextImpl(); + ModuleScopeContext scope = new ModuleScopeContext(ctx); + scope.registerConfigurations(createComponents()); + scope.start(); + scope.onEvent(EventContext.MODULE_START, null); + ModuleScopeInitDestroyComponent initDestroy = (ModuleScopeInitDestroyComponent) scope.getContext( + "TestServiceInitDestroy").getInstance(null); + Assert.assertNotNull(initDestroy); + ModuleScopeInitOnlyComponent initOnly = (ModuleScopeInitOnlyComponent) scope.getContext("TestServiceInitOnly") + .getInstance(null); + Assert.assertNotNull(initOnly); + ModuleScopeDestroyOnlyComponent destroyOnly = (ModuleScopeDestroyOnlyComponent) scope.getContext( + "TestServiceDestroyOnly").getInstance(null); + Assert.assertNotNull(destroyOnly); + + Assert.assertTrue(initDestroy.isInitialized()); + Assert.assertTrue(initOnly.isInitialized()); + Assert.assertFalse(initDestroy.isDestroyed()); + Assert.assertFalse(destroyOnly.isDestroyed()); + + // expire module + scope.onEvent(EventContext.MODULE_STOP, null); + + Assert.assertTrue(initDestroy.isDestroyed()); + Assert.assertTrue(destroyOnly.isDestroyed()); + + scope.stop(); + } + + public void testEagerInit() throws Exception { + EventContext ctx = new EventContextImpl(); + ModuleScopeContext scope = new ModuleScopeContext(ctx); + scope.registerConfigurations(createEagerInitComponents()); + scope.start(); + scope.onEvent(EventContext.MODULE_START, null); + ModuleScopeEagerInitDestroyComponent initDestroy = (ModuleScopeEagerInitDestroyComponent) scope.getContext( + "TestServiceEagerInitDestroy").getInstance(null); + Assert.assertNotNull(initDestroy); + ModuleScopeEagerInitComponent initOnly = (ModuleScopeEagerInitComponent) scope + .getContext("TestServiceEagerInit").getInstance(null); + Assert.assertNotNull(initOnly); + + Assert.assertTrue(initDestroy.isInitialized()); + Assert.assertTrue(initOnly.isInitialized()); + Assert.assertFalse(initDestroy.isDestroyed()); + + // expire module + scope.onEvent(EventContext.MODULE_STOP, null); + + Assert.assertTrue(initDestroy.isDestroyed()); + + scope.stop(); + + } + + public void testDestroyOrder() throws Exception { + EventContext ctx = new EventContextImpl(); + ModuleScopeContext scope = new ModuleScopeContext(ctx); + scope.registerConfigurations(createOrderedInitComponents()); + scope.start(); + scope.onEvent(EventContext.MODULE_START, null); + OrderedInitPojo one = (OrderedInitPojo) scope.getContext("one").getInstance(null); + Assert.assertNotNull(one); + Assert.assertEquals(1, one.getNumberInstantiated()); + Assert.assertEquals(1, one.getInitOrder()); + + OrderedInitPojo two = (OrderedInitPojo) scope.getContext("two").getInstance(null); + Assert.assertNotNull(two); + Assert.assertEquals(2, two.getNumberInstantiated()); + Assert.assertEquals(2, two.getInitOrder()); + + OrderedInitPojo three = (OrderedInitPojo) scope.getContext("three").getInstance(null); + Assert.assertNotNull(three); + Assert.assertEquals(3, three.getNumberInstantiated()); + Assert.assertEquals(3, three.getInitOrder()); + + // expire module + scope.onEvent(EventContext.MODULE_STOP, null); + Assert.assertEquals(0, one.getNumberInstantiated()); + scope.stop(); + } + + public void testEagerInitDestroyOrder() throws Exception { + EventContext ctx = new EventContextImpl(); + ModuleScopeContext scope = new ModuleScopeContext(ctx); + scope.registerConfigurations(createOrderedEagerInitComponents()); + scope.start(); + scope.onEvent(EventContext.MODULE_START, null); + OrderedEagerInitPojo one = (OrderedEagerInitPojo) scope.getContext("one").getInstance(null); + Assert.assertNotNull(one); + + OrderedEagerInitPojo two = (OrderedEagerInitPojo) scope.getContext("two").getInstance(null); + Assert.assertNotNull(two); + + OrderedEagerInitPojo three = (OrderedEagerInitPojo) scope.getContext("three").getInstance(null); + Assert.assertNotNull(three); + + // expire module + scope.onEvent(EventContext.MODULE_STOP, null); + Assert.assertEquals(0, one.getNumberInstantiated()); + scope.stop(); + } + + // ---------------------------------- + // Private methods + // ---------------------------------- + + JavaComponentContextBuilder builder = new JavaComponentContextBuilder(); + + private List<RuntimeConfiguration<InstanceContext>> createComponents() throws NoSuchMethodException, BuilderException { + SimpleComponent[] ca = new SimpleComponent[3]; + ca[0] = MockFactory.createComponent("TestServiceInitDestroy", ModuleScopeInitDestroyComponent.class, + Scope.MODULE); + ca[1] = MockFactory.createComponent("TestServiceInitOnly", ModuleScopeInitOnlyComponent.class, + Scope.MODULE); + ca[2] = MockFactory.createComponent("TestServiceDestroyOnly", ModuleScopeDestroyOnlyComponent.class, + Scope.MODULE); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + for (int i = 0; i < ca.length; i++) { + builder.build(ca[i], null); + configs.add((RuntimeConfiguration<InstanceContext>) ca[i].getComponentImplementation() + .getRuntimeConfiguration()); + + } + return configs; + } + + private List<RuntimeConfiguration<InstanceContext>> createEagerInitComponents() throws NoSuchMethodException, + BuilderException { + SimpleComponent[] ca = new SimpleComponent[2]; + ca[0] = MockFactory.createComponent("TestServiceEagerInitDestroy", ModuleScopeEagerInitDestroyComponent.class, + Scope.MODULE); + ca[1] = MockFactory.createComponent("TestServiceEagerInit", ModuleScopeEagerInitComponent.class, + Scope.MODULE); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + for (int i = 0; i < ca.length; i++) { + builder.build(ca[i], null); + configs.add((RuntimeConfiguration<InstanceContext>) ca[i].getComponentImplementation() + .getRuntimeConfiguration()); + + } + return configs; + } + + private List<RuntimeConfiguration<InstanceContext>> createOrderedInitComponents() throws NoSuchMethodException, + BuilderException { + SimpleComponent[] ca = new SimpleComponent[3]; + ca[0] = MockFactory.createComponent("one", OrderedInitPojo.class, Scope.MODULE); + ca[1] = MockFactory.createComponent("two", OrderedInitPojo.class, Scope.MODULE); + ca[2] = MockFactory.createComponent("three", OrderedInitPojo.class, Scope.MODULE); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + for (int i = 0; i < ca.length; i++) { + builder.build(ca[i], null); + configs.add((RuntimeConfiguration<InstanceContext>) ca[i].getComponentImplementation() + .getRuntimeConfiguration()); + + } + return configs; + } + + private List<RuntimeConfiguration<InstanceContext>> createOrderedEagerInitComponents() throws NoSuchMethodException, + BuilderException { + SimpleComponent[] ca = new SimpleComponent[3]; + ca[0] = MockFactory.createComponent("one", OrderedEagerInitPojo.class, Scope.MODULE); + ca[1] = MockFactory.createComponent("two", OrderedEagerInitPojo.class, Scope.MODULE); + ca[2] = MockFactory.createComponent("three", OrderedEagerInitPojo.class, Scope.MODULE); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + for (int i = 0; i < ca.length; i++) { + builder.build(ca[i], null); + configs.add((RuntimeConfiguration<InstanceContext>) ca[i].getComponentImplementation() + .getRuntimeConfiguration()); + + } + return configs; + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/OrderException.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/OrderException.java new file mode 100644 index 0000000000..fc5f7c8c58 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/OrderException.java @@ -0,0 +1,38 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +public class OrderException extends Exception { + + public OrderException() { + super(); + } + + public OrderException(String message) { + super(message); + } + + public OrderException(String message, Throwable cause) { + super(message, cause); + } + + public OrderException(Throwable cause) { + super(cause); + } + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/OrderedEagerInitPojo.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/OrderedEagerInitPojo.java new file mode 100644 index 0000000000..d439159550 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/OrderedEagerInitPojo.java @@ -0,0 +1,54 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Init; + +public class OrderedEagerInitPojo { + + private static Object lock = new Object(); + private static int numberInstantied; + private int initOrder; + + @Init(eager = true) + public void init() { + synchronized (lock) { + ++numberInstantied; + initOrder = numberInstantied; + } + } + + @Destroy + public void destroy() throws OrderException { + synchronized (lock) { + if (initOrder != numberInstantied) { + throw new OrderException("Instance shutdown done out of order"); + } + --numberInstantied; + } + } + + public int getNumberInstantiated() { + return numberInstantied; + } + + public int getInitOrder() { + return initOrder; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/OrderedInitPojo.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/OrderedInitPojo.java new file mode 100644 index 0000000000..ee5c5145c2 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/OrderedInitPojo.java @@ -0,0 +1,54 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Init; + +public class OrderedInitPojo { + + private static Object lock = new Object(); + private static int numberInstantied; + private int initOrder; + + @Init + public void init() { + synchronized (lock) { + ++numberInstantied; + initOrder = numberInstantied; + } + } + + @Destroy + public void destroy() throws OrderException { + synchronized (lock) { + if (initOrder != numberInstantied) { + throw new OrderException("Instance shutdown done out of order"); + } + --numberInstantied; + } + } + + public int getNumberInstantiated() { + return numberInstantied; + } + + public int getInitOrder() { + return initOrder; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/RequestScopeLifecycleTestCase.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/RequestScopeLifecycleTestCase.java new file mode 100644 index 0000000000..1979d9b25c --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/scopes/RequestScopeLifecycleTestCase.java @@ -0,0 +1,146 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.container.java.scopes; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.container.java.builder.JavaComponentContextBuilder; +import org.apache.tuscany.container.java.mock.MockFactory; +import org.apache.tuscany.container.java.mock.components.SessionScopeDestroyOnlyComponent; +import org.apache.tuscany.container.java.mock.components.SessionScopeInitDestroyComponent; +import org.apache.tuscany.container.java.mock.components.SessionScopeInitOnlyComponent; +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.RuntimeConfiguration; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.impl.EventContextImpl; +import org.apache.tuscany.core.context.scope.RequestScopeContext; +import org.apache.tuscany.model.assembly.Scope; +import org.apache.tuscany.model.assembly.SimpleComponent; + +/** + * Lifecycle unit tests for the Http session scope container + * + * @version $Rev$ $Date$ + */ +public class RequestScopeLifecycleTestCase extends TestCase { + + /** + * Tests instance identity is properly maintained + */ + public void testInitDestroy() throws Exception { + EventContext ctx = new EventContextImpl(); + RequestScopeContext scope = new RequestScopeContext(ctx); + scope.registerConfigurations(createComponents()); + scope.start(); + SessionScopeInitDestroyComponent initDestroy = (SessionScopeInitDestroyComponent) scope.getContext( + "TestServiceInitDestroy").getInstance(null); + Assert.assertNotNull(initDestroy); + SessionScopeInitOnlyComponent initOnly = (SessionScopeInitOnlyComponent) scope.getContext("TestServiceInitOnly") + .getInstance(null); + Assert.assertNotNull(initOnly); + SessionScopeDestroyOnlyComponent destroyOnly = (SessionScopeDestroyOnlyComponent) scope.getContext( + "TestServiceDestroyOnly").getInstance(null); + Assert.assertNotNull(destroyOnly); + + Assert.assertTrue(initDestroy.isInitialized()); + Assert.assertTrue(initOnly.isInitialized()); + Assert.assertFalse(initDestroy.isDestroyed()); + Assert.assertFalse(destroyOnly.isDestroyed()); + + // end request + scope.onEvent(EventContext.REQUEST_END, null); + Assert.assertTrue(initDestroy.isDestroyed()); + Assert.assertTrue(destroyOnly.isDestroyed()); + + scope.stop(); + } + + /** + * Test instances destroyed in proper (i.e. reverse) order + */ + public void testDestroyOrder() throws Exception { + EventContext ctx = new EventContextImpl(); + RequestScopeContext scope = new RequestScopeContext(ctx); + scope.registerConfigurations(createOrderedInitComponents()); + scope.start(); + // request start + OrderedInitPojo one = (OrderedInitPojo) scope.getContext("one").getInstance(null); + Assert.assertNotNull(one); + Assert.assertEquals(1, one.getNumberInstantiated()); + Assert.assertEquals(1, one.getInitOrder()); + + OrderedInitPojo two = (OrderedInitPojo) scope.getContext("two").getInstance(null); + Assert.assertNotNull(two); + Assert.assertEquals(2, two.getNumberInstantiated()); + Assert.assertEquals(2, two.getInitOrder()); + + OrderedInitPojo three = (OrderedInitPojo) scope.getContext("three").getInstance(null); + Assert.assertNotNull(three); + Assert.assertEquals(3, three.getNumberInstantiated()); + Assert.assertEquals(3, three.getInitOrder()); + + // end request + scope.onEvent(EventContext.REQUEST_END, null); + + Assert.assertEquals(0, one.getNumberInstantiated()); + scope.stop(); + } + + // ---------------------------------- + // Private methods + // ---------------------------------- + + JavaComponentContextBuilder builder = new JavaComponentContextBuilder(); + + private List<RuntimeConfiguration<InstanceContext>> createComponents() throws NoSuchMethodException, BuilderException { + SimpleComponent[] ca = new SimpleComponent[3]; + ca[0] = MockFactory.createComponent("TestServiceInitDestroy", SessionScopeInitDestroyComponent.class, + Scope.REQUEST); + ca[1] = MockFactory.createComponent("TestServiceInitOnly", SessionScopeInitOnlyComponent.class, + Scope.REQUEST); + ca[2] = MockFactory.createComponent("TestServiceDestroyOnly", SessionScopeDestroyOnlyComponent.class, + Scope.REQUEST); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + for (int i = 0; i < ca.length; i++) { + builder.build(ca[i], null); + configs.add((RuntimeConfiguration<InstanceContext>) ca[i].getComponentImplementation().getRuntimeConfiguration()); + + } + return configs; + } + + private List<RuntimeConfiguration<InstanceContext>> createOrderedInitComponents() throws NoSuchMethodException, + BuilderException { + SimpleComponent[] ca = new SimpleComponent[3]; + ca[0] = MockFactory.createComponent("one", OrderedInitPojo.class, Scope.REQUEST); + ca[1] = MockFactory.createComponent("two", OrderedInitPojo.class, Scope.REQUEST); + ca[2] = MockFactory.createComponent("three", OrderedInitPojo.class, Scope.REQUEST); + List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); + for (int i = 0; i < ca.length; i++) { + builder.build(ca[i], null); + configs.add((RuntimeConfiguration<InstanceContext>) ca[i].getComponentImplementation().getRuntimeConfiguration()); + + } + return configs; + } + +} |