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.assertFalse;
23  import static org.junit.Assert.assertNull;
24  
25  import org.apache.commons.beanutils2.testbeans.TestBean;
26  import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
27  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  public class SetPropertyTestCase
32  {
33  
34      private TestBean testBean;
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
52      public void setSimpleBoolean()
53          throws Exception
54      {
55          on( testBean ).set( "booleanProperty" ).with( false );
56          assertFalse( testBean.getBooleanProperty() );
57      }
58  
59      @Test
60      public void setSimpleByte()
61          throws Exception
62      {
63          byte byteValue = (byte) 36;
64          on( testBean ).set( "byteProperty" ).with( byteValue );
65          assertEquals( byteValue, testBean.getByteProperty() );
66      }
67  
68      // TODO test methods for all setters
69  
70      /**
71       * Test if setting null to an object property works.
72       */
73      @Test
74      public void setPropertyToNull() throws Exception
75      {
76          on(testBean).set( "stringProperty" ).with( null );
77          assertNull( testBean.getStringProperty() );
78      }
79  
80      /**
81       * Tests if trying to set a nonexistent property causes an NPE.
82       */
83      @Test( expected = NoSuchPropertyException.class )
84      public void setNonExistentProperty()
85          throws Exception
86      {
87          on( testBean ).set( "nonExistent" );
88      }
89  
90      /**
91       * Test if trying to set a property with an incompatible value type causes an IllegalArgumentException.
92       */
93      @Test( expected = IllegalArgumentException.class )
94      public void setPropertyWithInCompatibleValue()
95          throws Exception
96      {
97          on( testBean ).set( "booleanProperty" ).with( 'x' );
98      }
99  
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 }