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
018package org.apache.commons.proxy2;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNull;
022
023import java.io.Serializable;
024import java.util.Arrays;
025import java.util.Properties;
026
027import org.apache.commons.proxy2.util.AbstractTestCase;
028import org.apache.commons.proxy2.util.DuplicateEcho;
029import org.apache.commons.proxy2.util.Echo;
030import org.apache.commons.proxy2.util.EchoImpl;
031import org.junit.After;
032import org.junit.Before;
033import org.junit.Test;
034
035public class ProxyUtilsTest extends AbstractTestCase
036{
037    //**********************************************************************************************************************
038    // Fields
039    //**********************************************************************************************************************
040
041    private Properties prevProperties;
042
043    //**********************************************************************************************************************
044    // Other Methods
045    //**********************************************************************************************************************
046
047    @Before
048    public void setUp() throws Exception
049    {
050        prevProperties = System.getProperties();
051        System.setProperties(new Properties());
052    }
053
054    @After
055    public void tearDown() throws Exception
056    {
057        System.setProperties(prevProperties);
058    }
059
060    @Test
061    public void testNullValue()
062    {
063        // expecting objects so test against explicit objects rather than using autoboxing
064        assertNullValue(null, String.class);
065        assertNullValue(Character.valueOf((char) 0), Character.TYPE);
066        assertNullValue(Integer.valueOf(0), Integer.TYPE);
067        assertNullValue(Long.valueOf(0), Long.TYPE);
068        assertNullValue(Short.valueOf((short) 0), Short.TYPE);
069        assertNullValue(Double.valueOf(0), Double.TYPE);
070        assertNullValue(Float.valueOf(0), Float.TYPE);
071        assertNullValue(Boolean.FALSE, Boolean.TYPE);
072        assertNullValue(Byte.valueOf((byte) 0), Byte.TYPE);
073    }
074
075    private void assertNullValue(Object expected, Class<?> type)
076    {
077        assertEquals(expected, ProxyUtils.nullValue(type));
078    }
079
080    @Test
081    public void testGetAllInterfaces()
082    {
083        assertNull(ProxyUtils.getAllInterfaces(null));
084        assertEquals(Arrays.asList(new Class[] { DuplicateEcho.class, Serializable.class, Echo.class }),
085                Arrays.asList(ProxyUtils.getAllInterfaces(EchoImpl.class)));
086    }
087
088    @Test
089    public void testGetJavaClassName() throws Exception
090    {
091        assertEquals("java.lang.Object[]", ProxyUtils.getJavaClassName(Object[].class));
092        assertEquals("java.lang.Object[][]", ProxyUtils.getJavaClassName(Object[][].class));
093        assertEquals("java.lang.String[][][]", ProxyUtils.getJavaClassName(String[][][].class));
094        assertEquals("int", ProxyUtils.getJavaClassName(Integer.TYPE));
095        assertEquals("float", ProxyUtils.getJavaClassName(Float.TYPE));
096        assertEquals("long", ProxyUtils.getJavaClassName(Long.TYPE));
097        assertEquals("double", ProxyUtils.getJavaClassName(Double.TYPE));
098        assertEquals("short", ProxyUtils.getJavaClassName(Short.TYPE));
099        assertEquals("byte", ProxyUtils.getJavaClassName(Byte.TYPE));
100        assertEquals("char", ProxyUtils.getJavaClassName(Character.TYPE));
101        assertEquals("boolean", ProxyUtils.getJavaClassName(Boolean.TYPE));
102    }
103}