public void WriteFile( )
        {
            RecentComputerList computers = new RecentComputerList();
            computers.Add( new ComputerName( "1.1.1.1", "alias", true ) );
            computers.Add( new ComputerName( "work", null, false ) );

            StringWriter writer = new StringWriter();
            ComputerListFile serializer = new ComputerListFile();
            serializer.Write( writer, computers );

            Assert.AreEqual(
                "1.1.1.1\talias\tTrue\r\n" +
                "work\t\tFalse\r\n",
                writer.ToString() );
        }
        public RecentComputerList Read( TextReader reader )
        {
            RecentComputerList computers = new RecentComputerList();

            while ( true )
            {
                string line = reader.ReadLine();
                if ( line == null )
                    break;

                string[] pair = line.Split( '\t' );

                // Avoid processing an empty line
                if ( pair[0] == "" )
                    continue;
                string computer = pair[0];

                string alias = pair.Length > 1 ? pair[1] : "";

                bool adminMode = false;
                if ( pair.Length > 2 )
                    bool.TryParse( pair[2], out adminMode );

                computers.Add( new ComputerName( computer, alias, adminMode ) );
            }

            return computers;
        }
        public void Add( )
        {
            RecentComputerList computers = new RecentComputerList();

            Assert.IsTrue( computers.Add( new ComputerName( "cpu1", null ) ) );
            Assert.AreEqual( new string[] {
                "cpu1" },
                GetComputers( computers ) );

            Assert.IsTrue( computers.Add( new ComputerName( "cpu2", null ) ) );
            Assert.AreEqual( new string[] {
                "cpu1",
                "cpu2" },
                GetComputers( computers ) );

            RecentComputerList fullList = ListFromStrings( new string[]
            {
                "aaaa",
                "bbbb",
                "cccc",
                "dddd",
                "eeee",
                "ffff",
                "gggg",
                "hhhh",
                "iiii",
                "jjjj"
            } );

            Assert.IsFalse( fullList.Add( new ComputerName( "cpu1", null ) ) );

            Assert.AreEqual( new string[]
            {
                "aaaa",
                "bbbb",
                "cccc",
                "dddd",
                "eeee",
                "ffff",
                "gggg",
                "hhhh",
                "iiii",
                "jjjj"
            },
            GetComputers( fullList ) );
        }
        public void Clear( )
        {
            RecentComputerList computers = new RecentComputerList();

            computers.Add( new ComputerName( "cpu1", null ) );
            Assert.AreEqual( new string[] {
                "cpu1" },
                GetComputers( computers ) );

            computers.Clear();

            Assert.AreEqual( new string[0],
                GetComputers( computers ) );
        }
        // Does not throw.
        public RecentComputerList ReadFromRegistry( )
        {
            RecentComputerList computers = new RecentComputerList();

            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey( "Software" )
                    .OpenSubKey( "Microsoft" )
                    .OpenSubKey( "Terminal Server Client" )
                    .OpenSubKey( "Default", false );

                const int MaxComputerCount = 10;
                for ( int i = 0; i < MaxComputerCount; ++i )
                {
                    string valueName = string.Format( "MRU{0}", i );
                    string computer = (string) key.GetValue( valueName );
                    if ( !string.IsNullOrEmpty( computer ) )
                        computers.Add( new ComputerName( key.GetValue( valueName ).ToString(), null ) );
                }
            }
            catch ( Exception ex )
            {
                Logger.LogException( ex );
            }

            return computers;
        }
        private RecentComputerList ListFromStrings( string[] cnames )
        {
            RecentComputerList computers = new RecentComputerList();

            foreach ( string cname in cnames )
            {
                computers.Add( new ComputerName( cname, null ) );
            }

            return computers;
        }
        public void Contains( )
        {
            RecentComputerList computers = new RecentComputerList();

            computers.Add( new ComputerName( "cpu1", null ) );
            computers.Add( new ComputerName( "cpu2", "alias2" ) );

            // Basic check
            Assert.IsTrue( computers.Contains( new ComputerName( "cpu1", null ) ) );
            // Must match both computer and alias.
            Assert.IsFalse( computers.Contains( new ComputerName( "cpu1", "alias1" ) ) );
            Assert.IsTrue( computers.Contains( new ComputerName( "cpu2", "alias2" ) ) );
            // Case insensitive for computer name and alias.
            Assert.IsTrue( computers.Contains( new ComputerName( "CPU2", "alias2" ) ) );
            Assert.IsTrue( computers.Contains( new ComputerName( "cpu2", "ALIAS2" ) ) );
        }
        public void Remove( )
        {
            RecentComputerList computers = new RecentComputerList();
            computers.Add( new ComputerName( "cpu1", "alias1" ) );
            computers.Add( new ComputerName( "cpu2", "alias2" ) );
            Assert.AreEqual( 2, computers.Count );

            computers.Remove( new ComputerName( "cpu1", "alias1" ) );
            Assert.AreEqual( 1, computers.Count );

            // Is Remove case insensitive?
            computers.Remove( new ComputerName( "CPU2", "ALIAS2" ) );
            Assert.AreEqual( 0, computers.Count );
        }
        public void ICollectionAdd( )
        {
            ICollection<ComputerName> collection = new RecentComputerList();

            collection.Add( new ComputerName( "cpu1", null ) );
            Assert.AreEqual( new string[] {
                "cpu1" },
                GetComputers( (RecentComputerList) collection ) );

            collection.Add( new ComputerName( "cpu2", null ) );
            Assert.AreEqual( new string[] {
                "cpu1",
                "cpu2" },
                GetComputers( (RecentComputerList) collection ) );

            ICollection<ComputerName> fullCollection = ListFromStrings( new string[]
            {
                "aaaa",
                "bbbb",
                "cccc",
                "dddd",
                "eeee",
                "ffff",
                "gggg",
                "hhhh",
                "iiii",
                "jjjj"
            } );

            fullCollection.Add( new ComputerName( "cpu1", null ) );

            Assert.AreEqual( new string[]
            {
                "aaaa",
                "bbbb",
                "cccc",
                "dddd",
                "eeee",
                "ffff",
                "gggg",
                "hhhh",
                "iiii",
                "jjjj"
            },
            GetComputers( (RecentComputerList) fullCollection ) );
        }
        public void GetAliases( )
        {
            RecentComputerList computers = new RecentComputerList();
            computers.Add( new ComputerName( "1.1.1.1", null ) );
            computers.Add( new ComputerName( "2.2.2.2", "work" ) );
            computers.Add( new ComputerName( "3.3.3.3", null ) );
            computers.Add( new ComputerName( "4.4.4.4", "home" ) );

            Assert.AreEqual( new string[] { "work", "home" }, computers.GetAliases().ToArray() );

            // Test with no aliases
            computers = new RecentComputerList();
            computers.Add( new ComputerName( "1.1.1.1", null ) );

            Assert.AreEqual( new string[0], computers.GetAliases().ToArray() );

            // Test with no items
            computers = new RecentComputerList();

            Assert.AreEqual( new string[0], computers.GetAliases().ToArray() );
        }
        public void Find( )
        {
            RecentComputerList computers = new RecentComputerList();
            ComputerName withAlias = new ComputerName( "1.1.1.1", "alias" );
            ComputerName justComputer = new ComputerName( "work", null );
            computers.Add( withAlias );
            computers.Add( justComputer );

            // Find by alias
            Assert.AreEqual( withAlias, computers.Find( "alias" ) );
            // Find by computer
            Assert.AreEqual( withAlias, computers.Find( "1.1.1.1" ) );
            // Find with no alias
            Assert.AreEqual( justComputer, computers.Find( "work" ) );
            // Find by alias, case insensitive
            Assert.AreEqual( withAlias, computers.Find( "ALIAS" ) );
            // Find by computer, case insensitive
            Assert.AreEqual( justComputer, computers.Find( "WORK" ) );
            // Not found
            Assert.IsNull( computers.Find( "foobar" ) );
        }
        public void DuplicateAlias( )
        {
            RecentComputerList computers = new RecentComputerList();
            computers.Add( new ComputerName( "1.1.1.1", "work" ) );
            computers.Add( new ComputerName( "2.2.2.2", "CASE" ) );

            computers.Push( new ComputerName( "3.3.3.3", "work" ) );

            // Alias is removed from the old item.
            Assert2.AssertComputerNames( new string[]
            {
                "3.3.3.3", "work",
                "1.1.1.1", null,
                "2.2.2.2", "CASE",
            }, computers.ToArray() );

            // Confirm case sensitive.
            computers.Push( new ComputerName( "4.4.4.4", "case" ) );

            Assert2.AssertComputerNames( new string[]
            {
                "4.4.4.4", "case",
                "3.3.3.3", "work",
                "1.1.1.1", null,
                "2.2.2.2", null,
            }, computers.ToArray() );
        }
        public void Count( )
        {
            RecentComputerList computers = new RecentComputerList();
            Assert.AreEqual( 0, computers.Count );

            computers.Add( new ComputerName( "cpu1", "alias1" ) );
            Assert.AreEqual( 1, computers.Count );

            computers.Add( new ComputerName( "cpu2", "alias2" ) );
            Assert.AreEqual( 2, computers.Count );
        }
        public void CopyTo( )
        {
            ICollection<ComputerName> collection = new RecentComputerList();
            collection.Add( new ComputerName( "cpu1", "alias1" ) );
            collection.Add( new ComputerName( "cpu2", "alias2" ) );

            ComputerName[] array = new ComputerName[3];
            array[0] = new ComputerName( "0.0.0.0", "zero" );
            collection.CopyTo( array, 1 );

            Assert2.AssertComputerNames( new string[]
            {
                "0.0.0.0", "zero",
                "cpu1", "alias1",
                "cpu2", "alias2",
            }, array );
        }
        public void CopyCtor( )
        {
            var computers = new RecentComputerList();
            computers.Add( new ComputerName( "1.1.1.1", "work", true ) );
            computers.Add( new ComputerName( "2.2.2.2", "home", false ) );

            var otherComps = new RecentComputerList( computers );

            // Did we copy?
            Assert.AreEqual( new ComputerName[]
                {
                    new ComputerName( "1.1.1.1", "work", true ),
                    new ComputerName( "2.2.2.2", "home", false )
                },
                otherComps.ToArray() );

            // Are the underlying lists different objects?
            computers.Remove( computers.Find( "home" ) );
            Assert.AreEqual( 1, computers.Count );
            Assert.AreEqual( 2, otherComps.Count );

            // Are the underlayng ComputerName different objects?
            foreach ( var name in computers )
            {
                name.Alias = null;
            }
            Assert.IsNull( computers.Find( "work" ) );
            Assert.IsNotNull( otherComps.Find( "work" ) );
        }