429068e73ad44f07b25e77ea480205f5d01cf9a1
[mono.git] / mcs / class / corlib / Test / System.Runtime.Serialization / FormatterServicesTests.cs
1 //
2 // System.Runtime.Serialization.FormatterServicesTests: NUnit test
3 //
4 // Authors: 
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2002 Ximian Inc. (http://www.ximian.com)
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.Reflection;
13 using System.Runtime.Serialization;
14
15 namespace MonoTests.System.Runtime.Serialization
16 {
17         public class FormatterServicesTests : TestCase
18         {
19                 public void TestClass1 ()
20                 {
21                         DerivedClass1 derived = new DerivedClass1 ();
22                         derived.anotherInt = 69;
23                         MemberInfo [] members = FormatterServices.GetSerializableMembers (derived.GetType ());
24                         Assert ("#01", members != null);
25                         AssertEquals ("#02", 3, members.Length);
26
27                         object [] data = FormatterServices.GetObjectData (derived, members);
28                         Assert ("#03", data != null);
29                         AssertEquals ("#04", 3, data.Length);
30
31                         DerivedClass1 o = (DerivedClass1) FormatterServices.GetUninitializedObject (derived.GetType ());
32                         Assert ("#05", o != null);
33
34                         o = (DerivedClass1) FormatterServices.PopulateObjectMembers (o, members, data);
35                         Assert ("#06", o != null);
36                         AssertEquals ("#07", "hola", o.Hello);
37                         AssertEquals ("#08", 21, o.IntBase);
38                         AssertEquals ("#09", 1, o.IntDerived);
39                         AssertEquals ("#10", 69, o.anotherInt);
40                         AssertEquals ("#11", "hey", DerivedClass1.hey);
41                 }
42         }
43
44         [Serializable]
45         class BaseClass1
46         {
47                 public string hello = "hola";
48                 static int intBase = 21;
49
50                 public override int GetHashCode ()
51                 {
52                         return base.GetHashCode ();
53                 }
54
55                 public override bool Equals (object o)
56                 {
57                         BaseClass1 bc = o  as BaseClass1;
58                         if (o == null)
59                                 return false;
60
61                         if (hello != "hola")
62                                 return false;
63
64                         return (hello == bc.hello);
65                 }
66
67                 public string Hello
68                 {
69                         get {
70                                 return hello;
71                         }
72                 }
73
74                 public int IntBase
75                 {
76                         get {
77                                 return intBase;
78                         }
79                 }
80         }
81         
82         [Serializable]
83         class DerivedClass1 : BaseClass1
84         {
85                 private int intDerived = 1;
86                 [NonSerialized] public int publicint = 2;
87                 public int anotherInt = 22;
88                 public static string hey = "hey";
89
90                 public string Name 
91                 {
92                         get {
93                                 return "Ahem";
94                         }
95                 }
96
97                 public void SomeMethod ()
98                 {
99                         /* Does nothing */
100                 }
101
102                 public override int GetHashCode ()
103                 {
104                         return base.GetHashCode ();
105                 }
106
107                 public override bool Equals (object o)
108                 {
109                         DerivedClass1 dc = o  as DerivedClass1;
110                         if (o == null)
111                                 return false;
112
113                         if (anotherInt != 22 || hey != "hey")
114                                 return false;
115                         
116                         return (anotherInt == dc.anotherInt);
117                 }
118
119                 public int IntDerived
120                 {
121                         get {
122                                 return intDerived;
123                         }
124                 }
125         }
126 }
127