using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
+using System.Data;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
-using NUnit.Framework;
-using System.Xml.Serialization;
using System.Xml.Schema;
+using System.Xml.Serialization;
+using NUnit.Framework;
[assembly: ContractNamespace ("http://www.u2u.be/samples/wcf/2009", ClrNamespace = "U2U.DataContracts")] // bug #599889
[Test]
public void ConstructorKnownTypesNull ()
{
- // null knownTypes is allowed.
- new DataContractSerializer (typeof (Sample1), null);
- new DataContractSerializer (typeof (Sample1), "Foo", String.Empty, null);
- new DataContractSerializer (typeof (Sample1), new XmlDictionary ().Add ("Foo"), XmlDictionaryString.Empty, null);
+ // null knownTypes is allowed. Though the property is filled.
+ Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), null).KnownTypes, "#1");
+ Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), "Foo", String.Empty, null).KnownTypes, "#2");
+ Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), new XmlDictionary ().Add ("Foo"), XmlDictionaryString.Empty, null).KnownTypes, "#3");
}
[Test]
root.FDict.Add (foo2);
ds.WriteObject (ms, root);
- Console.WriteLine (Encoding.UTF8.GetString (ms.ToArray ()));
+ string result = Encoding.UTF8.GetString (ms.ToArray ());
ms.Position = 0;
root = (Root) ds.ReadObject (ms);
Assert.AreEqual (2, root.FDict.Count, "#1");
+ int idx = result.IndexOf ("foo1");
+ Assert.IsTrue (idx >= 0, "#2");
+ // since "foo1" is stored as z:Ref for string, it must not occur twice.
+ int idx2 = result.IndexOf ("foo1", idx + 1);
+ Assert.IsTrue (idx2 < 0, "idx2 should not occur at " + idx2);
+ }
+
+ [Test]
+ public void AncestralReference ()
+ {
+ // Reference to Parent comes inside the Parent itself.
+ // In this case, adding reference after complete deserialization won't work (but it should).
+ var ms = new MemoryStream ();
+ Type [] knownTypes = new Type [] { typeof (ParentClass), typeof (Foo), typeof (Bar) };
+
+ var ds = new DataContractSerializer (typeof (Parent));
+
+ var org = new Parent ();
+ ds.WriteObject (ms, org);
+ string result = Encoding.UTF8.GetString (ms.ToArray ());
+ ms.Position = 0;
+
+ var parent = (Parent) ds.ReadObject (ms);
+
+ Assert.IsNotNull (parent.Child, "#1");
+ Assert.AreEqual (parent, parent.Child.Parent, "#2");
+ }
+
+ [Test]
+ public void IXmlSerializableCallConstructor ()
+ {
+ IXmlSerializableCallConstructor (false);
+ IXmlSerializableCallConstructor (true);
+ }
+
+ void IXmlSerializableCallConstructor (bool binary)
+ {
+ Stream s = IXmlSerializableCallConstructorSerialize (binary);
+ var a = new byte [s.Length];
+ s.Position = 0;
+ s.Read (a, 0, a.Length);
+ s.Position = 0;
+ IXmlSerializableCallConstructorDeserialize (s, binary);
+ }
+
+ public Stream IXmlSerializableCallConstructorSerialize (bool binary)
+ {
+ var ds = new DataSet ("ds");
+ var dt = new DataTable ("dt");
+ ds.Tables.Add (dt);
+ dt.Columns.Add ("n", typeof (int));
+ dt.Columns.Add ("s", typeof (string));
+
+ dt.Rows.Add (5, "five");
+ dt.Rows.Add (10, "ten");
+
+ ds.AcceptChanges ();
+
+ var s = new MemoryStream ();
+
+ var w = binary ? XmlDictionaryWriter.CreateBinaryWriter (s) : XmlDictionaryWriter.CreateTextWriter (s);
+
+ var x = new DataContractSerializer (typeof (DataSet));
+ x.WriteObject (w, ds);
+ w.Flush ();
+
+ return s;
+ }
+
+ public void IXmlSerializableCallConstructorDeserialize (Stream s, bool binary)
+ {
+ var r = binary ? XmlDictionaryReader.CreateBinaryReader (s, XmlDictionaryReaderQuotas.Max)
+ : XmlDictionaryReader.CreateTextReader (s, XmlDictionaryReaderQuotas.Max);
+
+ var x = new DataContractSerializer (typeof (DataSet));
+
+ var ds = (DataSet) x.ReadObject (r);
+ }
+
+ [Test]
+ [ExpectedException (typeof (InvalidDataContractException))] // BaseConstraintType1 is neither DataContract nor Serializable.
+ public void BaseConstraint1 ()
+ {
+ new DataContractSerializer (typeof (BaseConstraintType3)).WriteObject (XmlWriter.Create (TextWriter.Null), new BaseConstraintType3 ());
+ }
+
+ [Test]
+ public void BaseConstraint2 ()
+ {
+ new DataContractSerializer (typeof (BaseConstraintType4)).WriteObject (XmlWriter.Create (TextWriter.Null), new BaseConstraintType4 ());
}
}
[DataMember]
public string X = "x";
}
+
+ class BaseConstraintType1 // non-serializable
+ {
+ }
+
+ [Serializable]
+ class BaseConstraintType2
+ {
+ }
+
+ [DataContract]
+ class BaseConstraintType3 : BaseConstraintType1
+ {
+ }
+
+ [DataContract]
+ class BaseConstraintType4 : BaseConstraintType2
+ {
+ }
}
[DataContract]
}
}
+
+[DataContract (IsReference = true)]
+public class Parent
+{
+ //constructor
+ public Parent ()
+ {
+ Child = new Child (this);
+ }
+
+ [DataMember]
+ public Child Child;
+}
+
+[DataContract]
+public class Child
+{
+ public Child ()
+ {
+ }
+
+ public Child (Parent parent)
+ {
+ this.Parent = parent;
+ }
+
+ [DataMember]
+ public Parent Parent;
+}
+
#endregion