You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/10/26 00:58:18 UTC
cvs commit: jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils BaseTestCase.java BasicRowProcessorTest.java TestBean.java
dgraham 2003/10/25 15:58:18
Modified: dbutils/src/java/org/apache/commons/dbutils
BasicRowProcessor.java
dbutils/src/test/org/apache/commons/dbutils
BaseTestCase.java BasicRowProcessorTest.java
TestBean.java
Log:
Fixed BasicRowProcessor.toBean() bug that wouldn't call
a setter with a primitve parameter because the
ResultSet.getObject() method returns primitive wrapper
objects.
Revision Changes Path
1.3 +65 -14 jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/BasicRowProcessor.java
Index: BasicRowProcessor.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/BasicRowProcessor.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BasicRowProcessor.java 22 Oct 2003 04:14:32 -0000 1.2
+++ BasicRowProcessor.java 25 Oct 2003 22:58:18 -0000 1.3
@@ -87,7 +87,7 @@
* @author David Graham
*/
public class BasicRowProcessor implements RowProcessor {
-
+
/**
* Special array index that indicates there is no bean property that
* matches a column from a ResultSet.
@@ -97,13 +97,12 @@
/**
* The Singleton instance of this class.
*/
- private static final BasicRowProcessor instance =
- new BasicRowProcessor();
-
+ private static final BasicRowProcessor instance = new BasicRowProcessor();
+
/**
* Returns the Singleton instance of this class.
*/
- public static BasicRowProcessor instance(){
+ public static BasicRowProcessor instance() {
return instance;
}
@@ -145,7 +144,8 @@
if (columnName.equalsIgnoreCase(pd[j].getName())) {
Object value = rs.getObject(i);
- if (rs.wasNull() && pd[j].getPropertyType().isPrimitive()) {
+ if (rs.wasNull()
+ && pd[j].getPropertyType().isPrimitive()) {
continue;
}
@@ -252,7 +252,7 @@
*/
private void callSetter(PropertyDescriptor pd, Object target, Object value)
throws SQLException {
-
+
Method setter = pd.getWriteMethod();
if (setter == null) {
@@ -262,7 +262,7 @@
Class[] params = setter.getParameterTypes();
try {
// Don't call setter if the value object isn't the right type
- if (value == null || params[0].isInstance(value)) {
+ if (this.isCompatibleType(value, params[0])) {
setter.invoke(target, new Object[] { value });
}
@@ -281,6 +281,55 @@
}
/**
+ * ResultSet.getObject() returns an Integer object for an INT column. The
+ * setter method for the property might take an Integer or a primitive int.
+ * This method returns true if the value can be successfully passed into
+ * the setter method. Remember, Method.invoke() handles the unwrapping
+ * of Integer into an int.
+ *
+ * @param value The value to be passed into the setter method.
+ * @param type The setter's parameter type.
+ */
+ private boolean isCompatibleType(Object value, Class type) {
+ // Do object check first, then primitives
+ if (value == null || type.isInstance(value)) {
+ return true;
+
+ } else if (
+ type.equals(Integer.TYPE) && Integer.class.isInstance(value)) {
+ return true;
+
+ } else if (type.equals(Long.TYPE) && Long.class.isInstance(value)) {
+ return true;
+
+ } else if (
+ type.equals(Double.TYPE) && Double.class.isInstance(value)) {
+ return true;
+
+ } else if (type.equals(Float.TYPE) && Float.class.isInstance(value)) {
+ return true;
+
+ } else if (type.equals(Short.TYPE) && Short.class.isInstance(value)) {
+ return true;
+
+ } else if (type.equals(Byte.TYPE) && Byte.class.isInstance(value)) {
+ return true;
+
+ } else if (
+ type.equals(Character.TYPE) && Character.class.isInstance(value)) {
+ return true;
+
+ } else if (
+ type.equals(Boolean.TYPE) && Boolean.class.isInstance(value)) {
+ return true;
+
+ } else {
+ return false;
+ }
+
+ }
+
+ /**
* Returns a new instance of the given Class.
* @param c The Class to create an object from.
* @return A newly created object of the Class.
@@ -306,19 +355,21 @@
* @return A PropertyDescriptor[] describing the Class.
* @throws SQLException if introspection failed.
*/
- private PropertyDescriptor[] propertyDescriptors(Class c) throws SQLException {
+ private PropertyDescriptor[] propertyDescriptors(Class c)
+ throws SQLException {
// Introspector caches BeanInfo classes for better performance
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(c);
} catch (IntrospectionException e) {
- throw new SQLException("Bean introspection failed: " + e.getMessage());
+ throw new SQLException(
+ "Bean introspection failed: " + e.getMessage());
}
return beanInfo.getPropertyDescriptors();
}
-
+
/**
* A Map that converts all keys to lowercase Strings for case insensitive
* lookups. This is needed for the toMap() implementation because
1.9 +31 -10 jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/BaseTestCase.java
Index: BaseTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/BaseTestCase.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- BaseTestCase.java 25 Oct 2003 22:46:29 -0000 1.8
+++ BaseTestCase.java 25 Oct 2003 22:58:18 -0000 1.9
@@ -87,21 +87,42 @@
public class BaseTestCase extends TestCase {
private static final String[] columnNames =
- new String[] { "one", "two", "three", "notInBean" };
+ new String[] {
+ "one",
+ "two",
+ "three",
+ "notInBean",
+ "intTest",
+ "integerTest" };
/**
* The number of columns in the MockResultSet.
*/
- protected static final int COLS = 4;
+ protected static final int COLS = 6;
protected static final ResultSetMetaData metaData =
MockResultSetMetaData.create(columnNames);
- private static final Object[][] rows =
- new Object[][] { { "1", "2", "3", " notInBean " }, {
- "4", "5", "6", " notInBean " }
- };
-
+ private static final Object[] row1 =
+ new Object[] {
+ "1",
+ "2",
+ "3",
+ " notInBean ",
+ new Integer(1),
+ new Integer(2)};
+
+ private static final Object[] row2 =
+ new Object[] {
+ "4",
+ "5",
+ "6",
+ " notInBean ",
+ new Integer(3),
+ new Integer(4)};
+
+ private static final Object[][] rows = new Object[][] { row1, row2 };
+
/**
* The number of rows in the MockResultSet.
*/
1.4 +7 -3 jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/BasicRowProcessorTest.java
Index: BasicRowProcessorTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/BasicRowProcessorTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- BasicRowProcessorTest.java 25 Oct 2003 22:46:29 -0000 1.3
+++ BasicRowProcessorTest.java 25 Oct 2003 22:58:18 -0000 1.4
@@ -113,6 +113,8 @@
assertEquals("5", b.getTwo());
assertEquals("6", b.getThree());
assertEquals("not set", b.getDoNotSet());
+ assertEquals(3, b.getIntTest());
+ assertEquals(new Integer(4), b.getIntegerTest());
}
public void testToBeanList() throws SQLException {
@@ -127,6 +129,8 @@
assertEquals("5", b.getTwo());
assertEquals("6", b.getThree());
assertEquals("not set", b.getDoNotSet());
+ assertEquals(3, b.getIntTest());
+ assertEquals(new Integer(4), b.getIntegerTest());
}
public void testToMap() throws SQLException {
1.3 +23 -3 jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/TestBean.java
Index: TestBean.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/TestBean.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestBean.java 23 Oct 2003 01:18:55 -0000 1.2
+++ TestBean.java 25 Oct 2003 22:58:18 -0000 1.3
@@ -74,6 +74,10 @@
private String three = null;
+ private int intTest = 0;
+
+ private Integer integerTest = new Integer(0);
+
private String doNotSet = "not set";
/**
@@ -113,6 +117,22 @@
public void setDoNotSet(String string) {
doNotSet = string;
+ }
+
+ public Integer getIntegerTest() {
+ return integerTest;
+ }
+
+ public int getIntTest() {
+ return intTest;
+ }
+
+ public void setIntegerTest(Integer integer) {
+ integerTest = integer;
+ }
+
+ public void setIntTest(int i) {
+ intTest = i;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org