View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht.util;
21  
22  import org.apache.commons.lang3.SerializationUtils;
23  import org.junit.Test;
24  
25  import java.io.IOException;
26  import java.lang.reflect.Method;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import static org.hamcrest.Matchers.equalTo;
31  import static org.junit.Assert.assertThat;
32  
33  public class MethodProxyTest {
34      private static class TestClass {
35          String foo(String name) { return "Hello, " + name; }
36          public int bar() { return -1; }
37          public void wombat(List<String> foo, String... args) {
38              Collections.addAll(foo, args);
39          }
40      }
41  
42      @Test
43      public void testBasicMethod() throws NoSuchMethodException, ClassNotFoundException {
44          Method f = TestClass.class.getDeclaredMethod("foo", String.class);
45          MethodProxy proxy = MethodProxy.of(f);
46          assertThat(proxy.resolve(), equalTo(f));
47      }
48  
49      @Test
50      public void testSerializeMethod() throws NoSuchMethodException, ClassNotFoundException, IOException {
51          Method f = TestClass.class.getDeclaredMethod("foo", String.class);
52          MethodProxy proxy = roundTrip(f);
53          assertThat(proxy.resolve(), equalTo(f));
54      }
55  
56      @Test
57      public void testSerializeNullaryMethod() throws NoSuchMethodException, ClassNotFoundException, IOException {
58          Method f = TestClass.class.getDeclaredMethod("bar");
59          MethodProxy proxy = roundTrip(f);
60          assertThat(proxy.resolve(), equalTo(f));
61      }
62  
63      @Test
64      public void testSerializeVariadicMethod() throws NoSuchMethodException, IOException, ClassNotFoundException {
65          Method f = TestClass.class.getDeclaredMethod("wombat", List.class, String[].class);
66          MethodProxy proxy = roundTrip(f);
67          assertThat(proxy.resolve(), equalTo(f));
68      }
69  
70      /**
71       * Serialize and deserialize a method proxy.
72       * @param fld The method to serialize
73       * @return A method proxy that is the result of serializing a proxy for {@code fld} and
74       * deserializing it.
75       */
76      private MethodProxy roundTrip(Method fld) throws IOException, ClassNotFoundException {
77          MethodProxy proxy = MethodProxy.of(fld);
78          return SerializationUtils.clone(proxy);
79      }
80  }