You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ws...@apache.org on 2012/12/14 19:49:50 UTC

svn commit: r1422036 - in /commons/proper/dbutils/trunk/src: main/java/org/apache/commons/dbutils/GenerousBeanProcessor.java test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java

Author: wspeirs
Date: Fri Dec 14 18:49:49 2012
New Revision: 1422036

URL: http://svn.apache.org/viewvc?rev=1422036&view=rev
Log:
- Adding GenerousBeanProcessor and unit test
  - GenerousBeanProcessor will handle column names with underscores, removing them

Added:
    commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/GenerousBeanProcessor.java
    commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java

Added: commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/GenerousBeanProcessor.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/GenerousBeanProcessor.java?rev=1422036&view=auto
==============================================================================
--- commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/GenerousBeanProcessor.java (added)
+++ commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/GenerousBeanProcessor.java Fri Dec 14 18:49:49 2012
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils;
+
+
+import java.beans.PropertyDescriptor;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.Arrays;
+
+
+/**
+ * Provides generous name matching (e.g. underscore-aware) from DB
+ * columns to Java Bean properties.
+ */
+public class GenerousBeanProcessor extends BeanProcessor {
+    
+    /**
+     * Default constructor.
+     */
+    public GenerousBeanProcessor() {
+        super();
+    }
+    
+    @Override
+    protected int[] mapColumnsToProperties(final ResultSetMetaData rsmd,
+                                           final PropertyDescriptor[] props) throws SQLException {
+
+        final int cols = rsmd.getColumnCount();
+        final int[] columnToProperty = new int[cols + 1];
+        Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND);
+
+        for (int col = 1; col <= cols; col++) {
+            String columnName = rsmd.getColumnLabel(col);
+            
+            if (null == columnName || 0 == columnName.length()) {
+                columnName = rsmd.getColumnName(col);
+            }
+            
+            final String generousColumnName = columnName.replace("_","");
+
+            for (int i = 0; i < props.length; i++) {
+                final String propName = props[i].getName();
+                
+                // see if either the column name, or the generous one matches
+                if (columnName.equalsIgnoreCase(propName) ||
+                    generousColumnName.equalsIgnoreCase(propName)) {
+                    columnToProperty[col] = i;
+                    break;
+                }
+            }
+        }
+
+        return columnToProperty;
+    }
+    
+}

Added: commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java?rev=1422036&view=auto
==============================================================================
--- commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java (added)
+++ commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java Fri Dec 14 18:49:49 2012
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.when;
+import java.beans.PropertyDescriptor;
+import java.sql.ResultSetMetaData;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+public class GenerousBeanProcessorTest {
+    
+    GenerousBeanProcessor processor = new GenerousBeanProcessor();
+    @Mock ResultSetMetaData metaData;
+    PropertyDescriptor[] propDescriptors;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        
+        propDescriptors = new PropertyDescriptor[3];
+        
+        propDescriptors[0] = new PropertyDescriptor("one", TestBean.class);
+        propDescriptors[1] = new PropertyDescriptor("two", TestBean.class);
+        propDescriptors[2] = new PropertyDescriptor("three", TestBean.class);
+    }
+
+    @Test
+    public void testMapColumnsToPropertiesWithOutUnderscores() throws Exception {
+        when(metaData.getColumnCount()).thenReturn(3);
+        
+        when(metaData.getColumnLabel(1)).thenReturn("three");
+        when(metaData.getColumnLabel(2)).thenReturn("one");
+        when(metaData.getColumnLabel(3)).thenReturn("two");
+        
+        int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors);
+        
+        assertNotNull(ret);
+        assertEquals(4, ret.length);
+        assertEquals(-1, ret[0]);
+        assertEquals(2, ret[1]);
+        assertEquals(0, ret[2]);
+        assertEquals(1, ret[3]);
+    }
+    
+    @Test
+    public void testMapColumnsToPropertiesWithUnderscores() throws Exception {
+        when(metaData.getColumnCount()).thenReturn(3);
+        
+        when(metaData.getColumnLabel(1)).thenReturn("t_h_r_e_e");
+        when(metaData.getColumnLabel(2)).thenReturn("o_n_e");
+        when(metaData.getColumnLabel(3)).thenReturn("t_w_o");
+        
+        int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors);
+        
+        assertNotNull(ret);
+        assertEquals(4, ret.length);
+        assertEquals(-1, ret[0]);
+        assertEquals(2, ret[1]);
+        assertEquals(0, ret[2]);
+        assertEquals(1, ret[3]);
+    }
+    
+    static class TestBean {
+        private String one;
+        private int two;
+        private long three;
+        
+        public String getOne() {
+            return one;
+        }
+        
+        public void setOne(String one) {
+            this.one = one;
+        }
+        
+        public int getTwo() {
+            return two;
+        }
+        
+        public void setTwo(int two) {
+            this.two = two;
+        }
+        
+        public long getThree() {
+            return three;
+        }
+        
+        public void setThree(long three) {
+            this.three = three;
+        }
+    }
+
+}