Author: jcarman Date: Sat Jul 27 16:13:50 2013 New Revision: 1507678 URL: http://svn.apache.org/r1507678 Log: PROXY-20: Implement A SwitchInterceptor
Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ConstantInterceptor.java commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InvocationMatcher.java commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/MethodNameMatcher.java commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ReturnTypeMatcher.java commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestConstantInterceptor.java commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestMethodNameMatcher.java commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestReturnTypeMatcher.java commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/util/MockInvocation.java Removed: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/filter/ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/logging/ Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ConstantInterceptor.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ConstantInterceptor.java?rev=1507678&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ConstantInterceptor.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ConstantInterceptor.java Sat Jul 27 16:13:50 2013 @@ -0,0 +1,52 @@ +/* + * 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.commons.proxy2.interceptor; + +import org.apache.commons.proxy2.Interceptor; +import org.apache.commons.proxy2.Invocation; + +/** + * A {@link ConstantInterceptor} always returns a constant value. + */ +public class ConstantInterceptor implements Interceptor +{ +//---------------------------------------------------------------------------------------------------------------------- +// Fields +//---------------------------------------------------------------------------------------------------------------------- + + private final Object value; + +//---------------------------------------------------------------------------------------------------------------------- +// Constructors +//---------------------------------------------------------------------------------------------------------------------- + + public ConstantInterceptor(Object value) + { + this.value = value; + } + +//---------------------------------------------------------------------------------------------------------------------- +// Interceptor Implementation +//---------------------------------------------------------------------------------------------------------------------- + + @Override + public Object intercept(Invocation invocation) throws Throwable + { + return value; + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InvocationMatcher.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InvocationMatcher.java?rev=1507678&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InvocationMatcher.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InvocationMatcher.java Sat Jul 27 16:13:50 2013 @@ -0,0 +1,33 @@ +/* + * 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.commons.proxy2.interceptor; + +import org.apache.commons.proxy2.Invocation; + +/** + * An {@link InvocationMatcher} is used to conditionally match {@link Invocation} objects based on + * some criteria such as method name, parameter values, etc. + */ +public interface InvocationMatcher +{ +//---------------------------------------------------------------------------------------------------------------------- +// Other Methods +//---------------------------------------------------------------------------------------------------------------------- + + boolean matches(Invocation invocation); +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java?rev=1507678&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java Sat Jul 27 16:13:50 2013 @@ -0,0 +1,96 @@ +/* + * 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.commons.proxy2.interceptor; + +import org.apache.commons.proxy2.Interceptor; +import org.apache.commons.proxy2.Invocation; + +import java.io.Serializable; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + * A {@link SwitchInterceptor} maintains a list of {@link InvocationMatcher}/{@link Interceptor} pairs. Each + * invocation will be checked against the registered InvocationMatchers. If one matches the current invocation, then + * the corresponding Interceptor will be called. If no InvocationMatchers match, then the invocation will merely + * {@link org.apache.commons.proxy2.Invocation#proceed()} method is called. + */ +public class SwitchInterceptor implements Interceptor, Serializable +{ +//---------------------------------------------------------------------------------------------------------------------- +// Fields +//---------------------------------------------------------------------------------------------------------------------- + + private final List<Case> cases = new CopyOnWriteArrayList<Case>(); + +//---------------------------------------------------------------------------------------------------------------------- +// Constructors +//---------------------------------------------------------------------------------------------------------------------- + + public SwitchInterceptor() + { + } + + public SwitchInterceptor(InvocationMatcher matcher, Interceptor interceptor) + { + cases.add(new Case(matcher, interceptor)); + } + +//---------------------------------------------------------------------------------------------------------------------- +// Interceptor Implementation +//---------------------------------------------------------------------------------------------------------------------- + + @Override + public Object intercept(Invocation invocation) throws Throwable + { + for (Case currentCase : cases) + { + if(currentCase.matcher.matches(invocation)) + { + return currentCase.interceptor.intercept(invocation); + } + } + return invocation.proceed(); + } + +//---------------------------------------------------------------------------------------------------------------------- +// Other Methods +//---------------------------------------------------------------------------------------------------------------------- + + public SwitchInterceptor onCase(InvocationMatcher matcher, Interceptor interceptor) + { + cases.add(new Case(matcher, interceptor)); + return this; + } + +//---------------------------------------------------------------------------------------------------------------------- +// Inner Classes +//---------------------------------------------------------------------------------------------------------------------- + + private static class Case implements Serializable + { + private InvocationMatcher matcher; + private Interceptor interceptor; + + private Case(InvocationMatcher matcher, Interceptor interceptor) + { + this.matcher = matcher; + this.interceptor = interceptor; + } + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/MethodNameMatcher.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/MethodNameMatcher.java?rev=1507678&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/MethodNameMatcher.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/MethodNameMatcher.java Sat Jul 27 16:13:50 2013 @@ -0,0 +1,53 @@ +/* + * 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.commons.proxy2.interceptor.matcher; + +import org.apache.commons.proxy2.Invocation; +import org.apache.commons.proxy2.interceptor.InvocationMatcher; + +/** + * A {@link MethodNameMatcher} simply checks to see that the method name of the invocation matches the target method + * name given in the constructor. + */ +public class MethodNameMatcher implements InvocationMatcher +{ +//---------------------------------------------------------------------------------------------------------------------- +// Fields +//---------------------------------------------------------------------------------------------------------------------- + + private final String methodName; + +//---------------------------------------------------------------------------------------------------------------------- +// Constructors +//---------------------------------------------------------------------------------------------------------------------- + + public MethodNameMatcher(String methodName) + { + this.methodName = methodName; + } + +//---------------------------------------------------------------------------------------------------------------------- +// InvocationMatcher Implementation +//---------------------------------------------------------------------------------------------------------------------- + + @Override + public boolean matches(Invocation invocation) + { + return methodName.equals(invocation.getMethod().getName()); + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ReturnTypeMatcher.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ReturnTypeMatcher.java?rev=1507678&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ReturnTypeMatcher.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ReturnTypeMatcher.java Sat Jul 27 16:13:50 2013 @@ -0,0 +1,59 @@ +/* + * 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.commons.proxy2.interceptor.matcher; + +import org.apache.commons.proxy2.Invocation; +import org.apache.commons.proxy2.interceptor.InvocationMatcher; + +public class ReturnTypeMatcher implements InvocationMatcher +{ +//---------------------------------------------------------------------------------------------------------------------- +// Fields +//---------------------------------------------------------------------------------------------------------------------- + + private final boolean exactMatch; + private final Class<?> returnType; + +//---------------------------------------------------------------------------------------------------------------------- +// Constructors +//---------------------------------------------------------------------------------------------------------------------- + + public ReturnTypeMatcher(Class<?> returnType) + { + this(returnType, false); + } + + public ReturnTypeMatcher(Class<?> returnType, boolean exactMatch) + { + this.returnType = returnType; + this.exactMatch = exactMatch; + } + +//---------------------------------------------------------------------------------------------------------------------- +// InvocationMatcher Implementation +//---------------------------------------------------------------------------------------------------------------------- + + + @Override + public boolean matches(Invocation invocation) + { + return exactMatch ? + returnType.equals(invocation.getMethod().getReturnType()) : + returnType.isAssignableFrom(invocation.getMethod().getReturnType()); + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestConstantInterceptor.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestConstantInterceptor.java?rev=1507678&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestConstantInterceptor.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestConstantInterceptor.java Sat Jul 27 16:13:50 2013 @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.proxy2.interceptor; + +import org.apache.commons.proxy2.util.AbstractTestCase; +import org.apache.commons.proxy2.util.Echo; +import org.apache.commons.proxy2.util.MockInvocation; + +import java.lang.reflect.Method; + +public class TestConstantInterceptor extends AbstractTestCase +{ +//---------------------------------------------------------------------------------------------------------------------- +// Other Methods +//---------------------------------------------------------------------------------------------------------------------- + + public void testReturnsConstantValue() throws Throwable + { + ConstantInterceptor interceptor = new ConstantInterceptor("foo"); + Method method = Echo.class.getMethod("echo"); + assertEquals("foo", interceptor.intercept(new MockInvocation(method, null))); + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java?rev=1507678&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java Sat Jul 27 16:13:50 2013 @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.proxy2.interceptor; + +import org.apache.commons.proxy2.Invocation; +import org.apache.commons.proxy2.interceptor.matcher.MethodNameMatcher; +import org.apache.commons.proxy2.util.AbstractTestCase; +import org.apache.commons.proxy2.util.Echo; +import org.apache.commons.proxy2.util.MockInvocation; + +import java.lang.reflect.Method; + +public class TestSwitchInterceptor extends AbstractTestCase +{ +//---------------------------------------------------------------------------------------------------------------------- +// Other Methods +//---------------------------------------------------------------------------------------------------------------------- + + public void testWithMultipleAdvices() throws Throwable + { + SwitchInterceptor interceptor = new SwitchInterceptor(); + interceptor.onCase(new MethodNameMatcher("echo"), new ConstantInterceptor("bar")); + interceptor.onCase(new MethodNameMatcher("echoBack"), new ConstantInterceptor("baz")); + Method method = Echo.class.getMethod("echoBack", String.class); + Invocation invocation = new MockInvocation(method, "foo", "foo"); + assertEquals("baz", interceptor.intercept(invocation)); + } + + public void testWithNoAdvice() throws Throwable + { + SwitchInterceptor interceptor = new SwitchInterceptor(); + Method method = Echo.class.getMethod("echoBack", String.class); + Invocation invocation = new MockInvocation(method, "foo", "foo"); + assertEquals("foo", interceptor.intercept(invocation)); + } + + public void testWithSingleAdviceWhichDoesNotMatch() throws Throwable + { + SwitchInterceptor interceptor = new SwitchInterceptor(new MethodNameMatcher("echoBackZZZZ"), new ConstantInterceptor("bar")); + Method method = Echo.class.getMethod("echoBack", String.class); + Invocation invocation = new MockInvocation(method, "foo", "foo"); + assertEquals("foo", interceptor.intercept(invocation)); + } + + public void testWithSingleAdviceWhichMatches() throws Throwable + { + SwitchInterceptor interceptor = new SwitchInterceptor(new MethodNameMatcher("echoBack"), new ConstantInterceptor("bar")); + Method method = Echo.class.getMethod("echoBack", String.class); + Invocation invocation = new MockInvocation(method, "foo", "foo"); + assertEquals("bar", interceptor.intercept(invocation)); + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestMethodNameMatcher.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestMethodNameMatcher.java?rev=1507678&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestMethodNameMatcher.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestMethodNameMatcher.java Sat Jul 27 16:13:50 2013 @@ -0,0 +1,45 @@ +/* + * 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.commons.proxy2.interceptor.matcher; + +import org.apache.commons.proxy2.util.AbstractTestCase; +import org.apache.commons.proxy2.util.Echo; +import org.apache.commons.proxy2.util.MockInvocation; + +import java.lang.reflect.Method; + +public class TestMethodNameMatcher extends AbstractTestCase +{ +//---------------------------------------------------------------------------------------------------------------------- +// Other Methods +//---------------------------------------------------------------------------------------------------------------------- + + public void testWithMatchingMethod() throws Exception + { + MethodNameMatcher matcher = new MethodNameMatcher("echo"); + final Method method = Echo.class.getMethod("echo"); + assertTrue(matcher.matches(new MockInvocation(method,null))); + } + + public void testWithNonMatchingMethod() throws Exception + { + MethodNameMatcher matcher = new MethodNameMatcher("foo"); + final Method method = Echo.class.getMethod("echo"); + assertFalse(matcher.matches(new MockInvocation(method, null))); + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestReturnTypeMatcher.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestReturnTypeMatcher.java?rev=1507678&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestReturnTypeMatcher.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestReturnTypeMatcher.java Sat Jul 27 16:13:50 2013 @@ -0,0 +1,58 @@ +/* + * 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.commons.proxy2.interceptor.matcher; + +import org.apache.commons.proxy2.Invocation; +import org.apache.commons.proxy2.interceptor.InvocationMatcher; +import org.apache.commons.proxy2.util.AbstractTestCase; +import org.apache.commons.proxy2.util.Echo; +import org.apache.commons.proxy2.util.MockInvocation; + +import java.io.Serializable; +import java.lang.reflect.Method; + +public class TestReturnTypeMatcher extends AbstractTestCase +{ +//---------------------------------------------------------------------------------------------------------------------- +// Other Methods +//---------------------------------------------------------------------------------------------------------------------- + + public void testExactMatchNonMatching() throws Throwable + { + Method method = Echo.class.getMethod("echoBack", String.class); + Invocation invocation = new MockInvocation(method, "foo"); + InvocationMatcher matcher = new ReturnTypeMatcher(Serializable.class, true); + assertFalse(matcher.matches(invocation)); + } + + public void testMatchVoid() throws Throwable + { + Method method = Echo.class.getMethod("echo"); + Invocation invocation = new MockInvocation(method, null); + InvocationMatcher matcher = new ReturnTypeMatcher(Void.TYPE); + assertTrue(matcher.matches(invocation)); + } + + public void testWithSupertypeMatch() throws Throwable + { + Method method = Echo.class.getMethod("echoBack", String.class); + Invocation invocation = new MockInvocation(method, "foo"); + InvocationMatcher matcher = new ReturnTypeMatcher(Serializable.class); + assertTrue(matcher.matches(invocation)); + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/util/MockInvocation.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/util/MockInvocation.java?rev=1507678&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/util/MockInvocation.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/util/MockInvocation.java Sat Jul 27 16:13:50 2013 @@ -0,0 +1,56 @@ +package org.apache.commons.proxy2.util; + +import org.apache.commons.proxy2.Invocation; + +import java.lang.reflect.Method; + +public class MockInvocation implements Invocation +{ +//---------------------------------------------------------------------------------------------------------------------- +// Fields +//---------------------------------------------------------------------------------------------------------------------- + + private final Method method; + private final Object[] arguments; + private final Object returnValue; + +//---------------------------------------------------------------------------------------------------------------------- +// Constructors +//---------------------------------------------------------------------------------------------------------------------- + + public MockInvocation(Method method, Object returnValue, Object... arguments) + { + this.returnValue = returnValue; + this.arguments = arguments; + this.method = method; + } + +//---------------------------------------------------------------------------------------------------------------------- +// Invocation Implementation +//---------------------------------------------------------------------------------------------------------------------- + + + @Override + public Object[] getArguments() + { + return arguments; + } + + @Override + public Method getMethod() + { + return method; + } + + @Override + public Object getProxy() + { + throw new UnsupportedOperationException("Proxy objects aren't supported."); + } + + @Override + public Object proceed() throws Throwable + { + return returnValue; + } +}