2 // MonoTests.System.Xml.Serialization.XmlSerializationWriterTests
4 // Author: Erik LeBel <eriklebel@yahoo.ca>
7 // Copyright 2003-2011 Novell
8 // Copyright 2011 Xamarin Inc
10 // FIXME add tests for callbacks
11 // FIXME add tests for writes that generate namespaces
12 // FIXME add test that write XmlNode objects
16 using System.Globalization;
19 using System.Xml.Serialization;
21 using NUnit.Framework;
23 using MonoTests.System.Xml.TestClasses;
25 namespace MonoTests.System.XmlSerialization
27 // base, common implementation of XmlSerializationWriter test harness.
28 // the reason for this is that all auto generated namespace prefixes
29 // of the form q# are modified by any Write* that defines a new namespace.
30 // The result of this is that even though we redefine the string results
31 // to exclude previous tests, the q#s will change depending on number of
32 // namespace declarations were made prior to the perticual test. This
33 // means that if the [Test] methods are called out of sequence, they
34 // all start to fail. For this reason, tests that define and verify
35 // temporary namespaces should be stored in a seperate class which protects
36 // itself from accidental pre-definitions.
37 public class XmlSerializarionWriterTester : XmlSerializationWriter
39 // appease the compiler
40 protected override void InitCallbacks ()
50 sw = new StringWriter ();
51 writer = new XmlTextWriter (sw);
52 writer.QuoteChar = '\'';
53 writer.Formatting = Formatting.None;
61 string val = sw.GetStringBuilder().ToString();
66 public void ExecuteWritePotentiallyReferencingElement (string name, string ns, object o, Type ambientType, bool suppressReference, bool isNullable)
68 WritePotentiallyReferencingElement (name, ns, o, ambientType, suppressReference, isNullable);
71 public void ExecuteWriteTypedPrimitive (string name, string ns, object o, bool xsiType)
73 WriteTypedPrimitive (name, ns, o, xsiType);
77 // this class tests the methods of the XmlSerializationWriter that
78 // can be executed out of order.
80 public class XmlSerializationWriterSimpleTests : XmlSerializarionWriterTester
82 const string XmlSchemaNamespace = "http://www.w3.org/2001/XMLSchema";
83 const string XmlSchemaInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
84 const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
85 const string WsdlTypesNamespace = "http://microsoft.com/wsdl/types/";
86 const string ANamespace = "some:urn";
87 const string AnotherNamespace = "another:urn";
89 // These TestFrom* methods indirectly test the functionality of XmlCustomFormatter
92 public void TestFromByteArrayBase64()
95 // This should work according to Mono's API, but .NET's FromByteArrayBase64
96 // returns a byte array.
98 //string val = this.FromByteArrayBase64(new byte [] {143, 144, 1, 0});
99 //Assert.AreEqual (FromByteArrayBase64(null), "");
101 //val = FromByteArrayBase64(null);
102 //try/catch or AssertEruals?
106 public void TestFromByteArrayHex()
108 byte [] vals = {143, 144, 1, 0};
109 Assert.AreEqual ("8F900100", FromByteArrayHex(vals));
110 Assert.IsNull (FromByteArrayHex (null));
114 public void TestFromChar()
116 Assert.AreEqual ("97", FromChar ('a'));
117 Assert.AreEqual ("0", FromChar ('\0'));
118 Assert.AreEqual ("10", FromChar ('\n'));
119 Assert.AreEqual ("65281", FromChar ('\uFF01'));
123 public void TestFromDate()
125 DateTime d = new DateTime();
126 Assert.AreEqual ("0001-01-01", FromDate (d));
130 public void TestFromDateTime()
132 DateTime d = new DateTime();
133 Assert.AreEqual ("0001-01-01T00:00:00", FromDateTime (d));
137 public void TestFromEnum()
139 long[] ids = {1, 2, 3, 4};
140 string[] values = {"one", "two", "three"};
142 Assert.AreEqual ("one", FromEnum (1, values, ids), "#1");
143 Assert.AreEqual (string.Empty, FromEnum (0, values, ids), "#2");
144 Assert.AreEqual ("one two", FromEnum (3, values, ids), "#3");
147 string dummy = FromEnum(4, values, ids);
149 } catch (IndexOutOfRangeException) {
152 string[] correctValues = {"one", "two", "three", "four"};
153 Assert.AreEqual ("four", FromEnum (4, correctValues, ids), "#5");
154 Assert.AreEqual ("one four", FromEnum (5, correctValues, ids), "#6");
155 Assert.AreEqual ("two four", FromEnum (6, correctValues, ids), "#7");
156 Assert.AreEqual ("one two three four", FromEnum (7, correctValues, ids), "#8");
158 string[] flagValues = {"one", "two", "four", "eight"};
159 long[] flagIDs = {1, 2, 4, 8};
160 Assert.AreEqual (string.Empty, FromEnum (0, flagValues, flagIDs), "#9");
161 Assert.AreEqual ("two", FromEnum (2, flagValues, flagIDs), "#10");
162 Assert.AreEqual ("four", FromEnum (4, flagValues, flagIDs), "#1");
163 Assert.AreEqual ("one four", FromEnum (5, flagValues, flagIDs), "#12");
164 Assert.AreEqual ("two four", FromEnum (6, flagValues, flagIDs), "#13");
165 Assert.AreEqual ("one two four", FromEnum (7, flagValues, flagIDs), "#14");
166 Assert.AreEqual ("eight", FromEnum (8, flagValues, flagIDs), "#15");
167 Assert.AreEqual ("one four eight", FromEnum (13, flagValues, flagIDs), "#16");
169 string[] unorderedValues = {"one", "four", "two", "zero"};
170 long[] unorderedIDs = {1, 4, 2, 0};
172 Assert.AreEqual (string.Empty, FromEnum (0, unorderedValues, unorderedIDs), "#17");
173 Assert.AreEqual ("two", FromEnum (2, unorderedValues, unorderedIDs), "#18");
174 Assert.AreEqual ("four", FromEnum (4, unorderedValues, unorderedIDs), "#19");
175 Assert.AreEqual ("one four", FromEnum (5, unorderedValues, unorderedIDs), "#20");
176 Assert.AreEqual ("four two", FromEnum (6, unorderedValues, unorderedIDs), "#21");
177 Assert.AreEqual ("one four two", FromEnum (7, unorderedValues, unorderedIDs), "#22");
179 string[] zeroValues = {"zero", "ten"};
180 long[] zeroIDs = {0, 10};
182 Assert.AreEqual ("zero", FromEnum (0, zeroValues, zeroIDs), "#9");
183 Assert.AreEqual ("ten", FromEnum (10, zeroValues, zeroIDs), "#9");
185 string[] reverseZeroValues = {"", "zero"};
186 long[] reverseZeroIDs = {4, 0};
187 Assert.AreEqual (string.Empty, FromEnum (0, reverseZeroValues, reverseZeroIDs), "#9");
188 Assert.AreEqual ("zero", FromEnum (4, reverseZeroValues, reverseZeroIDs), "#9");
190 string[] emptyValues = { "zero" };
191 long[] emptyIDs = {0};
192 Assert.AreEqual ("zero", FromEnum (0, emptyValues, emptyIDs), "#9");
196 public void TestFromEnum_InvalidValue ()
198 long[] ids = {1, 2, 3, 4};
199 string[] values = {"one", "two", "three", "four"};
202 FromEnum (8, values, ids);
204 } catch (InvalidOperationException ex) {
205 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
206 Assert.IsNotNull (ex.Message, "#A3");
207 Assert.IsTrue (ex.Message.IndexOf ("'8'") != -1, "#A4");
208 Assert.IsNull (ex.InnerException, "#A5");
212 FromEnum (8, values, ids, "Some.Type.Name");
214 } catch (InvalidOperationException ex) {
215 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
216 Assert.IsNotNull (ex.Message, "#B3");
217 Assert.IsTrue (ex.Message.IndexOf ("'8'") != -1, "#B4");
218 Assert.IsTrue (ex.Message.IndexOf ("Some.Type.Name") != -1, "#B5");
219 Assert.IsNull (ex.InnerException, "#B6");
224 [ExpectedException (typeof (NullReferenceException))]
225 public void TestFromEnum_Null_Values ()
227 long[] ids = { 1, 2, 3, 4 };
228 string[] values = { "one", "two", "three", "four" };
230 FromEnum (1, (string[]) null, ids);
234 [ExpectedException (typeof (NullReferenceException))]
235 public void TestFromEnum_Null_IDs ()
237 string[] values = { "one", "two", "three", "four" };
239 FromEnum (1, values, (long[]) null);
243 public void TestFromTime()
245 DateTime d = new DateTime();
246 // Don't include time zone.
247 Assert.AreEqual ("00:00:00.0000000", FromTime (d).Substring (0, 16));
251 public void TestFromXmlName()
253 Assert.AreEqual ("Hello", FromXmlName ("Hello"));
254 Assert.AreEqual ("go_x0020_dogs_x0020_go", FromXmlName ("go dogs go"));
255 Assert.AreEqual ("what_x0027_s_x0020_up", FromXmlName ("what's up"));
256 Assert.AreEqual ("_x0031_23go", FromXmlName ("123go"));
257 Assert.AreEqual ("Hello_x0020_what_x0027_s.up", FromXmlName ("Hello what's.up"));
261 public void TestFromXmlNCName()
263 Assert.AreEqual ("Hello", FromXmlNCName ("Hello"));
264 Assert.AreEqual ("go_x0020_dogs_x0020_go", FromXmlNCName ("go dogs go"));
265 Assert.AreEqual ("what_x0027_s_x0020_up", FromXmlNCName ("what's up"));
266 Assert.AreEqual ("_x0031_23go", FromXmlNCName ("123go"));
267 Assert.AreEqual ("Hello_x0020_what_x0027_s.up", FromXmlNCName ("Hello what's.up"));
271 public void TestFromXmlNmToken()
273 Assert.AreEqual ("Hello", FromXmlNmToken ("Hello"));
274 Assert.AreEqual ("go_x0020_dogs_x0020_go", FromXmlNmToken ("go dogs go"));
275 Assert.AreEqual ("what_x0027_s_x0020_up", FromXmlNmToken ("what's up"));
276 Assert.AreEqual ("123go", FromXmlNmToken ("123go"));
277 Assert.AreEqual ("Hello_x0020_what_x0027_s.up", FromXmlNmToken ("Hello what's.up"));
281 public void TestFromXmlNmTokens()
283 Assert.AreEqual ("Hello go dogs_go 123go what_x0027_s.up", FromXmlNmTokens ("Hello go dogs_go 123go what's.up"));
287 public void TestWriteAttribute()
289 WriteStartElement("x");
290 WriteAttribute("a", "b");
292 Assert.AreEqual ("<x a='b' />", Content);
295 WriteStartElement("x");
296 WriteAttribute("a", new byte[] {1, 2, 3});
298 Assert.AreEqual ("<x a='AQID' />", Content);
301 WriteStartElement("x");
302 WriteAttribute("a", "<b");
304 Assert.AreEqual ("<x a='<b' />", Content);
307 WriteStartElement("x");
308 string typedPlaceholder = null;
309 WriteAttribute("a", typedPlaceholder);
311 Assert.AreEqual ("<x />", Content);
314 WriteStartElement("x");
315 WriteAttribute("a", "\"");
317 Assert.AreEqual ("<x a='\"' />", Content);
320 WriteStartElement("x");
321 WriteAttribute("a", "b\nc");
323 Assert.AreEqual ("<x a='b
c' />", Content);
326 WriteStartElement("x");
327 WriteAttribute("a", ANamespace, "b");
329 Assert.AreEqual ("<x d1p1:a='b' xmlns:d1p1='some:urn' />", Content);
333 public void TestWriteElementEncoded()
340 public void TestWriteElementLiteral()
347 public void TestWriteElementString()
349 WriteElementString("x", "a");
350 Assert.AreEqual ("<x>a</x>", Content);
353 WriteElementString("x", "<a");
354 Assert.AreEqual ("<x><a</x>", Content);
358 public void TestWriteElementStringRaw()
360 byte [] placeHolderArray = null;
361 WriteElementStringRaw("x", placeHolderArray);
362 Assert.AreEqual ("", Content);
365 WriteElementStringRaw("x", new byte[] {0, 2, 4});
366 Assert.AreEqual ("<x>AAIE</x>", Content);
369 WriteElementStringRaw("x", new byte[] {});
370 Assert.AreEqual ("<x />", Content);
372 // Note to reader, the output is not valid xml
374 WriteElementStringRaw("x", "a > 13 && a < 19");
375 Assert.AreEqual ("<x>a > 13 && a < 19</x>", Content);
379 public void TestWriteEmptyTag()
382 Assert.AreEqual ("<x />", Content);
386 public void TestWriteNamespaceDeclarations()
388 XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
390 WriteStartElement("x");
391 WriteNamespaceDeclarations(ns);
393 Assert.AreEqual ("<x />", Content);
396 ns.Add("mypref", ANamespace);
397 WriteStartElement("x");
398 WriteNamespaceDeclarations(ns);
400 Assert.AreEqual (XmlSerializerTests.Infoset("<x xmlns:mypref='some:urn' />"), XmlSerializerTests.Infoset(Content));
403 ns.Add("ns2", "another:urn");
404 WriteStartElement("x");
405 WriteNamespaceDeclarations(ns);
407 Assert.AreEqual (XmlSerializerTests.Infoset("<x xmlns:ns2='another:urn' xmlns:mypref='some:urn' />"), XmlSerializerTests.Infoset(Content));
410 ns.Add("ns3", "ya:urn");
411 WriteStartElement("x");
412 WriteNamespaceDeclarations(ns);
414 Assert.AreEqual (XmlSerializerTests.Infoset("<x xmlns:ns3='ya:urn' xmlns:ns2='another:urn' xmlns:mypref='some:urn' />"), XmlSerializerTests.Infoset(Content));
418 public void TestWriteNullableStringLiteral()
420 WriteNullableStringLiteral("x", null, null);
421 Assert.AreEqual (XmlSerializerTests.Infoset("<x d1p1:nil='true' xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' />"), XmlSerializerTests.Infoset(Content));
424 WriteNullableStringLiteral("x", null, "");
425 Assert.AreEqual ("<x />", Content);
428 WriteNullableStringLiteral("x", null, "a<b\'c");
429 Assert.AreEqual ("<x>a<b\'c</x>", Content);
432 WriteNullableStringLiteral("x", ANamespace, "b");
433 Assert.AreEqual ("<x xmlns='some:urn'>b</x>", Content);
437 public void TestWriteNullableStringLiteralRaw()
439 WriteNullableStringLiteralRaw("x", null, new byte[] {1, 2, 244});
440 Assert.AreEqual ("<x>AQL0</x>", Content);
444 public void TestWriteNullTagEncoded()
446 WriteNullTagEncoded("x");
447 Assert.AreEqual (XmlSerializerTests.Infoset("<x d1p1:nil='true' xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' />"), XmlSerializerTests.Infoset(Content));
451 public void TestWriteNullTagLiteral()
453 WriteNullTagLiteral("x");
454 Assert.AreEqual (XmlSerializerTests.Infoset("<x d1p1:nil='true' xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' />"), XmlSerializerTests.Infoset(Content));
458 [Category ("MobileNotWorking")]
459 public void TestWritePotentiallyReferencingElement ()
461 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
464 xsw.ExecuteWritePotentiallyReferencingElement ("x", ANamespace, EnumDefaultValue.e1, typeof (EnumDefaultValue), true, false);
465 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
466 "<x xmlns='{0}'>1</x>", ANamespace), xsw.Content, "#1");
470 xsw.ExecuteWritePotentiallyReferencingElement ("x", ANamespace, (int) 1, typeof (EnumDefaultValue), true, false);
471 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
472 "<x xmlns:q1='{0}' d1p1:type='q1:int' xmlns:d1p1='{1}' xmlns='{2}'>1</x>",
473 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
478 xsw.ExecuteWritePotentiallyReferencingElement ("x", ANamespace, "something", typeof (string), true, false);
479 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
480 "<x xmlns='{0}'>something</x>", ANamespace), xsw.Content, "#3");
484 xsw.ExecuteWritePotentiallyReferencingElement ("x", ANamespace, "something", null, true, false);
485 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
486 "<x xmlns:q2='{0}' d1p1:type='q2:string' xmlns:d1p1='{1}' xmlns='{2}'>something</x>",
487 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
492 xsw.ExecuteWritePotentiallyReferencingElement ("x", ANamespace, new string[] { "A", "B" }, typeof (string[]), true, false);
493 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
494 "<q3:Array id='id1' xmlns:q4='{0}' q3:arrayType='q4:string[2]' xmlns:q3='{1}'>" +
497 "</q3:Array>", XmlSchemaNamespace, SoapEncodingNamespace), xsw.Content, "#5");
501 public void TestWriteStartDocument()
503 Assert.AreEqual ("", Content);
505 WriteStartDocument();
506 Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?>", Content);
510 public void TestWriteStartElement()
512 WriteStartElement("x");
514 Assert.AreEqual ("<x />", Content);
517 WriteStartElement("x");
520 Assert.AreEqual ("<x>a</x>", Content);
523 WriteStartElement("x");
524 WriteStartElement("y", "z");
527 Assert.AreEqual ("<x><y xmlns='z' /></x>", Content);
530 WriteStartElement("x");
531 WriteStartElement("y", "z", true);
534 Assert.AreEqual ("<x><q1:y xmlns:q1='z' /></x>", Content);
538 public void TestWriteTypedPrimitive_Base64Binary ()
540 byte[] byteArray = new byte[] { 255, 20, 10, 5, 0, 7 };
541 string expected = "/xQKBQAH";
543 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
546 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, byteArray, false);
547 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
548 "<x xmlns='{0}'>{1}</x>", ANamespace, expected),
553 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, byteArray, false);
554 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
555 "<x>{0}</x>", expected), xsw.Content, "#2");
559 xsw.ExecuteWriteTypedPrimitive ("x", null, byteArray, false);
560 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
561 "<x>{0}</x>", expected), xsw.Content, "#3");
565 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, byteArray, false);
566 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
567 "<x xmlns='{0}'>{1}</x>", XmlSchemaNamespace, expected),
572 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, byteArray, false);
573 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
574 "<x xmlns='{0}'>{1}</x>", XmlSchemaInstanceNamespace, expected),
579 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, byteArray, false);
580 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
581 "< xmlns='{0}'>{1}</>", ANamespace, expected), xsw.Content, "#6");
585 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, byteArray, false);
586 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
587 "<base64Binary xmlns='{0}'>{1}</base64Binary>",
588 XmlSchemaNamespace, expected), xsw.Content, "#7");
592 public void TestWriteTypedPrimitive_Base64Binary_XsiType ()
594 byte[] byteArray = new byte[] { 255, 20, 10, 5, 0, 7 };
595 string expected = "/xQKBQAH";
597 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
600 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, byteArray, true);
601 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
602 "<x xmlns:q1='{0}' d1p1:type='q1:base64Binary' xmlns:d1p1='{1}'>{2}</x>",
603 XmlSchemaNamespace, XmlSchemaInstanceNamespace, expected),
608 xsw.ExecuteWriteTypedPrimitive ("x", null, byteArray, true);
609 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
610 "<x xmlns:q2='{0}' d1p1:type='q2:base64Binary' xmlns:d1p1='{1}'>{2}</x>",
611 XmlSchemaNamespace, XmlSchemaInstanceNamespace, expected),
616 public void TestWriteTypedPrimitive_Boolean ()
618 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
621 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, true, false);
622 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
623 "<x xmlns='{0}'>true</x>", ANamespace), xsw.Content, "#1");
627 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, false, false);
628 Assert.AreEqual ("<x>false</x>", xsw.Content, "#2");
632 xsw.ExecuteWriteTypedPrimitive ("x", null, true, false);
633 Assert.AreEqual ("<x>true</x>", xsw.Content, "#3");
637 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, false, false);
638 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
639 "<x xmlns='{0}'>false</x>", XmlSchemaNamespace), xsw.Content, "#4");
643 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, true, false);
644 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
645 "<x xmlns='{0}'>true</x>", XmlSchemaInstanceNamespace),
650 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, false, false);
651 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
652 "< xmlns='{0}'>false</>", ANamespace), xsw.Content, "#6");
656 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, true, false);
657 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
658 "<boolean xmlns='{0}'>true</boolean>", XmlSchemaNamespace),
663 public void TestWriteTypedPrimitive_Boolean_XsiType ()
665 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
668 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, true, true);
669 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
670 "<x xmlns:q1='{0}' d1p1:type='q1:boolean' xmlns:d1p1='{1}'>true</x>",
671 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
676 xsw.ExecuteWriteTypedPrimitive ("x", null, false, true);
677 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
678 "<x xmlns:q2='{0}' d1p1:type='q2:boolean' xmlns:d1p1='{1}'>false</x>",
679 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
684 public void TestWriteTypedPrimitive_Char ()
686 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
689 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, 'c', false);
690 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
691 "<x xmlns='{0}'>99</x>", ANamespace), xsw.Content, "#1");
695 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, 'a', false);
696 Assert.AreEqual ("<x>97</x>", xsw.Content, "#2");
700 xsw.ExecuteWriteTypedPrimitive ("x", null, 'b', false);
701 Assert.AreEqual ("<x>98</x>", xsw.Content, "#3");
705 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, 'd', false);
706 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
707 "<x xmlns='{0}'>100</x>", XmlSchemaNamespace), xsw.Content, "#4");
711 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, 'e', false);
712 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
713 "<x xmlns='{0}'>101</x>", XmlSchemaInstanceNamespace),
718 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, ' ', false);
719 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
720 "< xmlns='{0}'>32</>", ANamespace), xsw.Content, "#6");
724 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, '0', false);
725 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
726 "<char xmlns='{0}'>48</char>", WsdlTypesNamespace),
731 public void TestWriteTypedPrimitive_Char_XsiType ()
733 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
736 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, 'c', true);
737 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
738 "<x xmlns:q1='{0}' d1p1:type='q1:char' xmlns:d1p1='{1}'>99</x>",
739 WsdlTypesNamespace, XmlSchemaInstanceNamespace),
744 xsw.ExecuteWriteTypedPrimitive ("x", null, 'a', true);
745 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
746 "<x xmlns:q2='{0}' d1p1:type='q2:char' xmlns:d1p1='{1}'>97</x>",
747 WsdlTypesNamespace, XmlSchemaInstanceNamespace),
752 public void TestWriteTypedPrimitive_DateTime ()
754 DateTime dateTime = new DateTime (1973, 08, 13);
756 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
759 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, dateTime, false);
760 // FIXME: This is a bad test case. The following switch
761 // should be applied to the entire test.
762 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
763 "<x xmlns='{0}'>1973-08-13T00:00:00</x>", ANamespace),
767 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, dateTime, false);
768 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
769 "<x>{0}</x>", FromDateTime (dateTime)), xsw.Content, "#2");
773 xsw.ExecuteWriteTypedPrimitive ("x", null, dateTime, false);
774 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
775 "<x>{0}</x>", FromDateTime (dateTime)), xsw.Content, "#3");
779 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, dateTime, false);
780 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
781 "<x xmlns='{0}'>{1}</x>", XmlSchemaNamespace,
782 FromDateTime (dateTime)), xsw.Content, "#4");
786 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, dateTime, false);
787 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
788 "<x xmlns='{0}'>{1}</x>", XmlSchemaInstanceNamespace,
789 FromDateTime (dateTime)), xsw.Content, "#5");
793 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, dateTime, false);
794 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
795 "< xmlns='{0}'>{1}</>", ANamespace, FromDateTime (dateTime)),
800 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, dateTime, false);
801 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
802 "<dateTime xmlns='{0}'>{1}</dateTime>", XmlSchemaNamespace,
803 FromDateTime (dateTime)), xsw.Content, "#7");
806 // FIXME: This is a bad test case.
807 // See TestWriteTypedPrimitive_DateTime.
809 public void TestWriteTypedPrimitive_DateTime_XsiType ()
811 DateTime dateTime = new DateTime (1973, 08, 13);
813 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
816 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, dateTime, true);
817 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
818 "<x xmlns:q1='{0}' d1p1:type='q1:dateTime' xmlns:d1p1='{1}'>{2}</x>",
819 XmlSchemaNamespace, XmlSchemaInstanceNamespace,
820 FromDateTime (dateTime)), xsw.Content, "#1");
824 xsw.ExecuteWriteTypedPrimitive ("x", null, dateTime, true);
825 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
826 "<x xmlns:q2='{0}' d1p1:type='q2:dateTime' xmlns:d1p1='{1}'>{2}</x>",
827 XmlSchemaNamespace, XmlSchemaInstanceNamespace,
828 FromDateTime (dateTime)), xsw.Content, "#2");
832 public void TestWriteTypedPrimitive_Enum ()
834 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
837 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, EnumDefaultValue.e1, false);
838 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
839 "<x xmlns='{0}'>1</x>", ANamespace), xsw.Content, "#1");
843 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, EnumDefaultValue.e2, false);
844 Assert.AreEqual ("<x>2</x>", xsw.Content, "#2");
848 xsw.ExecuteWriteTypedPrimitive ("x", null, EnumDefaultValue.e3, false);
849 Assert.AreEqual ("<x>3</x>", xsw.Content, "#3");
853 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, EnumDefaultValue.e1, false);
854 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
855 "<x xmlns='{0}'>1</x>", XmlSchemaNamespace), xsw.Content, "#4");
859 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, EnumDefaultValue.e2, false);
860 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
861 "<x xmlns='{0}'>2</x>", XmlSchemaInstanceNamespace),
866 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, EnumDefaultValue.e3, false);
867 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
868 "< xmlns='{0}'>3</>", ANamespace), xsw.Content, "#6");
872 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, EnumDefaultValue.e2, false);
873 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
874 "<int xmlns='{0}'>2</int>", XmlSchemaNamespace),
879 public void TestWriteTypedPrimitive_Enum_XsiType ()
881 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
884 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, EnumDefaultValue.e1, true);
885 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
886 "<x xmlns:q1='{0}' d1p1:type='q1:int' xmlns:d1p1='{1}'>1</x>",
887 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
892 xsw.ExecuteWriteTypedPrimitive ("x", null, EnumDefaultValue.e2, true);
893 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
894 "<x xmlns:q2='{0}' d1p1:type='q2:int' xmlns:d1p1='{1}'>2</x>",
895 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
900 public void TestWriteTypedPrimitive_Guid ()
902 Guid guid = new Guid ("CA761232-ED42-11CE-BACD-00AA0057B223");
903 string expectedGuid = "ca761232-ed42-11ce-bacd-00aa0057b223";
905 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
908 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, guid, false);
909 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
910 "<x xmlns='{0}'>{1}</x>", ANamespace, expectedGuid),
915 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, guid, false);
916 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
917 "<x>{0}</x>", expectedGuid), xsw.Content, "#2");
921 xsw.ExecuteWriteTypedPrimitive ("x", null, guid, false);
922 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
923 "<x>{0}</x>", expectedGuid), xsw.Content, "#3");
927 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, guid, false);
928 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
929 "<x xmlns='{0}'>{1}</x>", XmlSchemaNamespace, expectedGuid),
934 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, guid, false);
935 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
936 "<x xmlns='{0}'>{1}</x>", XmlSchemaInstanceNamespace, expectedGuid),
941 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, guid, false);
942 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
943 "< xmlns='{0}'>{1}</>", ANamespace, expectedGuid),
948 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, guid, false);
949 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
950 "<guid xmlns='{0}'>{1}</guid>", WsdlTypesNamespace,
951 expectedGuid), xsw.Content, "#7");
955 public void TestWriteTypedPrimitive_Guid_XsiType ()
957 Guid guid = new Guid ("CA761232-ED42-11CE-BACD-00AA0057B223");
958 string expectedGuid = "ca761232-ed42-11ce-bacd-00aa0057b223";
960 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
963 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, guid, true);
964 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
965 "<x xmlns:q1='{0}' d1p1:type='q1:guid' xmlns:d1p1='{1}'>{2}</x>",
966 WsdlTypesNamespace, XmlSchemaInstanceNamespace, expectedGuid),
971 xsw.ExecuteWriteTypedPrimitive ("x", null, guid, true);
972 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
973 "<x xmlns:q2='{0}' d1p1:type='q2:guid' xmlns:d1p1='{1}'>{2}</x>",
974 WsdlTypesNamespace, XmlSchemaInstanceNamespace, expectedGuid),
979 public void TestWriteTypedPrimitive_Int ()
981 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
984 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, 76665, false);
985 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
986 "<x xmlns='{0}'>76665</x>", ANamespace), xsw.Content, "#1");
990 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, -5656, false);
991 Assert.AreEqual ("<x>-5656</x>", xsw.Content, "#2");
995 xsw.ExecuteWriteTypedPrimitive ("x", null, 0, false);
996 Assert.AreEqual ("<x>0</x>", xsw.Content, "#3");
1000 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, 534, false);
1001 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1002 "<x xmlns='{0}'>534</x>", XmlSchemaNamespace), xsw.Content, "#4");
1006 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, -6756, false);
1007 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1008 "<x xmlns='{0}'>-6756</x>", XmlSchemaInstanceNamespace),
1013 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, 434, false);
1014 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1015 "< xmlns='{0}'>434</>", ANamespace), xsw.Content, "#6");
1019 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, 434, false);
1020 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1021 "<int xmlns='{0}'>434</int>", XmlSchemaNamespace),
1026 public void TestWriteTypedPrimitive_Int_XsiType ()
1028 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1031 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, -6756, true);
1032 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1033 "<x xmlns:q1='{0}' d1p1:type='q1:int' xmlns:d1p1='{1}'>-6756</x>",
1034 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1039 xsw.ExecuteWriteTypedPrimitive ("x", null, 434, true);
1040 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1041 "<x xmlns:q2='{0}' d1p1:type='q2:int' xmlns:d1p1='{1}'>434</x>",
1042 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1047 public void TestWriteTypedPrimitive_String ()
1049 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1052 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, "hello", false);
1053 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1054 "<x xmlns='{0}'>hello</x>", ANamespace), xsw.Content, "#1");
1058 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, "hello", false);
1059 Assert.AreEqual ("<x>hello</x>", xsw.Content, "#2");
1063 xsw.ExecuteWriteTypedPrimitive ("x", null, "hello", false);
1064 Assert.AreEqual ("<x>hello</x>", xsw.Content, "#3");
1068 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, "hello", false);
1069 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1070 "<x xmlns='{0}'>hello</x>", XmlSchemaNamespace),
1075 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, "hello", false);
1076 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1077 "<x xmlns='{0}'>hello</x>", XmlSchemaInstanceNamespace),
1082 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, string.Empty, false);
1083 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1084 "<x xmlns='{0}' />", ANamespace), xsw.Content, "#6");
1088 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, "<\"te'st\">", false);
1089 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1090 "< xmlns='{0}'><\"te'st\"></>", ANamespace),
1095 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, "hello", false);
1096 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1097 "<string xmlns='{0}'>hello</string>", XmlSchemaNamespace),
1102 public void TestWriteTypedPrimitive_String_XsiType ()
1104 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1107 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, "hello", true);
1108 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1109 "<x xmlns:q1='{0}' d1p1:type='q1:string' xmlns:d1p1='{1}'>hello</x>",
1110 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1115 xsw.ExecuteWriteTypedPrimitive ("x", null, "hello", true);
1116 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1117 "<x xmlns:q2='{0}' d1p1:type='q2:string' xmlns:d1p1='{1}'>hello</x>",
1118 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1123 public void TestWriteTypedPrimitive_String_XsiType_Namespace ()
1125 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1128 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, "hello", true);
1129 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1130 "<x xmlns:q1='{0}' d1p1:type='q1:string' xmlns:d1p1='{1}' xmlns='{2}'>hello</x>",
1131 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1136 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, "hello", true);
1137 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1138 "<x d1p1:type='string' xmlns:d1p1='{0}' xmlns='{1}'>hello</x>",
1139 XmlSchemaInstanceNamespace, XmlSchemaNamespace),
1144 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, "hello", true);
1145 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1146 "<x xmlns:q2='{0}' d1p1:type='q2:string' xmlns:d1p1='{1}' xmlns='{1}'>hello</x>",
1147 XmlSchemaNamespace, XmlSchemaInstanceNamespace), xsw.Content, "#3");
1151 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, string.Empty, true);
1152 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1153 "<x xmlns:q3='{0}' d1p1:type='q3:string' xmlns:d1p1='{1}' xmlns='{2}' />",
1154 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1159 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, "<\"te'st\">", true);
1160 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1161 "< xmlns:q4='{0}' d1p1:type='q4:string' xmlns:d1p1='{1}' xmlns='{2}'><\"te'st\"></>",
1162 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1167 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, "hello", true);
1168 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1169 "<string d1p1:type='string' xmlns:d1p1='{0}' xmlns='{1}'>hello</string>",
1170 XmlSchemaInstanceNamespace, XmlSchemaNamespace),
1175 public void TestWriteTypedPrimitive_UnsignedByte ()
1177 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1180 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, (byte) 5, false);
1181 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1182 "<x xmlns='{0}'>5</x>", ANamespace), xsw.Content, "#1");
1186 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, (byte) 125, false);
1187 Assert.AreEqual ("<x>125</x>", xsw.Content, "#2");
1191 xsw.ExecuteWriteTypedPrimitive ("x", null, (byte) 0, false);
1192 Assert.AreEqual ("<x>0</x>", xsw.Content, "#3");
1196 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, (byte) 255, false);
1197 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1198 "<x xmlns='{0}'>255</x>", XmlSchemaNamespace), xsw.Content, "#4");
1202 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, (byte) 128, false);
1203 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1204 "<x xmlns='{0}'>128</x>", XmlSchemaInstanceNamespace),
1209 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, (byte) 1, false);
1210 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1211 "< xmlns='{0}'>1</>", ANamespace), xsw.Content, "#6");
1215 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, (byte) 99, false);
1216 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1217 "<unsignedByte xmlns='{0}'>99</unsignedByte>",
1218 XmlSchemaNamespace), xsw.Content, "#7");
1222 public void TestWriteTypedPrimitive_UnsignedByte_XsiType ()
1224 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1227 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, (byte) 5, true);
1228 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1229 "<x xmlns:q1='{0}' d1p1:type='q1:unsignedByte' xmlns:d1p1='{1}'>5</x>",
1230 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1235 xsw.ExecuteWriteTypedPrimitive ("x", null, (byte) 99, true);
1236 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1237 "<x xmlns:q2='{0}' d1p1:type='q2:unsignedByte' xmlns:d1p1='{1}'>99</x>",
1238 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1243 public void TestWriteTypedPrimitive_XmlQualifiedName ()
1245 XmlQualifiedName qname = new XmlQualifiedName ("something", AnotherNamespace);
1247 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1250 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, qname, false);
1251 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1252 "<x xmlns:q1='{0}' xmlns='{1}'>q1:something</x>",
1253 AnotherNamespace, ANamespace), xsw.Content, "#A1");
1257 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, qname, false);
1258 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1259 "<x xmlns:q2='{0}'>q2:something</x>",
1260 AnotherNamespace), xsw.Content, "#A2");
1264 xsw.ExecuteWriteTypedPrimitive ("x", null, qname, false);
1265 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1266 "<x xmlns:q3='{0}'>q3:something</x>", AnotherNamespace),
1267 xsw.Content, "#A3");
1271 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, qname, false);
1272 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1273 "<x xmlns:q4='{0}' xmlns='{1}'>q4:something</x>", AnotherNamespace,
1274 XmlSchemaNamespace), xsw.Content, "#A4");
1278 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, qname, false);
1279 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1280 "<x xmlns:q5='{0}' xmlns='{1}'>q5:something</x>", AnotherNamespace,
1281 XmlSchemaInstanceNamespace), xsw.Content, "#A5");
1285 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, qname, false);
1286 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1287 "< xmlns:q6='{0}' xmlns='{1}'>q6:something</>", AnotherNamespace,
1288 ANamespace), xsw.Content, "#A6");
1292 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, qname, false);
1293 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1294 "<QName xmlns:q7='{0}' xmlns='{1}'>q7:something</QName>",
1295 AnotherNamespace, XmlSchemaNamespace), xsw.Content, "#A7");
1299 qname = new XmlQualifiedName ("else");
1301 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, qname, false);
1302 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1303 "<x xmlns='{0}'>else</x>", ANamespace), xsw.Content, "#B1");
1307 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, qname, false);
1308 Assert.AreEqual ("<x>else</x>", xsw.Content, "#B2");
1312 xsw.ExecuteWriteTypedPrimitive ("x", null, qname, false);
1313 Assert.AreEqual ("<x>else</x>", xsw.Content, "#B3");
1317 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, qname, false);
1318 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1319 "<x xmlns='{0}'>else</x>", XmlSchemaNamespace), xsw.Content, "#B4");
1323 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, qname, false);
1324 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1325 "<x xmlns='{0}'>else</x>", XmlSchemaInstanceNamespace),
1326 xsw.Content, "#B5");
1330 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, qname, false);
1331 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1332 "< xmlns='{0}'>else</>", ANamespace), xsw.Content, "#B6");
1336 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, qname, false);
1337 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1338 "<QName xmlns='{0}'>else</QName>", XmlSchemaNamespace),
1339 xsw.Content, "#B7");
1343 [ExpectedException (typeof (NullReferenceException))]
1344 public void TestWriteTypedPrimitive_Null_Value()
1346 WriteTypedPrimitive("x", ANamespace, null, false);
1350 [ExpectedException (typeof (InvalidOperationException))]
1351 public void TestWriteTypedPrimitive_NonPrimitive ()
1353 // The type System.Version was not expected. Use the XmlInclude
1354 // or SoapInclude attribute to specify types that are not known
1356 WriteTypedPrimitive ("x", ANamespace, new Version (), false);
1360 [ExpectedException (typeof (InvalidOperationException))]
1361 public void TestWriteTypedPrimitive_XmlNode ()
1363 WriteTypedPrimitive ("x", ANamespace, new XmlDocument ().CreateElement ("foo"), false);
1367 public void TestWriteValue()
1370 Assert.AreEqual ("", Content);
1373 WriteValue("hello");
1374 Assert.AreEqual ("hello", Content);
1379 Assert.AreEqual ("", Content);
1382 WriteValue(new byte[] {13, 8, 99});
1383 Assert.AreEqual ("DQhj", Content);
1386 public void TestWriteXmlAttribute()
1393 public void TestWriteXsiType()
1395 WriteStartElement("x");
1396 WriteXsiType("pref", null);
1398 Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
1399 "<x d1p1:type='pref' xmlns:d1p1='{0}' />", XmlSchemaInstanceNamespace),
1404 WriteStartElement ("x");
1405 WriteXsiType ("int", XmlSchemaNamespace);
1407 Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
1408 "<x xmlns:q2='{0}' d1p1:type='q2:int' xmlns:d1p1='{1}' />",
1409 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1414 WriteStartElement ("x");
1415 WriteXsiType ("int", ANamespace);
1417 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1418 "<x xmlns:q3='{0}' d1p1:type='q3:int' xmlns:d1p1='{1}' />",
1419 ANamespace, XmlSchemaInstanceNamespace), Content, "#3");
1423 WriteStartElement ("x");
1424 WriteXsiType ("int", XmlSchemaInstanceNamespace);
1426 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1427 "<x xmlns:q4='{0}' q4:type='q4:int' />",
1428 XmlSchemaInstanceNamespace), Content, "#4");
1432 WriteStartElement ("x");
1433 WriteXsiType ("int", string.Empty);
1435 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1436 "<x d1p1:type='int' xmlns:d1p1='{0}' />", XmlSchemaInstanceNamespace),
1441 WriteStartElement ("x");
1442 WriteXsiType (string.Empty, null);
1444 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1445 "<x d1p1:type='' xmlns:d1p1='{0}' />", XmlSchemaInstanceNamespace),
1450 WriteStartElement ("x");
1451 WriteXsiType (null, null);
1453 Assert.AreEqual ("<x />", Content, "#7");
1457 public void TestWriteXsiType_Namespace ()
1459 WriteStartElement ("x", ANamespace);
1460 WriteXsiType ("pref", null);
1462 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1463 "<x d1p1:type='pref' xmlns:d1p1='{0}' xmlns='{1}' />",
1464 XmlSchemaInstanceNamespace, ANamespace), Content, "#1");
1468 WriteStartElement ("x", ANamespace);
1469 WriteXsiType ("int", XmlSchemaNamespace);
1471 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1472 "<x xmlns:q5='{0}' d1p1:type='q5:int' xmlns:d1p1='{1}' xmlns='{2}' />",
1473 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1478 WriteStartElement ("x", ANamespace);
1479 WriteXsiType ("int", ANamespace);
1481 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1482 "<x d1p1:type='int' xmlns:d1p1='{1}' xmlns='{2}' />",
1483 ANamespace, XmlSchemaInstanceNamespace, ANamespace),
1488 WriteStartElement ("x", ANamespace);
1489 WriteXsiType ("int", XmlSchemaInstanceNamespace);
1491 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1492 "<x xmlns:q6='{0}' q6:type='q6:int' xmlns='{1}' />",
1493 XmlSchemaInstanceNamespace, ANamespace), Content, "#4");
1497 WriteStartElement ("x", ANamespace);
1498 WriteXsiType ("int", string.Empty);
1500 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1501 "<x d1p1:type='int' xmlns:d1p1='{0}' xmlns='{1}' />",
1502 XmlSchemaInstanceNamespace, ANamespace), Content, "#5");
1506 WriteStartElement ("x", ANamespace);
1507 WriteXsiType (string.Empty, null);
1509 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1510 "<x d1p1:type='' xmlns:d1p1='{0}' xmlns='{1}' />",
1511 XmlSchemaInstanceNamespace, ANamespace), Content, "#6");
1515 WriteStartElement ("x", ANamespace);
1516 WriteXsiType (null, null);
1518 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1519 "<x xmlns='{0}' />", ANamespace), Content, "#7");
1524 public void TestFromEnum_Null_TypeName ()
1526 string[] values = { "one", "two", "three", "four" };
1527 long[] ids = { 1, 2, 3, 4 };
1529 Assert.AreEqual ("one", FromEnum (1, values, ids, (string) null));
1533 public void TestCreateInvalidEnumValueException ()
1535 Exception ex = CreateInvalidEnumValueException("AnInvalidValue", "SomeType");
1536 Assert.IsNotNull (ex, "#1");
1537 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1538 Assert.IsNotNull (ex.Message, "#3");
1539 Assert.IsTrue (ex.Message.IndexOf ("AnInvalidValue") != -1, "#4");
1540 Assert.IsTrue (ex.Message.IndexOf ("SomeType") != -1, "#5");
1544 public void WriteCharacter ()
1546 // mostly from bug #673019
1547 var SerializerObj = new XmlSerializer (typeof (ToBeSerialized));
1548 StringWriter writer = new StringWriter ();
1549 SerializerObj.Serialize (writer, new ToBeSerialized ());
1550 Assert.IsTrue (writer.ToString ().IndexOf ("<character>39</character>") > 0, "#1");
1554 public class ToBeSerialized
1556 [global::System.ComponentModel.DefaultValue ('a')]
1557 public char character = '\'';
1561 [Category ("MobileNotWorking")]
1562 public void TestNullableDatesAndTimes ()
1564 DateTime dt = new DateTime (2012, 1, 3, 10, 0, 0, 0, DateTimeKind.Utc);
1566 var d = new NullableDatesAndTimes () {
1568 MyTimeNullable = dt,
1573 XmlSerializer ser = new XmlSerializer (d.GetType ());
1574 StringWriter sw = new StringWriter ();
1575 ser.Serialize (sw, d);
1576 string str = sw.ToString ();
1578 str = RemoveTZ (str, "MyTime");
1579 str = RemoveTZ (str, "MyTimeNullable");
1582 "<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
1583 "<root xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + Environment.NewLine +
1584 " <MyTime>10:00:00.0000000$TZ$</MyTime>" + Environment.NewLine +
1585 " <MyTimeNullable>10:00:00.0000000$TZ$</MyTimeNullable>" + Environment.NewLine +
1586 " <MyDate>2012-01-03</MyDate>" + Environment.NewLine +
1587 " <MyDateNullable>2012-01-03</MyDateNullable>" + Environment.NewLine +
1590 Assert.AreEqual (expected, str);
1593 static string RemoveTZ (string str, string tag)
1595 var st = str.IndexOf ("<" + tag + ">");
1596 var et = str.IndexOf ("</" + tag + ">");
1597 if (st < 0 || et < 0)
1600 var start = str.IndexOfAny (new [] { '+', '-' }, st, et - st);
1601 return str.Substring (0, start) + "$TZ$" + str.Substring (et, str.Length - et);