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.Constructor;
27 import java.util.List;
28
29 import static org.hamcrest.Matchers.equalTo;
30 import static org.junit.Assert.assertThat;
31
32 public class ConstructorProxyTest {
33 private static class TestClass {
34 public TestClass() {}
35 public TestClass(String foo) {}
36 public TestClass(List<String> foos, boolean frob) {}
37 }
38
39 @Test
40 public void testBasicConstructor() throws NoSuchMethodException, ClassNotFoundException {
41 Constructor f = TestClass.class.getDeclaredConstructor();
42 ConstructorProxy proxy = ConstructorProxy.of(f);
43 assertThat(proxy.resolve(), equalTo(f));
44 }
45
46 @Test
47 public void testSerializeConstructor() throws NoSuchMethodException, ClassNotFoundException, IOException {
48 Constructor f = TestClass.class.getDeclaredConstructor();
49 ConstructorProxy proxy = roundTrip(f);
50 assertThat(proxy.resolve(), equalTo(f));
51 }
52
53 @Test
54 public void testSerializeParamConstructor() throws NoSuchMethodException, ClassNotFoundException, IOException {
55 Constructor f = TestClass.class.getDeclaredConstructor(String.class);
56 ConstructorProxy proxy = roundTrip(f);
57 assertThat(proxy.resolve(), equalTo(f));
58 }
59
60 @Test
61 public void testSerializeTwoArgConstructor() throws NoSuchMethodException, IOException, ClassNotFoundException {
62 Constructor f = TestClass.class.getDeclaredConstructor(List.class, boolean.class);
63 ConstructorProxy proxy = roundTrip(f);
64 assertThat(proxy.resolve(), equalTo(f));
65 }
66
67
68
69
70
71
72
73 private ConstructorProxy roundTrip(Constructor fld) throws IOException, ClassNotFoundException {
74 ConstructorProxy proxy = ConstructorProxy.of(fld);
75 return SerializationUtils.clone(proxy);
76 }
77 }