Skip to content

Commit 5988ff0

Browse files
committed
Moving BeanTransformer to Experimental
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141260 13f79535-47bb-0310-9956-ffa450edef68
1 parent 63c36c4 commit 5988ff0

2 files changed

Lines changed: 173 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2003-2004 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.math.util;
17+
18+
import java.lang.reflect.InvocationTargetException;
19+
import org.apache.commons.math.MathException;
20+
import org.apache.commons.beanutils.PropertyUtils;
21+
22+
/**
23+
* Uses PropertyUtils to map a Bean getter to a double value.
24+
* @version $Revision: 1.1 $ $Date: 2004/06/01 22:10:17 $
25+
*/
26+
public class BeanTransformer implements NumberTransformer {
27+
28+
/**
29+
* The propertyName for this Transformer
30+
*/
31+
private String propertyName;
32+
33+
/**
34+
* Create a BeanTransformer
35+
*/
36+
public BeanTransformer() {
37+
this(null);
38+
}
39+
40+
/**
41+
* Create a BeanTransformer with a specific PropertyName.
42+
* @param property The property.
43+
*/
44+
public BeanTransformer(final String property) {
45+
super();
46+
setPropertyName(property);
47+
}
48+
49+
/**
50+
* Get the property String
51+
* @return the Property Name String
52+
*/
53+
public String getPropertyName() {
54+
return propertyName;
55+
}
56+
57+
/**
58+
* Set the propertyString
59+
* @param string The string to set the property to.
60+
*/
61+
public void setPropertyName(final String string) {
62+
propertyName = string;
63+
}
64+
65+
/**
66+
* @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
67+
*/
68+
public double transform(final Object o) throws MathException {
69+
try {
70+
return ((Number) PropertyUtils.getProperty(o, getPropertyName())).doubleValue();
71+
} catch (IllegalAccessException e) {
72+
throw new MathException("IllegalAccessException in Transformation: " + e.getMessage(), e);
73+
} catch (InvocationTargetException e) {
74+
throw new MathException("InvocationTargetException in Transformation: " + e.getMessage(), e);
75+
} catch (NoSuchMethodException e) {
76+
throw new MathException("oSuchMethodException in Transformation: " + e.getMessage(), e);
77+
}
78+
}
79+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2003-2004 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.commons.math.util;
18+
19+
import org.apache.commons.math.MathException;
20+
import org.apache.commons.math.TestUtils;
21+
22+
import junit.framework.TestCase;
23+
24+
/**
25+
* @version $Revision: 1.1 $ $Date: 2004/06/01 22:10:17 $
26+
*/
27+
public class BeanTransformerTest extends TestCase {
28+
29+
/**
30+
*
31+
*/
32+
public void testConstructor(){
33+
BeanTransformer b = new BeanTransformer();
34+
assertNull(b.getPropertyName());
35+
}
36+
37+
/**
38+
*
39+
*/
40+
public void testConstructorString(){
41+
String name = "property";
42+
BeanTransformer b = new BeanTransformer(name);
43+
assertEquals(name, b.getPropertyName());
44+
}
45+
46+
/**
47+
*
48+
*/
49+
public void testSetPropertyName(){
50+
String name = "property";
51+
BeanTransformer b = new BeanTransformer();
52+
b.setPropertyName(name);
53+
assertEquals(name, b.getPropertyName());
54+
}
55+
56+
/**
57+
*
58+
*/
59+
public void testTransformNoSuchMethod(){
60+
BeanTransformer b = new BeanTransformer("z");
61+
TestBean target = new TestBean();
62+
try {
63+
b.transform(target);
64+
fail("Expecting MathException");
65+
} catch (MathException e) {
66+
// expected
67+
}
68+
}
69+
70+
/**
71+
*
72+
*/
73+
public void testTransform() throws Exception {
74+
BeanTransformer b = new BeanTransformer("x");
75+
TestBean target = new TestBean();
76+
double value = Double.NaN;
77+
value = b.transform(target);
78+
TestUtils.assertEquals(1.0, value, 1.0e-2);
79+
}
80+
81+
/**
82+
*
83+
*/
84+
public void testTransformInvalidType() throws Exception {
85+
BeanTransformer b = new BeanTransformer("y");
86+
TestBean target = new TestBean();
87+
try {
88+
b.transform(target);
89+
fail("Expecting ClassCastException");
90+
} catch(ClassCastException ex){
91+
// success
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)