diff options
Diffstat (limited to 'sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java')
36 files changed, 5322 insertions, 0 deletions
diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/AddService.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/AddService.java new file mode 100644 index 0000000000..9c22177684 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/AddService.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +/** + * Interface for the Add Service. + * + * @version $Rev$ $Date$ + */ +public interface AddService { + + double add(double n1, double n2); + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/AddServiceImpl.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/AddServiceImpl.java new file mode 100644 index 0000000000..64298b6275 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/AddServiceImpl.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +import org.oasisopen.sca.annotation.EagerInit; +import org.oasisopen.sca.annotation.Scope; + +/** + * An implementation of the Add service. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +@EagerInit +public class AddServiceImpl implements AddService { + + public double add(double n1, double n2) { + return n1 + n2; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/CalculatorService.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/CalculatorService.java new file mode 100644 index 0000000000..e0cf23dc17 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/CalculatorService.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +/** + * The Calculator service interface. + * + * @version $Rev$ $Date$ + */ +public interface CalculatorService { + + double add(double n1, double n2); + + double subtract(double n1, double n2); + + double multiply(double n1, double n2); + + double divide(double n1, double n2); + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/CalculatorServiceImpl.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/CalculatorServiceImpl.java new file mode 100644 index 0000000000..9f64dc8bca --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/CalculatorServiceImpl.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Scope; + +/** + * An implementation of the Calculator service. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +public class CalculatorServiceImpl implements CalculatorService { + + private AddService addService; + private SubtractService subtractService; + private MultiplyService multiplyService; + private DivideService divideService; + + @Reference + public void setAddService(AddService addService) { + this.addService = addService; + } + + @Reference + public void setSubtractService(SubtractService subtractService) { + this.subtractService = subtractService; + } + + @Reference + public void setDivideService(DivideService divideService) { + this.divideService = divideService; + } + + @Reference + public void setMultiplyService(MultiplyService multiplyService) { + this.multiplyService = multiplyService; + } + + public double add(double n1, double n2) { + return addService.add(n1, n2); + } + + public double subtract(double n1, double n2) { + return subtractService.subtract(n1, n2); + } + + public double multiply(double n1, double n2) { + return multiplyService.multiply(n1, n2); + } + + public double divide(double n1, double n2) { + return divideService.divide(n1, n2); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/DivideService.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/DivideService.java new file mode 100644 index 0000000000..9599c86292 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/DivideService.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +/** + * Interface for the Divide Service. + * + * @version $Rev$ $Date$ + */ +public interface DivideService { + + double divide(double n1, double n2); + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/DivideServiceImpl.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/DivideServiceImpl.java new file mode 100644 index 0000000000..f5b8ff5be0 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/DivideServiceImpl.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +import org.oasisopen.sca.annotation.Scope; + +/** + * An implementation of the Divide service. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +public class DivideServiceImpl implements DivideService { + + public double divide(double n1, double n2) { + return n1 / n2; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/MultiplyService.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/MultiplyService.java new file mode 100644 index 0000000000..7a3d06c15e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/MultiplyService.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +/** + * Interface for the Multiply Service. + * + * @version $Rev$ $Date$ + */ +public interface MultiplyService { + + double multiply(double n1, double n2); + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/MultiplyServiceImpl.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/MultiplyServiceImpl.java new file mode 100644 index 0000000000..d146d23153 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/MultiplyServiceImpl.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +import org.oasisopen.sca.annotation.Scope; + +/** + * An implementation of the Multiply service. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +public class MultiplyServiceImpl implements MultiplyService { + + public double multiply(double n1, double n2) { + return n1 * n2; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/SubtractService.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/SubtractService.java new file mode 100644 index 0000000000..21af91f392 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/SubtractService.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +/** + * Interface for the Subtract Service. + * + * @version $Rev$ $Date$ + */ +public interface SubtractService { + + double subtract(double n1, double n2); + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/SubtractServiceImpl.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/SubtractServiceImpl.java new file mode 100644 index 0000000000..ffe09c3dde --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/calculator/SubtractServiceImpl.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +import org.oasisopen.sca.annotation.Scope; + +/** + * An implementation of the subtract service. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +public class SubtractServiceImpl implements SubtractService { + + public double subtract(double n1, double n2) { + return n1 - n2; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractProcessorTest.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractProcessorTest.java new file mode 100644 index 0000000000..b77aea4c78 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractProcessorTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; + + +/** + * Base class to simulate the processor sequences + * + * @version $Rev$ $Date$ + */ +public abstract class AbstractProcessorTest { + protected AssemblyFactory factory; + protected JavaInterfaceFactory javaFactory; + protected ConstructorProcessor constructorProcessor; + protected ReferenceProcessor referenceProcessor; + protected PropertyProcessor propertyProcessor; + private ResourceProcessor resourceProcessor; + + + protected AbstractProcessorTest() { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + factory = new DefaultAssemblyFactory(registry); + javaFactory = new DefaultJavaInterfaceFactory(registry); + referenceProcessor = new ReferenceProcessor(factory, javaFactory); + propertyProcessor = new PropertyProcessor(registry); + resourceProcessor = new ResourceProcessor(factory); + constructorProcessor = new ConstructorProcessor(factory); + referenceProcessor = new ReferenceProcessor(factory, javaFactory); + propertyProcessor = new PropertyProcessor(registry); + } + + protected <T> void visitConstructor(Constructor<T> constructor, + JavaImplementation type) throws IntrospectionException { + constructorProcessor.visitConstructor(constructor, type); + JavaConstructorImpl<?> definition = type.getConstructor(); + if (definition == null) { + definition = new JavaConstructorImpl<T>(constructor); + type.getConstructors().put(constructor, definition); + } + + JavaParameterImpl[] parameters = definition.getParameters(); + for (int i = 0; i < parameters.length; i++) { + referenceProcessor.visitConstructorParameter(parameters[i], type); + propertyProcessor.visitConstructorParameter(parameters[i], type); + resourceProcessor.visitConstructorParameter(parameters[i], type); + // monitorProcessor.visitConstructorParameter(parameters[i], type); + } + } + + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AllowsPassByReferenceProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AllowsPassByReferenceProcessorTestCase.java new file mode 100644 index 0000000000..ce79ae2045 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AllowsPassByReferenceProcessorTestCase.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.AllowsPassByReference; + +/** + * @version $Rev$ $Date$ + */ +public class AllowsPassByReferenceProcessorTestCase { + + JavaImplementation type; + AllowsPassByReferenceProcessor processor; + private JavaImplementationFactory javaImplementationFactory; + + @Test + public void testClassAnnotation() throws Exception { + processor.visitClass(Foo.class, type); + assertEquals(true, type.isAllowsPassByReference()); + + processor.visitClass(Bar.class, type); + assertEquals(false, type.isAllowsPassByReference()); + + Method m1 = Bar.class.getMethod("m1", new Class[] {}); + processor.visitMethod(m1, type); + assertTrue(type.isAllowsPassByReference(m1)); + } + + @Before + public void setUp() throws Exception { + javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + processor = new AllowsPassByReferenceProcessor(new DefaultAssemblyFactory()); + } + + @AllowsPassByReference + private class Foo { + } + + // no annotation + private class Bar { + @AllowsPassByReference + public void m1() { + + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorProcessorTestCase.java new file mode 100644 index 0000000000..512e34a0fa --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorProcessorTestCase.java @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getProperty; +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getReference; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Constructor; +import java.util.Collection; +import java.util.List; +import java.util.Set; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorProcessorTestCase { + private ConstructorProcessor processor = new ConstructorProcessor(new DefaultAssemblyFactory()); + + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testDuplicateConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitClass(BadFoo.class, type); + fail(); + } catch (DuplicateConstructorException e) { + // expected + } + } + + @Test + public void testConstructorAnnotation() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor1 = Foo.class.getConstructor(String.class); + processor.visitConstructor(ctor1, type); + assertEquals("foo", type.getConstructor().getParameters()[0].getName()); + } + + @Test + public void testNoAnnotation() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<NoAnnotation> ctor1 = NoAnnotation.class.getConstructor(); + processor.visitConstructor(ctor1, type); + assertNull(type.getConstructor()); + } + + @Test + public void testBadAnnotation() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadAnnotation> ctor1 = BadAnnotation.class.getConstructor(String.class, Foo.class); + try { + processor.visitConstructor(ctor1, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + @Test + public void testMixedParameters() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Mixed> ctor1 = Mixed.class.getConstructor(String.class, String.class, String.class); + processor.visitConstructor(ctor1, type); + + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + AssemblyFactory assemblyFactory = new DefaultAssemblyFactory(registry); + JavaInterfaceFactory javaFactory = new DefaultJavaInterfaceFactory(registry); + ReferenceProcessor referenceProcessor = new ReferenceProcessor(assemblyFactory, javaFactory); + PropertyProcessor propertyProcessor = new PropertyProcessor(registry); + JavaParameterImpl[] parameters = type.getConstructor().getParameters(); + for (int i = 0; i < parameters.length; i++) { + referenceProcessor.visitConstructorParameter(parameters[i], type); + propertyProcessor.visitConstructorParameter(parameters[i], type); + } + + assertEquals("_ref0", parameters[0].getName()); + assertEquals("foo", parameters[1].getName()); + assertEquals("bar", parameters[2].getName()); + } + + private static class BadFoo { + + @org.oasisopen.sca.annotation.Constructor("foo") + public BadFoo(String foo) { + + } + + @org.oasisopen.sca.annotation.Constructor( {"foo", "bar"}) + public BadFoo(String foo, String bar) { + + } + } + + private static class Foo { + @org.oasisopen.sca.annotation.Constructor("foo") + public Foo(@Property String foo) { + + } + } + + private static class NoAnnotation { + public NoAnnotation() { + } + } + + private static class BadAnnotation { + @org.oasisopen.sca.annotation.Constructor("foo") + public BadAnnotation(String foo, Foo ref) { + } + } + + public static final class Mixed { + @org.oasisopen.sca.annotation.Constructor + public Mixed(@Reference(name="_ref0") + String param1, @Property(name = "foo") + String param2, @Reference(name = "bar") + String param3) { + } + } + + public static final class Multiple { + @org.oasisopen.sca.annotation.Constructor + public Multiple(@Reference(name="_ref0") + Collection<String> param1, @Property(name = "foo") + String[] param2, @Reference(name = "bar", required = true) + List<String> param3, @Property(name = "abc") + Set<String> param4, @Reference(name = "xyz") + String[] param5) { + } + } + + @Test + public void testMultiplicity() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Multiple> ctor1 = Multiple.class.getConstructor(Collection.class, + String[].class, + List.class, + Set.class, + String[].class); + processor.visitConstructor(ctor1, type); + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + AssemblyFactory assemblyFactory = new DefaultAssemblyFactory(); + JavaInterfaceFactory javaFactory = new DefaultJavaInterfaceFactory(registry); + ReferenceProcessor referenceProcessor = new ReferenceProcessor(assemblyFactory, javaFactory); + PropertyProcessor propertyProcessor = new PropertyProcessor(registry); + JavaParameterImpl[] parameters = type.getConstructor().getParameters(); + for (int i = 0; i < parameters.length; i++) { + referenceProcessor.visitConstructorParameter(parameters[i], type); + propertyProcessor.visitConstructorParameter(parameters[i], type); + } + + org.apache.tuscany.sca.assembly.Reference ref0 = getReference(type, "_ref0"); + assertNotNull(ref0); + assertEquals(Multiplicity.ONE_N, ref0.getMultiplicity()); + org.apache.tuscany.sca.assembly.Reference ref1 = getReference(type, "bar"); + assertNotNull(ref1); + assertEquals(Multiplicity.ONE_N, ref1.getMultiplicity()); + org.apache.tuscany.sca.assembly.Reference ref2 = getReference(type, "xyz"); + assertNotNull(ref2); + assertEquals(Multiplicity.ONE_N, ref2.getMultiplicity()); + org.apache.tuscany.sca.assembly.Property prop1 = getProperty(type, "foo"); + assertNotNull(prop1); + assertTrue(prop1.isMany()); + org.apache.tuscany.sca.assembly.Property prop2 = getProperty(type, "abc"); + assertNotNull(prop2); + assertTrue(prop2.isMany()); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java new file mode 100644 index 0000000000..83e26dff84 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getProperty; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.List; + +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorPropertyTestCase extends AbstractProcessorTest { + + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testProperty() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class); + visitConstructor(ctor, type); + org.apache.tuscany.sca.assembly.Property property = getProperty(type, "myProp"); + assertTrue(property.isMustSupply()); + assertEquals("myProp", property.getName()); + } + + @Test + public void testTwoPropertiesSameType() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + visitConstructor(ctor, type); + assertNotNull(getProperty(type, "myProp1")); + assertNotNull(getProperty(type, "myProp2")); + } + + @Test + public void testDuplicateProperty() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class, String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + @Test + public void testNoName() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidPropertyException e) { + // expected + } + } + + @Test + public void testNamesOnConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(Integer.class); + visitConstructor(ctor, type); + assertNotNull(getProperty(type, "myProp")); + } + + @Test + public void testInvalidNumberOfNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(Integer.class, Integer.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + @Test + public void testNoMatchingNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(List.class, List.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + +// public void testMultiplicityRequired() throws Exception { + // TODO multiplicity +// } + + + + private static class Foo { + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Property(name = "myProp", required = true)String prop) { + + } + + @org.oasisopen.sca.annotation.Constructor("myProp") + public Foo(@Property Integer prop) { + + } + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Property(name = "myProp1")String prop1, @Property(name = "myProp2")String prop2) { + + } + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Property List prop) { + + } + } + + private static class BadFoo { + + @org.oasisopen.sca.annotation.Constructor() + public BadFoo(@Property(name = "myProp")String prop1, @Property(name = "myProp")String prop2) { + + } + + @org.oasisopen.sca.annotation.Constructor() + public BadFoo(@Property String prop) { + + } + + @org.oasisopen.sca.annotation.Constructor("myProp") + public BadFoo(@Property Integer prop, @Property Integer prop2) { + + } + + @org.oasisopen.sca.annotation.Constructor({"myRef", "myRef2"}) + public BadFoo(@Property List ref, @Property(name = "myOtherRef")List ref2) { + + } + + } + + + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java new file mode 100644 index 0000000000..eeeee851e4 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java @@ -0,0 +1,188 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getReference; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.List; + +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.junit.Ignore; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; + +//import com.sun.xml.internal.ws.model.JavaMethodImpl; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorReferenceTestCase extends AbstractProcessorTest { + + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testReference() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class); + visitConstructor(ctor, type); + org.apache.tuscany.sca.assembly.Reference reference = getReference(type, "myRef"); + assertEquals(Multiplicity.ONE_ONE, reference.getMultiplicity()); + assertEquals("myRef", reference.getName()); + } + + @Test + public void testTwoReferencesSameType() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + visitConstructor(ctor, type); + assertNotNull(getReference(type, "myRef1")); + assertNotNull(getReference(type, "myRef2")); + } + + @Test + public void testDuplicateProperty() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class, String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (DuplicateReferenceException e) { + // expected + } + } + + @Test + public void testNoName() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<NoNameFoo> ctor = NoNameFoo.class.getConstructor(String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidReferenceException e) { + //expected + } + } + + @Test + public void testNamesOnConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(Integer.class); + visitConstructor(ctor, type); + assertNotNull(getReference(type, "myRef2")); + } + + @Test + public void testInvalidNumberOfNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(Integer.class, Integer.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + @Test + public void testNoMatchingNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(List.class, List.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidReferenceException e) { + // expected + } + } + + +// public void testMultiplicityRequired() throws Exception { + // TODO multiplicity +// } + + private static class Foo { + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Reference(name = "myRef", required = true)String prop) { + + } + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Reference(name = "myRef1")String prop1, @Reference(name = "myRef2")String prop2) { + + } + + @org.oasisopen.sca.annotation.Constructor("myRef2") + public Foo(@Reference(name = "myRef2") Integer prop) { + + } + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Reference(name = "myRef3") List prop) { + + } + } + + private static class NoNameFoo { + + @org.oasisopen.sca.annotation.Constructor + public NoNameFoo(@Reference String prop) { + + } + } + + private static class BadFoo { + + @org.oasisopen.sca.annotation.Constructor + public BadFoo(@Reference(name = "myRef")String prop1, @Reference(name = "myRef")String prop2) { + + } + + @org.oasisopen.sca.annotation.Constructor + public BadFoo(@Reference String prop) { + + } + + @org.oasisopen.sca.annotation.Constructor("myRef") + public BadFoo(@Reference Integer ref, @Reference Integer ref2) { + + } + + @org.oasisopen.sca.annotation.Constructor({"myRef", "myRef2"}) + public BadFoo(@Reference List ref, @Reference(name = "myOtherRef")List ref2) { + + } + + } + + + + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorResourceTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorResourceTestCase.java new file mode 100644 index 0000000000..e8d26f6103 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorResourceTestCase.java @@ -0,0 +1,164 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Constructor; +import java.util.List; + +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorResourceTestCase extends AbstractProcessorTest { + + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testResource() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class); + visitConstructor(ctor, type); + org.apache.tuscany.sca.implementation.java.JavaResourceImpl resource = type.getResources().get("myResource"); + assertFalse(resource.isOptional()); + } + + @Test + public void testTwoResourcesSameType() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + visitConstructor(ctor, type); + assertNotNull(type.getResources().get("myResource1")); + assertNotNull(type.getResources().get("myResource2")); + } + + @Test + public void testDuplicateResource() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class, String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (DuplicateResourceException e) { + // expected + } + } + + @Test + public void testNoName() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ConstructorResourceTestCase.BadFoo> ctor = + ConstructorResourceTestCase.BadFoo.class.getConstructor(String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidResourceException e) { + // expected + } + } + + @Test + public void testNamesOnConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(Integer.class); + visitConstructor(ctor, type); + assertNotNull(type.getResources().get("myResource")); + } + + @Test + public void testInvalidNumberOfNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ConstructorResourceTestCase.BadFoo> ctor = + ConstructorResourceTestCase.BadFoo.class.getConstructor(Integer.class, Integer.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + @Test + public void testNoMatchingNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ConstructorResourceTestCase.BadFoo> ctor = + ConstructorResourceTestCase.BadFoo.class.getConstructor(List.class, List.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + private static class Foo { + + @org.oasisopen.sca.annotation.Constructor + public Foo(@Resource(name = "myResource") String resource) { + + } + + @org.oasisopen.sca.annotation.Constructor("myResource") + public Foo(@Resource Integer resource) { + + } + + @org.oasisopen.sca.annotation.Constructor + public Foo(@Resource(name = "myResource1") String res1, @Resource(name = "myResource2") String res2) { + + } + + @org.oasisopen.sca.annotation.Constructor + public Foo(@Resource List res) { + + } + } + + private static class BadFoo { + + @org.oasisopen.sca.annotation.Constructor + public BadFoo(@Resource(name = "myResource") String res1, @Resource(name = "myResource") String res2) { + + } + + @org.oasisopen.sca.annotation.Constructor + public BadFoo(@Resource String res) { + + } + + @org.oasisopen.sca.annotation.Constructor("myProp") + public BadFoo(@Resource Integer res, @Resource Integer res2) { + + } + + @org.oasisopen.sca.annotation.Constructor({"myRes", "myRes2"}) + public BadFoo(@Resource List res, @Resource(name = "myOtherRes") List res2) { + + } + + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ContextProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ContextProcessorTestCase.java new file mode 100644 index 0000000000..124c83e9e2 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ContextProcessorTestCase.java @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.ComponentContext; +import org.oasisopen.sca.RequestContext; +import org.oasisopen.sca.annotation.ComponentName; +import org.oasisopen.sca.annotation.Context; + +/** + * @version $Rev$ $Date$ + */ +public class ContextProcessorTestCase { + private ContextProcessor processor; + private ComponentNameProcessor nameProcessor; + private JavaImplementationFactory javaImplementationFactory; + + @Test + public void testComponentContextMethod() throws Exception { + Method method = Foo.class.getMethod("setContext", ComponentContext.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitMethod(method, type); + assertNotNull(type.getResources().get("context")); + } + + @Test + public void testComponentContextField() throws Exception { + Field field = Foo.class.getDeclaredField("context"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitField(field, type); + assertNotNull(type.getResources().get("context")); + } + + @Test + public void testRequestContextMethod() throws Exception { + Method method = Foo.class.getMethod("setRequestContext", RequestContext.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitMethod(method, type); + assertNotNull(type.getResources().get("requestContext")); + } + + @Test + public void testRequestContextField() throws Exception { + Field field = Foo.class.getDeclaredField("requestContext"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitField(field, type); + assertNotNull(type.getResources().get("requestContext")); + } + + @Test + public void testComponentNameMethod() throws Exception { + Method method = Foo.class.getMethod("setName", String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + nameProcessor.visitMethod(method, type); + assertNotNull(type.getResources().get("name")); + } + + @Test + public void testComponentNameField() throws Exception { + Field field = Foo.class.getDeclaredField("name"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + nameProcessor.visitField(field, type); + assertNotNull(type.getResources().get("name")); + } + + @Test + public void testInvalidParamType() throws Exception { + Method method = Foo.class.getMethod("setContext", String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitMethod(method, type); + fail(); + } catch (UnknownContextTypeException e) { + // expected + } + } + + @Test + public void testInvalidParamTypeField() throws Exception { + Field field = Foo.class.getDeclaredField("badContext"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitField(field, type); + fail(); + } catch (UnknownContextTypeException e) { + // expected + } + } + + + @Test + public void testInvalidParamNum() throws Exception { + Method method = Foo.class.getMethod("setContext", ComponentContext.class, String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalContextException e) { + // expected + } + } + + @Test + public void testInvalidNoParams() throws Exception { + Method method = Foo.class.getMethod("setContext"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalContextException e) { + // expected + } + } + + @Test + public void testNoContext() throws Exception { + Method method = Foo.class.getMethod("noContext", ComponentContext.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitMethod(method, type); + assertEquals(0, type.getResources().size()); + } + + @Test + public void testNoContextField() throws Exception { + Field field = Foo.class.getDeclaredField("noContext"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitField(field, type); + assertEquals(0, type.getResources().size()); + } + + @Before + public void setUp() throws Exception { + javaImplementationFactory = new DefaultJavaImplementationFactory(); + processor = new ContextProcessor(new DefaultAssemblyFactory()); + nameProcessor = new ComponentNameProcessor(new DefaultAssemblyFactory()); + } + + private class Foo { + @Context + protected ComponentContext context; + + @ComponentName + protected String name; + + @Context + protected Object badContext; + + protected ComponentContext noContext; + + @Context + protected RequestContext requestContext; + + @Context + public void setContext(ComponentContext context) { + + } + + @ComponentName + public void setName(String name) { + + } + + @Context + public void setContext(String context) { + + } + + @Context + public void setContext(ComponentContext context, String string) { + + } + + @Context + public void setContext() { + + } + + public void noContext(ComponentContext context) { + + } + + @Context + public void setRequestContext(RequestContext requestContext) { + this.requestContext = requestContext; + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DestroyProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DestroyProcessorTestCase.java new file mode 100644 index 0000000000..b524803062 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DestroyProcessorTestCase.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Destroy; + +/** + * @version $Rev$ $Date$ + */ +public class DestroyProcessorTestCase { + + private AssemblyFactory assemblyFactory = new DefaultAssemblyFactory(); + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Foo.class.getMethod("destroy"); + processor.visitMethod(method, type); + assertNotNull(type.getDestroyMethod()); + } + + @Test + public void testBadDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Bar.class.getMethod("badDestroy", String.class); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalDestructorException e) { + // expected + } + } + + @Test + public void testTwoDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Bar.class.getMethod("destroy"); + Method method2 = Bar.class.getMethod("destroy2"); + processor.visitMethod(method, type); + try { + processor.visitMethod(method2, type); + fail(); + } catch (DuplicateDestructorException e) { + // expected + } + } + + @Test + public void testProtectedDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Bar.class.getDeclaredMethod("protectedDestroy"); + try { + processor.visitMethod(method, type); + } catch (IllegalDestructorException e) { + fail(); + } + } + + @Test + public void testPrivateDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Bar.class.getDeclaredMethod("privateDestroy"); + try { + processor.visitMethod(method, type); + } catch (IllegalDestructorException e) { + fail(); + } + } + + @Test + public void testDefaultVisibilityDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Bar.class.getDeclaredMethod("defaultVisibilityDestroy"); + try { + processor.visitMethod(method, type); + } catch (IllegalDestructorException e) { + fail(); + } + } + + private class Foo { + + @Destroy + public void destroy() { + } + } + + + private class Bar { + + @Destroy + public void destroy() { + } + + @Destroy + public void destroy2() { + } + + @Destroy + public void badDestroy(String foo) { + } + + @Destroy + protected void protectedDestroy(){ + } + + @Destroy + private void privateDestroy(){ + } + + @Destroy + void defaultVisibilityDestroy(){ + } + + } +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/EagerInitProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/EagerInitProcessorTestCase.java new file mode 100644 index 0000000000..04d87f60fe --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/EagerInitProcessorTestCase.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.EagerInit; + +/** + * @version $Rev$ $Date$ + */ +public class EagerInitProcessorTestCase { + + private AssemblyFactory assemblyFactory = new DefaultAssemblyFactory(); + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testNoLevel() throws IntrospectionException { + EagerInitProcessor processor = new EagerInitProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(Level.class, type); + } + + @Test + public void testSubclass() throws IntrospectionException { + EagerInitProcessor processor = new EagerInitProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(SubClass.class, type); + } + + @EagerInit + private class Level { + } + + private class SubClass extends Level { + + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicAndPropertyTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicAndPropertyTestCase.java new file mode 100644 index 0000000000..39a2980b2b --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicAndPropertyTestCase.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; + +/** + * @version $Rev$ $Date$ + */ +public class HeuristicAndPropertyTestCase { + + private PropertyProcessor propertyProcessor; + private HeuristicPojoProcessor heuristicProcessor; + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + /** + * Verifies the property and heuristic processors don't collide + */ + @SuppressWarnings("unchecked") + @Test + public void testPropertyProcessorWithHeuristicProcessor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor ctor = Foo.class.getConstructor(String.class); + type.setConstructor(new JavaConstructorImpl(ctor)); + propertyProcessor.visitConstructorParameter(type.getConstructor().getParameters()[0], type); + heuristicProcessor.visitEnd(Foo.class, type); + assertEquals(1, type.getProperties().size()); + assertEquals("foo", type.getProperties().get(0).getName()); + } + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + propertyProcessor = new PropertyProcessor(registry); + heuristicProcessor = new HeuristicPojoProcessor(registry); + } + + public static class Foo { + public Foo(@Property(name = "foo") + String prop) { + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicConstructorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicConstructorTestCase.java new file mode 100644 index 0000000000..394857576d --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicConstructorTestCase.java @@ -0,0 +1,330 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getProperty; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Remotable; + +/** + * @version $Rev$ $Date$ + */ +public class HeuristicConstructorTestCase extends AbstractProcessorTest { + + private AssemblyFactory factory; + private JavaInterfaceFactory javaFactory; + private HeuristicPojoProcessor processor; + private JavaImplementationFactory javaImplementationFactory; + + public HeuristicConstructorTestCase() { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + factory = new DefaultAssemblyFactory(registry); + javaFactory = new DefaultJavaInterfaceFactory(registry); + javaImplementationFactory = new DefaultJavaImplementationFactory(); + processor = new HeuristicPojoProcessor(factory, javaFactory); + } + + private <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + for (Constructor<?> constructor : clazz.getConstructors()) { + visitConstructor(constructor, type); + } + processor.visitEnd(clazz, type); + } + + /** + * Verifies a single constructor is chosen with a parameter as the type + */ + @Test + public void testSingleConstructorWithParam() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + org.apache.tuscany.sca.assembly.Property prop = factory.createProperty(); + prop.setName("foo"); + type.getProperties().add(prop); + // Hack to add a property member + JavaElementImpl element = new JavaElementImpl("foo", String.class, null); + type.getPropertyMembers().put("foo", element); + visitEnd(Foo1.class, type); + assertNotNull(type.getConstructor().getConstructor()); + assertEquals("foo", type.getConstructor().getParameters()[0].getName()); + } + + /** + * Verifies a single constructor is chosen with a reference as the type + */ + @Test + public void testSingleConstructorWithRef() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + org.apache.tuscany.sca.assembly.Reference ref = factory.createReference(); + ref.setName("foo"); + type.getReferences().add(ref); + type.getReferenceMembers().put("foo", new JavaElementImpl("foo", String.class, null)); + visitEnd(Foo1.class, type); + assertNotNull(type.getConstructor().getConstructor()); + assertEquals("foo", type.getConstructor().getParameters()[0].getName()); + } + + /** + * Verifies a single constructor is chosen with a property and a reference + * as the type + */ + @Test + public void testSingleConstructorWithPropRef() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + + org.apache.tuscany.sca.assembly.Property prop = factory.createProperty(); + prop.setName("foo"); + type.getProperties().add(prop); + // Hack to add a property member + JavaElementImpl element = new JavaElementImpl("foo", String.class, null); + type.getPropertyMembers().put("foo", element); + + org.apache.tuscany.sca.assembly.Reference ref = ModelHelper.createReference(factory, javaFactory, "ref", Foo1.class); + type.getReferences().add(ref); + type.getReferenceMembers().put("ref", new JavaElementImpl("ref", Foo1.class, null)); + visitEnd(Foo2.class, type); + assertNotNull(type.getConstructor().getConstructor()); + assertEquals(2, type.getConstructor().getParameters().length); + } + + @Test + public void testSingleConstructorResolvableParam() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo5.class, type); + assertEquals(String.class, type.getPropertyMembers().get("string").getType()); + } + + @Test + public void testSingleConstructorResolvableRef() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo6.class, type); + assertTrue(ModelHelper.matches(ModelHelper.getReference(type, "ref"), Ref.class)); + } + + @Test + public void testSingleConstructorAmbiguousRef() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + org.apache.tuscany.sca.assembly.Reference ref = ModelHelper.createReference(factory, javaFactory, "ref", Foo1.class); + type.getReferences().add(ref); + type.getReferenceMembers().put("ref", new JavaElementImpl("ref", Foo1.class, null)); + org.apache.tuscany.sca.assembly.Reference ref2 = ModelHelper.createReference(factory, javaFactory, "ref2", Foo1.class); + type.getReferences().add(ref2); + type.getReferenceMembers().put("ref2", new JavaElementImpl("ref2", Foo1.class, null)); + try { + visitEnd(Foo4.class, type); + fail(); + } catch (AmbiguousConstructorException e) { + // expected + } + } + + @Test + public void testConstructorPropertyAnnotatedParamsOnly() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo7.class, type); + assertNotNull(getProperty(type, "myProp")); + } + + @Test + public void testConstructorReferenceAnnotatedParamsOnly() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo8.class, type); + assertNotNull(ModelHelper.getReference(type, "myRef")); + } + + @Test + public void testDefaultConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo3.class, type); + assertNotNull(type.getConstructor().getConstructor()); + } + + @Test + public void testSameTypesButAnnotated() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo12.class, type); + assertEquals(2, type.getProperties().size()); + assertNotNull(getProperty(type, "prop1")); + assertNotNull(getProperty(type, "prop2")); + } + + /** + * Verifies processing executes with additional extension annotations + */ + @Test + public void testRandomAnnotation() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo11.class, type); + assertEquals(1, type.getProperties().size()); + assertNotNull(getProperty(type, "prop1")); + } + + @Test + public void testPrivateConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + visitEnd(Foo14.class, type); + fail(); + } catch (NoConstructorException e) { + // expected + } + } + + @Test + public void testMultipleConstructors() throws Exception { + // throw new UnsupportedOperationException("Finish heuristic multiple + // constructors - Foo10"); + } + + public static class Foo1 { + public Foo1(String val) { + } + } + + public static class Foo2 { + public Foo2(String val, Foo1 ref) { + } + } + + public static class Foo3 { + } + + public static class Foo4 { + public Foo4(Foo1 ref) { + } + } + + public static class Prop { + + } + + @Remotable + public static interface Ref { + + } + + public static class Foo5 { + public Foo5(String val) { + } + } + + public static class Foo6 { + public Foo6(Ref ref) { + } + } + + public static class Foo7 { + public Foo7(@Property(name = "myProp") + String prop) { + } + } + + public static class Foo8 { + public Foo8(@Reference(name = "myRef") + String ref) { + } + } + + public static class Foo9 { + public Foo9(@Reference(name = "myRef") + String ref) { + } + } + + public static class Foo10 { + + public Foo10() { + } + + public Foo10(String prop) { + } + + public Foo10(@Property(name = "prop1") + String prop1, @Property(name = "prop2") + String prop2) { + + } + } + + public static class Foo11 { + + public Foo11(@Property(name = "prop1") + String prop, @Baz + String baz) { + } + } + + public static class Foo12 { + + public Foo12(@Property(name = "prop1") + String prop, @Property(name = "prop2") + String baz) { + } + } + + public @interface Baz { + + } + + public static class Foo13 { + public Foo13(@Reference + String foo) { + } + } + + public static final class Foo14 { + private Foo14() { + } + } + + public static final class Foo15 { + public Foo15(@Reference + String param1, @Reference + String param2) { + } + } + + public static final class Foo16 { + public Foo16(@Reference + String param1, @Property(name = "foo") + String param2, @Reference(name = "bar") + String param3) { + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessorTestCase.java new file mode 100644 index 0000000000..a850f86995 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessorTestCase.java @@ -0,0 +1,581 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Constructor; +import java.util.Collection; +import java.util.List; + +import javax.jws.WebService; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Remotable; +import org.oasisopen.sca.annotation.Service; + +/** + * Verifies component type information is properly introspected from an unadorned + * POJO according to the SCA Java Client and Implementation Model Specification + * + * @version $Rev$ $Date$ + */ +public class HeuristicPojoProcessorTestCase extends AbstractProcessorTest { + + private org.apache.tuscany.sca.implementation.java.introspect.impl.HeuristicPojoProcessor processor; + private JavaImplementationFactory javaImplementationFactory; + + public HeuristicPojoProcessorTestCase() { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + processor = new HeuristicPojoProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + private <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + for (Constructor<?> constructor : clazz.getConstructors()) { + visitConstructor(constructor, type); + } + processor.visitEnd(clazz, type); + } + + /** + * Verifies a single service interface is computed when only one interface + * is implemented + */ + @Test + public void testSingleInterface() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<SingleInterfaceImpl> ctor = SingleInterfaceImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<SingleInterfaceImpl>(ctor)); + processor.visitEnd(SingleInterfaceImpl.class, type); + assertEquals(1, type.getServices().size()); + assertTrue(ModelHelper.matches(ModelHelper.getService(type, PropertyInterface.class.getSimpleName()), + PropertyInterface.class)); + assertTrue(type.getProperties().isEmpty()); + assertTrue(type.getReferences().isEmpty()); + } + + /** + * Verifies property and reference setters are computed + */ + @Test + public void testPropertyReference() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<SingleInterfaceWithPropertyReferenceImpl> ctor = SingleInterfaceWithPropertyReferenceImpl.class + .getConstructor(); + type.setConstructor(new JavaConstructorImpl<SingleInterfaceWithPropertyReferenceImpl>(ctor)); + processor.visitEnd(SingleInterfaceWithPropertyReferenceImpl.class, type); + assertEquals(1, type.getServices().size()); + assertTrue(ModelHelper + .matches(ModelHelper.getService(type, Interface1.class.getSimpleName()), Interface1.class)); + assertEquals(1, type.getProperties().size()); + org.apache.tuscany.sca.assembly.Property prop = ModelHelper.getProperty(type, "property"); + assertNotNull(prop); + assertEquals(ComplexProperty.class, type.getPropertyMembers().get("property").getType()); + assertEquals(1, type.getReferences().size()); + assertTrue(ModelHelper.matches(ModelHelper.getReference(type, "reference"), Ref.class)); + } + + /** + * Verifies that a property setter is not introspected if an analogous + * operation is in the service interface + */ + @Test + public void testPropertySetterInInterface() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<SingleInterfaceImpl> ctor = SingleInterfaceImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<SingleInterfaceImpl>(ctor)); + processor.visitEnd(SingleInterfaceImpl.class, type); + assertEquals(0, type.getProperties().size()); + } + + /** + * Verifies that a reference setter is not introspected if an analogous + * operation is in the service interface + */ + @Test + public void testReferenceSetterInInterface() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<RefInterfaceImpl> ctor = RefInterfaceImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<RefInterfaceImpl>(ctor)); + processor.visitEnd(RefInterfaceImpl.class, type); + assertEquals(0, type.getReferences().size()); + } + + /** + * Verifies collection generic types or array types are introspected as + * references according to specification rules + */ + @Test + public void testReferenceCollectionType() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ReferenceCollectionImpl> ctor = ReferenceCollectionImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<ReferenceCollectionImpl>(ctor)); + processor.visitEnd(ReferenceCollectionImpl.class, type); + assertEquals(1, type.getProperties().size()); + assertEquals(3, type.getReferences().size()); + } + + /** + * Verifies collection generic types or array types are introspected as + * properties according to specification rules + */ + @Test + public void testPropertyCollectionType() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<PropertyCollectionImpl> ctor = PropertyCollectionImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<PropertyCollectionImpl>(ctor)); + processor.visitEnd(PropertyCollectionImpl.class, type); + assertEquals(0, type.getReferences().size()); + assertEquals(4, type.getProperties().size()); + } + + /** + * Verifies references are calculated when the type marked with is + * + * @Remotable + */ + @Test + public void testRemotableRef() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<RemotableRefImpl> ctor = RemotableRefImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<RemotableRefImpl>(ctor)); + processor.visitEnd(RemotableRefImpl.class, type); + assertEquals(2, type.getReferences().size()); + assertEquals(0, type.getProperties().size()); + } + + @Test + public void testParentInterface() throws IntrospectionException, NoSuchMethodException { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Child> ctor = Child.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<Child>(ctor)); + processor.visitEnd(Child.class, type); + assertNotNull(ModelHelper.getService(type, Interface1.class.getSimpleName())); + } + + /** + * Verifies a service interface is calculated when only props and refs are + * given + */ + @Test + public void testExcludedPropertyAndReference() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + org.apache.tuscany.sca.assembly.Reference ref = factory.createReference(); + ref.setName("reference"); + type.getReferences().add(ref); + type.getReferenceMembers().put("reference", new JavaElementImpl("reference", Ref.class, null)); + org.apache.tuscany.sca.assembly.Reference ref2 = factory.createReference(); + ref2.setName("reference2"); + type.getReferences().add(ref2); + type.getReferenceMembers().put("reference2", new JavaElementImpl("reference2", Ref.class, null)); + org.apache.tuscany.sca.assembly.Property prop1 = factory.createProperty(); + prop1.setName("string1"); + type.getProperties().add(prop1); + type.getPropertyMembers().put("string1", new JavaElementImpl("string1", String.class, null)); + org.apache.tuscany.sca.assembly.Property prop2 = factory.createProperty(); + prop2.setName("string2"); + type.getProperties().add(prop2); + type.getPropertyMembers().put("string2", new JavaElementImpl("string2", String.class, null)); + visitEnd(MockService.class, type); + assertEquals(1, type.getServices().size()); + } + + @Test + public void testProtectedRemotableRefField() throws IntrospectionException, NoSuchMethodException { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ProtectedRemotableRefFieldImpl> ctor = ProtectedRemotableRefFieldImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<ProtectedRemotableRefFieldImpl>(ctor)); + processor.visitEnd(ProtectedRemotableRefFieldImpl.class, type); + assertNotNull(ModelHelper.getReference(type, "otherRef")); + } + + @Test + public void testProtectedRemotableRefMethod() throws IntrospectionException, NoSuchMethodException { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ProtectedRemotableRefMethodImpl> ctor = ProtectedRemotableRefMethodImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<ProtectedRemotableRefMethodImpl>(ctor)); + processor.visitEnd(ProtectedRemotableRefMethodImpl.class, type); + assertNotNull(ModelHelper.getReference(type, "otherRef")); + } + + @Test + public void testSetDataTypes() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<PropertyIntTypeOnConstructor> ctor = PropertyIntTypeOnConstructor.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<PropertyIntTypeOnConstructor>(ctor)); + processor.visitEnd(PropertyIntTypeOnConstructor.class, type); + org.apache.tuscany.sca.assembly.Property foo = ModelHelper.getProperty(type, "foo"); + assertEquals(int.class, type.getPropertyMembers().get("foo").getType()); + // assertEquals(new QName(JavaXMLMapper.URI_2001_SCHEMA_XSD, "int"), foo.getXSDType()); + } + + /** + * Errata for Java Component Implementation Specification v1.0 corrects the algorithm for determining + * references of an unannotated POJO (section 1.2.7). This test makes sure that the earlier implementation + * is corrected as per the errata. A notable difference is that the interfaces annotated with @Service + * no longer result in references. + */ + @Test + public void testUpdatedRule() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(SomeServiceImpl.class, type); + assertEquals(12, type.getReferenceMembers().size()); + assertTrue(type.getReferenceMembers().containsKey("rri1")); + assertTrue(type.getReferenceMembers().containsKey("rri2")); + assertTrue(type.getReferenceMembers().containsKey("rri3")); + assertTrue(type.getReferenceMembers().containsKey("rri4")); + + assertTrue(type.getReferenceMembers().containsKey("rria1")); + assertTrue(type.getReferenceMembers().containsKey("rria2")); + assertTrue(type.getReferenceMembers().containsKey("rria3")); + assertTrue(type.getReferenceMembers().containsKey("rria4")); + + assertTrue(type.getReferenceMembers().containsKey("rric1")); + assertTrue(type.getReferenceMembers().containsKey("rric2")); + assertTrue(type.getReferenceMembers().containsKey("rric3")); + assertTrue(type.getReferenceMembers().containsKey("rric4")); + + assertEquals(16, type.getPropertyMembers().size()); + assertTrue(type.getPropertyMembers().containsKey("pnri1")); + assertTrue(type.getPropertyMembers().containsKey("pnri2")); + assertTrue(type.getPropertyMembers().containsKey("pnri3")); + assertTrue(type.getPropertyMembers().containsKey("pnri4")); + + assertTrue(type.getPropertyMembers().containsKey("pnria1")); + assertTrue(type.getPropertyMembers().containsKey("pnria2")); + assertTrue(type.getPropertyMembers().containsKey("pnria3")); + assertTrue(type.getPropertyMembers().containsKey("pnria4")); + + assertTrue(type.getPropertyMembers().containsKey("pnric1")); + assertTrue(type.getPropertyMembers().containsKey("pnric2")); + assertTrue(type.getPropertyMembers().containsKey("pnric3")); + assertTrue(type.getPropertyMembers().containsKey("pnric4")); + + assertTrue(type.getPropertyMembers().containsKey("gen1")); + assertTrue(type.getPropertyMembers().containsKey("gen2")); + assertTrue(type.getPropertyMembers().containsKey("gen3")); + assertTrue(type.getPropertyMembers().containsKey("gen4")); + } + + /** + * Interfaces with "@WebService" annotation implemented by the class should result + * in a Service in the same manner as an "@Remotable" annotation would. + */ + @Test + public void testInterfaceWithWebServiceAnnotation() throws Exception{ + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(SomeWebServiceImpl.class, type); + assertEquals(1, type.getServices().size()); + assertEquals("SomeWebService", type.getServices().get(0).getName()); + } + + @Remotable + private interface ReferenceRemotableInterface { + void operation1(String param1); + } + + private interface PropertyNonRemotableInterface { + void operation1(String param1); + } + + @Remotable + private interface SomeService { + void serviceOperation1(); + } + + private static class SomeServiceImpl implements SomeService { + + public SomeServiceImpl() { + } + + // References - interface with @Remotable + public void setRri1(ReferenceRemotableInterface rri) { + } + protected void setRri2(ReferenceRemotableInterface rri) { + } + public ReferenceRemotableInterface rri3; + protected ReferenceRemotableInterface rri4; + + // References - array of interface with @Remotable + public void setRria1(ReferenceRemotableInterface[] rri) { + } + protected void setRria2(ReferenceRemotableInterface[] rri) { + } + public ReferenceRemotableInterface[] rria3; + protected ReferenceRemotableInterface[] rria4; + + // References - parametrized Collection of interface with @Remotable + public void setRric1(Collection<ReferenceRemotableInterface> rri) { + } + protected void setRric2(Collection<ReferenceRemotableInterface> rri) { + } + public Collection<ReferenceRemotableInterface> rric3; + protected Collection<ReferenceRemotableInterface> rric4; + + // Properties - interface with @Service and without @Remotable + public void setPnri1(PropertyNonRemotableInterface arg) { + } + protected void setPnri2(PropertyNonRemotableInterface arg) { + } + public PropertyNonRemotableInterface pnri3; + protected PropertyNonRemotableInterface pnri4; + + // Properties - array of interface with @Service and without @Remotable + public void setPnria1(PropertyNonRemotableInterface[] arg) { + } + protected void setPnria2(PropertyNonRemotableInterface[] arg) { + } + public PropertyNonRemotableInterface[] pnria3; + protected PropertyNonRemotableInterface[] pnria4; + + // Properties - parametrized Collection of interface with @Service and without @Remotable + public void setPnric1(Collection<PropertyNonRemotableInterface> arg) { + } + protected void setPnric2(Collection<PropertyNonRemotableInterface> arg) { + } + public Collection<PropertyNonRemotableInterface> pnric3; + protected Collection<PropertyNonRemotableInterface> pnric4; + + // Properties - Non-parametrized Collection + public void setGen1(Collection arg) { + } + protected void setGen2(Collection arg) { + } + public Collection gen3; + protected Collection gen4; + + public void serviceOperation1() { + } + } + + private static class PropertyIntTypeOnConstructor { + protected int foo; + + public PropertyIntTypeOnConstructor() { + } + + public int getFoo() { + return foo; + } + } + + @Remotable + private interface PropertyInterface { + void setString1(String val); + } + + @Remotable + private interface Interface1 { + } + + private static class Parent implements Interface1 { + + } + + private static class Child extends Parent { + public Child() { + } + + } + + private static class SingleInterfaceImpl implements PropertyInterface { + public SingleInterfaceImpl() { + } + + public void setString1(String val) { + } + + } + + private interface HeuristicServiceInterface { + void fooOperation(String ref); + + void setInvalid1(); // No parameter + + void setInvalid2(String str, int i); // More than one parameter + + String setInvalid3(String str); // return should be void + } + + public static class MockService implements PropertyInterface, RefInterface, HeuristicServiceInterface { + + @Property + public void setString1(String val) { + } + + @Property + public void setString2(String val) { + } + + @Reference + public void setReference(Ref ref) { + } + + @Reference + public void setReference2(Ref ref) { + } + + public void fooOperation(String ref) { + + } + + public void setInvalid1() { + } + + public void setInvalid2(String str, int i) { + } + + public String setInvalid3(String str) { + return null; + } + + } + + @Remotable + private interface Ref { + } + + private class ComplexProperty { + } + + private interface RefInterface { + void setReference(Ref ref); + } + + private static class RefInterfaceImpl implements RefInterface { + public RefInterfaceImpl() { + } + + public void setReference(Ref ref) { + } + } + + private static class SingleInterfaceWithPropertyReferenceImpl implements Interface1 { + public SingleInterfaceWithPropertyReferenceImpl() { + } + + public void setReference(Ref ref) { + } + + public void setProperty(ComplexProperty prop) { + } + } + + private static class ReferenceCollectionImpl implements Interface1 { + public ReferenceCollectionImpl() { + } + + public void setCollectionReference(Collection<Ref> ref) { + } + + public void setNonGenericCollectionReference(Collection ref) { + // [rfeng] By the SCA specification, this should be classified as property + } + + public void setListReference(List<Ref> ref) { + } + + public void setArrayReference(Ref[] ref) { + } + } + + private static class PropertyCollectionImpl implements Interface1 { + public PropertyCollectionImpl() { + } + + public void setCollectionProperty(Collection<ComplexProperty> prop) { + } + + public void setCollectionProperty2(Collection<String> prop) { + } + + public void setArrayProperty(ComplexProperty[] prop) { + } + + public void setArrayProperty2(String[] prop) { + } + } + + @Remotable + private interface RemotableRef { + } + + private static class RemotableRefImpl implements Interface1 { + protected RemotableRef otherRef; + + public RemotableRefImpl() { + } + + public void setRef(RemotableRef ref) { + + } + } + + private static class ProtectedRemotableRefFieldImpl implements Interface1 { + protected RemotableRef otherRef; + + public ProtectedRemotableRefFieldImpl() { + } + + public ProtectedRemotableRefFieldImpl(RemotableRef otherRef) { + this.otherRef = otherRef; + } + + } + + private static class ProtectedRemotableRefMethodImpl implements Interface1 { + public ProtectedRemotableRefMethodImpl() { + } + + protected void setOtherRef(RemotableRef otherRef) { + } + + } + + @WebService + private interface SomeWebService { + void serviceOperation1(); + } + + @Service(SomeWebService.class) + private static class SomeWebServiceImpl implements SomeWebService { + public SomeWebServiceImpl() { + + } + + public void serviceOperation1() { + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeutisticExtensibleConstructorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeutisticExtensibleConstructorTestCase.java new file mode 100644 index 0000000000..07ab6f28c8 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeutisticExtensibleConstructorTestCase.java @@ -0,0 +1,157 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.junit.Test; + +/** + * Verifies constructors that have extensible annotation types, i.e. that have + * parameters marked by annotations which are themselves processed by some other + * implementation processor + * + * @version $Rev$ $Date$ + */ +public class HeutisticExtensibleConstructorTestCase extends AbstractProcessorTest { + + private org.apache.tuscany.sca.implementation.java.introspect.impl.HeuristicPojoProcessor processor; + private JavaImplementationFactory javaImplementationFactory; + + public HeutisticExtensibleConstructorTestCase() { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + processor = new HeuristicPojoProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + private <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + for (Constructor<?> constructor : clazz.getConstructors()) { + visitConstructor(constructor, type); + } + processor.visitEnd(clazz, type); + } + + /** + * Verifies heuristic processing can be called prior to an extension + * annotation processors being called. + */ + @Test + public void testBarAnnotationProcessedFirst() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + JavaConstructorImpl<Foo> definition = new JavaConstructorImpl<Foo>(ctor); + type.setConstructor(definition); + Property property = factory.createProperty(); + property.setName("myBar"); + definition.getParameters()[0].setName("myBar"); + type.getProperties().add(property); + visitEnd(Foo.class, type); + assertEquals(2, type.getProperties().size()); + } + + /** + * Verifies heuristic processing can be called before an extension + * annotation processors is called. <p/> For example, given: + * + * <pre> + * Foo(@Bar String prop, @org.oasisopen.sca.annotation.Property(name = "foo") String prop2) + * </pre> + * + * <p/> Heuristic evaluation of + * @Property can occur prior to another implementation processor evaluating + * @Bar + * @throws Exception + */ + @Test + public void testBarAnnotationProcessedLast() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo.class, type); + + // now simulate process the bar impl + JavaConstructorImpl<?> definition = type.getConstructor(); + definition.getParameters()[0].setName("myBar"); + Property property = factory.createProperty(); + property.setName("myBar"); + type.getProperties().add(property); + + assertEquals(2, type.getProperties().size()); + assertEquals("foo", definition.getParameters()[1].getName()); + } + + /** + * Verifies heuristic processing can be called before an extension + * annotation processors is called with the extension parameter in a middle + * position. Specifically, verifies that the heuristic processor updates + * injection names and preserves their ordering. + */ + @Test + public void testBarAnnotationProcessedFirstInMiddle() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo2> ctor = Foo2.class.getConstructor(String.class, String.class, String.class); + JavaConstructorImpl<Foo2> definition = new JavaConstructorImpl<Foo2>(ctor); + type.setConstructor(definition); + // insert placeholder for first param, which would be done by a + // processor + definition.getParameters()[0].setName(""); + Property property = factory.createProperty(); + // Hack to add a property member + JavaElementImpl element = new JavaElementImpl("myBar", String.class, null); + type.getPropertyMembers().put("myBar", element); + property.setName("myBar"); + definition.getParameters()[1].setName("myBar"); + type.getProperties().add(property); + visitEnd(Foo2.class, type); + assertEquals("baz", definition.getParameters()[0].getName()); + assertEquals(2, type.getProperties().size()); + assertEquals(1, type.getReferences().size()); + } + + public @interface Bar { + + } + + public static class Foo { + public Foo(@Bar + String prop, @org.oasisopen.sca.annotation.Property(name = "foo") + String prop2) { + } + } + + public static class Foo2 { + public Foo2(@org.oasisopen.sca.annotation.Reference(name = "baz") + String prop1, @Bar + String prop2, @org.oasisopen.sca.annotation.Property(name = "foo") + String prop3) { + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InitProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InitProcessorTestCase.java new file mode 100644 index 0000000000..5df7078aa6 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InitProcessorTestCase.java @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Init; + +/** + * @version $Rev$ $Date$ + */ +public class InitProcessorTestCase { + + private JavaImplementationFactory javaImplementationFactory; + + public InitProcessorTestCase() { + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + @Test + public void testInit() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Foo.class.getMethod("init"); + processor.visitMethod(method, type); + assertNotNull(type.getInitMethod()); + } + + @Test + public void testBadInit() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Bar.class.getMethod("badInit", String.class); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalInitException e) { + // expected + } + } + + @Test + public void testTwoInit() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Bar.class.getMethod("init"); + Method method2 = InitProcessorTestCase.Bar.class.getMethod("init2"); + processor.visitMethod(method, type); + try { + processor.visitMethod(method2, type); + fail(); + } catch (DuplicateInitException e) { + // expected + } + } + + @Test + public void testProtectedInit() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Bar.class.getDeclaredMethod("protectedInit"); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalInitException e) { + // expected + } + } + + @Test + public void testPrivateInit() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Bar.class.getDeclaredMethod("privateInit"); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalInitException e) { + // expected + } + } + + @Test + public void testBadInit2() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Bar.class.getDeclaredMethod("badInit2"); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalInitException e) { + // expected + } + } + + private class Foo { + @Init + public void init() { + } + } + + + private class Bar { + @Init + public void init() { + } + + @Init + public void init2() { + } + + @Init + public void badInit(String foo) { + } + + @Init + public String badInit2() { + return null; + } + + @Init + protected void protectedInit() { + } + + @Init + private void privateInit() { + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ModelHelper.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ModelHelper.java new file mode 100644 index 0000000000..aad6b96cf9 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ModelHelper.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; + +/** + * @version $Rev$ $Date$ + */ +public class ModelHelper { + + public static Property getProperty(JavaImplementation type, String name) { + for (Property prop : type.getProperties()) { + if (prop.getName().equals(name)) { + return prop; + } + } + return null; + } + + public static Reference getReference(JavaImplementation type, String name) { + for (Reference ref : type.getReferences()) { + if (ref.getName().equals(name)) { + return ref; + } + } + return null; + } + + public static Service getService(JavaImplementation type, String name) { + for (Service svc : type.getServices()) { + if (svc.getName().equals(name)) { + return svc; + } + } + return null; + } + + public static boolean matches(Contract contract, Class<?> type) { + Interface interface1 = contract.getInterfaceContract().getInterface(); + if (interface1 instanceof JavaInterface) { + return type == ((JavaInterface)interface1).getJavaClass(); + } else { + return false; + } + } + + public static ComponentService createService(AssemblyFactory factory, + JavaInterfaceFactory javaFactory, Class<?> type) { + org.apache.tuscany.sca.assembly.ComponentService ref = factory.createComponentService(); + ref.setName(type.getSimpleName()); + JavaInterface i = javaFactory.createJavaInterface(); + i.setJavaClass(type); + JavaInterfaceContract ic = javaFactory.createJavaInterfaceContract(); + ic.setInterface(i); + ref.setInterfaceContract(ic); + return ref; + } + + public static Reference createReference(AssemblyFactory factory, + JavaInterfaceFactory javaFactory, String name, Class<?> type) { + org.apache.tuscany.sca.assembly.Reference ref = factory.createReference(); + ref.setName(name); + JavaInterface i = javaFactory.createJavaInterface(); + i.setJavaClass(type); + JavaInterfaceContract ic = javaFactory.createJavaInterfaceContract(); + ic.setInterface(i); + ref.setInterfaceContract(ic); + return ref; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessorTestCase.java new file mode 100644 index 0000000000..e3d747fc28 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessorTestCase.java @@ -0,0 +1,428 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.impl.PolicyJavaInterfaceVisitor; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.Requires; +import org.oasisopen.sca.annotation.Service; + +/** + * @version $Rev$ $Date$ + */ +public class PolicyProcessorTestCase { + private ServiceProcessor serviceProcessor; + private PolicyProcessor policyProcessor; + private PolicyJavaInterfaceVisitor visitor; + private JavaImplementation type; + + // This actually is a test for PolicyJavaInterfaceProcessor. It will get + // invoked via the call to ImplementationProcessorServiceImpl.createService in + // ServiceProcessor. Of course ServiceProcessor class has to be working. + @Test + public void testSingleInterfaceWithIntentsOnInterfaceAtInterfaceLevel() throws Exception { + serviceProcessor.visitClass(Service1.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service1.class, type); + verifyIntents(Service1.class, type); + } + + @Test + public void testMultipleInterfacesWithIntentsOnInterfaceAtInterfaceLevel() throws Exception { + serviceProcessor.visitClass(Service2.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + visitor.visitInterface((JavaInterface)type.getServices().get(1).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service2.class, type); + verifyIntents(Service2.class, type); + } + + @Test + public void testSingleInterfaceWithIntentsOnImplAtClassLevel() throws Exception { + serviceProcessor.visitClass(Service3.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service3.class, type); + verifyIntents(Service3.class, type); + } + + @Test + public void testMultipleInterfacesWithIntentsOnImplAtClassLevel() throws Exception { + serviceProcessor.visitClass(Service4.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service4.class, type); + verifyIntents(Service4.class, type); + } + + public void stestSingleInterfaceWithIntentsOnInterfaceAtMethodLevel() throws Exception { + serviceProcessor.visitClass(Service5.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service5.class, type); + verifyIntents(Service5.class, type); + } + + @Test + public void testSingleInterfaceWithIntentsOnServiceAndInterfaceAtImplAndInertfaceAndMethodLevel() throws Exception { + serviceProcessor.visitClass(Service6.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service6.class, type); + for (Method method : Service6.class.getDeclaredMethods()) { + policyProcessor.visitMethod(method, type); + } + verifyIntents(Service6.class, type); + } + + private void verifyIntents(Class<?> serviceImplClass, JavaImplementation type) { + if ( !(type instanceof PolicySubject) ) { + fail("No Intents on the service "); + } + Requires serviceImplIntentAnnotation = (Requires)serviceImplClass.getAnnotation(Requires.class); + if (serviceImplIntentAnnotation != null) { + String[] serviceImplIntents = serviceImplIntentAnnotation.value(); + List<Intent> requiredIntents = ((PolicySubject)type).getRequiredIntents(); + if (serviceImplIntents.length > 0) { + if (requiredIntents == null || requiredIntents.size() == 0) { + fail("No Intents on the service "); + } + Map<String, Intent> intentMap = new HashMap<String, Intent>(); + for (Intent intent : requiredIntents) { + intentMap.put(intent.getName().getLocalPart(), intent); + } + for (String intent : serviceImplIntents) { + assertTrue("ComponentType for Service class " + serviceImplClass.getName() + + " did not contain Service Implementation intent " + + intent, intentMap.containsKey(intent)); + } + } + } + + // This should match what was specified on @Service for a Service Implementation + // If we use these to get the Service names and we get a null Service + // name then it would seem that wrong values were put on the @Service annotation + // or the wrong interfaces were specified on the implements list of the class + // statement? + Map<String, org.apache.tuscany.sca.assembly.Service> serviceMap = new HashMap<String, org.apache.tuscany.sca.assembly.Service>(); + for (org.apache.tuscany.sca.assembly.Service service: type.getServices()) { + serviceMap.put(service.getName(), service); + } + for (Class<?> interfaceClass : serviceImplClass.getInterfaces()) { + Requires interfaceIntentAnnotation = (Requires)interfaceClass.getAnnotation(Requires.class); + org.apache.tuscany.sca.assembly.Service service = serviceMap.get(interfaceClass.getSimpleName()); + if (service == null) { + fail("No service defined for interface " + interfaceClass.getSimpleName() + + " on Service Implementation " + + serviceImplClass.getName()); + } + + if (interfaceIntentAnnotation != null) { + String[] interfaceIntents = interfaceIntentAnnotation.value(); + List<Intent> requiredIntents = service.getInterfaceContract().getInterface().getRequiredIntents(); + if (interfaceIntents.length > 0) { + if (requiredIntents == null || requiredIntents.size() == 0) { + fail("No Intents on the service " + service.getName()); + } + Map<String, Intent> intentMap = new HashMap<String, Intent>(); + for (Intent intent : requiredIntents) { + intentMap.put(intent.getName().getLocalPart(), intent); + } + for (String intent : interfaceIntents) { + assertTrue("Interface " + service.getName() + + " did not contain Service Interface intent " + + intent, intentMap.containsKey(intent)); + } + } + } + + /* + for (Method method : interfaceClass.getDeclaredMethods()) { + Requires methodIntentAnnotation = method.getAnnotation(Requires.class); + + // Verify that each of the Intents on each of the Service + // Interface Methods exist on their associated operation. + if (methodIntentAnnotation != null) { + String[] methodIntents = methodIntentAnnotation.value(); + if (methodIntents.length > 0) { + List<Intent> requiredIntents = null; + for ( ConfiguredOperation confOp : service.getConfiguredOperations() ) { + if ( confOp.getName().equals(method.getName()) && + confOp.getContractName().equals(service.getName()) ) { + requiredIntents = confOp.getRequiredIntents(); + } + } + + if (requiredIntents == null || requiredIntents.size() == 0) { + fail("No Intents on operation " + method.getName()); + } + for (String intent : methodIntents) { + boolean found = false; + for (Intent requiredIntent: requiredIntents) { + if (requiredIntent.getName().getLocalPart().equals(intent)) { + found = true; + break; + } + } + assertTrue("Operation " + method.getName() + + " did not contain Service Interface method intent " + + intent, found); + } + } + } + } + + for (Method method : serviceImplClass.getDeclaredMethods()) { + Requires methodIntentAnnotation = method.getAnnotation(Requires.class); + + // Verify that each of the Intents on each of the Service + // Implementation Methods exist on their associated + // operation. + if (methodIntentAnnotation != null) { + String[] methodIntents = methodIntentAnnotation.value(); + if (methodIntents.length > 0) { + List<Intent> requiredIntents = null; + for ( ConfiguredOperation confOp : ((OperationsConfigurator)type).getConfiguredOperations() ) { + if ( confOp.getName().equals(method.getName()) ) { + requiredIntents = confOp.getRequiredIntents(); + } + } + + if (requiredIntents == null || requiredIntents.size() == 0) { + fail("No Intents on operation " + method.getName()); + } + + for (String intent : methodIntents) { + boolean found = false; + for (Intent requiredIntent: requiredIntents) { + if (requiredIntent.getName().getLocalPart().equals(intent)) { + found = true; + break; + } + } + assertTrue("Operation " + method.getName() + + " did not contain Implementation method intent " + + intent, found); + } + } + } + } + */ + } + } + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + serviceProcessor = new ServiceProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + policyProcessor = new PolicyProcessor(registry); + visitor = new PolicyJavaInterfaceVisitor(registry); + JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + } + + // @Remotable + @Requires( {"transaction.global"}) + private interface Interface1 { + int method1(); + + int method2(); + + int method3(); + + int method4(); + } + + @Service(Interface1.class) + private class Service1 implements Interface1 { + public int method1() { + return 0; + } + + public int method2() { + return 0; + } + + public int method3() { + return 0; + } + + public int method4() { + return 0; + } + } + + // @Remotable + @Requires( {"transaction.local"}) + private interface Interface2 { + int method5(); + + int method6(); + } + + @Service({Interface1.class, Interface2.class}) + private class Service2 implements Interface1, Interface2 { + public int method1() { + return 0; + } + + public int method2() { + return 0; + } + + public int method3() { + return 0; + } + + public int method4() { + return 0; + } + + public int method5() { + return 0; + } + + public int method6() { + return 0; + } + } + + // @Remotable + private interface Interface3 { + int method1(); + + int method2(); + + int method3(); + + int method4(); + } + + @Service(Interface3.class) + @Requires( {"transaction.global"}) + private class Service3 implements Interface3 { + public int method1() { + return 0; + } + + public int method2() { + return 0; + } + + public int method3() { + return 0; + } + + public int method4() { + return 0; + } + } + + // @Remotable + private interface Interface4 { + int method5(); + + int method6(); + } + + @Service({Interface3.class, Interface4.class}) + @Requires( {"transaction.local"}) + private class Service4 implements Interface3, Interface4 { + public int method1() { + return 0; + } + + public int method2() { + return 0; + } + + public int method3() { + return 0; + } + + public int method4() { + return 0; + } + + public int method5() { + return 0; + } + + public int method6() { + return 0; + } + } + + private interface Interface5 { + @Requires( {"transaction.global"}) + int method1(); + + @Requires( {"transaction.local"}) + int method2(); + } + + @Service(Interface5.class) + private class Service5 implements Interface5 { + public int method1() { + return 0; + } + + public int method2() { + return 0; + } + } + + @Requires( {"transaction.global.Interface6"}) + private interface Interface6 { + @Requires( {"transaction.global.Interface6.method1"}) + int method1(); + + @Requires( {"transaction.local.Interface6.method2"}) + int method2(); + } + + @Service(Interface6.class) + @Requires( {"transaction.global.Service6"}) + private class Service6 implements Interface6 { + // @Requires( {"transaction.global.Service6.method1"}) + public int method1() { + return 0; + } + + // @Requires( {"transaction.global.Service6.method1"}) + public int method2() { + return 0; + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java new file mode 100644 index 0000000000..559659cdeb --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java @@ -0,0 +1,304 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getProperty; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; + +/** + * @version $Rev$ $Date$ + */ +public class PropertyProcessorTestCase { + + JavaImplementation type; + PropertyProcessor processor; + + @Before + public void setUp() throws Exception { + JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + processor = new PropertyProcessor(new DefaultExtensionPointRegistry()); + } + + @Test + public void testMethodAnnotation() throws Exception { + processor.visitMethod(Foo.class.getMethod("setFoo", String.class), type); + assertNotNull(getProperty(type, "foo")); + } + + @Test + public void testMethodRequired() throws Exception { + processor.visitMethod(Foo.class.getMethod("setFooRequired", String.class), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "fooRequired"); + assertNotNull(prop); + assertTrue(prop.isMustSupply()); + } + + @Test + public void testMethodName() throws Exception { + processor.visitMethod(Foo.class.getMethod("setBarMethod", String.class), type); + assertNotNull(getProperty(type, "bar")); + } + + @Test + public void testFieldAnnotation() throws Exception { + processor.visitField(Foo.class.getDeclaredField("baz"), type); + assertNotNull(getProperty(type, "baz")); + } + + @Test + public void testFieldRequired() throws Exception { + processor.visitField(Foo.class.getDeclaredField("bazRequired"), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "bazRequired"); + assertNotNull(prop); + assertTrue(prop.isMustSupply()); + } + + @Test + public void testFieldName() throws Exception { + processor.visitField(Foo.class.getDeclaredField("bazField"), type); + assertNotNull(getProperty(type, "theBaz")); + } + + @Test + public void testDuplicateFields() throws Exception { + processor.visitField(Bar.class.getDeclaredField("dup"), type); + try { + processor.visitField(Bar.class.getDeclaredField("baz"), type); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + @Test + public void testDuplicateMethods() throws Exception { + processor.visitMethod(Bar.class.getMethod("setDupMethod", String.class), type); + try { + processor.visitMethod(Bar.class.getMethod("setDupSomeMethod", String.class), type); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + @Test + public void testInvalidProperty() throws Exception { + try { + processor.visitMethod(Bar.class.getMethod("badMethod"), type); + fail(); + } catch (IllegalPropertyException e) { + // expected + } + } + + @Test + public void testMultiplicityCollection() throws Exception { + processor.visitField(Multiple.class.getDeclaredField("refs1"), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "refs1"); + assertNotNull(prop); + assertSame(String.class, getBaseType(type.getPropertyMembers().get(prop.getName()))); + assertTrue(prop.isMany()); + } + + @Test + public void testMultiplicityArray() throws Exception { + processor.visitField(Multiple.class.getDeclaredField("refs2"), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "refs2"); + assertNotNull(prop); + assertSame(String.class, getBaseType(type.getPropertyMembers().get(prop.getName()))); + assertTrue(prop.isMany()); + } + + @Test + public void testMultiplicityArrayMethod() throws Exception { + processor.visitMethod(Multiple.class.getMethod("setRefs3", String[].class), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "refs3"); + assertNotNull(prop); + assertSame(String.class, getBaseType(type.getPropertyMembers().get(prop.getName()))); + assertTrue(prop.isMany()); + } + + @Test + public void testMultiplicityCollectionMethod() throws Exception { + processor.visitMethod(Multiple.class.getMethod("setRefs4", Collection.class), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "refs4"); + assertNotNull(prop); + assertSame(String.class, getBaseType(type.getPropertyMembers().get(prop.getName()))); + assertTrue(prop.isMany()); + } + + @Test + public void testRejectStaticFieldProperty() throws Exception { + try { + processor.visitField(BadStaticProps.class.getDeclaredField("stint"), type); + fail("Processor should not accept a static field with Property annotation"); + } + catch (IllegalPropertyException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception detected"); + } + } + + @Test + public void testRejectStaticMethodProperty() throws Exception { + try { + processor.visitMethod(BadStaticProps.class.getDeclaredMethod("setStint",int.class), type); + fail("Processor should not accept a static method with Property annotation"); + } + catch (IllegalPropertyException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception detected"); + e.printStackTrace(); + } + + } + + @Test + public void testClassWithBadMethodArgProperty() throws Exception { + Method meth = BadMethodProps.class.getMethod("BadMethod", String.class); + + try { + processor.visitMethod(meth, type); + + fail("Method with @Property annotated args should be rejected"); + } catch (IllegalPropertyException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception received"); + e.printStackTrace(); + } + + } + + /** + * Private classes utilized in the tests + */ + + private class Foo { + + @Property + protected String baz; + + @Property(required = true) + protected String bazRequired; + + @Property(name = "theBaz") + protected String bazField; + + @Property + public void setFoo(String string) { + } + + @Property(required = true) + public void setFooRequired(String string) { + } + + @Property(name = "bar") + public void setBarMethod(String string) { + } + + } + + private class Bar { + + @Property + protected String dup; + + @Property(name = "dup") + protected String baz; + + @Property + public void setDupMethod(String s) { + } + + @Property(name = "dupMethod") + public void setDupSomeMethod(String s) { + } + + @Property + public void badMethod() { + } + } + + private class Multiple { + @Property + protected List<String> refs1; + + @Property + protected String[] refs2; + + @Property + public void setRefs3(String[] refs) { + } + + @Property + public void setRefs4(Collection<String> refs) { + } + } + + private static class BadMethodProps { + + @org.oasisopen.sca.annotation.Constructor() + public BadMethodProps(@Property(name = "myProp", required = true)String prop) { + + } + + /** Java can't tell that the @reference argument is disallowed by SCA, but the run time must reject it*/ + public void BadMethod(@Property(name = "badMethodArgProp")String methArg) { + + } + } + + private static class BadStaticProps { + + @Property(name="badstaticfield")static int stint; + + @Property(name="badstaticfield")static void setStint(int theStint) { + stint = theStint; + } + } + + private Class<?> getBaseType(JavaElementImpl element) { + return JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType()); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java new file mode 100644 index 0000000000..6a7987a376 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java @@ -0,0 +1,310 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getReference; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; + +/** + * @version $Rev$ $Date$ + */ +public class ReferenceProcessorTestCase { + + private JavaImplementation type; + private ReferenceProcessor processor; + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + processor = new ReferenceProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + } + + @Test + public void testMethodAnnotation() throws Exception { + processor.visitMethod(ReferenceProcessorTestCase.Foo.class.getMethod("setFoo", Ref.class), type); + org.apache.tuscany.sca.assembly.Reference reference = getReference(type, "foo"); + assertNotNull(reference); + assertEquals(Ref.class, ((JavaInterface)reference.getInterfaceContract().getInterface()).getJavaClass()); + } + + @Test + public void testMethodRequired() throws Exception { + processor.visitMethod(ReferenceProcessorTestCase.Foo.class.getMethod("setFooRequired", Ref.class), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "fooRequired"); + assertNotNull(ref); + assertEquals(Multiplicity.ONE_ONE, ref.getMultiplicity()); + } + + @Test + public void testMethodName() throws Exception { + processor.visitMethod(ReferenceProcessorTestCase.Foo.class.getMethod("setBarMethod", Ref.class), type); + assertNotNull(getReference(type, "bar")); + } + + @Test + public void testFieldAnnotation() throws Exception { + processor.visitField(ReferenceProcessorTestCase.Foo.class.getDeclaredField("baz"), type); + org.apache.tuscany.sca.assembly.Reference reference = getReference(type, "baz"); + assertNotNull(reference); + assertEquals(Ref.class, ((JavaInterface)reference.getInterfaceContract().getInterface()).getJavaClass()); + } + + @Test + public void testFieldRequired() throws Exception { + processor.visitField(ReferenceProcessorTestCase.Foo.class.getDeclaredField("bazRequired"), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "bazRequired"); + assertNotNull(ref); + assertEquals(Multiplicity.ONE_ONE, ref.getMultiplicity()); + } + + @Test + public void testFieldName() throws Exception { + processor.visitField(ReferenceProcessorTestCase.Foo.class.getDeclaredField("bazField"), type); + assertNotNull(getReference(type, "theBaz")); + } + + @Test + public void testDuplicateFields() throws Exception { + processor.visitField(ReferenceProcessorTestCase.Bar.class.getDeclaredField("dup"), type); + try { + processor.visitField(ReferenceProcessorTestCase.Bar.class.getDeclaredField("baz"), type); + fail(); + } catch (DuplicateReferenceException e) { + // expected + } + } + + @Test + public void testDuplicateMethods() throws Exception { + processor.visitMethod(ReferenceProcessorTestCase.Bar.class.getMethod("setDupMethod", Ref.class), type); + try { + processor.visitMethod(ReferenceProcessorTestCase.Bar.class.getMethod("setDupSomeMethod", Ref.class), type); + fail(); + } catch (DuplicateReferenceException e) { + // expected + } + } + + @Test + public void testInvalidProperty() throws Exception { + try { + processor.visitMethod(ReferenceProcessorTestCase.Bar.class.getMethod("badMethod"), type); + fail(); + } catch (IllegalReferenceException e) { + // expected + } + } + + @Test + public void testClassWithBadMethodArgReference() throws Exception { + Method meth = BadMethAnn.class.getMethod("BadMethod", String.class); + + try { + processor.visitMethod(meth, type); + + fail("reference annotation on ordinary method arg should be rejected"); + } catch (IllegalReferenceException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception detected"); + e.printStackTrace(); + } + } + + @Test + public void testMultiplicity1ToN() throws Exception { + processor.visitField(Multiple.class.getDeclaredField("refs1"), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "refs1"); + assertNotNull(ref); + assertSame(Ref.class, ((JavaInterface)ref.getInterfaceContract().getInterface()).getJavaClass()); + assertEquals(Multiplicity.ONE_N, ref.getMultiplicity()); + } + + @Test + public void testMultiplicityTo0ToN() throws Exception { + processor.visitField(Multiple.class.getDeclaredField("refs2"), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "refs2"); + assertNotNull(ref); + assertSame(Ref.class, ((JavaInterface)ref.getInterfaceContract().getInterface()).getJavaClass()); + assertEquals(Multiplicity.ZERO_N, ref.getMultiplicity()); + } + + @Test + public void testMultiplicity1ToNMethod() throws Exception { + processor.visitMethod(Multiple.class.getMethod("setRefs3", Ref[].class), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "refs3"); + assertNotNull(ref); + assertSame(Ref.class, ((JavaInterface)ref.getInterfaceContract().getInterface()).getJavaClass()); + assertEquals(Multiplicity.ONE_N, ref.getMultiplicity()); + } + + @Test + public void testMultiplicity0ToNMethod() throws Exception { + processor.visitMethod(Multiple.class.getMethod("setRefs4", Collection.class), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "refs4"); + assertNotNull(ref); + assertSame(Ref.class, ((JavaInterface)ref.getInterfaceContract().getInterface()).getJavaClass()); + assertEquals(Multiplicity.ZERO_N, ref.getMultiplicity()); + } + + @Test + public void testRejectStaticFieldReference() throws Exception { + try { + processor.visitField(BadStaticRefs.class.getDeclaredField("stint"), type); + fail("Processor should not accept a static field with Property annotation"); + } + catch (IllegalReferenceException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception detected"); + e.printStackTrace(); + } + } + + @Test + public void testRejectStaticMethodReference() throws Exception { + try { + processor.visitMethod(BadStaticRefs.class.getDeclaredMethod("setStint",int.class), type); + fail("Processor should not accept a static method with Property annotation"); + } + catch (IllegalPropertyException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception detected"); + e.printStackTrace(); + } + + } + + /** + * Private classes utilized in the tests + */ + + private interface Ref { + } + + private class Foo { + @Reference + protected Ref baz; + + @Reference(required = true) + protected Ref bazRequired; + + @Reference(name = "theBaz") + protected Ref bazField; + + @Reference + public void setFoo(Ref ref) { + } + + @Reference(required = true) + public void setFooRequired(Ref ref) { + } + + @Reference(name = "bar") + public void setBarMethod(Ref ref) { + } + } + + private class Bar { + @Reference + protected Ref dup; + + @Reference(name = "dup") + protected Ref baz; + + @Reference + public void setDupMethod(Ref s) { + } + + @Reference(name = "dupMethod") + public void setDupSomeMethod(Ref s) { + } + + @Reference + public void badMethod() { + } + + } + + private class Multiple { + @Reference(required = true) + protected List<Ref> refs1; + + @Reference(required = false) + protected Ref[] refs2; + + @Reference(required = true) + public void setRefs3(Ref[] refs) { + } + + @Reference(required = false) + public void setRefs4(Collection<Ref> refs) { + } + } + + private static class BadStaticRefs { + + @Reference(name="badstaticfield")static int stint; + + @Reference(name="badstaticfield")static void setStint(int theStint) { + stint = theStint; + } + } + + + private static class BadMethAnn { + + @org.oasisopen.sca.annotation.Constructor() + public BadMethAnn(@Property(name = "myProp", required = true)String prop) { + + } + + /** Java can't tell that the @reference argument is disallowed by SCA, but the run time must reject it*/ + public void BadMethod(@Reference(name = "badMethodArgRef")String methArg) { + + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessorTestCase.java new file mode 100644 index 0000000000..c09b0547c9 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessorTestCase.java @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaResourceImpl; +import org.junit.Before; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class ResourceProcessorTestCase { + + JavaImplementation type; + ResourceProcessor processor = new ResourceProcessor(new DefaultAssemblyFactory()); + + @Test + public void testVisitField() throws Exception { + Field field = Foo.class.getDeclaredField("bar"); + processor.visitField(field, type); + JavaResourceImpl resource = type.getResources().get("bar"); + assertFalse(resource.isOptional()); + assertNull(resource.getMappedName()); + assertEquals(field.getType(), resource.getElement().getType()); + } + + @Test + public void testVisitMethod() throws Exception { + Method method = Foo.class.getMethod("setBar", Bar.class); + processor.visitMethod(method, type); + JavaResourceImpl resource = type.getResources().get("bar"); + assertFalse(resource.isOptional()); + assertNull(resource.getMappedName()); + assertEquals(method.getParameterTypes()[0], resource.getElement().getType()); + } + + @Test + public void testVisitNamedMethod() throws Exception { + Method method = Foo.class.getMethod("setBar2", Bar.class); + processor.visitMethod(method, type); + JavaResourceImpl resource = type.getResources().get("someName"); + assertFalse(resource.isOptional()); + assertEquals("mapped", resource.getMappedName()); + } + + @Test + public void testVisitBadMethod() throws Exception { + Method method = Foo.class.getMethod("setBad"); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalResourceException e) { + // expected + } + } + + @Test + public void testDuplicateResources() throws Exception { + Field field = Foo.class.getDeclaredField("bar"); + processor.visitField(field, type); + try { + processor.visitField(field, type); + fail(); + } catch (DuplicateResourceException e) { + //expected + } + } + + @Before + public void setUp() throws Exception { + JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + } + + private class Foo { + + @org.apache.tuscany.sca.implementation.java.introspect.impl.Resource + protected Bar bar; + + @org.apache.tuscany.sca.implementation.java.introspect.impl.Resource(optional = true) + protected Bar barNotRequired; + + @org.apache.tuscany.sca.implementation.java.introspect.impl.Resource + public void setBar(Bar bar) { + } + + @org.apache.tuscany.sca.implementation.java.introspect.impl.Resource(name = "someName", mappedName = "mapped") + public void setBar2(Bar bar) { + } + + @org.apache.tuscany.sca.implementation.java.introspect.impl.Resource + public void setBad() { + } + + } + + private interface Bar { + + } +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ScopeProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ScopeProcessorTestCase.java new file mode 100644 index 0000000000..758584221d --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ScopeProcessorTestCase.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaScopeImpl; +import org.junit.Before; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class ScopeProcessorTestCase { + + private JavaImplementationFactory javaImplementationFactory; + + @Test + public void testCompositeScope() throws IntrospectionException { + ScopeProcessor processor = new ScopeProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + + processor.visitClass(Composite.class, type); + assertEquals(JavaScopeImpl.COMPOSITE, type.getJavaScope()); + } + + @Test + public void testStatelessScope() throws IntrospectionException { + ScopeProcessor processor = new ScopeProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(Stateless.class, type); + assertEquals(JavaScopeImpl.STATELESS, type.getJavaScope()); + } + + @Test + public void testNoScope() throws IntrospectionException { + ScopeProcessor processor = new ScopeProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(None.class, type); + assertEquals(JavaScopeImpl.STATELESS, type.getJavaScope()); + } + + @Before + public void setUp() throws Exception { + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + @org.oasisopen.sca.annotation.Scope("COMPOSITE") + private class Composite { + } + + @org.oasisopen.sca.annotation.Scope("STATELESS") + private class Stateless { + } + + private class None { + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceCallbackTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceCallbackTestCase.java new file mode 100644 index 0000000000..68f3b9e456 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceCallbackTestCase.java @@ -0,0 +1,197 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getService; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.InvalidCallbackException; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.Service; + +/** + * @version $Rev$ $Date$ + */ +public class ServiceCallbackTestCase { + private ServiceProcessor processor; + private JavaImplementationFactory javaImplementationFactory; + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + processor = new ServiceProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + @Test + public void testMethodCallbackInterface() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(FooImpl.class, type); + org.apache.tuscany.sca.assembly.Service service = getService(type, Foo.class.getSimpleName()); + assertNotNull(service); + Method method = FooImpl.class.getMethod("setCallback", FooCallback.class); + processor.visitMethod(method, type); + assertEquals(method, type.getCallbackMembers().get(FooCallback.class.getName()).iterator().next().getAnchor()); + } + + @Test + public void testFieldCallbackInterface() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(FooImpl.class, type); + org.apache.tuscany.sca.assembly.Service service = getService(type, Foo.class.getSimpleName()); + assertNotNull(service); + Field field = FooImpl.class.getDeclaredField("callback"); + processor.visitField(field, type); + assertEquals(field, type.getCallbackMembers().get(FooCallback.class.getName()).iterator().next().getAnchor()); + } + + @Test + public void testFieldCallbackInterface1() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(FooImpl1.class, type); + org.apache.tuscany.sca.assembly.Service service = getService(type, Foo.class.getSimpleName()); + assertNotNull(service); + Field field1 = FooImpl1.class.getDeclaredField("callbackRef"); + processor.visitField(field1, type); + assertEquals(field1, type.getCallbackMembers().get(FooCallback.class.getName()).iterator().next().getAnchor()); + + } + + @Test + public void testMethodDoesNotMatchCallback() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(BadBarImpl.class, type); + Method method = BadBarImpl.class.getMethod("setWrongInterfaceCallback", String.class); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalCallbackReferenceException e) { + // expected + } + } + + @Test + public void testNoParamCallback() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(BadBarImpl.class, type); + Method method = BadBarImpl.class.getMethod("setNoParamCallback"); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalCallbackReferenceException e) { + // expected + } + } + + @Test + public void testFieldDoesNotMatchCallback() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(BadBarImpl.class, type); + Field field = BadBarImpl.class.getDeclaredField("wrongInterfaceCallback"); + try { + processor.visitField(field, type); + fail(); + } catch (IllegalCallbackReferenceException e) { + // expected + } + } + + @Test + public void testBadCallbackInterfaceAnnotation() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitClass(BadFooImpl.class, type); + fail(); + } catch (IntrospectionException e) { + // expected + assertTrue(e.getCause() instanceof InvalidCallbackException); + } + } + + @Callback(FooCallback.class) + private interface Foo { + + } + + private interface FooCallback { + + } + + @Service(Foo.class) + private static class FooImpl implements Foo { + + @Callback + protected FooCallback callback; + + @Callback + public void setCallback(FooCallback cb) { + + } + } + + @Service(Foo.class) + private static class FooImpl1 implements Foo { + @Callback + protected ServiceReference<FooCallback> callbackRef; + } + + private static class BadBarImpl implements Foo { + @Callback + protected String wrongInterfaceCallback; + + @Callback + public void setWrongInterfaceCallback(String cb) { + + } + + @Callback + public void setNoParamCallback() { + + } + + } + + @Callback + private interface BadFoo { + + } + + @Service(BadFoo.class) + private static class BadFooImpl implements BadFoo { + + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessorTestCase.java new file mode 100644 index 0000000000..4adf57eeeb --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessorTestCase.java @@ -0,0 +1,219 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import javax.jws.WebService; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.Remotable; +import org.oasisopen.sca.annotation.Service; + +/** + * @version $Rev$ $Date$ + */ +public class ServiceProcessorTestCase { + private ServiceProcessor processor; + private JavaImplementation type; + + @Test + public void testMultipleInterfaces() throws Exception { + processor.visitClass(FooMultiple.class, type); + assertEquals(2, type.getServices().size()); + org.apache.tuscany.sca.assembly.Service service = ModelHelper.getService(type, Baz.class.getSimpleName()); + assertEquals(Baz.class, ((JavaInterface)service.getInterfaceContract().getInterface()).getJavaClass()); + assertEquals(Bar.class, ((JavaInterface)service.getInterfaceContract().getCallbackInterface()).getJavaClass()); + assertNotNull(ModelHelper.getService(type, Bar.class.getSimpleName())); + } + + @Test + public void testSingleInterfaces() throws Exception { + processor.visitClass(FooSingle.class, type); + assertEquals(1, type.getServices().size()); + assertNotNull(ModelHelper.getService(type, Baz.class.getSimpleName())); + } + + @Test + public void testMultipleNoService() throws Exception { + processor.visitClass(FooMultipleNoService.class, type); + assertEquals(0, type.getServices().size()); + } + + /** + * Verifies a service with a callback annotation is recognized + */ + @Test + public void testMultipleWithCallbackAnnotation() throws Exception { + processor.visitClass(FooMultipleWithCalback.class, type); + assertEquals(1, type.getServices().size()); + } + + + @Test + public void testMultipleWithWebServiceAnnotation() throws Exception { + processor.visitClass(FooMultipleWithWebService.class, type); + assertEquals(2, type.getServices().size()); + } + + @Test + public void testRemotableNoService() throws Exception { + processor.visitClass(FooRemotableNoService.class, type); + assertEquals(1, type.getServices().size()); + org.apache.tuscany.sca.assembly.Service service = ModelHelper.getService(type, BazRemotable.class.getSimpleName()); + assertEquals(BazRemotable.class, ((JavaInterface)service.getInterfaceContract().getInterface()).getJavaClass()); + } + + @Test + public void testNonInterface() throws Exception { + processor.visitClass(FooServiceUsingClassImpl.class, type); + } + + @Test + public void testMultiplenamedInterfaces() throws Exception { + processor.visitClass(FooMultipleNamed.class, type); + assertEquals(2, type.getServices().size()); + org.apache.tuscany.sca.assembly.Service sbaz = type.getService("BazName"); + assertNotNull(sbaz); + assertEquals(Baz.class, ((JavaInterface)sbaz.getInterfaceContract().getInterface()).getJavaClass()); + org.apache.tuscany.sca.assembly.Service sbar = type.getService("BarName"); + assertNotNull(sbar); + assertEquals(Bar.class, ((JavaInterface)sbar.getInterfaceContract().getInterface()).getJavaClass()); + } + + @Test + public void testBadService() throws Exception { + try { + processor.visitClass(BadService.class, type); + fail(); + } catch (IntrospectionException e) { + assertTrue(e.getMessage().startsWith("[JCA90059]")); + } + } + + @Test + public void testBadServiceNames() throws Exception { + try { + processor.visitClass(BadServiceNames.class, type); + fail(); + } catch (IntrospectionException e) { + assertTrue(e.getMessage().startsWith("[JCA90050]")); + } + } + + @Test + public void testBadServiceDuplicateNames() throws Exception { + try { + processor.visitClass(BadServiceDuplicateNames.class, type); + fail(); + } catch (IntrospectionException e) { + assertTrue(e.getMessage().startsWith("[JCA90060]")); + } + } + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + processor = new ServiceProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + } + + @Callback(Bar.class) + private interface Baz { + } + + private interface Bar { + } + + private interface Bar2 { + } + + @WebService + private interface Bar3 { + } + + @Remotable + private interface BazRemotable { + } + + @Service({Baz.class, Bar.class}) + private class FooMultiple implements Baz, Bar { + + } + + @Service(Baz.class) + private class FooSingle implements Baz, Bar { + + } + + private class FooMultipleNoService implements Bar, Bar2 { + + } + + private class FooMultipleWithCalback implements Baz, Bar { + + } + + private class FooMultipleWithWebService implements BazRemotable, Bar3 { + } + + private class FooRemotableNoService implements BazRemotable, Bar { + + } + + @Service(FooSingle.class) + private class FooServiceUsingClassImpl extends FooSingle { + + } + + @Service(value={Baz.class, Bar.class}, names={"BazName", "BarName"}) + private class FooMultipleNamed implements Baz, Bar { + + } + + @Service(value={}) + private class BadService implements Baz { + + } + + @Service(value={Baz.class, Bar.class}, names={"BazName"}) + private class BadServiceNames implements Baz, Bar { + + } + + @Service(value={Baz.class, Bar.class}, names={"BazName", "BazName"}) + private class BadServiceDuplicateNames implements Baz, Bar { + + } +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/TestAbstractPropertyProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/TestAbstractPropertyProcessorTestCase.java new file mode 100644 index 0000000000..a90a6d18c7 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/TestAbstractPropertyProcessorTestCase.java @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getProperty; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.annotation.Retention; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor; +import org.junit.Before; +import org.junit.Test; + + +/** + * @version $Rev$ $Date$ + */ +public class TestAbstractPropertyProcessorTestCase { + + private JavaClassVisitor extension; + private JavaImplementationFactory javaImplementationFactory; + + @Test + public void testVisitMethod() throws Exception { + Method method = Foo.class.getMethod("setBar", String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + extension.visitMethod(method, type); + Property prop = getProperty(type, "test"); + assertNotNull(prop); + } + + @Test + public void testVisitNoParamsMethod() throws Exception { + Method method = Foo.class.getMethod("setNoParamsBar"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + extension.visitMethod(method, type); + fail(); + } catch (IllegalPropertyException e) { + // expected + } + } + + @Test + public void testVisitNonVoidMethod() throws Exception { + Method method = Foo.class.getMethod("setBadBar", String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + extension.visitMethod(method, type); + fail(); + } catch (IllegalPropertyException e) { + // expected + } + } + + @Test + public void testDuplicateMethod() throws Exception { + Method method = Foo.class.getMethod("setBar", String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + extension.visitMethod(method, type); + try { + extension.visitMethod(method, type); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + @Test + public void testVisitField() throws Exception { + Field field = Foo.class.getDeclaredField("d"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + extension.visitField(field, type); + Property prop = getProperty(type, "test"); + assertNotNull(prop); + } + + @Test + public void testVisitConstructor() throws Exception { + Constructor<Foo> ctor = Foo.class.getConstructor(String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + JavaConstructorImpl<Foo> def = new JavaConstructorImpl<Foo>(ctor); + JavaParameterImpl parameter = def.getParameters()[0]; + extension.visitConstructorParameter(parameter, type); + assertEquals("test", def.getParameters()[0].getName()); + assertNotNull(getProperty(type, "test")); + } + + @Before + public void setUp() throws Exception { + extension = new TestProcessor(); + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + @Retention(RUNTIME) + private @interface Bar { + + } + + private class TestProcessor extends AbstractPropertyProcessor<Bar> { + + public TestProcessor() { + super(new DefaultExtensionPointRegistry(), Bar.class); + } + + @Override + protected void initProperty(Property property, Bar annotation) { + // property.setDefaultValueFactory(EasyMock.createMock(ObjectFactory.class)); + property.setName("test"); + } + + @Override + protected String getName(Bar annotation) { + return "test"; + } + + @Override + protected boolean getRequired(Bar annotation) { + return true; + } + } + + private static class Foo { + + @Bar + protected String d; + + public Foo(String a, @Bar + String b) { + } + + public Foo(@Bar + String d) { + this.d = d; + } + + @Bar + public void setBar(String d) { + this.d = d; + } + + @Bar + public void setNoParamsBar() { + } + + @Bar + public String setBadBar(String d) { + return null; + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/ReadTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/ReadTestCase.java new file mode 100644 index 0000000000..635b491b06 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/ReadTestCase.java @@ -0,0 +1,241 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.InputStream; +import java.net.URI; +import java.net.URL; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Test reading Java implementations. + * + * @version $Rev$ $Date$ + */ +public class ReadTestCase { + + private static XMLInputFactory inputFactory; + private static StAXArtifactProcessor<Object> staxProcessor; + private static URLArtifactProcessor<Definitions> policyDefinitionsProcessor; + private static CompositeBuilder compositeBuilder; + private static Monitor monitor; + private static ProcessorContext context; + private static BuilderContext builderContext; + + @BeforeClass + public static void setUp() throws Exception { + DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + builderContext = new BuilderContext(extensionPoints); + inputFactory = XMLInputFactory.newInstance(); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null); + + compositeBuilder = extensionPoints.getExtensionPoint(BuilderExtensionPoint.class).getCompositeBuilder("org.apache.tuscany.sca.assembly.builder.CompositeBuilder"); + + URLArtifactProcessorExtensionPoint documentProcessors = extensionPoints.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + policyDefinitionsProcessor = documentProcessors.getProcessor(Definitions.class); + + UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); + monitor = monitorFactory.createMonitor(); + } + + @Test + public void testReadComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("Calculator.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite)staxProcessor.read(reader, context); + assertNotNull(composite); + + compositeBuilder.build(composite, builderContext); + + } + + @Ignore("To be fixed") + @Test + public void testPolicyIntents() throws Exception { + ModelResolver resolver = new TestModelResolver(getClass().getClassLoader()); + + URL url = getClass().getResource("definitions.xml"); + URI uri = URI.create("definitions.xml"); + Definitions scaDefns = policyDefinitionsProcessor.read(null, uri, url, context); + + InputStream is = getClass().getResourceAsStream("Calculator.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite)staxProcessor.read(reader, context); + assertNotNull(composite); + + staxProcessor.resolve(scaDefns, resolver, context); + staxProcessor.resolve(composite, resolver, context); + + builderContext.setDefinitions(scaDefns); + compositeBuilder.build(composite, builderContext); + + //intents are computed and aggregate intents from ancestor elements + assertEquals(((PolicySubject)composite.getComponents().get(0)).getRequiredIntents().size(), 3); + assertEquals(((PolicySubject)composite.getComponents().get(5)).getRequiredIntents().size(), 3); + + //assertEquals(((OperationsConfigurator)composite.getComponents().get(0)).getConfiguredOperations().isEmpty(), true); + //assertEquals(((OperationsConfigurator)composite.getComponents().get(5)).getConfiguredOperations().isEmpty(), false); + + + //test for proper aggregation of policy intents on implementation elements + for ( Intent intent : ((PolicySubject)composite.getComponents().get(0).getImplementation()).getRequiredIntents() ) { + String intentName = intent.getName().getLocalPart(); + if ( !(intentName.equals("tuscanyIntent_1") || intentName.equals("tuscanyIntent_2") || + intentName.equals("tuscanyIntent_3")) ) { + fail(); + } + } + + for ( Intent intent : ((PolicySubject)composite.getComponents().get(5)).getRequiredIntents() ) { + String intentName = intent.getName().getLocalPart(); + if ( !(intentName.equals("tuscanyIntent_1") || intentName.equals("tuscanyIntent_4") || + intentName.equals("tuscanyIntent_5")) ) { + fail(); + } + } + + /* + //test for proper aggregation of policy intents and policysets on operations of implementation + OperationsConfigurator opConf = (OperationsConfigurator)composite.getComponents().get(5); + assertEquals(opConf.getConfiguredOperations().get(0).getRequiredIntents().size(), 4); + for ( Intent intent : opConf.getConfiguredOperations().get(0).getRequiredIntents()) { + String intentName = intent.getName().getLocalPart(); + if ( !(intentName.equals("tuscanyIntent_1") || intentName.equals("tuscanyIntent_4") || + intentName.equals("tuscanyIntent_5") || intentName.equals("tuscanyIntent_6") ) ) { + fail(); + } + } + + opConf = (OperationsConfigurator)composite.getComponents().get(6); + assertEquals(opConf.getConfiguredOperations().get(0).getRequiredIntents().size(), 3); + for ( Intent intent : opConf.getConfiguredOperations().get(0).getRequiredIntents()) { + String intentName = intent.getName().getLocalPart(); + if ( !(intentName.equals("tuscanyIntent_1") || intentName.equals("tuscanyIntent_4") || + intentName.equals("tuscanyIntent_6.qualified2") ) ) { + fail(); + } + } + */ + } + + @Test + @Ignore("Broken for now in 2.0 bringup") + public void testPolicySets() throws Exception { + ModelResolver resolver = new TestModelResolver(getClass().getClassLoader()); + + URL url = getClass().getResource("definitions_with_policysets.xml"); + URI uri = URI.create("definitions_with_policysets.xml"); + Definitions policyDefinitions = policyDefinitionsProcessor.read(null, uri, url, context); + + InputStream is = getClass().getResourceAsStream("Calculator.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite)staxProcessor.read(reader, context); + assertNotNull(composite); + + for ( Component component : composite.getComponents() ) { + for ( PolicySet policySet : policyDefinitions.getPolicySets() ) { + component.getPolicySets().add(policySet); + } + } + + staxProcessor.resolve(policyDefinitions, resolver, context); + staxProcessor.resolve(composite, resolver, context); + + builderContext.setDefinitions(policyDefinitions); + compositeBuilder.build(composite, builderContext); + + //test for determination of policysets for implementation + assertEquals(((PolicySubject)composite.getComponents().get(0)).getPolicySets().size(), 1); + for ( PolicySet policySet : ((PolicySubject)composite.getComponents().get(0).getImplementation()).getPolicySets() ) { + String policySetName = policySet.getName().getLocalPart(); + if ( !(policySetName.equals("tuscanyPolicySet_1")) ) { + fail(); + } + } + + assertEquals(((PolicySubject)composite.getComponents().get(5)).getPolicySets().size(), 2); + for ( PolicySet policySet : ((PolicySubject)composite.getComponents().get(5).getImplementation()).getPolicySets() ) { + String policySetName = policySet.getName().getLocalPart(); + if ( !(policySetName.equals("tuscanyPolicySet_1") || policySetName.equals("tuscanyPolicySet_2")) ) { + fail(); + } + } + + /* + //test for computation of policysets on operations of implementation + OperationsConfigurator opConf = (OperationsConfigurator)composite.getComponents().get(5); + assertEquals(opConf.getConfiguredOperations().get(0).getPolicySets().size(), 3); + for ( PolicySet policySet : opConf.getConfiguredOperations().get(0).getPolicySets() ) { + String policySetName = policySet.getName().getLocalPart(); + if ( !(policySetName.equals("tuscanyPolicySet_1") || policySetName.equals("tuscanyPolicySet_2") + || policySetName.equals("tuscanyPolicySet_3")) ) { + fail(); + } + } + + opConf = (OperationsConfigurator)composite.getComponents().get(6); + assertEquals(opConf.getConfiguredOperations().get(0).getPolicySets().size(), 4); + for ( PolicySet policySet : opConf.getConfiguredOperations().get(0).getPolicySets() ) { + String policySetName = policySet.getName().getLocalPart(); + if ( !(policySetName.equals("tuscanyPolicySet_1") || policySetName.equals("tuscanyPolicySet_2") + || policySetName.equals("tuscanyPolicySet_3") + || policySetName.equals("tuscanyPolicySet_4")) ) { + fail(); + } + } + */ + + //new PrintUtil(System.out).print(composite); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/TestModelResolver.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/TestModelResolver.java new file mode 100644 index 0000000000..6be50d138f --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/TestModelResolver.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.xml; + +import java.lang.ref.WeakReference; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + + +/** + * A default implementation of an artifact resolver, based on a map. + * + * @version $Rev$ $Date$ + */ +public class TestModelResolver implements ModelResolver { + private static final long serialVersionUID = -7826976465762296634L; + + private Map<Object, Object> map = new HashMap<Object, Object>(); + + private WeakReference<ClassLoader> classLoader; + + public TestModelResolver(ClassLoader classLoader) { + this.classLoader = new WeakReference<ClassLoader>(classLoader); + } + + public <T> T resolveModel(Class<T> modelClass, T unresolved, ProcessorContext context) { + Object resolved = map.get(unresolved); + if (resolved != null) { + + // Return the resolved object + return modelClass.cast(resolved); + + } else if (unresolved instanceof ClassReference) { + + // Load a class on demand + ClassReference classReference = (ClassReference)unresolved; + Class<?> clazz; + try { + clazz = Class.forName(classReference.getClassName(), true, classLoader.get()); + } catch (ClassNotFoundException e) { + + // Return the unresolved object + return unresolved; + } + + // Store a new ClassReference wrapping the loaded class + resolved = new ClassReference(clazz); + map.put(resolved, resolved); + + // Return the resolved ClassReference + return modelClass.cast(resolved); + + } else { + + // Return the unresolved object + return unresolved; + } + } + + public void addModel(Object resolved, ProcessorContext context) { + map.put(resolved, resolved); + } + + public Object removeModel(Object resolved, ProcessorContext context) { + return map.remove(resolved); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/WriteTestCase.java b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/WriteTestCase.java new file mode 100644 index 0000000000..fdd606c58e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/WriteTestCase.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.xml; + +import java.io.InputStream; +import java.io.StringWriter; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test writing Java implementations. + * + * @version $Rev$ $Date$ + */ +public class WriteTestCase { + + private static StAXArtifactProcessor<Object> staxProcessor; + private static XMLInputFactory inputFactory; + private static XMLOutputFactory outputFactory; + private static ProcessorContext context; + + @BeforeClass + public static void setUp() throws Exception { + DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + inputFactory = XMLInputFactory.newInstance(); + outputFactory = XMLOutputFactory.newInstance(); + outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, outputFactory); + } + + @Test + public void testReadWriteComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("Calculator.composite"); + Composite composite = (Composite)staxProcessor.read(inputFactory.createXMLStreamReader(is), context); + Assert.assertNotNull(composite); + StringWriter sw = new StringWriter(); + staxProcessor.write(composite, outputFactory.createXMLStreamWriter(sw), context); + System.out.println(sw.toString()); + } + +} |