1 package org.apache.commons.beanutils2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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 }