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.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  }