d2c9ce66b3ecfa0bfcd3fd1b625806fadf429918
[mono.git] / mcs / class / System.XML / Test / System.Xml.Serialization / XmlSerializerTestClasses.cs
1 //
2 // System.Xml.XmlSerializerTestClasses
3 //
4 // Author:
5 //   Erik LeBel <eriklebel@yahoo.ca>
6 //
7 // (C) 2003 Erik LeBel
8 //
9 // Classes to use in the testing of the XmlSerializer
10 //
11
12 using System;
13 using System.ComponentModel;
14 using System.Collections;
15 using System.Xml;
16 using System.Xml.Schema;
17 using System.Xml.Serialization;
18
19 namespace MonoTests.System.Xml.TestClasses
20 {
21         public enum SimpleEnumeration { FIRST, SECOND };
22
23         [Flags]
24         public enum EnumDefaultValue { e1 = 1, e2 = 2, e3 = 3 }
25         public enum EnumDefaultValueNF { e1 = 1, e2 = 2, e3 = 3 }
26
27         [Flags]
28         public enum FlagEnum { 
29                 [XmlEnum ("one")]
30                 e1 = 1,
31                 [XmlEnum ("two")]
32                 e2 = 2,
33                 [XmlEnum ("four")]
34                 e4 = 4
35         }
36
37         [Flags]
38         [SoapType ("flagenum")]
39         public enum FlagEnum_Encoded
40         {
41                 [SoapEnum ("one")]
42                 e1 = 1,
43                 [SoapEnum ("two")]
44                 e2 = 2,
45                 [SoapEnum ("four")]
46                 e4 = 4
47         }
48
49         [Flags]
50         public enum ZeroFlagEnum {
51                 [XmlEnum ("zero")]
52                 e0 = 0,
53                 [XmlEnum ("o<n>e")]
54                 e1 = 1,
55                 [XmlEnum ("tns:t<w>o")]
56                 e2 = 2,
57                 [XmlEnum ("four")]
58                 [XmlIgnore]
59                 e4 = 4
60         }
61         
62         public class SimpleClass
63         {
64                 public string something = null;
65         }
66
67         public class StringCollection : CollectionBase
68         {
69                 public void Add (String parameter) 
70                 {
71                         List.Insert (Count, parameter);
72                 }
73                         
74                 public String this [int index]
75                 {
76                         get
77                         { 
78                                 if (index < 0 || index > Count)
79                                         throw new ArgumentOutOfRangeException ();
80                                         
81                                 return (String) List [index]; 
82                         }
83                         set { List [index] = value; }
84                 }
85         }
86         
87         public class StringCollectionContainer
88         {
89                 StringCollection messages = new StringCollection();
90                 
91                 public StringCollection Messages
92                 {
93                         get { return messages; }
94                 }
95         }
96
97         public class ArrayContainer
98         {
99                 public object [] items = null;
100         }
101         
102         public class ClassArrayContainer
103         {
104                 public SimpleClass [] items = null;
105         }
106         
107         [XmlRoot("simple")]
108         public class SimpleClassWithXmlAttributes
109         {
110                 [XmlAttribute("member")]
111                 public string something = null;
112         }
113         
114         [XmlRoot("field")]
115         public class Field
116         {
117                 [XmlAttribute ("flag1")]
118                 [DefaultValue (1)]
119                 public FlagEnum Flags1;
120
121                 [XmlAttribute ("flag2")]
122                 [DefaultValue (FlagEnum.e1)]
123                 public FlagEnum Flags2;
124
125                 [XmlAttribute ("flag3", Form = XmlSchemaForm.Qualified)]
126                 [DefaultValue (FlagEnum.e1 | FlagEnum.e2)]
127                 public FlagEnum Flags3;
128
129                 [XmlAttribute ("flag4")]
130                 public FlagEnum Flags4;
131
132                 [XmlAttribute("modifiers")]
133                 public MapModifiers Modifiers;
134
135                 [XmlAttribute ("modifiers2", Form=XmlSchemaForm.Unqualified)]
136                 public MapModifiers Modifiers2;
137
138                 [XmlAttribute ("modifiers3")]
139                 [DefaultValue (0)]
140                 public MapModifiers Modifiers3;
141
142                 [XmlAttribute ("modifiers4", Form=XmlSchemaForm.Unqualified)]
143                 [DefaultValue (MapModifiers.Protected)]
144                 public MapModifiers Modifiers4;
145
146                 [XmlAttribute ("modifiers5", Form = XmlSchemaForm.Qualified)]
147                 [DefaultValue (MapModifiers.Public)]
148                 public MapModifiers Modifiers5;
149
150                 [XmlAttribute ("names")]
151                 public string[] Names;
152
153                 [XmlAttribute ("street")]
154                 public string Street;
155         }
156
157         [SoapType ("field", Namespace = "some:urn")]
158         public class Field_Encoded
159         {
160                 [SoapAttribute ("flag1")]
161                 [DefaultValue (FlagEnum_Encoded.e1)]
162                 public FlagEnum_Encoded Flags1;
163
164                 [SoapAttribute ("flag2")]
165                 [DefaultValue (FlagEnum_Encoded.e1)]
166                 public FlagEnum_Encoded Flags2;
167
168                 [SoapAttribute ("flag3")]
169                 [DefaultValue (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2)]
170                 public FlagEnum_Encoded Flags3;
171
172                 [SoapAttribute ("flag4")]
173                 public FlagEnum_Encoded Flags4;
174
175                 [SoapAttribute ("modifiers")]
176                 public MapModifiers Modifiers;
177
178                 [SoapAttribute ("modifiers2")]
179                 public MapModifiers Modifiers2;
180
181                 [SoapAttribute ("modifiers3")]
182                 [DefaultValue (MapModifiers.Public)]
183                 public MapModifiers Modifiers3;
184
185                 [SoapAttribute ("modifiers4")]
186                 [DefaultValue (MapModifiers.Protected)]
187                 public MapModifiers Modifiers4;
188
189                 [SoapAttribute ("modifiers5")]
190                 [DefaultValue (MapModifiers.Public)]
191                 public MapModifiers Modifiers5;
192
193                 public string[] Names;
194
195                 [SoapAttribute ("street")]
196                 public string Street;
197         }
198
199         [Flags]
200         public enum MapModifiers
201         {
202                 [XmlEnum("public")]
203                 [SoapEnum ("PuBlIc")]
204                 Public = 0,
205                 [XmlEnum("protected")]
206                 Protected = 1,
207         }
208
209         public class MyList : ArrayList
210         {
211                 object container;
212                 
213                 // NOTE: MyList has no public constructor
214                 public MyList (object container) : base()
215                 {
216                         this.container = container;
217                 }
218         }
219         
220         public class Container
221         {
222                 public MyList Items;
223                 
224                 public Container () {
225                         Items = new MyList(this);
226                 }
227         }
228         
229         public class Container2
230         {
231                 public MyList Items;
232                 
233                 public Container2 () {
234                 }
235                 
236                 public Container2 (bool b) {
237                         Items = new MyList(this);
238                 }
239         }
240
241         public class MyElem: XmlElement
242         {
243                 public MyElem (XmlDocument doc): base ("","myelem","", doc)
244                 {
245                         SetAttribute ("aa","1");
246                 }
247
248                 [XmlAttribute]
249                 public int kk=1;
250         }
251
252         public class MyDocument: XmlDocument
253         {
254                 public MyDocument ()
255                 {
256                 }
257
258                 [XmlAttribute]
259                 public int kk=1;
260         }
261         
262         public class CDataContainer
263         {
264                 public XmlCDataSection cdata;
265         }
266         
267         public class NodeContainer
268         {
269                 public XmlNode node;
270         }
271         
272         public class Choices
273         {
274                 [XmlElementAttribute("ChoiceZero", typeof(string), IsNullable=false)]
275                 [XmlElementAttribute("ChoiceOne", typeof(string), IsNullable=false)]
276                 [XmlElementAttribute("ChoiceTwo", typeof(string), IsNullable=false)]
277                 [XmlChoiceIdentifier("ItemType")]
278                 public string MyChoice;
279
280                 [XmlIgnore]
281                 public ItemChoiceType ItemType;
282         }
283         
284         [XmlType(IncludeInSchema = false)]
285         public enum ItemChoiceType
286         {
287                 ChoiceZero,
288                 [XmlEnum ("ChoiceOne")]
289                 StrangeOne,
290                 ChoiceTwo,
291         }
292         
293         public class WrongChoices
294         {
295                 [XmlElementAttribute("ChoiceZero", typeof(string), IsNullable=false)]
296                 [XmlElementAttribute("StrangeOne", typeof(string), IsNullable=false)]
297                 [XmlElementAttribute("ChoiceTwo", typeof(string), IsNullable=false)]
298                 [XmlChoiceIdentifier("ItemType")]
299                 public string MyChoice;
300
301                 [XmlIgnore]
302                 public ItemChoiceType ItemType;
303         }
304         
305         [XmlType ("Type with space")]
306         public class TestSpace
307         {
308            [XmlElement (ElementName = "Element with space")]
309            public int elem;
310             
311            [XmlAttribute (AttributeName = "Attribute with space")]
312            public int attr; 
313         }
314
315         [Serializable]
316         public class ReadOnlyProperties {
317                 string[] strArr = new string[2] { "string1", "string2" };
318
319                 public string[] StrArr {
320                         get { return strArr; }
321                 }
322                 
323                 public string dat {
324                         get { return "fff"; }
325                 } 
326         }
327         
328         [XmlRoot("root")]
329         public class ListDefaults
330         {
331                 public ListDefaults ()
332                 {
333                         ed = new SimpleClass ();
334                         str = "hola";
335                 }
336                 
337             public ArrayList list2;
338             
339             public MyList list3;
340             
341             public string[] list4;
342             
343                 [XmlElement("e", typeof(SimpleClass))]
344             public ArrayList list5;
345             
346                 [DefaultValue (null)]
347             public SimpleClass ed;
348             
349                 [DefaultValue (null)]
350             public string str; 
351         }
352         
353         public class clsPerson
354         {
355                 public IList EmailAccounts;
356         }
357         
358         public class ArrayClass
359         {
360                 public object names = new object[] { "un","dos" };
361         }
362         
363         public class CompositeValueType
364         {
365                 public void Init ()
366                 {
367                         Items = new object[] { 1, 2 };
368                         ItemsElementName = new ItemsChoiceType[] { ItemsChoiceType.In, ItemsChoiceType.Es };
369                 }
370            
371                 [XmlElementAttribute("Es", typeof(int))]
372                 [XmlElementAttribute("In", typeof(int))]
373                 [XmlChoiceIdentifierAttribute("ItemsElementName")]
374                 public object[] Items;
375            
376                 [XmlElementAttribute("ItemsElementName")]
377                 [XmlIgnoreAttribute()]
378                 public ItemsChoiceType[] ItemsElementName;
379         }
380
381         public enum ItemsChoiceType {
382            In, Es
383         }
384         
385         public class ArrayAttributeWithType
386         {
387                 [XmlAttribute (DataType="anyURI")]
388                 public string[] at = new string [] { "a","b" };
389
390                 [XmlAttribute (DataType="base64Binary")]
391                 public byte[][] bin1 = new byte[][] { new byte[]{1,2},  new byte[]{1,2}};
392                 
393                 [XmlAttribute (DataType="base64Binary")]
394                 public byte[] bin2 = new byte[] { 1,2 };
395         }
396         
397         public class ArrayAttributeWithWrongType
398         {
399                 [XmlAttribute (DataType="int")]
400                 public string[] at = new string [] { "a","b" };
401         }
402         
403         [XmlType ("Container")]
404         public class EntityContainer
405         {
406                 EntityCollection collection1;
407                 EntityCollection collection2;
408                 EntityCollection collection3 = new EntityCollection ("root");
409                 EntityCollection collection4 = new EntityCollection ("root");
410                 
411                 [XmlArray (IsNullable=true)]
412                 public EntityCollection Collection1 {
413                         get { return collection1; }
414                         set { collection1 = value; collection1.Container = "assigned"; }
415                 }
416                 
417                 [XmlArray (IsNullable=false)]
418                 public EntityCollection Collection2 {
419                         get { return collection2; }
420                         set { collection2 = value; collection2.Container = "assigned"; }
421                 }
422                 
423                 [XmlArray (IsNullable=true)]
424                 public EntityCollection Collection3 {
425                         get { return collection3; }
426                         set { collection3 = value; collection3.Container = "assigned"; }
427                 }
428                 
429                 [XmlArray (IsNullable=false)]
430                 public EntityCollection Collection4 {
431                         get { return collection4; }
432                         set { collection4 = value; collection4.Container = "assigned"; }
433                 }
434         }
435         
436         [XmlType ("Container")]
437         public class ArrayEntityContainer
438         {
439                 Entity[] collection1;
440                 Entity[] collection2;
441                 Entity[] collection3 = new Entity [0];
442                 Entity[] collection4 = new Entity [0];
443                 
444                 [XmlArray (IsNullable=true)]
445                 public Entity[] Collection1 {
446                         get { return collection1; }
447                         set { collection1 = value; }
448                 }
449                 
450                 [XmlArray (IsNullable=false)]
451                 public Entity[] Collection2 {
452                         get { return collection2; }
453                         set { collection2 = value; }
454                 }
455                 
456                 [XmlArray (IsNullable=true)]
457                 public Entity[] Collection3 {
458                         get { return collection3; }
459                         set { collection3 = value; }
460                 }
461                 
462                 [XmlArray (IsNullable=false)]
463                 public Entity[] Collection4 {
464                         get { return collection4; }
465                         set { collection4 = value; }
466                 }
467         }
468         
469         public class Entity
470         {
471                 private string _name = string.Empty;
472                 private string _parent = null;
473
474                 [XmlAttribute]
475                 public string Name {
476                         get { return _name; }
477                         set { _name = value; }
478                 }
479
480                 [XmlIgnore] 
481                 public string Parent {
482                         get { return _parent; }
483                         set { _parent = value; }
484                 }
485         }
486
487         public class EntityCollection : ArrayList
488         {
489                 public string _container;
490
491                 public EntityCollection ()
492                 {
493                 }
494
495                 public EntityCollection (string c)
496                 {
497                         _container = c;
498                 }
499
500                 public string Container {
501                         get { return _container; }
502                         set { _container = value; }
503                 }
504
505                 public int Add (Entity value)
506                 {
507                         if(_container != null)
508                                 value.Parent = _container;
509
510                         return base.Add(value);
511                 }
512
513                 public new Entity this[int index]
514                 {
515                         get { return (Entity) base[index]; }
516                         set { base[index] = value; }
517                 }
518         }
519         
520         [XmlType ("Container")]
521         public class ObjectWithReadonlyCollection
522         {
523                 EntityCollection collection1 = new EntityCollection ("root");
524                 
525                 public EntityCollection Collection1 {
526                         get { return collection1; }
527                 }
528         }
529         
530         [XmlType ("Container")]
531         public class ObjectWithReadonlyNulCollection
532         {
533                 EntityCollection collection1;
534                 
535                 public EntityCollection Collection1 {
536                         get { return collection1; }
537                 }
538         }
539         
540         [XmlType ("Container")]
541         public class ObjectWithReadonlyArray
542         {
543                 Entity[] collection1 = new Entity [0];
544                 
545                 public Entity[] Collection1 {
546                         get { return collection1; }
547                 }
548         }
549         
550         [XmlInclude (typeof(SubclassTestSub))]
551         public class SubclassTestBase
552         {
553         }
554         
555         public class SubclassTestSub: SubclassTestBase
556         {
557         }
558         
559         public class SubclassTestExtra
560         {
561         }
562         
563         public class SubclassTestContainer
564         {
565                 [XmlElement ("a", typeof(SubclassTestBase))]
566                 [XmlElement ("b", typeof(SubclassTestExtra))]
567                 public object data;
568         }
569
570         public class DictionaryWithIndexer : DictionaryBase
571         {
572                 public TimeSpan this[int index] {
573                         get { return TimeSpan.MinValue; }
574                 }
575
576                 public void Add (TimeSpan value)
577                 {
578                 }
579         }
580
581         [XmlRoot(Namespace="some:urn")]
582         [SoapTypeAttribute (Namespace="another:urn")]
583         public class PrimitiveTypesContainer
584         {
585                 public PrimitiveTypesContainer ()
586                 {
587                         Number = 2004;
588                         Name = "some name";
589                         Index = (byte) 56;
590                         Password = new byte[] { 243, 15 };
591                         PathSeparatorCharacter = '/';
592                 }
593
594                 public int Number;
595                 public string Name;
596                 public byte Index;
597                 public byte[] Password;
598                 public char PathSeparatorCharacter;
599         }
600         
601         public class TestSchemaForm1
602         {
603                 public PrintTypeResponse p1;
604                 
605                 [XmlElement(Namespace="urn:oo")]
606                 public PrintTypeResponse p2;
607         }
608
609         [XmlType (Namespace="urn:testForm")]
610         public class TestSchemaForm2
611         {
612                 public PrintTypeResponse p1;
613                 
614                 [XmlElement(Namespace="urn:oo")]
615                 public PrintTypeResponse p2;
616         }
617
618         [XmlType (Namespace="urn:responseTypes")]
619         public class PrintTypeResponse {
620                 [XmlElement (Form=XmlSchemaForm.Unqualified, IsNullable=true)]
621             public OutputType result;
622             public PrintTypeResponse intern;
623             
624             public void Init ()
625             {
626                 result = new OutputType ();
627                 result.data = "data1";
628                 intern = new PrintTypeResponse ();
629                 intern.result = new OutputType ();
630                 intern.result.data = "data2";
631             }
632         }
633
634         [XmlType (Namespace="urn:responseTypes")]
635         public class OutputType {
636             
637                 [XmlElement (Form=XmlSchemaForm.Unqualified, IsNullable=true)]
638             public string data;
639         }
640
641         [XmlRootAttribute ("testDefault", Namespace="urn:myNS", IsNullable=false)]
642         [SoapType("testDefault", Namespace="urn:myNS")]
643         public class TestDefault
644         {
645                 public string str;
646
647                 [DefaultValue ("Default Value")]
648                 public string strDefault = "Default Value";
649
650                 [DefaultValue (true)]
651                 public bool boolT = true;
652
653                 [DefaultValue (false)]
654                 public bool boolF = false;
655
656                 [DefaultValue (typeof (decimal), "10")]
657                 public decimal decimalval = 10m;
658
659                 [DefaultValue (FlagEnum.e1 | FlagEnum.e4)]
660                 public FlagEnum flag = (FlagEnum.e1 | FlagEnum.e4);
661
662                 [DefaultValue (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4)]
663                 public FlagEnum_Encoded flagencoded = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4);
664         }
665
666         [XmlType ("optionalValueType", Namespace="some:urn")]
667         [XmlRootAttribute ("optionalValue", Namespace="another:urn", IsNullable=false)]
668         public class OptionalValueTypeContainer
669         {
670                 [DefaultValue (FlagEnum.e1 | FlagEnum.e4)]
671                 public FlagEnum Attributes = FlagEnum.e1 | FlagEnum.e4;
672
673                 [DefaultValue (FlagEnum.e1)]
674                 public FlagEnum Flags = FlagEnum.e1;
675
676                 [XmlIgnore]
677                 [SoapIgnore]
678                 public bool FlagsSpecified;
679
680                 [DefaultValue (false)]
681                 public bool IsEmpty;
682
683                 [XmlIgnore]
684                 [SoapIgnore]
685                 public bool IsEmptySpecified {
686                         get { return _isEmptySpecified; }
687                         set { _isEmptySpecified = value; }
688                 }
689
690                 [DefaultValue (false)]
691                 public bool IsNull;
692
693                 private bool _isEmptySpecified;
694         }
695
696         public class Group
697         {
698                 [SoapAttribute (Namespace = "http://www.cpandl.com")]
699                 public string GroupName;
700
701                 [SoapAttribute (DataType = "base64Binary")]
702                 public Byte [] GroupNumber;
703
704                 [SoapAttribute (DataType = "date", AttributeName = "CreationDate")]
705                 public DateTime Today;
706
707                 [SoapElement (DataType = "nonNegativeInteger", ElementName = "PosInt")]
708                 public string PostitiveInt;
709
710                 [SoapIgnore]
711                 public bool IgnoreThis;
712
713                 [DefaultValue (GroupType.B)]
714                 public GroupType Grouptype;
715                 public Vehicle MyVehicle;
716
717                 [SoapInclude (typeof (Car))]
718                 public Vehicle myCar (string licNumber)
719                 {
720                         Vehicle v;
721                         if (licNumber == string.Empty) {
722                                 v = new Car();
723                                 v.licenseNumber = "!!!!!!";
724                         } else {
725                                 v = new Car();
726                                 v.licenseNumber = licNumber;
727                         }
728                         return v;
729                 }
730         }
731
732         [SoapInclude (typeof (Car))]
733         public abstract class Vehicle
734         {
735                 public string licenseNumber;
736                 [SoapElement (DataType = "date")]
737                 public DateTime makeDate;
738                 [DefaultValue ("450")]
739                 public string weight;
740         }
741
742         public class Car: Vehicle
743         {
744         }
745
746         public enum GroupType
747         {
748                 [SoapEnum ("Small")]
749                 A,
750                 [SoapEnum ("Large")]
751                 B
752         }
753 }