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