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.assertNotNull;
23  
24  import org.apache.commons.beanutils2.testbeans.TestBean;
25  import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  public class GetIndexedPropertyTestCase
31  {
32  
33      private TestBean testBean;
34  
35      private ThrowingExceptionBean exceptionBean;
36  
37      @Before
38      public void setUp()
39      {
40          testBean = new TestBean();
41          exceptionBean = new ThrowingExceptionBean();
42      }
43  
44      @After
45      public void tearDown()
46      {
47          testBean = null;
48          exceptionBean = null;
49      }
50  
51      @Test( expected = NullPointerException.class )
52      public void getIndexedNull()
53      {
54          on( testBean ).getIndexed( null );
55      }
56  
57      @Test( expected = NoSuchPropertyException.class )
58      public void getIndexedUnknown()
59      {
60          on( testBean ).getIndexed( "unknown" );
61      }
62  
63      @Test( expected = IllegalArgumentException.class )
64      public void getIndexedNegative()
65      {
66          on( testBean ).getIndexed( "intIndexed" ).at( -1 );
67      }
68  
69      @Test
70      public void getIndexed()
71      {
72          int expected = testBean.getIntIndexed( 0 );
73          BeanAccessor<?> accessor = on( testBean ).getIndexed( "intIndexed" ).at( 0 );
74          assertNotNull( accessor );
75          assertEquals( expected, accessor.get() );
76      }
77  
78      @Test( expected = PropertyGetterInvocationException.class )
79      public void getExceptionIndexed()
80      {
81          on( exceptionBean ).getIndexed( "exceptionIndexed" ).at( 0 );
82      }
83  
84      @Test( expected = NoSuchPropertyException.class )
85      public void getPrivateIndexed()
86      {
87          on( exceptionBean ).getIndexed( "privateIndexed" );
88      }
89  
90      @Test( expected = NoSuchPropertyException.class )
91      public void getProtectedIndexed()
92      {
93          on( exceptionBean ).getIndexed( "protecedIndexed" );
94      }
95  
96      @Test( expected = NoSuchPropertyException.class )
97      public void getDefaultIndexed()
98      {
99          on( exceptionBean ).getIndexed( "defaultIndexed" );
100     }
101 
102 }