public CompositeOuter(CompositeInner inner)
 {
     Inner = inner;
 }
        public void TestCompositeArray()
        {
            // 1. Test simple array.
            CompositeInner[] inArr = { new CompositeInner(1) };

            IPortableObject portObj = _grid.Portables().Builder(typeof(CompositeArray)).HashCode(100)
                .SetField("inArr", inArr).Build();

            IPortableMetadata meta = portObj.Metadata();

            Assert.AreEqual(typeof(CompositeArray).Name, meta.TypeName);
            Assert.AreEqual(1, meta.Fields.Count);
            Assert.AreEqual(PortableTypeNames.TypeNameArrayObject, meta.FieldTypeName("inArr"));

            Assert.AreEqual(100, portObj.GetHashCode());

            IPortableObject[] portInArr = portObj.Field<IPortableObject[]>("inArr");

            Assert.AreEqual(1, portInArr.Length);
            Assert.AreEqual(1, portInArr[0].Field<int>("val"));

            CompositeArray arr = portObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.OutArr);
            Assert.AreEqual(1, arr.InArr.Length);
            Assert.AreEqual(1, arr.InArr[0].Val);

            // 2. Test addition to array.
            portInArr = new[] { portInArr[0], null };

            portObj = _grid.Portables().Builder(portObj).HashCode(200)
                .SetField("inArr", portInArr).Build();

            Assert.AreEqual(200, portObj.GetHashCode());

            portInArr = portObj.Field<IPortableObject[]>("inArr");

            Assert.AreEqual(2, portInArr.Length);
            Assert.AreEqual(1, portInArr[0].Field<int>("val"));
            Assert.IsNull(portInArr[1]);

            arr = portObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.OutArr);
            Assert.AreEqual(2, arr.InArr.Length);
            Assert.AreEqual(1, arr.InArr[0].Val);
            Assert.IsNull(arr.InArr[1]);

            portInArr[1] = _grid.Portables().Builder(typeof(CompositeInner)).SetField("val", 2).Build();

            portObj = _grid.Portables().Builder(portObj).HashCode(300)
                .SetField("inArr", portInArr).Build();

            Assert.AreEqual(300, portObj.GetHashCode());

            portInArr = portObj.Field<IPortableObject[]>("inArr");

            Assert.AreEqual(2, portInArr.Length);
            Assert.AreEqual(1, portInArr[0].Field<int>("val"));
            Assert.AreEqual(2, portInArr[1].Field<int>("val"));

            arr = portObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.OutArr);
            Assert.AreEqual(2, arr.InArr.Length);
            Assert.AreEqual(1, arr.InArr[0].Val);
            Assert.AreEqual(2, arr.InArr[1].Val);

            // 3. Test top-level handle inversion.
            CompositeInner inner = new CompositeInner(1);

            inArr = new[] { inner, inner };

            portObj = _grid.Portables().Builder(typeof(CompositeArray)).HashCode(100)
                .SetField("inArr", inArr).Build();

            Assert.AreEqual(100, portObj.GetHashCode());

            portInArr = portObj.Field<IPortableObject[]>("inArr");

            Assert.AreEqual(2, portInArr.Length);
            Assert.AreEqual(1, portInArr[0].Field<int>("val"));
            Assert.AreEqual(1, portInArr[1].Field<int>("val"));

            arr = portObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.OutArr);
            Assert.AreEqual(2, arr.InArr.Length);
            Assert.AreEqual(1, arr.InArr[0].Val);
            Assert.AreEqual(1, arr.InArr[1].Val);

            portInArr[0] = _grid.Portables().Builder(typeof(CompositeInner)).SetField("val", 2).Build();

            portObj = _grid.Portables().Builder(portObj).HashCode(200)
                .SetField("inArr", portInArr).Build();

            Assert.AreEqual(200, portObj.GetHashCode());

            portInArr = portObj.Field<IPortableObject[]>("inArr");

            Assert.AreEqual(2, portInArr.Length);
            Assert.AreEqual(2, portInArr[0].Field<int>("val"));
            Assert.AreEqual(1, portInArr[1].Field<int>("val"));

            arr = portObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.OutArr);
            Assert.AreEqual(2, arr.InArr.Length);
            Assert.AreEqual(2, arr.InArr[0].Val);
            Assert.AreEqual(1, arr.InArr[1].Val);

            // 4. Test nested object handle inversion.
            CompositeOuter[] outArr = { new CompositeOuter(inner), new CompositeOuter(inner) };

            portObj = _grid.Portables().Builder(typeof(CompositeArray)).HashCode(100)
                .SetField("outArr", outArr).Build();

            meta = portObj.Metadata();

            Assert.AreEqual(typeof(CompositeArray).Name, meta.TypeName);
            Assert.AreEqual(2, meta.Fields.Count);
            Assert.AreEqual(PortableTypeNames.TypeNameArrayObject, meta.FieldTypeName("inArr"));
            Assert.AreEqual(PortableTypeNames.TypeNameArrayObject, meta.FieldTypeName("outArr"));

            Assert.AreEqual(100, portObj.GetHashCode());

            IPortableObject[] portOutArr = portObj.Field<IPortableObject[]>("outArr");

            Assert.AreEqual(2, portOutArr.Length);
            Assert.AreEqual(1, portOutArr[0].Field<IPortableObject>("inner").Field<int>("val"));
            Assert.AreEqual(1, portOutArr[1].Field<IPortableObject>("inner").Field<int>("val"));

            arr = portObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.InArr);
            Assert.AreEqual(2, arr.OutArr.Length);
            Assert.AreEqual(1, arr.OutArr[0].Inner.Val);
            Assert.AreEqual(1, arr.OutArr[1].Inner.Val);

            portOutArr[0] = _grid.Portables().Builder(typeof(CompositeOuter))
                .SetField("inner", new CompositeInner(2)).Build();

            portObj = _grid.Portables().Builder(portObj).HashCode(200)
                .SetField("outArr", portOutArr).Build();

            Assert.AreEqual(200, portObj.GetHashCode());

            portInArr = portObj.Field<IPortableObject[]>("outArr");

            Assert.AreEqual(2, portInArr.Length);
            Assert.AreEqual(2, portOutArr[0].Field<IPortableObject>("inner").Field<int>("val"));
            Assert.AreEqual(1, portOutArr[1].Field<IPortableObject>("inner").Field<int>("val"));

            arr = portObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.InArr);
            Assert.AreEqual(2, arr.OutArr.Length);
            Assert.AreEqual(2, arr.OutArr[0].Inner.Val);
            Assert.AreEqual(1, arr.OutArr[1].Inner.Val);
        }
        public void TestCompositeContainer()
        {
            ArrayList col = new ArrayList();
            ICollection<CompositeInner> gCol = new List<CompositeInner>();
            IDictionary dict = new Hashtable();
            IDictionary<int, CompositeInner> gDict = new Dictionary<int, CompositeInner>();

            col.Add(new CompositeInner(1));
            gCol.Add(new CompositeInner(2));
            dict[3] = new CompositeInner(3);
            gDict[4] = new CompositeInner(4);

            IPortableObject portObj = _grid.Portables().Builder(typeof(CompositeContainer)).HashCode(100)
                .SetField<ICollection>("col", col)
                .SetField("gCol", gCol)
                .SetField("dict", dict)
                .SetField("gDict", gDict).Build();

            // 1. Check meta.
            IPortableMetadata meta = portObj.Metadata();

            Assert.AreEqual(typeof(CompositeContainer).Name, meta.TypeName);

            Assert.AreEqual(4, meta.Fields.Count);
            Assert.AreEqual(PortableTypeNames.TypeNameCollection, meta.FieldTypeName("col"));
            Assert.AreEqual(PortableTypeNames.TypeNameCollection, meta.FieldTypeName("gCol"));
            Assert.AreEqual(PortableTypeNames.TypeNameMap, meta.FieldTypeName("dict"));
            Assert.AreEqual(PortableTypeNames.TypeNameMap, meta.FieldTypeName("gDict"));

            // 2. Check in portable form.
            Assert.AreEqual(1, portObj.Field<ICollection>("col").Count);
            Assert.AreEqual(1, portObj.Field<ICollection>("col").OfType<IPortableObject>().First()
                .Field<int>("val"));

            Assert.AreEqual(1, portObj.Field<ICollection<IPortableObject>>("gCol").Count);
            Assert.AreEqual(2, portObj.Field<ICollection<IPortableObject>>("gCol").First().Field<int>("val"));

            Assert.AreEqual(1, portObj.Field<IDictionary>("dict").Count);
            Assert.AreEqual(3, ((IPortableObject) portObj.Field<IDictionary>("dict")[3]).Field<int>("val"));

            Assert.AreEqual(1, portObj.Field<IDictionary<int, IPortableObject>>("gDict").Count);
            Assert.AreEqual(4, portObj.Field<IDictionary<int, IPortableObject>>("gDict")[4].Field<int>("val"));

            // 3. Check in deserialized form.
            CompositeContainer obj = portObj.Deserialize<CompositeContainer>();

            Assert.AreEqual(1, obj.Col.Count);
            Assert.AreEqual(1, obj.Col.OfType<CompositeInner>().First().Val);

            Assert.AreEqual(1, obj.GCol.Count);
            Assert.AreEqual(2, obj.GCol.First().Val);

            Assert.AreEqual(1, obj.Dict.Count);
            Assert.AreEqual(3, ((CompositeInner) obj.Dict[3]).Val);

            Assert.AreEqual(1, obj.GDict.Count);
            Assert.AreEqual(4, obj.GDict[4].Val);
        }