1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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")
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")
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")
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")
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")
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 }