View Javadoc

1   package org.apache.torque.om.mapper;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.mockito.Mockito.verify;
23  import static org.mockito.Mockito.verifyNoMoreInteractions;
24  import static org.mockito.Mockito.when;
25  
26  import java.sql.ResultSet;
27  import java.sql.SQLException;
28  import java.util.Date;
29  
30  import org.apache.torque.BaseTestCase;
31  import org.apache.torque.TorqueException;
32  import org.mockito.Mock;
33  import org.mockito.MockitoAnnotations;
34  
35  public class DateMapperTest extends BaseTestCase
36  {
37      /** Mock result set. */
38      @Mock
39      private ResultSet resultSet;
40  
41      @Override
42      public void setUp() throws Exception
43      {
44          super.setUp();
45          MockitoAnnotations.initMocks(this);
46          when(resultSet.getTimestamp(1)).thenReturn(null);
47          when(resultSet.getTimestamp(2)).thenReturn(
48                  new java.sql.Timestamp(0));
49          when(resultSet.getTimestamp(3)).thenReturn(
50                  new java.sql.Timestamp(123456));
51          when(resultSet.getTimestamp(0)).thenThrow(
52                  new SQLException("index must be >= 1"));
53      }
54  
55      /**
56       * Tests the processRow method for a mapper without internal Offset.
57       *
58       * @throws Exception if the test fails
59       */
60      public void testProcessRow() throws Exception
61      {
62          // prepare
63          DateMapper mapper = new DateMapper();
64  
65          // execute
66          Date result1 = mapper.processRow(resultSet, 0);
67          Date result2 = mapper.processRow(resultSet, 1);
68  
69          // verify
70          assertEquals(null, result1);
71          assertEquals(new Date(0), result2);
72          verify(resultSet).getTimestamp(1);
73          verify(resultSet).getTimestamp(2);
74          verifyNoMoreInteractions(resultSet);
75      }
76  
77      /**
78       * Tests the processRow method for a mapper with internal Offset.
79       *
80       * @throws Exception if the test fails
81       */
82      public void testProcessRowInternalOffset() throws Exception
83      {
84          // prepare
85          DateMapper mapper = new DateMapper(1);
86  
87          // execute
88          Date result1 = mapper.processRow(resultSet, 0);
89          Date result2 = mapper.processRow(resultSet, 1);
90  
91          // verify
92          assertEquals(new Date(0), result1);
93          assertEquals(new Date(123456L), result2);
94          verify(resultSet).getTimestamp(2);
95          verify(resultSet).getTimestamp(3);
96          verifyNoMoreInteractions(resultSet);
97      }
98  
99      /**
100      * Tests the processRow method if the resultSet throws a SqlException.
101      *
102      * @throws Exception if the test fails
103      */
104     public void testProcessRowSqlException() throws Exception
105     {
106         // prepare
107         DateMapper mapper = new DateMapper();
108 
109         // execute
110         try
111         {
112             mapper.processRow(resultSet, -1);
113             // verify 1
114             fail("Exception expected");
115         }
116         catch (TorqueException e)
117         {
118             // verify 2
119             assertEquals(
120                     "Result could not be mapped to a Date",
121                     e.getMessage());
122             assertEquals(SQLException.class, e.getCause().getClass());
123             assertEquals("index must be >= 1", e.getCause().getMessage());
124             verify(resultSet).getTimestamp(0);
125             verifyNoMoreInteractions(resultSet);
126         }
127     }
128 }