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.assertFalse;
023import static org.junit.Assert.assertNull;
024
025import org.apache.commons.beanutils2.testbeans.TestBean;
026import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
027import org.junit.After;
028import org.junit.Before;
029import org.junit.Test;
030
031public class SetPropertyTestCase
032{
033
034    private TestBean testBean;
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
052    public void setSimpleBoolean()
053        throws Exception
054    {
055        on( testBean ).set( "booleanProperty" ).with( false );
056        assertFalse( testBean.getBooleanProperty() );
057    }
058
059    @Test
060    public void setSimpleByte()
061        throws Exception
062    {
063        byte byteValue = (byte) 36;
064        on( testBean ).set( "byteProperty" ).with( byteValue );
065        assertEquals( byteValue, testBean.getByteProperty() );
066    }
067
068    // TODO test methods for all setters
069
070    /**
071     * Test if setting null to an object property works.
072     */
073    @Test
074    public void setPropertyToNull() throws Exception
075    {
076        on(testBean).set( "stringProperty" ).with( null );
077        assertNull( testBean.getStringProperty() );
078    }
079
080    /**
081     * Tests if trying to set a nonexistent property causes an NPE.
082     */
083    @Test( expected = NoSuchPropertyException.class )
084    public void setNonExistentProperty()
085        throws Exception
086    {
087        on( testBean ).set( "nonExistent" );
088    }
089
090    /**
091     * Test if trying to set a property with an incompatible value type causes an IllegalArgumentException.
092     */
093    @Test( expected = IllegalArgumentException.class )
094    public void setPropertyWithInCompatibleValue()
095        throws Exception
096    {
097        on( testBean ).set( "booleanProperty" ).with( 'x' );
098    }
099
100    /**
101     * Tests if setting null to a primitive property causes an IllegalArgumentException.
102     */
103    @Test( expected = IllegalArgumentException.class )
104    public void setNullToPrimitiveProperty()
105        throws Exception
106    {
107        on( testBean ).set( "booleanProperty" ).with( null );
108    }
109
110    /**
111     * Test if trying to set a read only property causes a NoSuchMethodException.
112     */
113    @Test( expected = PropertyNotWritableException.class )
114    public void setReadOnlyProperty()
115        throws Exception
116    {
117        on( testBean ).set( "readOnlyProperty" );
118    }
119
120    /**
121     * Test if passing null to set causes an NPE
122     */
123    @Test( expected = NullPointerException.class )
124    public void setNull()
125        throws Exception
126    {
127        on( testBean ).set( null );
128    }
129
130    @Test( expected = NoSuchPropertyException.class )
131    public void setPrivateProperty()
132    {
133        on( exceptionBean ).set( "privateProperty" );
134    }
135
136    @Test( expected = NoSuchPropertyException.class )
137    public void setProtectedProperty()
138    {
139        on( exceptionBean ).set( "protectedProperty" );
140    }
141
142    @Test( expected = NoSuchPropertyException.class )
143    public void setDefaultProperty()
144    {
145        on( exceptionBean ).set( "defaultProperty" );
146    }
147
148    @Test( expected = PropertySetterInvocationException.class )
149    public void setExcpetionProperty()
150    {
151        on( exceptionBean ).set( "exceptionProperty" ).with( "Throw Exception!" );
152    }
153
154}