001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.proxy;
019    
020    import junit.framework.TestCase;
021    import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory;
022    import org.apache.commons.proxy.util.DuplicateEcho;
023    import org.apache.commons.proxy.util.Echo;
024    import org.apache.commons.proxy.util.EchoImpl;
025    
026    import java.io.Serializable;
027    import java.util.Arrays;
028    import java.util.Properties;
029    
030    public class TestProxyUtils extends TestCase
031    {
032        private Properties prevProperties;
033    
034        protected void setUp() throws Exception
035        {
036            prevProperties = System.getProperties();
037            System.setProperties( new Properties() );
038        }
039    
040        protected void tearDown() throws Exception
041        {
042            System.setProperties( prevProperties );
043        }
044    
045        public void testCreateNullObject() throws Exception
046        {
047            final Echo nullEcho = ( Echo ) ProxyUtils
048                    .createNullObject( new JavassistProxyFactory(), new Class[]{ Echo.class } );
049            assertNull( nullEcho.echoBack( "hello" ) );
050            assertNull( nullEcho.echoBack( "hello", "world" ) );
051            assertEquals( ( int ) 0, nullEcho.echoBack( 12345 ) );
052        }
053    
054        public void testCreateNullObjectWithClassLoader() throws Exception
055        {
056            final Echo nullEcho = ( Echo ) ProxyUtils.createNullObject( new JavassistProxyFactory(),
057                                                                        Echo.class.getClassLoader(),
058                                                                        new Class[]{ Echo.class } );
059            assertNull( nullEcho.echoBack( "hello" ) );
060            assertNull( nullEcho.echoBack( "hello", "world" ) );
061            assertEquals( ( int ) 0, nullEcho.echoBack( 12345 ) );
062        }
063    
064        public void testGetAllInterfaces()
065        {
066            assertNull( ProxyUtils.getAllInterfaces( null ) );
067            assertEquals( Arrays.asList( new Class[] { DuplicateEcho.class, Serializable.class, Echo.class } ), Arrays.asList( ProxyUtils.getAllInterfaces( EchoImpl.class ) ) );
068        }
069    
070        public void testGetJavaClassName() throws Exception
071        {
072            assertEquals( "java.lang.Object[]", ProxyUtils.getJavaClassName( Object[].class ) );
073            assertEquals( "java.lang.Object[][]", ProxyUtils.getJavaClassName( Object[][].class ) );
074            assertEquals( "java.lang.String[][][]", ProxyUtils.getJavaClassName( String[][][].class ) );
075            assertEquals( "int", ProxyUtils.getJavaClassName( Integer.TYPE ) );
076            assertEquals( "float", ProxyUtils.getJavaClassName( Float.TYPE ) );
077            assertEquals( "long", ProxyUtils.getJavaClassName( Long.TYPE ) );
078            assertEquals( "double", ProxyUtils.getJavaClassName( Double.TYPE ) );
079            assertEquals( "short", ProxyUtils.getJavaClassName( Short.TYPE ) );
080            assertEquals( "byte", ProxyUtils.getJavaClassName( Byte.TYPE ) );
081            assertEquals( "char", ProxyUtils.getJavaClassName( Character.TYPE ) );
082            assertEquals( "boolean", ProxyUtils.getJavaClassName( Boolean.TYPE ) );
083        }
084    }