|
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 | | - |
16 | | -using System.Collections.Generic; |
17 | | -using System.Collections.ObjectModel; |
18 | | -using MongoDB.Bson; |
19 | | -using MongoDB.Bson.Serialization; |
20 | | -using NUnit.Framework; |
21 | | - |
22 | | -namespace MongoDB.Bson.Tests.Jira.CSharp515 |
23 | | -{ |
24 | | - [TestFixture] |
25 | | - public class CSharp515Tests |
26 | | - { |
27 | | - public class C |
28 | | - { |
29 | | - public int Id { get; set; } |
30 | | - public ReadOnlyCollection<int> R { get; set; } |
31 | | - public S<int> S { get; set; } |
32 | | - public ReadOnlyCollection<int> RS { get; set; } // actual value will be of type S<int> |
33 | | - public object OR { get; set; } // actual value will be of type ReadOnlyCollection<int> |
34 | | - public object OS { get; set; } // actual value will be of type S<int> |
35 | | - } |
36 | | - |
37 | | - public class S<T> : ReadOnlyCollection<T> |
38 | | - { |
39 | | - public S(IList<T> list) |
40 | | - : base(list) |
41 | | - { |
42 | | - } |
| 1 | +/* Copyright 2010-2016 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.Collections.Generic; |
| 17 | +using System.Collections.ObjectModel; |
| 18 | +using System.Reflection; |
| 19 | +using MongoDB.Bson; |
| 20 | +using MongoDB.Bson.Serialization; |
| 21 | +using NUnit.Framework; |
| 22 | + |
| 23 | +namespace MongoDB.Bson.Tests.Jira.CSharp515 |
| 24 | +{ |
| 25 | + [TestFixture] |
| 26 | + public class CSharp515Tests |
| 27 | + { |
| 28 | + public class C |
| 29 | + { |
| 30 | + public int Id { get; set; } |
| 31 | + public ReadOnlyCollection<int> R { get; set; } |
| 32 | + public S<int> S { get; set; } |
| 33 | + public ReadOnlyCollection<int> RS { get; set; } // actual value will be of type S<int> |
| 34 | + public object OR { get; set; } // actual value will be of type ReadOnlyCollection<int> |
| 35 | + public object OS { get; set; } // actual value will be of type S<int> |
| 36 | + } |
| 37 | + |
| 38 | + public class S<T> : ReadOnlyCollection<T> |
| 39 | + { |
| 40 | + public S(IList<T> list) |
| 41 | + : base(list) |
| 42 | + { |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static readonly AssemblyName __assemblyName = Assembly.GetExecutingAssembly().GetName(); |
| 47 | + private static readonly bool __assemblyIsSigned = __assemblyName.GetPublicKey().Length > 0; |
| 48 | + private static readonly string __discriminatorAssemblyName = __assemblyIsSigned ? __assemblyName.FullName : __assemblyName.Name; |
| 49 | + |
| 50 | + private string _jsonTemplate = ("{ '_id' : 1, 'R' : #V, 'S' : #V, 'RS' : { '_t' : 'S`1', '_v' : #V }, 'OR' : { '_t' : 'System.Collections.ObjectModel.ReadOnlyCollection`1[System.Int32]', '_v' : #V }, 'OS' : { '_t' : 'MongoDB.Bson.Tests.Jira.CSharp515.CSharp515Tests+S`1[System.Int32], " + __discriminatorAssemblyName + "', '_v' : #V } }").Replace("'", "\""); |
| 51 | + |
| 52 | + [Test] |
| 53 | + public void TestNull() |
| 54 | + { |
| 55 | + var c = new C { Id = 1, R = null, S = null, RS = null, OR = null, OS = null }; |
| 56 | + |
| 57 | + var json = "{ '_id' : 1, 'R' : null, 'S' : null, 'RS' : null, 'OR' : null, 'OS' : null }".Replace("'", "\""); |
| 58 | + Assert.AreEqual(json, c.ToJson()); |
| 59 | + |
| 60 | + var rehydrated = BsonSerializer.Deserialize<C>(c.ToBson()); |
| 61 | + Assert.AreEqual(c.Id, rehydrated.Id); |
| 62 | + Assert.AreEqual(null, rehydrated.R); |
| 63 | + Assert.AreEqual(null, rehydrated.S); |
| 64 | + Assert.AreEqual(null, rehydrated.RS); |
| 65 | + Assert.AreEqual(null, rehydrated.OR); |
| 66 | + Assert.AreEqual(null, rehydrated.OS); |
| 67 | + } |
| 68 | + |
| 69 | + [Test] |
| 70 | + public void TestLength0() |
| 71 | + { |
| 72 | + var list = new List<int>(); |
| 73 | + var r = new ReadOnlyCollection<int>(list); |
| 74 | + var s = new S<int>(list); |
| 75 | + var c = new C { Id = 1, R = r, S = s, RS = s, OR = r, OS = s }; |
| 76 | + |
| 77 | + var json = _jsonTemplate.Replace("#V", "[]"); |
| 78 | + Assert.AreEqual(json, c.ToJson()); |
| 79 | + |
| 80 | + var rehydrated = BsonSerializer.Deserialize<C>(c.ToBson()); |
| 81 | + Assert.AreEqual(c.Id, rehydrated.Id); |
| 82 | + Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.R); |
| 83 | + Assert.IsInstanceOf<S<int>>(rehydrated.S); |
| 84 | + Assert.IsInstanceOf<S<int>>(rehydrated.RS); |
| 85 | + Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.OR); |
| 86 | + Assert.IsInstanceOf<S<int>>(rehydrated.OS); |
| 87 | + Assert.AreEqual(0, rehydrated.R.Count); |
| 88 | + Assert.AreEqual(0, rehydrated.S.Count); |
| 89 | + Assert.AreEqual(0, rehydrated.RS.Count); |
| 90 | + Assert.AreEqual(0, ((ReadOnlyCollection<int>)rehydrated.OR).Count); |
| 91 | + Assert.AreEqual(0, ((S<int>)rehydrated.OS).Count); |
| 92 | + } |
| 93 | + |
| 94 | + [Test] |
| 95 | + public void TestLength1() |
| 96 | + { |
| 97 | + var list = new List<int>() { 1 }; |
| 98 | + var r = new ReadOnlyCollection<int>(list); |
| 99 | + var s = new S<int>(list); |
| 100 | + var c = new C { Id = 1, R = r, S = s, RS = s, OR = r, OS = s }; |
| 101 | + |
| 102 | + var json = _jsonTemplate.Replace("#V", "[1]"); |
| 103 | + Assert.AreEqual(json, c.ToJson()); |
| 104 | + |
| 105 | + var rehydrated = BsonSerializer.Deserialize<C>(c.ToBson()); |
| 106 | + Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.R); |
| 107 | + Assert.IsInstanceOf<S<int>>(rehydrated.S); |
| 108 | + Assert.IsInstanceOf<S<int>>(rehydrated.RS); |
| 109 | + Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.OR); |
| 110 | + Assert.IsInstanceOf<S<int>>(rehydrated.OS); |
| 111 | + Assert.AreEqual(c.Id, rehydrated.Id); |
| 112 | + Assert.AreEqual(1, rehydrated.R.Count); |
| 113 | + Assert.AreEqual(1, rehydrated.S.Count); |
| 114 | + Assert.AreEqual(1, rehydrated.RS.Count); |
| 115 | + Assert.AreEqual(1, ((ReadOnlyCollection<int>)rehydrated.OR).Count); |
| 116 | + Assert.AreEqual(1, ((S<int>)rehydrated.OS).Count); |
| 117 | + Assert.AreEqual(1, rehydrated.R[0]); |
| 118 | + Assert.AreEqual(1, rehydrated.S[0]); |
| 119 | + Assert.AreEqual(1, rehydrated.RS[0]); |
| 120 | + Assert.AreEqual(1, ((ReadOnlyCollection<int>)rehydrated.OR)[0]); |
| 121 | + Assert.AreEqual(1, ((S<int>)rehydrated.OS)[0]); |
43 | 122 | } |
44 | 123 |
|
45 | | - private static readonly string STypeAssemblyName = typeof(S<>).Assembly.GetName().GetPublicKey().Length > 0 ? typeof(S<>).Assembly.FullName : typeof(S<>).Assembly.GetName().Name; |
46 | | - |
47 | | - private string _jsonTemplate = ("{ '_id' : 1, 'R' : #V, 'S' : #V, 'RS' : { '_t' : 'S`1', '_v' : #V }, 'OR' : { '_t' : 'System.Collections.ObjectModel.ReadOnlyCollection`1[System.Int32]', '_v' : #V }, 'OS' : { '_t' : 'MongoDB.Bson.Tests.Jira.CSharp515.CSharp515Tests+S`1[System.Int32], " + STypeAssemblyName + "', '_v' : #V } }").Replace("'", "\""); |
48 | | - |
49 | | - [Test] |
50 | | - public void TestNull() |
51 | | - { |
52 | | - var c = new C { Id = 1, R = null, S = null, RS = null, OR = null, OS = null }; |
53 | | - |
54 | | - var json = "{ '_id' : 1, 'R' : null, 'S' : null, 'RS' : null, 'OR' : null, 'OS' : null }".Replace("'", "\""); |
55 | | - Assert.AreEqual(json, c.ToJson()); |
56 | | - |
57 | | - var rehydrated = BsonSerializer.Deserialize<C>(c.ToBson()); |
58 | | - Assert.AreEqual(c.Id, rehydrated.Id); |
59 | | - Assert.AreEqual(null, rehydrated.R); |
60 | | - Assert.AreEqual(null, rehydrated.S); |
61 | | - Assert.AreEqual(null, rehydrated.RS); |
62 | | - Assert.AreEqual(null, rehydrated.OR); |
63 | | - Assert.AreEqual(null, rehydrated.OS); |
64 | | - } |
65 | | - |
66 | | - [Test] |
67 | | - public void TestLength0() |
68 | | - { |
69 | | - var list = new List<int>(); |
70 | | - var r = new ReadOnlyCollection<int>(list); |
71 | | - var s = new S<int>(list); |
72 | | - var c = new C { Id = 1, R = r, S = s, RS = s, OR = r, OS = s }; |
73 | | - |
74 | | - var json = _jsonTemplate.Replace("#V", "[]"); |
75 | | - Assert.AreEqual(json, c.ToJson()); |
76 | | - |
77 | | - var rehydrated = BsonSerializer.Deserialize<C>(c.ToBson()); |
78 | | - Assert.AreEqual(c.Id, rehydrated.Id); |
79 | | - Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.R); |
80 | | - Assert.IsInstanceOf<S<int>>(rehydrated.S); |
81 | | - Assert.IsInstanceOf<S<int>>(rehydrated.RS); |
82 | | - Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.OR); |
83 | | - Assert.IsInstanceOf<S<int>>(rehydrated.OS); |
84 | | - Assert.AreEqual(0, rehydrated.R.Count); |
85 | | - Assert.AreEqual(0, rehydrated.S.Count); |
86 | | - Assert.AreEqual(0, rehydrated.RS.Count); |
87 | | - Assert.AreEqual(0, ((ReadOnlyCollection<int>)rehydrated.OR).Count); |
88 | | - Assert.AreEqual(0, ((S<int>)rehydrated.OS).Count); |
89 | | - } |
90 | | - |
91 | | - [Test] |
92 | | - public void TestLength1() |
93 | | - { |
94 | | - var list = new List<int>() { 1 }; |
95 | | - var r = new ReadOnlyCollection<int>(list); |
96 | | - var s = new S<int>(list); |
97 | | - var c = new C { Id = 1, R = r, S = s, RS = s, OR = r, OS = s }; |
98 | | - |
99 | | - var json = _jsonTemplate.Replace("#V", "[1]"); |
100 | | - Assert.AreEqual(json, c.ToJson()); |
101 | | - |
102 | | - var rehydrated = BsonSerializer.Deserialize<C>(c.ToBson()); |
103 | | - Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.R); |
104 | | - Assert.IsInstanceOf<S<int>>(rehydrated.S); |
105 | | - Assert.IsInstanceOf<S<int>>(rehydrated.RS); |
106 | | - Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.OR); |
107 | | - Assert.IsInstanceOf<S<int>>(rehydrated.OS); |
108 | | - Assert.AreEqual(c.Id, rehydrated.Id); |
109 | | - Assert.AreEqual(1, rehydrated.R.Count); |
110 | | - Assert.AreEqual(1, rehydrated.S.Count); |
111 | | - Assert.AreEqual(1, rehydrated.RS.Count); |
112 | | - Assert.AreEqual(1, ((ReadOnlyCollection<int>)rehydrated.OR).Count); |
113 | | - Assert.AreEqual(1, ((S<int>)rehydrated.OS).Count); |
114 | | - Assert.AreEqual(1, rehydrated.R[0]); |
115 | | - Assert.AreEqual(1, rehydrated.S[0]); |
116 | | - Assert.AreEqual(1, rehydrated.RS[0]); |
117 | | - Assert.AreEqual(1, ((ReadOnlyCollection<int>)rehydrated.OR)[0]); |
118 | | - Assert.AreEqual(1, ((S<int>)rehydrated.OS)[0]); |
119 | | - } |
120 | | - |
121 | | - [Test] |
122 | | - public void TestLength2() |
123 | | - { |
124 | | - var list = new List<int>() { 1, 2 }; |
125 | | - var r = new ReadOnlyCollection<int>(list); |
126 | | - var s = new S<int>(list); |
127 | | - var c = new C { Id = 1, R = r, S = s, RS = s, OR = r, OS = s }; |
128 | | - |
129 | | - var json = _jsonTemplate.Replace("#V", "[1, 2]"); |
130 | | - Assert.AreEqual(json, c.ToJson()); |
131 | | - |
132 | | - var rehydrated = BsonSerializer.Deserialize<C>(c.ToBson()); |
133 | | - Assert.AreEqual(c.Id, rehydrated.Id); |
134 | | - Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.R); |
135 | | - Assert.IsInstanceOf<S<int>>(rehydrated.S); |
136 | | - Assert.IsInstanceOf<S<int>>(rehydrated.RS); |
137 | | - Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.OR); |
138 | | - Assert.IsInstanceOf<S<int>>(rehydrated.OS); |
139 | | - Assert.AreEqual(2, rehydrated.R.Count); |
140 | | - Assert.AreEqual(2, rehydrated.S.Count); |
141 | | - Assert.AreEqual(2, rehydrated.RS.Count); |
142 | | - Assert.AreEqual(2, ((ReadOnlyCollection<int>)rehydrated.OR).Count); |
143 | | - Assert.AreEqual(2, ((S<int>)rehydrated.OS).Count); |
144 | | - Assert.AreEqual(1, rehydrated.R[0]); |
145 | | - Assert.AreEqual(1, rehydrated.S[0]); |
146 | | - Assert.AreEqual(1, rehydrated.RS[0]); |
147 | | - Assert.AreEqual(1, ((ReadOnlyCollection<int>)rehydrated.OR)[0]); |
148 | | - Assert.AreEqual(1, ((S<int>)rehydrated.OS)[0]); |
149 | | - Assert.AreEqual(2, rehydrated.R[1]); |
150 | | - Assert.AreEqual(2, rehydrated.S[1]); |
151 | | - Assert.AreEqual(2, rehydrated.RS[1]); |
152 | | - Assert.AreEqual(2, ((ReadOnlyCollection<int>)rehydrated.OR)[1]); |
153 | | - Assert.AreEqual(2, ((S<int>)rehydrated.OS)[1]); |
154 | | - } |
155 | | - } |
156 | | -} |
| 124 | + [Test] |
| 125 | + public void TestLength2() |
| 126 | + { |
| 127 | + var list = new List<int>() { 1, 2 }; |
| 128 | + var r = new ReadOnlyCollection<int>(list); |
| 129 | + var s = new S<int>(list); |
| 130 | + var c = new C { Id = 1, R = r, S = s, RS = s, OR = r, OS = s }; |
| 131 | + |
| 132 | + var json = _jsonTemplate.Replace("#V", "[1, 2]"); |
| 133 | + Assert.AreEqual(json, c.ToJson()); |
| 134 | + |
| 135 | + var rehydrated = BsonSerializer.Deserialize<C>(c.ToBson()); |
| 136 | + Assert.AreEqual(c.Id, rehydrated.Id); |
| 137 | + Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.R); |
| 138 | + Assert.IsInstanceOf<S<int>>(rehydrated.S); |
| 139 | + Assert.IsInstanceOf<S<int>>(rehydrated.RS); |
| 140 | + Assert.IsInstanceOf<ReadOnlyCollection<int>>(rehydrated.OR); |
| 141 | + Assert.IsInstanceOf<S<int>>(rehydrated.OS); |
| 142 | + Assert.AreEqual(2, rehydrated.R.Count); |
| 143 | + Assert.AreEqual(2, rehydrated.S.Count); |
| 144 | + Assert.AreEqual(2, rehydrated.RS.Count); |
| 145 | + Assert.AreEqual(2, ((ReadOnlyCollection<int>)rehydrated.OR).Count); |
| 146 | + Assert.AreEqual(2, ((S<int>)rehydrated.OS).Count); |
| 147 | + Assert.AreEqual(1, rehydrated.R[0]); |
| 148 | + Assert.AreEqual(1, rehydrated.S[0]); |
| 149 | + Assert.AreEqual(1, rehydrated.RS[0]); |
| 150 | + Assert.AreEqual(1, ((ReadOnlyCollection<int>)rehydrated.OR)[0]); |
| 151 | + Assert.AreEqual(1, ((S<int>)rehydrated.OS)[0]); |
| 152 | + Assert.AreEqual(2, rehydrated.R[1]); |
| 153 | + Assert.AreEqual(2, rehydrated.S[1]); |
| 154 | + Assert.AreEqual(2, rehydrated.RS[1]); |
| 155 | + Assert.AreEqual(2, ((ReadOnlyCollection<int>)rehydrated.OR)[1]); |
| 156 | + Assert.AreEqual(2, ((S<int>)rehydrated.OS)[1]); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments