PageRenderTime 81ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MongoDB.Driver.Tests/MongoUrlTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 141 lines | 121 code | 5 blank | 15 comment | 0 complexity | f58e9de915584d882f180885acfb3412 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-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.Collections.Generic;
  17. using System.Linq;
  18. using MongoDB.Bson;
  19. using MongoDB.Driver;
  20. using NUnit.Framework;
  21. namespace MongoDB.Driver.Tests
  22. {
  23. [TestFixture]
  24. public class MongoUrlTests
  25. {
  26. [Test]
  27. public void TestAll()
  28. {
  29. var readPreference = new ReadPreference(ReadPreferenceMode.Secondary, new[] { new TagSet(new[] { new Tag("dc", "1") }) });
  30. var authMechanismProperties = new Dictionary<string, string>
  31. {
  32. { "SERVICE_NAME", "other" },
  33. { "CANONICALIZE_HOST_NAME", "true" }
  34. };
  35. var built = new MongoUrlBuilder()
  36. {
  37. AuthenticationMechanism = "GSSAPI",
  38. AuthenticationMechanismProperties = authMechanismProperties,
  39. AuthenticationSource = "db",
  40. ConnectionMode = ConnectionMode.ReplicaSet,
  41. ConnectTimeout = TimeSpan.FromSeconds(1),
  42. DatabaseName = "database",
  43. FSync = true,
  44. GuidRepresentation = GuidRepresentation.PythonLegacy,
  45. IPv6 = true,
  46. Journal = true,
  47. MaxConnectionIdleTime = TimeSpan.FromSeconds(2),
  48. MaxConnectionLifeTime = TimeSpan.FromSeconds(3),
  49. MaxConnectionPoolSize = 4,
  50. MinConnectionPoolSize = 5,
  51. Password = "password",
  52. ReadPreference = readPreference,
  53. ReplicaSetName = "name",
  54. LocalThreshold = TimeSpan.FromSeconds(6),
  55. Server = new MongoServerAddress("host"),
  56. ServerSelectionTimeout = TimeSpan.FromSeconds(10),
  57. SocketTimeout = TimeSpan.FromSeconds(7),
  58. Username = "username",
  59. UseSsl = true,
  60. VerifySslCertificate = false,
  61. W = 2,
  62. WaitQueueSize = 123,
  63. WaitQueueTimeout = TimeSpan.FromSeconds(8),
  64. WTimeout = TimeSpan.FromSeconds(9)
  65. };
  66. var connectionString = "mongodb://username:password@host/database?" + string.Join(";", new[] {
  67. "authMechanism=GSSAPI",
  68. "authMechanismProperties=SERVICE_NAME:other,CANONICALIZE_HOST_NAME:true",
  69. "authSource=db",
  70. "ipv6=true",
  71. "ssl=true", // UseSsl
  72. "sslVerifyCertificate=false", // VerifySslCertificate
  73. "connect=replicaSet",
  74. "replicaSet=name",
  75. "readPreference=secondary;readPreferenceTags=dc:1",
  76. "fsync=true",
  77. "journal=true",
  78. "w=2",
  79. "wtimeout=9s",
  80. "connectTimeout=1s",
  81. "maxIdleTime=2s",
  82. "maxLifeTime=3s",
  83. "maxPoolSize=4",
  84. "minPoolSize=5",
  85. "localThreshold=6s",
  86. "serverSelectionTimeout=10s",
  87. "socketTimeout=7s",
  88. "waitQueueSize=123",
  89. "waitQueueTimeout=8s",
  90. "uuidRepresentation=pythonLegacy"
  91. });
  92. foreach (var url in EnumerateBuiltAndParsedUrls(built, connectionString))
  93. {
  94. Assert.AreEqual("GSSAPI", url.AuthenticationMechanism);
  95. CollectionAssert.AreEqual(authMechanismProperties, url.AuthenticationMechanismProperties);
  96. Assert.AreEqual("db", url.AuthenticationSource);
  97. Assert.AreEqual(123, url.ComputedWaitQueueSize);
  98. Assert.AreEqual(ConnectionMode.ReplicaSet, url.ConnectionMode);
  99. Assert.AreEqual(TimeSpan.FromSeconds(1), url.ConnectTimeout);
  100. Assert.AreEqual("database", url.DatabaseName);
  101. Assert.AreEqual(true, url.FSync);
  102. Assert.AreEqual(GuidRepresentation.PythonLegacy, url.GuidRepresentation);
  103. Assert.AreEqual(true, url.IPv6);
  104. Assert.AreEqual(true, url.Journal);
  105. Assert.AreEqual(TimeSpan.FromSeconds(2), url.MaxConnectionIdleTime);
  106. Assert.AreEqual(TimeSpan.FromSeconds(3), url.MaxConnectionLifeTime);
  107. Assert.AreEqual(4, url.MaxConnectionPoolSize);
  108. Assert.AreEqual(5, url.MinConnectionPoolSize);
  109. Assert.AreEqual("password", url.Password);
  110. Assert.AreEqual(readPreference, url.ReadPreference);
  111. Assert.AreEqual("name", url.ReplicaSetName);
  112. Assert.AreEqual(TimeSpan.FromSeconds(6), url.LocalThreshold);
  113. Assert.AreEqual(new MongoServerAddress("host", 27017), url.Server);
  114. Assert.AreEqual(TimeSpan.FromSeconds(10), url.ServerSelectionTimeout);
  115. Assert.AreEqual(TimeSpan.FromSeconds(7), url.SocketTimeout);
  116. Assert.AreEqual("username", url.Username);
  117. Assert.AreEqual(true, url.UseSsl);
  118. Assert.AreEqual(false, url.VerifySslCertificate);
  119. Assert.AreEqual(2, ((WriteConcern.WCount)url.W).Value);
  120. Assert.AreEqual(0.0, url.WaitQueueMultiple);
  121. Assert.AreEqual(123, url.WaitQueueSize);
  122. Assert.AreEqual(TimeSpan.FromSeconds(8), url.WaitQueueTimeout);
  123. Assert.AreEqual(TimeSpan.FromSeconds(9), url.WTimeout);
  124. Assert.AreEqual(connectionString, url.ToString());
  125. }
  126. }
  127. // private methods
  128. private IEnumerable<MongoUrl> EnumerateBuiltAndParsedUrls(
  129. MongoUrlBuilder built,
  130. string connectionString)
  131. {
  132. yield return built.ToMongoUrl();
  133. yield return new MongoUrl(connectionString);
  134. }
  135. }
  136. }