1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.dbutils.handlers;
18
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.mockito.Mockito.when;
22
23 import java.sql.ResultSet;
24 import java.sql.ResultSetMetaData;
25 import java.util.Map;
26
27 import org.apache.commons.dbutils.RowProcessor;
28 import org.apache.commons.dbutils.TestBean;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
34
35 @RunWith(MockitoJUnitRunner.class)
36 public class BeanMapHandlerTest {
37
38 private BeanMapHandler<Long, TestBean> bmh;
39 private Map<Long, TestBean> res;
40 @Mock private ResultSet rs;
41 @Mock private ResultSetMetaData rsmd;
42 @Mock private RowProcessor rp;
43
44 private void handle() throws Exception {
45 res = bmh.handle(rs);
46 assertNotNull(res.get(Long.valueOf(23L)));
47 }
48
49 @Before
50 public void setUp() throws Exception {
51 when(Boolean.valueOf(rs.next())).thenReturn(Boolean.TRUE, Boolean.FALSE);
52 when(rs.getObject(1)).thenReturn(Long.valueOf(23L));
53 when(rs.getObject(2)).thenReturn(Long.valueOf(23L));
54 when(rs.getObject("id")).thenReturn(Long.valueOf(23L));
55 when(rs.getMetaData()).thenReturn(rsmd);
56 when(rp.toBean(rs, TestBean.class)).thenReturn(new TestBean());
57 }
58
59 @Test
60 public void testBeanMapHandlerClassOfV() throws Exception {
61 bmh = new BeanMapHandler<>(TestBean.class);
62 handle();
63 }
64
65 @Test
66 public void testBeanMapHandlerClassOfVInt() throws Exception {
67 bmh = new BeanMapHandler<>(TestBean.class, 2);
68 handle();
69 }
70
71 @Test
72 public void testBeanMapHandlerClassOfVRowProcessor() throws Exception {
73 bmh = new BeanMapHandler<>(TestBean.class, rp);
74 handle();
75 }
76
77 @Test
78 public void testBeanMapHandlerClassOfVString() throws Exception {
79 bmh = new BeanMapHandler<>(TestBean.class, "id");
80 handle();
81 }
82
83 @Test
84 public void testEmptyResultSet() throws Exception {
85 when(Boolean.valueOf(rs.next())).thenReturn(Boolean.FALSE);
86 bmh = new BeanMapHandler<>(TestBean.class);
87 res = bmh.handle(rs);
88 assertNull(res.get(Long.valueOf(23L)));
89 }
90
91 }