1 package org.apache.commons.beanutils2;
2
3 import static org.apache.commons.beanutils2.BeanUtils.on;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNull;
6
7 import org.apache.commons.beanutils2.testbeans.TestBean;
8 import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
9 import org.junit.Before;
10 import org.junit.Test;
11
12 public class SetMappedPropertyTestCase
13 {
14
15 private TestBean testBean;
16
17 private ThrowingExceptionBean exceptionBean;
18
19 @Before
20 public void setUp()
21 {
22 testBean = new TestBean();
23 exceptionBean = new ThrowingExceptionBean();
24 }
25
26 @Test( expected = NullPointerException.class )
27 public void setMappedNull()
28 {
29 on( testBean ).setMapped( null );
30 }
31
32 @Test( expected = NoSuchPropertyException.class )
33 public void setMappedUnknown()
34 {
35 on( testBean ).setMapped( "unknown" );
36 }
37
38 @Test( expected = IllegalArgumentException.class )
39 public void getMappedNotMappedProperty()
40 throws Exception
41 {
42 on( testBean ).getMapped( "intProperty" );
43 }
44
45 @Test( expected = NullPointerException.class )
46 public void getMappedIntNullKey()
47 throws Exception
48 {
49 on( testBean ).getMapped( "mappedIntProperty" ).of( null );
50 }
51
52 @Test
53 public void setMappedUnknownKey()
54 throws Exception
55 {
56 assertNull( testBean.getMappedProperty( "unknownKey" ) );
57
58 String value = "value";
59 on( testBean ).setMapped( "mappedProperty" ).of( "unknownKey" ).with( value );
60 assertEquals( value, testBean.getMappedProperty( "unknownKey" ) );
61 }
62
63 @Test
64 public void setMappedMappedProperty()
65 throws Exception
66 {
67 String value = "value";
68 on( testBean ).setMapped( "mappedProperty" ).of( "First Key" ).with( value );
69 assertEquals( value, testBean.getMappedProperty( "First Key" ) );
70 }
71
72 @Test
73 public void setMappedMappedIntProperty()
74 throws Exception
75 {
76
77 testBean.getMappedIntProperty( "One" );
78 on( testBean ).setMapped( "mappedIntProperty" ).of( "One" ).with( Integer.valueOf( 5 ) );
79 assertEquals( 5, testBean.getMappedIntProperty( "One" ) );
80 }
81
82 @Test
83 public void setMappedMappedObjects()
84 throws Exception
85 {
86 String value = "Value";
87 on( testBean ).setMapped( "mappedObjects" ).of( "First Key" ).with( value );
88 assertEquals( value, testBean.getMappedObjects( "First Key" ) );
89 }
90
91 @Test( expected = PropertySetterInvocationException.class )
92 public void setExceptionMapped()
93 {
94 on( exceptionBean ).setMapped( "exceptionMapped" ).of( "A Key" ).with( new RuntimeException() );
95 }
96
97 @Test( expected = NoSuchPropertyException.class )
98 public void getPrivateMapped()
99 {
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 }