1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 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
|