View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.apache.commons.beanutils2.Argument.argument;
23  import static org.apache.commons.beanutils2.BeanUtils.on;
24  import static org.junit.Assert.assertEquals;
25  
26  import org.apache.commons.beanutils2.testbeans.TestBean;
27  import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
28  import org.junit.After;
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  public final class StaticMethodsTestCase
33  {
34  
35      private int oldValue;
36  
37      @Before
38      public void setUp()
39      {
40          oldValue = TestBean.currentCounter();
41      }
42  
43      @After
44      public void tearDown()
45      {
46          oldValue = 0;
47      }
48  
49      @Test
50      public void invokeStaticMethodCurrentCounter()
51          throws Exception
52      {
53          Object value = on( TestBean.class ).invokeStatic( "currentCounter" ).with().get();
54          assertEquals( "currentCounter value", oldValue, ( (Integer) value ).intValue() );
55      }
56  
57      @Test
58      public void invokeStaticMethodIncrementCounter()
59          throws Exception
60      {
61          on( TestBean.class ).invokeStatic( "incrementCounter" ).with();
62          assertEquals( oldValue + 1, TestBean.currentCounter() );
63      }
64  
65      @Test
66      public void invokeStaticMethodIncrementCounterIntegerPrimitive()
67          throws Exception
68      {
69          on( TestBean.class ).invokeStatic( "incrementCounter" ).with( argument( 8  ) ).get();
70          assertEquals( oldValue + 8, TestBean.currentCounter() );
71      }
72  
73      @Test
74      public void invokeStaticMethodIncrementCounterIntegerWrapper()
75          throws Exception
76      {
77          on( TestBean.class ).invokeStatic( "incrementCounter" ).with( argument( new Integer( 8 ) ) ).get();
78          assertEquals( oldValue + 8, TestBean.currentCounter() );
79      }
80  
81      @Test
82      public void invokeStaticMethodIncrementCounterIntegerWrapperAsPrimitive()
83          throws Exception
84      {
85          on( TestBean.class ).invokeStatic( "incrementCounter" ).with( argument( int.class,
86                                                                                                 new Integer( 8 ) ) ).get();
87          assertEquals( oldValue + 8, TestBean.currentCounter() );
88      }
89  
90      @Test
91      public void invokeStaticMethodIncrementCounterNumberInteger()
92          throws Exception
93      {
94          on( TestBean.class ).invokeStatic( "incrementCounter" ).with( argument( Number.class,
95                                                                                                 new Integer( 8 ) ) ).get();
96          // incrementCounter(Number) will multiply its input with 2
97          assertEquals( oldValue + 8 * 2, TestBean.currentCounter() );
98      }
99  
100     @Test(expected = NoSuchBeanMethodException.class)
101     public void invokeStaticMethodWithInvalidArgument()
102         throws Exception
103     {
104         String methodName = "incrementCounter";
105         on( TestBean.class ).invokeStatic( methodName ).with( argument( 'x' ) );
106     }
107 
108     @Test
109     public void invokeExactStaticMethodIncrementCounter() throws Exception
110     {
111         on( TestBean.class ).invokeExactStatic( "incrementCounter" ).with();
112         assertEquals( oldValue + 1, TestBean.currentCounter() );
113     }
114 
115     @Test
116     public void invokeExactStaticMethodIncrementIntegerPrimitive()
117         throws Exception
118     {
119         on( TestBean.class ).invokeExactStatic( "incrementCounter" ).with( argument( int.class, 8 ) ).get();
120         assertEquals( oldValue + 8, TestBean.currentCounter() );
121     }
122 
123     @Test( expected = NoSuchBeanMethodException.class )
124     public void invokeExactStaticMethodIncrementIntegerWrapper()
125         throws Exception
126     {
127         on( TestBean.class ).invokeExactStatic( "incrementCounter" ).with( argument( new Integer( 8 ) ) ).get();
128     }
129 
130     @Test
131     public void invokeExactStaticMethodIncrementNumberInteger()
132         throws Exception
133     {
134         on( TestBean.class ).invokeExactStatic( "incrementCounter" ).with( argument( Number.class,
135                                                                                                     new Integer( 8 ) ) ).get();
136         // incrementCounter(Number) will multiply its input with 2
137         assertEquals( oldValue + 2 * 8, TestBean.currentCounter() );
138     }
139 
140     @Test(expected = MethodInvocationException.class)
141     public void invokeStaticExceptionMethod()
142         throws Exception
143     {
144         on( ThrowingExceptionBean.class ).invokeStatic( "staticException" ).with( argument( "Exception" ) );
145     }
146 
147     @Test( expected = MethodInvocationException.class )
148     public void invokeExactStaticExceptionMethod()
149         throws Exception
150     {
151         on( ThrowingExceptionBean.class ).invokeExactStatic( "staticException" ).with( argument( "Exception" ) );
152     }
153 
154 }