예제 #1
0
        public void Add_ExistsUser_ExceptionThrown()
        {
            MasterUserService service = new MasterUserService();
            var user = new User()
            {
                FirstName = "Ivan", LastName = "Metelsky", DateOfBirth = DateTime.Now
            };

            service.Add(user);
            service.Add(user);
        }
예제 #2
0
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings
            {
                Encoding = Encoding.UTF8,
                Indent   = true
            };

            MasterUserService master = new MasterUserService(new XmlStorage("users.xml", settings), new DefaultIdGenerator());

            master.OnStart();

            /*User a = new User("artem", "one", DateTime.Today);
             * a.VisaRecords.Add(new Visa() { Country = "Belarus", End = DateTime.Now, Start = DateTime.Now });
             * a.VisaRecords.Add(new Visa() { Country = "Usa", End = DateTime.Now, Start = DateTime.Now });
             * storage.Add(a, new CustomValidation());
             * storage.Add(new User("dmitry", "four", DateTime.Today), new CustomValidation());*/

            while (true)
            {
                Console.WriteLine("Write command:");
                string command = Console.ReadLine();
                switch (command)
                {
                case "savestate":
                    master.SaveState();
                    break;

                case "stop":
                    master.OnStop();
                    break;

                case "add":
                    master.Add(new User("abrakadabra", "four", DateTime.Today), new CustomValidation());
                    break;

                case "all":
                    foreach (var user in master.GetAll())
                    {
                        Console.WriteLine(user);
                    }
                    break;

                case "delete1":
                    master.Delete(1);
                    break;

                case "delete":
                    master.Delete(new User("dmitry", "four", DateTime.Today));
                    break;

                default:
                    Console.WriteLine("Wrong command");
                    break;
                }
            }
        }
예제 #3
0
        public void SearchByPredicate_UserNotFound_EmptyCollection()
        {
            MasterUserService service = new MasterUserService();
            var user = new User()
            {
                FirstName = "Ivan", LastName = "Metelsky", DateOfBirth = DateTime.Now
            };

            service.Add(user);
            var listUsers = service.SearchByPredicate((x) =>
            {
                if (x == new User())
                {
                    return(true);
                }
                return(false);
            });

            Assert.AreEqual(listUsers.Count(), 0);
        }
예제 #4
0
        public void SearchByPredicate_UserExists_User()
        {
            MasterUserService service = new MasterUserService();
            var user = new User()
            {
                FirstName = "Ivan", LastName = "Metelsky", DateOfBirth = DateTime.Now
            };

            service.Add(user);
            var listUsers = service.SearchByPredicate((x) =>
            {
                if (x == user)
                {
                    return(true);
                }
                return(false);
            });

            Assert.AreEqual(listUsers.First(), user);
        }
예제 #5
0
        public void Add_NullUser_ExceptionThrown()
        {
            MasterUserService service = new MasterUserService();

            service.Add(null);
        }
예제 #6
0
        private static void RunMaster(MasterUserService master)
        {
            if (object.ReferenceEquals(master, null))
            {
                return;
            }

            Random rand = new Random();

            ThreadStart masterSearch = () =>
            {
                while (true)
                {
                    var serachresult = master.SearchForUsers(new Func <BllUser, bool>[] { u => u.FirstName != null });
                    Console.Write("Master search results: ");
                    foreach (var result in serachresult)
                    {
                        Console.Write(result + " ");
                    }

                    Console.WriteLine();
                    Thread.Sleep(rand.Next(1000, 5000));
                }
            };

            ThreadStart masterAddDelete = () =>
            {
                var users = new List <BllUser>
                {
                    new BllUser {
                        FirstName = "User1", LastName = "User1"
                    },
                    new BllUser {
                        FirstName = "User2", LastName = "User2"
                    },
                    new BllUser {
                        FirstName = "User3", LastName = "User3"
                    }
                };
                BllUser userToDelete = null;

                while (true)
                {
                    foreach (var user in users)
                    {
                        int addChance = rand.Next(0, 5);
                        if (addChance == 0)
                        {
                            master.Add(user);
                        }

                        Thread.Sleep(rand.Next(1000, 5000));
                        if (userToDelete != null)
                        {
                            int deleteChance = rand.Next(0, 5);
                            if (deleteChance == 0)
                            {
                                master.Delete(userToDelete);
                            }
                        }

                        userToDelete = user;
                        Thread.Sleep(rand.Next(1000, 5000));
                        Console.WriteLine();
                    }
                }
            };

            Thread masterSearchThread = new Thread(masterSearch)
            {
                IsBackground = true
            };
            Thread masterAddThread = new Thread(masterAddDelete)
            {
                IsBackground = true
            };
            Thread masterSearchThread1 = new Thread(masterSearch)
            {
                IsBackground = true
            };
            Thread masterAddThread1 = new Thread(masterAddDelete)
            {
                IsBackground = true
            };

            masterAddThread.Start();
            masterSearchThread.Start();
            masterAddThread1.Start();
            masterSearchThread1.Start();
        }