Update PointConverter.cs
[mono.git] / mcs / class / System.ServiceModel.Web / Test / System.ServiceModel.Syndication / Atom10ItemFormatterTest.cs
1 //
2 // Atom10ItemFormatterTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.IO;
32 using System.Runtime.Serialization;
33 using System.Text;
34 using System.Text.RegularExpressions;
35 using System.Xml;
36 using System.Xml.Serialization;
37 using System.ServiceModel.Syndication;
38 using NUnit.Framework;
39
40 using QName = System.Xml.XmlQualifiedName;
41
42 namespace MonoTests.System.ServiceModel.Syndication
43 {
44         [TestFixture]
45         public class Atom10ItemFormatterTest
46         {
47                 [Test]
48                 [ExpectedException (typeof (ArgumentNullException))]
49                 public void ConstructorNullItem ()
50                 {
51                         new Atom10ItemFormatter ((SyndicationItem) null);
52                 }
53
54                 [Test]
55                 [ExpectedException (typeof (ArgumentNullException))]
56                 public void ConstructorNullType ()
57                 {
58                         new Atom10ItemFormatter ((Type) null);
59                 }
60
61                 /*
62                 [Test]
63                 public void ItemType ()
64                 {
65                         Atom10ItemFormatter f = new Atom10ItemFormatter ();
66                         Assert.IsNull (f.ItemType, "#1");
67                         f = new Atom10ItemFormatter (new SyndicationItem ());
68                         Assert.IsNull (f.ItemType, "#2");
69                 }
70                 */
71
72                 [Test]
73                 public void Version ()
74                 {
75                         Atom10ItemFormatter f = new Atom10ItemFormatter ();
76                         Assert.AreEqual ("Atom10", f.Version, "#1");
77                 }
78
79                 [Test]
80                 [ExpectedException (typeof (InvalidOperationException))]
81                 public void DefaultConstructorThenWriteXml ()
82                 {
83                         StringWriter sw = new StringWriter ();
84                         using (XmlWriter w = CreateWriter (sw))
85                                 new Atom10ItemFormatter ().WriteTo (w);
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (ArgumentNullException))]
90                 public void WriteToNull ()
91                 {
92                         SyndicationItem item = new SyndicationItem ();
93                         new Atom10ItemFormatter (item).WriteTo (null);
94                 }
95
96                 string DummyId (string s)
97                 {
98                         return Regex.Replace (s, "<id>.+</id>", "<id>XXX</id>");
99                 }
100
101                 string DummyId2 (string s)
102                 {
103                         return Regex.Replace (s, "<id xmlns=\"http://www.w3.org/2005/Atom\">.+</id>", "<id>XXX</id>");
104                 }
105
106                 string DummyUpdated (string s)
107                 {
108                         return Regex.Replace (s, "<updated>.+</updated>", "<updated>XXX</updated>");
109                 }
110
111                 string DummyUpdated2 (string s)
112                 {
113                         return Regex.Replace (s, "<updated xmlns=\"http://www.w3.org/2005/Atom\">.+</updated>", "<updated>XXX</updated>");
114                 }
115
116                 [Test]
117                 public void WriteTo_EmptyItem ()
118                 {
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 ())));
128                 }
129
130                 [Test]
131                 public void WriteTo_TitleOnlyItem ()
132                 {
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 ())));
140                 }
141
142                 [Test]
143                 public void WriteTo_CategoryAuthorsContributors ()
144                 {
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 ())));
155                 }
156
157                 [Test]
158                 public void WriteTo ()
159                 {
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");
170
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 ()));
175                 }
176
177                 [Test]
178                 public void ISerializableWriteXml ()
179                 {
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 ();
187                         }
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 ())));
189                 }
190
191                 [Test]
192                 public void WriteTo_IllegalDuplicateAltLinks ()
193                 {
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 ())));
202                 }
203
204                 XmlWriter CreateWriter (StringWriter sw)
205                 {
206                         XmlWriterSettings s = new XmlWriterSettings ();
207                         s.OmitXmlDeclaration = true;
208                         return XmlWriter.Create (sw, s);
209                 }
210
211                 XmlReader CreateReader (string xml)
212                 {
213                         return XmlReader.Create (new StringReader (xml));
214                 }
215
216                 [Test]
217                 public void CanRead ()
218                 {
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");
229
230                         r = CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>");
231                         r.Read (); // item
232                         r.Read (); // title
233                         Assert.IsFalse (f.CanRead (r), "#7");
234                 }
235
236                 [Test]
237                 [ExpectedException (typeof (XmlException))]
238                 public void ReadFromInvalid ()
239                 {
240                         new Atom10ItemFormatter ().ReadFrom (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom' />"));
241                 }
242
243                 [Test]
244                 public void ReadFrom1 ()
245                 {
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");
254                 }
255
256                 [Test]
257                 public void ReadXml_TitleOnly ()
258                 {
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");
263
264                         ((IXmlSerializable) f).ReadXml (CreateReader ("<dummy><title>test</title></dummy>")); // it is ok
265                 }
266
267                 [Test]
268                 [ExpectedException (typeof (XmlException))]
269                 public void ReadXmlFromContent ()
270                 {
271                         ((IXmlSerializable) new Atom10ItemFormatter ()).ReadXml (CreateReader ("<title xmlns='http://www.w3.org/2005/Atom'>test</title>"));
272                 }
273
274                 [Test]
275                 public void ReadXml_Extension ()
276                 {
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>"));
279                         try {
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) {
283                         }
284                 }
285
286                 class MySyndicationItem1 : SyndicationItem
287                 {
288                         protected override bool TryParseElement (XmlReader reader, string version)
289                         {
290                                 Assert.AreEqual ("Atom10", version, "#1");
291                                 Assert.IsFalse (base.TryParseElement (reader, version), "#2");
292                                 return false;
293                         }
294                 }
295
296                 class MySyndicationItem2 : SyndicationItem
297                 {
298                         protected override bool TryParseElement (XmlReader reader, string version)
299                         {
300                                 reader.Skip (); // without it, the caller expects that the reader did not proceed.
301                                 return true;
302                         }
303                 }
304
305                 class MySyndicationItem3 : SyndicationItem
306                 {
307                         protected override bool TryParseElement (XmlReader reader, string version)
308                         {
309                                 throw new ApplicationException ();
310                         }
311                 }
312
313                 [Test]
314                 // It is not rejected. Though I think it is .NET bug.
315                 public void ReadFrom_EmptyDate ()
316                 {
317                         Atom10ItemFormatter f = new Atom10ItemFormatter ();
318                         f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><pubDate /></entry>"));
319                 }
320
321                 [Test]
322                 [ExpectedException (typeof (XmlException))]
323                 public void ReadFrom_WrongDate ()
324                 {
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>"));
327                 }
328
329                 [Test]
330                 public void ReadFrom_Extension ()
331                 {
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");
336                 }
337
338                 [Test]
339                 public void ReadFrom_Id ()
340                 {
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");
345                 }
346
347                 [Test]
348                 public void ReadFrom_Link ()
349                 {
350                         Atom10ItemFormatter f = new Atom10ItemFormatter ();
351
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>"));
353
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");
362                 }
363
364                 [Test]
365                 public void GetSchema ()
366                 {
367                         Assert.IsNull (((IXmlSerializable) new Atom10ItemFormatter ()).GetSchema ());
368                 }
369
370                 [Test]
371                 public void TestToString ()
372                 {
373                         Assert.AreEqual (typeof (Atom10ItemFormatter).FullName + ", SyndicationVersion=Atom10", new Atom10ItemFormatter (new SyndicationItem ()).ToString ());
374                 }
375         }
376 }