public void AssignObjectNames()
        {
            string className = typeof (DependenciesObject).FullName;

            string targetName = className + ObjectDefinitionReaderUtils.GENERATED_OBJECT_NAME_SEPARATOR + "0";
            DependenciesObject topLevel1 = (DependenciesObject) objectFactory.GetObject(targetName);
            Assert.IsNotNull(topLevel1);

            targetName = className + ObjectDefinitionReaderUtils.GENERATED_OBJECT_NAME_SEPARATOR + "1";
            DependenciesObject topLevel2 = (DependenciesObject)objectFactory.GetObject(targetName);
            Assert.IsNotNull(topLevel1);

            targetName = className + ObjectDefinitionReaderUtils.GENERATED_OBJECT_NAME_SEPARATOR + "2";
            DependenciesObject topLevel3 = (DependenciesObject)objectFactory.GetObject(targetName);
            Assert.IsNotNull(topLevel1);


            string childClassName = typeof(TestObject).FullName;
            TestObject child1 = (TestObject) topLevel1.Spouse;
            Assert.IsNotNull(child1);
            Assert.IsTrue(child1.ObjectName.IndexOf(childClassName) != -1);

            TestObject child2 = (TestObject)topLevel2.Spouse;
            Assert.IsNotNull(child2);
            Assert.IsTrue(child2.ObjectName.IndexOf(childClassName) != -1);

            TestObject child3 = (TestObject)topLevel3.Spouse;
            Assert.IsNotNull(child3);
            Assert.IsTrue(child3.ObjectName.IndexOf(childClassName) != -1);

            Assert.AreNotEqual(child1.ObjectName, child2.ObjectName);
        }
        public BeanFactoryTask(int iterations)
            : base(iterations)
        {
            InitializeFactory();
            FFoo foo = (FFoo)factory.GetObject("foo");

            Assert.AreEqual(5, foo.i);
            Assert.AreEqual("test", foo.s);
            Assert.AreSame(foo.bar.tee, foo.copy.tee);
            Assert.AreEqual(5, foo.bar.i);
            Assert.AreEqual("test", foo.bar.Tee.S);
        }
        public void ThrowsTypeLoadExceptionIfProxyInterfacesValueIsSpecifiedInsteadOfListElement()
        {
            using (DefaultListableObjectFactory of = new DefaultListableObjectFactory())
            {
                XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(of);
                reader.LoadObjectDefinitions(new StringResource(
                                                 @"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net' xmlns:r='http://www.springframework.net/remoting'>  
    
    <r:saoExporter id='ISimpleCounterExporter' targetName='ISimpleCounterProxy' serviceName='RemotedSaoCounterProxy' />
    
    <object id='ISimpleCounter' type='Spring.Remoting.SimpleCounter, Spring.Services.Tests' />

    <object id='ISimpleCounterProxy' type='Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop'>
        <property name='proxyInterfaces' value='Spring.Remoting.ISimpleCounter, Spring.Services.Tests' />
        <property name='target' ref='ISimpleCounter'/>
    </object>
</objects>
"));
                try
                {
                    SaoExporter saoExporter = (SaoExporter)of.GetObject("ISimpleCounterExporter");
                    Assert.Fail();
                }
                catch (ObjectCreationException oce)
                {
                    TypeLoadException tle = (TypeLoadException)oce.GetBaseException();
                    Assert.AreEqual("Could not load type from string value ' Spring.Services.Tests'.", tle.Message);
                }
            }
        }
Beispiel #4
0
        public void ParsesNonDefaultNamespace()
        {
            try
            {
                NamespaceParserRegistry.Reset();

                DefaultListableObjectFactory of     = new DefaultListableObjectFactory();
                XmlObjectDefinitionReader    reader = new XmlObjectDefinitionReader(of);
                reader.LoadObjectDefinitions(new StringResource(
                                                 @"<?xml version='1.0' encoding='UTF-8' ?>
<core:objects xmlns:core='http://www.springframework.net'>  
	<core:object id='test2' type='Spring.Objects.TestObject, Spring.Core.Tests'>
        <core:property name='Sibling'>
            <core:object type='Spring.Objects.TestObject, Spring.Core.Tests' />
        </core:property>
    </core:object>
</core:objects>
"));
                TestObject test2 = (TestObject)of.GetObject("test2");
                Assert.AreEqual(typeof(TestObject), test2.GetType());
                Assert.IsNotNull(test2.Sibling);
            }
            finally
            {
                NamespaceParserRegistry.Reset();
            }
        }
        public void ProxyTransparentProxy()
        {
            DefaultListableObjectFactory of = new DefaultListableObjectFactory();

            ConstructorArgumentValues ctorArgs = new ConstructorArgumentValues();

            ctorArgs.AddNamedArgumentValue("objectType", typeof(ITestObject));
            of.RegisterObjectDefinition("bar", new RootObjectDefinition(typeof(TransparentProxyFactory), ctorArgs, null));

            TestAutoProxyCreator apc = new TestAutoProxyCreator(of);

            of.AddObjectPostProcessor(apc);

            ITestObject o = of.GetObject("bar") as ITestObject;

            Assert.IsTrue(AopUtils.IsAopProxy(o));

            // ensure interceptors get called
            o.Foo();
            Assert.AreEqual(1, apc.NopInterceptor.Count);
            IAdvised advised = (IAdvised)o;

            // ensure target was called
            object target = advised.TargetSource.GetTarget();

            Assert.AreEqual(1, TransparentProxyFactory.GetRealProxy(target).Count);
        }
Beispiel #6
0
        [Ignore] //this test cannot co-exist with AutoRegistersAllWellknownNamespaceParsers b/c that test will have already loaded the Spring.Data ass'y
        public void AutoRegistersWellknownNamespaceParser()
        {
            try
            {
                Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
                foreach (Assembly assembly in loadedAssemblies)
                {
                    if (assembly.GetName(true).Name.StartsWith("Spring.Data"))
                    {
                        Assert.Fail("Spring.Data is already loaded - this test checks if it gets loaded during xml parsing");
                    }
                }

                NamespaceParserRegistry.Reset();

                DefaultListableObjectFactory of     = new DefaultListableObjectFactory();
                XmlObjectDefinitionReader    reader = new XmlObjectDefinitionReader(of);
                reader.LoadObjectDefinitions(new StringResource(
                                                 @"<?xml version='1.0' encoding='UTF-8' ?>
                                                    <objects xmlns='http://www.springframework.net' 
                                                             xmlns:tx='http://www.springframework.net/tx'>  
                                                          <tx:attribute-driven />
                                                    </objects>
                                                    "));
                object apc = of.GetObject(AopNamespaceUtils.AUTO_PROXY_CREATOR_OBJECT_NAME);
                Assert.NotNull(apc);
            }
            finally
            {
                NamespaceParserRegistry.Reset();
            }
        }
Beispiel #7
0
 protected override void AddPersistenceExceptionTranslation(ProxyFactory pf, IPersistenceExceptionTranslator pet)
 {
     if (AttributeUtils.FindAttribute(pf.TargetType, typeof(RepositoryAttribute)) != null)
     {
         DefaultListableObjectFactory of = new DefaultListableObjectFactory();
         of.RegisterObjectDefinition("peti", new RootObjectDefinition(typeof(PersistenceExceptionTranslationInterceptor)));
         of.RegisterSingleton("pet", pet);
         pf.AddAdvice((PersistenceExceptionTranslationInterceptor)of.GetObject("peti"));
     }
 }
Beispiel #8
0
        public void Can_Create_Custom_Scan_Routine()
        {
            var scanner  = new ScanOverridingAssemblyObjectDefinitionScanner();
            var registry = new DefaultListableObjectFactory();

            scanner.ScanAndRegisterTypes(registry);
            Assert.That(registry.ObjectDefinitionCount, Is.EqualTo(1), "found multiple definitions");
            Assert.That(registry.GetObject <ComponentScan.ScanComponentsAndAddToContext.ConfigurationImpl>(), Is.Not.Null,
                        "correct single defintion was not registered");
        }
Beispiel #9
0
        public void ShouldAllowConfigurationClassInheritance()
        {
            var factory = new DefaultListableObjectFactory();

            factory.RegisterObjectDefinition("DerivedConfiguration", new GenericObjectDefinition
            {
                ObjectType = typeof(DerivedConfiguration)
            });

            var processor = new ConfigurationClassPostProcessor();

            processor.PostProcessObjectFactory(factory);

            // we should get singleton instances only
            TestObject testObject      = (TestObject)factory.GetObject("DerivedDefinition");
            string     singletonParent = (string)factory.GetObject("BaseDefinition");


            Assert.That(testObject.Value, Is.SameAs(singletonParent));
        }
Beispiel #10
0
        public void FailWithReplaceMethodOnSealedClass()
        {
            RootObjectDefinition replacerDef = new RootObjectDefinition(typeof(NewsFeedFactory));

            RootObjectDefinition managerDef = new RootObjectDefinition(typeof(SealedReturnsNullNewsFeedManager));

            managerDef.MethodOverrides.Add(new ReplacedMethodOverride("CreateNewsFeed", "replacer"));

            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            factory.RegisterObjectDefinition("manager", managerDef);
            factory.RegisterObjectDefinition("replacer", replacerDef);
            Assert.Throws <ObjectCreationException>(() => factory.GetObject("manager"));
        }
Beispiel #11
0
        public virtual void FactoryNesting()
        {
            ITestObject father = (ITestObject)ObjectFactory.GetObject("father");

            Assert.IsTrue(father != null, "Object from root context");

            ITestObject rod = (ITestObject)ObjectFactory.GetObject("rod");

            Assert.IsTrue("Rod".Equals(rod.Name), "Object from child context");
            Assert.IsTrue(rod.Spouse == father, "Object has external reference");

            rod = (ITestObject)parent.GetObject("rod");
            Assert.IsTrue("Roderick".Equals(rod.Name), "Object from root context");
        }
Beispiel #12
0
        public void WhitespaceValuesResultInEmptyStringForValueElement()
        {
            DefaultListableObjectFactory of     = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader    reader = new XmlObjectDefinitionReader(of);

            reader.LoadObjectDefinitions(new StringResource(
                                             @"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>  
	<object id='test2' type='Spring.Objects.TestObject, Spring.Core.Tests'>
		<property name='name'><value /></property>
	</object>
	<object id='test3' type='Spring.Objects.TestObject, Spring.Core.Tests'>
		<property name='name'><value></value></property>
	</object>
	<object id='test4' type='Spring.Objects.TestObject, Spring.Core.Tests'>
		<property name='name'><value xml:space='default'> &#x000a;&#x000d;&#x0009;</value></property>
	</object>
</objects>
"));
            Assert.AreEqual(string.Empty, ((TestObject)of.GetObject("test2")).Name);
            Assert.AreEqual(string.Empty, ((TestObject)of.GetObject("test3")).Name);
            Assert.AreEqual(string.Empty, ((TestObject)of.GetObject("test4")).Name);
        }
Beispiel #13
0
        public void WhitespaceValuesArePreservedForValueElementWhenSpaceIsSetToPreserve()
        {
            DefaultListableObjectFactory of     = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader    reader = new XmlObjectDefinitionReader(of);

            reader.LoadObjectDefinitions(new StringResource(
                                             @"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>  
	<object id='test4' type='Oragon.Spring.Objects.TestObject, Oragon.Spring.Core.Tests'>
		<property name='name'><value xml:space='preserve'> &#x000a;&#x000d;&#x0009;</value></property>
	</object>
</objects>
"));
            Assert.AreEqual(" \n\r\t", ((TestObject)of.GetObject("test4")).Name);
        }
Beispiel #14
0
        public void WhitespaceValuesArePreservedForValueAttribute()
        {
            DefaultListableObjectFactory of     = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader    reader = new XmlObjectDefinitionReader(of);

            reader.LoadObjectDefinitions(new StringResource(
                                             @"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>  
	<object id='test' type='Spring.Objects.TestObject, Spring.Core.Tests'>
		<property name='name' value=' &#x000a;&#x000d;&#x0009;' />
	</object>
</objects>
"));
            Assert.AreEqual(" \n\r\t", ((TestObject)of.GetObject("test")).Name);
        }
        public void ProxyObjectWithoutInterface()
        {
            DefaultListableObjectFactory of = new DefaultListableObjectFactory();

            of.RegisterObjectDefinition("bar", new RootObjectDefinition(typeof(ObjectWithoutInterface)));

            TestAutoProxyCreator apc = new TestAutoProxyCreator(of);

            of.AddObjectPostProcessor(apc);

            ObjectWithoutInterface o = of.GetObject("bar") as ObjectWithoutInterface;

            Assert.IsTrue(AopUtils.IsAopProxy(o));
            o.Foo();
            Assert.AreEqual(1, apc.NopInterceptor.Count);
        }
        public void Test()
        {
            int numIterations = 10000;

            start = DateTime.Now;
            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            InitFactory(factory);
            for (int i = 0; i < numIterations; i++)
            {
                FFoo foo = (FFoo)factory.GetObject("foo");
            }
            stop = DateTime.Now;
            double timeElapsed = Elapsed;

            PrintTest("Creations", numIterations, timeElapsed);
        }
        public void FailOnMethodWithVoidReturnType()
        {
            RootObjectDefinition feedDef = new RootObjectDefinition(typeof(NewsFeed));

            feedDef.IsSingleton = false;
            feedDef.PropertyValues.Add("name", "Bingo");

            RootObjectDefinition managerDef = new RootObjectDefinition(typeof(ReturnsVoidNewsFeedManager));

            managerDef.MethodOverrides.Add(new LookupMethodOverride("CreateNewsFeed", "feed"));

            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            factory.RegisterObjectDefinition("manager", managerDef);
            factory.RegisterObjectDefinition("feed", feedDef);
            factory.GetObject("manager");
        }
Beispiel #18
0
        public void ConvertsWriteConcernCorrectly()
        {
            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            factory.RegisterCustomConverter(typeof(WriteConcern), new WriteConcernTypeConverter());

            RootObjectDefinition definition = new RootObjectDefinition(typeof(MongoFactoryObject));

            definition.PropertyValues.Add("Url", "mongodb://localhost");
            definition.PropertyValues.Add("WriteConcern", "Acknowledged");
            factory.RegisterObjectDefinition("factory", definition);

            MongoFactoryObject obj = factory.GetObject <MongoFactoryObject>("&factory");

            Assert.That(ReflectionUtils.GetInstanceFieldValue(obj, "_writeConcern"),
                        Is.EqualTo(WriteConcern.Acknowledged));
        }
Beispiel #19
0
        public void FailOnNonVirtualProtectedMethod()
        {
            RootObjectDefinition feedDef = new RootObjectDefinition(typeof(NewsFeed));

            feedDef.IsSingleton = false;
            feedDef.PropertyValues.Add("name", "Bingo");

            RootObjectDefinition managerDef = new RootObjectDefinition(typeof(ProtectedReturnsNullNewsFeedManager));

            managerDef.MethodOverrides.Add(new LookupMethodOverride("CreateNewsFeed", "feed"));

            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            factory.RegisterObjectDefinition("manager", managerDef);
            factory.RegisterObjectDefinition("feed", feedDef);
            Assert.Throws <ObjectCreationException>(() => factory.GetObject("manager"));
        }
Beispiel #20
0
        public void FailOnLookupMethodThatHasArguments()
        {
            RootObjectDefinition feedDef = new RootObjectDefinition(typeof(NewsFeed));

            feedDef.IsSingleton = false;
            feedDef.PropertyValues.Add("name", "Bingo");

            RootObjectDefinition managerDef = new RootObjectDefinition(typeof(NewsFeedManagerWithLookupMethodThatTakesArguments));

            managerDef.MethodOverrides.Add(new LookupMethodOverride("CreateNewsFeed", "feed"));

            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            factory.RegisterObjectDefinition("manager", managerDef);
            factory.RegisterObjectDefinition("feed", feedDef);
            Assert.Throws <ObjectCreationException>(() => factory.GetObject("manager"));
        }
            /// <summary>
            /// Applies attributes to the proxy class.
            /// </summary>
            /// <param name="typeBuilder">The type builder to use.</param>
            /// <param name="targetType">The proxied class.</param>
            /// <see cref="IProxyTypeBuilder.ProxyTargetAttributes"/>
            /// <see cref="IProxyTypeBuilder.TypeAttributes"/>
            protected override void ApplyTypeAttributes(TypeBuilder typeBuilder, Type targetType)
            {
                foreach (object attr in GetTypeAttributes(targetType))
                {
                    if (attr is CustomAttributeBuilder)
                    {
                        typeBuilder.SetCustomAttribute((CustomAttributeBuilder)attr);
                    }
                    else if (attr is CustomAttributeData)
                    {
                        typeBuilder.SetCustomAttribute(
                            ReflectionUtils.CreateCustomAttribute((CustomAttributeData)attr));
                    }
                    else if (attr is Attribute)
                    {
                        typeBuilder.SetCustomAttribute(
                            ReflectionUtils.CreateCustomAttribute((Attribute)attr));
                    }
                    else if (attr is IObjectDefinition)
                    {
                        RootObjectDefinition objectDefinition = (RootObjectDefinition)attr;

                        //TODO check that object definition is for an Attribute type.

                        //Change object definition so it can be instantiated and make prototype scope.
                        objectDefinition.IsAbstract  = false;
                        objectDefinition.IsSingleton = false;
                        string objectName = ObjectDefinitionReaderUtils.GenerateObjectName(objectDefinition, objectFactory);
                        objectFactory.RegisterObjectDefinition(objectName, objectDefinition);


                        //find constructor and constructor arg values to create this attribute.
                        ConstructorResolver constructorResolver = new ConstructorResolver(objectFactory, objectFactory,
                                                                                          new SimpleInstantiationStrategy(),
                                                                                          new ObjectDefinitionValueResolver(objectFactory));


                        ConstructorInstantiationInfo ci = constructorResolver.GetConstructorInstantiationInfo(objectName,
                                                                                                              objectDefinition,
                                                                                                              null, null);

                        if (objectDefinition.PropertyValues.PropertyValues.Count == 0)
                        {
                            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo,
                                                                                    ci.ArgInstances);
                            typeBuilder.SetCustomAttribute(cab);
                        }
                        else
                        {
                            object         attributeInstance        = objectFactory.GetObject(objectName);
                            IObjectWrapper wrappedAttributeInstance = new ObjectWrapper(attributeInstance);
                            PropertyInfo[] namedProperties          = wrappedAttributeInstance.GetPropertyInfos();
                            object[]       propertyValues           = new object[namedProperties.Length];
                            for (int i = 0; i < namedProperties.Length; i++)
                            {
                                propertyValues[i] =
                                    wrappedAttributeInstance.GetPropertyValue(namedProperties[i].Name);
                            }
                            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo, ci.ArgInstances,
                                                                                    namedProperties, propertyValues);
                            typeBuilder.SetCustomAttribute(cab);
                        }
                    }
                }
            }