001package org.apache.commons.beanutils2; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static org.apache.commons.beanutils2.Argument.argument; 023import static org.apache.commons.beanutils2.BeanUtils.on; 024import static org.junit.Assert.assertEquals; 025 026import org.apache.commons.beanutils2.testbeans.TestBean; 027import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean; 028import org.junit.After; 029import org.junit.Before; 030import org.junit.Test; 031 032public final class StaticMethodsTestCase 033{ 034 035 private int oldValue; 036 037 @Before 038 public void setUp() 039 { 040 oldValue = TestBean.currentCounter(); 041 } 042 043 @After 044 public void tearDown() 045 { 046 oldValue = 0; 047 } 048 049 @Test 050 public void invokeStaticMethodCurrentCounter() 051 throws Exception 052 { 053 Object value = on( TestBean.class ).invokeStatic( "currentCounter" ).with().get(); 054 assertEquals( "currentCounter value", oldValue, ( (Integer) value ).intValue() ); 055 } 056 057 @Test 058 public void invokeStaticMethodIncrementCounter() 059 throws Exception 060 { 061 on( TestBean.class ).invokeStatic( "incrementCounter" ).with(); 062 assertEquals( oldValue + 1, TestBean.currentCounter() ); 063 } 064 065 @Test 066 public void invokeStaticMethodIncrementCounterIntegerPrimitive() 067 throws Exception 068 { 069 on( TestBean.class ).invokeStatic( "incrementCounter" ).with( argument( 8 ) ).get(); 070 assertEquals( oldValue + 8, TestBean.currentCounter() ); 071 } 072 073 @Test 074 public void invokeStaticMethodIncrementCounterIntegerWrapper() 075 throws Exception 076 { 077 on( TestBean.class ).invokeStatic( "incrementCounter" ).with( argument( new Integer( 8 ) ) ).get(); 078 assertEquals( oldValue + 8, TestBean.currentCounter() ); 079 } 080 081 @Test 082 public void invokeStaticMethodIncrementCounterIntegerWrapperAsPrimitive() 083 throws Exception 084 { 085 on( TestBean.class ).invokeStatic( "incrementCounter" ).with( argument( int.class, 086 new Integer( 8 ) ) ).get(); 087 assertEquals( oldValue + 8, TestBean.currentCounter() ); 088 } 089 090 @Test 091 public void invokeStaticMethodIncrementCounterNumberInteger() 092 throws Exception 093 { 094 on( TestBean.class ).invokeStatic( "incrementCounter" ).with( argument( Number.class, 095 new Integer( 8 ) ) ).get(); 096 // incrementCounter(Number) will multiply its input with 2 097 assertEquals( oldValue + 8 * 2, TestBean.currentCounter() ); 098 } 099 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}