|
| 1 | +/* Copyright 2019-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 MongoDB.Bson.Serialization; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace MongoDB.Bson.Tests.Serialization |
| 22 | +{ |
| 23 | + public class BsonClassMapSerializerISupportInitializeTests |
| 24 | + { |
| 25 | + // public methods |
| 26 | + [Fact] |
| 27 | + public void BeginInit_and_EndInit_should_be_called_when_deserializing_ClassDerivedFromClassImplementingISupportInitialize() |
| 28 | + { |
| 29 | + var subject = BsonSerializer.LookupSerializer<ClassDerivedFromClassImplementingISupportInitialize>(); |
| 30 | + using (var reader = new JsonReader("{ }")) |
| 31 | + { |
| 32 | + var context = BsonDeserializationContext.CreateRoot(reader); |
| 33 | + |
| 34 | + var result = subject.Deserialize(context); |
| 35 | + |
| 36 | + result.BeginInitWasCalled.Should().BeTrue(); |
| 37 | + result.EndInitWasCalled.Should().BeTrue(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void BeginInit_and_EndInit_should_be_called_when_deserializing_ClassDerivedFromClassImplementingISupportInitializeExplicitly() |
| 43 | + { |
| 44 | + var subject = BsonSerializer.LookupSerializer<ClassDerivedFromClassImplementingISupportInitializeExplicitly>(); |
| 45 | + using (var reader = new JsonReader("{ }")) |
| 46 | + { |
| 47 | + var context = BsonDeserializationContext.CreateRoot(reader); |
| 48 | + |
| 49 | + var result = subject.Deserialize(context); |
| 50 | + |
| 51 | + result.BeginInitWasCalled.Should().BeTrue(); |
| 52 | + result.EndInitWasCalled.Should().BeTrue(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void BeginInit_and_EndInit_should_be_called_when_deserializing_ClassImplementingISupportInitialize() |
| 58 | + { |
| 59 | + var subject = BsonSerializer.LookupSerializer<ClassImplementingISupportInitialize>(); |
| 60 | + using (var reader = new JsonReader("{ }")) |
| 61 | + { |
| 62 | + var context = BsonDeserializationContext.CreateRoot(reader); |
| 63 | + |
| 64 | + var result = subject.Deserialize(context); |
| 65 | + |
| 66 | + result.BeginInitWasCalled.Should().BeTrue(); |
| 67 | + result.EndInitWasCalled.Should().BeTrue(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void BeginInit_and_EndInit_should_be_called_when_deserializing_ClassImplementingISupportInitializeExplicitly() |
| 73 | + { |
| 74 | + var subject = BsonSerializer.LookupSerializer<ClassImplementingISupportInitializeExplicitly>(); |
| 75 | + using (var reader = new JsonReader("{ }")) |
| 76 | + { |
| 77 | + var context = BsonDeserializationContext.CreateRoot(reader); |
| 78 | + |
| 79 | + var result = subject.Deserialize(context); |
| 80 | + |
| 81 | + result.BeginInitWasCalled.Should().BeTrue(); |
| 82 | + result.EndInitWasCalled.Should().BeTrue(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void BeginInit_and_EndInit_should_not_be_called_when_deserializing_ClassDerivedFromClassImplementingISupportInitializeInDifferentNamespace() |
| 88 | + { |
| 89 | + var subject = BsonSerializer.LookupSerializer<ClassDerivedFromClassImplementingISupportInitializeInDifferentNamespace>(); |
| 90 | + using (var reader = new JsonReader("{ }")) |
| 91 | + { |
| 92 | + var context = BsonDeserializationContext.CreateRoot(reader); |
| 93 | + |
| 94 | + var result = subject.Deserialize(context); |
| 95 | + |
| 96 | + result.BeginInitWasCalled.Should().BeFalse(); |
| 97 | + result.EndInitWasCalled.Should().BeFalse(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void BeginInit_and_EndInit_should_not_be_called_when_deserializing_ClassImplementingISupportInitializeInDifferentNamespace() |
| 103 | + { |
| 104 | + var subject = BsonSerializer.LookupSerializer<ClassImplementingISupportInitializeInDifferentNamespace>(); |
| 105 | + using (var reader = new JsonReader("{ }")) |
| 106 | + { |
| 107 | + var context = BsonDeserializationContext.CreateRoot(reader); |
| 108 | + |
| 109 | + var result = subject.Deserialize(context); |
| 110 | + |
| 111 | + result.BeginInitWasCalled.Should().BeFalse(); |
| 112 | + result.EndInitWasCalled.Should().BeFalse(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // nested classes |
| 117 | + public interface ISupportInitialize |
| 118 | + { |
| 119 | + void BeginInit(); |
| 120 | + void EndInit(); |
| 121 | + } |
| 122 | + |
| 123 | + public class ClassImplementingISupportInitialize : System.ComponentModel.ISupportInitialize |
| 124 | + { |
| 125 | + public bool BeginInitWasCalled { get; set; } |
| 126 | + public bool EndInitWasCalled { get; set; } |
| 127 | + public void BeginInit() { BeginInitWasCalled = true; } |
| 128 | + public void EndInit() { EndInitWasCalled = true; } |
| 129 | + } |
| 130 | + |
| 131 | + public class ClassImplementingISupportInitializeExplicitly : System.ComponentModel.ISupportInitialize |
| 132 | + { |
| 133 | + public bool BeginInitWasCalled { get; set; } |
| 134 | + public bool EndInitWasCalled { get; set; } |
| 135 | + void System.ComponentModel.ISupportInitialize.BeginInit() { BeginInitWasCalled = true; } |
| 136 | + void System.ComponentModel.ISupportInitialize.EndInit() { EndInitWasCalled = true; } |
| 137 | + } |
| 138 | + |
| 139 | + public class ClassImplementingISupportInitializeInDifferentNamespace : MongoDB.Bson.Tests.Serialization.BsonClassMapSerializerISupportInitializeTests.ISupportInitialize |
| 140 | + { |
| 141 | + public bool BeginInitWasCalled { get; set; } |
| 142 | + public bool EndInitWasCalled { get; set; } |
| 143 | + public void BeginInit() { BeginInitWasCalled = true; } |
| 144 | + public void EndInit() { EndInitWasCalled = true; } |
| 145 | + } |
| 146 | + |
| 147 | + public class ClassDerivedFromClassImplementingISupportInitialize : ClassImplementingISupportInitialize |
| 148 | + { |
| 149 | + } |
| 150 | + |
| 151 | + public class ClassDerivedFromClassImplementingISupportInitializeExplicitly : ClassImplementingISupportInitializeExplicitly |
| 152 | + { |
| 153 | + } |
| 154 | + |
| 155 | + public class ClassDerivedFromClassImplementingISupportInitializeInDifferentNamespace : ClassImplementingISupportInitializeInDifferentNamespace |
| 156 | + { |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments