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  
18  package org.apache.commons.dbcp2;
19  
20  import java.sql.Connection;
21  import java.sql.ResultSet;
22  import java.sql.SQLException;
23  import java.sql.SQLWarning;
24  import java.sql.Statement;
25  
26  /**
27   * A dummy {@link Statement}, for testing purposes.
28   */
29  public class TesterStatement extends AbandonedTrace implements Statement {
30  
31      protected final Connection connection;
32      protected boolean open = true;
33      protected final long rowsUpdated = 1;
34      protected final boolean executeResponse = true;
35      protected int maxFieldSize = 1024;
36      protected long maxRows = 1024;
37      protected boolean escapeProcessing;
38      protected int queryTimeout = 1000;
39      protected String cursorName;
40      protected int fetchDirection = 1;
41      protected int fetchSize = 1;
42      protected int resultSetConcurrency = 1;
43      protected int resultSetType = 1;
44      private int resultSetHoldability = 1;
45      protected ResultSet resultSet;
46      protected boolean sqlExceptionOnClose;
47  
48      public TesterStatement(final Connection connection) {
49          this.connection = connection;
50      }
51  
52      public TesterStatement(final Connection connection, final int resultSetType, final int resultSetConcurrency) {
53          this.connection = connection;
54          this.resultSetType = resultSetType;
55          this.resultSetConcurrency = resultSetConcurrency;
56      }
57  
58      public TesterStatement(final Connection connection, final int resultSetType, final int resultSetConcurrency,
59              final int resultSetHoldability) {
60          this.connection = connection;
61          this.resultSetType = resultSetType;
62          this.resultSetConcurrency = resultSetConcurrency;
63          this.resultSetHoldability = resultSetHoldability;
64      }
65  
66      @Override
67      public void addBatch(final String sql) throws SQLException {
68          checkOpen();
69      }
70  
71      @Override
72      public void cancel() throws SQLException {
73          checkOpen();
74      }
75  
76      protected void checkOpen() throws SQLException {
77          if (!open) {
78              throw new SQLException("Connection is closed.");
79          }
80      }
81  
82      @Override
83      public void clearBatch() throws SQLException {
84          checkOpen();
85      }
86  
87      @Override
88      public void clearWarnings() throws SQLException {
89          checkOpen();
90      }
91  
92      @Override
93      public void close() throws SQLException {
94          if (sqlExceptionOnClose) {
95              throw new SQLException("TestSQLExceptionOnClose");
96          }
97  
98          // calling close twice has no effect
99          if (!open) {
100             return;
101         }
102 
103         open = false;
104         if (resultSet != null) {
105             resultSet.close();
106             resultSet = null;
107         }
108     }
109 
110     @Override
111     public void closeOnCompletion() throws SQLException {
112         throw new SQLException("Not implemented.");
113     }
114 
115     @Override
116     public boolean execute(final String sql) throws SQLException {
117         checkOpen();
118         if ("invalid".equals(sql)) {
119             throw new SQLException("invalid query");
120         }
121         return executeResponse;
122     }
123 
124     @Override
125     public boolean execute(final String sql, final int autoGeneratedKeys)
126         throws SQLException {
127         throw new SQLException("Not implemented.");
128     }
129 
130     @Override
131     public boolean execute(final String sql, final int[] columnIndexes)
132         throws SQLException {
133         throw new SQLException("Not implemented.");
134     }
135 
136     @Override
137     public boolean execute(final String sql, final String[] columnNames)
138         throws SQLException {
139         throw new SQLException("Not implemented.");
140     }
141 
142     @Override
143     public int[] executeBatch() throws SQLException {
144         checkOpen();
145         return new int[0];
146     }
147 
148     @Override
149     public long[] executeLargeBatch() throws SQLException {
150         checkOpen();
151         return new long[0];
152     }
153 
154     @Override
155     public long executeLargeUpdate(final String sql) throws SQLException {
156         checkOpen();
157         return rowsUpdated;
158     }
159 
160     @Override
161     public long executeLargeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
162         throw new SQLException("Not implemented.");
163     }
164 
165     @Override
166     public long executeLargeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
167         throw new SQLException("Not implemented.");
168     }
169 
170     @Override
171     public long executeLargeUpdate(final String sql, final String[] columnNames) throws SQLException {
172         throw new SQLException("Not implemented.");
173     }
174 
175     @Override
176     public ResultSet executeQuery(final String sql) throws SQLException {
177         checkOpen();
178         switch (sql) {
179         case "null":
180             return null;
181         case "invalid":
182             throw new SQLException("invalid query");
183         case "broken":
184             throw new SQLException("broken connection");
185         case "select username": {
186             final String userName = ((TesterConnection) connection).getUserName();
187             final Object[][] data = { { userName } };
188             return new TesterResultSet(this, data);
189         }
190         default:
191             break;
192         }
193         // Simulate timeout if queryTimout is set to less than 5 seconds
194         if (queryTimeout > 0 && queryTimeout < 5) {
195             throw new SQLException("query timeout");
196         }
197         return new TesterResultSet(this);
198     }
199 
200     @Override
201     public int executeUpdate(final String sql) throws SQLException {
202         checkOpen();
203         return (int) rowsUpdated;
204     }
205 
206     @Override
207     public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
208         throw new SQLException("Not implemented.");
209     }
210 
211     @Override
212     public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
213         throw new SQLException("Not implemented.");
214     }
215 
216     @Override
217     public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
218         throw new SQLException("Not implemented.");
219     }
220 
221     @Override
222     public Connection getConnection() throws SQLException {
223         checkOpen();
224         return connection;
225     }
226 
227     @Override
228     public int getFetchDirection() throws SQLException {
229         checkOpen();
230         return fetchDirection;
231     }
232 
233     @Override
234     public int getFetchSize() throws SQLException {
235         checkOpen();
236         return fetchSize;
237     }
238 
239     @Override
240     public ResultSet getGeneratedKeys() throws SQLException {
241         return new TesterResultSet(this);
242     }
243 
244     @Override
245     public long getLargeMaxRows() throws SQLException {
246         checkOpen();
247         return maxRows;
248     }
249 
250     @Override
251     public long getLargeUpdateCount() throws SQLException {
252         checkOpen();
253         return rowsUpdated;
254     }
255 
256     @Override
257     public int getMaxFieldSize() throws SQLException {
258         checkOpen();
259         return maxFieldSize;
260     }
261 
262     @Override
263     public int getMaxRows() throws SQLException {
264         checkOpen();
265         return (int) maxRows;
266     }
267 
268     @Override
269     public boolean getMoreResults() throws SQLException {
270         checkOpen();
271         return false;
272     }
273 
274     @Override
275     public boolean getMoreResults(final int current) throws SQLException {
276         throw new SQLException("Not implemented.");
277     }
278 
279     @Override
280     public int getQueryTimeout() throws SQLException {
281         checkOpen();
282         return queryTimeout;
283     }
284 
285     @Override
286     public ResultSet getResultSet() throws SQLException {
287         checkOpen();
288         if (resultSet == null) {
289             resultSet = new TesterResultSet(this);
290         }
291         return resultSet;
292     }
293 
294     @Override
295     public int getResultSetConcurrency() throws SQLException {
296         checkOpen();
297         return resultSetConcurrency;
298     }
299 
300     @Override
301     public int getResultSetHoldability() throws SQLException {
302         checkOpen();
303         return resultSetHoldability;
304     }
305 
306     @Override
307     public int getResultSetType() throws SQLException {
308         checkOpen();
309         return resultSetType;
310     }
311 
312     @Override
313     public int getUpdateCount() throws SQLException {
314         checkOpen();
315         return (int) rowsUpdated;
316     }
317 
318     @Override
319     public SQLWarning getWarnings() throws SQLException {
320         checkOpen();
321         return null;
322     }
323 
324     @Override
325     public boolean isClosed() throws SQLException {
326         return !open;
327     }
328 
329     @Override
330     public boolean isCloseOnCompletion() throws SQLException {
331         throw new SQLException("Not implemented.");
332     }
333 
334     @Override
335     public boolean isPoolable() throws SQLException {
336         throw new SQLException("Not implemented.");
337     }
338 
339     public boolean isSqlExceptionOnClose() {
340         return sqlExceptionOnClose;
341     }
342 
343     @Override
344     public boolean isWrapperFor(final Class<?> iface) throws SQLException {
345         throw new SQLException("Not implemented.");
346     }
347 
348     @Override
349     public void setCursorName(final String name) throws SQLException {
350         checkOpen();
351         this.cursorName = name;
352     }
353 
354     @Override
355     public void setEscapeProcessing(final boolean enable) throws SQLException {
356         checkOpen();
357         this.escapeProcessing = enable;
358     }
359 
360     @Override
361     public void setFetchDirection(final int direction) throws SQLException {
362         checkOpen();
363         this.fetchDirection = direction;
364     }
365 
366     @Override
367     public void setFetchSize(final int rows) throws SQLException {
368         checkOpen();
369         this.fetchSize = rows;
370     }
371 
372     @Override
373     public void setLargeMaxRows(final long max) throws SQLException {
374         checkOpen();
375         this.maxRows = max;
376     }
377 
378     @Override
379     public void setMaxFieldSize(final int max) throws SQLException {
380         checkOpen();
381         this.maxFieldSize = max;
382     }
383 
384     @Override
385     public void setMaxRows(final int max) throws SQLException {
386         checkOpen();
387         this.maxRows = max;
388     }
389 
390     @Override
391     public void setPoolable(final boolean poolable) throws SQLException {
392         throw new SQLException("Not implemented.");
393     }
394 
395     @Override
396     public void setQueryTimeout(final int seconds) throws SQLException {
397         checkOpen();
398         this.queryTimeout = seconds;
399     }
400 
401     public void setSqlExceptionOnClose(final boolean _sqlExceptionOnClose) {
402         this.sqlExceptionOnClose = _sqlExceptionOnClose;
403     }
404 
405     @Override
406     public <T> T unwrap(final Class<T> iface) throws SQLException {
407         throw new SQLException("Not implemented.");
408     }
409 }