Skip to content

Commit c44f347

Browse files
committed
CSHARP-1484: Support GridFSDownloadStream CopyTo and CopyToAsync methods.
1 parent b04d99b commit c44f347

File tree

5 files changed

+156
-11
lines changed

5 files changed

+156
-11
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* Copyright 2015 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.IO;
19+
using System.Linq;
20+
using System.Text;
21+
using System.Threading.Tasks;
22+
using FluentAssertions;
23+
using MongoDB.Bson;
24+
using MongoDB.Driver.Tests;
25+
using NUnit.Framework;
26+
27+
namespace MongoDB.Driver.GridFS.Tests
28+
{
29+
[TestFixture]
30+
public class GridFSDownloadStreamBaseTests
31+
{
32+
// public methods
33+
[Test]
34+
public void CopyTo_should_copy_stream(
35+
[Values(0.0, 0.5, 1.0, 1.5, 2.0, 2.5)] double contentSizeMultiple,
36+
[Values(null, 128)] int? bufferSize,
37+
[Values(false, true)] bool async)
38+
{
39+
var bucket = CreateBucket(128);
40+
var contentSize = (int)(bucket.Options.ChunkSizeBytes * contentSizeMultiple);
41+
var content = CreateContent(contentSize);
42+
var id = CreateGridFSFile(bucket, content);
43+
var subject = bucket.OpenDownloadStream(id);
44+
45+
using (var destination = new MemoryStream())
46+
{
47+
if (async)
48+
{
49+
if (bufferSize.HasValue)
50+
{
51+
subject.CopyToAsync(destination, bufferSize.Value).GetAwaiter().GetResult();
52+
}
53+
else
54+
{
55+
subject.CopyToAsync(destination).GetAwaiter().GetResult();
56+
}
57+
}
58+
else
59+
{
60+
if (bufferSize.HasValue)
61+
{
62+
subject.CopyTo(destination, bufferSize.Value);
63+
}
64+
else
65+
{
66+
subject.CopyTo(destination);
67+
}
68+
}
69+
70+
destination.ToArray().Should().Equal(content);
71+
}
72+
}
73+
74+
// private methods
75+
private IGridFSBucket CreateBucket(int chunkSize)
76+
{
77+
var client = DriverTestConfiguration.Client;
78+
var databaseNamespace = DriverTestConfiguration.DatabaseNamespace;
79+
var database = client.GetDatabase(databaseNamespace.DatabaseName);
80+
var bucketOptions = new GridFSBucketOptions { ChunkSizeBytes = chunkSize };
81+
return new GridFSBucket(database, bucketOptions);
82+
}
83+
84+
private byte[] CreateContent(int contentSize)
85+
{
86+
return Enumerable.Range(0, contentSize).Select(i => (byte)i).ToArray();
87+
}
88+
89+
private ObjectId CreateGridFSFile(IGridFSBucket bucket, byte[] content)
90+
{
91+
return bucket.UploadFromBytes("filename", content);
92+
}
93+
}
94+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Copyright 2015 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.IO;
18+
using FluentAssertions;
19+
using MongoDB.Driver.Tests;
20+
using NUnit.Framework;
21+
22+
namespace MongoDB.Driver.GridFS.Tests
23+
{
24+
[TestFixture]
25+
public class GridFSUploadStreamTests
26+
{
27+
// public methods
28+
[Test]
29+
public void CopyTo_should_throw(
30+
[Values(false, true)] bool async)
31+
{
32+
var bucket = CreateBucket();
33+
var subject = bucket.OpenUploadStream("Filename");
34+
35+
using (var destination = new MemoryStream())
36+
{
37+
Action action;
38+
if (async)
39+
{
40+
action = () => subject.CopyToAsync(destination).GetAwaiter().GetResult();
41+
}
42+
else
43+
{
44+
action = () => subject.CopyTo(destination);
45+
}
46+
47+
action.ShouldThrow<NotSupportedException>();
48+
}
49+
}
50+
51+
// private methods
52+
private IGridFSBucket CreateBucket()
53+
{
54+
var client = DriverTestConfiguration.Client;
55+
var databaseNamespace = DriverTestConfiguration.DatabaseNamespace;
56+
var database = client.GetDatabase(databaseNamespace.DatabaseName);
57+
return new GridFSBucket(database);
58+
}
59+
}
60+
}

src/MongoDB.Driver.GridFS.Tests/MongoDB.Driver.GridFS.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@
6060
<Compile Include="GridFSChunkExceptionTests.cs" />
6161
<Compile Include="GridFSDownloadByNameOptionsTests.cs" />
6262
<Compile Include="GridFSDownloadOptionsTests.cs" />
63+
<Compile Include="GridFSDownloadStreamBaseTests.cs" />
6364
<Compile Include="GridFSExceptionTests.cs" />
6465
<Compile Include="GridFSFileNotFoundExceptionTests.cs" />
6566
<Compile Include="GridFSFileInfoTests.cs" />
6667
<Compile Include="GridFSFindOptionsTests.cs" />
6768
<Compile Include="GridFSMD5ExceptionTests.cs" />
6869
<Compile Include="GridFSUploadOptionsTests.cs" />
70+
<Compile Include="GridFSUploadStreamTests.cs" />
6971
<Compile Include="Properties\AssemblyInfo.cs" />
7072
<Compile Include="SetupFixture.cs" />
7173
<Compile Include="Specifications\gridfs\GridFSDeleteTest.cs" />

src/MongoDB.Driver.GridFS/GridFSDownloadStreamBase.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ public override void Close(CancellationToken cancellationToken)
9292
return Task.FromResult(true);
9393
}
9494

95-
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
96-
{
97-
throw new NotImplementedException();
98-
}
99-
10095
public override void Flush()
10196
{
10297
throw new NotSupportedException();

src/MongoDB.Driver.GridFS/GridFSForwardOnlyUploadStream.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18-
using System.IO;
1918
using System.Linq;
2019
using System.Security.Cryptography;
2120
using System.Threading;
@@ -178,11 +177,6 @@ public override void Close(CancellationToken cancellationToken)
178177
base.Close();
179178
}
180179

181-
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
182-
{
183-
throw new NotSupportedException();
184-
}
185-
186180
public override void Flush()
187181
{
188182
throw new NotSupportedException();

0 commit comments

Comments
 (0)