001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils;
019:
020: import junit.framework.TestCase;
021:
022: /**
023: * Test cases for <code>BeanPropertyValueChangeClosure</code>.
024: *
025: * @author Norm Deane
026: */
027: public class BeanPropertyValueChangeClosureTestCase extends TestCase {
028:
029: private static final Integer expectedIntegerValue = new Integer(123);
030: private static final Float expectedFloatValue = new Float(123.123f);
031: private static final Double expectedDoubleValue = new Double(
032: 567879.12344d);
033: private static final Boolean expectedBooleanValue = Boolean.TRUE;
034: private static final Byte expectedByteValue = new Byte("12");
035:
036: /**
037: * Constructor for BeanPropertyValueChangeClosureTest.
038: *
039: * @param name Name of this test case.
040: */
041: public BeanPropertyValueChangeClosureTestCase(String name) {
042: super (name);
043: }
044:
045: /**
046: * Test execute with simple float property and Float value.
047: */
048: public void testExecuteWithSimpleFloatPropertyAndFloatValue() {
049: TestBean testBean = new TestBean();
050: new BeanPropertyValueChangeClosure("floatProperty",
051: expectedFloatValue).execute(testBean);
052: assertTrue(expectedFloatValue.floatValue() == testBean
053: .getFloatProperty());
054: }
055:
056: /**
057: * Test execute with simple float property and String value.
058: */
059: public void testExecuteWithSimpleFloatPropertyAndStringValue() {
060: try {
061: new BeanPropertyValueChangeClosure("floatProperty", "123")
062: .execute(new TestBean());
063: fail("Should have thrown an IllegalArgumentException");
064: } catch (IllegalArgumentException e) {
065: /* this is what we expect */
066: }
067: }
068:
069: /**
070: * Test execute with simple float property and Double value.
071: */
072: public void testExecuteWithSimpleFloatPropertyAndDoubleValue() {
073: try {
074: new BeanPropertyValueChangeClosure("floatProperty",
075: expectedDoubleValue).execute(new TestBean());
076: fail("Should have thrown an IllegalArgumentException");
077: } catch (IllegalArgumentException e) {
078: /* this is what we expect */
079: }
080: }
081:
082: /**
083: * Test execute with simple float property and Integer value.
084: */
085: public void testExecuteWithSimpleFloatPropertyAndIntegerValue() {
086: TestBean testBean = new TestBean();
087: new BeanPropertyValueChangeClosure("floatProperty",
088: expectedIntegerValue).execute(testBean);
089: assertTrue(expectedIntegerValue.floatValue() == testBean
090: .getFloatProperty());
091: }
092:
093: /**
094: * Test execute with simple double property and Double value.
095: */
096: public void testExecuteWithSimpleDoublePropertyAndDoubleValue() {
097: TestBean testBean = new TestBean();
098: new BeanPropertyValueChangeClosure("doubleProperty",
099: expectedDoubleValue).execute(testBean);
100: assertTrue(expectedDoubleValue.doubleValue() == testBean
101: .getDoubleProperty());
102: }
103:
104: /**
105: * Test execute with simple double property and String value.
106: */
107: public void testExecuteWithSimpleDoublePropertyAndStringValue() {
108: try {
109: new BeanPropertyValueChangeClosure("doubleProperty", "123")
110: .execute(new TestBean());
111: fail("Should have thrown an IllegalArgumentException");
112: } catch (IllegalArgumentException e) {
113: /* this is what we expect */
114: }
115: }
116:
117: /**
118: * Test execute with simple double property and Float value.
119: */
120: public void testExecuteWithSimpleDoublePropertyAndFloatValue() {
121: TestBean testBean = new TestBean();
122: new BeanPropertyValueChangeClosure("doubleProperty",
123: expectedFloatValue).execute(testBean);
124: assertTrue(expectedFloatValue.doubleValue() == testBean
125: .getDoubleProperty());
126: }
127:
128: /**
129: * Test execute with simple double property and Integer value.
130: */
131: public void testExecuteWithSimpleDoublePropertyAndIntegerValue() {
132: TestBean testBean = new TestBean();
133: new BeanPropertyValueChangeClosure("doubleProperty",
134: expectedIntegerValue).execute(testBean);
135: assertTrue(expectedIntegerValue.doubleValue() == testBean
136: .getDoubleProperty());
137: }
138:
139: /**
140: * Test execute with simple int property and Double value.
141: */
142: public void testExecuteWithSimpleIntPropertyAndDoubleValue() {
143: try {
144: new BeanPropertyValueChangeClosure("intProperty",
145: expectedDoubleValue).execute(new TestBean());
146: fail("Should have thrown an IllegalArgumentException");
147: } catch (IllegalArgumentException e) {
148: /* this is what we expect */
149: }
150: }
151:
152: /**
153: * Test execute with simple int property and String value.
154: */
155: public void testExecuteWithSimpleIntPropertyAndStringValue() {
156: try {
157: new BeanPropertyValueChangeClosure("intProperty", "123")
158: .execute(new TestBean());
159: fail("Should have thrown an IllegalArgumentException");
160: } catch (IllegalArgumentException e) {
161: /* this is what we expect */
162: }
163: }
164:
165: /**
166: * Test execute with simple int property and Float value.
167: */
168: public void testExecuteWithSimpleIntPropertyAndFloatValue() {
169: try {
170: new BeanPropertyValueChangeClosure("intProperty",
171: expectedFloatValue).execute(new TestBean());
172: fail("Should have thrown an IllegalArgumentException");
173: } catch (IllegalArgumentException e) {
174: /* this is what we expect */
175: }
176: }
177:
178: /**
179: * Test execute with simple int property and Integer value.
180: */
181: public void testExecuteWithSimpleIntPropertyAndIntegerValue() {
182: TestBean testBean = new TestBean();
183: new BeanPropertyValueChangeClosure("intProperty",
184: expectedIntegerValue).execute(testBean);
185: assertTrue(expectedIntegerValue.intValue() == testBean
186: .getIntProperty());
187: }
188:
189: /**
190: * Test execute with simple boolean property and Boolean value.
191: */
192: public void testExecuteWithSimpleBooleanPropertyAndBooleanValue() {
193: TestBean testBean = new TestBean();
194: new BeanPropertyValueChangeClosure("booleanProperty",
195: expectedBooleanValue).execute(testBean);
196: assertTrue(expectedBooleanValue.booleanValue() == testBean
197: .getBooleanProperty());
198: }
199:
200: /**
201: * Test execute with simple boolean property and String value.
202: */
203: public void testExecuteWithSimpleBooleanPropertyAndStringValue() {
204: try {
205: new BeanPropertyValueChangeClosure("booleanProperty",
206: "true").execute(new TestBean());
207: fail("Should have thrown an IllegalArgumentException");
208: } catch (IllegalArgumentException e) {
209: /* this is what we expect */
210: }
211: }
212:
213: /**
214: * Test execute with simple byte property and Byte value.
215: */
216: public void testExecuteWithSimpleBytePropertyAndByteValue() {
217: TestBean testBean = new TestBean();
218: new BeanPropertyValueChangeClosure("byteProperty",
219: expectedByteValue).execute(testBean);
220: assertTrue(expectedByteValue.byteValue() == testBean
221: .getByteProperty());
222: }
223:
224: /**
225: * Test execute with simple boolean property and String value.
226: */
227: public void testExecuteWithSimpleBytePropertyAndStringValue() {
228: try {
229: new BeanPropertyValueChangeClosure("byteProperty", "foo")
230: .execute(new TestBean());
231: fail("Should have thrown an IllegalArgumentException");
232: } catch (IllegalArgumentException e) {
233: /* this is what we expect */
234: }
235: }
236:
237: /**
238: * Test execute with simple primitive property and null value.
239: */
240: public void testExecuteWithSimplePrimitivePropertyAndNullValue() {
241: try {
242: new BeanPropertyValueChangeClosure("intProperty", null)
243: .execute(new TestBean());
244: fail("Should have thrown an IllegalArgumentException");
245: } catch (NullPointerException e) {
246: /* this is what we expect */
247: }
248: }
249:
250: /**
251: * Test execute with read only property.
252: */
253: public void testExecuteWithReadOnlyProperty() {
254: try {
255: new BeanPropertyValueChangeClosure("readOnlyProperty",
256: "foo").execute(new TestBean());
257: fail("Should have thrown an IllegalArgumentException");
258: } catch (IllegalArgumentException e) {
259: /* this is what we expect */
260: }
261: }
262:
263: /**
264: * Test execute with write only property.
265: */
266: public void testExecuteWithWriteOnlyProperty() {
267: TestBean testBean = new TestBean();
268: new BeanPropertyValueChangeClosure("writeOnlyProperty", "foo")
269: .execute(testBean);
270: assertEquals("foo", testBean.getWriteOnlyPropertyValue());
271: }
272:
273: /**
274: * Test execute with a nested property.
275: */
276: public void testExecuteWithNestedProperty() {
277: TestBean testBean = new TestBean();
278: new BeanPropertyValueChangeClosure("nested.stringProperty",
279: "bar").execute(testBean);
280: assertEquals("bar", testBean.getNested().getStringProperty());
281: }
282:
283: /**
284: * Test execute with a nested property and null in the property path.
285: */
286: public void testExecuteWithNullInPropertyPath() {
287: try {
288: new BeanPropertyValueChangeClosure(
289: "anotherNested.stringProperty", "foo")
290: .execute(new TestBean());
291: fail("Should have thrown an IllegalArgumentException");
292: } catch (IllegalArgumentException e) {
293: /* this is what we expect */
294: }
295: }
296:
297: /**
298: * Test execute with a nested property and null in the property path and ignoreNull = true.
299: */
300: public void testExecuteWithNullInPropertyPathAngIgnoreTrue() {
301: TestBean testBean = new TestBean();
302:
303: // create a closure that will attempt to set a property on the null bean in the path
304: BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure(
305: "anotherNested.stringProperty",
306: "Should ignore exception", true);
307:
308: try {
309: closure.execute(testBean);
310: } catch (IllegalArgumentException e) {
311: fail("Should have ignored the exception.");
312: }
313: }
314:
315: /**
316: * Test execute with indexed property.
317: */
318: public void testExecuteWithIndexedProperty() {
319: TestBean testBean = new TestBean();
320: new BeanPropertyValueChangeClosure("intIndexed[0]",
321: expectedIntegerValue).execute(testBean);
322: assertTrue(expectedIntegerValue.intValue() == testBean
323: .getIntIndexed(0));
324: }
325:
326: /**
327: * Test execute with mapped property.
328: */
329: public void testExecuteWithMappedProperty() {
330: TestBean testBean = new TestBean();
331: new BeanPropertyValueChangeClosure("mappedProperty(fred)",
332: "barney").execute(testBean);
333: assertEquals("barney", testBean.getMappedProperty("fred"));
334: }
335:
336: /**
337: * Test execute with a simple String property.
338: */
339: public void testExecuteWithSimpleStringProperty() {
340: TestBean testBean = new TestBean();
341: new BeanPropertyValueChangeClosure("stringProperty", "barney")
342: .execute(testBean);
343: assertEquals("barney", testBean.getStringProperty());
344: }
345:
346: /**
347: * Test execute with an invalid property name.
348: */
349: public void testExecuteWithInvalidPropertyName() {
350: try {
351: new BeanPropertyValueChangeClosure("bogusProperty", "foo")
352: .execute(new TestBean());
353: fail("Should have thrown an IllegalArgumentException");
354: } catch (IllegalArgumentException e) {
355: /* this is what we expect */
356: }
357: }
358: }
|