public void T041_UserAppearancePersistency()
        {
            AvatarAppearance appear = new AvatarAppearance();
            UUID             owner  = UUID.Random();
            int serial = random.Next();

            byte[] visualp = new byte[218];
            random.NextBytes(visualp);
            UUID bodyitem    = UUID.Random();
            UUID bodyasset   = UUID.Random();
            UUID skinitem    = UUID.Random();
            UUID skinasset   = UUID.Random();
            UUID hairitem    = UUID.Random();
            UUID hairasset   = UUID.Random();
            UUID eyesitem    = UUID.Random();
            UUID eyesasset   = UUID.Random();
            UUID shirtitem   = UUID.Random();
            UUID shirtasset  = UUID.Random();
            UUID pantsitem   = UUID.Random();
            UUID pantsasset  = UUID.Random();
            UUID shoesitem   = UUID.Random();
            UUID shoesasset  = UUID.Random();
            UUID socksitem   = UUID.Random();
            UUID socksasset  = UUID.Random();
            UUID jacketitem  = UUID.Random();
            UUID jacketasset = UUID.Random();
            UUID glovesitem  = UUID.Random();
            UUID glovesasset = UUID.Random();
            UUID ushirtitem  = UUID.Random();
            UUID ushirtasset = UUID.Random();
            UUID upantsitem  = UUID.Random();
            UUID upantsasset = UUID.Random();
            UUID skirtitem   = UUID.Random();
            UUID skirtasset  = UUID.Random();

            Primitive.TextureEntry texture = AvatarAppearance.GetDefaultTexture();
            float avatarheight             = (float)(Math.Round(random.NextDouble(), 5));

            appear.Owner           = owner;
            appear.Serial          = serial;
            appear.VisualParams    = visualp;
            appear.BodyItem        = bodyitem;
            appear.BodyAsset       = bodyasset;
            appear.SkinItem        = skinitem;
            appear.SkinAsset       = skinasset;
            appear.HairItem        = hairitem;
            appear.HairAsset       = hairasset;
            appear.EyesItem        = eyesitem;
            appear.EyesAsset       = eyesasset;
            appear.ShirtItem       = shirtitem;
            appear.ShirtAsset      = shirtasset;
            appear.PantsItem       = pantsitem;
            appear.PantsAsset      = pantsasset;
            appear.ShoesItem       = shoesitem;
            appear.ShoesAsset      = shoesasset;
            appear.SocksItem       = socksitem;
            appear.SocksAsset      = socksasset;
            appear.JacketItem      = jacketitem;
            appear.JacketAsset     = jacketasset;
            appear.GlovesItem      = glovesitem;
            appear.GlovesAsset     = glovesasset;
            appear.UnderShirtItem  = ushirtitem;
            appear.UnderShirtAsset = ushirtasset;
            appear.UnderPantsItem  = upantsitem;
            appear.UnderPantsAsset = upantsasset;
            appear.SkirtItem       = skirtitem;
            appear.SkirtAsset      = skirtasset;
            appear.Texture         = texture;
            appear.AvatarHeight    = avatarheight;

            db.UpdateUserAppearance(owner, appear);
            AvatarAppearance app = db.GetUserAppearance(owner);

            Assert.That(owner, Is.EqualTo(app.Owner), "Assert.That(owner,Is.EqualTo(app.Owner))");
            Assert.That(serial, Is.EqualTo(app.Serial), "Assert.That(serial,Is.EqualTo(app.Serial))");
            Assert.That(visualp, Is.EqualTo(app.VisualParams), "Assert.That(visualp,Is.EqualTo(app.VisualParams))");
            Assert.That(bodyitem, Is.EqualTo(app.BodyItem), "Assert.That(bodyitem,Is.EqualTo(app.BodyItem))");
            Assert.That(bodyasset, Is.EqualTo(app.BodyAsset), "Assert.That(bodyasset,Is.EqualTo(app.BodyAsset))");
            Assert.That(skinitem, Is.EqualTo(app.SkinItem), "Assert.That(skinitem,Is.EqualTo(app.SkinItem))");
            Assert.That(skinasset, Is.EqualTo(app.SkinAsset), "Assert.That(skinasset,Is.EqualTo(app.SkinAsset))");
            Assert.That(hairitem, Is.EqualTo(app.HairItem), "Assert.That(hairitem,Is.EqualTo(app.HairItem))");
            Assert.That(hairasset, Is.EqualTo(app.HairAsset), "Assert.That(hairasset,Is.EqualTo(app.HairAsset))");
            Assert.That(eyesitem, Is.EqualTo(app.EyesItem), "Assert.That(eyesitem,Is.EqualTo(app.EyesItem))");
            Assert.That(eyesasset, Is.EqualTo(app.EyesAsset), "Assert.That(eyesasset,Is.EqualTo(app.EyesAsset))");
            Assert.That(shirtitem, Is.EqualTo(app.ShirtItem), "Assert.That(shirtitem,Is.EqualTo(app.ShirtItem))");
            Assert.That(shirtasset, Is.EqualTo(app.ShirtAsset), "Assert.That(shirtasset,Is.EqualTo(app.ShirtAsset))");
            Assert.That(pantsitem, Is.EqualTo(app.PantsItem), "Assert.That(pantsitem,Is.EqualTo(app.PantsItem))");
            Assert.That(pantsasset, Is.EqualTo(app.PantsAsset), "Assert.That(pantsasset,Is.EqualTo(app.PantsAsset))");
            Assert.That(shoesitem, Is.EqualTo(app.ShoesItem), "Assert.That(shoesitem,Is.EqualTo(app.ShoesItem))");
            Assert.That(shoesasset, Is.EqualTo(app.ShoesAsset), "Assert.That(shoesasset,Is.EqualTo(app.ShoesAsset))");
            Assert.That(socksitem, Is.EqualTo(app.SocksItem), "Assert.That(socksitem,Is.EqualTo(app.SocksItem))");
            Assert.That(socksasset, Is.EqualTo(app.SocksAsset), "Assert.That(socksasset,Is.EqualTo(app.SocksAsset))");
            Assert.That(jacketitem, Is.EqualTo(app.JacketItem), "Assert.That(jacketitem,Is.EqualTo(app.JacketItem))");
            Assert.That(jacketasset, Is.EqualTo(app.JacketAsset), "Assert.That(jacketasset,Is.EqualTo(app.JacketAsset))");
            Assert.That(glovesitem, Is.EqualTo(app.GlovesItem), "Assert.That(glovesitem,Is.EqualTo(app.GlovesItem))");
            Assert.That(glovesasset, Is.EqualTo(app.GlovesAsset), "Assert.That(glovesasset,Is.EqualTo(app.GlovesAsset))");
            Assert.That(ushirtitem, Is.EqualTo(app.UnderShirtItem), "Assert.That(ushirtitem,Is.EqualTo(app.UnderShirtItem))");
            Assert.That(ushirtasset, Is.EqualTo(app.UnderShirtAsset), "Assert.That(ushirtasset,Is.EqualTo(app.UnderShirtAsset))");
            Assert.That(upantsitem, Is.EqualTo(app.UnderPantsItem), "Assert.That(upantsitem,Is.EqualTo(app.UnderPantsItem))");
            Assert.That(upantsasset, Is.EqualTo(app.UnderPantsAsset), "Assert.That(upantsasset,Is.EqualTo(app.UnderPantsAsset))");
            Assert.That(skirtitem, Is.EqualTo(app.SkirtItem), "Assert.That(skirtitem,Is.EqualTo(app.SkirtItem))");
            Assert.That(skirtasset, Is.EqualTo(app.SkirtAsset), "Assert.That(skirtasset,Is.EqualTo(app.SkirtAsset))");
            Assert.That(texture.ToString(), Is.EqualTo(app.Texture.ToString()), "Assert.That(texture.ToString(),Is.EqualTo(app.Texture.ToString()))");
            Assert.That(avatarheight, Is.EqualTo(app.AvatarHeight), "Assert.That(avatarheight,Is.EqualTo(app.AvatarHeight))");
        }
        /// <summary>
        /// Update what the avatar is wearing using an item from their inventory.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AvatarIsWearing(Object sender, AvatarWearingArgs e)
        {
            IClientAPI    clientView = (IClientAPI)sender;
            ScenePresence avatar     = m_scene.GetScenePresence(clientView.AgentId);

            if (avatar == null)
            {
                m_log.Error("[APPEARANCE]: Avatar is child agent, ignoring AvatarIsWearing event");
                return;
            }

            CachedUserInfo profile = m_scene.CommsManager.UserService.GetUserDetails(clientView.AgentId);

            if (profile != null)
            {
                AvatarAppearance appearance = avatar.Appearance;

                // we need to clean out the existing textures
                appearance.Texture = AvatarAppearance.GetDefaultTexture();

                List <AvatarWearable> wearables = new List <AvatarWearable>();
                lock (_currentlyWaitingCOFBuilds)
                {
                    //Check to see whether the client can manage itself
                    if (_cofSyncEnabled && !_viewer2Users.Contains(clientView.AgentId))
                    {
                        foreach (AvatarWearingArgs.Wearable wear in e.NowWearing)
                        {
                            wearables.Add(new AvatarWearable(wear.Type, wear.ItemID, UUID.Zero));
                            AvatarWearable oldWearable = appearance.GetWearableOfType(wear.Type);
                            if (wear.ItemID != UUID.Zero)
                            {
                                if (oldWearable == null || oldWearable.ItemID == UUID.Zero || wear.ItemID != oldWearable.ItemID)
                                {
                                    bool add = false;
                                    Dictionary <UUID, BuildCOF> waitingCOFs = new Dictionary <UUID, BuildCOF>();
                                    if ((add = !_currentlyWaitingCOFBuilds.TryGetValue(clientView.AgentId, out waitingCOFs)))
                                    {
                                        waitingCOFs = new Dictionary <UUID, BuildCOF>();
                                    }
                                    //Make sure that the new item is added
                                    if (waitingCOFs.ContainsKey(wear.ItemID))
                                    {
                                        BuildCOF cof = waitingCOFs[wear.ItemID];
                                        cof.SetWearableToLookFor(wear.ItemID, m_scene, clientView.AgentId, true);
                                        if (cof.Finished())
                                        {
                                            waitingCOFs.Remove(wear.ItemID);
                                        }
                                    }
                                    else
                                    {
                                        BuildCOF cof = new BuildCOF(ClearWaitingCOF);
                                        cof.SetWearableToLookFor(wear.ItemID, m_scene, clientView.AgentId, true);
                                        waitingCOFs.Add(wear.ItemID, cof);
                                    }
                                    if (add)
                                    {
                                        _currentlyWaitingCOFBuilds.Add(clientView.AgentId, waitingCOFs);
                                    }
                                }
                                if (oldWearable != null && oldWearable.ItemID != UUID.Zero && wear.ItemID != oldWearable.ItemID)
                                {
                                    bool add = false;
                                    Dictionary <UUID, BuildCOF> waitingCOFs = new Dictionary <UUID, BuildCOF>();
                                    if ((add = !_currentlyWaitingCOFBuilds.TryGetValue(clientView.AgentId, out waitingCOFs)))
                                    {
                                        waitingCOFs = new Dictionary <UUID, BuildCOF>();
                                    }
                                    //Check for removal of old item
                                    if (waitingCOFs.ContainsKey(oldWearable.ItemID))
                                    {
                                        BuildCOF cof = waitingCOFs[oldWearable.ItemID];
                                        cof.SetWearableToLookFor(oldWearable.ItemID, m_scene, clientView.AgentId, false);
                                        if (cof.Finished())
                                        {
                                            waitingCOFs.Remove(oldWearable.ItemID);
                                        }
                                    }
                                    else
                                    {
                                        BuildCOF cof = new BuildCOF(ClearWaitingCOF);
                                        cof.SetWearableToLookFor(oldWearable.ItemID, m_scene, clientView.AgentId, false);
                                        waitingCOFs.Add(oldWearable.ItemID, cof);
                                    }
                                    if (add)
                                    {
                                        _currentlyWaitingCOFBuilds.Add(clientView.AgentId, waitingCOFs);
                                    }
                                }
                            }
                            else if (oldWearable != null && oldWearable.ItemID != UUID.Zero)
                            {
                                bool add = false;
                                Dictionary <UUID, BuildCOF> waitingCOFs = new Dictionary <UUID, BuildCOF>();
                                if ((add = !_currentlyWaitingCOFBuilds.TryGetValue(clientView.AgentId, out waitingCOFs)))
                                {
                                    waitingCOFs = new Dictionary <UUID, BuildCOF>();
                                }
                                //Remove the item if it was just removed
                                if (waitingCOFs.ContainsKey(oldWearable.ItemID))
                                {
                                    BuildCOF cof = waitingCOFs[oldWearable.ItemID];
                                    cof.SetWearableToLookFor(oldWearable.ItemID, m_scene, clientView.AgentId, false);
                                    if (cof.Finished())
                                    {
                                        waitingCOFs.Remove(oldWearable.ItemID);
                                    }
                                }
                                else
                                {
                                    BuildCOF cof = new BuildCOF(ClearWaitingCOF);
                                    cof.SetWearableToLookFor(oldWearable.ItemID, m_scene, clientView.AgentId, false);
                                    waitingCOFs.Add(oldWearable.ItemID, cof);
                                }
                                if (add)
                                {
                                    _currentlyWaitingCOFBuilds.Add(clientView.AgentId, waitingCOFs);
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (AvatarWearingArgs.Wearable wear in e.NowWearing)
                        {
                            wearables.Add(new AvatarWearable(wear.Type, wear.ItemID, UUID.Zero));
                        }
                    }
                }
                // Wearables are a stack. The entries we have represent the current "top" stack state.  Apply them

                SetAppearanceAssets(profile, ref wearables, clientView);
                avatar.Appearance.SetWearables(wearables);
                this.UpdateDatabase(clientView.AgentId, avatar.Appearance, null, null);
            }
            else
            {
                m_log.WarnFormat("[APPEARANCE]: Cannot set wearables for {0}, no user profile found", clientView.Name);
            }
        }