//
// System.Xml.Serialization.XmlSchemaExporterTests
//
// Author:
// Gert Driesen (drieseng@users.sourceforge.net)
//
// (C) 2005 Novell
//
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using NUnit.Framework;
using MonoTests.System.Xml.TestClasses;
namespace MonoTests.System.XmlSerialization
{
[TestFixture]
public class XmlSchemaExporterTests
{
const string ANamespace = "some:urn";
const string AnotherNamespace = "another:urn";
string Infoset (string source)
{
return XmlSerializerTests.Infoset (source);
}
private XmlSchemas Export (Type type)
{
return Export (type, string.Empty);
}
private XmlSchemas Export (Type type, string defaultNamespace)
{
XmlReflectionImporter ri = new XmlReflectionImporter (defaultNamespace);
XmlSchemas schemas = new XmlSchemas ();
XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
XmlTypeMapping tm = ri.ImportTypeMapping (type);
sx.ExportTypeMapping (tm);
return schemas;
}
private XmlSchemas Export (Type type, XmlAttributeOverrides overrides)
{
return Export (type, overrides, string.Empty);
}
private XmlSchemas Export (Type type, XmlAttributeOverrides overrides, string defaultNamespace)
{
XmlReflectionImporter ri = new XmlReflectionImporter (overrides, defaultNamespace);
XmlSchemas schemas = new XmlSchemas ();
XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
XmlTypeMapping tm = ri.ImportTypeMapping (type);
sx.ExportTypeMapping (tm);
return schemas;
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
public void ExportStruct ()
{
XmlSchemas schemas = Export (typeof (TimeSpan), "NSTimeSpan");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (TimeSpan));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
public void ExportStruct_Array ()
{
XmlSchemas schemas = Export (typeof (TimeSpan[]), "NSTimeSpanArray");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (TimeSpan[]));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
public void ExportClass_SimpleClass ()
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
XmlAttributes attr = new XmlAttributes ();
XmlElementAttribute element = new XmlElementAttribute ();
element.ElementName = "saying";
element.IsNullable = true;
attr.XmlElements.Add (element);
overrides.Add (typeof (SimpleClass), "something", attr);
XmlSchemas schemas = Export (typeof (SimpleClass), overrides, "NSSimpleClass");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (SimpleClass), overrides);
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
public void ExportClass_StringCollection ()
{
XmlSchemas schemas = Export (typeof (StringCollection), "NSStringCollection");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (StringCollection));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
public void ExportClass_StringCollectionContainer ()
{
XmlSchemas schemas = Export (typeof (StringCollectionContainer), "NSStringCollectionContainer");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (StringCollectionContainer));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
public void ExportClass_ArrayContainer ()
{
XmlSchemas schemas = Export (typeof (ArrayContainer), "NSArrayContainer");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (ArrayContainer));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
public void ExportClass_ClassArrayContainer ()
{
XmlSchemas schemas = Export (typeof (ClassArrayContainer), "NSClassArrayContainer");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (ClassArrayContainer));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
public void ExportClass_SimpleClassWithXmlAttributes ()
{
XmlSchemas schemas = Export (typeof (SimpleClassWithXmlAttributes), "NSSimpleClassWithXmlAttributes");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (SimpleClassWithXmlAttributes));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
public void ExportClass_Field ()
{
XmlSchemas schemas = Export (typeof (Field), "NSField");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (Field));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
public void ExportClass_MyList ()
{
XmlSchemas schemas = Export (typeof (MyList), "NSMyList");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (MyList));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
}
[Test]
public void ExportClass_Container ()
{
XmlSchemas schemas = Export (typeof (Container), "NSContainer");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (Container));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
public void ExportClass_Container2 ()
{
XmlSchemas schemas = Export (typeof (Container2), "NSContainer2");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (Container2));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
[ExpectedException (typeof (InvalidOperationException))] // Cannot use wildcards at the top level of a schema.
public void ExportClass_MyElem ()
{
Export (typeof (MyElem), "NSMyElem");
}
[Test]
public void ExportClass_CDataContainer ()
{
XmlSchemas schemas = Export (typeof (CDataContainer), "NSCDataContainer");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (CDataContainer));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
}
[Test]
public void ExportClass_NodeContainer ()
{
XmlSchemas schemas = Export (typeof (NodeContainer), "NSNodeContainer");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (NodeContainer));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
[Category ("NotWorking")] // Mono does not generate the node
[Category ("NotDotNet")] // MS.NET randomly modifies the order of the elements! -> Of course. There is nothing strange. The test is rather strange.
public void ExportClass_Choices ()
{
XmlSchemas schemas = Export (typeof (Choices), "NSChoices");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (Choices));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
#if ONLY_1_1
[Category ("NotDotNet")] // MS.NET 1.x does not escape spaces in a type name, bug is fixed in .NET 2.0
#endif
public void ExportClass_TestSpace ()
{
XmlSchemas schemas = Export (typeof (TestSpace), "NSTestSpace");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (TestSpace));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
public void ExportClass_OptionalValueTypeContainer ()
{
XmlAttributeOverrides overrides;
XmlAttributes attr;
XmlSchemas schemas = Export (typeof (OptionalValueTypeContainer));
Assert.AreEqual (2, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
"", Environment.NewLine, AnotherNamespace, ANamespace),
sw.ToString (), "#2");
sw.GetStringBuilder ().Length = 0;
schemas[1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine, ANamespace), sw.ToString (), "#3");
overrides = new XmlAttributeOverrides ();
attr = new XmlAttributes ();
// remove the DefaultValue attribute on the Flags member
overrides.Add (typeof (OptionalValueTypeContainer), "Flags", attr);
// remove the DefaultValue attribute on the Attributes member
overrides.Add (typeof (OptionalValueTypeContainer), "Attributes", attr);
// remove the DefaultValue attribute on the IsEmpty member
overrides.Add (typeof (OptionalValueTypeContainer), "IsEmpty", attr);
// remove the DefaultValue attribute on the IsNull member
overrides.Add (typeof (OptionalValueTypeContainer), "IsNull", attr);
schemas = Export (typeof (OptionalValueTypeContainer), overrides, "urn:myNS");
Assert.AreEqual (2, schemas.Count, "#4");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
"", Environment.NewLine, AnotherNamespace, ANamespace),
sw.ToString (), "#5");
sw.GetStringBuilder ().Length = 0;
schemas[1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine, ANamespace), sw.ToString (), "#6");
}
[Test]
public void ExportClass_ReadOnlyProperties ()
{
XmlSchemas schemas = Export (typeof (ReadOnlyProperties), "NSReadOnlyProperties");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (ReadOnlyProperties));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
public void ExportClass_ListDefaults ()
{
XmlSchemas schemas = Export (typeof (ListDefaults), "NSListDefaults");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (ListDefaults));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
public void ExportClass_ClsPerson ()
{
XmlSchemas schemas = Export (typeof (clsPerson), "NSClsPerson");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (clsPerson));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
public void ExportClass_ArrayClass ()
{
XmlSchemas schemas = Export (typeof (ArrayClass), "NSArrayClass");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (ArrayClass));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
public void ExportClass_StructContainer ()
{
XmlSchemas schemas = Export (typeof (StructContainer), "NSStructContainer");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (StructContainer));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
public void ExportClass_TestDefault ()
{
XmlSchemas schemas = Export (typeof (TestDefault));
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas [0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (TestDefault), "NSTestDefault");
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas [0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
public void ExportClass_TestDefault_Overrides ()
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
XmlAttributes attr = new XmlAttributes ();
XmlTypeAttribute xmlType = new XmlTypeAttribute ("flagenum");
xmlType.Namespace = "yetanother:urn";
attr.XmlType = xmlType;
overrides.Add (typeof (FlagEnum_Encoded), attr);
XmlSchemas schemas = Export (typeof (TestDefault), overrides, "NSTestDefault");
Assert.AreEqual (2, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas [0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
sw.GetStringBuilder ().Length = 0;
schemas [1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#3");
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
[ExpectedException (typeof (InvalidOperationException))] // Cannot use wildcards at the top level of a schema.
public void ExportClass_XmlElement ()
{
XmlSchemas schemas = Export (typeof (XmlElement), "NS1");
}
[Test]
public void ExportClass_Array ()
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
XmlAttributes attr = new XmlAttributes ();
XmlElementAttribute element = new XmlElementAttribute ();
element.ElementName = "saying";
element.IsNullable = true;
attr.XmlElements.Add (element);
overrides.Add (typeof (SimpleClass), "something", attr);
XmlSchemas schemas = Export (typeof (SimpleClass[]), overrides, "NSSimpleClassArray");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (SimpleClass[]), overrides);
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
public void ExportEnum ()
{
XmlSchemas schemas = Export (typeof (EnumDefaultValue), "NSEnumDefaultValue");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (EnumDefaultValueNF), "NSEnumDefaultValueNF");
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
schemas = Export (typeof (EnumDefaultValue));
Assert.AreEqual (1, schemas.Count, "#5");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#6");
schemas = Export (typeof (EnumDefaultValueNF));
Assert.AreEqual (1, schemas.Count, "#7");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#8");
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
public void ExportXmlSerializable ()
{
XmlSchemas schemas = Export (typeof (Employee), "NSEmployee");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (Employee));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
public void ExportXmlSerializable_Schema ()
{
XmlSchemas schemas = Export (typeof (EmployeeSchema), "NSEmployeeSchema");
Assert.AreEqual (2, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
#if ONLY_1_1
" {0}" +
#endif
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
sw.GetStringBuilder ().Length = 0;
schemas[1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#3");
schemas = Export (typeof (EmployeeSchema));
Assert.AreEqual (2, schemas.Count, "#4");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
#if ONLY_1_1
" {0}" +
#endif
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#5");
sw.GetStringBuilder ().Length = 0;
schemas[1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#6");
schemas = Export (typeof (PrimitiveSchema), "NSPrimitiveSchema");
Assert.AreEqual (2, schemas.Count, "#7");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#8");
sw.GetStringBuilder ().Length = 0;
schemas[1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#9");
}
[Test]
[ExpectedException (typeof (InvalidOperationException))] // Schema Id is missing
public void ExportXmlSerializable_MissingID ()
{
Export (typeof (MissingIDSchema));
}
[ExpectedException (typeof (InvalidOperationException))]
public void ExportXmlSerializable_DuplicateNamespace ()
{
try {
Export (typeof (PrimitiveSchema));
Assert.Fail ("#1");
} catch (InvalidOperationException) {
// The namespace, , is a duplicate.
}
try {
Export (typeof (XmlSerializableContainer));
Assert.Fail ("#2");
} catch (InvalidOperationException) {
// The namespace, , is a duplicate.
}
}
[Test]
#if !NET_2_0
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
#endif
public void ExportXmlSerializable_SchemaProvider ()
{
XmlSchemas schemas = Export (typeof (EmployeeSchemaProvider), "NSEmployeeSchemaProvider");
//Assert.AreEqual (1, schemas.Count, "#1"); //# of returned schemas is checked in ExportXmlSerializable_SchemaProvider1
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
#if NET_2_0
" {0}" +
" {0}" +
#else
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#endif
"", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
schemas = Export (typeof (EmployeeSchemaProvider));
//Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
#if NET_2_0
" {0}" +
" {0}" +
#else
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#endif
"", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
schemas = Export (typeof (PrimitiveSchemaProvider), "NSPrimitiveSchemaProvider");
//Assert.AreEqual (1, schemas.Count, "#5");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
#if NET_2_0
//" {0}" +
" {0}" +
#else
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#endif
"", Environment.NewLine)), Infoset (sw.ToString ().Replace("" + Environment.NewLine, "")), "#6");
}
[Test]
#if NET_2_0
[Category ("NotWorking")] // support for XmlSchemaProvider is not implemented
#else
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
#endif
public void ExportXmlSerializable_SchemaProvider1 () {
XmlSchemas schemas = schemas = Export (typeof (PrimitiveSchemaProvider));
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
#if NET_2_0
" {0}" +
#else
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#endif
"", Environment.NewLine), sw.ToString (), "#8");
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
#if NET_2_0
[Category ("NotWorking")] // support for XmlSchemaProvider is not implemented
#endif
public void ExportXmlSerializable_Container ()
{
XmlSchemas schemas = Export (typeof (XmlSerializableContainer), "NSXmlSerializableContainer");
Assert.AreEqual (3, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
#if NET_2_0
" {0}" +
#endif
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#if NET_2_0
" {0}" +
#else
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#endif
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#if NET_2_0
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#else
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#endif
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
sw.GetStringBuilder ().Length = 0;
schemas[1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
#if NET_2_0
" {0}" +
#endif
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#if NET_2_0
" {0}" +
#else
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#endif
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#if NET_2_0
" {0}" +
#else
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#endif
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#if NET_2_0
" {0}" +
#else
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#endif
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#if NET_2_0
" {0}" +
#else
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
#endif
"", Environment.NewLine), sw.ToString (), "#3");
sw.GetStringBuilder ().Length = 0;
schemas[2].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
public void ExportXsdPrimitive ()
{
ArrayList types = new ArrayList ();
types.Add (new TypeDescription (typeof (byte), true, "unsignedByte", "Byte"));
types.Add (new TypeDescription (typeof (sbyte), true, "byte", "Byte"));
types.Add (new TypeDescription (typeof (bool), true, "boolean", "Boolean"));
types.Add (new TypeDescription (typeof (short), true, "short", "Short"));
types.Add (new TypeDescription (typeof (int), true, "int", "Int"));
types.Add (new TypeDescription (typeof (long), true, "long", "Long"));
types.Add (new TypeDescription (typeof (float), true, "float", "Float"));
types.Add (new TypeDescription (typeof (double), true, "double", "Double"));
types.Add (new TypeDescription (typeof (decimal), true, "decimal", "Decimal"));
types.Add (new TypeDescription (typeof (ushort), true, "unsignedShort", "UnsignedShort"));
types.Add (new TypeDescription (typeof (uint), true, "unsignedInt", "UnsignedInt"));
types.Add (new TypeDescription (typeof (ulong), true, "unsignedLong", "UnsignedLong"));
types.Add (new TypeDescription (typeof (DateTime), true, "dateTime", "DateTime"));
#if NET_2_0
types.Add (new TypeDescription (typeof (XmlQualifiedName), true, "QName", "QName", true));
#else
types.Add (new TypeDescription (typeof (XmlQualifiedName), true, "QName", "QName"));
#endif
types.Add (new TypeDescription (typeof (string), true, "string", "String", true));
foreach (TypeDescription typeDesc in types) {
XmlSchemas schemas = Export (typeDesc.Type, typeDesc.Type.Name);
Assert.AreEqual (1, schemas.Count, typeDesc.Type.FullName + "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
"", Environment.NewLine, typeDesc.Type.Name, typeDesc.XmlType,
typeDesc.IsNillable ? "nillable=\"true\" " : ""),
sw.ToString (), typeDesc.Type.FullName + "#2");
schemas = Export (typeDesc.Type);
Assert.AreEqual (1, schemas.Count, typeDesc.Type.FullName + "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
"", Environment.NewLine, typeDesc.XmlType,
typeDesc.IsNillable ? "nillable=\"true\" " : ""),
sw.ToString (), typeDesc.Type.FullName + "#4");
}
}
[Test]
[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
public void ExportXsdPrimitive_Object ()
{
XmlSchemas schemas = Export (typeof (object), "NSAnyType");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (object));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
public void ExportXsdPrimitive_ByteArray ()
{
XmlSchemas schemas = Export (typeof (byte[]), "NSByteArray");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (byte[]));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
public void ExportXsdPrimitive_Arrays ()
{
ArrayList types = new ArrayList ();
types.Add (new TypeDescription (typeof (sbyte[]), true, "byte", "Byte", true));
types.Add (new TypeDescription (typeof (bool[]), true, "boolean", "Boolean", true));
types.Add (new TypeDescription (typeof (short[]), true, "short", "Short", true));
types.Add (new TypeDescription (typeof (int[]), true, "int", "Int", true));
types.Add (new TypeDescription (typeof (long[]), true, "long", "Long", true));
types.Add (new TypeDescription (typeof (float[]), true, "float", "Float", true));
types.Add (new TypeDescription (typeof (double[]), true, "double", "Double", true));
types.Add (new TypeDescription (typeof (decimal[]), true, "decimal", "Decimal", true));
types.Add (new TypeDescription (typeof (ushort[]), true, "unsignedShort", "UnsignedShort", true));
types.Add (new TypeDescription (typeof (uint[]), true, "unsignedInt", "UnsignedInt", true));
types.Add (new TypeDescription (typeof (ulong[]), true, "unsignedLong", "UnsignedLong", true));
types.Add (new TypeDescription (typeof (DateTime[]), true, "dateTime", "DateTime", true));
#if NET_2_0
types.Add (new TypeDescription (typeof (XmlQualifiedName[]), true, "QName", "QName", true, true));
#else
types.Add (new TypeDescription (typeof (XmlQualifiedName[]), true, "QName", "QName", true));
#endif
types.Add (new TypeDescription (typeof (string[]), true, "string", "String", true, true));
foreach (TypeDescription typeDesc in types) {
XmlSchemas schemas = Export (typeDesc.Type, typeDesc.Type.Name);
Assert.AreEqual (1, schemas.Count, typeDesc.Type.FullName + "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine, typeDesc.Type.Name, typeDesc.ArrayType, typeDesc.XmlType,
typeDesc.XsdType ? "xs" : "tns", typeDesc.IsNillable ? "nillable=\"true\" " : "",
typeDesc.IsElementNillable ? "nillable=\"true\" " : ""),
sw.ToString (), typeDesc.Type.FullName + "#2" + "|" + typeDesc.IsNillable);
schemas = Export (typeDesc.Type);
Assert.AreEqual (1, schemas.Count, typeDesc.Type.FullName + "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine, typeDesc.ArrayType, typeDesc.XmlType,
typeDesc.XsdType ? "xs" : "tns", typeDesc.IsNillable ? "nillable=\"true\" " : "",
typeDesc.IsElementNillable ? "nillable=\"true\" " : ""),
sw.ToString (), typeDesc.Type.FullName + "#4");
}
}
[Test]
public void ExportXsdPrimitive_Object_Arrays ()
{
XmlSchemas schemas = Export (typeof (object[]), "NSArrayOfAnyType");
Assert.AreEqual (1, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
schemas = Export (typeof (object[]));
Assert.AreEqual (1, schemas.Count, "#3");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#4");
}
[Test]
public void ExportNonXsdPrimitive_Guid ()
{
XmlSchemas schemas = Export (typeof (Guid), "NSPrimGuid");
Assert.AreEqual (2, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
sw.GetStringBuilder ().Length = 0;
schemas[1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#3");
schemas = Export (typeof (Guid));
Assert.AreEqual (2, schemas.Count, "#4");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#5");
sw.GetStringBuilder ().Length = 0;
schemas[1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#6");
}
[Test]
public void ExportNonXsdPrimitive_Char ()
{
XmlSchemas schemas = Export (typeof (char), "NSPrimChar");
Assert.AreEqual (2, schemas.Count, "#1");
StringWriter sw = new StringWriter ();
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#2");
sw.GetStringBuilder ().Length = 0;
schemas[1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#3");
schemas = Export (typeof (char));
Assert.AreEqual (2, schemas.Count, "#4");
sw.GetStringBuilder ().Length = 0;
schemas[0].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#5");
sw.GetStringBuilder ().Length = 0;
schemas[1].Write (sw);
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
"", Environment.NewLine), sw.ToString (), "#6");
}
public class Employee : IXmlSerializable
{
private string _firstName;
private string _lastName;
private string _address;
public virtual XmlSchema GetSchema ()
{
return null;
}
public void WriteXml (XmlWriter writer)
{
writer.WriteStartElement ("employee", "urn:devx-com");
writer.WriteAttributeString ("firstName", _firstName);
writer.WriteAttributeString ("lastName", _lastName);
writer.WriteAttributeString ("address", _address);
writer.WriteEndElement ();
}
public void ReadXml (XmlReader reader)
{
XmlNodeType type = reader.MoveToContent ();
if (type == XmlNodeType.Element && reader.LocalName == "employee") {
_firstName = reader["firstName"];
_lastName = reader["lastName"];
_address = reader["address"];
}
}
}
public class EmployeeSchema : IXmlSerializable
{
private string _firstName;
private string _lastName;
private string _address;
public virtual XmlSchema GetSchema ()
{
return CreateSchema ();
}
public void WriteXml (XmlWriter writer)
{
writer.WriteStartElement ("employee", "urn:devx-com");
writer.WriteAttributeString ("firstName", _firstName);
writer.WriteAttributeString ("lastName", _lastName);
writer.WriteAttributeString ("address", _address);
writer.WriteEndElement ();
}
public void ReadXml (XmlReader reader)
{
XmlNodeType type = reader.MoveToContent ();
if (type == XmlNodeType.Element && reader.LocalName == "employee") {
_firstName = reader["firstName"];
_lastName = reader["lastName"];
_address = reader["address"];
}
}
protected static XmlSchema CreateSchema ()
{
XmlSchema schema = new XmlSchema ();
schema.Id = "EmployeeSchema";
schema.TargetNamespace = "urn:types-devx-com";
XmlSchemaComplexType type = new XmlSchemaComplexType ();
type.Name = "employeeRoot";
XmlSchemaAttribute firstNameAttr = new XmlSchemaAttribute ();
firstNameAttr.Name = "firstName";
type.Attributes.Add (firstNameAttr);
XmlSchemaAttribute lastNameAttr = new XmlSchemaAttribute ();
lastNameAttr.Name = "lastName";
type.Attributes.Add (lastNameAttr);
XmlSchemaAttribute addressAttr = new XmlSchemaAttribute ();
addressAttr.Name = "address";
type.Attributes.Add (addressAttr);
XmlSchemaElement employeeElement = new XmlSchemaElement ();
employeeElement.Name = "employee";
XmlQualifiedName name = new XmlQualifiedName ("employeeRoot", "urn:types-devx-com");
employeeElement.SchemaTypeName = name;
schema.Items.Add (type);
schema.Items.Add (employeeElement);
return schema;
}
}
public class PrimitiveSchema : IXmlSerializable
{
private string _firstName;
private string _lastName;
private string _address;
public virtual XmlSchema GetSchema ()
{
XmlSchema schema = new XmlSchema ();
schema.Id = "LuckyNumberSchema";
XmlSchemaElement luckyNumberElement = new XmlSchemaElement ();
luckyNumberElement.Name = "LuckyNumber";
luckyNumberElement.SchemaTypeName = new XmlQualifiedName ("int", "http://www.w3.org/2001/XMLSchema");
schema.Items.Add (luckyNumberElement);
return schema;
}
public void WriteXml (XmlWriter writer)
{
writer.WriteStartElement ("employee", "urn:devx-com");
writer.WriteAttributeString ("firstName", _firstName);
writer.WriteAttributeString ("lastName", _lastName);
writer.WriteAttributeString ("address", _address);
writer.WriteEndElement ();
}
public void ReadXml (XmlReader reader)
{
XmlNodeType type = reader.MoveToContent ();
if (type == XmlNodeType.Element && reader.LocalName == "employee") {
_firstName = reader["firstName"];
_lastName = reader["lastName"];
_address = reader["address"];
}
}
}
public class MissingIDSchema : IXmlSerializable
{
private string _firstName;
private string _lastName;
private string _address;
public virtual XmlSchema GetSchema ()
{
XmlSchema schema = new XmlSchema ();
XmlSchemaElement luckyNumberElement = new XmlSchemaElement ();
luckyNumberElement.Name = "LuckyNumber";
luckyNumberElement.SchemaTypeName = new XmlQualifiedName ("int", "http://www.w3.org/2001/XMLSchema");
return schema;
}
public void WriteXml (XmlWriter writer)
{
writer.WriteStartElement ("employee", "urn:devx-com");
writer.WriteAttributeString ("firstName", _firstName);
writer.WriteAttributeString ("lastName", _lastName);
writer.WriteAttributeString ("address", _address);
writer.WriteEndElement ();
}
public void ReadXml (XmlReader reader)
{
XmlNodeType type = reader.MoveToContent ();
if (type == XmlNodeType.Element && reader.LocalName == "employee") {
_firstName = reader["firstName"];
_lastName = reader["lastName"];
_address = reader["address"];
}
}
}
#if NET_2_0
[XmlSchemaProvider ("CreateEmployeeSchema")]
#endif
public class EmployeeSchemaProvider : EmployeeSchema
{
#if NET_2_0
public static XmlQualifiedName CreateEmployeeSchema (XmlSchemaSet schemaSet)
{
schemaSet.Add (CreateSchema ());
return new XmlQualifiedName ("employeeRoot", "urn:types-devx-com");
}
#else
public override XmlSchema GetSchema ()
{
return null;
}
#endif
}
#if NET_2_0
[XmlSchemaProvider ("CreateLuckyNumberSchema")]
#endif
public class PrimitiveSchemaProvider : IXmlSerializable
{
#if NET_2_0
public static XmlQualifiedName CreateLuckyNumberSchema (XmlSchemaSet schemaSet)
{
XmlSchema schema = new XmlSchema ();
XmlSchemaElement luckyNumberElement = new XmlSchemaElement ();
luckyNumberElement.Name = "LuckyNumber";
XmlQualifiedName typeName = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");
luckyNumberElement.SchemaTypeName = typeName;
schema.Items.Add (luckyNumberElement);
schemaSet.Add (schema);
return typeName;
}
#endif
public XmlSchema GetSchema ()
{
return null;
}
public void WriteXml (XmlWriter writer)
{
writer.WriteElementString ("LuckyNumber", "7");
}
public void ReadXml (XmlReader reader)
{
XmlNodeType type = reader.MoveToContent ();
if (type == XmlNodeType.Element && reader.LocalName == "LuckyNumber") {
}
}
}
private class TypeDescription
{
public TypeDescription (Type type, bool xsdType, string xmlType, string arrayType) : this (type, xsdType, xmlType, arrayType, false)
{
}
public TypeDescription (Type type, bool xsdType, string xmlType, string arrayType, bool isNillable) :
this (type, xsdType, xmlType, arrayType, isNillable, false)
{
}
public TypeDescription (Type type, bool xsdType, string xmlType, string arrayType, bool isNillable, bool isElementNillable)
{
_type = type;
_xsdType = xsdType;
_xmlType = xmlType;
_arrayType = arrayType;
_isNillable = isNillable;
_isElementNillable = isElementNillable;
}
public Type Type {
get { return _type; }
}
public string XmlType {
get { return _xmlType; }
}
public string ArrayType {
get { return _arrayType; }
}
public bool XsdType {
get { return _xsdType; }
}
public bool IsNillable {
get { return _isNillable; }
}
public bool IsElementNillable {
get { return _isElementNillable; }
}
private Type _type;
private bool _xsdType;
private string _xmlType;
private string _arrayType;
private bool _isNillable;
private bool _isElementNillable;
}
public class StructContainer
{
public EnumDefaultValue Value;
public TimeSpan[] Times;
}
public class XmlSerializableContainer
{
public Employee Slave;
public EmployeeSchema SlaveSchema;
public EmployeeSchemaProvider SlaveSchemaProvider;
public PrimitiveSchema NativeSchema;
public PrimitiveSchemaProvider NativeSchemaProvider;
[XmlElement ("SlaveNamespace", Namespace = "urn:types-devx-com")]
public Employee SlaveNS;
[XmlElement ("SlaveSchemaNamespace", Namespace = "urn:types-devx-com")]
public EmployeeSchema SlaveSchemaNS;
[XmlElement ("SlaveSchemaProviderNamespace", Namespace = "urn:types-devx-com")]
public EmployeeSchemaProvider SlaveSchemaProviderNS;
[XmlElement ("NativeSchemaNamespace", Namespace = "urn:types-devx-com")]
public PrimitiveSchema NativeSchemaNS;
[XmlElement ("NativeSchemaProviderNamespace", Namespace = "urn:types-devx-com")]
public PrimitiveSchemaProvider NativeSchemaProviderNS;
[XmlElement (Namespace = "urn:types-devx-com")]
public Employee SlaveNSOnly;
[XmlElement (Namespace = "urn:types-devx-com")]
public EmployeeSchema SlaveSchemaNSOnly;
[XmlElement (Namespace = "urn:types-devx-com")]
public EmployeeSchemaProvider SlaveSchemaProviderNSOnly;
[XmlElement (Namespace = "urn:types-devx-com")]
public PrimitiveSchema NativeSchemaNSOnly;
[XmlElement (Namespace = "urn:types-devx-com")]
public PrimitiveSchemaProvider NativeSchemaProviderNSOnly;
}
}
}