Merge pull request #799 from kebby/master
[mono.git] / mcs / class / System.ServiceModel.Web / Test / System.ServiceModel.Syndication / XmlSyndicationContentTest.cs
1 //
2 // XmlSyndicationContentTest.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 #if !MOBILE
29 using System;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.IO;
33 using System.Runtime.Serialization;
34 using System.Text;
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 XmlSyndicationContentTest
46         {
47                 [Test]
48                 [ExpectedException (typeof (ArgumentNullException))]
49                 public void ConstructorNullReader ()
50                 {
51                         new XmlSyndicationContent ((XmlReader) null);
52                 }
53
54                 [Test]
55                 [ExpectedException (typeof (ArgumentNullException))]
56                 public void ConstructorNullExtensionObject ()
57                 {
58                         new XmlSyndicationContent ("text/plain", null, (XmlObjectSerializer) null);
59                 }
60
61                 [Test]
62                 public void ConstructorNullSerializer ()
63                 {
64                         // allowed
65                         new XmlSyndicationContent ("text/plain", 5, (XmlObjectSerializer) null);
66                         new XmlSyndicationContent ("text/plain", 5, (XmlSerializer) null);
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (ArgumentNullException))]
71                 public void ConstructorNullExtension ()
72                 {
73                         new XmlSyndicationContent ("text/plain", null);
74                 }
75
76                 [Test]
77                 public void ConstructorNullType ()
78                 {
79                         // allowed
80                         new XmlSyndicationContent (null, 5, (XmlObjectSerializer) null);
81                 }
82
83                 [Test]
84                 public void Type ()
85                 {
86                         XmlSyndicationContent t = new XmlSyndicationContent (null, 3, (XmlObjectSerializer) null);
87                         Assert.AreEqual ("text/xml", t.Type, "#1");
88                         t = new XmlSyndicationContent ("text/plain", 3, (XmlObjectSerializer) null);
89                         Assert.AreEqual ("text/plain", t.Type, "#2");
90                 }
91
92                 [Test]
93                 public void Clone ()
94                 {
95                         XmlSyndicationContent t = new XmlSyndicationContent ("text/plain", 3, (XmlObjectSerializer) null);
96                         t = t.Clone () as XmlSyndicationContent;
97                         Assert.AreEqual ("text/plain", t.Type, "#1");
98                 }
99
100                 [Test]
101                 public void WriteTo ()
102                 {
103                         XmlSyndicationContent t = new XmlSyndicationContent ("text/plain", 6, (XmlObjectSerializer) null);
104                         StringWriter sw = new StringWriter ();
105                         using (XmlWriter w = CreateWriter (sw))
106                                 t.WriteTo (w, "root", String.Empty);
107                         Assert.AreEqual ("<root type=\"text/plain\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int></root>", sw.ToString ());
108                 }
109
110                 XmlWriter CreateWriter (StringWriter sw)
111                 {
112                         XmlWriterSettings s = new XmlWriterSettings ();
113                         s.OmitXmlDeclaration = true;
114                         return XmlWriter.Create (sw, s);
115                 }
116
117                 [Test]
118                 public void GetReaderAtContent ()
119                 {
120                         var x = new SyndicationElementExtension (6);
121                         // premise.
122                         Assert.AreEqual ("<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int>", x.GetReader ().ReadOuterXml (), "#1");
123
124                         var t = new XmlSyndicationContent ("text/xml", 6, (XmlObjectSerializer) null);
125                         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");
126                 }
127                 
128                 [Test]
129                 public void GetReaderAtContent2 ()
130                 {
131                         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>";
132                         var ms = new MemoryStream ();
133                         using (var xw = XmlWriter.Create (ms))
134                                 new XmlSyndicationContent (XmlReader.Create (new StringReader (inxml))).WriteTo (xw, "contentsss", "urn:x");
135                         ms.Position = 0;
136                         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 ('\'', '"');
137                         Assert.AreEqual (expected, new StreamReader (ms).ReadToEnd (), "#1");
138                 }
139         }
140 }
141 #endif