protected void Page_Load(object sender, EventArgs e) { AbstractMessageMediator messageMediator = new MessageMediator(); AbstractMessage user1 = new SqlMessage("user1"); AbstractMessage user2 = new SqlMessage("user2"); AbstractMessage user3 = new XmlMessage("user3"); AbstractMessage user4 = new XmlMessage("user4"); messageMediator.Register(user1); messageMediator.Register(user2); messageMediator.Register(user3); messageMediator.Register(user4); Response.Write(user1.Send("user2", new MessageModel("你好!", DateTime.Now))); Response.Write("<br />"); Response.Write(user2.Send("user1", new MessageModel("我不好!", DateTime.Now))); Response.Write("<br />"); Response.Write(user1.Send("user2", new MessageModel("不好就不好吧。", DateTime.Now))); Response.Write("<br />"); Response.Write(user3.Send("user4", new MessageModel("吃了吗?", DateTime.Now))); Response.Write("<br />"); Response.Write(user4.Send("user3", new MessageModel("没吃,你请我?", DateTime.Now))); Response.Write("<br />"); Response.Write(user3.Send("user4", new MessageModel("不请。", DateTime.Now))); Response.Write("<br />"); }
public void ReturnsFalsForDoubleRegistration() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); Assert.IsTrue(mediator.Register <string>(recipient, recipient.OnMessage)); Assert.IsFalse(mediator.Register <string>(recipient, recipient.OnMessage)); }
public void ReturnsTrueWhenRegisteredWithAndWithoutTag() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage, "myTag"); mediator.Register <string>(recipient, recipient.AnotherOnMessage); Assert.IsTrue(mediator.IsMessageRegistered(typeof(string))); }
public void ReturnsTrueForRegisteredHandlersWithTag() { var mediator = new MessageMediator(); var sender = new MessageSender(); var recipient = new MessageRecipient(); // Double registration with separate tags is possible mediator.Register <string>(recipient, recipient.OnMessage, "myTag"); mediator.Register <string>(recipient, recipient.OnMessage, "anotherTag"); Assert.IsTrue(sender.SendMessage(mediator, "test", "myTag")); Assert.AreEqual(1, recipient.MessagesReceived); }
public void DeadInstanceTest() { var target = new MessageMediator(); var tc = new TestCounter(); WeakReference wr = new WeakReference(tc); target.Register(tc); tc = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); // If it's still alive, might be debug if (wr.IsAlive) { Assert.Inconclusive("Target still alive - debug mode?"); tc = (TestCounter)wr.Target; if (tc != null) { target.Unregister(wr.Target); return; } } bool hadTarget = target.SendMessage("test2", "Test2"); Assert.IsFalse(hadTarget, "Mediator located dead reference - debug mode?"); }
public void SendMessage() { var a = new ReceiverA(); var b = new ReceiverB(); var m = new MessageMediator(); m.Register <Message>(a, a.OnMessageReceived); m.Register <Message>(b, b.OnMessageReceived); m.Unregister <Message>(b, b.OnMessageReceived); // this actually unregisters a's handler, not b's handler. m.SendMessage(new Message { Text = "hello" }); Assert.That(a.Received, Is.EqualTo("hello")); }
public void ReturnsTrueAfterRegistration() { var recipient = new MessageRecipient(); var messageMediator = new MessageMediator(); messageMediator.Register <string>(recipient, recipient.OnMessage); Assert.IsTrue(messageMediator.IsRegistered <string>(recipient, recipient.OnMessage)); }
public void ReturnsTrueWhenRegistered() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage); Assert.IsTrue(mediator.IsMessageRegistered(typeof(string))); }
public void ReturnsFalseWhenRegisteredWithTagButNotProvided() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage, "myTag"); Assert.IsFalse(mediator.IsMessageRegistered(typeof(string))); }
public void TestBadRegister() { var target = new MessageMediator(); var tc = new TestCounter(); target.Register(tc); Assert.ThrowsException <ArgumentException>(() => target.RegisterHandler <int>("test", tc.MessageHandler5)); target.Unregister(tc); }
public void ReturnsFalseForUnregisteredHandlersWithTag() { var mediator = new MessageMediator(); var sender = new MessageSender(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage); Assert.IsFalse(sender.SendMessage(mediator, "test", "myTag")); }
public void UnregistersAllMethodsOfRecipient() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage); mediator.Register <string>(recipient, recipient.AnotherOnMessage); mediator.Register <string>(recipient, recipient.YetAnotherOnMessage); Assert.IsTrue(mediator.IsRegistered <string>(recipient, recipient.OnMessage)); Assert.IsTrue(mediator.IsRegistered <string>(recipient, recipient.AnotherOnMessage)); Assert.IsTrue(mediator.IsRegistered <string>(recipient, recipient.YetAnotherOnMessage)); mediator.UnregisterRecipient(recipient); Assert.IsFalse(mediator.IsRegistered <string>(recipient, recipient.OnMessage)); Assert.IsFalse(mediator.IsRegistered <string>(recipient, recipient.AnotherOnMessage)); Assert.IsFalse(mediator.IsRegistered <string>(recipient, recipient.YetAnotherOnMessage)); }
public void CleanUpKeepsNonGarbageCollectedHandlersRegistered() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage); mediator.CleanUp(); Assert.IsTrue(mediator.IsRegistered <string>(recipient, recipient.OnMessage)); }
public void ReturnsTrueForRegisteredHandlers() { var mediator = new MessageMediator(); var sender = new MessageSender(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage); Assert.IsTrue(sender.SendMessage(mediator, "test")); Assert.AreEqual(1, recipient.MessagesReceived); }
public void DeadInstanceTest() { var target = new MessageMediator(); target.Register(new TestCounter()); GC.Collect(); bool hadTarget = target.SendMessage("test2", "Test2"); Assert.AreEqual(true, hadTarget, "Mediator did not return success"); }
public void UnregistersRegisteredHandlerWithTag() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage, "myTag"); Assert.IsTrue(mediator.IsRegistered <string>(recipient, recipient.OnMessage, "myTag")); Assert.IsTrue(mediator.Unregister <string>(recipient, recipient.OnMessage, "myTag")); Assert.IsFalse(mediator.IsRegistered <string>(recipient, recipient.OnMessage, "myTag")); }
public void RegistersHandler() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); Assert.IsFalse(mediator.IsRegistered <string>(recipient, recipient.OnMessage)); mediator.Register <string>(recipient, recipient.OnMessage); Assert.IsTrue(mediator.IsRegistered <string>(recipient, recipient.OnMessage)); }
public void TestInterface2() { var target = new MessageMediator(); var tc = new Test3(); target.Register(tc); bool hadTarget = target.SendMessage <IData>(new Data("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); Assert.AreEqual <int>(1, tc.Count); }
public void TestInterfaceUsingDirectKey() { var target = new MessageMediator(); var tc = new TestImpl(); target.Register(tc); bool hadTarget = target.SendMessage("test", new DataEvent("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); Assert.AreEqual(2, tc.Count); }
public void RegisterInstanceTest() { var target = new MessageMediator(); TestCounter counter = new TestCounter(); target.Register(counter); bool hadTarget = target.SendMessage("test2", "Test2"); Assert.AreEqual(true, hadTarget, "Mediator did not return success"); Assert.AreEqual(1, counter.Count, "Method did not receive message"); }
public void TestInterfaceUsingConcreteClass() { var target = new MessageMediator(); var tc = new TestImpl(); target.Register(tc); bool hadTarget = target.SendMessage(new DataEvent("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); // Should invoke two methods - interface AND direct Assert.AreEqual(5, tc.Count); }
public void ReturnsFalseAfterGarbageCollected() { var recipient = new MessageRecipient(); var messageMediator = new MessageMediator(); messageMediator.Register <string>(recipient, recipient.OnMessage); recipient = null; GC.Collect(); recipient = new MessageRecipient(); Assert.IsFalse(messageMediator.IsRegistered <string>(recipient, recipient.OnMessage)); }
public void CleanUpClearsGarbageCollectedHandlers() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage); recipient = null; GC.Collect(); mediator.CleanUp(); Assert.AreEqual(0, mediator.GetRegisteredHandlers <string>().Count); }
public void ReturnsFalseForHandlersClearedByGarbageCollector() { var mediator = new MessageMediator(); var sender = new MessageSender(); var recipient = new MessageRecipient(); mediator.Register <string>(recipient, recipient.OnMessage); recipient = null; GC.Collect(); Assert.IsFalse(sender.SendMessage(mediator, "test")); Assert.AreEqual(0, mediator.GetRegisteredHandlers <string>().Count, "SendMessage should auto cleanup"); }
public void TypeTest() { var target = new MessageMediator(); var tc = new TestCounter(); target.Register(tc); bool hadTarget = target.SendMessage("Test2"); Assert.AreEqual(true, hadTarget, "Mediator did not return success"); hadTarget = target.SendMessage("test3", "Test2"); Assert.AreEqual(true, hadTarget, "Mediator did not return success"); Assert.AreEqual <int>(2, tc.Count); }
public void TestBadRegister() { var target = new MessageMediator(); var tc = new TestCounter(); target.Register(tc); try { target.RegisterHandler <int>("test", tc.MessageHandler5); Assert.Fail("Did not throw exception on invalid register"); } catch (ArgumentException) { } }
public void TestIntParam2() { var target = new MessageMediator(); var tc = new TestCounter(); target.Register(tc); target.RegisterHandler <int>("test6", tc.MessageHandler6); bool hadTarget = target.SendMessage(5); Assert.AreEqual(false, hadTarget, "Mediator found unexpected target"); hadTarget = target.SendMessage("test6", 5); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); Assert.AreEqual <int>(5, tc.Count); }
public void TestInterface() { var target = new MessageMediator(); var tc = new TestImpl(); target.Register(tc); // Should invoke two methods - interface AND direct bool hadTarget = target.SendMessage <IDataEvent>(new DataEvent("Test")); Assert.IsTrue(hadTarget, "Mediator did not find target"); Assert.AreEqual(5, tc.Count); target.Unregister(tc); Assert.IsFalse(target.SendMessage <IDataEvent>(new DataEvent("Test"))); }
public void TestJustInterfaces() { var target = new MessageMediator(); var tc = new TestImplJustInterfaces(); target.Register(tc); bool hadTarget = target.SendMessage(new DataEvent("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); hadTarget = target.SendMessage(new DataEventUpper("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); hadTarget = target.SendMessage(new DataEventLower("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); }
/// <summary> /// Initializes a new instance of the <see cref="NotificationBarViewModel"/> class. /// </summary> public NotificationBarViewModel(IMessageMediator messageMediator) : base(messageMediator) { MessageMediator.Register <string>(this, data => { if (ObjectHelper.IsNull(data)) { return; } try { EventMessage = data; } catch { } }, "UpdateNotification"); }
public void ReturnsTrueWhenRegistered() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage); Assert.IsTrue(mediator.IsMessageRegistered(typeof (string))); }
public void ReturnsFalseWhenRegisteredWithTagButNotProvided() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage, "myTag"); Assert.IsFalse(mediator.IsMessageRegistered(typeof(string))); }
public void ReturnsTrueWhenRegisteredWithAndWithoutTag() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage, "myTag"); mediator.Register<string>(recipient, recipient.AnotherOnMessage); Assert.IsTrue(mediator.IsMessageRegistered(typeof(string))); }
public void RegistersHandler() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.OnMessage)); mediator.Register<string>(recipient, recipient.OnMessage); Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.OnMessage)); }
public void ReturnsFalsForDoubleRegistration() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); Assert.IsTrue(mediator.Register<string>(recipient, recipient.OnMessage)); Assert.IsFalse(mediator.Register<string>(recipient, recipient.OnMessage)); }
public void ReturnsFalseForUnregisteredHandlersWithTag() { var mediator = new MessageMediator(); var sender = new MessageSender(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage); Assert.IsFalse(sender.SendMessage(mediator, "test", "myTag")); }
public void ReturnsTrueForRegisteredHandlers() { var mediator = new MessageMediator(); var sender = new MessageSender(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage); Assert.IsTrue(sender.SendMessage(mediator, "test")); Assert.AreEqual(1, recipient.MessagesReceived); }
public void ReturnsTrueForRegisteredHandlersWithTag() { var mediator = new MessageMediator(); var sender = new MessageSender(); var recipient = new MessageRecipient(); // Double registration with separate tags is possible mediator.Register<string>(recipient, recipient.OnMessage, "myTag"); mediator.Register<string>(recipient, recipient.OnMessage, "anotherTag"); Assert.IsTrue(sender.SendMessage(mediator, "test", "myTag")); Assert.AreEqual(1, recipient.MessagesReceived); }
public void ReturnsFalseForHandlersClearedByGarbageCollector() { var mediator = new MessageMediator(); var sender = new MessageSender(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage); recipient = null; GC.Collect(); Assert.IsFalse(sender.SendMessage(mediator, "test")); Assert.AreEqual(0, mediator.GetRegisteredHandlers<string>().Count, "SendMessage should auto cleanup"); }
public void UnregistersRegisteredHandlerWithTag() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage, "myTag"); Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.OnMessage, "myTag")); Assert.IsTrue(mediator.Unregister<string>(recipient, recipient.OnMessage, "myTag")); Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.OnMessage, "myTag")); }
public void UnregistersAllMethodsOfRecipient() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage); mediator.Register<string>(recipient, recipient.AnotherOnMessage); mediator.Register<string>(recipient, recipient.YetAnotherOnMessage); Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.OnMessage)); Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.AnotherOnMessage)); Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.YetAnotherOnMessage)); mediator.UnregisterRecipient(recipient); Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.OnMessage)); Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.AnotherOnMessage)); Assert.IsFalse(mediator.IsRegistered<string>(recipient, recipient.YetAnotherOnMessage)); }
public void CleanUpKeepsNonGarbageCollectedHandlersRegistered() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage); mediator.CleanUp(); Assert.IsTrue(mediator.IsRegistered<string>(recipient, recipient.OnMessage)); }
public void CleanUpClearsGarbageCollectedHandlers() { var mediator = new MessageMediator(); var recipient = new MessageRecipient(); mediator.Register<string>(recipient, recipient.OnMessage); recipient = null; GC.Collect(); mediator.CleanUp(); Assert.AreEqual(0, mediator.GetRegisteredHandlers<string>().Count); }