/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.tuscany.core.wire; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.tuscany.spi.QualifiedName; import org.apache.tuscany.spi.component.ReferenceBinding; import org.apache.tuscany.spi.model.DataType; import org.apache.tuscany.spi.model.Operation; import static org.apache.tuscany.spi.model.Operation.NO_CONVERSATION; import org.apache.tuscany.spi.model.ServiceContract; import org.apache.tuscany.spi.wire.ChainHolder; import org.apache.tuscany.spi.wire.IncompatibleServiceContractException; import org.apache.tuscany.spi.wire.ProxyCreationException; import org.apache.tuscany.spi.wire.Wire; import org.apache.tuscany.spi.wire.ProxyService; import org.apache.tuscany.spi.wire.InvocationChain; import junit.framework.TestCase; import org.osoa.sca.CallableReference; /** * TODO some tests commented out due to DataType.equals() needing to be strict * * @version $Rev$ $Date$ */ public class ContractCompatibilityTestCase extends TestCase { private ProxyService proxyService = new MockProxyService(); public void testNoOperation() throws Exception { ServiceContract source = new MockContract("FooContract"); ServiceContract target = new MockContract("FooContract"); proxyService.checkCompatibility(source, target, false, false); } public void testBasic() throws Exception { ServiceContract source = new MockContract("FooContract"); Operation opSource1 = new Operation("op1", null, null, null, false, null, NO_CONVERSATION); Map> sourceOperations = new HashMap>(); sourceOperations.put("op1", opSource1); source.setOperations(sourceOperations); ServiceContract target = new MockContract("FooContract"); Operation opSource2 = new Operation("op1", null, null, null, false, null, NO_CONVERSATION); Map> targetOperations = new HashMap>(); targetOperations.put("op1", opSource2); target.setOperations(targetOperations); proxyService.checkCompatibility(source, target, false, false); } public void testBasicIncompatibleOperationNames() throws Exception { ServiceContract source = new MockContract("FooContract"); Operation opSource1 = new Operation("op1", null, null, null, false, null, NO_CONVERSATION); Map> sourceOperations = new HashMap>(); sourceOperations.put("op1", opSource1); source.setOperations(sourceOperations); ServiceContract target = new MockContract("FooContract"); Operation opSource2 = new Operation("op2", null, null, null, false, null, NO_CONVERSATION); Map> targetOperations = new HashMap>(); targetOperations.put("op2", opSource2); target.setOperations(targetOperations); try { proxyService.checkCompatibility(source, target, false, false); fail(); } catch (IncompatibleServiceContractException e) { //expected } } public void testInputTypes() throws Exception { ServiceContract source = new MockContract("FooContract"); List> sourceInputTypes = new ArrayList>(); sourceInputTypes.add(new DataType(Object.class, Object.class)); DataType>> inputType = new DataType>>(String.class, sourceInputTypes); Operation opSource1 = new Operation("op1", inputType, null, null, false, null, NO_CONVERSATION); Map> sourceOperations = new HashMap>(); sourceOperations.put("op1", opSource1); source.setOperations(sourceOperations); ServiceContract target = new MockContract("FooContract"); List> targetInputTypes = new ArrayList>(); targetInputTypes.add(new DataType(Object.class, Object.class)); DataType>> targetInputType = new DataType>>(String.class, targetInputTypes); Operation opTarget = new Operation("op1", targetInputType, null, null, false, null, NO_CONVERSATION); Map> targetOperations = new HashMap>(); targetOperations.put("op1", opTarget); target.setOperations(targetOperations); proxyService.checkCompatibility(source, target, false, false); } public void testIncompatibleInputTypes() throws Exception { ServiceContract source = new MockContract("FooContract"); List> sourceInputTypes = new ArrayList>(); sourceInputTypes.add(new DataType(Integer.class, Integer.class)); DataType>> inputType = new DataType>>(String.class, sourceInputTypes); Operation opSource1 = new Operation("op1", inputType, null, null, false, null, NO_CONVERSATION); Map> sourceOperations = new HashMap>(); sourceOperations.put("op1", opSource1); source.setOperations(sourceOperations); ServiceContract target = new MockContract("FooContract"); List> targetInputTypes = new ArrayList>(); targetInputTypes.add(new DataType(String.class, String.class)); DataType>> targetInputType = new DataType>>(String.class, targetInputTypes); Operation opTarget = new Operation("op1", targetInputType, null, null, false, null, NO_CONVERSATION); Map> targetOperations = new HashMap>(); targetOperations.put("op1", opTarget); target.setOperations(targetOperations); try { proxyService.checkCompatibility(source, target, false, false); fail(); } catch (IncompatibleServiceContractException e) { //expected } } /** * Verfies source input types can be super types of the target */ public void testSourceSuperTypeInputCompatibility() throws Exception { // ServiceContract source = new MockContract("FooContract"); // List> sourceInputTypes = new ArrayList>(); // sourceInputTypes.add(new DataType(Object.class, Object.class)); // DataType>> inputType = new DataType>>(String.class, sourceInputTypes); // Operation opSource1 = new Operation("op1", inputType, null, null, false, null); // Map> sourceOperations = new HashMap>(); // sourceOperations.put("op1", opSource1); // source.setOperations(sourceOperations); // // ServiceContract target = new MockContract("FooContract"); // List> targetInputTypes = new ArrayList>(); // targetInputTypes.add(new DataType(String.class, String.class)); // DataType>> targetInputType = // new DataType>>(String.class, targetInputTypes); // // Operation opTarget = new Operation("op1", targetInputType, null, null, false, null); // Map> targetOperations = new HashMap>(); // targetOperations.put("op1", opTarget); // target.setOperations(targetOperations); // wireService.checkCompatibility(source, target, false); } public void testOutputTypes() throws Exception { ServiceContract source = new MockContract("FooContract"); DataType sourceOutputType = new DataType(String.class, String.class); Operation opSource1 = new Operation("op1", null, sourceOutputType, null, false, null, NO_CONVERSATION); Map> sourceOperations = new HashMap>(); sourceOperations.put("op1", opSource1); source.setOperations(sourceOperations); ServiceContract target = new MockContract("FooContract"); DataType targetOutputType = new DataType(String.class, String.class); Operation opTarget = new Operation("op1", null, targetOutputType, null, false, null, NO_CONVERSATION); Map> targetOperations = new HashMap>(); targetOperations.put("op1", opTarget); target.setOperations(targetOperations); proxyService.checkCompatibility(source, target, false, false); } /** * Verfies a return type that is a supertype of of the target is compatible */ public void testSupertypeOutputTypes() throws Exception { // ServiceContract source = new MockContract("FooContract"); // DataType sourceOutputType = new DataType(Object.class, Object.class); // Operation opSource1 = new Operation("op1", null, sourceOutputType, null, false, null); // Map> sourceOperations = new HashMap>(); // sourceOperations.put("op1", opSource1); // source.setOperations(sourceOperations); // // ServiceContract target = new MockContract("FooContract"); // DataType targetOutputType = new DataType(String.class, String.class); // Operation opTarget = new Operation("op1", null, targetOutputType, null, false, null); // Map> targetOperations = new HashMap>(); // targetOperations.put("op1", opTarget); // target.setOperations(targetOperations); // wireService.checkCompatibility(source, target, false); } public void testIncompatibleOutputTypes() throws Exception { ServiceContract source = new MockContract("FooContract"); DataType sourceOutputType = new DataType(String.class, String.class); Operation opSource1 = new Operation("op1", null, sourceOutputType, null, false, null, NO_CONVERSATION); Map> sourceOperations = new HashMap>(); sourceOperations.put("op1", opSource1); source.setOperations(sourceOperations); ServiceContract target = new MockContract("FooContract"); DataType targetOutputType = new DataType(Integer.class, Integer.class); Operation opTarget = new Operation("op1", null, targetOutputType, null, false, null, NO_CONVERSATION); Map> targetOperations = new HashMap>(); targetOperations.put("op1", opTarget); target.setOperations(targetOperations); try { proxyService.checkCompatibility(source, target, false, false); fail(); } catch (IncompatibleServiceContractException e) { //expected } } public void testFaultTypes() throws Exception { ServiceContract source = new MockContract("FooContract"); DataType sourceFaultType = new DataType(String.class, String.class); List> sourceFaultTypes = new ArrayList>(); sourceFaultTypes.add(0, sourceFaultType); Operation opSource1 = new Operation("op1", null, null, sourceFaultTypes, false, null, NO_CONVERSATION); Map> sourceOperations = new HashMap>(); sourceOperations.put("op1", opSource1); source.setOperations(sourceOperations); ServiceContract target = new MockContract("FooContract"); DataType targetFaultType = new DataType(String.class, String.class); List> targetFaultTypes = new ArrayList>(); targetFaultTypes.add(0, targetFaultType); Operation opTarget = new Operation("op1", null, null, targetFaultTypes, false, null, NO_CONVERSATION); Map> targetOperations = new HashMap>(); targetOperations.put("op1", opTarget); target.setOperations(targetOperations); proxyService.checkCompatibility(source, target, false, false); } public void testSourceFaultTargetNoFaultCompatibility() throws Exception { ServiceContract source = new MockContract("FooContract"); DataType sourceFaultType = new DataType(String.class, String.class); List> sourceFaultTypes = new ArrayList>(); sourceFaultTypes.add(0, sourceFaultType); Operation opSource1 = new Operation("op1", null, null, sourceFaultTypes, false, null, NO_CONVERSATION); Map> sourceOperations = new HashMap>(); sourceOperations.put("op1", opSource1); source.setOperations(sourceOperations); ServiceContract target = new MockContract("FooContract"); Operation opTarget = new Operation("op1", null, null, null, false, null, NO_CONVERSATION); Map> targetOperations = new HashMap>(); targetOperations.put("op1", opTarget); target.setOperations(targetOperations); proxyService.checkCompatibility(source, target, false, false); } /** * Verifies a source's fault which is a supertype of the target's fault are compatibile * * @throws Exception */ public void testFaultSuperTypes() throws Exception { // ServiceContract source = new MockContract("FooContract"); // DataType sourceFaultType = new DataType(Exception.class, Exception.class); // List> sourceFaultTypes = new ArrayList>(); // sourceFaultTypes.add(0, sourceFaultType); // Operation opSource1 = new Operation("op1", null, null, sourceFaultTypes, false, null); // Map> sourceOperations = new HashMap>(); // sourceOperations.put("op1", opSource1); // source.setOperations(sourceOperations); // // ServiceContract target = new MockContract("FooContract"); // DataType targetFaultType = new DataType(TuscanyException.class, TuscanyException.class); // List> targetFaultTypes = new ArrayList>(); // targetFaultTypes.add(0, targetFaultType); // // Operation opTarget = new Operation("op1", null, null, targetFaultTypes, false, null); // Map> targetOperations = new HashMap>(); // targetOperations.put("op1", opTarget); // target.setOperations(targetOperations); // wireService.checkCompatibility(source, target, false); } /** * Verifies a source's faults which are supertypes and a superset of the target's faults are compatibile */ public void testFaultSuperTypesAndSuperset() throws Exception { // ServiceContract source = new MockContract("FooContract"); // DataType sourceFaultType = new DataType(Exception.class, Exception.class); // DataType sourceFaultType2 = new DataType(RuntimeException.class, RuntimeException.class); // List> sourceFaultTypes = new ArrayList>(); // sourceFaultTypes.add(0, sourceFaultType); // sourceFaultTypes.add(1, sourceFaultType2); // Operation opSource1 = new Operation("op1", null, null, sourceFaultTypes, false, null); // Map> sourceOperations = new HashMap>(); // sourceOperations.put("op1", opSource1); // source.setOperations(sourceOperations); // // ServiceContract target = new MockContract("FooContract"); // DataType targetFaultType = new DataType(TuscanyException.class, TuscanyException.class); // List> targetFaultTypes = new ArrayList>(); // targetFaultTypes.add(0, targetFaultType); // // Operation opTarget = new Operation("op1", null, null, targetFaultTypes, false, null); // Map> targetOperations = new HashMap>(); // targetOperations.put("op1", opTarget); // target.setOperations(targetOperations); // wireService.checkCompatibility(source, target, false); } private class MockContract extends ServiceContract { public MockContract() { } public MockContract(Class interfaceClass) { super(interfaceClass); } public MockContract(String interfaceName) { super(interfaceName); } } private class MockProxyService extends ProxyServiceExtension { public MockProxyService() { super(null); } public T createProxy(Class interfaze, Wire wire) throws ProxyCreationException { throw new UnsupportedOperationException(); } public T createProxy2(Class interfaze, boolean conversational, Wire wire) throws ProxyCreationException { throw new UnsupportedOperationException(); } public T createProxy2(Class interfaze, Wire wire) throws ProxyCreationException { throw new UnsupportedOperationException(); } public T createProxy(Class interfaze, Wire wire, Map mapping) throws ProxyCreationException { throw new UnsupportedOperationException(); } public T createProxy2(Class interfaze, Wire wire, Map mapping) throws ProxyCreationException { throw new UnsupportedOperationException(); } public Object createCallbackProxy(Class interfaze, List wires) throws ProxyCreationException { throw new UnsupportedOperationException(); } public Object createCallbackProxy(Class interfaze) throws ProxyCreationException { throw new UnsupportedOperationException(); } public void createWires(ReferenceBinding referenceBinding, ServiceContract contract, QualifiedName targetName) { throw new UnsupportedOperationException(); } public > R cast(B target) throws IllegalArgumentException { throw new UnsupportedOperationException(); } } }