View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.apache.commons.beanutils2.BeanUtils.on;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import org.apache.commons.beanutils2.testbeans.TestBean;
27  import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
28  import org.junit.After;
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  public final class GetPropertyTestCase
33  {
34  
35      private TestBean bean;
36      private ThrowingExceptionBean exceptionBean;
37  
38      @Before
39      public void setUp()
40      {
41          bean = new TestBean();
42          exceptionBean = new ThrowingExceptionBean();
43      }
44  
45      @After
46      public void tearDown()
47      {
48          bean = null;
49          exceptionBean = null;
50      }
51  
52      /**
53       * Test getSimpleProperty on a boolean property.
54       */
55      @Test
56      public void getSimpleBoolean()
57          throws Exception
58      {
59          Object value = on( bean ).get( "booleanProperty" ).get();
60          assertNotNull( "Got no value", value );
61          assertTrue( "Got a value of the wrong type", ( value instanceof Boolean ) );
62          assertTrue( "Got an incorrect value", ( (Boolean) value ).booleanValue() == bean.getBooleanProperty() );
63      }
64  
65      @Test( expected = PropertyNotReadableException.class )
66      public void getWirteOnlyProperty()
67          throws Exception
68      {
69          on( bean ).get( "writeOnlyProperty" );
70      }
71  
72      @Test( expected = NoSuchPropertyException.class )
73      public void getUnknownProperty()
74          throws Exception
75      {
76          on( bean ).get( "unknown" );
77      }
78  
79      @Test( expected = NullPointerException.class )
80      public void getNull()
81          throws Exception
82      {
83          on( bean ).get( null );
84      }
85  
86      @Test( expected = NoSuchPropertyException.class )
87      public void getPrivateProperty()
88      {
89          on( exceptionBean ).get( "privateProperty" );
90      }
91  
92      @Test( expected = NoSuchPropertyException.class )
93      public void getProtectedProperty()
94      {
95          on( exceptionBean ).get( "protectedProperty" );
96      }
97  
98      @Test( expected = NoSuchPropertyException.class )
99      public void getDefaultProperty()
100     {
101         on( exceptionBean ).get( "defaultProperty" );
102     }
103 
104     @Test( expected = PropertyGetterInvocationException.class )
105     public void getExcpetionProperty()
106     {
107         on( exceptionBean ).get( "exceptionProperty" );
108     }
109 
110 }