/driver-core/src/test/unit/com/mongodb/connection/TestConnection.java

http://github.com/mongodb/mongo-java-driver · Java · 262 lines · 206 code · 41 blank · 15 comment · 0 complexity · 0dcf7b70f0f4fc401584f1cc8cf1df68 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2015 MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.connection;
  17. import com.mongodb.MongoNamespace;
  18. import com.mongodb.WriteConcern;
  19. import com.mongodb.WriteConcernResult;
  20. import com.mongodb.async.SingleResultCallback;
  21. import com.mongodb.bulk.BulkWriteResult;
  22. import com.mongodb.bulk.DeleteRequest;
  23. import com.mongodb.bulk.InsertRequest;
  24. import com.mongodb.bulk.UpdateRequest;
  25. import org.bson.BsonDocument;
  26. import org.bson.FieldNameValidator;
  27. import org.bson.codecs.Decoder;
  28. import java.util.List;
  29. @SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
  30. class TestConnection implements Connection, AsyncConnection {
  31. private final InternalConnection internalConnection;
  32. private final ProtocolExecutor executor;
  33. private Protocol enqueuedProtocol;
  34. public TestConnection(final InternalConnection internalConnection, final ProtocolExecutor executor) {
  35. this.internalConnection = internalConnection;
  36. this.executor = executor;
  37. }
  38. @Override
  39. public int getCount() {
  40. return 1;
  41. }
  42. @Override
  43. public TestConnection retain() {
  44. return this;
  45. }
  46. @Override
  47. public void release() {
  48. }
  49. @Override
  50. public ConnectionDescription getDescription() {
  51. throw new UnsupportedOperationException("Not implemented yet!");
  52. }
  53. @Override
  54. public WriteConcernResult insert(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  55. final List<InsertRequest> inserts) {
  56. return executeEnqueuedProtocol();
  57. }
  58. @Override
  59. public void insertAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  60. final List<InsertRequest> inserts, final SingleResultCallback<WriteConcernResult> callback) {
  61. executeEnqueuedProtocolAsync(callback);
  62. }
  63. @Override
  64. public WriteConcernResult update(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  65. final List<UpdateRequest> updates) {
  66. return executeEnqueuedProtocol();
  67. }
  68. @Override
  69. public void updateAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  70. final List<UpdateRequest> updates, final SingleResultCallback<WriteConcernResult> callback) {
  71. executeEnqueuedProtocolAsync(callback);
  72. }
  73. @Override
  74. public WriteConcernResult delete(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  75. final List<DeleteRequest> deletes) {
  76. return executeEnqueuedProtocol();
  77. }
  78. @Override
  79. public void deleteAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  80. final List<DeleteRequest> deletes,
  81. final SingleResultCallback<WriteConcernResult> callback) {
  82. executeEnqueuedProtocolAsync(callback);
  83. }
  84. @Override
  85. public BulkWriteResult insertCommand(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  86. final List<InsertRequest> inserts) {
  87. throw new UnsupportedOperationException("Deprecated method called directly - this should have been updated");
  88. }
  89. @Override
  90. public BulkWriteResult insertCommand(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  91. final Boolean bypassDocumentValidation, final List<InsertRequest> inserts) {
  92. return executeEnqueuedProtocol();
  93. }
  94. @Override
  95. public void insertCommandAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  96. final List<InsertRequest> inserts, final SingleResultCallback<BulkWriteResult> callback) {
  97. throw new UnsupportedOperationException("Deprecated method called directly - this should have been updated");
  98. }
  99. @Override
  100. public void insertCommandAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  101. final Boolean bypassDocumentValidation, final List<InsertRequest> inserts,
  102. final SingleResultCallback<BulkWriteResult> callback) {
  103. executeEnqueuedProtocolAsync(callback);
  104. }
  105. @Override
  106. public BulkWriteResult updateCommand(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  107. final List<UpdateRequest> updates) {
  108. throw new UnsupportedOperationException("Deprecated method called directly - this should have been updated");
  109. }
  110. @Override
  111. public BulkWriteResult updateCommand(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  112. final Boolean bypassDocumentValidation, final List<UpdateRequest> updates) {
  113. return executeEnqueuedProtocol();
  114. }
  115. @Override
  116. public void updateCommandAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  117. final List<UpdateRequest> updates, final SingleResultCallback<BulkWriteResult> callback) {
  118. throw new UnsupportedOperationException("Deprecated method called directly - this should have been updated");
  119. }
  120. @Override
  121. public void updateCommandAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  122. final Boolean bypassDocumentValidation, final List<UpdateRequest> updates,
  123. final SingleResultCallback<BulkWriteResult> callback) {
  124. executeEnqueuedProtocolAsync(callback);
  125. }
  126. @Override
  127. public BulkWriteResult deleteCommand(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  128. final List<DeleteRequest> deletes) {
  129. return executeEnqueuedProtocol();
  130. }
  131. @Override
  132. public void deleteCommandAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
  133. final List<DeleteRequest> deletes,
  134. final SingleResultCallback<BulkWriteResult> callback) {
  135. executeEnqueuedProtocolAsync(callback);
  136. }
  137. @Override
  138. public <T> T command(final String database, final BsonDocument command, final boolean slaveOk,
  139. final FieldNameValidator fieldNameValidator,
  140. final Decoder<T> commandResultDecoder) {
  141. return executeEnqueuedProtocol();
  142. }
  143. @Override
  144. public <T> void commandAsync(final String database, final BsonDocument command, final boolean slaveOk,
  145. final FieldNameValidator fieldNameValidator,
  146. final Decoder<T> commandResultDecoder, final SingleResultCallback<T> callback) {
  147. executeEnqueuedProtocolAsync(callback);
  148. }
  149. @Override
  150. public <T> QueryResult<T> query(final MongoNamespace namespace, final BsonDocument queryDocument, final BsonDocument fields,
  151. final int numberToReturn, final int skip,
  152. final boolean slaveOk, final boolean tailableCursor, final boolean awaitData,
  153. final boolean noCursorTimeout,
  154. final boolean partial, final boolean oplogReplay, final Decoder<T> resultDecoder) {
  155. return executeEnqueuedProtocol();
  156. }
  157. @Override
  158. public <T> QueryResult<T> query(final MongoNamespace namespace, final BsonDocument queryDocument, final BsonDocument fields,
  159. final int skip, final int limit,
  160. final int batchSize, final boolean slaveOk, final boolean tailableCursor, final boolean awaitData,
  161. final boolean noCursorTimeout,
  162. final boolean partial, final boolean oplogReplay, final Decoder<T> resultDecoder) {
  163. return executeEnqueuedProtocol();
  164. }
  165. @Override
  166. public <T> void queryAsync(final MongoNamespace namespace, final BsonDocument queryDocument, final BsonDocument fields,
  167. final int numberToReturn, final int skip,
  168. final boolean slaveOk, final boolean tailableCursor, final boolean awaitData, final boolean noCursorTimeout,
  169. final boolean partial,
  170. final boolean oplogReplay, final Decoder<T> resultDecoder,
  171. final SingleResultCallback<QueryResult<T>> callback) {
  172. executeEnqueuedProtocolAsync(callback);
  173. }
  174. @Override
  175. public <T> void queryAsync(final MongoNamespace namespace, final BsonDocument queryDocument, final BsonDocument fields, final int skip,
  176. final int limit,
  177. final int batchSize, final boolean slaveOk, final boolean tailableCursor, final boolean awaitData,
  178. final boolean noCursorTimeout,
  179. final boolean partial, final boolean oplogReplay, final Decoder<T> resultDecoder,
  180. final SingleResultCallback<QueryResult<T>> callback) {
  181. executeEnqueuedProtocolAsync(callback);
  182. }
  183. @Override
  184. public <T> QueryResult<T> getMore(final MongoNamespace namespace, final long cursorId, final int numberToReturn,
  185. final Decoder<T> resultDecoder) {
  186. return executeEnqueuedProtocol();
  187. }
  188. @Override
  189. public <T> void getMoreAsync(final MongoNamespace namespace, final long cursorId, final int numberToReturn,
  190. final Decoder<T> resultDecoder,
  191. final SingleResultCallback<QueryResult<T>> callback) {
  192. executeEnqueuedProtocolAsync(callback);
  193. }
  194. @Override
  195. public void killCursor(final List<Long> cursors) {
  196. executeEnqueuedProtocol();
  197. }
  198. @Override
  199. public void killCursor(final MongoNamespace namespace, final List<Long> cursors) {
  200. executeEnqueuedProtocol();
  201. }
  202. @Override
  203. public void killCursorAsync(final List<Long> cursors, final SingleResultCallback<Void> callback) {
  204. executeEnqueuedProtocolAsync(callback);
  205. }
  206. @Override
  207. public void killCursorAsync(final MongoNamespace namespace, final List<Long> cursors, final SingleResultCallback<Void> callback) {
  208. executeEnqueuedProtocolAsync(callback);
  209. }
  210. @SuppressWarnings("unchecked")
  211. private <T> T executeEnqueuedProtocol() {
  212. return (T) executor.execute(enqueuedProtocol, internalConnection);
  213. }
  214. @SuppressWarnings("unchecked")
  215. private <T> void executeEnqueuedProtocolAsync(final SingleResultCallback<T> callback) {
  216. executor.executeAsync(enqueuedProtocol, internalConnection, callback);
  217. }
  218. void enqueueProtocol(final Protocol protocol) {
  219. enqueuedProtocol = protocol;
  220. }
  221. }