PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/BsonUnitTests/DefaultSerializer/Serializers/CollectionSerializerGenericTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 542 lines | 477 code | 51 blank | 14 comment | 0 complexity | 04621de27fe6831afd03dc8daf862ccc MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-2012 10gen 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 System.Text;
  19. using NUnit.Framework;
  20. using MongoDB.Bson;
  21. using MongoDB.Bson.Serialization;
  22. using MongoDB.Bson.Serialization.Attributes;
  23. namespace MongoDB.BsonUnitTests.Serialization.CollectionSerializersGeneric
  24. {
  25. [BsonDiscriminator("CollectionSerializersGeneric.C")] // "C" is an ambiguous discriminator when nominalType is System.Object
  26. public class C
  27. {
  28. public string P { get; set; }
  29. }
  30. [TestFixture]
  31. public class EnumerableSerializerTests
  32. {
  33. public class T
  34. {
  35. public List<object> L { get; set; }
  36. public ICollection<object> IC { get; set; }
  37. public IEnumerable<object> IE { get; set; }
  38. public IList<object> IL { get; set; }
  39. public Queue<object> Q { get; set; }
  40. public Stack<object> S { get; set; }
  41. public HashSet<object> H { get; set; }
  42. public LinkedList<object> LL { get; set; }
  43. }
  44. [Test]
  45. public void TestNull()
  46. {
  47. var obj = new T { L = null, IC = null, IE = null, IL = null, Q = null, S = null, H = null, LL = null };
  48. var json = obj.ToJson();
  49. var rep = "null";
  50. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  51. Assert.AreEqual(expected, json);
  52. var bson = obj.ToBson();
  53. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  54. Assert.IsNull(rehydrated.L);
  55. Assert.IsNull(rehydrated.Q);
  56. Assert.IsNull(rehydrated.S);
  57. Assert.IsNull(rehydrated.IC);
  58. Assert.IsNull(rehydrated.IE);
  59. Assert.IsNull(rehydrated.IL);
  60. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  61. }
  62. [Test]
  63. public void TestEmpty()
  64. {
  65. var list = new List<object>();
  66. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<object>(list), S = new Stack<object>(list), H = new HashSet<object>(list), LL = new LinkedList<object>(list) };
  67. var json = obj.ToJson();
  68. var rep = "[]";
  69. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  70. Assert.AreEqual(expected, json);
  71. var bson = obj.ToBson();
  72. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  73. Assert.IsInstanceOf<List<object>>(rehydrated.L);
  74. Assert.IsInstanceOf<Queue<object>>(rehydrated.Q);
  75. Assert.IsInstanceOf<Stack<object>>(rehydrated.S);
  76. Assert.IsInstanceOf<List<object>>(rehydrated.IC);
  77. Assert.IsInstanceOf<List<object>>(rehydrated.IE);
  78. Assert.IsInstanceOf<List<object>>(rehydrated.IL);
  79. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  80. }
  81. [Test]
  82. public void TestOneC()
  83. {
  84. var list = new List<object>(new[] { new C { P = "x" } });
  85. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<object>(list), S = new Stack<object>(list), H = new HashSet<object>(list), LL = new LinkedList<object>(list) };
  86. var json = obj.ToJson();
  87. var rep = "[{ '_t' : 'CollectionSerializersGeneric.C', 'P' : 'x' }]";
  88. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  89. Assert.AreEqual(expected, json);
  90. var bson = obj.ToBson();
  91. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  92. Assert.IsInstanceOf<List<object>>(rehydrated.L);
  93. Assert.IsInstanceOf<Queue<object>>(rehydrated.Q);
  94. Assert.IsInstanceOf<Stack<object>>(rehydrated.S);
  95. Assert.IsInstanceOf<List<object>>(rehydrated.IC);
  96. Assert.IsInstanceOf<List<object>>(rehydrated.IE);
  97. Assert.IsInstanceOf<List<object>>(rehydrated.IL);
  98. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  99. }
  100. [Test]
  101. public void TestOneInt()
  102. {
  103. var list = new List<object>(new object[] { 1 });
  104. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<object>(list), S = new Stack<object>(list), H = new HashSet<object>(list), LL = new LinkedList<object>(list) };
  105. var json = obj.ToJson();
  106. var rep = "[1]";
  107. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  108. Assert.AreEqual(expected, json);
  109. var bson = obj.ToBson();
  110. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  111. Assert.IsInstanceOf<List<object>>(rehydrated.L);
  112. Assert.IsInstanceOf<Queue<object>>(rehydrated.Q);
  113. Assert.IsInstanceOf<Stack<object>>(rehydrated.S);
  114. Assert.IsInstanceOf<List<object>>(rehydrated.IC);
  115. Assert.IsInstanceOf<List<object>>(rehydrated.IE);
  116. Assert.IsInstanceOf<List<object>>(rehydrated.IL);
  117. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  118. }
  119. [Test]
  120. public void TestOneString()
  121. {
  122. var list = new List<object>(new[] { "x" });
  123. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<object>(list), S = new Stack<object>(list), H = new HashSet<object>(list), LL = new LinkedList<object>(list) };
  124. var json = obj.ToJson();
  125. var rep = "['x']";
  126. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  127. Assert.AreEqual(expected, json);
  128. var bson = obj.ToBson();
  129. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  130. Assert.IsInstanceOf<List<object>>(rehydrated.L);
  131. Assert.IsInstanceOf<Queue<object>>(rehydrated.Q);
  132. Assert.IsInstanceOf<Stack<object>>(rehydrated.S);
  133. Assert.IsInstanceOf<List<object>>(rehydrated.IC);
  134. Assert.IsInstanceOf<List<object>>(rehydrated.IE);
  135. Assert.IsInstanceOf<List<object>>(rehydrated.IL);
  136. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  137. }
  138. [Test]
  139. public void TestTwoCs()
  140. {
  141. var list = new List<object>(new[] { new C { P = "x" }, new C { P = "y" } });
  142. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<object>(list), S = new Stack<object>(list), H = new HashSet<object>(list), LL = new LinkedList<object>(list) };
  143. var json = obj.ToJson();
  144. var rep = "[{ '_t' : 'CollectionSerializersGeneric.C', 'P' : 'x' }, { '_t' : 'CollectionSerializersGeneric.C', 'P' : 'y' }]";
  145. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  146. Assert.AreEqual(expected, json);
  147. var bson = obj.ToBson();
  148. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  149. Assert.IsInstanceOf<List<object>>(rehydrated.L);
  150. Assert.IsInstanceOf<Queue<object>>(rehydrated.Q);
  151. Assert.IsInstanceOf<Stack<object>>(rehydrated.S);
  152. Assert.IsInstanceOf<List<object>>(rehydrated.IC);
  153. Assert.IsInstanceOf<List<object>>(rehydrated.IE);
  154. Assert.IsInstanceOf<List<object>>(rehydrated.IL);
  155. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  156. }
  157. [Test]
  158. public void TestTwoInts()
  159. {
  160. var list = new List<object>(new object[] { 1, 2 });
  161. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<object>(list), S = new Stack<object>(list), H = new HashSet<object>(list), LL = new LinkedList<object>(list) };
  162. var json = obj.ToJson();
  163. var rep = "[1, 2]";
  164. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  165. Assert.AreEqual(expected, json);
  166. var bson = obj.ToBson();
  167. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  168. Assert.IsInstanceOf<List<object>>(rehydrated.L);
  169. Assert.IsInstanceOf<Queue<object>>(rehydrated.Q);
  170. Assert.IsInstanceOf<Stack<object>>(rehydrated.S);
  171. Assert.IsInstanceOf<List<object>>(rehydrated.IC);
  172. Assert.IsInstanceOf<List<object>>(rehydrated.IE);
  173. Assert.IsInstanceOf<List<object>>(rehydrated.IL);
  174. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  175. }
  176. [Test]
  177. public void TestTwoStrings()
  178. {
  179. var list = new List<object>(new[] { "x", "y" });
  180. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<object>(list), S = new Stack<object>(list), H = new HashSet<object>(list), LL = new LinkedList<object>(list) };
  181. var json = obj.ToJson();
  182. var rep = "['x', 'y']";
  183. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  184. Assert.AreEqual(expected, json);
  185. var bson = obj.ToBson();
  186. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  187. Assert.IsInstanceOf<List<object>>(rehydrated.L);
  188. Assert.IsInstanceOf<Queue<object>>(rehydrated.Q);
  189. Assert.IsInstanceOf<Stack<object>>(rehydrated.S);
  190. Assert.IsInstanceOf<List<object>>(rehydrated.IC);
  191. Assert.IsInstanceOf<List<object>>(rehydrated.IE);
  192. Assert.IsInstanceOf<List<object>>(rehydrated.IL);
  193. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  194. }
  195. [Test]
  196. public void TestMixedPrimitiveTypes()
  197. {
  198. var dateTime = DateTime.SpecifyKind(new DateTime(2010, 1, 1, 11, 22, 33), DateTimeKind.Utc);
  199. var isoDate = string.Format("ISODate(\"{0}\")", dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
  200. var guid = Guid.Empty;
  201. var objectId = ObjectId.Empty;
  202. var list = new List<object>(new object[] { true, dateTime, 1.5, 1, 2L, guid, objectId, "x" });
  203. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<object>(list), S = new Stack<object>(list), H = new HashSet<object>(list), LL = new LinkedList<object>(list) };
  204. var json = obj.ToJson();
  205. var rep = "[true, #Date, 1.5, 1, NumberLong(2), #Guid, #ObjectId, 'x']";
  206. rep = rep.Replace("#Date", isoDate);
  207. rep = rep.Replace("#Guid", "CSUUID('00000000-0000-0000-0000-000000000000')");
  208. rep = rep.Replace("#ObjectId", "ObjectId('000000000000000000000000')");
  209. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  210. Assert.AreEqual(expected, json);
  211. var bson = obj.ToBson();
  212. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  213. Assert.IsInstanceOf<List<object>>(rehydrated.L);
  214. Assert.IsInstanceOf<Queue<object>>(rehydrated.Q);
  215. Assert.IsInstanceOf<Stack<object>>(rehydrated.S);
  216. Assert.IsInstanceOf<List<object>>(rehydrated.IC);
  217. Assert.IsInstanceOf<List<object>>(rehydrated.IE);
  218. Assert.IsInstanceOf<List<object>>(rehydrated.IL);
  219. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  220. }
  221. }
  222. [TestFixture]
  223. public class EnumerableSerializerNominalTypeObjectTests
  224. {
  225. public class T
  226. {
  227. public object L { get; set; }
  228. public object Q { get; set; }
  229. public object S { get; set; }
  230. public object H { get; set; }
  231. public object LL { get; set; }
  232. }
  233. [Test]
  234. public void TestNull()
  235. {
  236. var obj = new T { L = null, Q = null, S = null, H = null, LL = null };
  237. var json = obj.ToJson();
  238. var rep = "null";
  239. var expected = "{ 'L' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  240. Assert.AreEqual(expected, json);
  241. var bson = obj.ToBson();
  242. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  243. Assert.IsNull(rehydrated.L);
  244. Assert.IsNull(rehydrated.Q);
  245. Assert.IsNull(rehydrated.S);
  246. Assert.IsNull(rehydrated.H);
  247. Assert.IsNull(rehydrated.LL);
  248. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  249. }
  250. [Test]
  251. public void TestEmpty()
  252. {
  253. var list = new List<object>();
  254. var obj = new T { L = list, Q = new Queue<object>(list), S = new Stack<object>(list), H = new HashSet<object>(list), LL = new LinkedList<object>(list) };
  255. var json = obj.ToJson();
  256. var rep = "[]";
  257. var expected = "{ 'L' : { '_t' : 'System.Collections.Generic.List`1[System.Object]', '_v' : #R }, 'Q' : { '_t' : 'System.Collections.Generic.Queue`1[System.Object]', '_v' : #R }, 'S' : { '_t' : 'System.Collections.Generic.Stack`1[System.Object]', '_v' : #R }, 'H' : { '_t' : 'System.Collections.Generic.HashSet`1[System.Object]', '_v' : #R }, 'LL' : { '_t' : 'System.Collections.Generic.LinkedList`1[System.Object]', '_v' : #R } }".Replace("#R", rep).Replace("'", "\"");
  258. Assert.AreEqual(expected, json);
  259. var bson = obj.ToBson();
  260. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  261. Assert.IsInstanceOf<List<object>>(rehydrated.L);
  262. Assert.IsInstanceOf<Queue<object>>(rehydrated.Q);
  263. Assert.IsInstanceOf<Stack<object>>(rehydrated.S);
  264. Assert.IsInstanceOf<HashSet<object>>(rehydrated.H);
  265. Assert.IsInstanceOf<LinkedList<object>>(rehydrated.LL);
  266. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  267. }
  268. [Test]
  269. public void TestOneInt()
  270. {
  271. var list = new List<object>(new object[] { 1 });
  272. var obj = new T { L = list, Q = new Queue<object>(list), S = new Stack<object>(list), H = new HashSet<object>(list), LL = new LinkedList<object>(list) };
  273. var json = obj.ToJson();
  274. var rep = "[1]";
  275. var expected = "{ 'L' : { '_t' : 'System.Collections.Generic.List`1[System.Object]', '_v' : #R }, 'Q' : { '_t' : 'System.Collections.Generic.Queue`1[System.Object]', '_v' : #R }, 'S' : { '_t' : 'System.Collections.Generic.Stack`1[System.Object]', '_v' : #R }, 'H' : { '_t' : 'System.Collections.Generic.HashSet`1[System.Object]', '_v' : #R }, 'LL' : { '_t' : 'System.Collections.Generic.LinkedList`1[System.Object]', '_v' : #R } }".Replace("#R", rep).Replace("'", "\"");
  276. Assert.AreEqual(expected, json);
  277. var bson = obj.ToBson();
  278. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  279. Assert.IsInstanceOf<List<object>>(rehydrated.L);
  280. Assert.IsInstanceOf<Queue<object>>(rehydrated.Q);
  281. Assert.IsInstanceOf<Stack<object>>(rehydrated.S);
  282. Assert.IsInstanceOf<HashSet<object>>(rehydrated.H);
  283. Assert.IsInstanceOf<LinkedList<object>>(rehydrated.LL);
  284. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  285. }
  286. }
  287. [TestFixture]
  288. public class EnumerableSerializerWithItemSerializationOptionsTests
  289. {
  290. public enum E
  291. {
  292. None,
  293. A,
  294. B
  295. }
  296. public class T
  297. {
  298. [BsonRepresentation(BsonType.String)]
  299. public List<E> L { get; set; }
  300. [BsonRepresentation(BsonType.String)]
  301. public ICollection<E> IC { get; set; }
  302. [BsonRepresentation(BsonType.String)]
  303. public IEnumerable<E> IE { get; set; }
  304. [BsonRepresentation(BsonType.String)]
  305. public IList<E> IL { get; set; }
  306. [BsonRepresentation(BsonType.String)]
  307. public Queue<E> Q { get; set; }
  308. [BsonRepresentation(BsonType.String)]
  309. public Stack<E> S { get; set; }
  310. [BsonRepresentation(BsonType.String)]
  311. public HashSet<E> H { get; set; }
  312. [BsonRepresentation(BsonType.String)]
  313. public LinkedList<E> LL { get; set; }
  314. }
  315. [Test]
  316. public void TestNull()
  317. {
  318. var obj = new T { L = null, IC = null, IE = null, IL = null, Q = null, S = null, H = null, LL = null };
  319. var json = obj.ToJson();
  320. var rep = "null";
  321. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  322. Assert.AreEqual(expected, json);
  323. var bson = obj.ToBson();
  324. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  325. Assert.IsNull(rehydrated.L);
  326. Assert.IsNull(rehydrated.Q);
  327. Assert.IsNull(rehydrated.S);
  328. Assert.IsNull(rehydrated.IC);
  329. Assert.IsNull(rehydrated.IE);
  330. Assert.IsNull(rehydrated.IL);
  331. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  332. }
  333. [Test]
  334. public void TestEmpty()
  335. {
  336. var list = new List<E>();
  337. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<E>(list), S = new Stack<E>(list), H = new HashSet<E>(list), LL = new LinkedList<E>(list) };
  338. var json = obj.ToJson();
  339. var rep = "[]";
  340. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  341. Assert.AreEqual(expected, json);
  342. var bson = obj.ToBson();
  343. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  344. Assert.IsInstanceOf<List<E>>(rehydrated.L);
  345. Assert.IsInstanceOf<Queue<E>>(rehydrated.Q);
  346. Assert.IsInstanceOf<Stack<E>>(rehydrated.S);
  347. Assert.IsInstanceOf<List<E>>(rehydrated.IC);
  348. Assert.IsInstanceOf<List<E>>(rehydrated.IE);
  349. Assert.IsInstanceOf<List<E>>(rehydrated.IL);
  350. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  351. }
  352. [Test]
  353. public void TestOneE()
  354. {
  355. var list = new List<E>(new[] { E.A });
  356. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<E>(list), S = new Stack<E>(list), H = new HashSet<E>(list), LL = new LinkedList<E>(list) };
  357. var json = obj.ToJson();
  358. var rep = "['A']";
  359. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  360. Assert.AreEqual(expected, json);
  361. var bson = obj.ToBson();
  362. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  363. Assert.IsInstanceOf<List<E>>(rehydrated.L);
  364. Assert.IsInstanceOf<Queue<E>>(rehydrated.Q);
  365. Assert.IsInstanceOf<Stack<E>>(rehydrated.S);
  366. Assert.IsInstanceOf<List<E>>(rehydrated.IC);
  367. Assert.IsInstanceOf<List<E>>(rehydrated.IE);
  368. Assert.IsInstanceOf<List<E>>(rehydrated.IL);
  369. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  370. }
  371. [Test]
  372. public void TestTwoEs()
  373. {
  374. var list = new List<E>(new[] { E.A, E.B });
  375. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<E>(list), S = new Stack<E>(list), H = new HashSet<E>(list), LL = new LinkedList<E>(list) };
  376. var json = obj.ToJson();
  377. var rep = "['A', 'B']";
  378. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  379. Assert.AreEqual(expected, json);
  380. var bson = obj.ToBson();
  381. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  382. Assert.IsInstanceOf<List<E>>(rehydrated.L);
  383. Assert.IsInstanceOf<Queue<E>>(rehydrated.Q);
  384. Assert.IsInstanceOf<Stack<E>>(rehydrated.S);
  385. Assert.IsInstanceOf<List<E>>(rehydrated.IC);
  386. Assert.IsInstanceOf<List<E>>(rehydrated.IE);
  387. Assert.IsInstanceOf<List<E>>(rehydrated.IL);
  388. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  389. }
  390. }
  391. [TestFixture]
  392. public class EnumerableSerializerWithStringToObjectIdTests
  393. {
  394. public class T
  395. {
  396. [BsonRepresentation(BsonType.ObjectId)]
  397. public List<string> L { get; set; }
  398. [BsonRepresentation(BsonType.ObjectId)]
  399. public ICollection<string> IC { get; set; }
  400. [BsonRepresentation(BsonType.ObjectId)]
  401. public IEnumerable<string> IE { get; set; }
  402. [BsonRepresentation(BsonType.ObjectId)]
  403. public IList<string> IL { get; set; }
  404. [BsonRepresentation(BsonType.ObjectId)]
  405. public Queue<string> Q { get; set; }
  406. [BsonRepresentation(BsonType.ObjectId)]
  407. public Stack<string> S { get; set; }
  408. [BsonRepresentation(BsonType.ObjectId)]
  409. public HashSet<string> H { get; set; }
  410. [BsonRepresentation(BsonType.ObjectId)]
  411. public LinkedList<string> LL { get; set; }
  412. }
  413. private static string id1 = "123456789012345678901234";
  414. private static string id2 = "432109876543210987654321";
  415. [Test]
  416. public void TestNull()
  417. {
  418. var obj = new T { L = null, IC = null, IE = null, IL = null, Q = null, S = null, H = null, LL = null };
  419. var json = obj.ToJson();
  420. var rep = "null";
  421. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  422. Assert.AreEqual(expected, json);
  423. var bson = obj.ToBson();
  424. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  425. Assert.IsNull(rehydrated.L);
  426. Assert.IsNull(rehydrated.Q);
  427. Assert.IsNull(rehydrated.S);
  428. Assert.IsNull(rehydrated.IC);
  429. Assert.IsNull(rehydrated.IE);
  430. Assert.IsNull(rehydrated.IL);
  431. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  432. }
  433. [Test]
  434. public void TestEmpty()
  435. {
  436. var list = new List<string>();
  437. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<string>(list), S = new Stack<string>(list), H = new HashSet<string>(list), LL = new LinkedList<string>(list) };
  438. var json = obj.ToJson();
  439. var rep = "[]";
  440. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  441. Assert.AreEqual(expected, json);
  442. var bson = obj.ToBson();
  443. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  444. Assert.IsInstanceOf<List<string>>(rehydrated.L);
  445. Assert.IsInstanceOf<Queue<string>>(rehydrated.Q);
  446. Assert.IsInstanceOf<Stack<string>>(rehydrated.S);
  447. Assert.IsInstanceOf<List<string>>(rehydrated.IC);
  448. Assert.IsInstanceOf<List<string>>(rehydrated.IE);
  449. Assert.IsInstanceOf<List<string>>(rehydrated.IL);
  450. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  451. }
  452. [Test]
  453. public void TestOneString()
  454. {
  455. var list = new List<string>(new[] { id1 });
  456. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<string>(list), S = new Stack<string>(list), H = new HashSet<string>(list), LL = new LinkedList<string>(list) };
  457. var json = obj.ToJson();
  458. var rep = "[ObjectId(\"123456789012345678901234\")]";
  459. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  460. Assert.AreEqual(expected, json);
  461. var bson = obj.ToBson();
  462. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  463. Assert.IsInstanceOf<List<string>>(rehydrated.L);
  464. Assert.IsInstanceOf<Queue<string>>(rehydrated.Q);
  465. Assert.IsInstanceOf<Stack<string>>(rehydrated.S);
  466. Assert.IsInstanceOf<List<string>>(rehydrated.IC);
  467. Assert.IsInstanceOf<List<string>>(rehydrated.IE);
  468. Assert.IsInstanceOf<List<string>>(rehydrated.IL);
  469. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  470. }
  471. [Test]
  472. public void TestTwoStrings()
  473. {
  474. var list = new List<string>(new[] { id1, id2 });
  475. var obj = new T { L = list, IC = list, IE = list, IL = list, Q = new Queue<string>(list), S = new Stack<string>(list), H = new HashSet<string>(list), LL = new LinkedList<string>(list) };
  476. var json = obj.ToJson();
  477. var rep = "[ObjectId(\"123456789012345678901234\"), ObjectId(\"432109876543210987654321\")]";
  478. var expected = "{ 'L' : #R, 'IC' : #R, 'IE' : #R, 'IL' : #R, 'Q' : #R, 'S' : #R, 'H' : #R, 'LL' : #R }".Replace("#R", rep).Replace("'", "\"");
  479. Assert.AreEqual(expected, json);
  480. var bson = obj.ToBson();
  481. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  482. Assert.IsInstanceOf<List<string>>(rehydrated.L);
  483. Assert.IsInstanceOf<Queue<string>>(rehydrated.Q);
  484. Assert.IsInstanceOf<Stack<string>>(rehydrated.S);
  485. Assert.IsInstanceOf<List<string>>(rehydrated.IC);
  486. Assert.IsInstanceOf<List<string>>(rehydrated.IE);
  487. Assert.IsInstanceOf<List<string>>(rehydrated.IL);
  488. Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
  489. }
  490. }
  491. }