View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy2;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNull;
22  
23  import java.io.Serializable;
24  import java.util.Arrays;
25  import java.util.Properties;
26  
27  import org.apache.commons.proxy2.util.AbstractTestCase;
28  import org.apache.commons.proxy2.util.DuplicateEcho;
29  import org.apache.commons.proxy2.util.Echo;
30  import org.apache.commons.proxy2.util.EchoImpl;
31  import org.junit.After;
32  import org.junit.Before;
33  import org.junit.Test;
34  
35  public class ProxyUtilsTest extends AbstractTestCase
36  {
37      //**********************************************************************************************************************
38      // Fields
39      //**********************************************************************************************************************
40  
41      private Properties prevProperties;
42  
43      //**********************************************************************************************************************
44      // Other Methods
45      //**********************************************************************************************************************
46  
47      @Before
48      public void setUp() throws Exception
49      {
50          prevProperties = System.getProperties();
51          System.setProperties(new Properties());
52      }
53  
54      @After
55      public void tearDown() throws Exception
56      {
57          System.setProperties(prevProperties);
58      }
59  
60      @Test
61      public void testNullValue()
62      {
63          // expecting objects so test against explicit objects rather than using autoboxing
64          assertNullValue(null, String.class);
65          assertNullValue(Character.valueOf((char) 0), Character.TYPE);
66          assertNullValue(Integer.valueOf(0), Integer.TYPE);
67          assertNullValue(Long.valueOf(0), Long.TYPE);
68          assertNullValue(Short.valueOf((short) 0), Short.TYPE);
69          assertNullValue(Double.valueOf(0), Double.TYPE);
70          assertNullValue(Float.valueOf(0), Float.TYPE);
71          assertNullValue(Boolean.FALSE, Boolean.TYPE);
72          assertNullValue(Byte.valueOf((byte) 0), Byte.TYPE);
73      }
74  
75      private void assertNullValue(Object expected, Class<?> type)
76      {
77          assertEquals(expected, ProxyUtils.nullValue(type));
78      }
79  
80      @Test
81      public void testGetAllInterfaces()
82      {
83          assertNull(ProxyUtils.getAllInterfaces(null));
84          assertEquals(Arrays.asList(new Class[] { DuplicateEcho.class, Serializable.class, Echo.class }),
85                  Arrays.asList(ProxyUtils.getAllInterfaces(EchoImpl.class)));
86      }
87  
88      @Test
89      public void testGetJavaClassName() throws Exception
90      {
91          assertEquals("java.lang.Object[]", ProxyUtils.getJavaClassName(Object[].class));
92          assertEquals("java.lang.Object[][]", ProxyUtils.getJavaClassName(Object[][].class));
93          assertEquals("java.lang.String[][][]", ProxyUtils.getJavaClassName(String[][][].class));
94          assertEquals("int", ProxyUtils.getJavaClassName(Integer.TYPE));
95          assertEquals("float", ProxyUtils.getJavaClassName(Float.TYPE));
96          assertEquals("long", ProxyUtils.getJavaClassName(Long.TYPE));
97          assertEquals("double", ProxyUtils.getJavaClassName(Double.TYPE));
98          assertEquals("short", ProxyUtils.getJavaClassName(Short.TYPE));
99          assertEquals("byte", ProxyUtils.getJavaClassName(Byte.TYPE));
100         assertEquals("char", ProxyUtils.getJavaClassName(Character.TYPE));
101         assertEquals("boolean", ProxyUtils.getJavaClassName(Boolean.TYPE));
102     }
103 }