1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
72
73
74
75
76 private MethodProxy roundTrip(Method fld) throws IOException, ClassNotFoundException {
77 MethodProxy proxy = MethodProxy.of(fld);
78 return SerializationUtils.clone(proxy);
79 }
80 }