File: BinarySerializationOverVersionsTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (136 lines) | stat: -rw-r--r-- 4,013 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;

namespace MonoTests.System.Runtime.Serialization.Formatters.Binary
{
		//TODO: add tests that use SoapFormatter (if you go this
		//	  path, it would be interesting also to test a
		//	  custom formatter, like the XML-RPC.net one!)
	[TestFixture]
	public class BinarySerializationOverVersionsTest {

		[Test]
		public void TestDroppedField () //eliminate CountryCode
		{
			Deserialize ("2.0", Serialize ("3.0"));
		}

		[Test]
		public void TestAddedField () //add CountryCode
		{
			Deserialize ("3.0", Serialize ("2.0"));
		}

		[Test]
		public void TestAddedFieldWithData () //add Country
		{
			Deserialize( "1.0", Serialize ("2.0"));
		}

		[Test]
		public void TestDroppedFieldWithData () //eliminate Country
		{
			Deserialize ("2.0", Serialize ("3.0"));
		}

		[Test]
		public void TestAddedFieldWithOptionalAttrib () //add PostCode
		{
			Deserialize ("4.0", Serialize ("3.0"));
		}

		[Test]
		public void TestDroppedFieldWithOptionalAttrib () //eliminate PostCode
		{
			Deserialize ("3.0", Serialize ("4.0"));
		}

		[Test]
		public void TestAddedFieldWithOptionalAttribAndData () //add AreaCode
		{
			Deserialize ("5.0", Serialize ("4.0"));
		}

		[Test]
		public void TestDroppedFieldWithOptionalAttribAndData () //eliminate AreaCode
		{
			Deserialize ("4.0", Serialize ("5.0"));
		}

		[Test]
		public void TestDroppedPrimitiveTypeField() //eliminate Id (int)
		{
			Deserialize ("5.0", Serialize ("6.0"));
		}

		[Test]
		public void TestAddedPrimitiveTypeField () //add Id (int)
		{
			Deserialize ("6.0", Serialize ("5.0"));
		}

		const string AssemblyName = "Address.dll";
		const string TypeName = "VersionTolerantSerializationTestLib.Address";

		class Serializer : MarshalByRefObject {
			static IFormatter formatter = new BinaryFormatter ();
			public static IFormatter Formatter { get { return formatter; } }

			public byte[] Serialize (string version) {
				var assembly = Assembly.LoadFrom (Find (version));
				var type = assembly.GetType (TypeName);
				var obj = Activator.CreateInstance (type);
				var stream = new MemoryStream ();
				
				Formatter.Serialize (stream, obj);
				return stream.ToArray ();
			}

			public void Deserialize (string version, byte[] payload) {
				var assembly = Assembly.LoadFrom (Find (version));
				var stream = new MemoryStream (payload);
				var obj = Formatter.Deserialize (stream);
				//Console.WriteLine ("obj version {0} -> {1}", version, obj);
			}
		}

		private static byte[] Serialize (string assemblyVersion)
		{
			var setup = new AppDomainSetup ();
			setup.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			AppDomain ad = AppDomain.CreateDomain (assemblyVersion, null, setup);
			Serializer ser = (Serializer) ad.CreateInstanceAndUnwrap (typeof(Serializer).Assembly.FullName, typeof(Serializer).FullName);
			byte[] stuff = ser.Serialize (assemblyVersion);
			AppDomain.Unload (ad);
			return stuff;
		}

		private static void Deserialize (string assemblyVersion, byte[] payload)
		{
			var setup = new AppDomainSetup ();
			setup.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			AppDomain ad = AppDomain.CreateDomain (assemblyVersion, null, setup);
			Serializer ser = (Serializer) ad.CreateInstanceAndUnwrap (typeof(Serializer).Assembly.FullName, typeof(Serializer).FullName);
			ser.Deserialize (assemblyVersion, payload);
			AppDomain.Unload (ad);
		}

		private static string Find (string assemblyVersion)
		{
			string initDir = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
			return Find (assemblyVersion, initDir);
		}

		private static string Find (string assemblyVersion, string path)
		{
			return Path.Combine (Path.Combine (path, "vts", assemblyVersion), AssemblyName);
		}
	}
}