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.on;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNotNull;
023
024import org.apache.commons.beanutils2.testbeans.TestBean;
025import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
026import org.junit.After;
027import org.junit.Before;
028import org.junit.Test;
029
030public class GetIndexedPropertyTestCase
031{
032
033    private TestBean testBean;
034
035    private ThrowingExceptionBean exceptionBean;
036
037    @Before
038    public void setUp()
039    {
040        testBean = new TestBean();
041        exceptionBean = new ThrowingExceptionBean();
042    }
043
044    @After
045    public void tearDown()
046    {
047        testBean = null;
048        exceptionBean = null;
049    }
050
051    @Test( expected = NullPointerException.class )
052    public void getIndexedNull()
053    {
054        on( testBean ).getIndexed( null );
055    }
056
057    @Test( expected = NoSuchPropertyException.class )
058    public void getIndexedUnknown()
059    {
060        on( testBean ).getIndexed( "unknown" );
061    }
062
063    @Test( expected = IllegalArgumentException.class )
064    public void getIndexedNegative()
065    {
066        on( testBean ).getIndexed( "intIndexed" ).at( -1 );
067    }
068
069    @Test
070    public void getIndexed()
071    {
072        int expected = testBean.getIntIndexed( 0 );
073        BeanAccessor<?> accessor = on( testBean ).getIndexed( "intIndexed" ).at( 0 );
074        assertNotNull( accessor );
075        assertEquals( expected, accessor.get() );
076    }
077
078    @Test( expected = PropertyGetterInvocationException.class )
079    public void getExceptionIndexed()
080    {
081        on( exceptionBean ).getIndexed( "exceptionIndexed" ).at( 0 );
082    }
083
084    @Test( expected = NoSuchPropertyException.class )
085    public void getPrivateIndexed()
086    {
087        on( exceptionBean ).getIndexed( "privateIndexed" );
088    }
089
090    @Test( expected = NoSuchPropertyException.class )
091    public void getProtectedIndexed()
092    {
093        on( exceptionBean ).getIndexed( "protecedIndexed" );
094    }
095
096    @Test( expected = NoSuchPropertyException.class )
097    public void getDefaultIndexed()
098    {
099        on( exceptionBean ).getIndexed( "defaultIndexed" );
100    }
101
102}