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 [Ignore ("Additional namespace prefixes are added")]
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 TestWriteSerializable()
504 //Assert.AreEqual (, "");
508 public void TestWriteStartDocument()
510 Assert.AreEqual ("", Content);
512 WriteStartDocument();
513 Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?>", Content);
517 public void TestWriteStartElement()
519 WriteStartElement("x");
521 Assert.AreEqual ("<x />", Content);
524 WriteStartElement("x");
527 Assert.AreEqual ("<x>a</x>", Content);
530 WriteStartElement("x");
531 WriteStartElement("y", "z");
534 Assert.AreEqual ("<x><y xmlns='z' /></x>", Content);
537 WriteStartElement("x");
538 WriteStartElement("y", "z", true);
541 Assert.AreEqual ("<x><q1:y xmlns:q1='z' /></x>", Content);
545 public void TestWriteTypedPrimitive_Base64Binary ()
547 byte[] byteArray = new byte[] { 255, 20, 10, 5, 0, 7 };
548 string expected = "/xQKBQAH";
550 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
553 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, byteArray, false);
554 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
555 "<x xmlns='{0}'>{1}</x>", ANamespace, expected),
560 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, byteArray, false);
561 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
562 "<x>{0}</x>", expected), xsw.Content, "#2");
566 xsw.ExecuteWriteTypedPrimitive ("x", null, byteArray, false);
567 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
568 "<x>{0}</x>", expected), xsw.Content, "#3");
572 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, byteArray, false);
573 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
574 "<x xmlns='{0}'>{1}</x>", XmlSchemaNamespace, expected),
579 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, byteArray, false);
580 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
581 "<x xmlns='{0}'>{1}</x>", XmlSchemaInstanceNamespace, expected),
586 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, byteArray, false);
587 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
588 "< xmlns='{0}'>{1}</>", ANamespace, expected), xsw.Content, "#6");
592 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, byteArray, false);
593 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
594 "<base64Binary xmlns='{0}'>{1}</base64Binary>",
595 XmlSchemaNamespace, expected), xsw.Content, "#7");
599 public void TestWriteTypedPrimitive_Base64Binary_XsiType ()
601 byte[] byteArray = new byte[] { 255, 20, 10, 5, 0, 7 };
602 string expected = "/xQKBQAH";
604 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
607 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, byteArray, true);
608 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
609 "<x xmlns:q1='{0}' d1p1:type='q1:base64Binary' xmlns:d1p1='{1}'>{2}</x>",
610 XmlSchemaNamespace, XmlSchemaInstanceNamespace, expected),
615 xsw.ExecuteWriteTypedPrimitive ("x", null, byteArray, true);
616 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
617 "<x xmlns:q2='{0}' d1p1:type='q2:base64Binary' xmlns:d1p1='{1}'>{2}</x>",
618 XmlSchemaNamespace, XmlSchemaInstanceNamespace, expected),
623 public void TestWriteTypedPrimitive_Boolean ()
625 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
628 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, true, false);
629 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
630 "<x xmlns='{0}'>true</x>", ANamespace), xsw.Content, "#1");
634 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, false, false);
635 Assert.AreEqual ("<x>false</x>", xsw.Content, "#2");
639 xsw.ExecuteWriteTypedPrimitive ("x", null, true, false);
640 Assert.AreEqual ("<x>true</x>", xsw.Content, "#3");
644 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, false, false);
645 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
646 "<x xmlns='{0}'>false</x>", XmlSchemaNamespace), xsw.Content, "#4");
650 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, true, false);
651 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
652 "<x xmlns='{0}'>true</x>", XmlSchemaInstanceNamespace),
657 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, false, false);
658 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
659 "< xmlns='{0}'>false</>", ANamespace), xsw.Content, "#6");
663 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, true, false);
664 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
665 "<boolean xmlns='{0}'>true</boolean>", XmlSchemaNamespace),
670 public void TestWriteTypedPrimitive_Boolean_XsiType ()
672 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
675 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, true, true);
676 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
677 "<x xmlns:q1='{0}' d1p1:type='q1:boolean' xmlns:d1p1='{1}'>true</x>",
678 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
683 xsw.ExecuteWriteTypedPrimitive ("x", null, false, true);
684 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
685 "<x xmlns:q2='{0}' d1p1:type='q2:boolean' xmlns:d1p1='{1}'>false</x>",
686 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
691 public void TestWriteTypedPrimitive_Char ()
693 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
696 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, 'c', false);
697 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
698 "<x xmlns='{0}'>99</x>", ANamespace), xsw.Content, "#1");
702 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, 'a', false);
703 Assert.AreEqual ("<x>97</x>", xsw.Content, "#2");
707 xsw.ExecuteWriteTypedPrimitive ("x", null, 'b', false);
708 Assert.AreEqual ("<x>98</x>", xsw.Content, "#3");
712 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, 'd', false);
713 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
714 "<x xmlns='{0}'>100</x>", XmlSchemaNamespace), xsw.Content, "#4");
718 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, 'e', false);
719 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
720 "<x xmlns='{0}'>101</x>", XmlSchemaInstanceNamespace),
725 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, ' ', false);
726 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
727 "< xmlns='{0}'>32</>", ANamespace), xsw.Content, "#6");
731 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, '0', false);
732 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
733 "<char xmlns='{0}'>48</char>", WsdlTypesNamespace),
738 public void TestWriteTypedPrimitive_Char_XsiType ()
740 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
743 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, 'c', true);
744 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
745 "<x xmlns:q1='{0}' d1p1:type='q1:char' xmlns:d1p1='{1}'>99</x>",
746 WsdlTypesNamespace, XmlSchemaInstanceNamespace),
751 xsw.ExecuteWriteTypedPrimitive ("x", null, 'a', true);
752 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
753 "<x xmlns:q2='{0}' d1p1:type='q2:char' xmlns:d1p1='{1}'>97</x>",
754 WsdlTypesNamespace, XmlSchemaInstanceNamespace),
759 public void TestWriteTypedPrimitive_DateTime ()
761 DateTime dateTime = new DateTime (1973, 08, 13);
763 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
766 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, dateTime, false);
767 // FIXME: This is a bad test case. The following switch
768 // should be applied to the entire test.
769 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
770 "<x xmlns='{0}'>1973-08-13T00:00:00</x>", ANamespace),
774 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, dateTime, false);
775 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
776 "<x>{0}</x>", FromDateTime (dateTime)), xsw.Content, "#2");
780 xsw.ExecuteWriteTypedPrimitive ("x", null, dateTime, false);
781 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
782 "<x>{0}</x>", FromDateTime (dateTime)), xsw.Content, "#3");
786 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, dateTime, false);
787 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
788 "<x xmlns='{0}'>{1}</x>", XmlSchemaNamespace,
789 FromDateTime (dateTime)), xsw.Content, "#4");
793 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, dateTime, false);
794 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
795 "<x xmlns='{0}'>{1}</x>", XmlSchemaInstanceNamespace,
796 FromDateTime (dateTime)), xsw.Content, "#5");
800 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, dateTime, false);
801 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
802 "< xmlns='{0}'>{1}</>", ANamespace, FromDateTime (dateTime)),
807 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, dateTime, false);
808 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
809 "<dateTime xmlns='{0}'>{1}</dateTime>", XmlSchemaNamespace,
810 FromDateTime (dateTime)), xsw.Content, "#7");
813 // FIXME: This is a bad test case.
814 // See TestWriteTypedPrimitive_DateTime.
816 public void TestWriteTypedPrimitive_DateTime_XsiType ()
818 DateTime dateTime = new DateTime (1973, 08, 13);
820 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
823 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, dateTime, true);
824 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
825 "<x xmlns:q1='{0}' d1p1:type='q1:dateTime' xmlns:d1p1='{1}'>{2}</x>",
826 XmlSchemaNamespace, XmlSchemaInstanceNamespace,
827 FromDateTime (dateTime)), xsw.Content, "#1");
831 xsw.ExecuteWriteTypedPrimitive ("x", null, dateTime, true);
832 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
833 "<x xmlns:q2='{0}' d1p1:type='q2:dateTime' xmlns:d1p1='{1}'>{2}</x>",
834 XmlSchemaNamespace, XmlSchemaInstanceNamespace,
835 FromDateTime (dateTime)), xsw.Content, "#2");
839 [Category ("NotWorking")] // enum name is output instead of integral value
840 public void TestWriteTypedPrimitive_Enum ()
842 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
845 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, EnumDefaultValue.e1, false);
846 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
847 "<x xmlns='{0}'>1</x>", ANamespace), xsw.Content, "#1");
851 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, EnumDefaultValue.e2, false);
852 Assert.AreEqual ("<x>2</x>", xsw.Content, "#2");
856 xsw.ExecuteWriteTypedPrimitive ("x", null, EnumDefaultValue.e3, false);
857 Assert.AreEqual ("<x>3</x>", xsw.Content, "#3");
861 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, EnumDefaultValue.e1, false);
862 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
863 "<x xmlns='{0}'>1</x>", XmlSchemaNamespace), xsw.Content, "#4");
867 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, EnumDefaultValue.e2, false);
868 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
869 "<x xmlns='{0}'>2</x>", XmlSchemaInstanceNamespace),
874 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, EnumDefaultValue.e3, false);
875 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
876 "< xmlns='{0}'>3</>", ANamespace), xsw.Content, "#6");
880 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, EnumDefaultValue.e2, false);
881 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
882 "<int xmlns='{0}'>2</int>", XmlSchemaNamespace),
887 [Category ("NotWorking")] // InvalidOperationException is thrown
888 public void TestWriteTypedPrimitive_Enum_XsiType ()
890 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
893 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, EnumDefaultValue.e1, true);
894 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
895 "<x xmlns:q1='{0}' d1p1:type='q1:int' xmlns:d1p1='{1}'>1</x>",
896 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
901 xsw.ExecuteWriteTypedPrimitive ("x", null, EnumDefaultValue.e2, true);
902 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
903 "<x xmlns:q2='{0}' d1p1:type='q2:int' xmlns:d1p1='{1}'>2</x>",
904 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
909 public void TestWriteTypedPrimitive_Guid ()
911 Guid guid = new Guid ("CA761232-ED42-11CE-BACD-00AA0057B223");
912 string expectedGuid = "ca761232-ed42-11ce-bacd-00aa0057b223";
914 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
917 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, guid, false);
918 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
919 "<x xmlns='{0}'>{1}</x>", ANamespace, expectedGuid),
924 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, guid, false);
925 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
926 "<x>{0}</x>", expectedGuid), xsw.Content, "#2");
930 xsw.ExecuteWriteTypedPrimitive ("x", null, guid, false);
931 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
932 "<x>{0}</x>", expectedGuid), xsw.Content, "#3");
936 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, guid, false);
937 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
938 "<x xmlns='{0}'>{1}</x>", XmlSchemaNamespace, expectedGuid),
943 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, guid, false);
944 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
945 "<x xmlns='{0}'>{1}</x>", XmlSchemaInstanceNamespace, expectedGuid),
950 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, guid, false);
951 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
952 "< xmlns='{0}'>{1}</>", ANamespace, expectedGuid),
957 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, guid, false);
958 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
959 "<guid xmlns='{0}'>{1}</guid>", WsdlTypesNamespace,
960 expectedGuid), xsw.Content, "#7");
964 public void TestWriteTypedPrimitive_Guid_XsiType ()
966 Guid guid = new Guid ("CA761232-ED42-11CE-BACD-00AA0057B223");
967 string expectedGuid = "ca761232-ed42-11ce-bacd-00aa0057b223";
969 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
972 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, guid, true);
973 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
974 "<x xmlns:q1='{0}' d1p1:type='q1:guid' xmlns:d1p1='{1}'>{2}</x>",
975 WsdlTypesNamespace, XmlSchemaInstanceNamespace, expectedGuid),
980 xsw.ExecuteWriteTypedPrimitive ("x", null, guid, true);
981 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
982 "<x xmlns:q2='{0}' d1p1:type='q2:guid' xmlns:d1p1='{1}'>{2}</x>",
983 WsdlTypesNamespace, XmlSchemaInstanceNamespace, expectedGuid),
988 public void TestWriteTypedPrimitive_Int ()
990 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
993 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, 76665, false);
994 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
995 "<x xmlns='{0}'>76665</x>", ANamespace), xsw.Content, "#1");
999 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, -5656, false);
1000 Assert.AreEqual ("<x>-5656</x>", xsw.Content, "#2");
1004 xsw.ExecuteWriteTypedPrimitive ("x", null, 0, false);
1005 Assert.AreEqual ("<x>0</x>", xsw.Content, "#3");
1009 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, 534, false);
1010 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1011 "<x xmlns='{0}'>534</x>", XmlSchemaNamespace), xsw.Content, "#4");
1015 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, -6756, false);
1016 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1017 "<x xmlns='{0}'>-6756</x>", XmlSchemaInstanceNamespace),
1022 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, 434, false);
1023 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1024 "< xmlns='{0}'>434</>", ANamespace), xsw.Content, "#6");
1028 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, 434, false);
1029 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1030 "<int xmlns='{0}'>434</int>", XmlSchemaNamespace),
1035 public void TestWriteTypedPrimitive_Int_XsiType ()
1037 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1040 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, -6756, true);
1041 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1042 "<x xmlns:q1='{0}' d1p1:type='q1:int' xmlns:d1p1='{1}'>-6756</x>",
1043 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1048 xsw.ExecuteWriteTypedPrimitive ("x", null, 434, true);
1049 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1050 "<x xmlns:q2='{0}' d1p1:type='q2:int' xmlns:d1p1='{1}'>434</x>",
1051 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1056 public void TestWriteTypedPrimitive_String ()
1058 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1061 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, "hello", false);
1062 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1063 "<x xmlns='{0}'>hello</x>", ANamespace), xsw.Content, "#1");
1067 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, "hello", false);
1068 Assert.AreEqual ("<x>hello</x>", xsw.Content, "#2");
1072 xsw.ExecuteWriteTypedPrimitive ("x", null, "hello", false);
1073 Assert.AreEqual ("<x>hello</x>", xsw.Content, "#3");
1077 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, "hello", false);
1078 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1079 "<x xmlns='{0}'>hello</x>", XmlSchemaNamespace),
1084 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, "hello", false);
1085 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1086 "<x xmlns='{0}'>hello</x>", XmlSchemaInstanceNamespace),
1091 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, string.Empty, false);
1092 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1093 "<x xmlns='{0}' />", ANamespace), xsw.Content, "#6");
1097 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, "<\"te'st\">", false);
1098 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1099 "< xmlns='{0}'><\"te'st\"></>", ANamespace),
1104 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, "hello", false);
1105 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1106 "<string xmlns='{0}'>hello</string>", XmlSchemaNamespace),
1111 public void TestWriteTypedPrimitive_String_XsiType ()
1113 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1116 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, "hello", true);
1117 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1118 "<x xmlns:q1='{0}' d1p1:type='q1:string' xmlns:d1p1='{1}'>hello</x>",
1119 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1124 xsw.ExecuteWriteTypedPrimitive ("x", null, "hello", true);
1125 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1126 "<x xmlns:q2='{0}' d1p1:type='q2:string' xmlns:d1p1='{1}'>hello</x>",
1127 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1132 public void TestWriteTypedPrimitive_String_XsiType_Namespace ()
1134 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1137 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, "hello", true);
1138 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1139 "<x xmlns:q1='{0}' d1p1:type='q1:string' xmlns:d1p1='{1}' xmlns='{2}'>hello</x>",
1140 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1145 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, "hello", true);
1146 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1147 "<x d1p1:type='string' xmlns:d1p1='{0}' xmlns='{1}'>hello</x>",
1148 XmlSchemaInstanceNamespace, XmlSchemaNamespace),
1153 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, "hello", true);
1154 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1155 "<x xmlns:q2='{0}' d1p1:type='q2:string' xmlns:d1p1='{1}' xmlns='{1}'>hello</x>",
1156 XmlSchemaNamespace, XmlSchemaInstanceNamespace), xsw.Content, "#3");
1160 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, string.Empty, true);
1161 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1162 "<x xmlns:q3='{0}' d1p1:type='q3:string' xmlns:d1p1='{1}' xmlns='{2}' />",
1163 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1168 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, "<\"te'st\">", true);
1169 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1170 "< xmlns:q4='{0}' d1p1:type='q4:string' xmlns:d1p1='{1}' xmlns='{2}'><\"te'st\"></>",
1171 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1176 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, "hello", true);
1177 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1178 "<string d1p1:type='string' xmlns:d1p1='{0}' xmlns='{1}'>hello</string>",
1179 XmlSchemaInstanceNamespace, XmlSchemaNamespace),
1184 public void TestWriteTypedPrimitive_UnsignedByte ()
1186 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1189 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, (byte) 5, false);
1190 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1191 "<x xmlns='{0}'>5</x>", ANamespace), xsw.Content, "#1");
1195 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, (byte) 125, false);
1196 Assert.AreEqual ("<x>125</x>", xsw.Content, "#2");
1200 xsw.ExecuteWriteTypedPrimitive ("x", null, (byte) 0, false);
1201 Assert.AreEqual ("<x>0</x>", xsw.Content, "#3");
1205 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, (byte) 255, false);
1206 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1207 "<x xmlns='{0}'>255</x>", XmlSchemaNamespace), xsw.Content, "#4");
1211 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, (byte) 128, false);
1212 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1213 "<x xmlns='{0}'>128</x>", XmlSchemaInstanceNamespace),
1218 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, (byte) 1, false);
1219 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1220 "< xmlns='{0}'>1</>", ANamespace), xsw.Content, "#6");
1224 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, (byte) 99, false);
1225 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1226 "<unsignedByte xmlns='{0}'>99</unsignedByte>",
1227 XmlSchemaNamespace), xsw.Content, "#7");
1231 public void TestWriteTypedPrimitive_UnsignedByte_XsiType ()
1233 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1236 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, (byte) 5, true);
1237 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1238 "<x xmlns:q1='{0}' d1p1:type='q1:unsignedByte' xmlns:d1p1='{1}'>5</x>",
1239 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1244 xsw.ExecuteWriteTypedPrimitive ("x", null, (byte) 99, true);
1245 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1246 "<x xmlns:q2='{0}' d1p1:type='q2:unsignedByte' xmlns:d1p1='{1}'>99</x>",
1247 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1252 public void TestWriteTypedPrimitive_XmlQualifiedName ()
1254 XmlQualifiedName qname = new XmlQualifiedName ("something", AnotherNamespace);
1256 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1259 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, qname, false);
1260 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1261 "<x xmlns:q1='{0}' xmlns='{1}'>q1:something</x>",
1262 AnotherNamespace, ANamespace), xsw.Content, "#A1");
1266 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, qname, false);
1267 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1268 "<x xmlns:q2='{0}'>q2:something</x>",
1269 AnotherNamespace), xsw.Content, "#A2");
1273 xsw.ExecuteWriteTypedPrimitive ("x", null, qname, false);
1274 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1275 "<x xmlns:q3='{0}'>q3:something</x>", AnotherNamespace),
1276 xsw.Content, "#A3");
1280 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, qname, false);
1281 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1282 "<x xmlns:q4='{0}' xmlns='{1}'>q4:something</x>", AnotherNamespace,
1283 XmlSchemaNamespace), xsw.Content, "#A4");
1287 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, qname, false);
1288 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1289 "<x xmlns:q5='{0}' xmlns='{1}'>q5:something</x>", AnotherNamespace,
1290 XmlSchemaInstanceNamespace), xsw.Content, "#A5");
1294 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, qname, false);
1295 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1296 "< xmlns:q6='{0}' xmlns='{1}'>q6:something</>", AnotherNamespace,
1297 ANamespace), xsw.Content, "#A6");
1301 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, qname, false);
1302 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1303 "<QName xmlns:q7='{0}' xmlns='{1}'>q7:something</QName>",
1304 AnotherNamespace, XmlSchemaNamespace), xsw.Content, "#A7");
1308 qname = new XmlQualifiedName ("else");
1310 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, qname, false);
1311 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1312 "<x xmlns='{0}'>else</x>", ANamespace), xsw.Content, "#B1");
1316 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, qname, false);
1317 Assert.AreEqual ("<x>else</x>", xsw.Content, "#B2");
1321 xsw.ExecuteWriteTypedPrimitive ("x", null, qname, false);
1322 Assert.AreEqual ("<x>else</x>", xsw.Content, "#B3");
1326 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, qname, false);
1327 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1328 "<x xmlns='{0}'>else</x>", XmlSchemaNamespace), xsw.Content, "#B4");
1332 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, qname, false);
1333 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1334 "<x xmlns='{0}'>else</x>", XmlSchemaInstanceNamespace),
1335 xsw.Content, "#B5");
1339 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, qname, false);
1340 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1341 "< xmlns='{0}'>else</>", ANamespace), xsw.Content, "#B6");
1345 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, qname, false);
1346 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1347 "<QName xmlns='{0}'>else</QName>", XmlSchemaNamespace),
1348 xsw.Content, "#B7");
1352 [ExpectedException (typeof (NullReferenceException))]
1353 public void TestWriteTypedPrimitive_Null_Value()
1355 WriteTypedPrimitive("x", ANamespace, null, false);
1359 [ExpectedException (typeof (InvalidOperationException))]
1360 public void TestWriteTypedPrimitive_NonPrimitive ()
1362 // The type System.Version was not expected. Use the XmlInclude
1363 // or SoapInclude attribute to specify types that are not known
1365 WriteTypedPrimitive ("x", ANamespace, new Version (), false);
1369 [ExpectedException (typeof (InvalidOperationException))]
1370 public void TestWriteTypedPrimitive_XmlNode ()
1372 WriteTypedPrimitive ("x", ANamespace, new XmlDocument ().CreateElement ("foo"), false);
1376 public void TestWriteValue()
1379 Assert.AreEqual ("", Content);
1382 WriteValue("hello");
1383 Assert.AreEqual ("hello", Content);
1388 Assert.AreEqual ("", Content);
1391 WriteValue(new byte[] {13, 8, 99});
1392 Assert.AreEqual ("DQhj", Content);
1395 public void TestWriteXmlAttribute()
1402 public void TestWriteXsiType()
1404 WriteStartElement("x");
1405 WriteXsiType("pref", null);
1407 Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
1408 "<x d1p1:type='pref' xmlns:d1p1='{0}' />", XmlSchemaInstanceNamespace),
1413 WriteStartElement ("x");
1414 WriteXsiType ("int", XmlSchemaNamespace);
1416 Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
1417 "<x xmlns:q2='{0}' d1p1:type='q2:int' xmlns:d1p1='{1}' />",
1418 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1423 WriteStartElement ("x");
1424 WriteXsiType ("int", ANamespace);
1426 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1427 "<x xmlns:q3='{0}' d1p1:type='q3:int' xmlns:d1p1='{1}' />",
1428 ANamespace, XmlSchemaInstanceNamespace), Content, "#3");
1432 WriteStartElement ("x");
1433 WriteXsiType ("int", XmlSchemaInstanceNamespace);
1435 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1436 "<x xmlns:q4='{0}' q4:type='q4:int' />",
1437 XmlSchemaInstanceNamespace), Content, "#4");
1441 WriteStartElement ("x");
1442 WriteXsiType ("int", string.Empty);
1444 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1445 "<x d1p1:type='int' xmlns:d1p1='{0}' />", XmlSchemaInstanceNamespace),
1450 WriteStartElement ("x");
1451 WriteXsiType (string.Empty, null);
1453 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1454 "<x d1p1:type='' xmlns:d1p1='{0}' />", XmlSchemaInstanceNamespace),
1459 WriteStartElement ("x");
1460 WriteXsiType (null, null);
1462 Assert.AreEqual ("<x />", Content, "#7");
1466 public void TestWriteXsiType_Namespace ()
1468 WriteStartElement ("x", ANamespace);
1469 WriteXsiType ("pref", null);
1471 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1472 "<x d1p1:type='pref' xmlns:d1p1='{0}' xmlns='{1}' />",
1473 XmlSchemaInstanceNamespace, ANamespace), Content, "#1");
1477 WriteStartElement ("x", ANamespace);
1478 WriteXsiType ("int", XmlSchemaNamespace);
1480 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1481 "<x xmlns:q5='{0}' d1p1:type='q5:int' xmlns:d1p1='{1}' xmlns='{2}' />",
1482 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1487 WriteStartElement ("x", ANamespace);
1488 WriteXsiType ("int", ANamespace);
1490 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1491 "<x d1p1:type='int' xmlns:d1p1='{1}' xmlns='{2}' />",
1492 ANamespace, XmlSchemaInstanceNamespace, ANamespace),
1497 WriteStartElement ("x", ANamespace);
1498 WriteXsiType ("int", XmlSchemaInstanceNamespace);
1500 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1501 "<x xmlns:q6='{0}' q6:type='q6:int' xmlns='{1}' />",
1502 XmlSchemaInstanceNamespace, ANamespace), Content, "#4");
1506 WriteStartElement ("x", ANamespace);
1507 WriteXsiType ("int", string.Empty);
1509 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1510 "<x d1p1:type='int' xmlns:d1p1='{0}' xmlns='{1}' />",
1511 XmlSchemaInstanceNamespace, ANamespace), Content, "#5");
1515 WriteStartElement ("x", ANamespace);
1516 WriteXsiType (string.Empty, null);
1518 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1519 "<x d1p1:type='' xmlns:d1p1='{0}' xmlns='{1}' />",
1520 XmlSchemaInstanceNamespace, ANamespace), Content, "#6");
1524 WriteStartElement ("x", ANamespace);
1525 WriteXsiType (null, null);
1527 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1528 "<x xmlns='{0}' />", ANamespace), Content, "#7");
1533 public void TestFromEnum_Null_TypeName ()
1535 string[] values = { "one", "two", "three", "four" };
1536 long[] ids = { 1, 2, 3, 4 };
1538 Assert.AreEqual ("one", FromEnum (1, values, ids, (string) null));
1542 public void TestCreateInvalidEnumValueException ()
1544 Exception ex = CreateInvalidEnumValueException("AnInvalidValue", "SomeType");
1545 Assert.IsNotNull (ex, "#1");
1546 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1547 Assert.IsNotNull (ex.Message, "#3");
1548 Assert.IsTrue (ex.Message.IndexOf ("AnInvalidValue") != -1, "#4");
1549 Assert.IsTrue (ex.Message.IndexOf ("SomeType") != -1, "#5");
1553 public void WriteCharacter ()
1555 // mostly from bug #673019
1556 var SerializerObj = new XmlSerializer (typeof (ToBeSerialized));
1557 StringWriter writer = new StringWriter ();
1558 SerializerObj.Serialize (writer, new ToBeSerialized ());
1559 Assert.IsTrue (writer.ToString ().IndexOf ("<character>39</character>") > 0, "#1");
1563 public class ToBeSerialized
1565 [global::System.ComponentModel.DefaultValue ('a')]
1566 public char character = '\'';
1570 public void TestNullableDatesAndTimes ()
1572 DateTime dt = new DateTime (2012, 1, 3, 10, 0, 0, 0);
1574 var d = new NullableDatesAndTimes () {
1576 MyTimeNullable = dt,
1581 XmlSerializer ser = new XmlSerializer (d.GetType ());
1582 StringWriter sw = new StringWriter ();
1583 ser.Serialize (sw, d);
1584 string str = sw.ToString ();
1586 Assert.IsTrue (str.IndexOf ("<MyTime>10:00:00</MyTime>") != -1, "Time");
1587 Assert.IsTrue (str.IndexOf ("<MyTimeNullable>10:00:00</MyTimeNullable>") != -1, "Nullable Time");
1588 Assert.IsTrue (str.IndexOf ("<MyDate>2012-01-03</MyDate>") != -1, "Date");
1589 Assert.IsTrue (str.IndexOf ("<MyDateNullable>2012-01-03</MyDateNullable>") != -1, "Nullable Datwe");