View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy2.impl;
19  
20  import static org.junit.Assert.assertArrayEquals;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.lang.reflect.Method;
26  
27  import org.apache.commons.lang3.SerializationUtils;
28  import org.apache.commons.proxy2.util.AbstractEcho;
29  import org.apache.commons.proxy2.util.AbstractTestCase;
30  import org.apache.commons.proxy2.util.DuplicateEcho;
31  import org.apache.commons.proxy2.util.Echo;
32  import org.apache.commons.proxy2.util.EchoImpl;
33  import org.junit.Test;
34  
35  public class MethodSignatureTest extends AbstractTestCase
36  {
37      //**********************************************************************************************************************
38      // Other Methods
39      //**********************************************************************************************************************
40  
41      @Test
42      public void testEquals() throws Exception
43      {
44          final MethodSignature sig = new MethodSignature(Echo.class.getMethod("echoBack", String.class));
45          assertTrue(sig.equals(sig));
46          assertFalse(sig.equals("echoBack"));
47          assertEquals(sig, new MethodSignature(Echo.class.getMethod("echoBack", String.class)));
48          assertEquals(sig, new MethodSignature(DuplicateEcho.class.getMethod("echoBack", String.class)));
49          assertFalse(sig.equals(new MethodSignature(Echo.class.getMethod("echoBack", String.class, String.class))));
50          assertFalse(sig.equals(new MethodSignature(Echo.class.getMethod("echo"))));
51      }
52  
53      @Test
54      public void testSerialization() throws Exception
55      {
56          final MethodSignature sig = new MethodSignature(Echo.class.getMethod("echoBack", String.class));
57          assertEquals(sig, SerializationUtils.clone(sig));
58      }
59  
60      @Test
61      public void testToString() throws Exception
62      {
63          assertEquals("echo()", new MethodSignature(Echo.class.getMethod("echo")).toString());
64          assertEquals("echoBack(Ljava/lang/String;)",
65                  new MethodSignature(Echo.class.getMethod("echoBack", String.class)).toString());
66          assertEquals("echoBack([Ljava/lang/String;)",
67                  new MethodSignature(Echo.class.getMethod("echoBack", String[].class)).toString());
68          assertEquals("echoBack([[Ljava/lang/String;)",
69                  new MethodSignature(Echo.class.getMethod("echoBack", String[][].class)).toString());
70          assertEquals("echoBack([[[Ljava/lang/String;)",
71                  new MethodSignature(Echo.class.getMethod("echoBack", String[][][].class)).toString());
72          assertEquals("echoBack(I)", new MethodSignature(Echo.class.getMethod("echoBack", int.class)).toString());
73          assertEquals("echoBack(Z)", new MethodSignature(Echo.class.getMethod("echoBack", boolean.class)).toString());
74          assertEquals("echoBack(Ljava/lang/String;Ljava/lang/String;)",
75                  new MethodSignature(Echo.class.getMethod("echoBack", String.class, String.class)).toString());
76          assertEquals("illegalArgument()", new MethodSignature(Echo.class.getMethod("illegalArgument")).toString());
77          assertEquals("ioException()", new MethodSignature(Echo.class.getMethod("ioException")).toString());
78      }
79  
80      @Test
81      public void testToMethod() throws Exception
82      {
83          final MethodSignature sig = new MethodSignature(Echo.class.getMethod("echoBack", String.class));
84  
85          assertMethodIs(sig.toMethod(Echo.class), Echo.class, "echoBack", String.class);
86          assertMethodIs(sig.toMethod(AbstractEcho.class), AbstractEcho.class, "echoBack", String.class);
87          assertMethodIs(sig.toMethod(EchoImpl.class), AbstractEcho.class, "echoBack", String.class);
88          assertMethodIs(sig.toMethod(DuplicateEcho.class), DuplicateEcho.class, "echoBack", String.class);
89      }
90  
91      private void assertMethodIs(Method method, Class<?> declaredBy, String name, Class<?>... parameterTypes)
92      {
93          assertEquals(declaredBy, method.getDeclaringClass());
94          assertEquals(name, method.getName());
95          assertArrayEquals(parameterTypes, method.getParameterTypes());
96      }
97  }