001package org.apache.commons.beanutils2;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020import static org.apache.commons.beanutils2.BeanUtils.onClassName;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import org.apache.commons.beanutils2.testbeans.TestBean;
025import org.junit.Test;
026
027public class OnClassNameTestCase
028{
029
030    @Test( expected = NullPointerException.class )
031    public void onClassNameNull()
032    {
033        onClassName( null );
034    }
035
036    @Test( expected = BeanClassNotFoundException.class )
037    public void onClassNameUnknown()
038        throws Exception
039    {
040        onClassName( "unknown" ).loadWithBeanUtilsClassLoader();
041    }
042
043    @Test( expected = NullPointerException.class )
044    public void onClassNameTestBeanWithClassLoaderNull()
045        throws Exception
046    {
047        onClassName( TestBean.class.getName() ).loadWith( null );
048    }
049
050    @Test
051    public void onClassNameTestBeanWithThreadContextClassLoader()
052        throws Exception
053    {
054        assertNotNull( onClassName( TestBean.class.getName() ).loadWithThreadContextClassLoader() );
055    }
056
057    @Test
058    public void onClassNameTestBeanWithBeanUtilsClassLoader()
059        throws Exception
060    {
061        assertNotNull( onClassName( TestBean.class.getName() ).loadWithBeanUtilsClassLoader() );
062    }
063
064    @Test
065    public void onClassNameTestBeanWithJUnitClassLoader()
066        throws Exception
067    {
068        assertNotNull( onClassName( TestBean.class.getName() ).loadWith( getClass().getClassLoader() ) );
069    }
070
071    @Test
072    public void correctInstanceThreadContextClassLoader()
073        throws Exception
074    {
075        Object instance =
076            onClassName( TestBean.class.getName() ).loadWithThreadContextClassLoader().newInstance().get();
077        assertTrue( instance instanceof TestBean );
078    }
079
080    @Test
081    public void correctInstanceBeanUtilsClassLoader()
082        throws Exception
083    {
084        Object instance = onClassName( TestBean.class.getName() ).loadWithBeanUtilsClassLoader().newInstance().get();
085        assertTrue( instance instanceof TestBean );
086    }
087
088    @Test
089    public void correctInstanceJUnitClassLoader()
090        throws Exception
091    {
092        Object instance =
093            onClassName( TestBean.class.getName() ).loadWith( getClass().getClassLoader() ).newInstance().get();
094        assertTrue( instance instanceof TestBean );
095    }
096
097}