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      private Properties prevProperties;
33  
34      protected void setUp() throws Exception
35      {
36          prevProperties = System.getProperties();
37          System.setProperties( new Properties() );
38      }
39  
40      protected void tearDown() throws Exception
41      {
42          System.setProperties( prevProperties );
43      }
44  
45      public void testCreateNullObject() throws Exception
46      {
47          final Echo nullEcho = ( Echo ) ProxyUtils
48                  .createNullObject( new JavassistProxyFactory(), new Class[]{ Echo.class } );
49          assertNull( nullEcho.echoBack( "hello" ) );
50          assertNull( nullEcho.echoBack( "hello", "world" ) );
51          assertEquals( ( int ) 0, nullEcho.echoBack( 12345 ) );
52      }
53  
54      public void testCreateNullObjectWithClassLoader() throws Exception
55      {
56          final Echo nullEcho = ( Echo ) ProxyUtils.createNullObject( new JavassistProxyFactory(),
57                                                                      Echo.class.getClassLoader(),
58                                                                      new Class[]{ Echo.class } );
59          assertNull( nullEcho.echoBack( "hello" ) );
60          assertNull( nullEcho.echoBack( "hello", "world" ) );
61          assertEquals( ( int ) 0, nullEcho.echoBack( 12345 ) );
62      }
63  
64      public void testGetAllInterfaces()
65      {
66          assertNull( ProxyUtils.getAllInterfaces( null ) );
67          assertEquals( Arrays.asList( new Class[] { DuplicateEcho.class, Serializable.class, Echo.class } ), Arrays.asList( ProxyUtils.getAllInterfaces( EchoImpl.class ) ) );
68      }
69  
70      public void testGetJavaClassName() throws Exception
71      {
72          assertEquals( "java.lang.Object[]", ProxyUtils.getJavaClassName( Object[].class ) );
73          assertEquals( "java.lang.Object[][]", ProxyUtils.getJavaClassName( Object[][].class ) );
74          assertEquals( "java.lang.String[][][]", ProxyUtils.getJavaClassName( String[][][].class ) );
75          assertEquals( "int", ProxyUtils.getJavaClassName( Integer.TYPE ) );
76          assertEquals( "float", ProxyUtils.getJavaClassName( Float.TYPE ) );
77          assertEquals( "long", ProxyUtils.getJavaClassName( Long.TYPE ) );
78          assertEquals( "double", ProxyUtils.getJavaClassName( Double.TYPE ) );
79          assertEquals( "short", ProxyUtils.getJavaClassName( Short.TYPE ) );
80          assertEquals( "byte", ProxyUtils.getJavaClassName( Byte.TYPE ) );
81          assertEquals( "char", ProxyUtils.getJavaClassName( Character.TYPE ) );
82          assertEquals( "boolean", ProxyUtils.getJavaClassName( Boolean.TYPE ) );
83      }
84  }