public void AutowireAllHandlersWithCompatibleSignatures() { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource(); PingListener listener = new PingListener(); // wire them up (any methods on the listener that have a compatible signature will be wired up) IEventHandlerValue wirer = new AutoWiringEventHandlerValue(); wirer.EventName = "^Ping$"; wirer.Wire(source, listener); // raise the event on the source source.OnPing(); // ascertain that the event handler was indeed notified of the raised event Assert.IsTrue(listener.GotPing, "The listener was not notified of the raised event."); Assert.IsFalse(listener.GotPinging, "The listener was (incorrectly) notified of an event that wasn't raised."); Assert.IsFalse(listener.GotPinged, "The listener was (incorrectly) notified of an event that wasn't raised."); }
public void AutowireExplicitlyWithBadSignatureMethod () { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource (); PingListener listener = new PingListener (); // wire them up IEventHandlerValue wirer = new AutoWiringEventHandlerValue (); wirer.EventName = "Ping"; wirer.MethodName = "OnPinged"; // signature is incompatible wirer.Wire (source, listener); // raise the event on the source source.OnPing (); // ascertain that the event listener was not notified of the raised event Assert.IsFalse (listener.GotPing, "The listener was (incorrectly) notified of the raised event."); Assert.IsFalse (listener.GotPinging, "The listener was (incorrectly) notified of an event that wasn't raised."); Assert.IsFalse (listener.GotPinged, "The listener was (incorrectly) notified of an event that wasn't raised."); }
public void AutowireExplicitly () { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource (); PingListener listener = new PingListener (); // wire them up IEventHandlerValue wirer = new AutoWiringEventHandlerValue (); wirer.EventName = "Ping"; wirer.MethodName = "OnPing"; wirer.Wire (source, listener); // raise the event on the source source.OnPing (); // ascertain that the event listener was indeed notified of the raised event Assert.IsTrue (listener.GotPing, "The listener was not notified of the raised event."); Assert.IsTrue (listener.PingCount == 1, "The listener was notified more than once for a single raising of the source event (registered to listen more than once?)"); Assert.IsFalse (listener.GotPinging, "The listener was (incorrectly) notified of an event that wasn't raised."); Assert.IsFalse (listener.GotPinged, "The listener was (incorrectly) notified of an event that wasn't raised."); }
public void AutowireExplicitlyWithBadSignatureMethod() { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource(); PingListener listener = new PingListener(); // wire them up IEventHandlerValue wirer = new AutoWiringEventHandlerValue(); wirer.EventName = "Ping"; wirer.MethodName = "OnPinged"; // signature is incompatible wirer.Wire(source, listener); // raise the event on the source source.OnPing(); // ascertain that the event listener was not notified of the raised event Assert.IsFalse(listener.GotPing, "The listener was (incorrectly) notified of the raised event."); Assert.IsFalse(listener.GotPinging, "The listener was (incorrectly) notified of an event that wasn't raised."); Assert.IsFalse(listener.GotPinged, "The listener was (incorrectly) notified of an event that wasn't raised."); }
public void TestDefaultMethodMatching() { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource(); PingListener listener = new PerversePingListener(); // wire them up IEventHandlerValue wirer = new AutoWiringEventHandlerValue(); wirer.EventName = "^Ping$"; wirer.MethodName = ".+${event}.+"; // matches 'WhatClubDoYouUsePingAhGood' wirer.Wire(source, listener); // raise the event on the source source.OnPing(); // ascertain that the event listener was not notified of the raised event Assert.IsTrue(listener.GotPing, "The listener was not notified of the raised event."); Assert.IsFalse(listener.GotPinging, "The listener was (incorrectly) notified of an event that wasn't raised."); Assert.IsFalse(listener.GotPinged, "The listener was (incorrectly) notified of an event that wasn't raised."); }
public void AutowiringIsCaseInsensitive() { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource(); PingListener listener = new PingListener(); // wire them up IEventHandlerValue wirer = new AutoWiringEventHandlerValue(); wirer.EventName = "pInG"; wirer.MethodName = "onpiNg"; wirer.Wire(source, listener); // raise the event (s) on the source source.OnPing(); // ascertain that the event listener was indeed notified of the raised event Assert.IsFalse(listener.GotPinging, "The listener was (incorrectly) notified of the raised event."); Assert.IsTrue(listener.GotPing, "The listener was not notified of the raised event."); Assert.IsFalse(listener.GotPinged, "The listener was (incorrectly) notified of the raised event."); }
public void AutowireExplicitly() { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource(); PingListener listener = new PingListener(); // wire them up IEventHandlerValue wirer = new AutoWiringEventHandlerValue(); wirer.EventName = "Ping"; wirer.MethodName = "OnPing"; wirer.Wire(source, listener); // raise the event on the source source.OnPing(); // ascertain that the event listener was indeed notified of the raised event Assert.IsTrue(listener.GotPing, "The listener was not notified of the raised event."); Assert.IsTrue(listener.PingCount == 1, "The listener was notified more than once for a single raising of the source event (registered to listen more than once?)"); Assert.IsFalse(listener.GotPinging, "The listener was (incorrectly) notified of an event that wasn't raised."); Assert.IsFalse(listener.GotPinged, "The listener was (incorrectly) notified of an event that wasn't raised."); }
public void AutowireAllEventsOnSource() { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource(); PingListener listener = new PingListener(); // wire them up (any methods on the listener that have a signature compatible // with any event exposed on the source will be wired up) IEventHandlerValue wirer = new AutoWiringEventHandlerValue(); wirer.Wire(source, listener); // raise the event (s) on the source source.OnPinging(); source.OnPing(); source.OnPinged(); // ascertain that the event listener was indeed notified of the raised event Assert.IsTrue(listener.GotPinging, "The listener was not notified of the raised event."); Assert.IsTrue(listener.GotPing, "The listener was not notified of the raised event."); Assert.IsTrue(listener.GotPinged, "The listener was not notified of the raised event."); }
public void AllCompatibleSignaturesAreWired() { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource(); PingListener listener = new PingListener(); // wire them up IEventHandlerValue wirer = new AutoWiringEventHandlerValue(); wirer.EventName = "Pinging"; wirer.MethodName = ".+"; // both OnPinging and OnPinged have compatible sigs :'( wirer.Wire(source, listener); // raise the event (s) on the source source.OnPinging(); // ascertain that the event listener was indeed notified of the raised event Assert.IsTrue(listener.GotPinging, "The listener was not notified of the raised event."); Assert.IsFalse(listener.GotPing, "The listener was (incorrectly) notified of the raised event."); // the moral of the story is... // don't use greedy method name regex's for autowiring, be explicit Assert.IsTrue(listener.GotPinged, "The listener was not notified of the raised event."); }
/// <summary> /// Factory method for getting concrete /// <see cref="Spring.Objects.IEventHandlerValue"/> instances. /// </summary> /// <param name="methodName"> /// The name of the event handler method. This may be straight text, a regular /// expression, <see langword="null"/>, or empty. /// </param> /// <param name="eventName"> /// The name of the event being wired. This too may be straight text, a regular /// expression, <see langword="null"/>, or empty. /// </param> /// <returns> /// A concrete <see cref="Spring.Objects.IEventHandlerValue"/> /// instance. /// </returns> public static IEventHandlerValue CreateEventHandlerValue( string methodName, string eventName) { bool weAreAutowiring = false; if (StringUtils.HasText(eventName)) { // does the value contain regular expression characters? mmm, totally trent... if (Regex.IsMatch(eventName, @"[\*\.\[\]\{\},\(\)\$\^\+]+")) { // wildcarded event name weAreAutowiring = true; } } else { // we're definitely autowiring based on the event name weAreAutowiring = true; } if (!weAreAutowiring) { if (StringUtils.HasText(methodName)) { // does the value contain the string ${event}? if (methodName.IndexOf("${event}") >= 0) { // wildcarded method name weAreAutowiring = true; } } else { // we're definitely autowiring based on the method name weAreAutowiring = true; } } IEventHandlerValue myHandler; if (weAreAutowiring) { myHandler = new AutoWiringEventHandlerValue(); } else { myHandler = new InstanceEventHandlerValue(); } myHandler.EventName = eventName; myHandler.MethodName = methodName; return myHandler; }
public void AutowireAllHandlersWithCompatibleSignatures () { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource (); PingListener listener = new PingListener (); // wire them up (any methods on the listener that have a compatible signature will be wired up) IEventHandlerValue wirer = new AutoWiringEventHandlerValue (); wirer.EventName = "^Ping$"; wirer.Wire (source, listener); // raise the event on the source source.OnPing (); // ascertain that the event handler was indeed notified of the raised event Assert.IsTrue (listener.GotPing, "The listener was not notified of the raised event."); Assert.IsFalse (listener.GotPinging, "The listener was (incorrectly) notified of an event that wasn't raised."); Assert.IsFalse (listener.GotPinged, "The listener was (incorrectly) notified of an event that wasn't raised."); }
public void TestDefaultMethodMatching () { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource (); PingListener listener = new PerversePingListener (); // wire them up IEventHandlerValue wirer = new AutoWiringEventHandlerValue (); wirer.EventName = "^Ping$"; wirer.MethodName = ".+${event}.+"; // matches 'WhatClubDoYouUsePingAhGood' wirer.Wire (source, listener); // raise the event on the source source.OnPing (); // ascertain that the event listener was not notified of the raised event Assert.IsTrue (listener.GotPing, "The listener was not notified of the raised event."); Assert.IsFalse (listener.GotPinging, "The listener was (incorrectly) notified of an event that wasn't raised."); Assert.IsFalse (listener.GotPinged, "The listener was (incorrectly) notified of an event that wasn't raised."); }
public void AllCompatibleSignaturesAreWired () { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource (); PingListener listener = new PingListener (); // wire them up IEventHandlerValue wirer = new AutoWiringEventHandlerValue (); wirer.EventName = "Pinging"; wirer.MethodName = ".+"; // both OnPinging and OnPinged have compatible sigs :'( wirer.Wire (source, listener); // raise the event (s) on the source source.OnPinging (); // ascertain that the event listener was indeed notified of the raised event Assert.IsTrue (listener.GotPinging, "The listener was not notified of the raised event."); Assert.IsFalse (listener.GotPing, "The listener was (incorrectly) notified of the raised event."); // the moral of the story is... // don't use greedy method name regex's for autowiring, be explicit Assert.IsTrue (listener.GotPinged, "The listener was not notified of the raised event."); }
public void AutowiringIsCaseInsensitive () { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource (); PingListener listener = new PingListener (); // wire them up IEventHandlerValue wirer = new AutoWiringEventHandlerValue (); wirer.EventName = "pInG"; wirer.MethodName = "onpiNg"; wirer.Wire (source, listener); // raise the event (s) on the source source.OnPing (); // ascertain that the event listener was indeed notified of the raised event Assert.IsFalse (listener.GotPinging, "The listener was (incorrectly) notified of the raised event."); Assert.IsTrue (listener.GotPing, "The listener was not notified of the raised event."); Assert.IsFalse (listener.GotPinged, "The listener was (incorrectly) notified of the raised event."); }
public void AutowireAllEventsOnSource () { // create the event source, and a listener to wire to an event (s) on the source PingSource source = new PingSource (); PingListener listener = new PingListener (); // wire them up (any methods on the listener that have a signature compatible // with any event exposed on the source will be wired up) IEventHandlerValue wirer = new AutoWiringEventHandlerValue (); wirer.Wire (source, listener); // raise the event (s) on the source source.OnPinging (); source.OnPing (); source.OnPinged (); // ascertain that the event listener was indeed notified of the raised event Assert.IsTrue (listener.GotPinging, "The listener was not notified of the raised event."); Assert.IsTrue (listener.GotPing, "The listener was not notified of the raised event."); Assert.IsTrue (listener.GotPinged, "The listener was not notified of the raised event."); }