public void TestReRegisterAndExecuteCommand()
        {
            // Fetch the controller, register the ControllerTestCommand2 to handle 'ControllerTest2' notes
            IController controller = Controller.GetInstance("ControllerTestKey5", () => new Controller("ControllerTestKey5"));

            controller.RegisterCommand("ControllerTest2", () => new ControllerTestCommand2());

            // Remove the Command from the Controller
            controller.RemoveCommand("ContrllerTest2");

            // Re-register the Command with the Controller
            controller.RegisterCommand("ControllerTest2", () => new ControllerTestCommand2());

            // Create a 'ControllerTest2' note
            ControllerTestVO vo   = new ControllerTestVO(12);
            INotification    note = new Notification("ControllerTest2", vo);

            // retrieve a reference to the View from the same core.
            IView view = View.GetInstance("ControllerTestKey5", () => new View("ControllerTestKey5"));

            // send the Notification
            view.NotifyObservers(note);

            // test assertions
            // if the command is executed once the value will be 24
            Assert.IsTrue(vo.result == 24, "Expecting vo.result == 24");

            // Prove that accumulation works in the VO by sending the notification again
            view.NotifyObservers(note);

            // if the command is executed twice the value will be 48
            Assert.IsTrue(vo.result == 48, "Expecting vo.result == 48");
        }
        public void TestRegisterAndRemoveCommand()
        {
            // Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes
            IController controller = Controller.GetInstance("ControllerTestKey3", () => new Controller("ControllerTestKey3"));

            controller.RegisterCommand("ControllerRemoveTest", () => new ControllerTestCommand());

            // Create a 'ControllerTest' note
            ControllerTestVO vo   = new ControllerTestVO(12);
            INotification    note = new Notification("ControllerRemoveTest", vo);

            // Tell the controller to execute the Command associated with the note
            // the ControllerTestCommand invoked will multiply the vo.input value
            // by 2 and set the result on vo.result

            controller.ExecuteCommand(note);

            // test assertions
            Assert.AreEqual(24, vo.result, "Expecting vo.result == 24");

            // Reset result
            vo.result = 0;

            // Remove the Command from the Controller
            controller.RemoveCommand("ControllerRemoveTest");

            // Tell the controller to execute the Command associated with the
            // note. This time, it should not be registered, and our vo result
            // will not change
            controller.ExecuteCommand(note);

            // test assertions
            Assert.IsTrue(vo.result == 0, "Expecting vo.result == 0");
        }
        public void TestGetInstance()
        {
            // Test Factory Method
            IController controller = Controller.GetInstance("ControllerTestKey1", () => new Controller("ControllerTestKey1"));

            // test assertions
            Assert.IsNotNull(controller, "Expecting instance not null");
            Assert.IsTrue(controller != null, "Expecting instance implements IController");
        }
        public void TestMultitons()
        {
            var temp1 = Controller.GetInstance("A", k => new Controller(k));
            var temp2 = Controller.GetInstance("A", k => new Controller(k));

            Assert.IsTrue(temp1 == temp2);

            temp2 = Controller.GetInstance("B", k => new Controller(k));

            Assert.IsFalse(temp1 == temp2);
        }
        public void TestHasCommand()
        {
            // register the ControllerTestCommand to handle 'hasCommandTest' notes
            IController controller = Controller.GetInstance("ControllerTestKey4", () => new Controller("TestKey4"));

            controller.RegisterCommand("HasCommandTest", () => new ControllerTestCommand());

            // test that hasCommand returns true for hasCommandTest notifications
            Assert.IsTrue(controller.HasCommand("HasCommandTest") == true, "Expecting controller.HasCommand('HasCommandTest') == true");

            // Remove the Command from the Controller
            controller.RemoveCommand("HasCommandTest");

            // test that hasCommand returns false for hasCommandTest notifications
            Assert.IsTrue(controller.HasCommand("HasCommandTest") == false, "Expecting controller.HasCommand('HasCommandTest') == false");
        }
        public void TestRegisterAndExecuteCommand()
        {
            // Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes
            IController controller = Controller.GetInstance("ControllerTestKey2", () => new Controller("ControllerTestKey2"));

            controller.RegisterCommand("ControllerTest", () => new ControllerTestCommand());

            // Create a 'ControllerTest' notification
            var           vo   = new ControllerTestVO(12);
            INotification note = new Notification("ControllerTest", vo);

            // Tell the controller to execute the Command associated with the notification
            // the ControllerTestCommand invoked will multiply the vo.input value
            // by 2 and set the result on vo.result
            controller.ExecuteCommand(note);

            // test assertions
            Assert.IsTrue(vo.result == 24, "Expecting vo.result == 24");
        }
        public void TestGetInstanceThreadSafety()
        {
            var instances = new List <IController>();

            for (var i = 0; i < 1000; i++)
            {
                new Thread(() =>
                {
                    instances.Add(Controller.GetInstance("ThreadSafety", key => new Controller(key)));
                }).Start();
            }

            // test assertions
            for (int i = 1, count = instances.Count; i < count; i++)
            {
                Assert.AreEqual(instances[0], instances[i]);
            }

            Controller.GetInstance("ThreadSafety", key => new Controller(key));
        }