001package org.apache.commons.beanutils2; 002 003import static org.apache.commons.beanutils2.BeanUtils.on; 004import static org.junit.Assert.assertEquals; 005import static org.junit.Assert.assertNull; 006 007import org.apache.commons.beanutils2.testbeans.TestBean; 008import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean; 009import org.junit.Before; 010import org.junit.Test; 011 012public class SetMappedPropertyTestCase 013{ 014 015 private TestBean testBean; 016 017 private ThrowingExceptionBean exceptionBean; 018 019 @Before 020 public void setUp() 021 { 022 testBean = new TestBean(); 023 exceptionBean = new ThrowingExceptionBean(); 024 } 025 026 @Test( expected = NullPointerException.class ) 027 public void setMappedNull() 028 { 029 on( testBean ).setMapped( null ); 030 } 031 032 @Test( expected = NoSuchPropertyException.class ) 033 public void setMappedUnknown() 034 { 035 on( testBean ).setMapped( "unknown" ); 036 } 037 038 @Test( expected = IllegalArgumentException.class ) 039 public void getMappedNotMappedProperty() 040 throws Exception 041 { 042 on( testBean ).getMapped( "intProperty" ); 043 } 044 045 @Test( expected = NullPointerException.class ) 046 public void getMappedIntNullKey() 047 throws Exception 048 { 049 on( testBean ).getMapped( "mappedIntProperty" ).of( null ); 050 } 051 052 @Test 053 public void setMappedUnknownKey() 054 throws Exception 055 { 056 assertNull( testBean.getMappedProperty( "unknownKey" ) ); 057 058 String value = "value"; 059 on( testBean ).setMapped( "mappedProperty" ).of( "unknownKey" ).with( value ); 060 assertEquals( value, testBean.getMappedProperty( "unknownKey" ) ); 061 } 062 063 @Test 064 public void setMappedMappedProperty() 065 throws Exception 066 { 067 String value = "value"; 068 on( testBean ).setMapped( "mappedProperty" ).of( "First Key" ).with( value ); 069 assertEquals( value, testBean.getMappedProperty( "First Key" ) ); 070 } 071 072 @Test 073 public void setMappedMappedIntProperty() 074 throws Exception 075 { 076 // FIXME we have to call the getter to initialize the internal map. Change TestBean instead? 077 testBean.getMappedIntProperty( "One" ); 078 on( testBean ).setMapped( "mappedIntProperty" ).of( "One" ).with( Integer.valueOf( 5 ) ); 079 assertEquals( 5, testBean.getMappedIntProperty( "One" ) ); 080 } 081 082 @Test 083 public void setMappedMappedObjects() 084 throws Exception 085 { 086 String value = "Value"; 087 on( testBean ).setMapped( "mappedObjects" ).of( "First Key" ).with( value ); 088 assertEquals( value, testBean.getMappedObjects( "First Key" ) ); 089 } 090 091 @Test( expected = PropertySetterInvocationException.class ) 092 public void setExceptionMapped() 093 { 094 on( exceptionBean ).setMapped( "exceptionMapped" ).of( "A Key" ).with( new RuntimeException() ); 095 } 096 097 @Test( expected = NoSuchPropertyException.class ) 098 public void getPrivateMapped() 099 { 100 on( exceptionBean ).setMapped( "privateMapped" ); 101 } 102 103 @Test( expected = NoSuchPropertyException.class ) 104 public void getProtectedMapped() 105 { 106 on( exceptionBean ).setMapped( "protecedMapped" ); 107 } 108 109 @Test( expected = NoSuchPropertyException.class ) 110 public void getDefaultMapped() 111 { 112 on( exceptionBean ).setMapped( "defaultMapped" ); 113 } 114 115}