Skip to content

Commit

Permalink
2003-11-28 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
Browse files Browse the repository at this point in the history
	* XmlWriterTests.cs : Added WriteNodeFullDocument() with
	  encoding specification. Added WriteNodeNonEmptyElement().
	* XmlUrlResolverTests.cs : Added GetEntityWithNonStreamReturnType()
	  and GetEntityWithRelativeFileUri().
	* XmlTextWriterTests.cs : Added some namespace related tests.
	* XmlTextReaderTests.cs : Added char-entity and ReadBase64() tests.
	* XmlNodeTests.cs : GetPrefixOfNamespace() test also for disconnected
	  nodes.
	* XmlElementTests.cs : Added ConstructionAndDefaultAttributes().
	* XmlDeclarationTests.cs : Added InvalidInnerText().
	* XmlAttributeTests.cs : Added CheckPrefixWithNamespace().

svn path=/trunk/mcs/; revision=20590
  • Loading branch information
atsushieno committed Nov 28, 2003
1 parent acf66eb commit 1395cd6
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 59 deletions.
14 changes: 14 additions & 0 deletions mcs/class/System.XML/Test/System.Xml/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
2003-11-28 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>

* XmlWriterTests.cs : Added WriteNodeFullDocument() with
encoding specification. Added WriteNodeNonEmptyElement().
* XmlUrlResolverTests.cs : Added GetEntityWithNonStreamReturnType()
and GetEntityWithRelativeFileUri().
* XmlTextWriterTests.cs : Added some namespace related tests.
* XmlTextReaderTests.cs : Added char-entity and ReadBase64() tests.
* XmlNodeTests.cs : GetPrefixOfNamespace() test also for disconnected
nodes.
* XmlElementTests.cs : Added ConstructionAndDefaultAttributes().
* XmlDeclarationTests.cs : Added InvalidInnerText().
* XmlAttributeTests.cs : Added CheckPrefixWithNamespace().

2003-10-13 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>

* XmlWriterTests.cs : Added WriteSurrogateCharEntity().
Expand Down
12 changes: 12 additions & 0 deletions mcs/class/System.XML/Test/System.Xml/XmlAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public void AttributeWithValue ()
AssertEquals ("value", attribute.ChildNodes [0].Value);
}

[Test]
[ExpectedException (typeof (ArgumentException))]
public void CheckPrefixWithNamespace ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<root xmlns:foo='urn:foo' foo='attfoo' foo:foo='attfoofoo' />");
// hogehoge does not match to any namespace.
AssertEquals ("xmlns:foo", doc.DocumentElement.Attributes [0].Name);
doc.DocumentElement.Attributes [0].Prefix="hogehoge";
doc.Save (Console.Out);
}

[Test]
public void NamespaceAttributes ()
{
Expand Down
13 changes: 13 additions & 0 deletions mcs/class/System.XML/Test/System.Xml/XmlElementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ public void CloneNode ()
AssertEquals ("Shallow clones shalt have no children!", false, shallow.HasChildNodes);
}

[Test]
public void ConstructionAndDefaultAttributes ()
{
string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root foo CDATA 'def'>]>";
string xml = dtd + "<root />";
XmlValidatingReader xvr = new XmlValidatingReader (new XmlTextReader (xml, XmlNodeType.Document, null));
XmlDocument doc = new XmlDocument ();
doc.Load (xvr);
Console.WriteLine (doc.DocumentElement.Attributes.Count);
Console.WriteLine (doc.CreateElement ("root").Attributes.Count);
Console.WriteLine (doc.CreateElement ("root2").Attributes.Count);
}

[Test]
public void CreateElement1 ()
{
Expand Down
79 changes: 43 additions & 36 deletions mcs/class/System.XML/Test/System.Xml/XmlNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,36 +204,36 @@ public void InsertAfter()
public void Normalize ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<root>This is the <b>hardest</b> one.</root>");
doc.NodeInserted += new XmlNodeChangedEventHandler (EventNodeInserted);
doc.NodeChanged += new XmlNodeChangedEventHandler (EventNodeChanged);
doc.NodeRemoved += new XmlNodeChangedEventHandler (EventNodeRemoved);

AssertEquals (3, doc.DocumentElement.ChildNodes.Count);

doc.DocumentElement.Normalize ();
AssertEquals (3, doc.DocumentElement.ChildNodes.Count);
Assert (changed);
inserted = changed = removed = false;

doc.DocumentElement.AppendChild (doc.CreateTextNode ("Addendum."));
AssertEquals (4, doc.DocumentElement.ChildNodes.Count);
inserted = changed = removed = false;

doc.DocumentElement.Normalize ();
AssertEquals (3, doc.DocumentElement.ChildNodes.Count);
Assert (changed);
Assert (removed);
inserted = changed = removed = false;

doc.DocumentElement.SetAttribute ("attr", "");
XmlAttribute attr = doc.DocumentElement.Attributes [0] as XmlAttribute;
AssertEquals (1, attr.ChildNodes.Count);
inserted = changed = removed = false;
attr.Normalize ();
// Such behavior violates DOM Level 2 Node#normalize(),
// but MS DOM is designed as such.
AssertEquals (1, attr.ChildNodes.Count);
doc.LoadXml ("<root>This is the <b>hardest</b> one.</root>");
doc.NodeInserted += new XmlNodeChangedEventHandler (EventNodeInserted);
doc.NodeChanged += new XmlNodeChangedEventHandler (EventNodeChanged);
doc.NodeRemoved += new XmlNodeChangedEventHandler (EventNodeRemoved);

AssertEquals (3, doc.DocumentElement.ChildNodes.Count);

doc.DocumentElement.Normalize ();
AssertEquals (3, doc.DocumentElement.ChildNodes.Count);
Assert (changed);
inserted = changed = removed = false;

doc.DocumentElement.AppendChild (doc.CreateTextNode ("Addendum."));
AssertEquals (4, doc.DocumentElement.ChildNodes.Count);
inserted = changed = removed = false;

doc.DocumentElement.Normalize ();
AssertEquals (3, doc.DocumentElement.ChildNodes.Count);
Assert (changed);
Assert (removed);
inserted = changed = removed = false;

doc.DocumentElement.SetAttribute ("attr", "");
XmlAttribute attr = doc.DocumentElement.Attributes [0] as XmlAttribute;
AssertEquals (1, attr.ChildNodes.Count);
inserted = changed = removed = false;
attr.Normalize ();
// Such behavior violates DOM Level 2 Node#normalize(),
// but MS DOM is designed as such.
AssertEquals (1, attr.ChildNodes.Count);
}

[Test]
Expand Down Expand Up @@ -285,6 +285,13 @@ public void GetPrefixOfNamespace ()
AssertEquals ("c2", String.Empty, document.DocumentElement.FirstChild.GetPrefixOfNamespace ("urn:foo"));
AssertEquals ("c3", "foo", document.DocumentElement.FirstChild.FirstChild.GetPrefixOfNamespace ("urn:foo"));

// disconnected nodes.
XmlNode n = document.CreateElement ("foo");
AssertEquals (String.Empty, n.GetPrefixOfNamespace ("foo"));
n = document.CreateTextNode ("text"); // does not have Attributes
AssertEquals (String.Empty, n.GetPrefixOfNamespace ("foo"));
n = document.CreateXmlDeclaration ("1.0", null, null); // does not have Attributes
AssertEquals (String.Empty, n.GetPrefixOfNamespace ("foo"));
}

[Test]
Expand Down Expand Up @@ -317,12 +324,12 @@ public void InnerText ()
[Test]
public void InnerXmlWithXmlns ()
{
XmlDocument document = new XmlDocument ();
XmlElement xel = document.CreateElement ("KeyValue", "http://www.w3.org/2000/09/xmldsig#");
xel.SetAttribute ("xmlns", "http://www.w3.org/2000/09/xmldsig#");
xel.InnerXml = "<DSAKeyValue>blablabla</DSAKeyValue>";
string expected = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue>blablabla</DSAKeyValue></KeyValue>";
AssertEquals (expected, xel.OuterXml);
XmlDocument document = new XmlDocument ();
XmlElement xel = document.CreateElement ("KeyValue", "http://www.w3.org/2000/09/xmldsig#");
xel.SetAttribute ("xmlns", "http://www.w3.org/2000/09/xmldsig#");
xel.InnerXml = "<DSAKeyValue>blablabla</DSAKeyValue>";
string expected = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue>blablabla</DSAKeyValue></KeyValue>";
AssertEquals (expected, xel.OuterXml);
}
}
}
59 changes: 58 additions & 1 deletion mcs/class/System.XML/Test/System.Xml/XmlTextReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,19 @@ public void ExternalDocument ()
doc.Load ("XmlFiles/nested-dtd-test.xml");
}

// MS.NET 1.0 fails this test.
[Test]
[ExpectedException (typeof (XmlException))]
public void NotAllowedCharRef ()
{
string xml = "<root>&#0;</root>";
XmlTextReader xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
xtr.Normalization = true;
xtr.Read ();
xtr.Read ();
}

[Test]
public void NotAllowedCharRefButPassNormalizationFalse ()
{
string xml = "<root>&#0;</root>";
XmlTextReader xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
Expand Down Expand Up @@ -709,5 +718,53 @@ public void IfNamespacesThenProhibitedAttributes ()
doc.LoadXml (xml);
}

[Test]
public void ReadBase64 ()
{
byte [] bytes = new byte [] {4,14,54,114,134,184,254,255};

string base64 = "<root><foo>BA42coa44</foo></root>";
XmlTextReader xtr = new XmlTextReader (base64, XmlNodeType.Document, null);
byte [] bytes2 = new byte [10];
xtr.Read (); // root
xtr.Read (); // foo
this.AssertNodeValues (xtr, XmlNodeType.Element, 1, false, "foo", String.Empty,
"foo", String.Empty, String.Empty, 0);
AssertEquals (6, xtr.ReadBase64 (bytes2, 0, 10));
this.AssertNodeValues (xtr, XmlNodeType.EndElement, 0, false, "root", String.Empty,
"root", String.Empty, String.Empty, 0);
Assert (!xtr.Read ());
AssertEquals (4, bytes2 [0]);
AssertEquals (14, bytes2 [1]);
AssertEquals (54, bytes2 [2]);
AssertEquals (114, bytes2 [3]);
AssertEquals (134, bytes2 [4]);
AssertEquals (184, bytes2 [5]);
AssertEquals (0, bytes2 [6]);

xtr = new XmlTextReader (base64, XmlNodeType.Document, null);
bytes2 = new byte [10];
xtr.Read (); // root
xtr.Read (); // foo
this.AssertNodeValues (xtr, XmlNodeType.Element, 1, false, "foo", String.Empty,
"foo", String.Empty, String.Empty, 0);

// Read less than 4 (i.e. one Base64 block)
AssertEquals (1, xtr.ReadBase64 (bytes2, 0, 1));
this.AssertNodeValues (xtr, XmlNodeType.Element, 1, false, "foo", String.Empty,
"foo", String.Empty, String.Empty, 0);
AssertEquals (4, bytes2 [0]);

AssertEquals (5, xtr.ReadBase64 (bytes2, 0, 10));
this.AssertNodeValues (xtr, XmlNodeType.EndElement, 0, false, "root", String.Empty,
"root", String.Empty, String.Empty, 0);
Assert (!xtr.Read ());
AssertEquals (14, bytes2 [0]);
AssertEquals (54, bytes2 [1]);
AssertEquals (114, bytes2 [2]);
AssertEquals (134, bytes2 [3]);
AssertEquals (184, bytes2 [4]);
AssertEquals (0, bytes2 [5]);
}
}
}
55 changes: 35 additions & 20 deletions mcs/class/System.XML/Test/System.Xml/XmlTextWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public void AttributeNamespacesXmlnsXmlns ()
xtw.WriteStartElement ("foo");
try {
xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
// This should not be allowed, even though
// MS.NET doesn't treat as an error.
// This should not be allowed, even though MS.NET doesn't treat as an error.
// See http://www.w3.org/TR/REC-xml-names/ Namespace Constraint: Prefix Declared
Fail ("any prefix which name starts from \"xml\" must not be allowed.");
}
catch (ArgumentException) {}
Expand Down Expand Up @@ -174,7 +174,7 @@ public void CDataInvalid ()
{
xtw.WriteCData("foo]]>bar");
}

[Test]
public void CloseOpenElements ()
{
Expand Down Expand Up @@ -354,6 +354,15 @@ public void DocumentStart ()
AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
}

[Test]
public void ElementAndAttributeSameXmlns ()
{
xtw.WriteStartElement ("ped", "foo", "urn:foo");
xtw.WriteStartAttribute ("ped", "foo", "urn:foo");
xtw.WriteEndElement ();
AssertEquals ("<ped:foo ped:foo='' xmlns:ped='urn:foo' />", StringWriterText);
}

[Test]
public void ElementEmpty ()
{
Expand Down Expand Up @@ -539,19 +548,17 @@ public void NamespacesPrefix ()
}

[Test]
public void NamespacesPrefixWithEmptyAndNullNamespace ()
[ExpectedException (typeof (ArgumentException))]
public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
{
try {
xtw.WriteStartElement ("foo", "bar", "");
Fail ("Should have thrown an ArgumentException.");
} catch (ArgumentException) {}
xtw.WriteStartElement ("foo", "bar", "");
}

try
{
xtw.WriteStartElement ("foo", "bar", null);
Fail ("Should have thrown an ArgumentException.");
}
catch (ArgumentException) {}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
{
xtw.WriteStartElement ("foo", "bar", null);
}

[Test]
Expand Down Expand Up @@ -658,6 +665,14 @@ public void WriteBase64 ()
} catch (ArgumentNullException) {}
}

[Test]
public void WriteBinHex ()
{
byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5};
xtw.WriteBinHex (bytes, 0, 11);
AssertEquals ("040E22365E7286C2FF0005", StringWriterText);
}

[Test]
public void WriteCharEntity ()
{
Expand Down Expand Up @@ -1059,9 +1074,9 @@ public void WriteWhitespaceClosesTag ()
[Test]
public void DontOutputMultipleXmlns ()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
XmlDocument doc2 = new XmlDocument();
XmlDocument doc = new XmlDocument();
doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(doc.InnerXml);
AssertEquals ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
doc2.OuterXml);
Expand All @@ -1071,9 +1086,9 @@ public void DontOutputMultipleXmlns ()
public void DontOutputNonDeclaredXmlns ()
{
string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlDocument doc2 = new XmlDocument();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(doc.InnerXml);
AssertEquals (xml.Replace ('\'', '"'), doc2.OuterXml);
}
Expand Down
14 changes: 14 additions & 0 deletions mcs/class/System.XML/Test/System.Xml/XmlUrlResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,19 @@ public void GetEntityWithNullArgs ()
Uri uri = new Uri ("http://www.go-mono.com/index.rss");
resolver.GetEntity (uri, null, null);
}

[Test]
[ExpectedException (typeof (ArgumentException))]
public void GetEntityWithRelativeFileUri ()
{
resolver.GetEntity (new Uri ("file://file.txt"), null, typeof (Stream));
}

[Test]
[ExpectedException (typeof (XmlException))]
public void GetEntityWithNonStreamReturnType ()
{
resolver.GetEntity (new Uri ("http://www.go-mono.com/"), null, typeof (File));
}
}
}
19 changes: 17 additions & 2 deletions mcs/class/System.XML/Test/System.Xml/XmlWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ public void WriteNodeFullDocument ()
{
setupWriter ();
string xml = "<?xml version='1.0'?><root />";
StringReader sr = new StringReader (xml);
XmlTextReader xtr = new XmlTextReader (sr);
XmlTextReader xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
xtw.WriteNode (xtr, false);
AssertEquals (xml, writer.ToString ());

// With encoding
setupWriter ();
xml = "<?xml version='1.0' encoding='iso-2022-jp' ?><root />";
xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
xtw.WriteNode (xtr, false);
AssertEquals (xml, writer.ToString ());
}
Expand Down Expand Up @@ -75,6 +81,15 @@ public void WriteNodeEmptyElement ()
writer.ToString ());
}

[Test]
public void WriteNodeNonEmptyElement ()
{
setupWriter ();
string xml = @"<foo><bar></bar></foo>";
xtw.WriteNode (new XmlTextReader (xml, XmlNodeType.Document, null), false);
AssertEquals (xml, writer.ToString ());
}

[Test]
public void WriteNodeSingleContentElement ()
{
Expand Down

0 comments on commit 1395cd6

Please sign in to comment.