PageRenderTime 30ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MongoDB.Driver.Core.Tests/MongoConnectionExceptionTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 73 lines | 50 code | 9 blank | 14 comment | 0 complexity | b7f7fb7c4479571a37e72d9e4989eecd MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-2014 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. using System;
  16. using System.IO;
  17. using System.Net;
  18. using System.Runtime.Serialization.Formatters.Binary;
  19. using FluentAssertions;
  20. using MongoDB.Driver.Core.Clusters;
  21. using MongoDB.Driver.Core.Connections;
  22. using MongoDB.Driver.Core.Servers;
  23. using NUnit.Framework;
  24. namespace MongoDB.Driver
  25. {
  26. [TestFixture]
  27. public class MongoConnectionExceptionTests
  28. {
  29. private readonly ConnectionId _connectionId = new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("localhost", 27017)), 2).WithServerValue(3);
  30. private Exception _innerException = new Exception("inner");
  31. private string _message = "message";
  32. [Test]
  33. public void constructor_should_initialize_subject()
  34. {
  35. var subject = new MongoConnectionException(_connectionId, _message);
  36. subject.ConnectionId.Should().BeSameAs(_connectionId);
  37. subject.InnerException.Should().BeNull();
  38. subject.Message.Should().BeSameAs(_message);
  39. }
  40. [Test]
  41. public void constructor_with_innerException_should_initialize_subject()
  42. {
  43. var subject = new MongoConnectionException(_connectionId, _message, _innerException);
  44. subject.ConnectionId.Should().BeSameAs(_connectionId);
  45. subject.InnerException.Should().BeSameAs(_innerException);
  46. subject.Message.Should().BeSameAs(_message);
  47. }
  48. [Test]
  49. public void Serialization_should_work()
  50. {
  51. var subject = new MongoConnectionException(_connectionId, _message, _innerException);
  52. var formatter = new BinaryFormatter();
  53. using (var stream = new MemoryStream())
  54. {
  55. formatter.Serialize(stream, subject);
  56. stream.Position = 0;
  57. var rehydrated = (MongoConnectionException)formatter.Deserialize(stream);
  58. rehydrated.ConnectionId.Should().Be(subject.ConnectionId);
  59. rehydrated.Message.Should().Be(subject.Message);
  60. rehydrated.InnerException.Message.Should().Be(subject.InnerException.Message); // Exception does not override Equals
  61. }
  62. }
  63. }
  64. }