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.assertNull;
023
024import org.apache.commons.beanutils2.testbeans.TestBean;
025import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
026import org.junit.Before;
027import org.junit.Test;
028
029public class GetMappedPropertyTestCase
030{
031
032    private TestBean testBean;
033    private ThrowingExceptionBean exceptionBean;
034
035    @Before
036    public void setUp()
037    {
038        testBean = new TestBean();
039        exceptionBean = new ThrowingExceptionBean();
040    }
041
042    @Test( expected = NullPointerException.class )
043    public void getMappedNull()
044        throws Exception
045    {
046        on( testBean ).getMapped( null );
047    }
048
049    @Test( expected = NoSuchPropertyException.class )
050    public void getMappedUnknownProperty()
051        throws Exception
052    {
053        on( testBean ).getMapped( "unknown" );
054    }
055
056    @Test( expected = IllegalArgumentException.class )
057    public void getMappedNotMappedProperty()
058        throws Exception
059    {
060        on( testBean ).getMapped( "intProperty" );
061    }
062
063    @Test( expected = NullPointerException.class )
064    public void getMappedIntNullKey()
065        throws Exception
066    {
067        on( testBean ).getMapped( "mappedIntProperty" ).of( null );
068    }
069
070    @Test
071    public void getMappedUnknownKey()
072        throws Exception
073    {
074        BeanAccessor<?> result = on( testBean ).getMapped( "mappedProperty" ).of( "unknownKey" );
075        assertNull( result.get() );
076    }
077
078    @Test
079    public void getMappedMappedProperty()
080        throws Exception
081    {
082        BeanAccessor<?> result = on( testBean ).getMapped( "mappedProperty" ).of( "First Key" );
083        assertEquals( "First Value", result.get() );
084    }
085
086    @Test
087    public void getMappedMappedIntProperty()
088        throws Exception
089    {
090        BeanAccessor<?> result = on( testBean ).getMapped( "mappedIntProperty" ).of( "One" );
091        assertEquals( Integer.valueOf( 1 ), result.get() );
092    }
093
094    @Test
095    public void getMappedMappedObjects()
096        throws Exception
097    {
098        BeanAccessor<?> result = on( testBean ).getMapped( "mappedObjects" ).of( "First Key" );
099        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}