2 // Atom10ItemFormatterTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
32 using System.Runtime.Serialization;
34 using System.Text.RegularExpressions;
36 using System.Xml.Serialization;
37 using System.ServiceModel.Syndication;
38 using NUnit.Framework;
40 using QName = System.Xml.XmlQualifiedName;
42 namespace MonoTests.System.ServiceModel.Syndication
45 public class Atom10ItemFormatterTest
48 [ExpectedException (typeof (ArgumentNullException))]
49 public void ConstructorNullItem ()
51 new Atom10ItemFormatter ((SyndicationItem) null);
55 [ExpectedException (typeof (ArgumentNullException))]
56 public void ConstructorNullType ()
58 new Atom10ItemFormatter ((Type) null);
63 public void ItemType ()
65 Atom10ItemFormatter f = new Atom10ItemFormatter ();
66 Assert.IsNull (f.ItemType, "#1");
67 f = new Atom10ItemFormatter (new SyndicationItem ());
68 Assert.IsNull (f.ItemType, "#2");
73 public void Version ()
75 Atom10ItemFormatter f = new Atom10ItemFormatter ();
76 Assert.AreEqual ("Atom10", f.Version, "#1");
80 [ExpectedException (typeof (InvalidOperationException))]
81 public void DefaultConstructorThenWriteXml ()
83 StringWriter sw = new StringWriter ();
84 using (XmlWriter w = CreateWriter (sw))
85 new Atom10ItemFormatter ().WriteTo (w);
89 [ExpectedException (typeof (ArgumentNullException))]
90 public void WriteToNull ()
92 SyndicationItem item = new SyndicationItem ();
93 new Atom10ItemFormatter (item).WriteTo (null);
96 string DummyId (string s)
98 return Regex.Replace (s, "<id>.+</id>", "<id>XXX</id>");
101 string DummyId2 (string s)
103 return Regex.Replace (s, "<id xmlns=\"http://www.w3.org/2005/Atom\">.+</id>", "<id>XXX</id>");
106 string DummyUpdated (string s)
108 return Regex.Replace (s, "<updated>.+</updated>", "<updated>XXX</updated>");
111 string DummyUpdated2 (string s)
113 return Regex.Replace (s, "<updated xmlns=\"http://www.w3.org/2005/Atom\">.+</updated>", "<updated>XXX</updated>");
117 public void WriteTo_EmptyItem ()
119 // It however automatically fills id (very likely bug in .NET) and DateTimeOffset though.
120 SyndicationItem item = new SyndicationItem ();
121 StringWriter sw = new StringWriter ();
122 using (XmlWriter w = CreateWriter (sw))
123 new Atom10ItemFormatter (item).WriteTo (w);
124 Assert.IsNull (item.Id, "#1"); // automatically generated, but not automatically set.
125 using (XmlWriter w = CreateWriter (sw))
126 new Atom10ItemFormatter (item).WriteTo (w);
127 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 ())));
131 public void WriteTo_TitleOnlyItem ()
133 // It however automatically fills id (very likely bug in .NET) and DateTimeOffset though.
134 SyndicationItem item = new SyndicationItem ();
135 item.Title = new TextSyndicationContent ("title text");
136 StringWriter sw = new StringWriter ();
137 using (XmlWriter w = CreateWriter (sw))
138 new Atom10ItemFormatter (item).WriteTo (w);
139 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 ())));
143 public void WriteTo_CategoryAuthorsContributors ()
145 // It however automatically fills ...
146 SyndicationItem item = new SyndicationItem ();
147 item.Categories.Add (new SyndicationCategory ("myname", "myscheme", "mylabel"));
148 item.Authors.Add (new SyndicationPerson ("john@doe.com", "John Doe", "http://john.doe.name"));
149 item.Contributors.Add (new SyndicationPerson ("jane@doe.com", "Jane Doe", "http://jane.doe.name"));
150 StringWriter sw = new StringWriter ();
151 using (XmlWriter w = CreateWriter (sw))
152 new Atom10ItemFormatter (item).WriteTo (w);
153 // contributors are serialized as Atom extension
154 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 ())));
158 public void WriteTo ()
160 SyndicationItem item = new SyndicationItem ();
161 item.BaseUri = new Uri ("http://mono-project.com");
162 item.Copyright = new TextSyndicationContent ("No rights reserved");
163 item.Content = new XmlSyndicationContent (null, 5, (XmlObjectSerializer) null);
164 // .NET bug: it ignores this value.
165 item.Id = "urn:myid";
166 item.PublishDate = new DateTimeOffset (DateTime.SpecifyKind (new DateTime (2000, 1, 1), DateTimeKind.Utc));
167 item.LastUpdatedTime = new DateTimeOffset (DateTime.SpecifyKind (new DateTime (2008, 1, 1), DateTimeKind.Utc));
168 //item.SourceFeed = new SyndicationFeed ();
169 item.Summary = new TextSyndicationContent ("great text");
171 StringWriter sw = new StringWriter ();
172 using (XmlWriter w = CreateWriter (sw))
173 new Atom10ItemFormatter (item).WriteTo (w);
174 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 ()));
178 public void ISerializableWriteXml ()
180 SyndicationItem item = new SyndicationItem ();
181 item.Title = new TextSyndicationContent ("title text");
182 StringWriter sw = new StringWriter ();
183 using (XmlWriter w = CreateWriter (sw)) {
184 w.WriteStartElement ("dummy");
185 ((IXmlSerializable) new Atom10ItemFormatter (item)).WriteXml (w);
186 w.WriteEndElement ();
188 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 ())));
192 public void WriteTo_IllegalDuplicateAltLinks ()
194 // ... and it passes.
195 SyndicationItem item = new SyndicationItem ();
196 item.Links.Add (new SyndicationLink (new Uri ("http://mono-project.com/Page1"), "alternate", "Page 1", "text/html", 0));
197 item.Links.Add (new SyndicationLink (new Uri ("http://mono-project.com/Page2"), "alternate", "Page 2", "text/html", 0));
198 StringWriter sw = new StringWriter ();
199 using (XmlWriter w = CreateWriter (sw))
200 new Atom10ItemFormatter (item).WriteTo (w);
201 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 ())));
204 XmlWriter CreateWriter (StringWriter sw)
206 XmlWriterSettings s = new XmlWriterSettings ();
207 s.OmitXmlDeclaration = true;
208 return XmlWriter.Create (sw, s);
211 XmlReader CreateReader (string xml)
213 return XmlReader.Create (new StringReader (xml));
217 public void CanRead ()
219 Atom10ItemFormatter f = new Atom10ItemFormatter ();
220 Assert.IsFalse (f.CanRead (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'>")), "#1");
221 Assert.IsTrue (f.CanRead (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'>")), "#2");
222 Assert.IsFalse (f.CanRead (CreateReader ("<entry>")), "#3");
223 Assert.IsFalse (f.CanRead (CreateReader ("<item>")), "#4");
224 Assert.IsFalse (f.CanRead (CreateReader ("<hoge xmlns='http://www.w3.org/2005/Atom'>")), "#5");
225 XmlReader r = CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'></entry>");
226 r.Read (); // element
227 r.Read (); // endelement
228 Assert.IsFalse (f.CanRead (r), "#6");
230 r = CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>");
233 Assert.IsFalse (f.CanRead (r), "#7");
237 [ExpectedException (typeof (XmlException))]
238 public void ReadFromInvalid ()
240 new Atom10ItemFormatter ().ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom' />"));
244 public void ReadFrom1 ()
246 Atom10ItemFormatter f = new Atom10ItemFormatter ();
247 Assert.IsNull (f.Item, "#1");
248 f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>"));
249 SyndicationItem item1 = f.Item;
250 Assert.IsNotNull (f.Item.Title, "#2");
251 Assert.AreEqual ("test", f.Item.Title.Text, "#3");
252 f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>"));
253 Assert.IsFalse (object.ReferenceEquals (item1, f.Item), "#4");
257 public void ReadXml_TitleOnly ()
259 Atom10ItemFormatter f = new Atom10ItemFormatter ();
260 ((IXmlSerializable) f).ReadXml (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>"));
261 Assert.IsNotNull (f.Item.Title, "#1");
262 Assert.AreEqual ("test", f.Item.Title.Text, "#2");
264 ((IXmlSerializable) f).ReadXml (CreateReader ("<dummy><title>test</title></dummy>")); // it is ok
268 [ExpectedException (typeof (XmlException))]
269 public void ReadXmlFromContent ()
271 ((IXmlSerializable) new Atom10ItemFormatter ()).ReadXml (CreateReader ("<title xmlns='http://www.w3.org/2005/Atom'>test</title>"));
275 public void ReadXml_Extension ()
277 new Atom10ItemFormatter<MySyndicationItem1> ().ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><foo>test</foo></entry>"));
278 new Atom10ItemFormatter<MySyndicationItem2> ().ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><foo>test</foo></entry>"));
280 new Atom10ItemFormatter<MySyndicationItem3> ().ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><foo>test</foo></entry>"));
281 Assert.Fail ("should trigger TryParseElement");
282 } catch (ApplicationException) {
286 class MySyndicationItem1 : SyndicationItem
288 protected override bool TryParseElement (XmlReader reader, string version)
290 Assert.AreEqual ("Atom10", version, "#1");
291 Assert.IsFalse (base.TryParseElement (reader, version), "#2");
296 class MySyndicationItem2 : SyndicationItem
298 protected override bool TryParseElement (XmlReader reader, string version)
300 reader.Skip (); // without it, the caller expects that the reader did not proceed.
305 class MySyndicationItem3 : SyndicationItem
307 protected override bool TryParseElement (XmlReader reader, string version)
309 throw new ApplicationException ();
314 // It is not rejected. Though I think it is .NET bug.
315 public void ReadFrom_EmptyDate ()
317 Atom10ItemFormatter f = new Atom10ItemFormatter ();
318 f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><pubDate /></entry>"));
322 [ExpectedException (typeof (XmlException))]
323 public void ReadFrom_WrongDate ()
325 Atom10ItemFormatter f = new Atom10ItemFormatter ();
326 f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><published>Sat, 01 Jan 2000 00:00:00 Z</pubDate></entry>"));
330 public void ReadFrom_Extension ()
332 Atom10ItemFormatter f = new Atom10ItemFormatter ();
333 f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><ext>external</ext></entry>"));
334 Assert.IsNotNull (f.Item, "#1");
335 Assert.AreEqual (1, f.Item.ElementExtensions.Count, "#2");
339 public void ReadFrom_Id ()
341 Atom10ItemFormatter f = new Atom10ItemFormatter ();
342 f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><id>urn:myid</id></entry>"));
343 Assert.IsNotNull (f.Item, "#1");
344 Assert.AreEqual ("urn:myid", f.Item.Id, "#2");
348 public void ReadFrom_Link ()
350 Atom10ItemFormatter f = new Atom10ItemFormatter ();
352 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>"));
354 Assert.AreEqual (1, f.Item.Links.Count, "#1");
355 SyndicationLink link = f.Item.Links [0];
356 Assert.AreEqual (50, link.Length, "#2");
357 Assert.AreEqual ("urn:foo", link.Uri.ToString (), "#3");
358 Assert.AreEqual ("text/html", link.MediaType, "#4");
359 Assert.AreEqual ("enclosure", link.RelationshipType, "#5");
360 Assert.AreEqual (1, link.AttributeExtensions.Count, "#6");
361 Assert.AreEqual (1, link.ElementExtensions.Count, "#7");
365 public void GetSchema ()
367 Assert.IsNull (((IXmlSerializable) new Atom10ItemFormatter ()).GetSchema ());
371 public void TestToString ()
373 Assert.AreEqual (typeof (Atom10ItemFormatter).FullName + ", SyndicationVersion=Atom10", new Atom10ItemFormatter (new SyndicationItem ()).ToString ());