View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.dbutils;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.mockito.Mockito.when;
22  
23  import java.beans.PropertyDescriptor;
24  import java.sql.ResultSetMetaData;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  import org.junit.runner.RunWith;
29  import org.mockito.Mock;
30  import org.mockito.junit.MockitoJUnitRunner;
31  
32  @RunWith(MockitoJUnitRunner.class)
33  public class GenerousBeanProcessorTest {
34  
35      static class TestBean {
36          private String one;
37          private int two;
38          private long three;
39  
40          public String getOne() {
41              return one;
42          }
43  
44          public long getThree() {
45              return three;
46          }
47  
48          public int getTwo() {
49              return two;
50          }
51  
52          public void setOne(final String one) {
53              this.one = one;
54          }
55  
56          public void setThree(final long three) {
57              this.three = three;
58          }
59  
60          public void setTwo(final int two) {
61              this.two = two;
62          }
63      }
64      GenerousBeanProcessor processor = new GenerousBeanProcessor();
65      @Mock ResultSetMetaData metaData;
66  
67      PropertyDescriptor[] propDescriptors;
68  
69      @Before
70      public void setUp() throws Exception {
71          propDescriptors = new PropertyDescriptor[3];
72  
73          propDescriptors[0] = new PropertyDescriptor("one", TestBean.class);
74          propDescriptors[1] = new PropertyDescriptor("two", TestBean.class);
75          propDescriptors[2] = new PropertyDescriptor("three", TestBean.class);
76      }
77  
78      @SuppressWarnings("boxing") // test code
79      @Test
80      public void testMapColumnsToPropertiesColumnLabelIsNull() throws Exception {
81          when(metaData.getColumnCount()).thenReturn(1);
82          when(metaData.getColumnName(1)).thenReturn("juhu");
83          when(metaData.getColumnLabel(1)).thenReturn(null);
84  
85          final int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors);
86  
87          assertNotNull(ret);
88          assertEquals(2, ret.length);
89          assertEquals(-1, ret[0]);
90          assertEquals(-1, ret[1]);
91      }
92  
93      @SuppressWarnings("boxing") // test code
94      @Test
95      public void testMapColumnsToPropertiesMixedCase() throws Exception {
96          when(metaData.getColumnCount()).thenReturn(3);
97  
98          when(metaData.getColumnLabel(1)).thenReturn("tHree");
99          when(metaData.getColumnLabel(2)).thenReturn("One");
100         when(metaData.getColumnLabel(3)).thenReturn("tWO");
101 
102         final int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors);
103 
104         assertNotNull(ret);
105         assertEquals(4, ret.length);
106         assertEquals(-1, ret[0]);
107         assertEquals(2, ret[1]);
108         assertEquals(0, ret[2]);
109         assertEquals(1, ret[3]);
110     }
111 
112     @SuppressWarnings("boxing") // test code
113     @Test
114     public void testMapColumnsToPropertiesWithOutUnderscores() throws Exception {
115         when(metaData.getColumnCount()).thenReturn(3);
116 
117         when(metaData.getColumnLabel(1)).thenReturn("three");
118         when(metaData.getColumnLabel(2)).thenReturn("one");
119         when(metaData.getColumnLabel(3)).thenReturn("two");
120 
121         final int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors);
122 
123         assertNotNull(ret);
124         assertEquals(4, ret.length);
125         assertEquals(-1, ret[0]);
126         assertEquals(2, ret[1]);
127         assertEquals(0, ret[2]);
128         assertEquals(1, ret[3]);
129     }
130 
131     @SuppressWarnings("boxing") // test code
132     @Test
133     public void testMapColumnsToPropertiesWithSpaces() throws Exception {
134         when(metaData.getColumnCount()).thenReturn(3);
135 
136         when(metaData.getColumnLabel(1)).thenReturn("th ree");
137         when(metaData.getColumnLabel(2)).thenReturn("o n e");
138         when(metaData.getColumnLabel(3)).thenReturn("t wo");
139 
140         final int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors);
141 
142         assertNotNull(ret);
143         assertEquals(4, ret.length);
144         assertEquals(-1, ret[0]);
145         assertEquals(2, ret[1]);
146         assertEquals(0, ret[2]);
147         assertEquals(1, ret[3]);
148     }
149 
150     @SuppressWarnings("boxing") // test code
151     @Test
152     public void testMapColumnsToPropertiesWithUnderscores() throws Exception {
153         when(metaData.getColumnCount()).thenReturn(3);
154 
155         when(metaData.getColumnLabel(1)).thenReturn("t_h_r_e_e");
156         when(metaData.getColumnLabel(2)).thenReturn("o_n_e");
157         when(metaData.getColumnLabel(3)).thenReturn("t_w_o");
158 
159         final int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors);
160 
161         assertNotNull(ret);
162         assertEquals(4, ret.length);
163         assertEquals(-1, ret[0]);
164         assertEquals(2, ret[1]);
165         assertEquals(0, ret[2]);
166         assertEquals(1, ret[3]);
167     }
168 
169 }