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();
134 Assert.AreEqual ("0001-01-01T00:00:00", FromDateTime (d));
136 Assert.AreEqual ("0001-01-01T00:00:00.0000000", FromDateTime (d).Substring (0, 27));
141 public void TestFromEnum()
143 long[] ids = {1, 2, 3, 4};
144 string[] values = {"one", "two", "three"};
146 Assert.AreEqual ("one", FromEnum (1, values, ids), "#1");
147 Assert.AreEqual (string.Empty, FromEnum (0, values, ids), "#2");
148 Assert.AreEqual ("one two", FromEnum (3, values, ids), "#3");
151 string dummy = FromEnum(4, values, ids);
153 } catch (IndexOutOfRangeException) {
156 string[] correctValues = {"one", "two", "three", "four"};
157 Assert.AreEqual ("four", FromEnum (4, correctValues, ids), "#5");
158 Assert.AreEqual ("one four", FromEnum (5, correctValues, ids), "#6");
159 Assert.AreEqual ("two four", FromEnum (6, correctValues, ids), "#7");
160 Assert.AreEqual ("one two three four", FromEnum (7, correctValues, ids), "#8");
162 string[] flagValues = {"one", "two", "four", "eight"};
163 long[] flagIDs = {1, 2, 4, 8};
164 Assert.AreEqual (string.Empty, FromEnum (0, flagValues, flagIDs), "#9");
165 Assert.AreEqual ("two", FromEnum (2, flagValues, flagIDs), "#10");
166 Assert.AreEqual ("four", FromEnum (4, flagValues, flagIDs), "#1");
167 Assert.AreEqual ("one four", FromEnum (5, flagValues, flagIDs), "#12");
168 Assert.AreEqual ("two four", FromEnum (6, flagValues, flagIDs), "#13");
169 Assert.AreEqual ("one two four", FromEnum (7, flagValues, flagIDs), "#14");
170 Assert.AreEqual ("eight", FromEnum (8, flagValues, flagIDs), "#15");
171 Assert.AreEqual ("one four eight", FromEnum (13, flagValues, flagIDs), "#16");
173 string[] unorderedValues = {"one", "four", "two", "zero"};
174 long[] unorderedIDs = {1, 4, 2, 0};
176 Assert.AreEqual (string.Empty, FromEnum (0, unorderedValues, unorderedIDs), "#17");
177 Assert.AreEqual ("two", FromEnum (2, unorderedValues, unorderedIDs), "#18");
178 Assert.AreEqual ("four", FromEnum (4, unorderedValues, unorderedIDs), "#19");
179 Assert.AreEqual ("one four", FromEnum (5, unorderedValues, unorderedIDs), "#20");
180 Assert.AreEqual ("four two", FromEnum (6, unorderedValues, unorderedIDs), "#21");
181 Assert.AreEqual ("one four two", FromEnum (7, unorderedValues, unorderedIDs), "#22");
183 string[] zeroValues = {"zero", "ten"};
184 long[] zeroIDs = {0, 10};
186 Assert.AreEqual ("zero", FromEnum (0, zeroValues, zeroIDs), "#9");
187 Assert.AreEqual ("ten", FromEnum (10, zeroValues, zeroIDs), "#9");
189 string[] reverseZeroValues = {"", "zero"};
190 long[] reverseZeroIDs = {4, 0};
191 Assert.AreEqual (string.Empty, FromEnum (0, reverseZeroValues, reverseZeroIDs), "#9");
192 Assert.AreEqual ("zero", FromEnum (4, reverseZeroValues, reverseZeroIDs), "#9");
194 string[] emptyValues = { "zero" };
195 long[] emptyIDs = {0};
196 Assert.AreEqual ("zero", FromEnum (0, emptyValues, emptyIDs), "#9");
200 public void TestFromEnum_InvalidValue ()
202 long[] ids = {1, 2, 3, 4};
203 string[] values = {"one", "two", "three", "four"};
207 FromEnum (8, values, ids);
209 } catch (InvalidOperationException ex) {
210 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
211 Assert.IsNotNull (ex.Message, "#A3");
212 Assert.IsTrue (ex.Message.IndexOf ("'8'") != -1, "#A4");
213 Assert.IsNull (ex.InnerException, "#A5");
216 Assert.AreEqual ("8", FromEnum (8, values, ids), "#A6");
221 FromEnum (8, values, ids, "Some.Type.Name");
223 } catch (InvalidOperationException ex) {
224 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
225 Assert.IsNotNull (ex.Message, "#B3");
226 Assert.IsTrue (ex.Message.IndexOf ("'8'") != -1, "#B4");
227 Assert.IsTrue (ex.Message.IndexOf ("Some.Type.Name") != -1, "#B5");
228 Assert.IsNull (ex.InnerException, "#B6");
234 [ExpectedException (typeof (NullReferenceException))]
235 public void TestFromEnum_Null_Values ()
237 long[] ids = { 1, 2, 3, 4 };
238 string[] values = { "one", "two", "three", "four" };
240 FromEnum (1, (string[]) null, ids);
244 [ExpectedException (typeof (NullReferenceException))]
245 public void TestFromEnum_Null_IDs ()
247 string[] values = { "one", "two", "three", "four" };
249 FromEnum (1, values, (long[]) null);
253 public void TestFromTime()
255 DateTime d = new DateTime();
256 // Don't include time zone.
257 Assert.AreEqual ("00:00:00.0000000", FromTime (d).Substring (0, 16));
261 public void TestFromXmlName()
263 Assert.AreEqual ("Hello", FromXmlName ("Hello"));
264 Assert.AreEqual ("go_x0020_dogs_x0020_go", FromXmlName ("go dogs go"));
265 Assert.AreEqual ("what_x0027_s_x0020_up", FromXmlName ("what's up"));
266 Assert.AreEqual ("_x0031_23go", FromXmlName ("123go"));
267 Assert.AreEqual ("Hello_x0020_what_x0027_s.up", FromXmlName ("Hello what's.up"));
271 public void TestFromXmlNCName()
273 Assert.AreEqual ("Hello", FromXmlNCName ("Hello"));
274 Assert.AreEqual ("go_x0020_dogs_x0020_go", FromXmlNCName ("go dogs go"));
275 Assert.AreEqual ("what_x0027_s_x0020_up", FromXmlNCName ("what's up"));
276 Assert.AreEqual ("_x0031_23go", FromXmlNCName ("123go"));
277 Assert.AreEqual ("Hello_x0020_what_x0027_s.up", FromXmlNCName ("Hello what's.up"));
281 public void TestFromXmlNmToken()
283 Assert.AreEqual ("Hello", FromXmlNmToken ("Hello"));
284 Assert.AreEqual ("go_x0020_dogs_x0020_go", FromXmlNmToken ("go dogs go"));
285 Assert.AreEqual ("what_x0027_s_x0020_up", FromXmlNmToken ("what's up"));
286 Assert.AreEqual ("123go", FromXmlNmToken ("123go"));
287 Assert.AreEqual ("Hello_x0020_what_x0027_s.up", FromXmlNmToken ("Hello what's.up"));
291 public void TestFromXmlNmTokens()
293 Assert.AreEqual ("Hello go dogs_go 123go what_x0027_s.up", FromXmlNmTokens ("Hello go dogs_go 123go what's.up"));
297 public void TestWriteAttribute()
299 WriteStartElement("x");
300 WriteAttribute("a", "b");
302 Assert.AreEqual ("<x a='b' />", Content);
305 WriteStartElement("x");
306 WriteAttribute("a", new byte[] {1, 2, 3});
308 Assert.AreEqual ("<x a='AQID' />", Content);
311 WriteStartElement("x");
312 WriteAttribute("a", "<b");
314 Assert.AreEqual ("<x a='<b' />", Content);
317 WriteStartElement("x");
318 string typedPlaceholder = null;
319 WriteAttribute("a", typedPlaceholder);
321 Assert.AreEqual ("<x />", Content);
324 WriteStartElement("x");
325 WriteAttribute("a", "\"");
327 Assert.AreEqual ("<x a='\"' />", Content);
330 WriteStartElement("x");
331 WriteAttribute("a", "b\nc");
333 Assert.AreEqual ("<x a='b
c' />", Content);
336 WriteStartElement("x");
337 WriteAttribute("a", ANamespace, "b");
339 Assert.AreEqual ("<x d1p1:a='b' xmlns:d1p1='some:urn' />", Content);
343 public void TestWriteElementEncoded()
350 public void TestWriteElementLiteral()
357 public void TestWriteElementString()
359 WriteElementString("x", "a");
360 Assert.AreEqual ("<x>a</x>", Content);
363 WriteElementString("x", "<a");
364 Assert.AreEqual ("<x><a</x>", Content);
368 public void TestWriteElementStringRaw()
370 byte [] placeHolderArray = null;
371 WriteElementStringRaw("x", placeHolderArray);
372 Assert.AreEqual ("", Content);
375 WriteElementStringRaw("x", new byte[] {0, 2, 4});
376 Assert.AreEqual ("<x>AAIE</x>", Content);
379 WriteElementStringRaw("x", new byte[] {});
380 Assert.AreEqual ("<x />", Content);
382 // Note to reader, the output is not valid xml
384 WriteElementStringRaw("x", "a > 13 && a < 19");
385 Assert.AreEqual ("<x>a > 13 && a < 19</x>", Content);
389 public void TestWriteEmptyTag()
392 Assert.AreEqual ("<x />", Content);
396 public void TestWriteNamespaceDeclarations()
398 XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
400 WriteStartElement("x");
401 WriteNamespaceDeclarations(ns);
403 Assert.AreEqual ("<x />", Content);
406 ns.Add("mypref", ANamespace);
407 WriteStartElement("x");
408 WriteNamespaceDeclarations(ns);
410 Assert.AreEqual (XmlSerializerTests.Infoset("<x xmlns:mypref='some:urn' />"), XmlSerializerTests.Infoset(Content));
413 ns.Add("ns2", "another:urn");
414 WriteStartElement("x");
415 WriteNamespaceDeclarations(ns);
417 Assert.AreEqual (XmlSerializerTests.Infoset("<x xmlns:ns2='another:urn' xmlns:mypref='some:urn' />"), XmlSerializerTests.Infoset(Content));
420 ns.Add("ns3", "ya:urn");
421 WriteStartElement("x");
422 WriteNamespaceDeclarations(ns);
424 Assert.AreEqual (XmlSerializerTests.Infoset("<x xmlns:ns3='ya:urn' xmlns:ns2='another:urn' xmlns:mypref='some:urn' />"), XmlSerializerTests.Infoset(Content));
428 public void TestWriteNullableStringLiteral()
430 WriteNullableStringLiteral("x", null, null);
431 Assert.AreEqual (XmlSerializerTests.Infoset("<x d1p1:nil='true' xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' />"), XmlSerializerTests.Infoset(Content));
434 WriteNullableStringLiteral("x", null, "");
435 Assert.AreEqual ("<x />", Content);
438 WriteNullableStringLiteral("x", null, "a<b\'c");
439 Assert.AreEqual ("<x>a<b\'c</x>", Content);
442 WriteNullableStringLiteral("x", ANamespace, "b");
443 Assert.AreEqual ("<x xmlns='some:urn'>b</x>", Content);
447 public void TestWriteNullableStringLiteralRaw()
449 WriteNullableStringLiteralRaw("x", null, new byte[] {1, 2, 244});
450 Assert.AreEqual ("<x>AQL0</x>", Content);
454 public void TestWriteNullTagEncoded()
456 WriteNullTagEncoded("x");
457 Assert.AreEqual (XmlSerializerTests.Infoset("<x d1p1:nil='true' xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' />"), XmlSerializerTests.Infoset(Content));
461 public void TestWriteNullTagLiteral()
463 WriteNullTagLiteral("x");
464 Assert.AreEqual (XmlSerializerTests.Infoset("<x d1p1:nil='true' xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' />"), XmlSerializerTests.Infoset(Content));
468 [Ignore ("Additional namespace prefixes are added")]
469 public void TestWritePotentiallyReferencingElement ()
471 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
474 xsw.ExecuteWritePotentiallyReferencingElement ("x", ANamespace, EnumDefaultValue.e1, typeof (EnumDefaultValue), true, false);
475 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
476 "<x xmlns='{0}'>1</x>", ANamespace), xsw.Content, "#1");
480 xsw.ExecuteWritePotentiallyReferencingElement ("x", ANamespace, (int) 1, typeof (EnumDefaultValue), true, false);
481 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
482 "<x xmlns:q1='{0}' d1p1:type='q1:int' xmlns:d1p1='{1}' xmlns='{2}'>1</x>",
483 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
488 xsw.ExecuteWritePotentiallyReferencingElement ("x", ANamespace, "something", typeof (string), true, false);
489 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
490 "<x xmlns='{0}'>something</x>", ANamespace), xsw.Content, "#3");
494 xsw.ExecuteWritePotentiallyReferencingElement ("x", ANamespace, "something", null, true, false);
495 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
496 "<x xmlns:q2='{0}' d1p1:type='q2:string' xmlns:d1p1='{1}' xmlns='{2}'>something</x>",
497 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
502 xsw.ExecuteWritePotentiallyReferencingElement ("x", ANamespace, new string[] { "A", "B" }, typeof (string[]), true, false);
503 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
504 "<q3:Array id='id1' xmlns:q4='{0}' q3:arrayType='q4:string[2]' xmlns:q3='{1}'>" +
507 "</q3:Array>", XmlSchemaNamespace, SoapEncodingNamespace), xsw.Content, "#5");
511 public void TestWriteSerializable()
514 //Assert.AreEqual (, "");
518 public void TestWriteStartDocument()
520 Assert.AreEqual ("", Content);
522 WriteStartDocument();
523 Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?>", Content);
527 public void TestWriteStartElement()
529 WriteStartElement("x");
531 Assert.AreEqual ("<x />", Content);
534 WriteStartElement("x");
537 Assert.AreEqual ("<x>a</x>", Content);
540 WriteStartElement("x");
541 WriteStartElement("y", "z");
544 Assert.AreEqual ("<x><y xmlns='z' /></x>", Content);
547 WriteStartElement("x");
548 WriteStartElement("y", "z", true);
551 Assert.AreEqual ("<x><q1:y xmlns:q1='z' /></x>", Content);
555 public void TestWriteTypedPrimitive_Base64Binary ()
557 byte[] byteArray = new byte[] { 255, 20, 10, 5, 0, 7 };
558 string expected = "/xQKBQAH";
560 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
563 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, byteArray, false);
564 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
565 "<x xmlns='{0}'>{1}</x>", ANamespace, expected),
570 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, byteArray, false);
571 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
572 "<x>{0}</x>", expected), xsw.Content, "#2");
576 xsw.ExecuteWriteTypedPrimitive ("x", null, byteArray, false);
577 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
578 "<x>{0}</x>", expected), xsw.Content, "#3");
582 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, byteArray, false);
583 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
584 "<x xmlns='{0}'>{1}</x>", XmlSchemaNamespace, expected),
589 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, byteArray, false);
590 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
591 "<x xmlns='{0}'>{1}</x>", XmlSchemaInstanceNamespace, expected),
596 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, byteArray, false);
597 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
598 "< xmlns='{0}'>{1}</>", ANamespace, expected), xsw.Content, "#6");
602 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, byteArray, false);
603 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
604 "<base64Binary xmlns='{0}'>{1}</base64Binary>",
605 XmlSchemaNamespace, expected), xsw.Content, "#7");
609 public void TestWriteTypedPrimitive_Base64Binary_XsiType ()
611 byte[] byteArray = new byte[] { 255, 20, 10, 5, 0, 7 };
612 string expected = "/xQKBQAH";
614 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
617 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, byteArray, true);
618 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
619 "<x xmlns:q1='{0}' d1p1:type='q1:base64Binary' xmlns:d1p1='{1}'>{2}</x>",
620 XmlSchemaNamespace, XmlSchemaInstanceNamespace, expected),
625 xsw.ExecuteWriteTypedPrimitive ("x", null, byteArray, true);
626 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
627 "<x xmlns:q2='{0}' d1p1:type='q2:base64Binary' xmlns:d1p1='{1}'>{2}</x>",
628 XmlSchemaNamespace, XmlSchemaInstanceNamespace, expected),
633 public void TestWriteTypedPrimitive_Boolean ()
635 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
638 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, true, false);
639 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
640 "<x xmlns='{0}'>true</x>", ANamespace), xsw.Content, "#1");
644 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, false, false);
645 Assert.AreEqual ("<x>false</x>", xsw.Content, "#2");
649 xsw.ExecuteWriteTypedPrimitive ("x", null, true, false);
650 Assert.AreEqual ("<x>true</x>", xsw.Content, "#3");
654 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, false, false);
655 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
656 "<x xmlns='{0}'>false</x>", XmlSchemaNamespace), xsw.Content, "#4");
660 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, true, false);
661 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
662 "<x xmlns='{0}'>true</x>", XmlSchemaInstanceNamespace),
667 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, false, false);
668 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
669 "< xmlns='{0}'>false</>", ANamespace), xsw.Content, "#6");
673 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, true, false);
674 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
675 "<boolean xmlns='{0}'>true</boolean>", XmlSchemaNamespace),
680 public void TestWriteTypedPrimitive_Boolean_XsiType ()
682 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
685 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, true, true);
686 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
687 "<x xmlns:q1='{0}' d1p1:type='q1:boolean' xmlns:d1p1='{1}'>true</x>",
688 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
693 xsw.ExecuteWriteTypedPrimitive ("x", null, false, true);
694 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
695 "<x xmlns:q2='{0}' d1p1:type='q2:boolean' xmlns:d1p1='{1}'>false</x>",
696 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
701 public void TestWriteTypedPrimitive_Char ()
703 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
706 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, 'c', false);
707 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
708 "<x xmlns='{0}'>99</x>", ANamespace), xsw.Content, "#1");
712 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, 'a', false);
713 Assert.AreEqual ("<x>97</x>", xsw.Content, "#2");
717 xsw.ExecuteWriteTypedPrimitive ("x", null, 'b', false);
718 Assert.AreEqual ("<x>98</x>", xsw.Content, "#3");
722 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, 'd', false);
723 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
724 "<x xmlns='{0}'>100</x>", XmlSchemaNamespace), xsw.Content, "#4");
728 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, 'e', false);
729 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
730 "<x xmlns='{0}'>101</x>", XmlSchemaInstanceNamespace),
735 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, ' ', false);
736 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
737 "< xmlns='{0}'>32</>", ANamespace), xsw.Content, "#6");
741 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, '0', false);
742 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
743 "<char xmlns='{0}'>48</char>", WsdlTypesNamespace),
748 public void TestWriteTypedPrimitive_Char_XsiType ()
750 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
753 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, 'c', true);
754 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
755 "<x xmlns:q1='{0}' d1p1:type='q1:char' xmlns:d1p1='{1}'>99</x>",
756 WsdlTypesNamespace, XmlSchemaInstanceNamespace),
761 xsw.ExecuteWriteTypedPrimitive ("x", null, 'a', true);
762 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
763 "<x xmlns:q2='{0}' d1p1:type='q2:char' xmlns:d1p1='{1}'>97</x>",
764 WsdlTypesNamespace, XmlSchemaInstanceNamespace),
769 public void TestWriteTypedPrimitive_DateTime ()
771 DateTime dateTime = new DateTime (1973, 08, 13);
773 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
776 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, dateTime, false);
777 // FIXME: This is a bad test case. The following switch
778 // should be applied to the entire test.
780 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
781 "<x xmlns='{0}'>1973-08-13T00:00:00</x>", ANamespace),
784 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
785 "<x xmlns='{0}'>{1}</x>", ANamespace, FromDateTime (dateTime)),
790 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, dateTime, false);
791 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
792 "<x>{0}</x>", FromDateTime (dateTime)), xsw.Content, "#2");
796 xsw.ExecuteWriteTypedPrimitive ("x", null, dateTime, false);
797 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
798 "<x>{0}</x>", FromDateTime (dateTime)), xsw.Content, "#3");
802 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, dateTime, false);
803 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
804 "<x xmlns='{0}'>{1}</x>", XmlSchemaNamespace,
805 FromDateTime (dateTime)), xsw.Content, "#4");
809 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, dateTime, false);
810 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
811 "<x xmlns='{0}'>{1}</x>", XmlSchemaInstanceNamespace,
812 FromDateTime (dateTime)), xsw.Content, "#5");
816 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, dateTime, false);
817 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
818 "< xmlns='{0}'>{1}</>", ANamespace, FromDateTime (dateTime)),
823 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, dateTime, false);
824 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
825 "<dateTime xmlns='{0}'>{1}</dateTime>", XmlSchemaNamespace,
826 FromDateTime (dateTime)), xsw.Content, "#7");
829 // FIXME: This is a bad test case.
830 // See TestWriteTypedPrimitive_DateTime.
832 public void TestWriteTypedPrimitive_DateTime_XsiType ()
834 DateTime dateTime = new DateTime (1973, 08, 13);
836 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
839 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, dateTime, true);
840 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
841 "<x xmlns:q1='{0}' d1p1:type='q1:dateTime' xmlns:d1p1='{1}'>{2}</x>",
842 XmlSchemaNamespace, XmlSchemaInstanceNamespace,
843 FromDateTime (dateTime)), xsw.Content, "#1");
847 xsw.ExecuteWriteTypedPrimitive ("x", null, dateTime, true);
848 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
849 "<x xmlns:q2='{0}' d1p1:type='q2:dateTime' xmlns:d1p1='{1}'>{2}</x>",
850 XmlSchemaNamespace, XmlSchemaInstanceNamespace,
851 FromDateTime (dateTime)), xsw.Content, "#2");
855 [Category ("NotWorking")] // enum name is output instead of integral value
856 public void TestWriteTypedPrimitive_Enum ()
858 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
861 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, EnumDefaultValue.e1, false);
862 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
863 "<x xmlns='{0}'>1</x>", ANamespace), xsw.Content, "#1");
867 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, EnumDefaultValue.e2, false);
868 Assert.AreEqual ("<x>2</x>", xsw.Content, "#2");
872 xsw.ExecuteWriteTypedPrimitive ("x", null, EnumDefaultValue.e3, false);
873 Assert.AreEqual ("<x>3</x>", xsw.Content, "#3");
877 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, EnumDefaultValue.e1, false);
878 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
879 "<x xmlns='{0}'>1</x>", XmlSchemaNamespace), xsw.Content, "#4");
883 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, EnumDefaultValue.e2, false);
884 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
885 "<x xmlns='{0}'>2</x>", XmlSchemaInstanceNamespace),
890 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, EnumDefaultValue.e3, false);
891 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
892 "< xmlns='{0}'>3</>", ANamespace), xsw.Content, "#6");
896 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, EnumDefaultValue.e2, false);
897 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
898 "<int xmlns='{0}'>2</int>", XmlSchemaNamespace),
903 [Category ("NotWorking")] // InvalidOperationException is thrown
904 public void TestWriteTypedPrimitive_Enum_XsiType ()
906 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
909 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, EnumDefaultValue.e1, true);
910 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
911 "<x xmlns:q1='{0}' d1p1:type='q1:int' xmlns:d1p1='{1}'>1</x>",
912 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
917 xsw.ExecuteWriteTypedPrimitive ("x", null, EnumDefaultValue.e2, true);
918 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
919 "<x xmlns:q2='{0}' d1p1:type='q2:int' xmlns:d1p1='{1}'>2</x>",
920 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
925 public void TestWriteTypedPrimitive_Guid ()
927 Guid guid = new Guid ("CA761232-ED42-11CE-BACD-00AA0057B223");
928 string expectedGuid = "ca761232-ed42-11ce-bacd-00aa0057b223";
930 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
933 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, guid, false);
934 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
935 "<x xmlns='{0}'>{1}</x>", ANamespace, expectedGuid),
940 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, guid, false);
941 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
942 "<x>{0}</x>", expectedGuid), xsw.Content, "#2");
946 xsw.ExecuteWriteTypedPrimitive ("x", null, guid, false);
947 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
948 "<x>{0}</x>", expectedGuid), xsw.Content, "#3");
952 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, guid, false);
953 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
954 "<x xmlns='{0}'>{1}</x>", XmlSchemaNamespace, expectedGuid),
959 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, guid, false);
960 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
961 "<x xmlns='{0}'>{1}</x>", XmlSchemaInstanceNamespace, expectedGuid),
966 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, guid, false);
967 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
968 "< xmlns='{0}'>{1}</>", ANamespace, expectedGuid),
973 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, guid, false);
974 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
975 "<guid xmlns='{0}'>{1}</guid>", WsdlTypesNamespace,
976 expectedGuid), xsw.Content, "#7");
980 public void TestWriteTypedPrimitive_Guid_XsiType ()
982 Guid guid = new Guid ("CA761232-ED42-11CE-BACD-00AA0057B223");
983 string expectedGuid = "ca761232-ed42-11ce-bacd-00aa0057b223";
985 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
988 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, guid, true);
989 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
990 "<x xmlns:q1='{0}' d1p1:type='q1:guid' xmlns:d1p1='{1}'>{2}</x>",
991 WsdlTypesNamespace, XmlSchemaInstanceNamespace, expectedGuid),
996 xsw.ExecuteWriteTypedPrimitive ("x", null, guid, true);
997 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
998 "<x xmlns:q2='{0}' d1p1:type='q2:guid' xmlns:d1p1='{1}'>{2}</x>",
999 WsdlTypesNamespace, XmlSchemaInstanceNamespace, expectedGuid),
1004 public void TestWriteTypedPrimitive_Int ()
1006 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1009 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, 76665, false);
1010 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1011 "<x xmlns='{0}'>76665</x>", ANamespace), xsw.Content, "#1");
1015 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, -5656, false);
1016 Assert.AreEqual ("<x>-5656</x>", xsw.Content, "#2");
1020 xsw.ExecuteWriteTypedPrimitive ("x", null, 0, false);
1021 Assert.AreEqual ("<x>0</x>", xsw.Content, "#3");
1025 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, 534, false);
1026 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1027 "<x xmlns='{0}'>534</x>", XmlSchemaNamespace), xsw.Content, "#4");
1031 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, -6756, false);
1032 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1033 "<x xmlns='{0}'>-6756</x>", XmlSchemaInstanceNamespace),
1038 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, 434, false);
1039 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1040 "< xmlns='{0}'>434</>", ANamespace), xsw.Content, "#6");
1044 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, 434, false);
1045 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1046 "<int xmlns='{0}'>434</int>", XmlSchemaNamespace),
1051 public void TestWriteTypedPrimitive_Int_XsiType ()
1053 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1056 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, -6756, true);
1057 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1058 "<x xmlns:q1='{0}' d1p1:type='q1:int' xmlns:d1p1='{1}'>-6756</x>",
1059 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1064 xsw.ExecuteWriteTypedPrimitive ("x", null, 434, true);
1065 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1066 "<x xmlns:q2='{0}' d1p1:type='q2:int' xmlns:d1p1='{1}'>434</x>",
1067 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1072 public void TestWriteTypedPrimitive_String ()
1074 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1077 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, "hello", false);
1078 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1079 "<x xmlns='{0}'>hello</x>", ANamespace), xsw.Content, "#1");
1083 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, "hello", false);
1084 Assert.AreEqual ("<x>hello</x>", xsw.Content, "#2");
1088 xsw.ExecuteWriteTypedPrimitive ("x", null, "hello", false);
1089 Assert.AreEqual ("<x>hello</x>", xsw.Content, "#3");
1093 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, "hello", false);
1094 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1095 "<x xmlns='{0}'>hello</x>", XmlSchemaNamespace),
1100 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, "hello", false);
1101 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1102 "<x xmlns='{0}'>hello</x>", XmlSchemaInstanceNamespace),
1107 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, string.Empty, false);
1108 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1109 "<x xmlns='{0}' />", ANamespace), xsw.Content, "#6");
1113 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, "<\"te'st\">", false);
1114 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1115 "< xmlns='{0}'><\"te'st\"></>", ANamespace),
1120 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, "hello", false);
1121 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1122 "<string xmlns='{0}'>hello</string>", XmlSchemaNamespace),
1127 public void TestWriteTypedPrimitive_String_XsiType ()
1129 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1132 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, "hello", true);
1133 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1134 "<x xmlns:q1='{0}' d1p1:type='q1:string' xmlns:d1p1='{1}'>hello</x>",
1135 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1140 xsw.ExecuteWriteTypedPrimitive ("x", null, "hello", true);
1141 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1142 "<x xmlns:q2='{0}' d1p1:type='q2:string' xmlns:d1p1='{1}'>hello</x>",
1143 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1148 public void TestWriteTypedPrimitive_String_XsiType_Namespace ()
1150 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1153 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, "hello", true);
1154 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1155 "<x xmlns:q1='{0}' d1p1:type='q1:string' xmlns:d1p1='{1}' xmlns='{2}'>hello</x>",
1156 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1161 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, "hello", true);
1162 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1163 "<x d1p1:type='string' xmlns:d1p1='{0}' xmlns='{1}'>hello</x>",
1164 XmlSchemaInstanceNamespace, XmlSchemaNamespace),
1169 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, "hello", true);
1170 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1171 "<x xmlns:q2='{0}' d1p1:type='q2:string' xmlns:d1p1='{1}' xmlns='{1}'>hello</x>",
1172 XmlSchemaNamespace, XmlSchemaInstanceNamespace), xsw.Content, "#3");
1176 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, string.Empty, true);
1177 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1178 "<x xmlns:q3='{0}' d1p1:type='q3:string' xmlns:d1p1='{1}' xmlns='{2}' />",
1179 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1184 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, "<\"te'st\">", true);
1185 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1186 "< xmlns:q4='{0}' d1p1:type='q4:string' xmlns:d1p1='{1}' xmlns='{2}'><\"te'st\"></>",
1187 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1192 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, "hello", true);
1193 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1194 "<string d1p1:type='string' xmlns:d1p1='{0}' xmlns='{1}'>hello</string>",
1195 XmlSchemaInstanceNamespace, XmlSchemaNamespace),
1200 public void TestWriteTypedPrimitive_UnsignedByte ()
1202 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1205 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, (byte) 5, false);
1206 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1207 "<x xmlns='{0}'>5</x>", ANamespace), xsw.Content, "#1");
1211 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, (byte) 125, false);
1212 Assert.AreEqual ("<x>125</x>", xsw.Content, "#2");
1216 xsw.ExecuteWriteTypedPrimitive ("x", null, (byte) 0, false);
1217 Assert.AreEqual ("<x>0</x>", xsw.Content, "#3");
1221 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, (byte) 255, false);
1222 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1223 "<x xmlns='{0}'>255</x>", XmlSchemaNamespace), xsw.Content, "#4");
1227 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, (byte) 128, false);
1228 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1229 "<x xmlns='{0}'>128</x>", XmlSchemaInstanceNamespace),
1234 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, (byte) 1, false);
1235 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1236 "< xmlns='{0}'>1</>", ANamespace), xsw.Content, "#6");
1240 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, (byte) 99, false);
1241 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1242 "<unsignedByte xmlns='{0}'>99</unsignedByte>",
1243 XmlSchemaNamespace), xsw.Content, "#7");
1247 public void TestWriteTypedPrimitive_UnsignedByte_XsiType ()
1249 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1252 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, (byte) 5, true);
1253 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1254 "<x xmlns:q1='{0}' d1p1:type='q1:unsignedByte' xmlns:d1p1='{1}'>5</x>",
1255 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1260 xsw.ExecuteWriteTypedPrimitive ("x", null, (byte) 99, true);
1261 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1262 "<x xmlns:q2='{0}' d1p1:type='q2:unsignedByte' xmlns:d1p1='{1}'>99</x>",
1263 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1268 public void TestWriteTypedPrimitive_XmlQualifiedName ()
1270 XmlQualifiedName qname = new XmlQualifiedName ("something", AnotherNamespace);
1272 XmlSerializarionWriterTester xsw = new XmlSerializarionWriterTester ();
1275 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, qname, false);
1276 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1277 "<x xmlns:q1='{0}' xmlns='{1}'>q1:something</x>",
1278 AnotherNamespace, ANamespace), xsw.Content, "#A1");
1282 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, qname, false);
1283 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1284 "<x xmlns:q2='{0}'>q2:something</x>",
1285 AnotherNamespace), xsw.Content, "#A2");
1289 xsw.ExecuteWriteTypedPrimitive ("x", null, qname, false);
1290 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1291 "<x xmlns:q3='{0}'>q3:something</x>", AnotherNamespace),
1292 xsw.Content, "#A3");
1296 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, qname, false);
1297 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1298 "<x xmlns:q4='{0}' xmlns='{1}'>q4:something</x>", AnotherNamespace,
1299 XmlSchemaNamespace), xsw.Content, "#A4");
1303 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, qname, false);
1304 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1305 "<x xmlns:q5='{0}' xmlns='{1}'>q5:something</x>", AnotherNamespace,
1306 XmlSchemaInstanceNamespace), xsw.Content, "#A5");
1310 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, qname, false);
1311 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1312 "< xmlns:q6='{0}' xmlns='{1}'>q6:something</>", AnotherNamespace,
1313 ANamespace), xsw.Content, "#A6");
1317 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, qname, false);
1318 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1319 "<QName xmlns:q7='{0}' xmlns='{1}'>q7:something</QName>",
1320 AnotherNamespace, XmlSchemaNamespace), xsw.Content, "#A7");
1324 qname = new XmlQualifiedName ("else");
1326 xsw.ExecuteWriteTypedPrimitive ("x", ANamespace, qname, false);
1327 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1328 "<x xmlns='{0}'>else</x>", ANamespace), xsw.Content, "#B1");
1332 xsw.ExecuteWriteTypedPrimitive ("x", string.Empty, qname, false);
1333 Assert.AreEqual ("<x>else</x>", xsw.Content, "#B2");
1337 xsw.ExecuteWriteTypedPrimitive ("x", null, qname, false);
1338 Assert.AreEqual ("<x>else</x>", xsw.Content, "#B3");
1342 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaNamespace, qname, false);
1343 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1344 "<x xmlns='{0}'>else</x>", XmlSchemaNamespace), xsw.Content, "#B4");
1348 xsw.ExecuteWriteTypedPrimitive ("x", XmlSchemaInstanceNamespace, qname, false);
1349 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1350 "<x xmlns='{0}'>else</x>", XmlSchemaInstanceNamespace),
1351 xsw.Content, "#B5");
1355 xsw.ExecuteWriteTypedPrimitive (string.Empty, ANamespace, qname, false);
1356 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1357 "< xmlns='{0}'>else</>", ANamespace), xsw.Content, "#B6");
1361 xsw.ExecuteWriteTypedPrimitive (null, ANamespace, qname, false);
1362 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1363 "<QName xmlns='{0}'>else</QName>", XmlSchemaNamespace),
1364 xsw.Content, "#B7");
1368 [ExpectedException (typeof (NullReferenceException))]
1369 public void TestWriteTypedPrimitive_Null_Value()
1371 WriteTypedPrimitive("x", ANamespace, null, false);
1375 [ExpectedException (typeof (InvalidOperationException))]
1376 public void TestWriteTypedPrimitive_NonPrimitive ()
1378 // The type System.Version was not expected. Use the XmlInclude
1379 // or SoapInclude attribute to specify types that are not known
1381 WriteTypedPrimitive ("x", ANamespace, new Version (), false);
1385 [ExpectedException (typeof (InvalidOperationException))]
1386 public void TestWriteTypedPrimitive_XmlNode ()
1388 WriteTypedPrimitive ("x", ANamespace, new XmlDocument ().CreateElement ("foo"), false);
1392 public void TestWriteValue()
1395 Assert.AreEqual ("", Content);
1398 WriteValue("hello");
1399 Assert.AreEqual ("hello", Content);
1404 Assert.AreEqual ("", Content);
1407 WriteValue(new byte[] {13, 8, 99});
1408 Assert.AreEqual ("DQhj", Content);
1411 public void TestWriteXmlAttribute()
1418 public void TestWriteXsiType()
1420 WriteStartElement("x");
1421 WriteXsiType("pref", null);
1423 Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
1424 "<x d1p1:type='pref' xmlns:d1p1='{0}' />", XmlSchemaInstanceNamespace),
1429 WriteStartElement ("x");
1430 WriteXsiType ("int", XmlSchemaNamespace);
1432 Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
1433 "<x xmlns:q2='{0}' d1p1:type='q2:int' xmlns:d1p1='{1}' />",
1434 XmlSchemaNamespace, XmlSchemaInstanceNamespace),
1439 WriteStartElement ("x");
1440 WriteXsiType ("int", ANamespace);
1442 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1443 "<x xmlns:q3='{0}' d1p1:type='q3:int' xmlns:d1p1='{1}' />",
1444 ANamespace, XmlSchemaInstanceNamespace), Content, "#3");
1448 WriteStartElement ("x");
1449 WriteXsiType ("int", XmlSchemaInstanceNamespace);
1451 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1452 "<x xmlns:q4='{0}' q4:type='q4:int' />",
1453 XmlSchemaInstanceNamespace), Content, "#4");
1457 WriteStartElement ("x");
1458 WriteXsiType ("int", string.Empty);
1460 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1461 "<x d1p1:type='int' xmlns:d1p1='{0}' />", XmlSchemaInstanceNamespace),
1466 WriteStartElement ("x");
1467 WriteXsiType (string.Empty, null);
1469 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1470 "<x d1p1:type='' xmlns:d1p1='{0}' />", XmlSchemaInstanceNamespace),
1475 WriteStartElement ("x");
1476 WriteXsiType (null, null);
1478 Assert.AreEqual ("<x />", Content, "#7");
1482 public void TestWriteXsiType_Namespace ()
1484 WriteStartElement ("x", ANamespace);
1485 WriteXsiType ("pref", null);
1487 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1488 "<x d1p1:type='pref' xmlns:d1p1='{0}' xmlns='{1}' />",
1489 XmlSchemaInstanceNamespace, ANamespace), Content, "#1");
1493 WriteStartElement ("x", ANamespace);
1494 WriteXsiType ("int", XmlSchemaNamespace);
1496 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1497 "<x xmlns:q5='{0}' d1p1:type='q5:int' xmlns:d1p1='{1}' xmlns='{2}' />",
1498 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace),
1503 WriteStartElement ("x", ANamespace);
1504 WriteXsiType ("int", ANamespace);
1506 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1507 "<x d1p1:type='int' xmlns:d1p1='{1}' xmlns='{2}' />",
1508 ANamespace, XmlSchemaInstanceNamespace, ANamespace),
1513 WriteStartElement ("x", ANamespace);
1514 WriteXsiType ("int", XmlSchemaInstanceNamespace);
1516 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1517 "<x xmlns:q6='{0}' q6:type='q6:int' xmlns='{1}' />",
1518 XmlSchemaInstanceNamespace, ANamespace), Content, "#4");
1522 WriteStartElement ("x", ANamespace);
1523 WriteXsiType ("int", string.Empty);
1525 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1526 "<x d1p1:type='int' xmlns:d1p1='{0}' xmlns='{1}' />",
1527 XmlSchemaInstanceNamespace, ANamespace), Content, "#5");
1531 WriteStartElement ("x", ANamespace);
1532 WriteXsiType (string.Empty, null);
1534 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1535 "<x d1p1:type='' xmlns:d1p1='{0}' xmlns='{1}' />",
1536 XmlSchemaInstanceNamespace, ANamespace), Content, "#6");
1540 WriteStartElement ("x", ANamespace);
1541 WriteXsiType (null, null);
1543 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1544 "<x xmlns='{0}' />", ANamespace), Content, "#7");
1549 public void TestFromEnum_Null_TypeName ()
1551 string[] values = { "one", "two", "three", "four" };
1552 long[] ids = { 1, 2, 3, 4 };
1554 Assert.AreEqual ("one", FromEnum (1, values, ids, (string) null));
1558 public void TestCreateInvalidEnumValueException ()
1560 Exception ex = CreateInvalidEnumValueException("AnInvalidValue", "SomeType");
1561 Assert.IsNotNull (ex, "#1");
1562 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1563 Assert.IsNotNull (ex.Message, "#3");
1564 Assert.IsTrue (ex.Message.IndexOf ("AnInvalidValue") != -1, "#4");
1565 Assert.IsTrue (ex.Message.IndexOf ("SomeType") != -1, "#5");
1569 public void WriteCharacter ()
1571 // mostly from bug #673019
1572 var SerializerObj = new XmlSerializer (typeof (ToBeSerialized));
1573 StringWriter writer = new StringWriter ();
1574 SerializerObj.Serialize (writer, new ToBeSerialized ());
1575 Assert.IsTrue (writer.ToString ().IndexOf ("<character>39</character>") > 0, "#1");
1579 public class ToBeSerialized
1581 [global::System.ComponentModel.DefaultValue ('a')]
1582 public char character = '\'';
1586 public void TestNullableDatesAndTimes ()
1588 DateTime dt = new DateTime (2012, 1, 3, 10, 0, 0, 0);
1590 var d = new NullableDatesAndTimes () {
1592 MyTimeNullable = dt,
1597 XmlSerializer ser = new XmlSerializer (d.GetType ());
1598 StringWriter sw = new StringWriter ();
1599 ser.Serialize (sw, d);
1600 string str = sw.ToString ();
1602 Assert.IsTrue (str.IndexOf ("<MyTime>10:00:00</MyTime>") != -1, "Time");
1603 Assert.IsTrue (str.IndexOf ("<MyTimeNullable>10:00:00</MyTimeNullable>") != -1, "Nullable Time");
1604 Assert.IsTrue (str.IndexOf ("<MyDate>2012-01-03</MyDate>") != -1, "Date");
1605 Assert.IsTrue (str.IndexOf ("<MyDateNullable>2012-01-03</MyDateNullable>") != -1, "Nullable Datwe");