View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import static org.apache.commons.beanutils2.BeanUtils.onClassName;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import org.apache.commons.beanutils2.testbeans.TestBean;
25  import org.junit.Test;
26  
27  public class OnClassNameTestCase
28  {
29  
30      @Test( expected = NullPointerException.class )
31      public void onClassNameNull()
32      {
33          onClassName( null );
34      }
35  
36      @Test( expected = BeanClassNotFoundException.class )
37      public void onClassNameUnknown()
38          throws Exception
39      {
40          onClassName( "unknown" ).loadWithBeanUtilsClassLoader();
41      }
42  
43      @Test( expected = NullPointerException.class )
44      public void onClassNameTestBeanWithClassLoaderNull()
45          throws Exception
46      {
47          onClassName( TestBean.class.getName() ).loadWith( null );
48      }
49  
50      @Test
51      public void onClassNameTestBeanWithThreadContextClassLoader()
52          throws Exception
53      {
54          assertNotNull( onClassName( TestBean.class.getName() ).loadWithThreadContextClassLoader() );
55      }
56  
57      @Test
58      public void onClassNameTestBeanWithBeanUtilsClassLoader()
59          throws Exception
60      {
61          assertNotNull( onClassName( TestBean.class.getName() ).loadWithBeanUtilsClassLoader() );
62      }
63  
64      @Test
65      public void onClassNameTestBeanWithJUnitClassLoader()
66          throws Exception
67      {
68          assertNotNull( onClassName( TestBean.class.getName() ).loadWith( getClass().getClassLoader() ) );
69      }
70  
71      @Test
72      public void correctInstanceThreadContextClassLoader()
73          throws Exception
74      {
75          Object instance =
76              onClassName( TestBean.class.getName() ).loadWithThreadContextClassLoader().newInstance().get();
77          assertTrue( instance instanceof TestBean );
78      }
79  
80      @Test
81      public void correctInstanceBeanUtilsClassLoader()
82          throws Exception
83      {
84          Object instance = onClassName( TestBean.class.getName() ).loadWithBeanUtilsClassLoader().newInstance().get();
85          assertTrue( instance instanceof TestBean );
86      }
87  
88      @Test
89      public void correctInstanceJUnitClassLoader()
90          throws Exception
91      {
92          Object instance =
93              onClassName( TestBean.class.getName() ).loadWith( getClass().getClassLoader() ).newInstance().get();
94          assertTrue( instance instanceof TestBean );
95      }
96  
97  }