Author: lluis
Date: 2005-12-05 15:44:42 -0500 (Mon, 05 Dec 2005)
New Revision: 53949

Modified:
   trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog
   
trunk/mcs/class/System.XML/System.Xml.Serialization/SerializationCodeGenerator.cs
   
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs
   trunk/mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog
   trunk/mcs/class/System.XML/Test/System.Xml.Serialization/DeserializeTests.cs
   
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTestClasses.cs
Log:
2005-12-05  Lluis Sanchez Gual  <[EMAIL PROTECTED]> 

        * XmlSerializerTestClasses.cs:
        * DeserializeTests.cs: Added tests for bug #76808 and related behavior.

2005-12-05  Lluis Sanchez Gual  <[EMAIL PROTECTED]>

        * XmlSerializationReaderInterpreter.cs:
        * SerializationCodeGenerator.cs: When deserializing a collection,
        don't create a new collection if the property already has a 
        collection instance. This does not apply to arrays. Changed how nullabe
        array properties are set. Setting IsNullable=false to the property means
        that it won't be set if the read array is null (so, if the property
        already has a value, it will be kept).
        Another change is that can't never be null (only arrays can).



Modified: trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog
===================================================================
--- trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog       
2005-12-05 20:32:13 UTC (rev 53948)
+++ trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog       
2005-12-05 20:44:42 UTC (rev 53949)
@@ -1,3 +1,14 @@
+2005-12-05  Lluis Sanchez Gual  <[EMAIL PROTECTED]>
+
+       * XmlSerializationReaderInterpreter.cs:
+       * SerializationCodeGenerator.cs: When deserializing a collection,
+       don't create a new collection if the property already has a 
+       collection instance. This does not apply to arrays. Changed how nullabe
+       array properties are set. Setting IsNullable=false to the property means
+       that it won't be set if the read array is null (so, if the property
+       already has a value, it will be kept).
+       Another change is that can't never be null (only arrays can).
+
 2005-12-01  Lluis Sanchez Gual  <[EMAIL PROTECTED]>
 
        * TypeTranslator.cs: In GetTypeData, if an xmlType is specified for

Modified: 
trunk/mcs/class/System.XML/System.Xml.Serialization/SerializationCodeGenerator.cs
===================================================================
--- 
trunk/mcs/class/System.XML/System.Xml.Serialization/SerializationCodeGenerator.cs
   2005-12-05 20:32:13 UTC (rev 53948)
+++ 
trunk/mcs/class/System.XML/System.Xml.Serialization/SerializationCodeGenerator.cs
   2005-12-05 20:44:42 UTC (rev 53949)
@@ -1278,7 +1278,7 @@
                                        WriteLine ("throw 
CreateUnknownNodeException();");
                                        Unindent ();
 
-                                       WriteLine ("return " + 
GetReadObjectCall (typeMap, "true", "true") + ";");
+                                       WriteLine ("return " + 
GetReadObjectCall (typeMap, GetLiteral(typeMap.IsNullable), "true") + ";");
                                }
                                else
                                {
@@ -1739,8 +1739,27 @@
                                                else
                                                {
                                                        if 
(!GenerateReadMemberHook (xmlMapType, info.Member)) {
-                                                               if (IsReadOnly 
(typeMap, info.Member, info.TypeData, isValueList)) GenerateReadListElement 
(info.MappedType, GenerateGetMemberValue (info.Member, ob, isValueList), 
GetLiteral(info.IsNullable), false);
-                                                               else 
GenerateSetMemberValue (info.Member, ob, GenerateReadListElement 
(info.MappedType, null, GetLiteral(info.IsNullable), true), isValueList);
+                                                               if (IsReadOnly 
(typeMap, info.Member, info.TypeData, isValueList)) {
+                                                                       
GenerateReadListElement (info.MappedType, GenerateGetMemberValue (info.Member, 
ob, isValueList), GetLiteral(info.IsNullable), false);
+                                                               } else if 
(info.MappedType.TypeData.Type.IsArray) {
+                                                                       if 
(info.IsNullable)
+                                                                               
GenerateSetMemberValue (info.Member, ob, GenerateReadListElement 
(info.MappedType, null, GetLiteral(info.IsNullable), true), isValueList);
+                                                                       else {
+                                                                               
string list = GetObTempVar ();
+                                                                               
WriteLine (info.MappedType.TypeFullName + " " + list + " = " + 
GenerateReadListElement (info.MappedType, null, GetLiteral(info.IsNullable), 
true) + ";");
+                                                                               
WriteLineInd ("if (((object)" + list + ") != null) {");
+                                                                               
GenerateSetMemberValue (info.Member, ob, list, isValueList);
+                                                                               
WriteLineUni ("}");
+                                                                       }
+                                                               } else {
+                                                                       string 
list = GetObTempVar ();
+                                                                       
WriteLine (info.MappedType.TypeFullName + " " + list + " = " + 
GenerateGetMemberValue (info.Member, ob, isValueList) + ";");
+                                                                       
WriteLineInd ("if (((object)" + list + ") == null) {");
+                                                                       
WriteLine (list + " = " + GenerateCreateList (info.MappedType.TypeData.Type) + 
";");
+                                                                       
GenerateSetMemberValue (info.Member, ob, list, isValueList);
+                                                                       
WriteLineUni ("}");
+                                                                       
GenerateReadListElement (info.MappedType, list, GetLiteral(info.IsNullable), 
true);
+                                                               }
                                                                GenerateEndHook 
();
                                                        }
                                                }
@@ -2024,13 +2043,20 @@
                {
                        Type listType = typeMap.TypeData.Type;
                        ListMap listMap = (ListMap)typeMap.ObjectMap;
+                       bool doNullCheck = typeMap.TypeData.Type.IsArray;
 
                        if (canCreateInstance && 
typeMap.TypeData.HasPublicConstructor) 
                        {
-                               list = GetObTempVar ();
-                               WriteLine (typeMap.TypeFullName + " " + list + 
" = null;");
-                               WriteLineInd ("if (!ReadNull()) {");
-                               WriteLine (list + " = " + GenerateCreateList 
(listType) + ";");
+                               if (list == null) {
+                                       list = GetObTempVar ();
+                                       WriteLine (typeMap.TypeFullName + " " + 
list + " = null;");
+                                       if (doNullCheck)
+                                               WriteLineInd ("if (!ReadNull()) 
{");
+                                       WriteLine (list + " = " + 
GenerateCreateList (listType) + ";");
+                               } else {
+                                       if (doNullCheck)
+                                               WriteLineInd ("if (!ReadNull()) 
{");
+                               }
                        }
                        else
                        {
@@ -2038,14 +2064,10 @@
                                        WriteLineInd ("if (((object)" + list + 
") == null)");
                                        WriteLine ("throw 
CreateReadOnlyCollectionException (" + GetLiteral (typeMap.TypeFullName) + 
");");
                                        Unindent ();
-                                       WriteLineInd ("if (!ReadNull()) {");
+                                       doNullCheck = false;
                                }
                                else {
-                                       list = GetObTempVar ();
-                                       WriteLine (typeMap.TypeFullName + " " + 
list + " = null;");
-                                       WriteLineInd ("if (((object)" + list + 
") == null)");
                                        WriteLine ("throw 
CreateReadOnlyCollectionException (" + GetLiteral (typeMap.TypeFullName) + 
");");
-                                       Unindent ();
                                        return list;
                                }
                        }
@@ -2053,7 +2075,7 @@
                        WriteLineInd ("if (Reader.IsEmptyElement) {");
                        WriteLine ("Reader.Skip();");
                        if (listType.IsArray)
-                               WriteLine (list + " = (" + typeMap.TypeFullName 
+ ") ShrinkArray (" + list + ", 0, " + GetTypeOf(listType.GetElementType()) + 
", " + isNullable + ");");
+                               WriteLine (list + " = (" + typeMap.TypeFullName 
+ ") ShrinkArray (" + list + ", 0, " + GetTypeOf(listType.GetElementType()) + 
", false);");
 
                        Unindent ();
                        WriteLineInd ("} else {");
@@ -2090,10 +2112,11 @@
                        WriteLine ("ReadEndElement();");
 
                        if (listType.IsArray)
-                               WriteLine (list + " = (" + typeMap.TypeFullName 
+ ") ShrinkArray (" + list + ", " + index + ", " + 
GetTypeOf(listType.GetElementType()) + ", " + isNullable + ");");
+                               WriteLine (list + " = (" + typeMap.TypeFullName 
+ ") ShrinkArray (" + list + ", " + index + ", " + 
GetTypeOf(listType.GetElementType()) + ", false);");
 
                        WriteLineUni ("}");
-                       WriteLineUni ("}");
+                       if (doNullCheck)
+                               WriteLineUni ("}");
 
                        return list;
                }

Modified: 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs
===================================================================
--- 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs
    2005-12-05 20:32:13 UTC (rev 53948)
+++ 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs
    2005-12-05 20:44:42 UTC (rev 53949)
@@ -163,7 +163,7 @@
                                if (Reader.LocalName != rootMap.ElementName || 
Reader.NamespaceURI != rootMap.Namespace)
                                        throw CreateUnknownNodeException();
                                
-                               return ReadObject (rootMap, true, true);
+                               return ReadObject (rootMap, rootMap.IsNullable, 
true);
                        }
                }               
 
@@ -381,8 +381,21 @@
                                                        }
                                                        else
                                                        {
-                                                               if (IsReadOnly 
(info.Member, info.TypeData, ob, isValueList)) ReadListElement 
(info.MappedType, info.IsNullable, GetMemberValue (info.Member, ob, 
isValueList), false);
-                                                               else 
SetMemberValue (info.Member, ob, ReadListElement (info.MappedType, 
info.IsNullable, null, true), isValueList);
+                                                               if (IsReadOnly 
(info.Member, info.TypeData, ob, isValueList)) {
+                                                                       
ReadListElement (info.MappedType, info.IsNullable, GetMemberValue (info.Member, 
ob, isValueList), false);
+                                                               } else if 
(info.MappedType.TypeData.Type.IsArray) {
+                                                                       object 
list = ReadListElement (info.MappedType, info.IsNullable, null, true);
+                                                                       if 
(list != null || info.IsNullable)
+                                                                               
SetMemberValue (info.Member, ob, list, isValueList);
+                                                               } else {
+                                                                       // If 
the member already has a list, reuse that list. No need to create a new one. 
+                                                                       object 
list = GetMemberValue (info.Member, ob, isValueList);
+                                                                       if 
(list == null) {
+                                                                               
list = CreateList (info.MappedType.TypeData.Type);
+                                                                               
SetMemberValue (info.Member, ob, list, isValueList);
+                                                                       }
+                                                                       
ReadListElement (info.MappedType, info.IsNullable, list, true);
+                                                               }
                                                        }
                                                        
readFlag[info.Member.Index] = true;
                                                }
@@ -615,7 +628,7 @@
                        Type listType = typeMap.TypeData.Type;
                        ListMap listMap = (ListMap)typeMap.ObjectMap;
 
-                       if (ReadNull()) return null;
+                       if (listType.IsArray && ReadNull()) return null;
 
                        if (list == null) {
                                if (canCreateInstance && 
typeMap.TypeData.HasPublicConstructor) list = CreateList (listType);
@@ -625,7 +638,7 @@
                        if (Reader.IsEmptyElement) {
                                Reader.Skip();
                                if (listType.IsArray)
-                                       list = ShrinkArray ((Array)list, 0, 
listType.GetElementType(), isNullable);
+                                       list = ShrinkArray ((Array)list, 0, 
listType.GetElementType(), false);
                                return list;
                        }
 
@@ -651,7 +664,7 @@
                        ReadEndElement();
 
                        if (listType.IsArray)
-                               list = ShrinkArray ((Array)list, index, 
listType.GetElementType(), isNullable);
+                               list = ShrinkArray ((Array)list, index, 
listType.GetElementType(), false);
 
                        return list;
                }

Modified: trunk/mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog
===================================================================
--- trunk/mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog  
2005-12-05 20:32:13 UTC (rev 53948)
+++ trunk/mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog  
2005-12-05 20:44:42 UTC (rev 53949)
@@ -1,3 +1,8 @@
+2005-12-05  Lluis Sanchez Gual  <[EMAIL PROTECTED]> 
+
+       * XmlSerializerTestClasses.cs:
+       * DeserializeTests.cs: Added tests for bug #76808 and related behavior.
+
 2005-12-01  Lluis Sanchez Gual  <[EMAIL PROTECTED]> 
 
        * XmlSerializerTestClasses.cs:

Modified: 
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/DeserializeTests.cs
===================================================================
--- 
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/DeserializeTests.cs    
    2005-12-05 20:32:13 UTC (rev 53948)
+++ 
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/DeserializeTests.cs    
    2005-12-05 20:44:42 UTC (rev 53949)
@@ -237,5 +237,341 @@
                        Assertion.AssertEquals ("v.ItemsElementName[0]", 
ItemsChoiceType.In, v.ItemsElementName[0]);
                        Assertion.AssertEquals ("v.ItemsElementName[1]", 
ItemsChoiceType.Es, v.ItemsElementName[1]);
                }
+               
+               [Test]
+               public void TestDeserializeCollection ()
+               {
+                       string s0 = "";
+                       s0+="   <ArrayOfEntity 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s0+="           <Entity Name='node1'/>";
+                       s0+="           <Entity Name='node2'/>";
+                       s0+="   </ArrayOfEntity>";
+                       
+                       EntityCollection col = (EntityCollection) Deserialize 
(typeof(EntityCollection), s0);
+               Assertion.AssertNotNull ("col", col);
+                       Assertion.AssertEquals ("col.Count", 2, col.Count);
+               Assertion.AssertNull ("col[0]", col[0].Parent);
+               Assertion.AssertNull ("col[1]", col[1].Parent);
+               }
+               
+               [Test]
+               public void TestDeserializeEmptyCollection ()
+               {
+                       string s1 = "";
+                       s1+="   <ArrayOfEntity 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
+                       
+                       EntityCollection col = (EntityCollection) Deserialize 
(typeof(EntityCollection), s1);
+               Assertion.AssertNotNull ("col", col);
+                       Assertion.AssertEquals ("col.Count", 0, col.Count);
+                       
+                       string s1_1 = "";
+                       s1_1+=" <ArrayOfEntity 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s1_1+=" </ArrayOfEntity>";
+                       
+                       col = (EntityCollection) Deserialize 
(typeof(EntityCollection), s1_1);
+               Assertion.AssertNotNull ("col", col);
+                       Assertion.AssertEquals ("col.Count", 0, col.Count);
+               }
+               
+               [Test]
+               public void TestDeserializeNilCollectionIsNotNull ()
+               {
+                       string s2 = "";
+                       s2+="   <ArrayOfEntity 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/>";
+                       
+                       EntityCollection col = (EntityCollection) Deserialize 
(typeof(EntityCollection), s2);
+               Assertion.AssertNotNull ("col", col);
+                       Assertion.AssertEquals ("col.Count", 0, col.Count);
+               }
+               
+               [Test]
+               public void TestDeserializeObjectCollections ()
+               {
+                       string s3 = "";
+                       s3+="<Container 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s3+="   <Collection1 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s3+="           <Entity Name='node1'/>";
+                       s3+="           <Entity Name='node2'/>";
+                       s3+="   </Collection1>";
+                       s3+="   <Collection2 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s3+="           <Entity Name='node1'/>";
+                       s3+="           <Entity Name='node2'/>";
+                       s3+="   </Collection2>";
+                       s3+="   <Collection3 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s3+="           <Entity Name='node1'/>";
+                       s3+="           <Entity Name='node2'/>";
+                       s3+="   </Collection3>";
+                       s3+="   <Collection4 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s3+="           <Entity Name='node1'/>";
+                       s3+="           <Entity Name='node2'/>";
+                       s3+="   </Collection4>";
+                       s3+="</Container>";
+                       
+                       EntityContainer cont = (EntityContainer) Deserialize 
(typeof(EntityContainer), s3);
+               Assertion.AssertNotNull ("cont", cont);
+               
+               Assertion.AssertNotNull ("cont.Collection1", cont.Collection1);
+                       Assertion.AssertEquals ("cont.Collection1", 2, 
cont.Collection1.Count);
+                       Assertion.AssertEquals ("cont.Collection1.Container", 
"assigned", cont.Collection1.Container);
+                       Assertion.AssertEquals ("cont.Collection1[0].Parent", 
"assigned", cont.Collection1[0].Parent);
+                       Assertion.AssertEquals ("cont.Collection1[1].Parent", 
"assigned", cont.Collection1[1].Parent);
+                       
+               Assertion.AssertNotNull ("cont.Collection2", cont.Collection2);
+                       Assertion.AssertEquals ("cont.Collection2", 2, 
cont.Collection2.Count);
+                       Assertion.AssertEquals ("cont.Collection2.Container", 
"assigned", cont.Collection2.Container);
+                       Assertion.AssertEquals ("cont.Collection2[0].Parent", 
"assigned", cont.Collection2[0].Parent);
+                       Assertion.AssertEquals ("cont.Collection2[1].Parent", 
"assigned", cont.Collection2[1].Parent);
+                       
+               Assertion.AssertNotNull ("cont.Collection3", cont.Collection3);
+                       Assertion.AssertEquals ("cont.Collection3", 2, 
cont.Collection3.Count);
+                       Assertion.AssertEquals ("cont.Collection3.Container", 
"root", cont.Collection3.Container);
+                       Assertion.AssertEquals ("cont.Collection3[0].Parent", 
"root", cont.Collection3[0].Parent);
+                       Assertion.AssertEquals ("cont.Collection3[1].Parent", 
"root", cont.Collection3[1].Parent);
+                       
+               Assertion.AssertNotNull ("cont.Collection4", cont.Collection4);
+                       Assertion.AssertEquals ("cont.Collection4", 2, 
cont.Collection4.Count);
+                       Assertion.AssertEquals ("cont.Collection4.Container", 
"root", cont.Collection4.Container);
+                       Assertion.AssertEquals ("cont.Collection4[0].Parent", 
"root", cont.Collection4[0].Parent);
+                       Assertion.AssertEquals ("cont.Collection4[1].Parent", 
"root", cont.Collection4[1].Parent);
+               }
+               
+               [Test]
+               public void TestDeserializeEmptyObjectCollections ()
+               {
+                       string s4 = "";
+                       s4+="<Container 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s4+="</Container>";
+                       
+                       EntityContainer cont = (EntityContainer) Deserialize 
(typeof(EntityContainer), s4);
+               Assertion.AssertNotNull ("cont", cont);
+               
+               Assertion.AssertNotNull ("cont.Collection1", cont.Collection1);
+                       Assertion.AssertEquals ("cont.Collection1", 0, 
cont.Collection1.Count);
+                       Assertion.AssertEquals ("cont.Collection1.Container", 
"assigned", cont.Collection1.Container);
+                       
+               Assertion.AssertNotNull ("cont.Collection2", cont.Collection2);
+                       Assertion.AssertEquals ("cont.Collection2", 0, 
cont.Collection2.Count);
+                       Assertion.AssertEquals ("cont.Collection2.Container", 
"assigned", cont.Collection2.Container);
+                       
+               Assertion.AssertNotNull ("cont.Collection3", cont.Collection3);
+                       Assertion.AssertEquals ("cont.Collection3", 0, 
cont.Collection3.Count);
+                       Assertion.AssertEquals ("cont.Collection3.Container", 
"root", cont.Collection3.Container);
+                       
+               Assertion.AssertNotNull ("cont.Collection4", cont.Collection4);
+                       Assertion.AssertEquals ("cont.Collection4", 0, 
cont.Collection4.Count);
+                       Assertion.AssertEquals ("cont.Collection4.Container", 
"root", cont.Collection4.Container);
+               }
+               
+               [Test]
+               public void TestDeserializeObjectNilCollectionsAreNotNull ()
+               {
+                       string s5 = "";
+                       s5+="<Container 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s5+="   <Collection1 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
+                       s5+="   <Collection2 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
+                       s5+="   <Collection3 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
+                       s5+="   <Collection4 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
+                       s5+="</Container>";
+                       
+                       EntityContainer cont = (EntityContainer) Deserialize 
(typeof(EntityContainer), s5);
+               Assertion.AssertNotNull ("cont", cont);
+               
+               Assertion.AssertNotNull ("cont.Collection1", cont.Collection1);
+                       Assertion.AssertEquals ("cont.Collection1", 0, 
cont.Collection1.Count);
+                       Assertion.AssertEquals ("cont.Collection1.Container", 
"assigned", cont.Collection1.Container);
+                       
+               Assertion.AssertNotNull ("cont.Collection2", cont.Collection2);
+                       Assertion.AssertEquals ("cont.Collection2", 0, 
cont.Collection2.Count);
+                       Assertion.AssertEquals ("cont.Collection2.Container", 
"assigned", cont.Collection2.Container);
+                       
+               Assertion.AssertNotNull ("cont.Collection3", cont.Collection3);
+                       Assertion.AssertEquals ("cont.Collection3", 0, 
cont.Collection3.Count);
+                       Assertion.AssertEquals ("cont.Collection3.Container", 
"root", cont.Collection3.Container);
+                       
+               Assertion.AssertNotNull ("cont.Collection4", cont.Collection4);
+                       Assertion.AssertEquals ("cont.Collection4", 0, 
cont.Collection4.Count);
+                       Assertion.AssertEquals ("cont.Collection4.Container", 
"root", cont.Collection4.Container);
+               }
+               
+               [Test]
+               public void TestDeserializeObjectEmptyCollections ()
+               {
+                       string s6 = "";
+                       s6+="<Container 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s6+="   <Collection1 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
+                       s6+="   <Collection2 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
+                       s6+="   <Collection3 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
+                       s6+="   <Collection4 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
+                       s6+="</Container>";
+                       
+                       EntityContainer cont = (EntityContainer) Deserialize 
(typeof(EntityContainer), s6);
+               Assertion.AssertNotNull ("cont", cont);
+               
+               Assertion.AssertNotNull ("cont.Collection1", cont.Collection1);
+                       Assertion.AssertEquals ("cont.Collection1", 0, 
cont.Collection1.Count);
+                       Assertion.AssertEquals ("cont.Collection1.Container", 
"assigned", cont.Collection1.Container);
+                       
+               Assertion.AssertNotNull ("cont.Collection2", cont.Collection2);
+                       Assertion.AssertEquals ("cont.Collection2", 0, 
cont.Collection2.Count);
+                       Assertion.AssertEquals ("cont.Collection2.Container", 
"assigned", cont.Collection2.Container);
+                       
+               Assertion.AssertNotNull ("cont.Collection3", cont.Collection3);
+                       Assertion.AssertEquals ("cont.Collection3", 0, 
cont.Collection3.Count);
+                       Assertion.AssertEquals ("cont.Collection3.Container", 
"root", cont.Collection3.Container);
+                       
+               Assertion.AssertNotNull ("cont.Collection4", cont.Collection4);
+                       Assertion.AssertEquals ("cont.Collection4", 0, 
cont.Collection4.Count);
+                       Assertion.AssertEquals ("cont.Collection4.Container", 
"root", cont.Collection4.Container);
+               }
+               
+               [Test]
+               public void TestDeserializeObjectEmptyArrays ()
+               {
+                       string s6 = "";
+                       s6+="<Container 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s6+="   <Collection1 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
+                       s6+="   <Collection2 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
+                       s6+="   <Collection3 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
+                       s6+="   <Collection4 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
+                       s6+="</Container>";
+                       
+                       ArrayEntityContainer cont = (ArrayEntityContainer) 
Deserialize (typeof(ArrayEntityContainer), s6);
+               Assertion.AssertNotNull ("cont", cont);
+               
+               Assertion.AssertNotNull ("cont.Collection1", cont.Collection1);
+                       Assertion.AssertEquals ("cont.Collection1.Length", 0, 
cont.Collection1.Length);
+                       
+               Assertion.AssertNotNull ("cont.Collection2", cont.Collection2);
+                       Assertion.AssertEquals ("cont.Collection2.Length", 0, 
cont.Collection2.Length);
+                       
+               Assertion.AssertNotNull ("cont.Collection3", cont.Collection3);
+                       Assertion.AssertEquals ("cont.Collection3.Length", 0, 
cont.Collection3.Length);
+                       
+               Assertion.AssertNotNull ("cont.Collection4", cont.Collection4);
+                       Assertion.AssertEquals ("cont.Collection4.Length", 0, 
cont.Collection4.Length);
+               }
+               
+               [Test]
+               public void TestDeserializeEmptyObjectArrays ()
+               {
+                       string s4 = "";
+                       s4+="<Container 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s4+="</Container>";
+                       
+                       ArrayEntityContainer cont = (ArrayEntityContainer) 
Deserialize (typeof(ArrayEntityContainer), s4);
+               Assertion.AssertNotNull ("cont", cont);
+               
+               Assertion.AssertNull ("cont.Collection1", cont.Collection1);
+               Assertion.AssertNull ("cont.Collection2", cont.Collection2);
+                       
+               Assertion.AssertNotNull ("cont.Collection3", cont.Collection3);
+                       Assertion.AssertEquals ("cont.Collection3.Length", 0, 
cont.Collection3.Length);
+                       
+               Assertion.AssertNotNull ("cont.Collection4", cont.Collection4);
+                       Assertion.AssertEquals ("cont.Collection4.Length", 0, 
cont.Collection4.Length);
+               }
+               
+               [Test]
+               public void TestDeserializeObjectNilArrays ()
+               {
+                       string s5 = "";
+                       s5+="<Container 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s5+="   <Collection1 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
+                       s5+="   <Collection2 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
+                       s5+="   <Collection3 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
+                       s5+="   <Collection4 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
+                       s5+="</Container>";
+                       
+                       ArrayEntityContainer cont = (ArrayEntityContainer) 
Deserialize (typeof(ArrayEntityContainer), s5);
+               Assertion.AssertNotNull ("cont", cont);
+               
+               Assertion.AssertNull ("cont.Collection1", cont.Collection1);
+               Assertion.AssertNull ("cont.Collection2", cont.Collection2);
+               Assertion.AssertNull ("cont.Collection3", cont.Collection3);
+                       
+               Assertion.AssertNotNull ("cont.Collection4", cont.Collection4);
+                       Assertion.AssertEquals ("cont.Collection4.Length", 0, 
cont.Collection4.Length);
+               }
+               
+               [Test]
+               public void TestDeserializeEmptyArray ()
+               {
+                       string s1 = "";
+                       s1+="<ArrayOfEntity 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
+                       
+                       Entity[] col = (Entity[]) Deserialize 
(typeof(Entity[]), s1);
+               Assertion.AssertNotNull ("col", col);
+                       Assertion.AssertEquals ("col.Length", 0, col.Length);
+                       
+                       string s1_1 = "";
+                       s1_1+=" <ArrayOfEntity 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s1_1+=" </ArrayOfEntity>";
+                       
+                       col = (Entity[]) Deserialize (typeof(Entity[]), s1_1);
+               Assertion.AssertNotNull ("col", col);
+                       Assertion.AssertEquals ("col.Length", 0, col.Length);
+               }
+               
+               [Test]
+               public void TestDeserializeNilArray ()
+               {
+                       string s2 = "";
+                       s2 += "<ArrayOfEntity 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/>";
+                       
+                       Entity[] col = (Entity[]) Deserialize 
(typeof(Entity[]), s2);
+               Assertion.AssertNull ("col", col);
+               }
+               
+               [Test]
+               public void TestDeserializeObjectWithReadonlyCollection ()
+               {
+                       string s3 = "";
+                       s3+="<Container 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s3+="   <Collection1>";
+                       s3+="           <Entity Name='node1'/>";
+                       s3+="           <Entity Name='node2'/>";
+                       s3+="   </Collection1>";
+                       s3+="</Container>";
+                       
+                       ObjectWithReadonlyCollection cont = 
(ObjectWithReadonlyCollection) Deserialize 
(typeof(ObjectWithReadonlyCollection), s3);
+               Assertion.AssertNotNull ("cont", cont);
+               Assertion.AssertNotNull ("cont.Collection1", cont.Collection1);
+                       Assertion.AssertEquals ("cont.Collection1.Count", 2, 
cont.Collection1.Count);
+                       Assertion.AssertEquals ("cont.Collection1.Container", 
"root", cont.Collection1.Container);
+                       Assertion.AssertEquals ("cont.Collection1[0].Parent", 
"root", cont.Collection1[0].Parent);
+                       Assertion.AssertEquals ("cont.Collection1[1].Parent", 
"root", cont.Collection1[1].Parent);
+               }
+               
+               [Test]
+               [ExpectedException (typeof(InvalidOperationException))]
+               public void TestDeserializeObjectWithReadonlyNulCollection ()
+               {
+                       string s3 = "";
+                       s3+="<Container 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s3+="   <Collection1>";
+                       s3+="           <Entity Name='node1'/>";
+                       s3+="           <Entity Name='node2'/>";
+                       s3+="   </Collection1>";
+                       s3+="</Container>";
+                       
+                       Deserialize (typeof(ObjectWithReadonlyNulCollection), 
s3);
+               }
+               
+               [Test]
+               public void TestDeserializeObjectWithReadonlyArray ()
+               {
+                       string s3 = "";
+                       s3+="<Container 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+                       s3+="   <Collection1>";
+                       s3+="           <Entity Name='node1'/>";
+                       s3+="           <Entity Name='node2'/>";
+                       s3+="   </Collection1>";
+                       s3+="</Container>";
+                       
+                       ObjectWithReadonlyArray cont = 
(ObjectWithReadonlyArray) Deserialize (typeof(ObjectWithReadonlyArray), s3);
+               Assertion.AssertNotNull ("cont", cont);
+               Assertion.AssertNotNull ("cont.Collection1", cont.Collection1);
+                       Assertion.AssertEquals ("cont.Collection1.Length", 0, 
cont.Collection1.Length);
+               }
        }
 }

Modified: 
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTestClasses.cs
===================================================================
--- 
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTestClasses.cs
        2005-12-05 20:32:13 UTC (rev 53948)
+++ 
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTestClasses.cs
        2005-12-05 20:44:42 UTC (rev 53949)
@@ -284,5 +284,152 @@
                [XmlAttribute (DataType="int")]
                public string[] at = new string [] { "a","b" };
        }
+       
+       [XmlType ("Container")]
+       public class EntityContainer
+       {
+               EntityCollection collection1;
+               EntityCollection collection2;
+               EntityCollection collection3 = new EntityCollection ("root");
+               EntityCollection collection4 = new EntityCollection ("root");
+               
+               [XmlArray (IsNullable=true)]
+               public EntityCollection Collection1 {
+                       get { return collection1; }
+                       set { collection1 = value; collection1.Container = 
"assigned"; }
+               }
+               
+               [XmlArray (IsNullable=false)]
+               public EntityCollection Collection2 {
+                       get { return collection2; }
+                       set { collection2 = value; collection2.Container = 
"assigned"; }
+               }
+               
+               [XmlArray (IsNullable=true)]
+               public EntityCollection Collection3 {
+                       get { return collection3; }
+                       set { collection3 = value; collection3.Container = 
"assigned"; }
+               }
+               
+               [XmlArray (IsNullable=false)]
+               public EntityCollection Collection4 {
+                       get { return collection4; }
+                       set { collection4 = value; collection4.Container = 
"assigned"; }
+               }
+       }
+       
+       [XmlType ("Container")]
+       public class ArrayEntityContainer
+       {
+               Entity[] collection1;
+               Entity[] collection2;
+               Entity[] collection3 = new Entity [0];
+               Entity[] collection4 = new Entity [0];
+               
+               [XmlArray (IsNullable=true)]
+               public Entity[] Collection1 {
+                       get { return collection1; }
+                       set { collection1 = value; }
+               }
+               
+               [XmlArray (IsNullable=false)]
+               public Entity[] Collection2 {
+                       get { return collection2; }
+                       set { collection2 = value; }
+               }
+               
+               [XmlArray (IsNullable=true)]
+               public Entity[] Collection3 {
+                       get { return collection3; }
+                       set { collection3 = value; }
+               }
+               
+               [XmlArray (IsNullable=false)]
+               public Entity[] Collection4 {
+                       get { return collection4; }
+                       set { collection4 = value; }
+               }
+       }
+       
+       public class Entity
+       {
+               private string _name = string.Empty;
+               private string _parent = null;
+
+               [XmlAttribute]
+               public string Name {
+                       get { return _name; }
+                       set { _name = value; }
+               }
+
+               [XmlIgnore] 
+               public string Parent {
+                       get { return _parent; }
+                       set { _parent = value; }
+               }
+       }
+
+       public class EntityCollection : ArrayList
+       {
+               public string _container;
+
+               public EntityCollection ()
+               {
+               }
+
+               public EntityCollection (string c)
+               {
+                       _container = c;
+               }
+
+               public string Container {
+                       get { return _container; }
+                       set { _container = value; }
+               }
+
+               public int Add (Entity value)
+               {
+                       if(_container != null)
+                               value.Parent = _container;
+
+                       return base.Add(value);
+               }
+
+               public new Entity this[int index]
+               {
+                       get { return (Entity) base[index]; }
+                       set { base[index] = value; }
+               }
+       }
+       
+       [XmlType ("Container")]
+       public class ObjectWithReadonlyCollection
+       {
+               EntityCollection collection1 = new EntityCollection ("root");
+               
+               public EntityCollection Collection1 {
+                       get { return collection1; }
+               }
+       }
+       
+       [XmlType ("Container")]
+       public class ObjectWithReadonlyNulCollection
+       {
+               EntityCollection collection1;
+               
+               public EntityCollection Collection1 {
+                       get { return collection1; }
+               }
+       }
+       
+       [XmlType ("Container")]
+       public class ObjectWithReadonlyArray
+       {
+               Entity[] collection1 = new Entity [0];
+               
+               public Entity[] Collection1 {
+                       get { return collection1; }
+               }
+       }
 }
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to