2 using System.Collections;
4 using System.Diagnostics;
5 using System.Reflection;
7 using System.Runtime.Serialization;
8 using System.Runtime.Serialization.Formatters.Binary;
11 namespace MonoTests.System.Runtime.Serialization.Formatters.Binary
14 public class BinarySerializationOverVersions {
16 static readonly string dirName = typeof(VersionTolerantSerializationTestLib.Address).Namespace;
17 static readonly string assemblyName = typeof(VersionTolerantSerializationTestLib.Address).Name;
18 static readonly string assemblyFileName = assemblyName + ".dll";
19 static readonly string binName = Path.GetFileName (Assembly.GetExecutingAssembly ().Location);
20 const bool cleanup = true;
21 enum OopOperation { Serialize, Deserialize }
22 static IFormatter formatter = new BinaryFormatter ();
25 //TODO: add tests that use SoapFormatter (if you go this
26 // path, it would be interesting also to test a
27 // custom formatter, like the XML-RPC.net one!)
28 public virtual IFormatter Formatter { get { return formatter; } }
30 static void Main (string [] args)
32 var p = new BinarySerializationOverVersions ();
34 /* this block is useful for testing this without NUnit:
37 Console.WriteLine ("Starting...");
38 p.TestDroppedField ();
43 p.TestAddedFieldWithData ();
45 p.TestDroppedFieldWithData ();
48 p.TestAddedFieldWithOptionalAttrib ();
50 p.TestDroppedFieldWithOptionalAttrib ();
53 p.TestAddedFieldWithOptionalAttribAndData ();
55 p.TestDroppedFieldWithOptionalAttribAndData ();
63 throw new Exception ("Please specify arguments");
65 if (args [0] == OopOperation.Serialize.ToString ())
67 p.SerializeToFile (args [1]);
69 else if (args [0] == OopOperation.Deserialize.ToString ())
71 p.DeserializeFromFile (args [1]);
75 throw new Exception(String.Format ("{0} operation not recognized. Only {Serialize|Deserialize} operations are supported.", args [0]));
80 public void TestDroppedField () //eliminate CountryCode
82 Deserialize ("2.0", Serialize ("3.0"));
86 public void TestAddedField () //add CountryCode
88 Deserialize ("3.0", Serialize ("2.0"));
92 public void TestAddedFieldWithData () //add Country
94 Deserialize( "1.0", Serialize ("2.0"));
98 public void TestDroppedFieldWithData () //eliminate Country
100 Deserialize ("2.0", Serialize ("3.0"));
104 public void TestAddedFieldWithOptionalAttrib () //add PostCode
106 Deserialize ("4.0", Serialize ("3.0"));
110 public void TestDroppedFieldWithOptionalAttrib () //eliminate PostCode
112 Deserialize ("3.0", Serialize ("4.0"));
116 public void TestAddedFieldWithOptionalAttribAndData () //add AreaCode
118 Deserialize ("5.0", Serialize ("4.0"));
122 public void TestDroppedFieldWithOptionalAttribAndData () //eliminate AreaCode
124 Deserialize ("4.0", Serialize ("5.0"));
128 public void TestDroppedPrimitiveTypeField() //eliminate Id (int)
130 Deserialize ("5.0", Serialize ("6.0"));
134 public void TestAddedPrimitiveTypeField () //add Id (int)
136 Deserialize ("6.0", Serialize ("5.0"));
139 private static string Serialize (string assemblyVersion)
141 return SerializeOOP (SetEnvironment (assemblyVersion)); ;
144 private static void Deserialize (string assemblyVersion, string filename)
146 DeserializeOOP (SetEnvironment (assemblyVersion), filename);
149 private static string SerializeOOP (string executionDir)
151 string filename = Path.GetTempFileName ();
152 var p = new Process ();
153 p.StartInfo.WorkingDirectory = executionDir;
154 p.StartInfo.FileName = Path.Combine (executionDir, binName);
155 p.StartInfo.Arguments = OopOperation.Serialize.ToString () + " \"" + filename + "\"";
156 p.StartInfo.UseShellExecute = false;
160 throw new Exception ("Problem in serialization operation");
163 Directory.Delete (executionDir, true);
168 private static void DeserializeOOP (string executionDir, string filename)
170 var p = new Process ();
171 p.StartInfo.WorkingDirectory = executionDir;
172 p.StartInfo.FileName = Path.Combine (executionDir, binName);
173 p.StartInfo.Arguments = OopOperation.Deserialize.ToString () + " \"" + filename + "\"";
174 p.StartInfo.UseShellExecute = false;
178 throw new Exception("Problem in deserialization operation");
182 Directory.Delete (executionDir, true);
183 File.Delete (filename);
187 private static string SetEnvironment (string assemblyVersion)
189 if (!assemblyVersion.Contains ("."))
190 throw new NotSupportedException ("The version number should contain a dot, i.e.: 2.0");
192 string tmpDir = Path.Combine (Path.GetTempPath (), "deleteme" + Guid.NewGuid ());
193 Directory.CreateDirectory (tmpDir);
194 string currentBin = Assembly.GetExecutingAssembly ().Location;
195 File.Copy (currentBin, Path.Combine (tmpDir, Path.GetFileName (currentBin)));
197 string ass = Find (assemblyVersion);
198 File.Copy (ass, Path.Combine (tmpDir, Path.GetFileName (ass)));
202 private static string Find (string assemblyVersion)
204 string initDir = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
205 return Find (assemblyVersion, initDir);
208 private static string Find (string assemblyVersion, string path)
210 return Path.Combine (Path.Combine (path, assemblyVersion), assemblyFileName);
212 Console.WriteLine ("Looking in " + path);
215 if (!path.Contains(assemblyVersion))
219 if (Path.GetFileName(path) == dirName)
221 test = Path.Combine (path, assemblyVersion);
222 if (Directory.Exists (test))
223 return Find (assemblyVersion, test);
225 throw new DirectoryNotFoundException (String.Format ("{0} was not found", test));
228 test = Path.Combine (path, dirName);
229 if (Directory.Exists (test))
230 return Find (assemblyVersion, test);
232 return Find (assemblyVersion, Path.Combine (path, ".."));
237 test = Path.Combine (path, assemblyFileName);
238 if (File.Exists (test))
241 test = Path.Combine (path, "bin");
242 if (Directory.Exists (test))
243 return Find (assemblyVersion, test);
245 test = Path.Combine (path, "Debug");
246 if (Directory.Exists (test))
247 return Find (assemblyVersion, test);
249 test = Path.Combine (path, "Release");
250 if (Directory.Exists (test))
251 return Find (assemblyVersion, test);
253 test = Path.Combine (path.Replace ("Debug", "Release"), assemblyFileName);
254 if (File.Exists (test))
257 throw new NotSupportedException(
259 "The tree is not predictible according to the philosophy of the test. (Stuck in {0})",
265 private void SerializeToFile (string filename)
267 var type = typeof (VersionTolerantSerializationTestLib.Address);
268 object obj = Activator.CreateInstance (type);
269 Stream stream = new FileStream (filename,
270 FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
271 Formatter.Serialize (stream, obj);
275 private void DeserializeFromFile (string filename)
277 //Console.WriteLine("Trying to deserialize {0}...", filename);
278 FileStream readStream = new FileStream (filename, FileMode.Open);
280 Formatter.Deserialize (readStream);