@@ -21,19 +21,27 @@ import com.mongodb.async.client.MongoCollection
2121import com.mongodb.async.client.MongoDatabase
2222import com.mongodb.async.client.gridfs.GridFSBucket
2323import com.mongodb.async.client.gridfs.GridFSBucketImpl
24- import com.mongodb.async.client.gridfs.GridFSTestHelper
2524import org.bson.Document
2625import spock.lang.Requires
2726
2827import java.nio.ByteBuffer
29- import java.nio.channels.* // Wildcard import to work around Java 1.6
30- import java.nio.file.* // Wildcard import to work around Java 1.6
28+ import java.nio.channels.AsynchronousByteChannel
29+ import java.nio.channels.AsynchronousFileChannel
30+ import java.nio.channels.CompletionHandler
31+ import java.nio.file.Files
32+
33+ // Wildcard import to work around Java 1.6
34+ import java.nio.file.Path
35+ import java.nio.file.Paths
36+ import java.nio.file.StandardOpenOption
37+ import java.util.concurrent.Future
3138
32- import static GridFSTestHelper.TestAsynchronousByteChannel
33- import static GridFSTestHelper.run
3439import static com.mongodb.async.client.Fixture.getDefaultDatabaseName
3540import static com.mongodb.async.client.Fixture.getMongoClient
41+ import static com.mongodb.async.client.TestHelper.run
3642import static com.mongodb.async.client.gridfs.helpers.AsynchronousChannelHelper.channelToInputStream
43+
44+ // Wildcard import to work around Java 1.6
3745import static com.mongodb.async.client.gridfs.helpers.AsynchronousChannelHelper.channelToOutputStream
3846
3947@Requires ({ javaVersion >= 1.7 })
@@ -47,7 +55,7 @@ class AsynchronousChannelHelperSmokeTestSpecification extends FunctionalSpecific
4755 mongoDatabase = getMongoClient(). getDatabase(getDefaultDatabaseName())
4856 filesCollection = mongoDatabase. getCollection(' fs.files' )
4957 chunksCollection = mongoDatabase. getCollection(' fs.chunks' )
50- GridFSTestHelper . run(filesCollection. &drop)
58+ run(filesCollection. &drop)
5159 run(chunksCollection. &drop)
5260 gridFSBucket = new GridFSBucketImpl (mongoDatabase)
5361 }
@@ -109,4 +117,70 @@ class AsynchronousChannelHelperSmokeTestSpecification extends FunctionalSpecific
109117 content == asyncByteChannel. getWriteBuffer()
110118 }
111119
120+ static class TestAsynchronousByteChannel implements AsynchronousByteChannel , Closeable {
121+ private boolean closed
122+ private final readBuffer
123+ private final writeBuffer = ByteBuffer . allocate(1024 )
124+
125+ TestAsynchronousByteChannel (final readBuffer ) {
126+ this . readBuffer = readBuffer
127+ }
128+
129+ ByteBuffer getReadBuffer () {
130+ readBuffer. flip()
131+ }
132+
133+ ByteBuffer getWriteBuffer () {
134+ writeBuffer. flip()
135+ }
136+
137+ @Override
138+ < A> void read(final ByteBuffer dst, final A attachment, final CompletionHandler<Integer , ? super A> handler) {
139+ int transferAmount = Math . min(dst. remaining(), readBuffer. remaining());
140+ if (transferAmount == 0 ) {
141+ transferAmount = -1
142+ }
143+ if (transferAmount > 0 ) {
144+ ByteBuffer temp = readBuffer. duplicate()
145+ temp. limit(temp. position() + transferAmount)
146+ dst. put(temp)
147+ readBuffer. position(readBuffer. position() + transferAmount)
148+ }
149+ handler. completed(transferAmount, attachment)
150+ }
151+
152+ @Override
153+ Future<Integer > read (final ByteBuffer dst ) {
154+ throw new UnsupportedOperationException (' Not Supported' )
155+ }
156+
157+ @Override
158+ < A> void write(final ByteBuffer src, final A attachment, final CompletionHandler<Integer , ? super A> handler) {
159+ int transferAmount = Math . min(src. remaining(), writeBuffer. remaining());
160+ if (transferAmount == 0 ) {
161+ transferAmount = -1
162+ }
163+ if (transferAmount > 0 ) {
164+ ByteBuffer temp = src. duplicate()
165+ temp. limit(temp. position() + transferAmount)
166+ writeBuffer. put(temp)
167+ }
168+ handler. completed(transferAmount, attachment)
169+ }
170+
171+ @Override
172+ Future<Integer > write (final ByteBuffer src ) {
173+ throw new UnsupportedOperationException (' Not Supported' )
174+ }
175+
176+ @Override
177+ void close () throws IOException {
178+ closed = true
179+ }
180+
181+ @Override
182+ boolean isOpen () {
183+ ! closed;
184+ }
185+ }
112186}
0 commit comments