001package org.apache.commons.beanutils2;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.apache.commons.beanutils2.BeanUtils.on;
023import static org.junit.Assert.assertNotNull;
024import static org.junit.Assert.assertTrue;
025
026import org.apache.commons.beanutils2.testbeans.TestBean;
027import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
028import org.junit.After;
029import org.junit.Before;
030import org.junit.Test;
031
032public final class GetPropertyTestCase
033{
034
035    private TestBean bean;
036    private ThrowingExceptionBean exceptionBean;
037
038    @Before
039    public void setUp()
040    {
041        bean = new TestBean();
042        exceptionBean = new ThrowingExceptionBean();
043    }
044
045    @After
046    public void tearDown()
047    {
048        bean = null;
049        exceptionBean = null;
050    }
051
052    /**
053     * Test getSimpleProperty on a boolean property.
054     */
055    @Test
056    public void getSimpleBoolean()
057        throws Exception
058    {
059        Object value = on( bean ).get( "booleanProperty" ).get();
060        assertNotNull( "Got no value", value );
061        assertTrue( "Got a value of the wrong type", ( value instanceof Boolean ) );
062        assertTrue( "Got an incorrect value", ( (Boolean) value ).booleanValue() == bean.getBooleanProperty() );
063    }
064
065    @Test( expected = PropertyNotReadableException.class )
066    public void getWirteOnlyProperty()
067        throws Exception
068    {
069        on( bean ).get( "writeOnlyProperty" );
070    }
071
072    @Test( expected = NoSuchPropertyException.class )
073    public void getUnknownProperty()
074        throws Exception
075    {
076        on( bean ).get( "unknown" );
077    }
078
079    @Test( expected = NullPointerException.class )
080    public void getNull()
081        throws Exception
082    {
083        on( bean ).get( null );
084    }
085
086    @Test( expected = NoSuchPropertyException.class )
087    public void getPrivateProperty()
088    {
089        on( exceptionBean ).get( "privateProperty" );
090    }
091
092    @Test( expected = NoSuchPropertyException.class )
093    public void getProtectedProperty()
094    {
095        on( exceptionBean ).get( "protectedProperty" );
096    }
097
098    @Test( expected = NoSuchPropertyException.class )
099    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}