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>BeanToPropertyValueTransformer</code>.
024: *
025: * @author Norm Deane
026: */
027: public class BeanToPropertyValueTransformerTestCase extends TestCase {
028:
029: private static final Integer expectedIntegerValue = new Integer(123);
030: private static final Long expectedLongValue = new Long(123);
031: private static final Float expectedFloatValue = new Float(123.123f);
032: private static final Double expectedDoubleValue = new Double(
033: 567879.12344d);
034: private static final Boolean expectedBooleanValue = Boolean.TRUE;
035: private static final Byte expectedByteValue = new Byte("12");
036:
037: /**
038: * Constructor for BeanToPropertyValueTransformerTestCase.
039: *
040: * @param name Name of this test case.
041: */
042: public BeanToPropertyValueTransformerTestCase(String name) {
043: super (name);
044: }
045:
046: /**
047: * Test transform with simple String property.
048: */
049: public void testTransformWithSimpleStringProperty() {
050: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
051: "stringProperty");
052: TestBean testBean = new TestBean("foo");
053: assertEquals("foo", transformer.transform(testBean));
054: }
055:
056: /**
057: * Test transform with simple String property and null value.
058: *
059: */
060: public void testTransformWithSimpleStringPropertyAndNullValue() {
061: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
062: "stringProperty");
063: TestBean testBean = new TestBean((String) null);
064: assertNull(transformer.transform(testBean));
065: }
066:
067: /**
068: * Test transform with simple int property.
069: */
070: public void testTransformWithSimpleIntProperty() {
071: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
072: "intProperty");
073: TestBean testBean = new TestBean(expectedIntegerValue
074: .intValue());
075: assertEquals(expectedIntegerValue, transformer
076: .transform(testBean));
077: }
078:
079: /**
080: * Test transform with simple long property.
081: */
082: public void testTransformWithSimpleLongProperty() {
083: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
084: "longProperty");
085: TestBean testBean = new TestBean();
086: testBean.setLongProperty(expectedLongValue.longValue());
087: assertEquals(expectedLongValue, transformer.transform(testBean));
088: }
089:
090: /**
091: * Test transform with simple float property.
092: */
093: public void testTransformWithSimpleFloatProperty() {
094: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
095: "floatProperty");
096: TestBean testBean = new TestBean(expectedFloatValue
097: .floatValue());
098: assertEquals(expectedFloatValue, transformer
099: .transform(testBean));
100: }
101:
102: /**
103: * Test transform with simple double property.
104: */
105: public void testTransformWithSimpleDoubleProperty() {
106: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
107: "doubleProperty");
108: TestBean testBean = new TestBean(expectedDoubleValue
109: .doubleValue());
110: assertEquals(expectedDoubleValue, transformer
111: .transform(testBean));
112: }
113:
114: /**
115: * Test transform with simple byte property.
116: */
117: public void testTransformWithSimpleByteProperty() {
118: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
119: "byteProperty");
120: TestBean testBean = new TestBean();
121: testBean.setByteProperty(expectedByteValue.byteValue());
122: assertEquals(expectedByteValue, transformer.transform(testBean));
123: }
124:
125: /**
126: * Test transform with simple boolean property.
127: */
128: public void testTransformWithSimpleBooleanProperty() {
129: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
130: "booleanProperty");
131: TestBean testBean = new TestBean(expectedBooleanValue
132: .booleanValue());
133: assertEquals(expectedBooleanValue, transformer
134: .transform(testBean));
135: }
136:
137: /**
138: * Test transform with write only property.
139: */
140: public void testTransformWithWriteOnlyProperty() {
141: try {
142: new BeanToPropertyValueTransformer("writeOnlyProperty")
143: .transform(new TestBean());
144: } catch (IllegalArgumentException e) {
145: /* This is what should happen */
146: }
147: }
148:
149: /**
150: * Test transform with read only property.
151: */
152: public void testTransformWithReadOnlyProperty() {
153: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
154: "readOnlyProperty");
155: TestBean testBean = new TestBean();
156: assertEquals(testBean.getReadOnlyProperty(), transformer
157: .transform(testBean));
158: }
159:
160: /**
161: * Test transform with invalid property.
162: */
163: public void testTransformWithInvalidProperty() {
164: try {
165: new BeanToPropertyValueTransformer("bogusProperty")
166: .transform(new TestBean());
167: } catch (IllegalArgumentException e) {
168: /* This is what should happen */
169: }
170: }
171:
172: /**
173: * Test transform with nested property.
174: */
175: public void testTransformWithNestedProperty() {
176: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
177: "anotherNested.stringProperty");
178: TestBean testBean = new TestBean();
179: TestBean nestedBean = new TestBean("foo");
180: testBean.setAnotherNested(nestedBean);
181: assertEquals("foo", transformer.transform(testBean));
182: }
183:
184: /**
185: * Test transform with mapped property.
186: */
187: public void testTransformWithMappedProperty() {
188: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
189: "mappedProperty(test-key)");
190: TestBean testBean = new TestBean();
191:
192: // try a valid key
193: testBean.setMappedProperty("test-key", "test-value");
194: assertEquals("test-value", transformer.transform(testBean));
195:
196: // now try an invalid key
197: transformer = new BeanToPropertyValueTransformer(
198: "mappedProperty(bogus-key)");
199: assertEquals(null, transformer.transform(testBean));
200: }
201:
202: /**
203: * Test transform with indexed property.
204: */
205: public void testTransformWithIndexedProperty() {
206: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
207: "intIndexed[0]");
208: TestBean testBean = new TestBean();
209: testBean.setIntIndexed(0, expectedIntegerValue.intValue());
210: assertEquals(expectedIntegerValue, transformer
211: .transform(testBean));
212:
213: // test index out of range
214: transformer = new BeanToPropertyValueTransformer(
215: "intIndexed[9999]");
216:
217: try {
218: transformer.transform(testBean);
219: fail("Should have thrown an ArrayIndexOutOfBoundsException");
220: } catch (ArrayIndexOutOfBoundsException e) {
221: /* this is what should happen */
222: }
223: }
224:
225: /**
226: * Test transform with nested indexed property.
227: */
228: public void testTransformWithNestedIndexedProperty() {
229: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
230: "anotherNested.intIndexed[0]");
231: TestBean testBean = new TestBean();
232: TestBean nestedBean = new TestBean();
233: nestedBean.setIntIndexed(0, expectedIntegerValue.intValue());
234: testBean.setAnotherNested(nestedBean);
235: assertEquals(expectedIntegerValue, transformer
236: .transform(testBean));
237: }
238:
239: /**
240: * Test transform with null in property path.
241: */
242: public void testTransformWithNullInPath() {
243: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
244: "anotherNested.stringProperty");
245:
246: try {
247: transformer.transform(new TestBean());
248: fail("Should have throw IllegalArgumentException");
249: } catch (IllegalArgumentException e) {
250: /* ignore this is what should happen */
251: }
252: }
253:
254: /**
255: * Test transform with null in property path and ignore = true.
256: */
257: public void testTransformWithNullInPathAndIgnoreTrue() {
258: BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer(
259: "anotherNested.stringProperty", true);
260: assertEquals(null, transformer.transform(new TestBean()));
261: }
262: }
|