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.on;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNull;
23  
24  import org.apache.commons.beanutils2.testbeans.TestBean;
25  import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  public class GetMappedPropertyTestCase
30  {
31  
32      private TestBean testBean;
33      private ThrowingExceptionBean exceptionBean;
34  
35      @Before
36      public void setUp()
37      {
38          testBean = new TestBean();
39          exceptionBean = new ThrowingExceptionBean();
40      }
41  
42      @Test( expected = NullPointerException.class )
43      public void getMappedNull()
44          throws Exception
45      {
46          on( testBean ).getMapped( null );
47      }
48  
49      @Test( expected = NoSuchPropertyException.class )
50      public void getMappedUnknownProperty()
51          throws Exception
52      {
53          on( testBean ).getMapped( "unknown" );
54      }
55  
56      @Test( expected = IllegalArgumentException.class )
57      public void getMappedNotMappedProperty()
58          throws Exception
59      {
60          on( testBean ).getMapped( "intProperty" );
61      }
62  
63      @Test( expected = NullPointerException.class )
64      public void getMappedIntNullKey()
65          throws Exception
66      {
67          on( testBean ).getMapped( "mappedIntProperty" ).of( null );
68      }
69  
70      @Test
71      public void getMappedUnknownKey()
72          throws Exception
73      {
74          BeanAccessor<?> result = on( testBean ).getMapped( "mappedProperty" ).of( "unknownKey" );
75          assertNull( result.get() );
76      }
77  
78      @Test
79      public void getMappedMappedProperty()
80          throws Exception
81      {
82          BeanAccessor<?> result = on( testBean ).getMapped( "mappedProperty" ).of( "First Key" );
83          assertEquals( "First Value", result.get() );
84      }
85  
86      @Test
87      public void getMappedMappedIntProperty()
88          throws Exception
89      {
90          BeanAccessor<?> result = on( testBean ).getMapped( "mappedIntProperty" ).of( "One" );
91          assertEquals( Integer.valueOf( 1 ), result.get() );
92      }
93  
94      @Test
95      public void getMappedMappedObjects()
96          throws Exception
97      {
98          BeanAccessor<?> result = on( testBean ).getMapped( "mappedObjects" ).of( "First Key" );
99          assertEquals( "First Value", result.get() );
100     }
101 
102     @Test( expected = PropertyGetterInvocationException.class )
103     public void getExceptionMapped()
104     {
105         on( exceptionBean ).getMapped( "exceptionMapped" ).of( "A Key" );
106     }
107 
108     @Test( expected = NoSuchPropertyException.class )
109     public void getPrivateMapped()
110     {
111         on( exceptionBean ).getMapped( "privateMapped" );
112     }
113 
114     @Test( expected = NoSuchPropertyException.class )
115     public void getProtectedMapped()
116     {
117         on( exceptionBean ).getMapped( "protecedMapped" );
118     }
119 
120     @Test( expected = NoSuchPropertyException.class )
121     public void getDefaultMapped()
122     {
123         on( exceptionBean ).getMapped( "defaultMapped" );
124     }
125 }