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.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       * Serialize and deserialize a constructor proxy.
69       * @param fld The constructor to serialize
70       * @return A constructor proxy that is the result of serializing a proxy for {@code fld} and
71       * deserializing it.
72       */
73      private ConstructorProxy roundTrip(Constructor fld) throws IOException, ClassNotFoundException {
74          ConstructorProxy proxy = ConstructorProxy.of(fld);
75          return SerializationUtils.clone(proxy);
76      }
77  }