public void BailsIfMethodDoesntExist () 
 {
     PingSource source = new PingSource ();
     InstanceEventHandlerValue wirer
         = new InstanceEventHandlerValue (source, "Roo");
     wirer.EventName = "Ping";
     wirer.Wire (source, typeof (StaticPingListener));
 }
 public void BailsIfMethodDoesntExist () 
 {
     PingSource source = new PingSource ();
     InstanceEventHandlerValue wirer
         = new InstanceEventHandlerValue (source, "Roo");
     wirer.EventName = "Ping";
     Assert.Throws<FatalObjectException>(() => wirer.Wire (source, typeof (StaticPingListener)));
 }
 public void WireWithStaticHandler () 
 {
     PingSource source = new PingSource ();
     InstanceEventHandlerValue wirer
         = new InstanceEventHandlerValue (source, "OnPing");
     wirer.EventName = "Ping";
     wirer.Wire (source, typeof (StaticPingListener));
 }
예제 #4
0
        public void BailsIfMethodDoesntExist()
        {
            PingSource source = new PingSource();
            InstanceEventHandlerValue wirer
                = new InstanceEventHandlerValue(source, "Roo");

            wirer.EventName = "Ping";
            Assert.Throws <FatalObjectException>(() => wirer.Wire(source, typeof(StaticPingListener)));
        }
예제 #5
0
        public void WireWithStaticHandler()
        {
            PingSource source = new PingSource();
            InstanceEventHandlerValue wirer
                = new InstanceEventHandlerValue(source, "OnPing");

            wirer.EventName = "Ping";
            wirer.Wire(source, typeof(StaticPingListener));
        }
예제 #6
0
        public void BailsIfMethodDoesntExist()
        {
            PingSource source = new PingSource();
            InstanceEventHandlerValue wirer
                = new InstanceEventHandlerValue(source, "Roo");

            wirer.EventName = "Ping";
            wirer.Wire(source, typeof(StaticPingListener));
        }
 public void Wire () {
     InstanceEventHandlerValue wirer = new InstanceEventHandlerValue ();
     wirer.EventName = "Ping";
     wirer.MethodName = "OnPing";
     PingSource source = new PingSource ();
     PingListener sink = new PingListener ();
     wirer.Wire (source, sink);
     source.OnPing ();
     Assert.IsTrue (sink.GotPing, "The event handler did not get notified when the event was raised.");
     Assert.AreEqual (1, sink.PingCount, "The event handler was not get notified exactly once when the event was raised exactly once.");
 }
예제 #8
0
        public void Wire()
        {
            InstanceEventHandlerValue wirer = new InstanceEventHandlerValue();

            wirer.EventName  = "Ping";
            wirer.MethodName = "OnPing";
            PingSource   source = new PingSource();
            PingListener sink   = new PingListener();

            wirer.Wire(source, sink);
            source.OnPing();
            Assert.IsTrue(sink.GotPing, "The event handler did not get notified when the event was raised.");
            Assert.AreEqual(1, sink.PingCount, "The event handler was not get notified exactly once when the event was raised exactly once.");
        }
        public void InstantiationFromOther()
        {
            RootObjectDefinition other
                = new RootObjectDefinition( typeof( TestObject ), AutoWiringMode.Constructor );
            other.IsSingleton = false;
            other.InitMethodName = "Umberto Eco";
            other.DestroyMethodName = "Pedulismus";
            other.ConstructorArgumentValues = new ConstructorArgumentValues();
            other.DependencyCheck = DependencyCheckingMode.Objects;
            other.DependsOn = new string[] { "Jimmy", "Joyce" };
            other.EventHandlerValues = new EventValues();
            other.IsAbstract = true;
            other.IsLazyInit = true;
            other.FactoryMethodName = "IfINeedYouIllJustUseYourSimpleName";
            other.FactoryObjectName = "PerspirationShop";
            other.ResourceDescription = "Ohhh";
            other.PropertyValues = new MutablePropertyValues();
            other.PropertyValues.Add( "Age", 100 );
            InstanceEventHandlerValue handler =
                new InstanceEventHandlerValue( new TestObject(), "Ping" );
            handler.EventName = "Bing";
            other.EventHandlerValues.AddHandler( handler );
            other.ConstructorArgumentValues.AddGenericArgumentValue( "Wulf", "Sternhammer" );

            RootObjectDefinition def = new RootObjectDefinition( other );

            // ...
            Assert.AreEqual( typeof( TestObject ), def.ObjectType );
            Assert.AreEqual( AutoWiringMode.Constructor, def.AutowireMode );
            Assert.IsFalse( def.IsSingleton );
            Assert.AreEqual( "Umberto Eco", def.InitMethodName );
            Assert.AreEqual( "Pedulismus", def.DestroyMethodName );
            Assert.AreEqual( DependencyCheckingMode.Objects, def.DependencyCheck );
            Assert.IsTrue( def.IsAbstract );
            Assert.IsTrue( def.IsLazyInit );
            Assert.AreEqual( "IfINeedYouIllJustUseYourSimpleName", def.FactoryMethodName );
            Assert.AreEqual( "PerspirationShop", def.FactoryObjectName );
            Assert.AreEqual( "Ohhh", def.ResourceDescription );
            Assert.IsTrue( def.HasConstructorArgumentValues );
            Assert.AreEqual( other.ConstructorArgumentValues.ArgumentCount, def.ConstructorArgumentValues.ArgumentCount );
            Assert.AreEqual( other.PropertyValues.PropertyValues.Count, def.PropertyValues.PropertyValues.Count );
            Assert.AreEqual( other.EventHandlerValues.Events.Count, def.EventHandlerValues.Events.Count );
        }
 /// <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 OverrideFromOther()
        {
            RootObjectDefinition rod = new RootObjectDefinition( typeof( TestObject ), AutoWiringMode.Constructor );
            rod.IsSingleton = false;
            rod.InitMethodName = "Umberto Eco";
            rod.DestroyMethodName = "Pedulismus";
            rod.ConstructorArgumentValues = new ConstructorArgumentValues();
            rod.ConstructorArgumentValues.AddGenericArgumentValue( "Wulf", "Sternhammer" );
            rod.DependencyCheck = DependencyCheckingMode.Objects;
            rod.DependsOn = new string[] { "Jimmy", "Joyce" };
            rod.IsAbstract = true;
            rod.IsLazyInit = true;
            rod.FactoryMethodName = "IfINeedYouIllJustUseYourSimpleName";
            rod.FactoryObjectName = "PerspirationShop";
            rod.ResourceDescription = "Ohhh";
            rod.PropertyValues = new MutablePropertyValues();
            rod.PropertyValues.Add( "Age", 100 );
            rod.EventHandlerValues = new EventValues();
            InstanceEventHandlerValue handler = new InstanceEventHandlerValue( new TestObject(), "Ping" );
            handler.EventName = "Bing";
            rod.EventHandlerValues.AddHandler( handler );

            ChildObjectDefinition cod = new ChildObjectDefinition( "parent" );
            cod.ObjectTypeName = "ChildObjectTypeName";
            cod.IsSingleton = true;
            cod.AutowireMode = AutoWiringMode.ByType;
            cod.InitMethodName = "InitChild";
            cod.DestroyMethodName = "DestroyChild";
            cod.ConstructorArgumentValues = new ConstructorArgumentValues();
            cod.ConstructorArgumentValues.AddNamedArgumentValue( "ctorarg", "ctorarg-value" );
            cod.DependencyCheck = DependencyCheckingMode.None;
            cod.DependsOn = new string[] { "Aleks", "Mark" };
            cod.IsAbstract = false;
            cod.IsLazyInit = false;
            cod.FactoryMethodName = "ChildFactoryMethodName";
            cod.FactoryObjectName = "ChildFactoryObjectName";
            cod.ResourceDescription = "ChildResourceDescription";
            cod.PropertyValues = new MutablePropertyValues();
            cod.PropertyValues.Add( "Prop1", "Val1" );
            cod.PropertyValues.Add( "Age", 50 );
            cod.EventHandlerValues = new EventValues();
            handler = new InstanceEventHandlerValue( new TestObject(), "Pong" );
            handler.EventName = "Bong";
            cod.EventHandlerValues.AddHandler( handler );

            rod.OverrideFrom( cod );

            // ...
            Assert.AreEqual( "ChildObjectTypeName", rod.ObjectTypeName );
            Assert.AreEqual( AutoWiringMode.ByType, rod.AutowireMode );
            Assert.AreEqual( true, rod.IsSingleton );
            Assert.AreEqual( "InitChild", rod.InitMethodName );
            Assert.AreEqual( "DestroyChild", rod.DestroyMethodName );
            Assert.AreEqual( DependencyCheckingMode.None, rod.DependencyCheck );
            Assert.AreEqual( 4, rod.DependsOn.Count);
            Assert.AreEqual( false, rod.IsAbstract );
            Assert.AreEqual( false, rod.IsLazyInit );
            Assert.AreEqual( "ChildFactoryMethodName", rod.FactoryMethodName );
            Assert.AreEqual( "ChildFactoryObjectName", rod.FactoryObjectName );
            Assert.AreEqual( "ChildResourceDescription", rod.ResourceDescription );
            Assert.AreEqual( 2, rod.ConstructorArgumentValues.ArgumentCount );
            Assert.AreEqual( 2, rod.PropertyValues.PropertyValues.Count );
            Assert.AreEqual( "Val1", rod.PropertyValues.GetPropertyValue("Prop1").Value);
            Assert.AreEqual( 50, rod.PropertyValues.GetPropertyValue("Age").Value);
            Assert.AreEqual( 2, rod.EventHandlerValues.Events.Count );
        }