@@ -21,6 +21,7 @@ import com.mongodb.CursorType
21
21
import com.mongodb.FindIterableImpl
22
22
import com.mongodb.Function
23
23
import com.mongodb.MongoClient
24
+ import com.mongodb.MongoGridFSException
24
25
import com.mongodb.MongoNamespace
25
26
import com.mongodb.ReadConcern
26
27
import com.mongodb.TestOperationExecutor
@@ -152,8 +153,8 @@ class GridFSFindIterableSpecification extends Specification {
152
153
firstResult. getId() == new BsonInt32 (expectedResult. getInteger(' _id' ));
153
154
}
154
155
firstResult. getFilename() == expectedResult. getString(' filename' )
155
- firstResult. getLength() == expectedResult. getLong (' length' )
156
- firstResult. getChunkSize() == expectedResult. getInteger (' chunkSize' )
156
+ firstResult. getLength() == expectedResult. get (' length' )
157
+ firstResult. getChunkSize() == expectedResult. get (' chunkSize' )
157
158
firstResult. getMD5() == expectedResult. getString(' md5' )
158
159
firstResult. getUploadDate() == expectedResult. getDate(' uploadDate' )
159
160
@@ -210,6 +211,66 @@ class GridFSFindIterableSpecification extends Specification {
210
211
cannedResults << [toSpecCannedResults, legacyCannedResults]
211
212
}
212
213
214
+ def ' should handle alternative data for the GridFS file' () {
215
+ given :
216
+ def expectedLength = 123
217
+ def expectedChunkSize = 255
218
+
219
+ def cursor = {
220
+ Stub (BatchCursor ) {
221
+ def count = 0
222
+ def getResult = {
223
+ count++
224
+ count == 1 ? [cannedResults] : null
225
+ }
226
+ next() >> { getResult() }
227
+ hasNext() >> { count == 0 }
228
+ }
229
+ }
230
+ def executor = new TestOperationExecutor ([cursor()]);
231
+ def underlying = new FindIterableImpl (namespace, Document , Document , codecRegistry, readPreference, readConcern, executor,
232
+ new Document (), new FindOptions ())
233
+ def mongoIterable = new GridFSFindIterableImpl (underlying)
234
+
235
+ when :
236
+ def results = mongoIterable. first()
237
+
238
+ then :
239
+ results. getLength() == expectedLength
240
+ results. getChunkSize() == expectedChunkSize
241
+
242
+ where :
243
+ cannedResults << alternativeImplCannedResults
244
+ }
245
+
246
+ def ' should throw if has a value with a decimal' () {
247
+ given :
248
+ def cursor = {
249
+ Stub (BatchCursor ) {
250
+ def count = 0
251
+ def getResult = {
252
+ count++
253
+ count == 1 ? [cannedResults] : null
254
+ }
255
+ next() >> { getResult() }
256
+ hasNext() >> { count == 0 }
257
+ }
258
+ }
259
+ def executor = new TestOperationExecutor ([cursor()]);
260
+ def underlying = new FindIterableImpl (namespace, Document , Document , codecRegistry, readPreference, readConcern, executor,
261
+ new Document (), new FindOptions ())
262
+ def mongoIterable = new GridFSFindIterableImpl (underlying)
263
+
264
+ when :
265
+ mongoIterable. first()
266
+
267
+ then :
268
+ thrown(MongoGridFSException )
269
+
270
+ where :
271
+ cannedResults << invalidFloatCannedResults
272
+ }
273
+
213
274
@Shared
214
275
def toSpecCannedResults = [
215
276
Document . parse(''' { '_id' : { '$oid' : '000000000000000000000001' }, 'filename' : 'File 1',
@@ -236,6 +297,24 @@ class GridFSFindIterableSpecification extends Specification {
236
297
'md5' : 'd41d8cd98f00b204e9800998ecf8427e', 'aliases' : ['File Three', 'Third File'] }''' )
237
298
]
238
299
300
+ @Shared
301
+ def alternativeImplCannedResults = [
302
+ Document . parse(''' { '_id' : 1, 'filename' : 'File 1', 'length' : 123, 'chunkSize' : { '$numberLong' : '255' },
303
+ 'uploadDate' : { '$date' : 1438679434041 }, 'md5' : 'd41d8cd98f00b204e9800998ecf8427e' }''' ),
304
+ Document . parse(''' { '_id' : { '$oid' : '000000000000000000000001' }, 'filename' : 'File 2', 'length' : 123.0,
305
+ 'chunkSize' : 255.0, 'uploadDate' : { '$date' : 1438679434050 },
306
+ 'md5' : 'd41d8cd98f00b204e9800998ecf8427e'}''' )
307
+ ]
308
+
309
+ @Shared
310
+ def invalidFloatCannedResults = [
311
+ Document . parse(''' { '_id' : 1, 'filename' : 'File 1', 'length' : 123.4, 'chunkSize' : 255,
312
+ 'uploadDate' : { '$date' : 1438679434041 }, 'md5' : 'd41d8cd98f00b204e9800998ecf8427e' }''' ),
313
+ Document . parse(''' { '_id' : { '$oid' : '000000000000000000000001' }, 'filename' : 'File 2', 'length' : 123,
314
+ 'chunkSize' : 255.5, 'uploadDate' : { '$date' : 1438679434050 },
315
+ 'md5' : 'd41d8cd98f00b204e9800998ecf8427e'}''' )
316
+ ]
317
+
239
318
@Shared
240
319
mixedResults = [toSpecCannedResults[0 ], legacyCannedResults[1 ], legacyCannedResults[2 ]]
241
320
}
0 commit comments