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.Array;
27  import java.util.List;
28  
29  import static org.hamcrest.Matchers.equalTo;
30  import static org.junit.Assert.assertThat;
31  
32  @SuppressWarnings("rawtypes")
33  public class ClassProxyTest {
34      @Test
35      public void testBasicProxy() throws ClassNotFoundException {
36          ClassProxy proxy = ClassProxy.of(String.class);
37          assertThat(proxy.getClassName(), equalTo("java.lang.String"));
38          assertThat(proxy.resolve(), equalTo((Class) String.class));
39      }
40  
41      @Test
42      public void testPrimitiveProxy() throws ClassNotFoundException {
43          ClassProxy proxy = ClassProxy.of(int.class);
44          assertThat(proxy.getClassName(), equalTo("int"));
45          assertThat(proxy.resolve(), equalTo((Class) int.class));
46      }
47  
48      @Test
49      public void testArrayProxy() throws ClassNotFoundException {
50          ClassProxy proxy = ClassProxy.of(String[].class);
51          assertThat(proxy.getClassName(), equalTo("[Ljava.lang.String;"));
52          assertThat(proxy.resolve(),
53                     equalTo((Class) Array.newInstance(String.class, 0).getClass()));
54      }
55  
56      /**
57       * Serialize and deserialize a class proxy.
58       * @param cls The class to serialize
59       * @return A class proxy that is the result of serializing a proxy for {@code cls} and
60       * deserializing it.
61       */
62      private ClassProxy roundTrip(Class<?> cls) throws IOException, ClassNotFoundException {
63          ClassProxy proxy = ClassProxy.of(cls);
64          return SerializationUtils.clone(proxy);
65      }
66  
67      @Test
68      public void testSerializeString() throws ClassNotFoundException, IOException {
69          ClassProxy proxy = roundTrip(String.class);
70          assertThat(proxy.resolve(),
71                     equalTo((Class) String.class));
72      }
73  
74      @Test
75      public void testSerializePrimitive() throws ClassNotFoundException, IOException {
76          ClassProxy proxy = roundTrip(double.class);
77          assertThat(proxy.resolve(),
78                     equalTo((Class) double.class));
79      }
80  
81      @Test
82      public void testSerializeArray() throws ClassNotFoundException, IOException {
83          ClassProxy proxy = roundTrip(String[][].class);
84          assertThat(proxy.resolve(),
85                     equalTo((Class) String[][].class));
86      }
87  
88      @Test
89      public void testSerializePrimitiveArray() throws ClassNotFoundException, IOException {
90          ClassProxy proxy = roundTrip(double[][].class);
91          assertThat(proxy.resolve(),
92                     equalTo((Class) double[][].class));
93      }
94  
95      @Test
96      public void testEquals() {
97          ClassProxy proxy = ClassProxy.of(String.class);
98          assertThat(proxy.equals(null), equalTo(false));
99          assertThat(proxy.equals(proxy), equalTo(true));
100         ClassProxy equal = ClassProxy.of(String.class);
101         ClassProxy unequal = ClassProxy.of(List.class);
102         assertThat(proxy.equals(equal), equalTo(true));
103         assertThat(proxy.equals(unequal), equalTo(false));
104 
105         ClassProxy serialized = SerializationUtils.clone(proxy);
106         assertThat(proxy.equals(serialized), equalTo(true));
107 
108         // and test the hash code
109         assertThat(equal.hashCode(), equalTo(proxy.hashCode()));
110     }
111 }