Skip to content

Commit 122d35a

Browse files
committed
- Part 1
1 parent e3f3943 commit 122d35a

14 files changed

+1194
-1
lines changed

src/MongoDB.Bson/Serialization/BsonSerializationInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class BsonSerializationInfo
2828
{
2929
#region static
3030
/// <summary>
31-
/// Creates a new instance of the BsonSerializationinfo class with an element path instead of an element name.
31+
/// Creates a new instance of the BsonSerializationInfo class with an element path instead of an element name.
3232
/// </summary>
3333
/// <param name="elementPath">The element path.</param>
3434
/// <param name="serializer">The serializer.</param>

src/MongoDB.Bson/Serialization/Serializers/NullableSerializer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public static class NullableSerializer
4040
/// <returns>A NullableSerializer</returns>
4141
public static IBsonSerializer Create(IBsonSerializer valueSerializer)
4242
{
43+
if (valueSerializer == null)
44+
{
45+
throw new ArgumentNullException(nameof(valueSerializer));
46+
}
47+
4348
var valueType = valueSerializer.ValueType;
4449
var nullableSerializerType = typeof(NullableSerializer<>).MakeGenericType(valueType);
4550
return (IBsonSerializer)Activator.CreateInstance(nullableSerializerType, valueSerializer);
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* Copyright 2010-present 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.Runtime.Serialization;
18+
using FluentAssertions;
19+
using Xunit;
20+
21+
namespace MongoDB.Bson.Tests.Exceptions
22+
{
23+
public class BsonExceptionTests
24+
{
25+
[Fact]
26+
public void constructor_with_format_and_args_should_format_message_correctly()
27+
{
28+
// Act
29+
var exception = new BsonException("Error code: {0}, message: {1}", 123, "Test error");
30+
31+
// Assert
32+
exception.Message.Should().Be("Error code: 123, message: Test error");
33+
}
34+
35+
[Fact]
36+
public void constructor_with_format_and_args_should_handle_empty_args()
37+
{
38+
// Act
39+
var exception = new BsonException("Simple message");
40+
41+
// Assert
42+
exception.Message.Should().Be("Simple message");
43+
}
44+
45+
[Fact]
46+
public void constructor_with_format_and_args_should_handle_null_args()
47+
{
48+
// Act
49+
var exception = new BsonException("Message with {0}", (object)null);
50+
51+
// Assert
52+
exception.Message.Should().Be("Message with ");
53+
}
54+
55+
[Fact]
56+
public void constructor_with_serialization_info_should_deserialize_correctly()
57+
{
58+
// Arrange
59+
var expectedMessage = "Test serialized exception";
60+
var info = new SerializationInfo(typeof(BsonException), new FormatterConverter());
61+
info.AddValue("Message", expectedMessage);
62+
info.AddValue("ClassName", typeof(BsonException).FullName);
63+
info.AddValue("Data", null);
64+
info.AddValue("InnerException", null);
65+
info.AddValue("HelpURL", null);
66+
info.AddValue("StackTraceString", null);
67+
info.AddValue("RemoteStackTraceString", null);
68+
info.AddValue("RemoteStackIndex", 0);
69+
info.AddValue("ExceptionMethod", null);
70+
info.AddValue("HResult", -2146233088);
71+
info.AddValue("Source", null);
72+
73+
// Act
74+
var exception = new BsonException(info, new StreamingContext());
75+
76+
// Assert
77+
exception.Message.Should().Be(expectedMessage);
78+
}
79+
80+
[Fact]
81+
public void constructor_with_serialization_info_should_preserve_inner_exception()
82+
{
83+
// Arrange
84+
var innerExceptionMessage = "Inner exception message";
85+
var innerException = new InvalidOperationException(innerExceptionMessage);
86+
var originalException = new BsonException("Outer message", innerException);
87+
88+
var info = new SerializationInfo(typeof(BsonException), new FormatterConverter());
89+
originalException.GetObjectData(info, new StreamingContext());
90+
91+
// Act
92+
var deserializedException = new BsonException(info, new StreamingContext());
93+
94+
// Assert
95+
deserializedException.InnerException.Should().NotBeNull();
96+
deserializedException.InnerException.Message.Should().Be(innerExceptionMessage);
97+
}
98+
}
99+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* Copyright 2010-present 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.Runtime.Serialization;
18+
using FluentAssertions;
19+
using Xunit;
20+
21+
namespace MongoDB.Bson.Tests.Exceptions
22+
{
23+
public class BsonInternalExceptionTests
24+
{
25+
[Fact]
26+
public void constructor_should_initialize_empty_instance()
27+
{
28+
// Act
29+
var exception = new BsonInternalException();
30+
31+
// Assert
32+
exception.Message.Should().Be("Exception of type 'MongoDB.Bson.BsonInternalException' was thrown.");
33+
exception.InnerException.Should().BeNull();
34+
}
35+
36+
[Fact]
37+
public void constructor_should_initialize_instance_with_message()
38+
{
39+
// Arrange
40+
var message = "Test internal exception message";
41+
42+
// Act
43+
var exception = new BsonInternalException(message);
44+
45+
// Assert
46+
exception.Message.Should().Be(message);
47+
exception.InnerException.Should().BeNull();
48+
}
49+
50+
[Fact]
51+
public void constructor_should_initialize_instance_with_message_and_inner_exception()
52+
{
53+
// Arrange
54+
var message = "Test internal exception message";
55+
var innerException = new Exception("Inner exception message");
56+
57+
// Act
58+
var exception = new BsonInternalException(message, innerException);
59+
60+
// Assert
61+
exception.Message.Should().Be(message);
62+
exception.InnerException.Should().BeSameAs(innerException);
63+
}
64+
65+
[Fact]
66+
public void constructor_should_initialize_instance_with_serialization_info()
67+
{
68+
// Arrange
69+
var message = "Test serialized internal exception";
70+
var info = new SerializationInfo(typeof(BsonInternalException), new FormatterConverter());
71+
info.AddValue("Message", message);
72+
info.AddValue("ClassName", typeof(BsonInternalException).FullName);
73+
info.AddValue("Data", null);
74+
info.AddValue("InnerException", null);
75+
info.AddValue("HelpURL", null);
76+
info.AddValue("StackTraceString", null);
77+
info.AddValue("RemoteStackTraceString", null);
78+
info.AddValue("RemoteStackIndex", 0);
79+
info.AddValue("ExceptionMethod", null);
80+
info.AddValue("HResult", -2146233088);
81+
info.AddValue("Source", null);
82+
var context = new StreamingContext();
83+
84+
// Act
85+
var exception = new BsonInternalException(info, context);
86+
87+
// Assert
88+
exception.Message.Should().Be(message);
89+
}
90+
91+
[Fact]
92+
public void constructor_with_serialization_info_should_preserve_inner_exception()
93+
{
94+
// Arrange
95+
var message = "Test serialized internal exception";
96+
var innerExceptionMessage = "Inner exception message";
97+
var innerException = new Exception(innerExceptionMessage);
98+
99+
// Create an exception with an inner exception
100+
var originalException = new BsonInternalException(message, innerException);
101+
102+
// Serialize it
103+
var info = new SerializationInfo(typeof(BsonInternalException), new FormatterConverter());
104+
originalException.GetObjectData(info, new StreamingContext());
105+
106+
// Act
107+
var deserializedException = new BsonInternalException(info, new StreamingContext());
108+
109+
// Assert
110+
deserializedException.Message.Should().Be(message);
111+
deserializedException.InnerException.Should().NotBeNull();
112+
deserializedException.InnerException.Message.Should().Be(innerExceptionMessage);
113+
}
114+
115+
[Fact]
116+
public void should_inherit_from_bson_exception()
117+
{
118+
// Act
119+
var exception = new BsonInternalException();
120+
121+
// Assert
122+
exception.Should().BeAssignableTo<BsonException>();
123+
}
124+
}
125+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* Copyright 2010-present 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 FluentAssertions;
18+
using MongoDB.Bson.IO;
19+
using Xunit;
20+
21+
namespace MongoDB.Bson.Tests.IO
22+
{
23+
public class DateTimeJsonTokenTests
24+
{
25+
[Fact]
26+
public void Constructor_should_initialize_token_with_provided_values()
27+
{
28+
// Arrange
29+
var lexeme = "ISODate(\"2023-01-01T00:00:00Z\")";
30+
var value = new BsonDateTime(new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc));
31+
32+
// Act
33+
var token = new DateTimeJsonToken(lexeme, value);
34+
35+
// Assert
36+
token.Lexeme.Should().Be(lexeme);
37+
token.Type.Should().Be(JsonTokenType.DateTime);
38+
}
39+
40+
[Fact]
41+
public void Constructor_should_set_token_type_to_DateTime()
42+
{
43+
// Arrange
44+
var lexeme = "ISODate(\"2023-01-01T00:00:00Z\")";
45+
var value = new BsonDateTime(new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc));
46+
47+
// Act
48+
var token = new DateTimeJsonToken(lexeme, value);
49+
50+
// Assert
51+
token.Type.Should().Be(JsonTokenType.DateTime);
52+
}
53+
54+
[Fact]
55+
public void DateTimeValue_should_return_provided_value()
56+
{
57+
// Arrange
58+
var lexeme = "ISODate(\"2023-01-01T00:00:00Z\")";
59+
var value = new BsonDateTime(new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc));
60+
var token = new DateTimeJsonToken(lexeme, value);
61+
62+
// Act
63+
var result = token.DateTimeValue;
64+
65+
// Assert
66+
result.Should().Be(value);
67+
}
68+
}
69+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Copyright 2010-present 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 FluentAssertions;
17+
using MongoDB.Bson.IO;
18+
using Xunit;
19+
20+
namespace MongoDB.Bson.Tests.IO
21+
{
22+
public class ObjectIdJsonTokenTests
23+
{
24+
[Fact]
25+
public void Constructor_should_initialize_token_with_provided_values()
26+
{
27+
// Arrange
28+
var lexeme = "ObjectId(\"507f1f77bcf86cd799439011\")";
29+
var value = new ObjectId("507f1f77bcf86cd799439011");
30+
31+
// Act
32+
var token = new ObjectIdJsonToken(lexeme, value);
33+
34+
// Assert
35+
token.Lexeme.Should().Be(lexeme);
36+
token.Type.Should().Be(JsonTokenType.ObjectId);
37+
}
38+
39+
[Fact]
40+
public void Constructor_should_set_token_type_to_ObjectId()
41+
{
42+
// Arrange
43+
var lexeme = "ObjectId(\"507f1f77bcf86cd799439011\")";
44+
var value = new ObjectId("507f1f77bcf86cd799439011");
45+
46+
// Act
47+
var token = new ObjectIdJsonToken(lexeme, value);
48+
49+
// Assert
50+
token.Type.Should().Be(JsonTokenType.ObjectId);
51+
}
52+
53+
[Fact]
54+
public void ObjectIdValue_should_return_provided_value()
55+
{
56+
// Arrange
57+
var lexeme = "ObjectId(\"507f1f77bcf86cd799439011\")";
58+
var value = new ObjectId("507f1f77bcf86cd799439011");
59+
var token = new ObjectIdJsonToken(lexeme, value);
60+
61+
// Act
62+
var result = token.ObjectIdValue;
63+
64+
// Assert
65+
result.Should().Be(value);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)