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 import static org.apache.commons.beanutils2.BeanUtils.on;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNull;
24
25 import org.apache.commons.beanutils2.testbeans.TestBean;
26 import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 public class SetPropertyTestCase
32 {
33
34 private TestBean testBean;
35 private ThrowingExceptionBean exceptionBean;
36
37 @Before
38 public void setUp()
39 {
40 testBean = new TestBean();
41 exceptionBean = new ThrowingExceptionBean();
42 }
43
44 @After
45 public void tearDown()
46 {
47 testBean = null;
48 exceptionBean = null;
49 }
50
51 @Test
52 public void setSimpleBoolean()
53 throws Exception
54 {
55 on( testBean ).set( "booleanProperty" ).with( false );
56 assertFalse( testBean.getBooleanProperty() );
57 }
58
59 @Test
60 public void setSimpleByte()
61 throws Exception
62 {
63 byte byteValue = (byte) 36;
64 on( testBean ).set( "byteProperty" ).with( byteValue );
65 assertEquals( byteValue, testBean.getByteProperty() );
66 }
67
68
69
70
71
72
73 @Test
74 public void setPropertyToNull() throws Exception
75 {
76 on(testBean).set( "stringProperty" ).with( null );
77 assertNull( testBean.getStringProperty() );
78 }
79
80
81
82
83 @Test( expected = NoSuchPropertyException.class )
84 public void setNonExistentProperty()
85 throws Exception
86 {
87 on( testBean ).set( "nonExistent" );
88 }
89
90
91
92
93 @Test( expected = IllegalArgumentException.class )
94 public void setPropertyWithInCompatibleValue()
95 throws Exception
96 {
97 on( testBean ).set( "booleanProperty" ).with( 'x' );
98 }
99
100
101
102
103 @Test( expected = IllegalArgumentException.class )
104 public void setNullToPrimitiveProperty()
105 throws Exception
106 {
107 on( testBean ).set( "booleanProperty" ).with( null );
108 }
109
110
111
112
113 @Test( expected = PropertyNotWritableException.class )
114 public void setReadOnlyProperty()
115 throws Exception
116 {
117 on( testBean ).set( "readOnlyProperty" );
118 }
119
120
121
122
123 @Test( expected = NullPointerException.class )
124 public void setNull()
125 throws Exception
126 {
127 on( testBean ).set( null );
128 }
129
130 @Test( expected = NoSuchPropertyException.class )
131 public void setPrivateProperty()
132 {
133 on( exceptionBean ).set( "privateProperty" );
134 }
135
136 @Test( expected = NoSuchPropertyException.class )
137 public void setProtectedProperty()
138 {
139 on( exceptionBean ).set( "protectedProperty" );
140 }
141
142 @Test( expected = NoSuchPropertyException.class )
143 public void setDefaultProperty()
144 {
145 on( exceptionBean ).set( "defaultProperty" );
146 }
147
148 @Test( expected = PropertySetterInvocationException.class )
149 public void setExcpetionProperty()
150 {
151 on( exceptionBean ).set( "exceptionProperty" ).with( "Throw Exception!" );
152 }
153
154 }