View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.database.statement;
23  
24  import static org.assertj.core.api.Assertions.assertThat;
25  
26  import java.sql.SQLException;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.LinkedList;
30  import java.util.List;
31  import java.util.Objects;
32  
33  /**
34   * Mock implementation of {@link IBatchStatement} for use in unit tests.
35   * Records all SQL strings added via {@link #addBatch(String)} for later
36   * verification, either via exact-match {@link #verify()} or via
37   * {@link #getCapturedSql()} for pattern-based assertions.
38   *
39   * @author Manuel Laflamme
40   * @version $Revision$
41   * @since Mar 16, 2002
42   */
43  public class MockBatchStatement implements IBatchStatement
44  {
45      private Integer _executeBatchCalls = 0;
46      private Integer _expectedExecuteBatchCalls = 0;
47      private Integer _clearBatchCalls = 0;
48      private Integer _expectedClearBatchCalls = 0;
49      private Integer _closeCalls = 0;
50      private Integer _expectedCloseCalls = 0;
51      private List<String> _batchStrings = new LinkedList<>();
52      private List<String> _actualBatchStrings = new LinkedList<>();
53      private int _addBatchCalls = 0;
54  
55      public MockBatchStatement()
56      {
57      }
58  
59      public void addExpectedBatchString(final String sql)
60      {
61          _batchStrings.add(sql);
62      }
63  
64      public void addExpectedBatchStrings(final String[] sql)
65      {
66          _batchStrings.addAll(Arrays.asList(sql));
67      }
68  
69      public void setExpectedExecuteBatchCalls(final int callsCount)
70      {
71          _expectedExecuteBatchCalls = callsCount;
72      }
73  
74      public void setExpectedClearBatchCalls(final int callsCount)
75      {
76          _expectedClearBatchCalls = callsCount;
77      }
78  
79      public void setExpectedCloseCalls(final int callsCount)
80      {
81          _expectedCloseCalls = callsCount;
82      }
83  
84      /**
85       * Returns the SQL strings that were actually passed to {@link #addBatch(String)},
86       * in the order they were added. Useful for pattern-based assertions when exact
87       * string matching via {@link #addExpectedBatchString(String)} is too strict.
88       *
89       * @return unmodifiable view of the captured SQL strings
90       */
91      public List<String> getCapturedSql()
92      {
93          return Collections.unmodifiableList(_actualBatchStrings);
94      }
95  
96      public void verify()
97      {
98          verify(_executeBatchCalls, _expectedExecuteBatchCalls);
99          verify(_clearBatchCalls, _expectedClearBatchCalls);
100         verify(_closeCalls, _expectedCloseCalls);
101         assertThat(_batchStrings).isEqualTo(_actualBatchStrings);
102     }
103 
104     private void verify(final int count, final int expected)
105     {
106         if (!Objects.isNull(expected))
107         {
108             assertThat(count).isEqualTo(expected);
109         }
110     }
111 
112     ////////////////////////////////////////////////////////////////////////////
113     // IBatchStatement interface
114 
115     @Override
116     public void addBatch(final String sql) throws SQLException
117     {
118         _actualBatchStrings.add(sql);
119         _addBatchCalls++;
120     }
121 
122     @Override
123     public int executeBatch() throws SQLException
124     {
125         _executeBatchCalls++;
126         return _addBatchCalls;
127     }
128 
129     @Override
130     public void clearBatch() throws SQLException
131     {
132         _clearBatchCalls++;
133         _addBatchCalls = 0;
134     }
135 
136     @Override
137     public void close() throws SQLException
138     {
139         _closeCalls++;
140     }
141 }