001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.proxy2.impl; 019 020import static org.junit.Assert.assertArrayEquals; 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertFalse; 023import static org.junit.Assert.assertTrue; 024 025import java.lang.reflect.Method; 026 027import org.apache.commons.lang3.SerializationUtils; 028import org.apache.commons.proxy2.util.AbstractEcho; 029import org.apache.commons.proxy2.util.AbstractTestCase; 030import org.apache.commons.proxy2.util.DuplicateEcho; 031import org.apache.commons.proxy2.util.Echo; 032import org.apache.commons.proxy2.util.EchoImpl; 033import org.junit.Test; 034 035public class MethodSignatureTest extends AbstractTestCase 036{ 037 //********************************************************************************************************************** 038 // Other Methods 039 //********************************************************************************************************************** 040 041 @Test 042 public void testEquals() throws Exception 043 { 044 final MethodSignature sig = new MethodSignature(Echo.class.getMethod("echoBack", String.class)); 045 assertTrue(sig.equals(sig)); 046 assertFalse(sig.equals("echoBack")); 047 assertEquals(sig, new MethodSignature(Echo.class.getMethod("echoBack", String.class))); 048 assertEquals(sig, new MethodSignature(DuplicateEcho.class.getMethod("echoBack", String.class))); 049 assertFalse(sig.equals(new MethodSignature(Echo.class.getMethod("echoBack", String.class, String.class)))); 050 assertFalse(sig.equals(new MethodSignature(Echo.class.getMethod("echo")))); 051 } 052 053 @Test 054 public void testSerialization() throws Exception 055 { 056 final MethodSignature sig = new MethodSignature(Echo.class.getMethod("echoBack", String.class)); 057 assertEquals(sig, SerializationUtils.clone(sig)); 058 } 059 060 @Test 061 public void testToString() throws Exception 062 { 063 assertEquals("echo()", new MethodSignature(Echo.class.getMethod("echo")).toString()); 064 assertEquals("echoBack(Ljava/lang/String;)", 065 new MethodSignature(Echo.class.getMethod("echoBack", String.class)).toString()); 066 assertEquals("echoBack([Ljava/lang/String;)", 067 new MethodSignature(Echo.class.getMethod("echoBack", String[].class)).toString()); 068 assertEquals("echoBack([[Ljava/lang/String;)", 069 new MethodSignature(Echo.class.getMethod("echoBack", String[][].class)).toString()); 070 assertEquals("echoBack([[[Ljava/lang/String;)", 071 new MethodSignature(Echo.class.getMethod("echoBack", String[][][].class)).toString()); 072 assertEquals("echoBack(I)", new MethodSignature(Echo.class.getMethod("echoBack", int.class)).toString()); 073 assertEquals("echoBack(Z)", new MethodSignature(Echo.class.getMethod("echoBack", boolean.class)).toString()); 074 assertEquals("echoBack(Ljava/lang/String;Ljava/lang/String;)", 075 new MethodSignature(Echo.class.getMethod("echoBack", String.class, String.class)).toString()); 076 assertEquals("illegalArgument()", new MethodSignature(Echo.class.getMethod("illegalArgument")).toString()); 077 assertEquals("ioException()", new MethodSignature(Echo.class.getMethod("ioException")).toString()); 078 } 079 080 @Test 081 public void testToMethod() throws Exception 082 { 083 final MethodSignature sig = new MethodSignature(Echo.class.getMethod("echoBack", String.class)); 084 085 assertMethodIs(sig.toMethod(Echo.class), Echo.class, "echoBack", String.class); 086 assertMethodIs(sig.toMethod(AbstractEcho.class), AbstractEcho.class, "echoBack", String.class); 087 assertMethodIs(sig.toMethod(EchoImpl.class), AbstractEcho.class, "echoBack", String.class); 088 assertMethodIs(sig.toMethod(DuplicateEcho.class), DuplicateEcho.class, "echoBack", String.class); 089 } 090 091 private void assertMethodIs(Method method, Class<?> declaredBy, String name, Class<?>... parameterTypes) 092 { 093 assertEquals(declaredBy, method.getDeclaringClass()); 094 assertEquals(name, method.getName()); 095 assertArrayEquals(parameterTypes, method.getParameterTypes()); 096 } 097}