All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json.Tests / Converters / BinaryConverterTests.cs
1 using System;
2 using System.Collections.Generic;
3 #if !SILVERLIGHT && !PocketPC && !NET20
4 using System.Data.Linq;
5 #endif
6 #if !SILVERLIGHT
7 using System.Data.SqlTypes;
8 #endif
9 using System.Linq;
10 using System.Text;
11 using Newtonsoft.Json.Converters;
12 using NUnit.Framework;
13 using Newtonsoft.Json.Tests.TestObjects;
14
15 namespace Newtonsoft.Json.Tests.Converters
16 {
17   public class BinaryConverterTests : TestFixtureBase
18   {
19     private static readonly byte[] TestData = Encoding.UTF8.GetBytes("This is some test data!!!");
20
21     public class ByteArrayClass
22     {
23       public byte[] ByteArray { get; set; }
24       public byte[] NullByteArray { get; set; }
25     }
26
27 #if !SILVERLIGHT && !PocketPC && !NET20
28     [Test]
29     public void DeserializeBinaryClass()
30     {
31       string json = @"{
32   ""Binary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
33   ""NullBinary"": null
34 }";
35
36       BinaryClass binaryClass = JsonConvert.DeserializeObject<BinaryClass>(json, new BinaryConverter());
37
38       Assert.AreEqual(new Binary(TestData), binaryClass.Binary);
39       Assert.AreEqual(null, binaryClass.NullBinary);
40     }
41
42     public class BinaryClass
43     {
44       public Binary Binary { get; set; }
45       public Binary NullBinary { get; set; }
46     }
47
48     [Test]
49     public void SerializeBinaryClass()
50     {
51       BinaryClass binaryClass = new BinaryClass();
52       binaryClass.Binary = new Binary(TestData);
53       binaryClass.NullBinary = null;
54
55       string json = JsonConvert.SerializeObject(binaryClass, Formatting.Indented, new BinaryConverter());
56
57       Assert.AreEqual(@"{
58   ""Binary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
59   ""NullBinary"": null
60 }", json);
61     }
62 #endif
63
64     [Test]
65     public void SerializeByteArrayClass()
66     {
67       ByteArrayClass byteArrayClass = new ByteArrayClass();
68       byteArrayClass.ByteArray = TestData;
69       byteArrayClass.NullByteArray = null;
70
71       string json = JsonConvert.SerializeObject(byteArrayClass, Formatting.Indented, new BinaryConverter());
72
73       Assert.AreEqual(@"{
74   ""ByteArray"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
75   ""NullByteArray"": null
76 }", json);
77     }
78
79 #if !SILVERLIGHT
80     public class SqlBinaryClass
81     {
82       public SqlBinary SqlBinary { get; set; }
83       public SqlBinary? NullableSqlBinary1 { get; set; }
84       public SqlBinary? NullableSqlBinary2 { get; set; }
85     }
86
87     [Test]
88     public void SerializeSqlBinaryClass()
89     {
90       SqlBinaryClass sqlBinaryClass = new SqlBinaryClass();
91       sqlBinaryClass.SqlBinary = new SqlBinary(TestData);
92       sqlBinaryClass.NullableSqlBinary1 = new SqlBinary(TestData);
93       sqlBinaryClass.NullableSqlBinary2 = null;
94
95       string json = JsonConvert.SerializeObject(sqlBinaryClass, Formatting.Indented, new BinaryConverter());
96
97       Assert.AreEqual(@"{
98   ""SqlBinary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
99   ""NullableSqlBinary1"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
100   ""NullableSqlBinary2"": null
101 }", json);
102     }
103
104     [Test]
105     public void DeserializeSqlBinaryClass()
106     {
107       string json = @"{
108   ""SqlBinary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
109   ""NullableSqlBinary1"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
110   ""NullableSqlBinary2"": null
111 }";
112
113       SqlBinaryClass sqlBinaryClass = JsonConvert.DeserializeObject<SqlBinaryClass>(json, new BinaryConverter());
114
115       Assert.AreEqual(new SqlBinary(TestData), sqlBinaryClass.SqlBinary);
116       Assert.AreEqual(new SqlBinary(TestData), sqlBinaryClass.NullableSqlBinary1);
117       Assert.AreEqual(null, sqlBinaryClass.NullableSqlBinary2);
118     }
119 #endif
120
121     [Test]
122     public void DeserializeByteArrayClass()
123     {
124       string json = @"{
125   ""ByteArray"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
126   ""NullByteArray"": null
127 }";
128
129       ByteArrayClass byteArrayClass = JsonConvert.DeserializeObject<ByteArrayClass>(json, new BinaryConverter());
130
131       Assert.AreEqual(TestData, byteArrayClass.ByteArray);
132       Assert.AreEqual(null, byteArrayClass.NullByteArray);
133     }
134
135   }
136 }