public void AddLastTest()
        {
            var list = new ReAttachTargetList(5);
            for (var i = 1; i <= 3; i++)
                list.AddLast(new ReAttachTarget(i, "path" + i, "user" + i));
            Assert.AreEqual(3, list[2].ProcessId);
            Assert.AreEqual(2, list[1].ProcessId);

            for (var i = 4; i <= 5; i++)
                list.AddLast(new ReAttachTarget(i, "path" + i, "user" + i));
            Assert.AreEqual(5, list[4].ProcessId);
            Assert.AreEqual(4, list[3].ProcessId);
        }
        public void AddExistingElementLastTest()
        {
            var list = new ReAttachTargetList(5);
            for (var i = 1; i <= 4; i++)
                list.AddLast(new ReAttachTarget(i, "path" + i, "user" + i));
            Assert.AreEqual(3, list[2].ProcessId);
            Assert.AreEqual(4, list.Count);

            list.AddLast(new ReAttachTarget(3, "path3", "user3"));
            Assert.AreNotEqual(3, list[2].ProcessId);
            Assert.AreEqual(3, list[3].ProcessId);
            Assert.AreEqual(4, list.Count);
        }
        public void AddExistingElementLastTest()
        {
            var list = new ReAttachTargetList(5);

            for (var i = 1; i <= 4; i++)
            {
                list.AddLast(new ReAttachTarget(i, "path" + i, "user" + i));
            }
            Assert.AreEqual(3, list[2].ProcessId);
            Assert.AreEqual(4, list.Count);

            list.AddLast(new ReAttachTarget(3, "path3", "user3"));
            Assert.AreNotEqual(3, list[2].ProcessId);
            Assert.AreEqual(3, list[3].ProcessId);
            Assert.AreEqual(4, list.Count);
        }
        public void AddLastTest()
        {
            var list = new ReAttachTargetList(5);

            for (var i = 1; i <= 3; i++)
            {
                list.AddLast(new ReAttachTarget(i, "path" + i, "user" + i));
            }
            Assert.AreEqual(3, list[2].ProcessId);
            Assert.AreEqual(2, list[1].ProcessId);

            for (var i = 4; i <= 5; i++)
            {
                list.AddLast(new ReAttachTarget(i, "path" + i, "user" + i));
            }
            Assert.AreEqual(5, list[4].ProcessId);
            Assert.AreEqual(4, list[3].ProcessId);
        }
        public void ContainsTest()
        {
            var list = new ReAttachTargetList(5);
            var indices = new[] {1, 2, 3, 4, 5};

            for (var i = 1; i <= 5; i++)
                list.AddLast(new ReAttachTarget(indices[i-1], "path" + i, "user" + i));
            foreach (var index in indices)
            {
                Assert.AreEqual(index, list[index-1].ProcessId);
            }
            Assert.IsNull(list[123]);
        }
        public void ContainsTest()
        {
            var list    = new ReAttachTargetList(5);
            var indices = new[] { 1, 2, 3, 4, 5 };

            for (var i = 1; i <= 5; i++)
            {
                list.AddLast(new ReAttachTarget(indices[i - 1], "path" + i, "user" + i));
            }
            foreach (var index in indices)
            {
                Assert.AreEqual(index, list[index - 1].ProcessId);
            }
            Assert.IsNull(list[123]);
        }
        public ReAttachTargetList LoadTargets()
        {
            try
            {
                var root = _package.OpenUserRegistryRoot();
                if (root == null)
                {
                    _package.Reporter.ReportError("Unable to open user root registry key.");
                    return null;
                }

                var subkey = root.OpenSubKey(ReAttachConstants.ReAttachRegistryKeyName);
                if (subkey == null)
                {
                    _package.Reporter.ReportWarning(
                        "Unable to open ReAttach registry subkey. This might be the first time ReAttach is started.");
                    root.Close();
                    return null;
                }

                var targets = new ReAttachTargetList(ReAttachConstants.ReAttachHistorySize);
                for (var i = 1; i < ReAttachConstants.ReAttachHistorySize; i++)
                {
                    var json = subkey.GetValue(ReAttachConstants.ReAttachRegistryHistoryKeyPrefix + i) as string;
                    if (json == null)
                        continue;
                    try
                    {
                        var target = JsonConvert.DeserializeObject<ReAttachTarget>(json);
                        targets.AddLast(target);
                    } catch (Exception e) { /* Ignore broken elements */}
                }
                subkey.Close();
                root.Close();
                return targets;
            }
            catch (Exception e)
            {
                _package.Reporter.ReportWarning(
                    "Unable to load history. This might be first time ReAttach is started. Exception: {0}", e.Message);
            }
            return null;
        }
        public ReAttachTargetList LoadTargets()
        {
            try
            {
                var root = _package.OpenUserRegistryRoot();
                if (root == null)
                {
                    _package.Reporter.ReportError("Unable to open user root registry key.");
                    return null;
                }

                var subkey = root.OpenSubKey(ReAttachConstants.ReAttachRegistryKeyName);
                if (subkey == null)
                {
                    _package.Reporter.ReportWarning(
                        "Unable to open ReAttach registry subkey. This might be the first time ReAttach is started.");
                    root.Close();
                    return null;
                }

                var targets = new ReAttachTargetList(ReAttachConstants.ReAttachHistorySize);
                for (var i = 1; i < ReAttachConstants.ReAttachHistorySize; i++)
                {
                    var value = subkey.GetValue(ReAttachConstants.ReAttachRegistryHistoryKeyPrefix + i) as string;
                    if (value == null)
                        continue;
                    var tokens = value.Split(new[] {ReAttachConstants.ReAttachRegistrySplitChar});
                    if (tokens.Length != 4)
                        continue;
                    int pid;
                    int.TryParse(tokens[2], out pid);
                    targets.AddLast(new ReAttachTarget(pid, tokens[0], tokens[1], tokens[3]));
                }
                subkey.Close();
                root.Close();
                return targets;
            }
            catch (Exception e)
            {
                _package.Reporter.ReportWarning(
                    "Unable to load history. This might be first time ReAttach is started. Exception: {0}", e.Message);
            }
            return null;
        }