DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
BinarySerializeTests.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 using System;
16 using System.IO;
17 
18 using Deveel.Data.Sql;
19 
20 using NUnit.Framework;
21 
22 namespace Deveel.Data.Serialization {
23  [TestFixture]
24  public static class BinarySerializeTests {
25  [Test]
26  public static void SerializeWithNoParent() {
27  var obj = new TestClass {Value = "test1"};
28 
29  var serializer = new BinarySerializer();
30  byte[] bytes;
31 
32  using (var memoryStream = new MemoryStream()) {
33  serializer.Serialize(memoryStream, obj);
34  memoryStream.Flush();
35  bytes = memoryStream.ToArray();
36  }
37 
38  object graph = null;
39  using (var memoryStream = new MemoryStream(bytes)) {
40  graph = serializer.Deserialize(memoryStream, typeof(TestClass));
41  }
42 
43  Assert.IsNotNull(graph);
44  Assert.IsInstanceOf<TestClass>(obj);
45 
46  obj = (TestClass) graph;
47  Assert.AreEqual("test1", obj.Value);
48  Assert.IsNull(obj.Parent);
49  }
50 
51  [Test]
52  public static void SerializeObjectNameWithNoParent() {
53  var objName = new ObjectName("name");
54 
55  var serializer = new BinarySerializer();
56  byte[] bytes;
57 
58  using (var memoryStream = new MemoryStream()) {
59  serializer.Serialize(memoryStream, objName);
60  memoryStream.Flush();
61  bytes = memoryStream.ToArray();
62  }
63 
64  object graph = null;
65  using (var memoryStream = new MemoryStream(bytes)) {
66  graph = serializer.Deserialize(memoryStream, typeof(ObjectName));
67  }
68 
69  Assert.IsNotNull(graph);
70  Assert.IsInstanceOf<ObjectName>(graph);
71 
72  var objName2 = (ObjectName) graph;
73  Assert.AreEqual(objName.Name, objName2.Name);
74  Assert.AreEqual(objName, objName2);
75  }
76 
77  [Test]
78  public static void SerializeImplicit() {
79  var obj = new TestClass2("test2");
80 
81  var serializer = new BinarySerializer();
82  byte[] bytes;
83 
84  using (var memoryStream = new MemoryStream()) {
85  serializer.Serialize(memoryStream, obj);
86  memoryStream.Flush();
87  bytes = memoryStream.ToArray();
88  }
89 
90  object graph = null;
91  using (var memoryStream = new MemoryStream(bytes)) {
92  graph = serializer.Deserialize(memoryStream, typeof(TestClass2));
93  }
94 
95  Assert.IsNotNull(graph);
96  Assert.IsInstanceOf<TestClass2>(obj);
97 
98  obj = (TestClass2) graph;
99  Assert.AreEqual("test2", obj.Value);
100  Assert.IsNull(obj.Parent);
101 
102  }
103 
104  [Serializable]
106  public TestClass() {
107  }
108 
109  private TestClass(ObjectData graph) {
110  Value = graph.GetString("value");
111  Parent = graph.GetValue<TestClass>("parent");
112  }
113 
114  public string Value { get; set; }
115 
116  public TestClass Parent { get; set; }
117 
118  public void GetData(SerializeData graph) {
119  graph.SetValue("value", Value);
120  graph.SetValue("parent", Parent);
121  }
122  }
123 
124  [Serializable]
125  class TestClass2 {
126  [NonSerialized]
127  internal TestClass2 parent;
128 
129  private TestClass2() {
130  }
131 
132  public TestClass2(string value) {
133  Value = value;
134  }
135 
136  public string Value { get; private set; }
137 
138  public TestClass2 Parent {
139  get { return parent; }
140  set {
141  parent = value;
142  }
143  }
144  }
145  }
146 }
void SetValue(string key, Type type, object value)
Describes the name of an object within a database.
Definition: ObjectName.cs:44