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.*;
26 import java.lang.reflect.Field;
27
28 import static org.hamcrest.Matchers.equalTo;
29 import static org.junit.Assert.assertThat;
30
31 public class FieldProxyTest {
32 private static class TestClass {
33 String foo;
34 int bar;
35 }
36
37 @Test
38 public void testBasicField() throws NoSuchFieldException, ClassNotFoundException {
39 Field f = TestClass.class.getDeclaredField("foo");
40 FieldProxy proxy = FieldProxy.of(f);
41 assertThat(proxy.resolve(), equalTo(f));
42 }
43
44 @Test
45 public void testSerializeField() throws NoSuchFieldException, ClassNotFoundException, IOException {
46 Field f = TestClass.class.getDeclaredField("foo");
47 FieldProxy proxy = roundTrip(f);
48 assertThat(proxy.resolve(), equalTo(f));
49 }
50
51 @Test
52 public void testSerializePrimitiveField() throws NoSuchFieldException, ClassNotFoundException, IOException {
53 Field f = TestClass.class.getDeclaredField("bar");
54 FieldProxy proxy = roundTrip(f);
55 assertThat(proxy.resolve(), equalTo(f));
56 }
57
58
59
60
61
62
63
64 private FieldProxy roundTrip(Field fld) throws IOException, ClassNotFoundException {
65 FieldProxy proxy = FieldProxy.of(fld);
66 return SerializationUtils.clone(proxy);
67 }
68 }