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.proxy;
19  
20  import junit.framework.TestCase;
21  import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory;
22  import org.apache.commons.proxy.util.DuplicateEcho;
23  import org.apache.commons.proxy.util.Echo;
24  import org.apache.commons.proxy.util.EchoImpl;
25  
26  import java.io.Serializable;
27  import java.util.Arrays;
28  import java.util.Properties;
29  
30  public class TestProxyUtils extends TestCase
31  {
32  //**********************************************************************************************************************
33  // Fields
34  //**********************************************************************************************************************
35  
36      private Properties prevProperties;
37  
38  //**********************************************************************************************************************
39  // Other Methods
40  //**********************************************************************************************************************
41  
42      protected void setUp() throws Exception
43      {
44          prevProperties = System.getProperties();
45          System.setProperties(new Properties());
46      }
47  
48      protected void tearDown() throws Exception
49      {
50          System.setProperties(prevProperties);
51      }
52  
53      public void testCreateNullObject() throws Exception
54      {
55          final Echo nullEcho = ( Echo ) ProxyUtils
56                  .createNullObject(new JavassistProxyFactory(), new Class[] {Echo.class});
57          assertNull(nullEcho.echoBack("hello"));
58          assertNull(nullEcho.echoBack("hello", "world"));
59          assertEquals(( int ) 0, nullEcho.echoBack(12345));
60      }
61  
62      public void testCreateNullObjectWithClassLoader() throws Exception
63      {
64          final Echo nullEcho = ( Echo ) ProxyUtils.createNullObject(new JavassistProxyFactory(),
65                  Echo.class.getClassLoader(),
66                  new Class[] {Echo.class});
67          assertNull(nullEcho.echoBack("hello"));
68          assertNull(nullEcho.echoBack("hello", "world"));
69          assertEquals(( int ) 0, nullEcho.echoBack(12345));
70      }
71  
72      public void testGetAllInterfaces()
73      {
74          assertNull(ProxyUtils.getAllInterfaces(null));
75          assertEquals(Arrays.asList(new Class[] {DuplicateEcho.class, Serializable.class, Echo.class}), Arrays.asList(ProxyUtils.getAllInterfaces(EchoImpl.class)));
76      }
77  
78      public void testGetJavaClassName() throws Exception
79      {
80          assertEquals("java.lang.Object[]", ProxyUtils.getJavaClassName(Object[].class));
81          assertEquals("java.lang.Object[][]", ProxyUtils.getJavaClassName(Object[][].class));
82          assertEquals("java.lang.String[][][]", ProxyUtils.getJavaClassName(String[][][].class));
83          assertEquals("int", ProxyUtils.getJavaClassName(Integer.TYPE));
84          assertEquals("float", ProxyUtils.getJavaClassName(Float.TYPE));
85          assertEquals("long", ProxyUtils.getJavaClassName(Long.TYPE));
86          assertEquals("double", ProxyUtils.getJavaClassName(Double.TYPE));
87          assertEquals("short", ProxyUtils.getJavaClassName(Short.TYPE));
88          assertEquals("byte", ProxyUtils.getJavaClassName(Byte.TYPE));
89          assertEquals("char", ProxyUtils.getJavaClassName(Character.TYPE));
90          assertEquals("boolean", ProxyUtils.getJavaClassName(Boolean.TYPE));
91      }
92  }