~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Serialization/EntitiesSerializationTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
#if !(NET35 || NET20 || SILVERLIGHT || NETFX_CORE || PORTABLE)
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Data;
 
30
using System.Linq;
 
31
using System.Text;
 
32
using Newtonsoft.Json.Converters;
 
33
#if !NETFX_CORE
 
34
using NUnit.Framework;
 
35
#else
 
36
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
37
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
38
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
39
#endif
 
40
 
 
41
namespace Newtonsoft.Json.Tests.Serialization
 
42
{
 
43
  [TestFixture]
 
44
  public class EntitiesSerializationTests : TestFixtureBase
 
45
  {
 
46
    [Test]
 
47
    public void SerializeEntity()
 
48
    {
 
49
      Folder rootFolder = CreateEntitiesTestData();
 
50
 
 
51
      string json = JsonConvert.SerializeObject(rootFolder, Formatting.Indented, new IsoDateTimeConverter());
 
52
 
 
53
      string expected = @"{
 
54
  ""$id"": ""1"",
 
55
  ""FolderId"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e"",
 
56
  ""Name"": ""Root folder"",
 
57
  ""Description"": ""Description!"",
 
58
  ""CreatedDate"": ""2000-12-10T10:50:00Z"",
 
59
  ""Files"": [],
 
60
  ""ChildFolders"": [
 
61
    {
 
62
      ""$id"": ""2"",
 
63
      ""FolderId"": ""484936e2-7cbb-4592-93ff-b2103e5705e4"",
 
64
      ""Name"": ""Child folder"",
 
65
      ""Description"": ""Description!"",
 
66
      ""CreatedDate"": ""2001-11-20T10:50:00Z"",
 
67
      ""Files"": [
 
68
        {
 
69
          ""$id"": ""3"",
 
70
          ""FileId"": ""cc76d734-49f1-4616-bb38-41514228ac6c"",
 
71
          ""Name"": ""File 1"",
 
72
          ""Description"": ""Description!"",
 
73
          ""CreatedDate"": ""2002-10-30T10:50:00Z"",
 
74
          ""Folder"": {
 
75
            ""$ref"": ""2""
 
76
          },
 
77
          ""EntityKey"": {
 
78
            ""$id"": ""4"",
 
79
            ""EntitySetName"": ""File"",
 
80
            ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
 
81
            ""EntityKeyValues"": [
 
82
              {
 
83
                ""Key"": ""FileId"",
 
84
                ""Type"": ""System.Guid"",
 
85
                ""Value"": ""cc76d734-49f1-4616-bb38-41514228ac6c""
 
86
              }
 
87
            ]
 
88
          }
 
89
        }
 
90
      ],
 
91
      ""ChildFolders"": [],
 
92
      ""ParentFolder"": {
 
93
        ""$ref"": ""1""
 
94
      },
 
95
      ""EntityKey"": {
 
96
        ""$id"": ""5"",
 
97
        ""EntitySetName"": ""Folder"",
 
98
        ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
 
99
        ""EntityKeyValues"": [
 
100
          {
 
101
            ""Key"": ""FolderId"",
 
102
            ""Type"": ""System.Guid"",
 
103
            ""Value"": ""484936e2-7cbb-4592-93ff-b2103e5705e4""
 
104
          }
 
105
        ]
 
106
      }
 
107
    }
 
108
  ],
 
109
  ""ParentFolder"": null,
 
110
  ""EntityKey"": {
 
111
    ""$id"": ""6"",
 
112
    ""EntitySetName"": ""Folder"",
 
113
    ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
 
114
    ""EntityKeyValues"": [
 
115
      {
 
116
        ""Key"": ""FolderId"",
 
117
        ""Type"": ""System.Guid"",
 
118
        ""Value"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e""
 
119
      }
 
120
    ]
 
121
  }
 
122
}";
 
123
 
 
124
      Assert.AreEqual(expected, json);
 
125
    }
 
126
 
 
127
    [Test]
 
128
    public void DeserializeEntity()
 
129
    {
 
130
      string json = @"{
 
131
  ""$id"": ""1"",
 
132
  ""FolderId"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e"",
 
133
  ""Name"": ""Root folder"",
 
134
  ""Description"": ""Description!"",
 
135
  ""CreatedDate"": ""2000-12-10T10:50:00Z"",
 
136
  ""Files"": [],
 
137
  ""ChildFolders"": [
 
138
    {
 
139
      ""$id"": ""2"",
 
140
      ""FolderId"": ""484936e2-7cbb-4592-93ff-b2103e5705e4"",
 
141
      ""Name"": ""Child folder"",
 
142
      ""Description"": ""Description!"",
 
143
      ""CreatedDate"": ""2001-11-20T10:50:00Z"",
 
144
      ""Files"": [
 
145
        {
 
146
          ""$id"": ""3"",
 
147
          ""FileId"": ""cc76d734-49f1-4616-bb38-41514228ac6c"",
 
148
          ""Name"": ""File 1"",
 
149
          ""Description"": ""Description!"",
 
150
          ""CreatedDate"": ""2002-10-30T10:50:00Z"",
 
151
          ""Folder"": {
 
152
            ""$ref"": ""2""
 
153
          },
 
154
          ""EntityKey"": {
 
155
            ""$id"": ""4"",
 
156
            ""EntitySetName"": ""File"",
 
157
            ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
 
158
            ""EntityKeyValues"": [
 
159
              {
 
160
                ""Key"": ""FileId"",
 
161
                ""Type"": ""System.Guid"",
 
162
                ""Value"": ""cc76d734-49f1-4616-bb38-41514228ac6c""
 
163
              }
 
164
            ]
 
165
          }
 
166
        }
 
167
      ],
 
168
      ""ChildFolders"": [],
 
169
      ""ParentFolder"": {
 
170
        ""$ref"": ""1""
 
171
      },
 
172
      ""EntityKey"": {
 
173
        ""$id"": ""5"",
 
174
        ""EntitySetName"": ""Folder"",
 
175
        ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
 
176
        ""EntityKeyValues"": [
 
177
          {
 
178
            ""Key"": ""FolderId"",
 
179
            ""Type"": ""System.Guid"",
 
180
            ""Value"": ""484936e2-7cbb-4592-93ff-b2103e5705e4""
 
181
          }
 
182
        ]
 
183
      }
 
184
    }
 
185
  ],
 
186
  ""ParentFolder"": null,
 
187
  ""EntityKey"": {
 
188
    ""$id"": ""6"",
 
189
    ""EntitySetName"": ""Folder"",
 
190
    ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
 
191
    ""EntityKeyValues"": [
 
192
      {
 
193
        ""Key"": ""FolderId"",
 
194
        ""Type"": ""System.Guid"",
 
195
        ""Value"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e""
 
196
      }
 
197
    ]
 
198
  }
 
199
}";
 
200
 
 
201
      Folder f = JsonConvert.DeserializeObject<Folder>(json, new IsoDateTimeConverter());
 
202
 
 
203
      Assert.IsNotNull(f);
 
204
      Assert.AreEqual(new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E"), f.FolderId);
 
205
      Assert.AreEqual("Folder", f.EntityKey.EntitySetName);
 
206
      Assert.AreEqual("DataServicesTestDatabaseEntities", f.EntityKey.EntityContainerName);
 
207
      Assert.AreEqual("Folder", f.EntityKey.EntitySetName);
 
208
      Assert.AreEqual(false, f.EntityKey.IsTemporary);
 
209
      Assert.AreEqual(1, f.EntityKey.EntityKeyValues.Length);
 
210
      Assert.AreEqual("FolderId", f.EntityKey.EntityKeyValues[0].Key);
 
211
      Assert.AreEqual(new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E"), f.EntityKey.EntityKeyValues[0].Value);
 
212
      Assert.AreEqual("Root folder", f.Name);
 
213
      Assert.AreEqual(new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc), f.CreatedDate);
 
214
      Assert.AreEqual(null, f.ParentFolder);
 
215
      Assert.AreEqual(1, f.ChildFolders.Count);
 
216
 
 
217
      Folder childFolder = f.ChildFolders.ElementAt(0);
 
218
 
 
219
      Assert.AreEqual("Child folder", childFolder.Name);
 
220
      Assert.AreEqual("Description!", childFolder.Description);
 
221
      Assert.AreEqual(f, childFolder.ParentFolder);
 
222
      Assert.AreEqual(f, childFolder.ParentFolderReference.Value);
 
223
      // is this a problem?
 
224
      Assert.AreEqual(null, childFolder.ParentFolderReference.EntityKey);
 
225
    }
 
226
 
 
227
    [Test]
 
228
    public void SerializeMultiValueEntityKey()
 
229
    {
 
230
      EntityKey e = new EntityKey("DataServicesTestDatabaseEntities.Folder",
 
231
                                  new List<EntityKeyMember>
 
232
                                    {
 
233
                                      new EntityKeyMember("GuidId", new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E")),
 
234
                                      new EntityKeyMember("IntId", int.MaxValue),
 
235
                                      new EntityKeyMember("LongId", long.MaxValue),
 
236
                                      new EntityKeyMember("StringId", "String!"),
 
237
                                      new EntityKeyMember("DateTimeId", new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc))
 
238
                                    });
 
239
 
 
240
      string json = JsonConvert.SerializeObject(e, Formatting.Indented);
 
241
 
 
242
      Assert.AreEqual(@"{
 
243
  ""$id"": ""1"",
 
244
  ""EntitySetName"": ""Folder"",
 
245
  ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
 
246
  ""EntityKeyValues"": [
 
247
    {
 
248
      ""Key"": ""GuidId"",
 
249
      ""Type"": ""System.Guid"",
 
250
      ""Value"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e""
 
251
    },
 
252
    {
 
253
      ""Key"": ""IntId"",
 
254
      ""Type"": ""System.Int32"",
 
255
      ""Value"": ""2147483647""
 
256
    },
 
257
    {
 
258
      ""Key"": ""LongId"",
 
259
      ""Type"": ""System.Int64"",
 
260
      ""Value"": ""9223372036854775807""
 
261
    },
 
262
    {
 
263
      ""Key"": ""StringId"",
 
264
      ""Type"": ""System.String"",
 
265
      ""Value"": ""String!""
 
266
    },
 
267
    {
 
268
      ""Key"": ""DateTimeId"",
 
269
      ""Type"": ""System.DateTime"",
 
270
      ""Value"": ""12/10/2000 10:50:00""
 
271
    }
 
272
  ]
 
273
}", json);
 
274
 
 
275
      EntityKey newKey = JsonConvert.DeserializeObject<EntityKey>(json);
 
276
      Assert.IsFalse(ReferenceEquals(e, newKey));
 
277
 
 
278
      Assert.AreEqual(5, newKey.EntityKeyValues.Length);
 
279
      Assert.AreEqual("GuidId", newKey.EntityKeyValues[0].Key);
 
280
      Assert.AreEqual(new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E"), newKey.EntityKeyValues[0].Value);
 
281
      Assert.AreEqual("IntId", newKey.EntityKeyValues[1].Key);
 
282
      Assert.AreEqual(int.MaxValue, newKey.EntityKeyValues[1].Value);
 
283
      Assert.AreEqual("LongId", newKey.EntityKeyValues[2].Key);
 
284
      Assert.AreEqual(long.MaxValue, newKey.EntityKeyValues[2].Value);
 
285
      Assert.AreEqual("StringId", newKey.EntityKeyValues[3].Key);
 
286
      Assert.AreEqual("String!", newKey.EntityKeyValues[3].Value);
 
287
      Assert.AreEqual("DateTimeId", newKey.EntityKeyValues[4].Key);
 
288
      Assert.AreEqual(new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc), newKey.EntityKeyValues[4].Value);
 
289
    }
 
290
 
 
291
    private Folder CreateEntitiesTestData()
 
292
    {
 
293
      Folder folder = new Folder();
 
294
      folder.FolderId = new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E");
 
295
      folder.EntityKey = new EntityKey("DataServicesTestDatabaseEntities.Folder", "FolderId", folder.FolderId);
 
296
      folder.Name = "Root folder";
 
297
      folder.Description = "Description!";
 
298
      folder.CreatedDate = new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc);
 
299
      
 
300
      Folder childFolder = new Folder();
 
301
      childFolder.FolderId = new Guid("484936E2-7CBB-4592-93FF-B2103E5705E4");
 
302
      childFolder.EntityKey = new EntityKey("DataServicesTestDatabaseEntities.Folder", "FolderId", childFolder.FolderId);
 
303
      childFolder.Name = "Child folder";
 
304
      childFolder.Description = "Description!";
 
305
      childFolder.CreatedDate = new DateTime(2001, 11, 20, 10, 50, 0, DateTimeKind.Utc);
 
306
 
 
307
      folder.ChildFolders.Add(childFolder);
 
308
 
 
309
      File file1 = new File();
 
310
      file1.FileId = new Guid("CC76D734-49F1-4616-BB38-41514228AC6C");
 
311
      file1.EntityKey = new EntityKey("DataServicesTestDatabaseEntities.File", "FileId", file1.FileId);
 
312
      file1.Name = "File 1";
 
313
      file1.Description = "Description!";
 
314
      file1.CreatedDate = new DateTime(2002, 10, 30, 10, 50, 0, DateTimeKind.Utc);
 
315
 
 
316
      childFolder.Files.Add(file1);
 
317
      return folder;
 
318
    }
 
319
  }
 
320
}
 
321
#endif
 
 
b'\\ No newline at end of file'