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

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Converters/BinaryConverterTests.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
using System;
 
27
using System.Collections.Generic;
 
28
#if !SILVERLIGHT && !PocketPC && !NET20 && !NETFX_CORE
 
29
using System.Data.Linq;
 
30
#endif
 
31
#if !(SILVERLIGHT || NETFX_CORE)
 
32
using System.Data.SqlTypes;
 
33
#endif
 
34
using System.Text;
 
35
using Newtonsoft.Json.Converters;
 
36
#if !NETFX_CORE
 
37
using NUnit.Framework;
 
38
#else
 
39
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
40
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
41
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
42
#endif
 
43
 
 
44
namespace Newtonsoft.Json.Tests.Converters
 
45
{
 
46
  [TestFixture]
 
47
  public class BinaryConverterTests : TestFixtureBase
 
48
  {
 
49
    private static readonly byte[] TestData = Encoding.UTF8.GetBytes("This is some test data!!!");
 
50
 
 
51
    public class ByteArrayClass
 
52
    {
 
53
      public byte[] ByteArray { get; set; }
 
54
      public byte[] NullByteArray { get; set; }
 
55
    }
 
56
 
 
57
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
 
58
    [Test]
 
59
    public void DeserializeBinaryClass()
 
60
    {
 
61
      string json = @"{
 
62
  ""Binary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
 
63
  ""NullBinary"": null
 
64
}";
 
65
 
 
66
      BinaryClass binaryClass = JsonConvert.DeserializeObject<BinaryClass>(json, new BinaryConverter());
 
67
 
 
68
      Assert.AreEqual(new Binary(TestData), binaryClass.Binary);
 
69
      Assert.AreEqual(null, binaryClass.NullBinary);
 
70
    }
 
71
 
 
72
    [Test]
 
73
    public void DeserializeBinaryClassFromJsonArray()
 
74
    {
 
75
      string json = @"{
 
76
  ""Binary"": [0, 1, 2, 3],
 
77
  ""NullBinary"": null
 
78
}";
 
79
 
 
80
      BinaryClass binaryClass = JsonConvert.DeserializeObject<BinaryClass>(json, new BinaryConverter());
 
81
 
 
82
      Assert.AreEqual(new byte[] { 0, 1, 2, 3 }, binaryClass.Binary.ToArray());
 
83
      Assert.AreEqual(null, binaryClass.NullBinary);
 
84
    }
 
85
 
 
86
    public class BinaryClass
 
87
    {
 
88
      public Binary Binary { get; set; }
 
89
      public Binary NullBinary { get; set; }
 
90
    }
 
91
 
 
92
    [Test]
 
93
    public void SerializeBinaryClass()
 
94
    {
 
95
      BinaryClass binaryClass = new BinaryClass();
 
96
      binaryClass.Binary = new Binary(TestData);
 
97
      binaryClass.NullBinary = null;
 
98
 
 
99
      string json = JsonConvert.SerializeObject(binaryClass, Formatting.Indented, new BinaryConverter());
 
100
 
 
101
      Assert.AreEqual(@"{
 
102
  ""Binary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
 
103
  ""NullBinary"": null
 
104
}", json);
 
105
    }
 
106
#endif
 
107
 
 
108
    [Test]
 
109
    public void SerializeByteArrayClass()
 
110
    {
 
111
      ByteArrayClass byteArrayClass = new ByteArrayClass();
 
112
      byteArrayClass.ByteArray = TestData;
 
113
      byteArrayClass.NullByteArray = null;
 
114
 
 
115
      string json = JsonConvert.SerializeObject(byteArrayClass, Formatting.Indented);
 
116
 
 
117
      Assert.AreEqual(@"{
 
118
  ""ByteArray"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
 
119
  ""NullByteArray"": null
 
120
}", json);
 
121
    }
 
122
 
 
123
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
124
    public class SqlBinaryClass
 
125
    {
 
126
      public SqlBinary SqlBinary { get; set; }
 
127
      public SqlBinary? NullableSqlBinary1 { get; set; }
 
128
      public SqlBinary? NullableSqlBinary2 { get; set; }
 
129
    }
 
130
 
 
131
    [Test]
 
132
    public void SerializeSqlBinaryClass()
 
133
    {
 
134
      SqlBinaryClass sqlBinaryClass = new SqlBinaryClass();
 
135
      sqlBinaryClass.SqlBinary = new SqlBinary(TestData);
 
136
      sqlBinaryClass.NullableSqlBinary1 = new SqlBinary(TestData);
 
137
      sqlBinaryClass.NullableSqlBinary2 = null;
 
138
 
 
139
      string json = JsonConvert.SerializeObject(sqlBinaryClass, Formatting.Indented, new BinaryConverter());
 
140
 
 
141
      Assert.AreEqual(@"{
 
142
  ""SqlBinary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
 
143
  ""NullableSqlBinary1"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
 
144
  ""NullableSqlBinary2"": null
 
145
}", json);
 
146
    }
 
147
 
 
148
    [Test]
 
149
    public void DeserializeSqlBinaryClass()
 
150
    {
 
151
      string json = @"{
 
152
  ""SqlBinary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
 
153
  ""NullableSqlBinary1"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
 
154
  ""NullableSqlBinary2"": null
 
155
}";
 
156
 
 
157
      SqlBinaryClass sqlBinaryClass = JsonConvert.DeserializeObject<SqlBinaryClass>(json, new BinaryConverter());
 
158
 
 
159
      Assert.AreEqual(new SqlBinary(TestData), sqlBinaryClass.SqlBinary);
 
160
      Assert.AreEqual(new SqlBinary(TestData), sqlBinaryClass.NullableSqlBinary1);
 
161
      Assert.AreEqual(null, sqlBinaryClass.NullableSqlBinary2);
 
162
    }
 
163
#endif
 
164
 
 
165
    [Test]
 
166
    public void DeserializeByteArrayClass()
 
167
    {
 
168
      string json = @"{
 
169
  ""ByteArray"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
 
170
  ""NullByteArray"": null
 
171
}";
 
172
 
 
173
      ByteArrayClass byteArrayClass = JsonConvert.DeserializeObject<ByteArrayClass>(json);
 
174
 
 
175
      CollectionAssert.AreEquivalent(TestData, byteArrayClass.ByteArray);
 
176
      Assert.AreEqual(null, byteArrayClass.NullByteArray);
 
177
    }
 
178
 
 
179
    [Test]
 
180
    public void DeserializeByteArrayFromJsonArray()
 
181
    {
 
182
      string json = @"{
 
183
  ""ByteArray"": [0, 1, 2, 3],
 
184
  ""NullByteArray"": null
 
185
}";
 
186
 
 
187
      ByteArrayClass c = JsonConvert.DeserializeObject<ByteArrayClass>(json);
 
188
      Assert.IsNotNull(c.ByteArray);
 
189
      Assert.AreEqual(4, c.ByteArray.Length);
 
190
      CollectionAssert.AreEquivalent(new byte[] { 0, 1, 2, 3 }, c.ByteArray);
 
191
    }
 
192
  }
 
193
}
 
 
b'\\ No newline at end of file'