Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,389 @@
//
// Atom10FeedFormatterTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class Atom10FeedFormatterTest
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullFeed ()
{
new Atom10FeedFormatter ((SyndicationFeed) null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullType ()
{
new Atom10FeedFormatter ((Type) null);
}
/*
[Test]
public void FeedType ()
{
Atom10FeedFormatter f = new Atom10FeedFormatter ();
Assert.IsNull (f.FeedType, "#1");
f = new Atom10FeedFormatter (new SyndicationFeed ());
Assert.IsNull (f.FeedType, "#2");
}
*/
[Test]
public void Version ()
{
Atom10FeedFormatter f = new Atom10FeedFormatter ();
Assert.AreEqual ("Atom10", f.Version, "#1");
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void DefaultConstructorThenWriteXml ()
{
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10FeedFormatter ().WriteTo (w);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void WriteToNull ()
{
SyndicationFeed feed = new SyndicationFeed ();
new Atom10FeedFormatter (feed).WriteTo (null);
}
string DummyId (string s)
{
return Regex.Replace (s, "<id>.+</id>", "<id>XXX</id>");
}
string DummyId2 (string s)
{
return Regex.Replace (s, "<id xmlns=\"http://www.w3.org/2005/Atom\">.+</id>", "<id>XXX</id>");
}
string DummyUpdated (string s)
{
return Regex.Replace (s, "<updated>.+</updated>", "<updated>XXX</updated>");
}
string DummyUpdated2 (string s)
{
return Regex.Replace (s, "<updated xmlns=\"http://www.w3.org/2005/Atom\">.+</updated>", "<updated>XXX</updated>");
}
[Test]
public void WriteTo_EmptyFeed ()
{
// It however automatically fills id (very likely bug in .NET) and DateTimeOffset though.
SyndicationFeed feed = new SyndicationFeed ();
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10FeedFormatter (feed).WriteTo (w);
Assert.IsNull (feed.Id, "#1"); // automatically generated, but not automatically set.
using (XmlWriter w = CreateWriter (sw))
new Atom10FeedFormatter (feed).WriteTo (w);
Assert.AreEqual ("<feed xmlns=\"http://www.w3.org/2005/Atom\"><title type=\"text\"></title><id>XXX</id><updated>XXX</updated></feed>", DummyUpdated (DummyId (sw.ToString ())));
}
[Test]
public void WriteTo_TitleOnlyFeed ()
{
// It however automatically fills id (very likely bug in .NET) and DateTimeOffset though.
SyndicationFeed feed = new SyndicationFeed ();
feed.Title = new TextSyndicationContent ("title text");
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10FeedFormatter (feed).WriteTo (w);
Assert.AreEqual ("<feed xmlns=\"http://www.w3.org/2005/Atom\"><title type=\"text\">title text</title><id>XXX</id><updated>XXX</updated></feed>", DummyUpdated (DummyId (sw.ToString ())));
}
[Test]
public void WriteTo_CategoryAuthorsContributors ()
{
// It however automatically fills ...
SyndicationFeed feed = new SyndicationFeed ();
feed.Categories.Add (new SyndicationCategory ("myname", "myscheme", "mylabel"));
feed.Authors.Add (new SyndicationPerson ("john@doe.com", "John Doe", "http://john.doe.name"));
feed.Contributors.Add (new SyndicationPerson ("jane@doe.com", "Jane Doe", "http://jane.doe.name"));
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10FeedFormatter (feed).WriteTo (w);
// contributors are serialized as Atom extension
Assert.AreEqual ("<feed xmlns=\"http://www.w3.org/2005/Atom\"><title type=\"text\"></title><id>XXX</id><updated>XXX</updated><category term=\"myname\" label=\"mylabel\" scheme=\"myscheme\" /><author><name>John Doe</name><uri>http://john.doe.name</uri><email>john@doe.com</email></author><contributor><name>Jane Doe</name><uri>http://jane.doe.name</uri><email>jane@doe.com</email></contributor></feed>", DummyUpdated (DummyId (sw.ToString ())));
}
[Test]
public void WriteTo ()
{
SyndicationFeed feed = new SyndicationFeed ();
feed.BaseUri = new Uri ("http://mono-project.com");
feed.Copyright = new TextSyndicationContent ("No rights reserved");
feed.Description = new TextSyndicationContent ("A sample feed for unit testing");
feed.Generator = "mono test generator";
// .NET bug: it ignores this value.
feed.Id = "urn:myid";
feed.ImageUrl = new Uri ("http://mono-project.com/images/mono.png");
feed.LastUpdatedTime = new DateTimeOffset (DateTime.SpecifyKind (new DateTime (2008, 1, 1), DateTimeKind.Utc));
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10FeedFormatter (feed).WriteTo (w);
Assert.AreEqual ("<feed xml:base=\"http://mono-project.com/\" xmlns=\"http://www.w3.org/2005/Atom\"><title type=\"text\"></title><id>XXX</id><rights type=\"text\">No rights reserved</rights><updated>2008-01-01T00:00:00Z</updated><subtitle type=\"text\">A sample feed for unit testing</subtitle><logo>http://mono-project.com/images/mono.png</logo><generator>mono test generator</generator></feed>", DummyId (sw.ToString ()));
}
[Test]
public void ISerializableWriteXml ()
{
SyndicationFeed feed = new SyndicationFeed ();
feed.Title = new TextSyndicationContent ("title text");
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw)) {
w.WriteStartElement ("dummy");
((IXmlSerializable) new Atom10FeedFormatter (feed)).WriteXml (w);
w.WriteEndElement ();
}
Assert.AreEqual ("<dummy><title type=\"text\" xmlns=\"http://www.w3.org/2005/Atom\">title text</title><id>XXX</id><updated>XXX</updated></dummy>", DummyUpdated2 (DummyId2 (sw.ToString ())));
}
[Test]
public void WriteTo_IllegalDuplicateAltLinks ()
{
// ... and it passes.
SyndicationFeed feed = new SyndicationFeed ();
feed.Links.Add (new SyndicationLink (new Uri ("http://mono-project.com/Page1"), "alternate", "Page 1", "text/html", 0));
feed.Links.Add (new SyndicationLink (new Uri ("http://mono-project.com/Page2"), "alternate", "Page 2", "text/html", 0));
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10FeedFormatter (feed).WriteTo (w);
Assert.AreEqual ("<feed xmlns=\"http://www.w3.org/2005/Atom\"><title type=\"text\"></title><id>XXX</id><updated>XXX</updated><link rel=\"alternate\" type=\"text/html\" title=\"Page 1\" href=\"http://mono-project.com/Page1\" /><link rel=\"alternate\" type=\"text/html\" title=\"Page 2\" href=\"http://mono-project.com/Page2\" /></feed>", DummyUpdated (DummyId (sw.ToString ())));
}
XmlWriter CreateWriter (StringWriter sw)
{
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
return XmlWriter.Create (sw, s);
}
XmlReader CreateReader (string xml)
{
return XmlReader.Create (new StringReader (xml));
}
[Test]
public void CanRead ()
{
Atom10FeedFormatter f = new Atom10FeedFormatter ();
Assert.IsTrue (f.CanRead (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'>")), "#1");
Assert.IsFalse (f.CanRead (CreateReader ("<item xmlns='http://www.w3.org/2005/Atom'>")), "#2");
Assert.IsFalse (f.CanRead (CreateReader ("<feed xmlns='urn:foo'>")), "#3");
Assert.IsFalse (f.CanRead (CreateReader ("<feed>")), "#4");
Assert.IsFalse (f.CanRead (CreateReader ("<hoge>")), "#5");
XmlReader r = CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'></feed>");
r.Read (); // element
r.Read (); // endelement
Assert.IsFalse (f.CanRead (r), "#6");
r = CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><title>test</title></feed>");
r.Read (); // feed
r.Read (); // channel
Assert.IsFalse (f.CanRead (r), "#7");
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadFromInvalid ()
{
new Atom10FeedFormatter ().ReadFrom (CreateReader ("<feed>"));
}
[Test]
public void ReadFrom1 ()
{
Atom10FeedFormatter f = new Atom10FeedFormatter ();
Assert.IsNull (f.Feed, "#1");
f.ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><title>test</title></feed>"));
SyndicationFeed feed1 = f.Feed;
Assert.IsNotNull (f.Feed.Title, "#2");
Assert.AreEqual ("test", f.Feed.Title.Text, "#3");
f.ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><title>test</title></feed>"));
Assert.IsFalse (object.ReferenceEquals (feed1, f.Feed), "#4");
}
[Test]
public void ReadXml_TitleOnly ()
{
Atom10FeedFormatter f = new Atom10FeedFormatter ();
((IXmlSerializable) f).ReadXml (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><title>test</title></feed>"));
Assert.IsNotNull (f.Feed.Title, "#1");
Assert.AreEqual ("test", f.Feed.Title.Text, "#2");
((IXmlSerializable) f).ReadXml (CreateReader ("<dummy xmlns='http://www.w3.org/2005/Atom'><title>test</title></dummy>")); // it is ok
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadXmlFromContent ()
{
((IXmlSerializable) new Atom10FeedFormatter ()).ReadXml (CreateReader ("<title xmlns='http://www.w3.org/2005/Atom'>test</title>"));
}
[Test]
public void ReadXml_Extension ()
{
new Atom10FeedFormatter<MySyndicationFeed1> ().ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><foo>test</foo></feed>"));
new Atom10FeedFormatter<MySyndicationFeed2> ().ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><foo>test</foo></feed>"));
try {
new Atom10FeedFormatter<MySyndicationFeed3> ().ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><foo>test</foo></feed>"));
Assert.Fail ("should trigger TryParseElement");
} catch (ApplicationException) {
}
}
class MySyndicationFeed1 : SyndicationFeed
{
protected override bool TryParseElement (XmlReader reader, string version)
{
Assert.AreEqual ("Atom10", version, "#1");
Assert.IsFalse (base.TryParseElement (reader, version), "#2");
return false;
}
}
class MySyndicationFeed2 : SyndicationFeed
{
protected override bool TryParseElement (XmlReader reader, string version)
{
reader.Skip (); // without it, the caller expects that the reader did not proceed.
return true;
}
}
class MySyndicationFeed3 : SyndicationFeed
{
protected override bool TryParseElement (XmlReader reader, string version)
{
throw new ApplicationException ();
}
}
[Test]
[ExpectedException (typeof (XmlException))]
[Category("NotWorking")]
public void ReadFrom_EmptyDate ()
{
// strangely, it is checked while 'entry' is not checked
Atom10FeedFormatter f = new Atom10FeedFormatter ();
f.ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><updated /></feed>"));
}
[Test]
[ExpectedException (typeof (XmlException))]
[Category("NotWorking")]
public void ReadFrom_WrongDate ()
{
Atom10FeedFormatter f = new Atom10FeedFormatter ();
f.ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><updated>2000-01-01T00:00:00</updated></feed>"));
}
[Test]
public void ReadFrom_Extension ()
{
Atom10FeedFormatter f = new Atom10FeedFormatter ();
f.ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><icon>http://www.mono-project.com/icons/mono.png</icon></feed>"));
Assert.IsNotNull (f.Feed, "#1");
// 'icon' is treated as an extension ...
Assert.AreEqual (1, f.Feed.ElementExtensions.Count, "#2");
}
[Test]
public void ReadFrom_Link ()
{
Atom10FeedFormatter f = new Atom10FeedFormatter ();
f.ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><link href='urn:foo' rel='enclosure' length='50' type='text/html' wcf='wtf'><extended /></link></feed>"));
Assert.AreEqual (1, f.Feed.Links.Count, "#1");
SyndicationLink link = f.Feed.Links [0];
Assert.AreEqual (50, link.Length, "#2");
Assert.AreEqual ("urn:foo", link.Uri.ToString (), "#3");
Assert.AreEqual ("text/html", link.MediaType, "#4");
Assert.AreEqual ("enclosure", link.RelationshipType, "#5");
Assert.AreEqual (1, link.AttributeExtensions.Count, "#6");
Assert.AreEqual (1, link.ElementExtensions.Count, "#7");
}
[Test]
public void ReadFrom_ImageUrl ()
{
Atom10FeedFormatter f = new Atom10FeedFormatter ();
f.ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'><logo>http://mono-project.com/images/mono.png</logo></feed>"));
Assert.IsNotNull (f.Feed.ImageUrl, "#1");
Assert.AreEqual ("http://mono-project.com/images/mono.png", f.Feed.ImageUrl.ToString (), "#2");
}
[Test]
public void ReadFrom_Language ()
{
Atom10FeedFormatter f = new Atom10FeedFormatter ();
f.ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom' xml:lang='ar-AR'></feed>"));
Assert.AreEqual ("ar-AR", f.Feed.Language, "#1");
}
[Test]
public void GetSchema ()
{
Assert.IsNull (((IXmlSerializable) new Atom10FeedFormatter ()).GetSchema ());
}
[Test]
public void TestToString ()
{
Assert.AreEqual (typeof (Atom10FeedFormatter).FullName + ", SyndicationVersion=Atom10", new Atom10FeedFormatter (new SyndicationFeed ()).ToString ());
}
}
}
#endif

View File

@ -0,0 +1,378 @@
//
// Atom10ItemFormatterTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class Atom10ItemFormatterTest
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullItem ()
{
new Atom10ItemFormatter ((SyndicationItem) null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullType ()
{
new Atom10ItemFormatter ((Type) null);
}
/*
[Test]
public void ItemType ()
{
Atom10ItemFormatter f = new Atom10ItemFormatter ();
Assert.IsNull (f.ItemType, "#1");
f = new Atom10ItemFormatter (new SyndicationItem ());
Assert.IsNull (f.ItemType, "#2");
}
*/
[Test]
public void Version ()
{
Atom10ItemFormatter f = new Atom10ItemFormatter ();
Assert.AreEqual ("Atom10", f.Version, "#1");
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void DefaultConstructorThenWriteXml ()
{
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10ItemFormatter ().WriteTo (w);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void WriteToNull ()
{
SyndicationItem item = new SyndicationItem ();
new Atom10ItemFormatter (item).WriteTo (null);
}
string DummyId (string s)
{
return Regex.Replace (s, "<id>.+</id>", "<id>XXX</id>");
}
string DummyId2 (string s)
{
return Regex.Replace (s, "<id xmlns=\"http://www.w3.org/2005/Atom\">.+</id>", "<id>XXX</id>");
}
string DummyUpdated (string s)
{
return Regex.Replace (s, "<updated>.+</updated>", "<updated>XXX</updated>");
}
string DummyUpdated2 (string s)
{
return Regex.Replace (s, "<updated xmlns=\"http://www.w3.org/2005/Atom\">.+</updated>", "<updated>XXX</updated>");
}
[Test]
public void WriteTo_EmptyItem ()
{
// It however automatically fills id (very likely bug in .NET) and DateTimeOffset though.
SyndicationItem item = new SyndicationItem ();
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10ItemFormatter (item).WriteTo (w);
Assert.IsNull (item.Id, "#1"); // automatically generated, but not automatically set.
using (XmlWriter w = CreateWriter (sw))
new Atom10ItemFormatter (item).WriteTo (w);
Assert.AreEqual ("<entry xmlns=\"http://www.w3.org/2005/Atom\"><id>XXX</id><title type=\"text\"></title><updated>XXX</updated></entry>", DummyUpdated (DummyId (sw.ToString ())));
}
[Test]
public void WriteTo_TitleOnlyItem ()
{
// It however automatically fills id (very likely bug in .NET) and DateTimeOffset though.
SyndicationItem item = new SyndicationItem ();
item.Title = new TextSyndicationContent ("title text");
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10ItemFormatter (item).WriteTo (w);
Assert.AreEqual ("<entry xmlns=\"http://www.w3.org/2005/Atom\"><id>XXX</id><title type=\"text\">title text</title><updated>XXX</updated></entry>", DummyUpdated (DummyId (sw.ToString ())));
}
[Test]
public void WriteTo_CategoryAuthorsContributors ()
{
// It however automatically fills ...
SyndicationItem item = new SyndicationItem ();
item.Categories.Add (new SyndicationCategory ("myname", "myscheme", "mylabel"));
item.Authors.Add (new SyndicationPerson ("john@doe.com", "John Doe", "http://john.doe.name"));
item.Contributors.Add (new SyndicationPerson ("jane@doe.com", "Jane Doe", "http://jane.doe.name"));
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10ItemFormatter (item).WriteTo (w);
// contributors are serialized as Atom extension
Assert.AreEqual ("<entry xmlns=\"http://www.w3.org/2005/Atom\"><id>XXX</id><title type=\"text\"></title><updated>XXX</updated><author><name>John Doe</name><uri>http://john.doe.name</uri><email>john@doe.com</email></author><contributor><name>Jane Doe</name><uri>http://jane.doe.name</uri><email>jane@doe.com</email></contributor><category term=\"myname\" label=\"mylabel\" scheme=\"myscheme\" /></entry>", DummyUpdated (DummyId (sw.ToString ())));
}
[Test]
public void WriteTo ()
{
SyndicationItem item = new SyndicationItem ();
item.BaseUri = new Uri ("http://mono-project.com");
item.Copyright = new TextSyndicationContent ("No rights reserved");
item.Content = new XmlSyndicationContent (null, 5, (XmlObjectSerializer) null);
// .NET bug: it ignores this value.
item.Id = "urn:myid";
item.PublishDate = new DateTimeOffset (DateTime.SpecifyKind (new DateTime (2000, 1, 1), DateTimeKind.Utc));
item.LastUpdatedTime = new DateTimeOffset (DateTime.SpecifyKind (new DateTime (2008, 1, 1), DateTimeKind.Utc));
//item.SourceFeed = new SyndicationFeed ();
item.Summary = new TextSyndicationContent ("great text");
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10ItemFormatter (item).WriteTo (w);
Assert.AreEqual ("<entry xml:base=\"http://mono-project.com/\" xmlns=\"http://www.w3.org/2005/Atom\"><id>XXX</id><title type=\"text\"></title><summary type=\"text\">great text</summary><published>2000-01-01T00:00:00Z</published><updated>2008-01-01T00:00:00Z</updated><content type=\"text/xml\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">5</int></content><rights type=\"text\">No rights reserved</rights></entry>", DummyId (sw.ToString ()));
}
[Test]
public void ISerializableWriteXml ()
{
SyndicationItem item = new SyndicationItem ();
item.Title = new TextSyndicationContent ("title text");
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw)) {
w.WriteStartElement ("dummy");
((IXmlSerializable) new Atom10ItemFormatter (item)).WriteXml (w);
w.WriteEndElement ();
}
Assert.AreEqual ("<dummy><id>XXX</id><title type=\"text\" xmlns=\"http://www.w3.org/2005/Atom\">title text</title><updated>XXX</updated></dummy>", DummyUpdated2 (DummyId2 (sw.ToString ())));
}
[Test]
public void WriteTo_IllegalDuplicateAltLinks ()
{
// ... and it passes.
SyndicationItem item = new SyndicationItem ();
item.Links.Add (new SyndicationLink (new Uri ("http://mono-project.com/Page1"), "alternate", "Page 1", "text/html", 0));
item.Links.Add (new SyndicationLink (new Uri ("http://mono-project.com/Page2"), "alternate", "Page 2", "text/html", 0));
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Atom10ItemFormatter (item).WriteTo (w);
Assert.AreEqual ("<entry xmlns=\"http://www.w3.org/2005/Atom\"><id>XXX</id><title type=\"text\"></title><updated>XXX</updated><link rel=\"alternate\" type=\"text/html\" title=\"Page 1\" href=\"http://mono-project.com/Page1\" /><link rel=\"alternate\" type=\"text/html\" title=\"Page 2\" href=\"http://mono-project.com/Page2\" /></entry>", DummyUpdated (DummyId (sw.ToString ())));
}
XmlWriter CreateWriter (StringWriter sw)
{
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
return XmlWriter.Create (sw, s);
}
XmlReader CreateReader (string xml)
{
return XmlReader.Create (new StringReader (xml));
}
[Test]
public void CanRead ()
{
Atom10ItemFormatter f = new Atom10ItemFormatter ();
Assert.IsFalse (f.CanRead (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'>")), "#1");
Assert.IsTrue (f.CanRead (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'>")), "#2");
Assert.IsFalse (f.CanRead (CreateReader ("<entry>")), "#3");
Assert.IsFalse (f.CanRead (CreateReader ("<item>")), "#4");
Assert.IsFalse (f.CanRead (CreateReader ("<hoge xmlns='http://www.w3.org/2005/Atom'>")), "#5");
XmlReader r = CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'></entry>");
r.Read (); // element
r.Read (); // endelement
Assert.IsFalse (f.CanRead (r), "#6");
r = CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>");
r.Read (); // item
r.Read (); // title
Assert.IsFalse (f.CanRead (r), "#7");
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadFromInvalid ()
{
new Atom10ItemFormatter ().ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom' />"));
}
[Test]
public void ReadFrom1 ()
{
Atom10ItemFormatter f = new Atom10ItemFormatter ();
Assert.IsNull (f.Item, "#1");
f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>"));
SyndicationItem item1 = f.Item;
Assert.IsNotNull (f.Item.Title, "#2");
Assert.AreEqual ("test", f.Item.Title.Text, "#3");
f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>"));
Assert.IsFalse (object.ReferenceEquals (item1, f.Item), "#4");
}
[Test]
public void ReadXml_TitleOnly ()
{
Atom10ItemFormatter f = new Atom10ItemFormatter ();
((IXmlSerializable) f).ReadXml (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>"));
Assert.IsNotNull (f.Item.Title, "#1");
Assert.AreEqual ("test", f.Item.Title.Text, "#2");
((IXmlSerializable) f).ReadXml (CreateReader ("<dummy><title>test</title></dummy>")); // it is ok
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadXmlFromContent ()
{
((IXmlSerializable) new Atom10ItemFormatter ()).ReadXml (CreateReader ("<title xmlns='http://www.w3.org/2005/Atom'>test</title>"));
}
[Test]
public void ReadXml_Extension ()
{
new Atom10ItemFormatter<MySyndicationItem1> ().ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><foo>test</foo></entry>"));
new Atom10ItemFormatter<MySyndicationItem2> ().ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><foo>test</foo></entry>"));
try {
new Atom10ItemFormatter<MySyndicationItem3> ().ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><foo>test</foo></entry>"));
Assert.Fail ("should trigger TryParseElement");
} catch (ApplicationException) {
}
}
class MySyndicationItem1 : SyndicationItem
{
protected override bool TryParseElement (XmlReader reader, string version)
{
Assert.AreEqual ("Atom10", version, "#1");
Assert.IsFalse (base.TryParseElement (reader, version), "#2");
return false;
}
}
class MySyndicationItem2 : SyndicationItem
{
protected override bool TryParseElement (XmlReader reader, string version)
{
reader.Skip (); // without it, the caller expects that the reader did not proceed.
return true;
}
}
class MySyndicationItem3 : SyndicationItem
{
protected override bool TryParseElement (XmlReader reader, string version)
{
throw new ApplicationException ();
}
}
[Test]
// It is not rejected. Though I think it is .NET bug.
public void ReadFrom_EmptyDate ()
{
Atom10ItemFormatter f = new Atom10ItemFormatter ();
f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><pubDate /></entry>"));
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadFrom_WrongDate ()
{
Atom10ItemFormatter f = new Atom10ItemFormatter ();
f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><published>Sat, 01 Jan 2000 00:00:00 Z</pubDate></entry>"));
}
[Test]
public void ReadFrom_Extension ()
{
Atom10ItemFormatter f = new Atom10ItemFormatter ();
f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><ext>external</ext></entry>"));
Assert.IsNotNull (f.Item, "#1");
Assert.AreEqual (1, f.Item.ElementExtensions.Count, "#2");
}
[Test]
public void ReadFrom_Id ()
{
Atom10ItemFormatter f = new Atom10ItemFormatter ();
f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><id>urn:myid</id></entry>"));
Assert.IsNotNull (f.Item, "#1");
Assert.AreEqual ("urn:myid", f.Item.Id, "#2");
}
[Test]
public void ReadFrom_Link ()
{
Atom10ItemFormatter f = new Atom10ItemFormatter ();
f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><link href='urn:foo' rel='enclosure' length='50' type='text/html' wcf='wtf'><extended /></link></entry>"));
Assert.AreEqual (1, f.Item.Links.Count, "#1");
SyndicationLink link = f.Item.Links [0];
Assert.AreEqual (50, link.Length, "#2");
Assert.AreEqual ("urn:foo", link.Uri.ToString (), "#3");
Assert.AreEqual ("text/html", link.MediaType, "#4");
Assert.AreEqual ("enclosure", link.RelationshipType, "#5");
Assert.AreEqual (1, link.AttributeExtensions.Count, "#6");
Assert.AreEqual (1, link.ElementExtensions.Count, "#7");
}
[Test]
public void GetSchema ()
{
Assert.IsNull (((IXmlSerializable) new Atom10ItemFormatter ()).GetSchema ());
}
[Test]
public void TestToString ()
{
Assert.AreEqual (typeof (Atom10ItemFormatter).FullName + ", SyndicationVersion=Atom10", new Atom10ItemFormatter (new SyndicationItem ()).ToString ());
}
}
}
#endif

View File

@ -0,0 +1,129 @@
//
// AtomPub10CategoriesDocumentFormatterTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2009 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class AtomPub10CategoriesDocumentFormatterTest
{
static XmlWriterSettings settings = new XmlWriterSettings () { OmitXmlDeclaration = true};
class MyFormatter : AtomPub10CategoriesDocumentFormatter
{
public InlineCategoriesDocument CreateInline ()
{
return CreateInlineCategoriesDocument ();
}
public ReferencedCategoriesDocument CreateReferenced ()
{
return CreateReferencedCategoriesDocument ();
}
}
[Test]
public void CreateInstances ()
{
var f = new MyFormatter ();
Assert.IsTrue (f.CreateInline () is InlineCategoriesDocument, "#1");
Assert.IsTrue (f.CreateReferenced () is ReferencedCategoriesDocument, "#2");
}
[Test]
public void WriteTo ()
{
var doc = new InlineCategoriesDocument ();
var f = new AtomPub10CategoriesDocumentFormatter (doc);
doc.Scheme = "http";
doc.IsFixed = false;
var sw = new StringWriter ();
using (var xw = XmlWriter.Create (sw, settings))
f.WriteTo (xw);
Assert.AreEqual (app1, sw.ToString ());
}
string app1 = "<app:categories xmlns:a10=\"http://www.w3.org/2005/Atom\" scheme=\"http\" xmlns:app=\"http://www.w3.org/2007/app\" />";
[Test]
public void WriteTo2 ()
{
var doc = new InlineCategoriesDocument ();
var f = new AtomPub10CategoriesDocumentFormatter (doc);
doc.Categories.Add (new SyndicationCategory ("TEST CATEGORY"));
var sw = new StringWriter ();
using (var xw = XmlWriter.Create (sw, settings))
f.WriteTo (xw);
Assert.AreEqual (app2, sw.ToString ());
}
string app2 = "<app:categories xmlns:a10=\"http://www.w3.org/2005/Atom\" xmlns:app=\"http://www.w3.org/2007/app\"><a10:category term=\"TEST CATEGORY\" /></app:categories>";
[Test]
public void ReadFrom ()
{
var f = new AtomPub10CategoriesDocumentFormatter ();
f.ReadFrom (XmlReader.Create (new StringReader (app1)));
var inline = f.Document as InlineCategoriesDocument;
Assert.IsNotNull (inline, "#1");
Assert.AreEqual ("http", inline.Scheme, "#2");
}
[Test]
public void ReadFrom2 ()
{
var f = new AtomPub10CategoriesDocumentFormatter ();
f.ReadFrom (XmlReader.Create (new StringReader (app2)));
var inline = f.Document as InlineCategoriesDocument;
Assert.IsNotNull (inline, "#1");
Assert.IsNull (inline.Scheme, "#2");
Assert.AreEqual (1, inline.Categories.Count, "#3");
}
[Test]
public void GetSchema ()
{
IXmlSerializable i = new AtomPub10CategoriesDocumentFormatter ();
Assert.IsNull (i.GetSchema ());
}
}
}
#endif

View File

@ -0,0 +1,141 @@
//
// AtomPub10ServiceDocumentFormatterTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2009 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
class MyServiceFormatter : AtomPub10ServiceDocumentFormatter
{
public void Read (XmlReader reader)
{
ReadFrom (reader);
}
public void CallWriteDocument ()
{
var w = XmlWriter.Create (TextWriter.Null);
ServiceDocumentFormatter.WriteElementExtensions (w, new MyDocument (), "http://www.w3.org/2007/app");
}
}
class MyDocument : ServiceDocument
{
protected override void WriteElementExtensions (XmlWriter writer, string version)
{
throw new ApplicationException ();
}
}
[TestFixture]
public class AtomPub10ServiceDocumentFormatterTest
{
static XmlWriterSettings settings = new XmlWriterSettings () { OmitXmlDeclaration = true};
string app1 = "<app:service xmlns:a10=\"http://www.w3.org/2005/Atom\" xmlns:app=\"http://www.w3.org/2007/app\" />";
[Test]
public void WriteTo ()
{
var s = new ServiceDocument ();
var a = new AtomPub10ServiceDocumentFormatter (s);
Assert.AreEqual ("http://www.w3.org/2007/app", a.Version, "#1");
Assert.IsTrue (a.CanRead (XmlReader.Create (new StringReader (app1))), "#2");
var sw = new StringWriter ();
using (var xw = XmlWriter.Create (sw, settings))
a.WriteTo (xw);
Assert.AreEqual (app1, sw.ToString (), "#3");
}
string app2 = "<app:service xmlns:a10=\"http://www.w3.org/2005/Atom\" xmlns:app=\"http://www.w3.org/2007/app\"><app:workspace><a10:title type=\"text\">test title</a10:title><app:collection href=\"urn:foo\"><a10:title type=\"text\">test resource</a10:title><app:accept>application/atom+xml;type=entry</app:accept></app:collection></app:workspace></app:service>";
[Test]
public void WriteTo2 ()
{
var s = new ServiceDocument ();
var ws = new Workspace ("test title", null);
var rc = new ResourceCollectionInfo ("test resource", new Uri ("urn:foo"));
rc.Accepts.Add ("application/atom+xml;type=entry");
ws.Collections.Add (rc);
s.Workspaces.Add (ws);
var a = new AtomPub10ServiceDocumentFormatter (s);
Assert.AreEqual ("http://www.w3.org/2007/app", a.Version, "#1");
Assert.IsTrue (a.CanRead (XmlReader.Create (new StringReader (app2))), "#2");
var sw = new StringWriter ();
using (var xw = XmlWriter.Create (sw, settings))
a.WriteTo (xw);
Assert.AreEqual (app2, sw.ToString (), "#3");
}
[Test]
[ExpectedException (typeof (XmlException))] // insufficient content
public void Load ()
{
ServiceDocument.Load (XmlReader.Create (new StringReader (app1)));
}
[Test]
public void Load2 ()
{
ServiceDocument.Load (XmlReader.Create (new StringReader (app2)));
}
[Test]
public void ReadFrom ()
{
new MyServiceFormatter ().Read (XmlReader.Create (new StringReader (app2)));
}
[Test]
public void GetSchema ()
{
IXmlSerializable i = new AtomPub10ServiceDocumentFormatter ();
Assert.IsNull (i.GetSchema ());
}
[Test]
[ExpectedException (typeof (ApplicationException))]
public void WriteElementExtensions ()
{
// this test is to verify that the overriden WriteElementExtensions() is called in the staic ServiceDocumentFormatter method.
new MyServiceFormatter ().CallWriteDocument ();
}
}
}
#endif

View File

@ -0,0 +1,103 @@
2009-10-07 Atsushi Enomoto <atsushi@ximian.com>
* SyndicationItemTest.cs, SyndicationFeedTest.cs : allow atom feed
too. More Load() tests. Patch by David Mitchell.
2009-10-07 Atsushi Enomoto <atsushi@ximian.com>
* SyndicationElementExtensionTest.cs : added test for OuterName/ns
with .ctor(XmlReader).
* SyndicationItemTest.cs : added failure case for LoadItem() with
feed element.
2009-04-15 Atsushi Enomoto <atsushi@ximian.com>
* AtomPub10CategoriesDocumentFormatterTest.cs,
AtomPub10ServiceDocumentFormatterTest.cs : aded GetSchema() and
WriteElementExtensions() tests.
2009-04-06 Atsushi Enomoto <atsushi@ximian.com>
* AtomPub10ServiceDocumentFormatterTest.cs : add ReadFrom() test.
2009-04-06 Atsushi Enomoto <atsushi@ximian.com>
* AtomPub10CategoriesDocumentFormatterTest.cs : new test.
* AtomPub10ServiceDocumentFormatterTest.cs : add reader tests.
2009-04-03 Atsushi Enomoto <atsushi@ximian.com>
* AtomPub10ServiceDocumentFormatterTest.cs : new test.
2007-12-18 Atsushi Enomoto <atsushi@ximian.com>
* Rss20ItemFormatterTest.cs : test (enable) SourceFeed output too.
2007-12-18 Atsushi Enomoto <atsushi@ximian.com>
* Atom10FeedFormatterTest.cs : tests for ReadFrom() and ReadXml().
* Atom10ItemFormatterTest.cs : test for ToString().
2007-12-18 Atsushi Enomoto <atsushi@ximian.com>
* Atom10ItemFormatterTest.cs : tests for ReadFrom() and ReadXml().
2007-12-18 Atsushi Enomoto <atsushi@ximian.com>
* Rss20FeedFormatterTest.cs : some tests for ReadFrom() and ReadXml().
* Rss20ItemFormatterTest.cs : assure that Item instance is
instantiated every time read methods are invoked.
2007-12-18 Atsushi Enomoto <atsushi@ximian.com>
* Rss20ItemFormatterTest.cs : some tests for ReadFrom() and ReadXml().
* SyndicationItemTest.cs : Test Load() with non-Atom/non-RSS xml.
2007-12-17 Atsushi Enomoto <atsushi@ximian.com>
* Atom10FeedFormatterTest.cs : new, test for WriteTo and WriteXml.
2007-12-17 Atsushi Enomoto <atsushi@ximian.com>
* Rss20FeedFormatterTest.cs : they are feed, not item :)
2007-12-17 Atsushi Enomoto <atsushi@ximian.com>
* Atom10ItemFormatterTest.cs : test for spec violation i.e. to prove
that this class is not trustworthy for valid output.
2007-12-17 Atsushi Enomoto <atsushi@ximian.com>
* Atom10ItemFormatterTest.cs : added test for WriteTo and WriteXml.
2007-12-15 Atsushi Enomoto <atsushi@ximian.com>
* Rss20FeedFormatterTest.cs : new, mostly copied from item formatter.
2007-12-15 Atsushi Enomoto <atsushi@ximian.com>
* Rss20ItemFormatterTest.cs : added test for writing properties.
* XmlSyndicationContentTest.cs, UrlSyndicationContentTest.cs : new.
2007-12-15 Atsushi Enomoto <atsushi@ximian.com>
* SyndicationElementExtensionTest.cs : new test.
* TextSyndicationContentTest.cs : was missing in the previous commit.
2007-12-11 Atsushi Enomoto <atsushi@ximian.com>
* TextSyndicationContentTest.cs : new test.
* Rss20ItemFormatterTest.cs : test WriteTo()/WriteXml().
2007-12-10 Atsushi Enomoto <atsushi@ximian.com>
* Atom10ItemFormatterTest.cs, Rss20ItemFormatterTest.cs,
SyndicationFeedTest.cs, SyndicationItemTest.cs : new tests.
2007-12-10 Atsushi Enomoto <atsushi@ximian.com>
* SyndicationLinkTest.cs : test RelationshipType too. Tiny renaming.
2007-12-10 Atsushi Enomoto <atsushi@ximian.com>
* SyndicationLinkTest.cs : new test.

View File

@ -0,0 +1,360 @@
//
// Rss20FeedFormatterTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class Rss20FeedFormatterTest
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullFeed ()
{
new Rss20FeedFormatter ((SyndicationFeed) null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullType ()
{
new Rss20FeedFormatter ((Type) null);
}
/*
[Test]
public void FeedType ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
Assert.IsNull (f.FeedType, "#1");
f = new Rss20FeedFormatter (new SyndicationFeed ());
Assert.IsNull (f.FeedType, "#2");
}
*/
[Test]
public void Version ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
Assert.AreEqual ("Rss20", f.Version, "#1");
}
[Test]
public void SerializeExtensionsAsAtom ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
Assert.IsTrue (f.SerializeExtensionsAsAtom, "#1");
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void DefaultConstructorThenWriteXml ()
{
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20FeedFormatter ().WriteTo (w);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void WriteToNull ()
{
SyndicationFeed feed = new SyndicationFeed ();
new Rss20FeedFormatter (feed).WriteTo (null);
}
[Test]
public void WriteTo_EmptyFeed ()
{
SyndicationFeed feed = new SyndicationFeed ();
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20FeedFormatter (feed).WriteTo (w);
// either title or description must exist (RSS 2.0 spec)
Assert.AreEqual ("<rss xmlns:a10=\"http://www.w3.org/2005/Atom\" version=\"2.0\"><channel><title /><description /></channel></rss>", sw.ToString ());
}
[Test]
public void WriteTo_TitleOnlyFeed ()
{
SyndicationFeed feed = new SyndicationFeed ();
feed.Title = new TextSyndicationContent ("title text");
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20FeedFormatter (feed).WriteTo (w);
Assert.AreEqual ("<rss xmlns:a10=\"http://www.w3.org/2005/Atom\" version=\"2.0\"><channel><title>title text</title><description /></channel></rss>", sw.ToString ());
}
[Test]
public void WriteTo_CategoryAuthorsContributors ()
{
SyndicationFeed feed = new SyndicationFeed ();
feed.Categories.Add (new SyndicationCategory ("myname", "myscheme", "mylabel"));
feed.Authors.Add (new SyndicationPerson ("john@doe.com", "John Doe", "http://john.doe.name"));
feed.Contributors.Add (new SyndicationPerson ("jane@doe.com", "Jane Doe", "http://jane.doe.name"));
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20FeedFormatter (feed).WriteTo (w);
// contributors are serialized as Atom extension
Assert.AreEqual ("<rss xmlns:a10=\"http://www.w3.org/2005/Atom\" version=\"2.0\"><channel><title /><description /><managingEditor>john@doe.com</managingEditor><category domain=\"myscheme\">myname</category><a10:contributor><a10:name>Jane Doe</a10:name><a10:uri>http://jane.doe.name</a10:uri><a10:email>jane@doe.com</a10:email></a10:contributor></channel></rss>", sw.ToString ());
}
[Test]
[Category("NotWorking")]
public void WriteTo ()
{
SyndicationFeed feed = new SyndicationFeed ();
feed.BaseUri = new Uri ("http://mono-project.com");
feed.Copyright = new TextSyndicationContent ("No rights reserved");
feed.Generator = "mono test generator";
feed.Id = "urn:myid";
feed.ImageUrl = new Uri ("http://mono-project.com/images/mono.png");
feed.LastUpdatedTime = new DateTimeOffset (DateTime.SpecifyKind (new DateTime (2008, 1, 1), DateTimeKind.Utc));
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20FeedFormatter (feed).WriteTo (w);
Assert.AreEqual ("<rss xmlns:a10=\"http://www.w3.org/2005/Atom\" version=\"2.0\"><channel xml:base=\"http://mono-project.com/\"><title /><description /><copyright>No rights reserved</copyright><lastBuildDate>Tue, 01 Jan 2008 00:00:00 Z</lastBuildDate><generator>mono test generator</generator><image><url>http://mono-project.com/images/mono.png</url><title /><link /></image><a10:id>urn:myid</a10:id></channel></rss>", sw.ToString ());
}
[Test]
public void SerializeExtensionsAsAtomFalse ()
{
SyndicationFeed feed = new SyndicationFeed ();
feed.Contributors.Add (new SyndicationPerson ("jane@doe.com", "Jane Doe", "http://jane.doe.name"));
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20FeedFormatter (feed, false).WriteTo (w);
// skip contributors
Assert.AreEqual ("<rss version=\"2.0\"><channel><title /><description /></channel></rss>", sw.ToString ());
}
[Test]
public void ISerializableWriteXml ()
{
SyndicationFeed feed = new SyndicationFeed ();
feed.Title = new TextSyndicationContent ("title text");
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw)) {
w.WriteStartElement ("dummy");
((IXmlSerializable) new Rss20FeedFormatter (feed)).WriteXml (w);
w.WriteEndElement ();
}
Assert.AreEqual ("<dummy xmlns:a10=\"http://www.w3.org/2005/Atom\" version=\"2.0\"><channel><title>title text</title><description /></channel></dummy>", sw.ToString ());
}
XmlWriter CreateWriter (StringWriter sw)
{
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
return XmlWriter.Create (sw, s);
}
XmlReader CreateReader (string xml)
{
return XmlReader.Create (new StringReader (xml));
}
[Test]
public void CanRead ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
Assert.IsTrue (f.CanRead (CreateReader ("<rss>")), "#1");
Assert.IsFalse (f.CanRead (CreateReader ("<item>")), "#2");
Assert.IsFalse (f.CanRead (CreateReader ("<rss xmlns='urn:foo'>")), "#3");
Assert.IsFalse (f.CanRead (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'>")), "#4");
Assert.IsFalse (f.CanRead (CreateReader ("<hoge>")), "#5");
XmlReader r = CreateReader ("<rss></rss>");
r.Read (); // element
r.Read (); // endelement
Assert.IsFalse (f.CanRead (r), "#6");
r = CreateReader ("<rss><channel><title>test</title></channel></rss>");
r.Read (); // feed
r.Read (); // channel
Assert.IsFalse (f.CanRead (r), "#7");
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadFromInvalid ()
{
new Rss20FeedFormatter ().ReadFrom (CreateReader ("<item>"));
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void ReadFrom_Versionless ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
Assert.IsNull (f.Feed, "#1");
f.ReadFrom (CreateReader ("<rss>"));
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void ReadXml_Versionless ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
((IXmlSerializable) f).ReadXml (CreateReader ("<dummy></dummy>"));
}
[Test]
public void ReadFrom1 ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
Assert.IsNull (f.Feed, "#1");
f.ReadFrom (CreateReader ("<rss version='2.0'><channel><title>test</title></channel></rss>"));
SyndicationFeed feed1 = f.Feed;
Assert.IsNotNull (f.Feed.Title, "#2");
Assert.AreEqual ("test", f.Feed.Title.Text, "#3");
f.ReadFrom (CreateReader ("<rss version='2.0'><channel><title>test</title></channel></rss>"));
Assert.IsFalse (object.ReferenceEquals (feed1, f.Feed), "#4");
}
[Test]
public void ReadFrom_SyndicationFeed () {
SyndicationFeed f = SyndicationFeed.Load (CreateReader ("<rss version='2.0'><channel><title>test</title></channel></rss>"));
Assert.IsNotNull (f.Title);
}
[Test]
public void ReadXml_TitleOnly ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
((IXmlSerializable) f).ReadXml (CreateReader ("<rss version='2.0'><channel><title>test</title></channel></rss>"));
Assert.IsNotNull (f.Feed.Title, "#1");
Assert.AreEqual ("test", f.Feed.Title.Text, "#2");
((IXmlSerializable) f).ReadXml (CreateReader ("<dummy version='2.0'><channel><title>test</title></channel></dummy>")); // it is ok
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadXmlFromContent ()
{
((IXmlSerializable) new Rss20FeedFormatter ()).ReadXml (CreateReader ("<channel version='2.0'><title>test</title></channel>"));
}
[Test]
public void ReadXml_Extension ()
{
new Rss20FeedFormatter<MySyndicationFeed1> ().ReadFrom (CreateReader ("<rss version='2.0'><channel><foo>test</foo></channel></rss>"));
new Rss20FeedFormatter<MySyndicationFeed2> ().ReadFrom (CreateReader ("<rss version='2.0'><channel><foo>test</foo></channel></rss>"));
try {
new Rss20FeedFormatter<MySyndicationFeed3> ().ReadFrom (CreateReader ("<rss version='2.0'><channel><foo>test</foo></channel></rss>"));
Assert.Fail ("should trigger TryParseElement");
} catch (ApplicationException) {
}
}
class MySyndicationFeed1 : SyndicationFeed
{
protected override bool TryParseElement (XmlReader reader, string version)
{
Assert.AreEqual ("Rss20", version, "#1");
Assert.IsFalse (base.TryParseElement (reader, version), "#2");
return false;
}
}
class MySyndicationFeed2 : SyndicationFeed
{
protected override bool TryParseElement (XmlReader reader, string version)
{
reader.Skip (); // without it, the caller expects that the reader did not proceed.
return true;
}
}
class MySyndicationFeed3 : SyndicationFeed
{
protected override bool TryParseElement (XmlReader reader, string version)
{
throw new ApplicationException ();
}
}
[Test]
[ExpectedException (typeof (XmlException))]
[Category("NotWorking")]
public void ReadFrom_WrongDate1 ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
f.ReadFrom (CreateReader ("<rss version='2.0'><channel><lastBuildDate /></channel></rss>"));
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadFrom_WrongDate2 ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
f.ReadFrom (CreateReader ("<rss version='2.0'><channel><lastBuildDate>2000-01-01T00:00:00</lastBuildDate></rss></channel>"));
}
[Test]
public void ReadFrom_Docs ()
{
Rss20FeedFormatter f = new Rss20FeedFormatter ();
f.ReadFrom (CreateReader ("<rss version='2.0'><channel><docs>documents</docs></channel></rss>"));
Assert.IsNotNull (f.Feed, "#1");
// 'docs' is treated as extensions ...
Assert.AreEqual (1, f.Feed.ElementExtensions.Count, "#2");
}
[Test]
public void GetSchema ()
{
Assert.IsNull (((IXmlSerializable) new Rss20FeedFormatter ()).GetSchema ());
}
[Test]
public void ReadFrom_Feed () {
string feed =
@"<rss version=""2.0"" xmlns:a10=""http://www.w3.org/2005/Atom""><channel><title>My Blog Feed</title><link>http://someuri/</link><description>This is a how to sample that demonstrates how to expose a feed using RSS with WCF</description><managingEditor>someone@microsoft.com</managingEditor><category>How To Sample Code</category><item><guid isPermaLink=""false"">ItemOneID</guid><link>http://localhost/Content/One</link><title>Item One</title><description>This is the content for item one</description><a10:updated>2008-06-02T10:13:13+03:00</a10:updated></item><item><guid isPermaLink=""false"">ItemTwoID</guid><link>http://localhost/Content/Two</link><title>Item Two</title><description>This is the content for item two</description><a10:updated>2008-06-02T10:13:13+03:00</a10:updated></item><item><guid isPermaLink=""false"">ItemThreeID</guid><link>http://localhost/Content/three</link><title>Item Three</title><description>This is the content for item three</description><a10:updated>2008-06-02T10:13:13+03:00</a10:updated></item></channel></rss>";
Rss20FeedFormatter f = new Rss20FeedFormatter ();
f.ReadFrom (CreateReader (feed));
Assert.IsNotNull (f.Feed, "#1");
}
}
}
#endif

View File

@ -0,0 +1,356 @@
//
// Rss20ItemFormatterTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class Rss20ItemFormatterTest
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullItem ()
{
new Rss20ItemFormatter ((SyndicationItem) null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullType ()
{
new Rss20ItemFormatter ((Type) null);
}
/*
[Test]
public void ItemType ()
{
Rss20ItemFormatter f = new Rss20ItemFormatter ();
Assert.IsNull (f.ItemType, "#1");
f = new Rss20ItemFormatter (new SyndicationItem ());
Assert.IsNull (f.ItemType, "#2");
}
*/
[Test]
public void Version ()
{
Rss20ItemFormatter f = new Rss20ItemFormatter ();
Assert.AreEqual ("Rss20", f.Version, "#1");
}
[Test]
public void SerializeExtensionsAsAtom ()
{
Rss20ItemFormatter f = new Rss20ItemFormatter ();
Assert.IsTrue (f.SerializeExtensionsAsAtom, "#1");
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void DefaultConstructorThenWriteXml ()
{
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20ItemFormatter ().WriteTo (w);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void WriteToNull ()
{
SyndicationItem item = new SyndicationItem ();
new Rss20ItemFormatter (item).WriteTo (null);
}
[Test]
public void WriteTo_EmptyItem ()
{
SyndicationItem item = new SyndicationItem ();
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20ItemFormatter (item).WriteTo (w);
// either title or description must exist (RSS 2.0 spec)
Assert.AreEqual ("<item><description /></item>", sw.ToString ());
}
[Test]
public void WriteTo_TitleOnlyItem ()
{
SyndicationItem item = new SyndicationItem ();
item.Title = new TextSyndicationContent ("title text");
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20ItemFormatter (item).WriteTo (w);
Assert.AreEqual ("<item><title>title text</title></item>", sw.ToString ());
}
[Test]
public void WriteTo_CategoryAuthorsContributors ()
{
SyndicationItem item = new SyndicationItem ();
item.Categories.Add (new SyndicationCategory ("myname", "myscheme", "mylabel"));
item.Authors.Add (new SyndicationPerson ("john@doe.com", "John Doe", "http://john.doe.name"));
item.Contributors.Add (new SyndicationPerson ("jane@doe.com", "Jane Doe", "http://jane.doe.name"));
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20ItemFormatter (item).WriteTo (w);
// contributors are serialized as Atom extension
Assert.AreEqual ("<item><author>john@doe.com</author><category domain=\"myscheme\">myname</category><description /><contributor xmlns=\"http://www.w3.org/2005/Atom\"><name>Jane Doe</name><uri>http://jane.doe.name</uri><email>jane@doe.com</email></contributor></item>", sw.ToString ());
}
[Test]
[Category("NotWorking")]
public void WriteTo ()
{
SyndicationItem item = new SyndicationItem ();
item.BaseUri = new Uri ("http://mono-project.com");
item.Copyright = new TextSyndicationContent ("No rights reserved");
item.Content = new XmlSyndicationContent (null, 5, (XmlObjectSerializer) null);
item.Id = "urn:myid";
item.PublishDate = new DateTimeOffset (DateTime.SpecifyKind (new DateTime (2000, 1, 1), DateTimeKind.Utc));
item.LastUpdatedTime = new DateTimeOffset (DateTime.SpecifyKind (new DateTime (2008, 1, 1), DateTimeKind.Utc));
item.SourceFeed = new SyndicationFeed ();
item.SourceFeed.Title = new TextSyndicationContent ("source title");
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20ItemFormatter (item).WriteTo (w);
Assert.AreEqual ("<item xml:base=\"http://mono-project.com/\"><guid isPermaLink=\"false\">urn:myid</guid><description /><source>source title</source><pubDate>Sat, 01 Jan 2000 00:00:00 Z</pubDate><updated xmlns=\"http://www.w3.org/2005/Atom\">2008-01-01T00:00:00Z</updated><rights type=\"text\" xmlns=\"http://www.w3.org/2005/Atom\">No rights reserved</rights><content type=\"text/xml\" xmlns=\"http://www.w3.org/2005/Atom\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">5</int></content></item>", sw.ToString ());
}
[Test]
public void SerializeExtensionsAsAtomFalse ()
{
SyndicationItem item = new SyndicationItem ();
item.Contributors.Add (new SyndicationPerson ("jane@doe.com", "Jane Doe", "http://jane.doe.name"));
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
new Rss20ItemFormatter (item, false).WriteTo (w);
// skip contributors
Assert.AreEqual ("<item><description /></item>", sw.ToString ());
}
[Test]
public void ISerializableWriteXml ()
{
SyndicationItem item = new SyndicationItem ();
item.Title = new TextSyndicationContent ("title text");
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
((IXmlSerializable) new Rss20ItemFormatter (item)).WriteXml (w);
Assert.AreEqual ("<title>title text</title>", sw.ToString ());
}
XmlWriter CreateWriter (StringWriter sw)
{
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
return XmlWriter.Create (sw, s);
}
XmlReader CreateReader (string xml)
{
return XmlReader.Create (new StringReader (xml));
}
[Test]
public void CanRead ()
{
Rss20ItemFormatter f = new Rss20ItemFormatter ();
Assert.IsFalse (f.CanRead (CreateReader ("<rss>")), "#1");
Assert.IsTrue (f.CanRead (CreateReader ("<item>")), "#2");
Assert.IsFalse (f.CanRead (CreateReader ("<item xmlns='urn:foo'>")), "#3");
Assert.IsFalse (f.CanRead (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'>")), "#4");
Assert.IsFalse (f.CanRead (CreateReader ("<hoge>")), "#5");
XmlReader r = CreateReader ("<item></item>");
r.Read (); // element
r.Read (); // endelement
Assert.IsFalse (f.CanRead (r), "#6");
r = CreateReader ("<item><title>test</title></item>");
r.Read (); // item
r.Read (); // title
Assert.IsFalse (f.CanRead (r), "#7");
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadFromInvalid ()
{
new Rss20ItemFormatter ().ReadFrom (CreateReader ("<rss>"));
}
[Test]
public void ReadFrom1 ()
{
Rss20ItemFormatter f = new Rss20ItemFormatter ();
Assert.IsNull (f.Item, "#1");
f.ReadFrom (CreateReader ("<item><title>test</title></item>"));
SyndicationItem item1 = f.Item;
Assert.IsNotNull (f.Item.Title, "#2");
Assert.AreEqual ("test", f.Item.Title.Text, "#3");
f.ReadFrom (CreateReader ("<item><title>test</title></item>"));
Assert.IsFalse (object.ReferenceEquals (item1, f.Item), "#4");
}
[Test]
public void ReadFrom2 () {
SyndicationItem item = new SyndicationItem ();
Rss20ItemFormatter f = new Rss20ItemFormatter (item);
Assert.IsTrue (object.ReferenceEquals (item, f.Item), "Item #1");
f.ReadFrom (CreateReader ("<item><title>test</title></item>"));
Assert.IsFalse (object.ReferenceEquals(item, f.Item), "Item #2");
}
[Test]
public void ReadXml_TitleOnly ()
{
Rss20ItemFormatter f = new Rss20ItemFormatter ();
((IXmlSerializable) f).ReadXml (CreateReader ("<item><title>test</title></item>"));
Assert.IsNotNull (f.Item.Title, "#1");
Assert.AreEqual ("test", f.Item.Title.Text, "#2");
((IXmlSerializable) f).ReadXml (CreateReader ("<dummy><title>test</title></dummy>")); // it is ok
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadXmlFromContent ()
{
((IXmlSerializable) new Rss20ItemFormatter ()).ReadXml (CreateReader ("<title>test</title>"));
}
[Test]
public void ReadXml_Extension ()
{
new Rss20ItemFormatter<MySyndicationItem1> ().ReadFrom (CreateReader ("<item><foo>test</foo></item>"));
new Rss20ItemFormatter<MySyndicationItem2> ().ReadFrom (CreateReader ("<item><foo>test</foo></item>"));
try {
new Rss20ItemFormatter<MySyndicationItem3> ().ReadFrom (CreateReader ("<item><foo>test</foo></item>"));
Assert.Fail ("should trigger TryParseElement");
} catch (ApplicationException) {
}
}
class MySyndicationItem1 : SyndicationItem
{
protected override bool TryParseElement (XmlReader reader, string version)
{
Assert.AreEqual ("Rss20", version, "#1");
Assert.IsFalse (base.TryParseElement (reader, version), "#2");
return false;
}
}
class MySyndicationItem2 : SyndicationItem
{
protected override bool TryParseElement (XmlReader reader, string version)
{
reader.Skip (); // without it, the caller expects that the reader did not proceed.
return true;
}
}
class MySyndicationItem3 : SyndicationItem
{
protected override bool TryParseElement (XmlReader reader, string version)
{
throw new ApplicationException ();
}
}
[Test]
[ExpectedException (typeof (XmlException))]
[Category("NotWorking")]
public void ReadFrom_WrongDate1 ()
{
Rss20ItemFormatter f = new Rss20ItemFormatter ();
f.ReadFrom (CreateReader ("<item><pubDate /></item>"));
}
[Test]
[ExpectedException (typeof (XmlException))]
[Category("NotWorking")]
public void ReadFrom_WrongDate2 ()
{
Rss20ItemFormatter f = new Rss20ItemFormatter ();
f.ReadFrom (CreateReader ("<item><pubDate>2000-01-01T00:00:00</pubDate></item>"));
}
[Test]
public void ReadFrom_Comments ()
{
Rss20ItemFormatter f = new Rss20ItemFormatter ();
f.ReadFrom (CreateReader ("<item><comments>comment</comments></item>"));
Assert.IsNotNull (f.Item, "#1");
// 'comments' is treated as extensions ...
Assert.AreEqual (1, f.Item.ElementExtensions.Count, "#2");
}
[Test]
public void ReadFrom_Enclosure ()
{
Rss20ItemFormatter f = new Rss20ItemFormatter ();
// .NET bug: it allows extension attributes, but rejects extension elements.
// f.ReadFrom (CreateReader ("<item><enclosure url='urn:foo' length='50' type='text/html' wcf='wtf'><extended /></enclosure></item>"));
f.ReadFrom (CreateReader ("<item><enclosure url='urn:foo' length='50' type='text/html' wcf='wtf'></enclosure></item>"));
// 'enclosure' is treated as SyndicationLink
Assert.AreEqual (1, f.Item.Links.Count, "#1");
SyndicationLink link = f.Item.Links [0];
Assert.AreEqual (50, link.Length, "#2");
Assert.AreEqual ("urn:foo", link.Uri.ToString (), "#3");
Assert.AreEqual ("text/html", link.MediaType, "#4");
Assert.AreEqual ("enclosure", link.RelationshipType, "#5");
Assert.AreEqual (1, link.AttributeExtensions.Count, "#6");
//Assert.AreEqual (1, link.ElementExtensions.Count, "#7");
}
[Test]
public void GetSchema ()
{
Assert.IsNull (((IXmlSerializable) new Rss20ItemFormatter ()).GetSchema ());
}
}
}
#endif

View File

@ -0,0 +1,74 @@
//
// ServiceDocumentTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class ServiceDocumentTest
{
static XmlWriterSettings settings = new XmlWriterSettings () { OmitXmlDeclaration = true};
[Test]
public void ConstructorNullWorkspaces ()
{
new ServiceDocument (null); // null workspaces is allowed
}
[Test]
public void GetFormatter ()
{
var v = new ServiceDocument ();
var f = v.GetFormatter ();
Assert.IsTrue (f is AtomPub10ServiceDocumentFormatter, "#1");
Assert.IsTrue (f.Document == v, "#2");
}
[Test]
public void Save ()
{
var v = new ServiceDocument ();
var sw = new StringWriter ();
using (var xw = XmlWriter.Create (sw, settings))
v.Save (xw);
Assert.AreEqual ("<app:service xmlns:a10=\"http://www.w3.org/2005/Atom\" xmlns:app=\"http://www.w3.org/2007/app\" />", sw.ToString ());
}
}
}
#endif

View File

@ -0,0 +1,228 @@
//
// SyndicationElementExtensionTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Syndication;
using NUnit.Framework;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class SyndicationElementExtensionTest
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorExtensionNull ()
{
new SyndicationElementExtension ((object) null);
}
[Test]
public void ConstructorXmlObjectSerializerNull ()
{
// null XmlObjectSerializer is allowed.
var x = new SyndicationElementExtension (5, (XmlObjectSerializer) null);
Assert.AreEqual ("int", x.OuterName, "#1");
Assert.AreEqual ("http://schemas.microsoft.com/2003/10/Serialization/", x.OuterNamespace, "#2");
}
[Test]
public void ConstructorOuterNameNull ()
{
// null name strings are allowed.
var x = new SyndicationElementExtension (null, null, 5, null);
Assert.AreEqual ("int", x.OuterName, "#1");
Assert.AreEqual ("http://schemas.microsoft.com/2003/10/Serialization/", x.OuterNamespace, "#2");
}
[Test]
public void ConstructorXmlSerializerNull ()
{
// null XmlSerializer is allowed.
new SyndicationElementExtension (5, (XmlSerializer) null);
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorXmlSerializerNonSerializable ()
{
new SyndicationElementExtension (new NonXmlSerializable (null), (XmlSerializer) null);
}
public class NonXmlSerializable
{
public NonXmlSerializable (string dummy)
{
}
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorXmlReaderNull ()
{
new SyndicationElementExtension ((XmlReader) null);
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ConstructorXmlReaderUnexpectedEndElement ()
{
string xml = "<root></root>";
XmlReader r = XmlReader.Create (new StringReader (xml));
r.Read ();
r.Read (); // at end element
new SyndicationElementExtension (r);
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ConstructorXmlReaderUnexpectedText ()
{
string xml = "<root>2</root>";
XmlReader r = XmlReader.Create (new StringReader (xml));
r.Read ();
r.Read (); // at text
new SyndicationElementExtension (r);
}
[Test]
public void ConstructorXmlReader ()
{
string xml = "<root xmlns='x'>2</root>";
XmlReader r = XmlReader.Create (new StringReader (xml));
var e = new SyndicationElementExtension (r);
Assert.AreEqual ("root", e.OuterName, "#1");
Assert.AreEqual ("x", e.OuterNamespace, "#2");
}
XmlWriter GetWriter (StringWriter sw)
{
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
return XmlWriter.Create (sw, s);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void WriteTo_NullWriter ()
{
new SyndicationElementExtension (5).WriteTo (null);
}
[Test]
[Category("NotWorking")]
public void WriteTo_Reader ()
{
string xml = "<root><child /><child2 /></root>";
StringWriter sw = new StringWriter ();
using (XmlWriter w = GetWriter (sw)) {
XmlReader r = XmlReader.Create (new StringReader (xml));
r.Read ();
r.Read (); // at child
new SyndicationElementExtension (r).WriteTo (w);
}
Assert.AreEqual ("<child></child>", sw.ToString ());
}
[Test]
[Category("NotWorking")]
public void WriteToTwice_Reader ()
{
string xml = "<root><child /><child2 /></root>";
StringWriter sw = new StringWriter ();
using (XmlWriter w = GetWriter (sw)) {
XmlReader r = XmlReader.Create (new StringReader (xml));
r.Read ();
r.Read (); // at child
SyndicationElementExtension x = new SyndicationElementExtension (r);
w.WriteStartElement ("root");
x.WriteTo (w);
x.WriteTo (w); // it is VALID.
w.WriteEndElement ();
}
Assert.AreEqual ("<root><child></child><child></child></root>", sw.ToString ());
}
[Test]
public void WriteTo_ExtensionObject ()
{
StringWriter sw = new StringWriter ();
using (XmlWriter w = GetWriter (sw))
new SyndicationElementExtension (5).WriteTo (w);
Assert.AreEqual ("<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">5</int>", sw.ToString ());
}
[Test]
public void GetObject_DataContract ()
{
new SyndicationElementExtension (5).GetObject<int> ();
new SyndicationElementExtension (5).GetObject<double> ();
new SyndicationElementExtension ("5").GetObject<int> ();
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void GetObject_DataContract_XmlObjectSerializerNull ()
{
new SyndicationElementExtension (5).GetObject<int> ((XmlObjectSerializer) null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void GetObject_DataContract_XmlSerializerNull ()
{
new SyndicationElementExtension (5).GetObject<int> ((XmlSerializer) null);
}
[Test]
public void GetObject_XmlReader ()
{
string xml = "<root>3</root>";
XmlReader r = XmlReader.Create (new StringReader (xml));
SyndicationElementExtension x = new SyndicationElementExtension (r);
Assert.AreEqual (3, x.GetObject<int> (), "#1");
Assert.AreEqual (3, x.GetObject<int> (), "#2"); // it is VALID
}
[Test]
[ExpectedException (typeof (SerializationException))]
[Category("NotWorking")]
public void GetObject_DataContractError ()
{
new SyndicationElementExtension (DBNull.Value).GetObject<int> (); // Nullable<int> as well
}
}
}
#endif

View File

@ -0,0 +1,106 @@
//
// SyndicationFeedTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class SyndicationFeedTest
{
[Test]
public void ConstructorItemsNull ()
{
Assert.IsNotNull (new SyndicationFeed (null).Items, "#1");
Assert.IsNotNull (new SyndicationFeed (null, null, null, null).Items, "#2");
Assert.IsNotNull (new SyndicationFeed (null, null, null, null, default (DateTimeOffset), null).Items, "#3");
}
[Test]
public void SetNullForProperties ()
{
SyndicationFeed feed = new SyndicationFeed ();
feed.BaseUri = null;
feed.Copyright = null;
feed.Id = null;
feed.Title = null;
feed.Description = null;
feed.Generator = null;
feed.ImageUrl = null;
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void SetItemsNull ()
{
SyndicationFeed feed = new SyndicationFeed ();
feed.Items = null;
}
[Test]
public void Items ()
{
SyndicationFeed feed = new SyndicationFeed ();
Assert.IsNotNull (feed.Items, "#1");
feed.Items = new SyndicationItem [] {new SyndicationItem ()};
Assert.IsTrue (feed.Items.GetEnumerator ().MoveNext (), "#2");
/*
feed.Items = null;
// even after setting null, it autofills a collection.
Assert.IsNotNull (feed.Items, "#3");
Assert.IsFalse (feed.Items.GetEnumerator ().MoveNext (), "#4"); // make sure we reset it
*/
}
[Test]
public void LoadFeed ()
{
SyndicationFeed.Load (XmlReader.Create (new StringReader ("<feed xmlns=\"http://www.w3.org/2005/Atom\"></feed>")));
}
[Test]
[ExpectedException (typeof (XmlException))]
public void LoadEntry ()
{
// entry is not allowed.
SyndicationFeed.Load (XmlReader.Create (new StringReader ("<entry xmlns=\"http://www.w3.org/2005/Atom\"></entry>")));
}
}
}
#endif

View File

@ -0,0 +1,148 @@
//
// SyndicationItemTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class SyndicationItemTest
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void AddPermalinkNull ()
{
SyndicationItem item = new SyndicationItem ();
item.AddPermalink (null);
}
[Test]
public void SetNullForProperties ()
{
SyndicationItem item = new SyndicationItem ();
item.BaseUri = null;
item.Copyright = null;
item.Content = null;
item.Id = null;
item.SourceFeed = null;
item.Summary = null;
item.Title = null;
}
[Test]
public void LastUpdatedTimeBeforePublishDate ()
{
SyndicationItem item = new SyndicationItem ();
item.PublishDate = new DateTimeOffset (new DateTime (2007, 12, 10));
item.LastUpdatedTime = new DateTimeOffset (new DateTime (2007, 12, 9));
}
[Test]
public void AddPermalink ()
{
SyndicationItem item = new SyndicationItem ();
Assert.AreEqual (0, item.Links.Count, "#1");
item.AddPermalink (new Uri ("http://mono-project.com/index.rss.20071210"));
Assert.AreEqual (1, item.Links.Count, "#2");
SyndicationLink link = item.Links [0];
Assert.AreEqual ("http://mono-project.com/index.rss.20071210", link.Uri.ToString (), "#3");
Assert.AreEqual ("alternate", link.RelationshipType, "#4");
}
[Test]
public void Clone ()
{
SyndicationItem item = new SyndicationItem ();
item.AddPermalink (new Uri ("http://mono-project.com/index.rss.20071210"));
item.Id = Guid.NewGuid ().ToString ();
item.BaseUri = new Uri ("http://mono-project.com");
item.Authors.Add (new SyndicationPerson ("atsushi@ximian.com"));
item.SourceFeed = new SyndicationFeed ();
item.SourceFeed.Items = new SyndicationItem [] {new SyndicationItem ()};
SyndicationItem clone = item.Clone ();
Assert.AreEqual (1, clone.Links.Count, "#1");
Assert.AreEqual (item.Id, clone.Id, "#2"); // hmm ...
Assert.AreEqual ("http://mono-project.com/", clone.BaseUri.ToString (), "#3");
// LAMESPEC: .NET fails to clone it
// Assert.IsFalse (Object.ReferenceEquals (item.BaseUri, clone.BaseUri), "#4"); // should not be just a shallow copy
Assert.IsNull (clone.Title, "#5");
Assert.IsNotNull (clone.SourceFeed, "#6");
Assert.IsFalse (Object.ReferenceEquals (item.SourceFeed, clone.SourceFeed), "#7"); // ... not just a shallow copy??
// items in the SourceFeed are not cloned, but Items property is not null
Assert.IsNotNull (clone.SourceFeed.Items, "#8-1");
Assert.IsFalse (clone.SourceFeed.Items.GetEnumerator ().MoveNext (), "#8-2");
Assert.AreEqual (1, clone.Authors.Count, "#9");
SyndicationPerson person = clone.Authors [0];
Assert.IsFalse (Object.ReferenceEquals (item.Authors [0], person), "#10"); // should not be just a shallow copy
Assert.AreEqual ("atsushi@ximian.com", person.Email, "#11");
}
[Test]
public void GetRss20Formatter ()
{
SyndicationItem item = new SyndicationItem ();
Rss20ItemFormatter f = item.GetRss20Formatter ();
Assert.AreEqual (true, f.SerializeExtensionsAsAtom, "#1");
}
[Test]
[ExpectedException (typeof (XmlException))]
public void LoadNonAtomRss ()
{
SyndicationItem.Load (XmlReader.Create (new StringReader ("<dummy />")));
}
[Test]
[ExpectedException (typeof (XmlException))]
public void LoadFeed ()
{
// feed is not allowed.
SyndicationItem.Load (XmlReader.Create (new StringReader ("<feed xmlns=\"http://www.w3.org/2005/Atom\"></feed>")));
}
[Test]
public void LoadEntry ()
{
SyndicationItem.Load (XmlReader.Create (new StringReader ("<entry xmlns=\"http://www.w3.org/2005/Atom\"></entry>")));
}
}
}
#endif

View File

@ -0,0 +1,219 @@
//
// SyndicationLinkTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class SyndicationLinkTest
{
[Test]
public void Constructor ()
{
// null Uri is allowed
SyndicationLink link = new SyndicationLink (null);
link = new SyndicationLink (new Uri ("empty.xml", UriKind.Relative));
Assert.AreEqual ("empty.xml", link.Uri.ToString (), "#1-1");
Assert.AreEqual (null, link.BaseUri, "#1-2");
Assert.AreEqual (0, link.Length, "#1-3");
Assert.AreEqual (null, link.MediaType, "#1-4");
link = new SyndicationLink (null, null, null, null, 0);
}
[Test]
public void TestUri ()
{
SyndicationLink link = new SyndicationLink (new Uri ("empty.xml", UriKind.Relative));
link.Uri = null;
Assert.IsNull (link.Uri, "#1");
Assert.IsNull (link.GetAbsoluteUri (), "#2");
}
[Test]
public void TestBaseUri ()
{
// relative
SyndicationLink link = new SyndicationLink (new Uri ("empty.xml", UriKind.Relative));
Assert.IsNull (link.BaseUri, "#1");
// absolute
link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
Assert.IsNull (link.BaseUri, "#2");
// absolute #2
link = new SyndicationLink ();
link.Uri = new Uri ("http://mono-project.com/index.rss");
Assert.IsNull (link.BaseUri, "#3");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
[Category ("NotDotNet")] // LAMESPEC. See below.
public void SetRelativeUriAsBaseUri ()
{
SyndicationLink link = new SyndicationLink (new Uri ("empty.xml", UriKind.Relative));
// LAMESPEC: setting relative Uri as BaseUri is allowed (likely broken)
link.BaseUri = new Uri ("base.xml", UriKind.Relative);
// and below causes ArgumentOutOfRangeException.
// Assert.IsNull (link.GetAbsoluteUri (), "#1");
}
[Test]
public void MediaType ()
{
SyndicationLink link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
link.MediaType = "text/xml";
Assert.AreEqual ("text/xml", link.MediaType, "#1");
link.MediaType = null;
Assert.IsNull (link.MediaType, "#2");
link.MediaType = "WTF"; // no error
}
[Test]
public void RelationshipType ()
{
SyndicationLink link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
link.RelationshipType = "alternate";
Assert.AreEqual ("alternate", link.RelationshipType, "#1");
link.RelationshipType = null;
Assert.IsNull (link.RelationshipType, "#2");
link.RelationshipType = "WTF"; // no error
}
[Test]
public void Length ()
{
SyndicationLink link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
link.Length = 0;
link.Length = long.MaxValue;
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void NegativeLength ()
{
SyndicationLink link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
link.Length = -1;
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void NegativeLength2 ()
{
new SyndicationLink (null, null, null, null, -1);
}
[Test]
public void AttributeElementExtensions ()
{
// The properties do not affect extension attributes.
SyndicationLink link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
Assert.AreEqual (0, link.ElementExtensions.Count, "#0");
Assert.IsFalse (link.AttributeExtensions.ContainsKey (new QName ("mediaType")), "#3");
link.MediaType = "text/xml";
}
[Test]
public void GetAbsoluteUri ()
{
// no Uri
SyndicationLink link = new SyndicationLink ();
Assert.IsNull (link.GetAbsoluteUri (), "#1");
// Uri is relative
link = new SyndicationLink (new Uri ("empty.xml", UriKind.Relative));
Assert.IsNull (link.GetAbsoluteUri (), "#2");
// Uri is absolute
link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
Assert.AreEqual ("http://mono-project.com/index.rss", link.GetAbsoluteUri ().ToString (), "#3");
// only BaseUri - null result
link = new SyndicationLink ();
link.BaseUri = new Uri ("http://mono-project.com/index.rss");
Assert.IsNull (link.GetAbsoluteUri (), "#4");
}
[Test]
public void Clone ()
{
SyndicationLink link = new SyndicationLink (null, null, "my RSS", "text/xml", 1);
link.BaseUri = new Uri ("http://mono-project.com/index.rss");
SyndicationLink clone = link.Clone ();
Assert.AreEqual (link.BaseUri, clone.BaseUri, "#1");
Assert.AreEqual ("my RSS", clone.Title, "#2");
Assert.AreEqual ("text/xml", clone.MediaType, "#3");
Assert.IsNull (clone.RelationshipType, "#4");
Assert.IsNull (clone.Uri, "#5");
}
[Test]
public void CreateAlternateLink ()
{
SyndicationLink link = SyndicationLink.CreateAlternateLink (null);
Assert.IsNull (link.Uri, "#1");
Assert.IsNull (link.MediaType, "#2");
Assert.AreEqual ("alternate", link.RelationshipType, "#3");
}
[Test]
public void CreateMediaEnclosureLink ()
{
SyndicationLink link = SyndicationLink.CreateMediaEnclosureLink (null, null, 1);
Assert.IsNull (link.Uri, "#1");
Assert.IsNull (link.MediaType, "#2");
Assert.AreEqual (1, link.Length, "#3");
Assert.AreEqual ("enclosure", link.RelationshipType, "#4");
}
[Test]
public void CreateSelfLink ()
{
SyndicationLink link = SyndicationLink.CreateSelfLink (null);
Assert.IsNull (link.Uri, "#1");
Assert.IsNull (link.MediaType, "#2");
Assert.AreEqual ("self", link.RelationshipType, "#3");
}
}
}
#endif

View File

@ -0,0 +1,101 @@
//
// TextSyndicationContentTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class TextSyndicationContentTest
{
[Test]
public void Constructor ()
{
TextSyndicationContent t = new TextSyndicationContent (null); // hmm, null is allowed...
Assert.IsNull (t.Text, "#0");
t = new TextSyndicationContent ("test");
Assert.AreEqual ("test", t.Text, "#1");
Assert.AreEqual ("text", t.Type, "#2");
t = new TextSyndicationContent ("test", TextSyndicationContentKind.Html);
Assert.AreEqual ("html", t.Type, "#3");
t = new TextSyndicationContent ("test", TextSyndicationContentKind.XHtml);
Assert.AreEqual ("xhtml", t.Type, "#4");
}
[Test]
public void Clone ()
{
TextSyndicationContent t = new TextSyndicationContent ("test");
t = t.Clone () as TextSyndicationContent;
Assert.AreEqual ("test", t.Text, "#1");
Assert.AreEqual ("text", t.Type, "#2");
t = new TextSyndicationContent ("test", TextSyndicationContentKind.Html);
t = t.Clone () as TextSyndicationContent;
Assert.AreEqual ("html", t.Type, "#3");
}
[Test]
public void WriteTo ()
{
TextSyndicationContent t = new TextSyndicationContent (null);
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
t.WriteTo (w, "root", String.Empty);
Assert.AreEqual ("<root type=\"text\"></root>", sw.ToString ());
t = new TextSyndicationContent ("broken<b>html", TextSyndicationContentKind.Html);
sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
t.WriteTo (w, "root", String.Empty);
Assert.AreEqual ("<root type=\"html\">broken&lt;b&gt;html</root>", sw.ToString ());
}
XmlWriter CreateWriter (StringWriter sw)
{
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
return XmlWriter.Create (sw, s);
}
}
}
#endif

View File

@ -0,0 +1,100 @@
//
// UrlSyndicationContentTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class UrlSyndicationContentTest
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullUrl ()
{
new UrlSyndicationContent (null, "text/plain");
}
[Test]
public void Constructor ()
{
Uri uri = new Uri ("http://mono-project.com");
UrlSyndicationContent t = new UrlSyndicationContent (uri, null);
t = new UrlSyndicationContent (uri, "text/plain");
Assert.AreEqual (uri, t.Url, "#1");
Assert.AreEqual ("text/plain", t.Type, "#2");
}
[Test]
public void Clone ()
{
Uri uri = new Uri ("http://mono-project.com");
UrlSyndicationContent t = new UrlSyndicationContent (uri, "text/plain");
t = t.Clone () as UrlSyndicationContent;
Assert.AreEqual (uri, t.Url, "#1");
Assert.AreEqual ("text/plain", t.Type, "#2");
}
[Test]
[Category("NotWorking")]
public void WriteTo ()
{
Uri uri = new Uri ("http://mono-project.com/");
UrlSyndicationContent t = new UrlSyndicationContent (uri, null);
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
t.WriteTo (w, "root", String.Empty);
Assert.AreEqual ("<root type=\"\" src=\"http://mono-project.com/\" />", sw.ToString ());
t = new UrlSyndicationContent (uri, "application/xml+svg");
sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
t.WriteTo (w, "root", String.Empty);
Assert.AreEqual ("<root type=\"application/xml+svg\" src=\"http://mono-project.com/\" />", sw.ToString ());
}
XmlWriter CreateWriter (StringWriter sw)
{
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
return XmlWriter.Create (sw, s);
}
}
}
#endif

View File

@ -0,0 +1,141 @@
//
// XmlSyndicationContentTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if !MOBILE
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Syndication;
using NUnit.Framework;
using QName = System.Xml.XmlQualifiedName;
namespace MonoTests.System.ServiceModel.Syndication
{
[TestFixture]
public class XmlSyndicationContentTest
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullReader ()
{
new XmlSyndicationContent ((XmlReader) null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullExtensionObject ()
{
new XmlSyndicationContent ("text/plain", null, (XmlObjectSerializer) null);
}
[Test]
public void ConstructorNullSerializer ()
{
// allowed
new XmlSyndicationContent ("text/plain", 5, (XmlObjectSerializer) null);
new XmlSyndicationContent ("text/plain", 5, (XmlSerializer) null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullExtension ()
{
new XmlSyndicationContent ("text/plain", null);
}
[Test]
public void ConstructorNullType ()
{
// allowed
new XmlSyndicationContent (null, 5, (XmlObjectSerializer) null);
}
[Test]
public void Type ()
{
XmlSyndicationContent t = new XmlSyndicationContent (null, 3, (XmlObjectSerializer) null);
Assert.AreEqual ("text/xml", t.Type, "#1");
t = new XmlSyndicationContent ("text/plain", 3, (XmlObjectSerializer) null);
Assert.AreEqual ("text/plain", t.Type, "#2");
}
[Test]
public void Clone ()
{
XmlSyndicationContent t = new XmlSyndicationContent ("text/plain", 3, (XmlObjectSerializer) null);
t = t.Clone () as XmlSyndicationContent;
Assert.AreEqual ("text/plain", t.Type, "#1");
}
[Test]
public void WriteTo ()
{
XmlSyndicationContent t = new XmlSyndicationContent ("text/plain", 6, (XmlObjectSerializer) null);
StringWriter sw = new StringWriter ();
using (XmlWriter w = CreateWriter (sw))
t.WriteTo (w, "root", String.Empty);
Assert.AreEqual ("<root type=\"text/plain\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int></root>", sw.ToString ());
}
XmlWriter CreateWriter (StringWriter sw)
{
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
return XmlWriter.Create (sw, s);
}
[Test]
public void GetReaderAtContent ()
{
var x = new SyndicationElementExtension (6);
// premise.
Assert.AreEqual ("<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int>", x.GetReader ().ReadOuterXml (), "#1");
var t = new XmlSyndicationContent ("text/xml", 6, (XmlObjectSerializer) null);
Assert.AreEqual ("<content type=\"text/xml\" xmlns=\"http://www.w3.org/2005/Atom\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int></content>", t.GetReaderAtContent ().ReadOuterXml (), "#2");
}
[Test]
public void GetReaderAtContent2 ()
{
var inxml = "<xcontent type=\"text/xhtml\" xmlns=\"XXX-http://www.w3.org/2005/Atom\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int></xcontent>";
var ms = new MemoryStream ();
using (var xw = XmlWriter.Create (ms))
new XmlSyndicationContent (XmlReader.Create (new StringReader (inxml))).WriteTo (xw, "contentsss", "urn:x");
ms.Position = 0;
var expected = "<?xml version='1.0' encoding='utf-8'?><contentsss type='text/xml' xmlns='urn:x'><int xmlns='http://schemas.microsoft.com/2003/10/Serialization/'>6</int></contentsss>".Replace ('\'', '"');
Assert.AreEqual (expected, new StreamReader (ms).ReadToEnd (), "#1");
}
}
}
#endif