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.Argument.argument;
21  import static org.apache.commons.beanutils2.BeanUtils.on;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  import org.apache.commons.beanutils2.testbeans.TestBean;
28  import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  /**
34   * Test case for invoking methods on instances.
35   */
36  public class MethodsTestCase
37  {
38  
39      private TestBean testBean;
40      private ThrowingExceptionBean exceptionBean;
41  
42      @Before
43      public void setUp()
44      {
45          testBean = new TestBean();
46          exceptionBean = new ThrowingExceptionBean();
47      }
48  
49      @After
50      public void tearDown()
51      {
52          testBean = null;
53          exceptionBean = null;
54      }
55  
56      @Test
57      public void invokeMethodGetBooleanProperty()
58          throws Exception
59      {
60          Object value = on( testBean ).invoke( "getBooleanProperty" ).with().get();
61          assertNotNull( value );
62          assertTrue( value instanceof Boolean );
63          assertEquals( testBean.getBooleanProperty(), ( (Boolean) value ).booleanValue() );
64      }
65  
66      @Test
67      public void invokeMethodSetBooleanProperty()
68          throws Exception
69      {
70          on( testBean ).invoke( "setBooleanProperty" ).with( argument( false ) );
71          assertFalse( testBean.getBooleanProperty() );
72      }
73  
74      @Test
75      public void invokeMethodSetBooleanPropertyWithWrapper()
76          throws Exception
77      {
78          on( testBean ).invoke( "setBooleanProperty" ).with( argument( new Boolean( false ) ) );
79          assertFalse( testBean.getBooleanProperty() );
80      }
81  
82      @Test
83      public void invokeMethodGetIntProperty()
84          throws Exception
85      {
86          Object value = on( testBean ).invoke( "getIntProperty" ).with().get();
87          assertNotNull( value );
88          assertTrue( value instanceof Integer );
89          assertEquals( testBean.getIntProperty(), ( (Integer) value ).intValue() );
90      }
91  
92      @Test
93      public void invokeMethodSetIntProperty()
94          throws Exception
95      {
96          on( testBean ).invoke( "setIntProperty" ).with( argument( 47 ) );
97          assertEquals( testBean.getIntProperty(), 47 );
98      }
99  
100     @Test
101     public void invokeMethodSetIntPropertyWithWrapper()
102         throws Exception
103     {
104         on( testBean ).invoke( "setIntProperty" ).with( argument( new Integer( 47 ) ) );
105         assertEquals( testBean.getIntProperty(), 47 );
106     }
107 
108     @Test( expected = NullPointerException.class )
109     public void invokeMethodNull()
110         throws Exception
111     {
112         on( testBean ).invoke( null );
113     }
114 
115     @Test( expected = NoSuchBeanMethodException.class )
116     public void invokeMethodNonExistent()
117         throws Exception
118     {
119         on( testBean ).invoke( "nonExistent" ).with();
120     }
121 
122     @Test
123     public void invokeExactGetBooleanProperty()
124         throws Exception
125     {
126         Object value = on( testBean ).invokeExact( "getBooleanProperty" ).with().get();
127         assertNotNull( value );
128         assertTrue( value instanceof Boolean );
129         assertTrue( testBean.getBooleanProperty() == (Boolean) value );
130     }
131 
132     @Test
133     public void invokeExactMethodSetBooleanProperty()
134         throws Exception
135     {
136         on( testBean ).invokeExact( "setBooleanProperty" ).with( argument( boolean.class, false ) );
137         assertFalse( testBean.getBooleanProperty() );
138     }
139 
140     @Test( expected = NoSuchBeanMethodException.class )
141     public void invokeExactMethodSetBooleanPropertyWithWrapper()
142         throws Exception
143     {
144         on( testBean ).invokeExact( "setBooleanProperty" ).with( argument( new Boolean( false ) ) );
145     }
146 
147     @Test
148     public void invokeExactMethodSetBooleanPropertyWithWrapperAsPrimitive()
149         throws Exception
150     {
151         on( testBean ).invokeExact( "setBooleanProperty" ).with( argument( boolean.class,
152                                                                                           new Boolean( false ) ) );
153         assertFalse( testBean.getBooleanProperty() );
154     }
155 
156     @Test
157     public void invokeExactGetIntProperty()
158         throws Exception
159     {
160         Object value = on( testBean ).invokeExact( "getIntProperty" ).with().get();
161         assertNotNull( value );
162         assertTrue( value instanceof Integer );
163         assertEquals( testBean.getIntProperty(), ( (Integer) value ).intValue() );
164     }
165 
166     @Test
167     public void invokeExactMethodSetIntProperty()
168         throws Exception
169     {
170         on( testBean ).invokeExact( "setIntProperty" ).with( argument( int.class, 47 ) );
171         assertEquals( testBean.getIntProperty(), 47 );
172     }
173 
174     @Test( expected = NoSuchBeanMethodException.class )
175     public void invokeExactMethodSetIntPropertyWithWrapper()
176         throws Exception
177     {
178         on( testBean ).invokeExact( "setIntProperty" ).with( argument( new Integer( 47 ) ) );
179     }
180 
181     @Test
182     public void invokeExactMethodSetIntPropertyWithWrapperAsPrimitive()
183         throws Exception
184     {
185         on( testBean ).invokeExact( "setIntProperty" ).with( argument( int.class, new Integer( 47 ) ) );
186         assertEquals( testBean.getIntProperty(), 47 );
187     }
188 
189     @Test( expected = NullPointerException.class )
190     public void invokeExactMethodNull()
191         throws Exception
192     {
193         on( testBean ).invokeExact( null );
194     }
195 
196     @Test( expected = NoSuchBeanMethodException.class )
197     public void invokeExactMethodNonExistent()
198         throws Exception
199     {
200         on( testBean ).invokeExact( "nonExistent" ).with();
201     }
202 
203     @Test(expected = MethodInvocationException.class)
204     public void invokeExceptionMethod()
205         throws Exception
206     {
207         on( exceptionBean ).invoke( "setExceptionProperty" ).with( argument( "Exception" ) );
208     }
209 
210     @Test( expected = MethodInvocationException.class )
211     public void invokeExactExceptionMethod()
212         throws Exception
213     {
214         on( exceptionBean ).invokeExact( "setExceptionProperty" ).with( argument( "Exception" ) );
215     }
216 
217 }