Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Converters\BinaryConverterTests.cs

Symbol Coverage: 100.00% (36 of 36)

Branch Coverage: 100.00% (7 of 7)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 56


L V Source
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
 1
    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
 1
      string json = @"{
32
 1
  ""Binary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
33
 1
  ""NullBinary"": null
34
 1
}";
35

  
36
 1
      BinaryClass binaryClass = JsonConvert.DeserializeObject<BinaryClass>(json, new BinaryConverter());
37

  
38
 1
      Assert.AreEqual(new Binary(TestData), binaryClass.Binary);
39
 1
      Assert.AreEqual(null, binaryClass.NullBinary);
40
 1
    }
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
 1
      BinaryClass binaryClass = new BinaryClass();
52
 1
      binaryClass.Binary = new Binary(TestData);
53
 1
      binaryClass.NullBinary = null;
54

  
55
 1
      string json = JsonConvert.SerializeObject(binaryClass, Formatting.Indented, new BinaryConverter());
56

  
57
 1
      Assert.AreEqual(@"{
58
 1
  ""Binary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
59
 1
  ""NullBinary"": null
60
 1
}", json);
61
 1
    }
62
#endif
63

  
64
    [Test]
65
    public void SerializeByteArrayClass()
66
    {
67
 1
      ByteArrayClass byteArrayClass = new ByteArrayClass();
68
 1
      byteArrayClass.ByteArray = TestData;
69
 1
      byteArrayClass.NullByteArray = null;
70

  
71
 1
      string json = JsonConvert.SerializeObject(byteArrayClass, Formatting.Indented, new BinaryConverter());
72

  
73
 1
      Assert.AreEqual(@"{
74
 1
  ""ByteArray"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
75
 1
  ""NullByteArray"": null
76
 1
}", json);
77
 1
    }
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
 1
      SqlBinaryClass sqlBinaryClass = new SqlBinaryClass();
91
 1
      sqlBinaryClass.SqlBinary = new SqlBinary(TestData);
92
 1
      sqlBinaryClass.NullableSqlBinary1 = new SqlBinary(TestData);
93
 1
      sqlBinaryClass.NullableSqlBinary2 = null;
94

  
95
 1
      string json = JsonConvert.SerializeObject(sqlBinaryClass, Formatting.Indented, new BinaryConverter());
96

  
97
 1
      Assert.AreEqual(@"{
98
 1
  ""SqlBinary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
99
 1
  ""NullableSqlBinary1"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
100
 1
  ""NullableSqlBinary2"": null
101
 1
}", json);
102
 1
    }
103

  
104
    [Test]
105
    public void DeserializeSqlBinaryClass()
106
    {
107
 1
      string json = @"{
108
 1
  ""SqlBinary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
109
 1
  ""NullableSqlBinary1"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
110
 1
  ""NullableSqlBinary2"": null
111
 1
}";
112

  
113
 1
      SqlBinaryClass sqlBinaryClass = JsonConvert.DeserializeObject<SqlBinaryClass>(json, new BinaryConverter());
114

  
115
 1
      Assert.AreEqual(new SqlBinary(TestData), sqlBinaryClass.SqlBinary);
116
 1
      Assert.AreEqual(new SqlBinary(TestData), sqlBinaryClass.NullableSqlBinary1);
117
 1
      Assert.AreEqual(null, sqlBinaryClass.NullableSqlBinary2);
118
 1
    }
119
#endif
120

  
121
    [Test]
122
    public void DeserializeByteArrayClass()
123
    {
124
 1
      string json = @"{
125
 1
  ""ByteArray"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
126
 1
  ""NullByteArray"": null
127
 1
}";
128

  
129
 1
      ByteArrayClass byteArrayClass = JsonConvert.DeserializeObject<ByteArrayClass>(json, new BinaryConverter());
130

  
131
 1
      Assert.AreEqual(TestData, byteArrayClass.ByteArray);
132
 1
      Assert.AreEqual(null, byteArrayClass.NullByteArray);
133
 1
    }
134

  
135
  }
136
}