context: //
  // XmlObjectSerializerTest.cs
  //
  // Author:
  //	Atsushi Enomoto <atsushi@ximian.com>
  //	Ankit Jain <JAnkit@novell.com>
  //
  // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
  
  //
  // Permission is hereby granted, free of charge, to any person obtaining
  // a copy of this software and associated documentation files (the
  // "Software"), to deal in the Software without restriction, including
  // without limitation the rights to use, copy, modify, merge, publish,
  // distribute, sublicense, and/or sell copies of the Software, and to
  // permit persons to whom the Software is furnished to do so, subject to
  // the following conditions:
  // 
  // The above copyright notice and this permission notice shall be
  // included in all copies or substantial portions of the Software.
  // 
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  //
  
  //
  // This test code contains tests for both DataContractSerializer and
  // NetDataContractSerializer. The code could be mostly common.
  //
  
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Data;
  using System.IO;
  using System.Linq;
  using System.Net;
  using System.Runtime.Serialization;
  using System.Text;
  using System.Xml;
  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
  
  namespace MonoTests.System.Runtime.Serialization
  {
  	[TestFixture]
  	public class DataContractSerializerTest
  	{
  		static readonly XmlWriterSettings settings;
  
  		static DataContractSerializerTest ()
  		{
  			settings = new XmlWriterSettings ();
  			settings.OmitXmlDeclaration = true;
  		}
  
  		[DataContract]
  		class Sample1
  		{
  			[DataMember]
  			public string Member1;
  		}
  
  		[Test]
  		[ExpectedException (typeof (ArgumentNullException))]
  		public void ConstructorTypeNull ()
  		{
  			new DataContractSerializer (null);
  		}
  
  		[Test]
  		public void ConstructorKnownTypesNull ()
  		{
  			// null knownTypes is allowed. Though the property is filled.
  			Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), (IEnumerable<Type>)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]
  		[ExpectedException (typeof (ArgumentNullException))]
  		public void ConstructorNameNull ()
  		{
  			new DataContractSerializer (typeof (Sample1), null, String.Empty);
  		}
  
  		[Test]
  		[ExpectedException (typeof (ArgumentNullException))]
  		public void ConstructorNamespaceNull ()
  		{
  			new DataContractSerializer (typeof (Sample1), "foo", null);
  		}
  
  		[Test]
  		[ExpectedException (typeof (ArgumentOutOfRangeException))]
  		public void ConstructorNegativeMaxObjects ()
  		{
  			new DataContractSerializer (typeof (Sample1), null,
  				-1, false, false, null);
  		}
  
  		[Test]
  		public void ConstructorMisc ()
  		{
  			new DataContractSerializer (typeof (GlobalSample1));
  		}
  
  		[Test]
  		public void WriteObjectContent ()
  		{
  			StringWriter sw = new StringWriter ();
  			using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
  				DataContractSerializer ser =
  					new DataContractSerializer (typeof (string));
  				xw.WriteStartElement ("my-element");
  				ser.WriteObjectContent (xw, "TEST STRING");
  				xw.WriteEndElement ();
  			}
  			Assert.AreEqual ("<my-element>TEST STRING</my-element>",
  				sw.ToString ());
  		}
  
  		[Test]
  		public void WriteObjectToStream ()
  		{
  			DataContractSerializer ser =
  				new DataContractSerializer (typeof (int));
  			MemoryStream sw = new MemoryStream ();
  			ser.WriteObject (sw, 1);
  			string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
  			Assert.AreEqual (expected, Encoding.UTF8.GetString (sw.ToArray ()));
  		}
  
  		[Test]
  		public void ReadObjectFromStream ()
  		{
  			DataContractSerializer ser =
  				new DataContractSerializer (typeof (int));
  			string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
  			byte[] buf = Encoding.UTF8.GetBytes (expected);
  			MemoryStream sw = new MemoryStream (buf);
  			object res = ser.ReadObject (sw);
  			Assert.AreEqual (1, res);
  		}
  
  		// int
  
  		[Test]
  		public void SerializeInt ()
  		{
  			DataContractSerializer ser =
  				new DataContractSerializer (typeof (int));
  			SerializeInt (ser, "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>");
  		}
  
  
  		[Test]
  		[Category ("NotWorking")]
  		public void NetSerializeInt ()
  		{
  			NetDataContractSerializer ser =
  				new NetDataContractSerializer ();
  			// z:Assembly="0" ???
  			SerializeInt (ser, String.Format ("<int z:Type=\"System.Int32\" z:Assembly=\"0\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>", typeof (int).Assembly.FullName));
  		}
  
  		void SerializeInt (XmlObjectSerializer ser, string expected)
  		{
  			StringWriter sw = new StringWriter ();
  			using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  				ser.WriteObject (w, 1);
  			}
  			Assert.AreEqual (expected, sw.ToString ());
  		}
  
  		// pass typeof(DCEmpty), serialize int
  
  		[Test]
  		public void SerializeIntForDCEmpty ()
  		{
  			DataContractSerializer ser =
  				new DataContractSerializer (typeof (DCEmpty));
  			// tricky!
  			SerializeIntForDCEmpty (ser, "<DCEmpty xmlns:d1p1=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d1p1:int\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\">1</DCEmpty>");
  		}
  
  		void SerializeIntForDCEmpty (XmlObjectSerializer ser, string expected)
  		{
  			StringWriter sw = new StringWriter ();
  			using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  				ser.WriteObject (w, 1);
  			}
  			XmlComparer.AssertAreEqual (expected, sw.ToString ());
  		}
  
  		// DCEmpty
  
  		[Test]
  		public void SerializeEmptyClass ()
  		{
  			DataContractSerializer ser =
  				new DataContractSerializer (typeof (DCEmpty));
  			SerializeEmptyClass (ser, "<DCEmpty xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />");
  		}
  
  		[Test]
  		[Category ("NotWorking")]
  		public void NetSerializeEmptyClass ()
  		{
  			NetDataContractSerializer ser =
  				new NetDataContractSerializer ();
  			SerializeEmptyClass (ser, String.Format ("<DCEmpty xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" z:Id=\"1\" z:Type=\"MonoTests.System.Runtime.Serialization.DCEmpty\" z:Assembly=\"{0}\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />", this.GetType ().Assembly.FullName));
  		}
  
  		void SerializeEmptyClass (XmlObjectSerializer ser, string expected)
  		{
  			StringWriter sw = new StringWriter ();
  			using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  				ser.WriteObject (w, new DCEmpty ());
  			}
  			Assert.AreEqual (expected, sw.ToString ());
  		}
  
  		// DCEmpty
  
  		[Test]
  		public void SerializeEmptyNoNSClass ()
  		{
  			var ser = new DataContractSerializer (typeof (DCEmptyNoNS));
  			SerializeEmptyNoNSClass (ser, "<DCEmptyNoNS xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" />");
  		}
  
  		void SerializeEmptyNoNSClass (XmlObjectSerializer ser, string expected)
  		{
  			var sw = new StringWriter ();
  			using (var w = XmlWriter.Create (sw, settings)) {
  				ser.WriteObject (w, new DCEmptyNoNS ());
  			}
  			Assert.AreEqual (expected, sw.ToString ());
  		}
  		// string (primitive)
  
  		[Test]
  		public void SerializePrimitiveString ()
  		{
  			XmlObjectSerializer ser =
  
gt: 				new DataContractSerializer (typeof (string));