Ejemplo n.º 1
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 LoadObjectDefinitionsWithNullResource()
 {
     XmlObjectDefinitionReader reader
         = new XmlObjectDefinitionReader(
             new DefaultListableObjectFactory());
     reader.LoadObjectDefinitions((string)null);
 }
Ejemplo n.º 3
0
        public void ParsesAutowireCandidate()
        {
            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' default-autowire-candidates='test1*,test4*'>  
	<object id='test1' type='Spring.Objects.TestObject, Spring.Core.Tests' />
	<object id='test2' type='Spring.Objects.TestObject, Spring.Core.Tests' autowire-candidate='false' />
	<object id='test3' type='Spring.Objects.TestObject, Spring.Core.Tests' autowire-candidate='true' />
	<object id='test4' type='Spring.Objects.TestObject, Spring.Core.Tests' autowire-candidate='default' />
	<object id='test5' type='Spring.Objects.TestObject, Spring.Core.Tests' autowire-candidate='default' />
</objects>
"));
            var od = (AbstractObjectDefinition)of.GetObjectDefinition("test1");

            Assert.That(od.IsAutowireCandidate, Is.True, "No attribute set should default to true");

            od = (AbstractObjectDefinition)of.GetObjectDefinition("test2");
            Assert.That(od.IsAutowireCandidate, Is.False, "Specifically attribute set to false should set to false");

            od = (AbstractObjectDefinition)of.GetObjectDefinition("test3");
            Assert.That(od.IsAutowireCandidate, Is.True, "Specifically attribute set to true should set to false");

            od = (AbstractObjectDefinition)of.GetObjectDefinition("test4");
            Assert.That(od.IsAutowireCandidate, Is.True, "Attribute set to default should check pattern and return true");

            od = (AbstractObjectDefinition)of.GetObjectDefinition("test5");
            Assert.That(od.IsAutowireCandidate, Is.False, "Attribute set to default should check pattern and return false");
        }
Ejemplo n.º 4
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();
            }
        }
 public void LoadObjectDefinitionsWithNonExistentResource()
 {
     XmlObjectDefinitionReader reader
         = new XmlObjectDefinitionReader(
             new DefaultListableObjectFactory());
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("/dev/null"));
 }
        public void SetUp()
        {
            this.objectFactory = new DefaultListableObjectFactory();
            IObjectDefinitionReader reader = new XmlObjectDefinitionReader(this.objectFactory);

            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("collectionMergingGeneric.xml", GetType()));
        }
Ejemplo n.º 7
0
        public void ParsesObjectAttributes()
        {
            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='test1' type='Spring.Objects.TestObject, Spring.Core.Tests' singleton='false' abstract='true' />
	<object id='test2' type='Spring.Objects.TestObject, Spring.Core.Tests' singleton='true' abstract='false' lazy-init='true' 
        autowire='no' dependency-check='simple'
        depends-on='test1' 
        init-method='init'
        destroy-method='destroy'
    />
</objects>
"));
            AbstractObjectDefinition od1 = (AbstractObjectDefinition)of.GetObjectDefinition("test1");

            Assert.IsFalse(od1.IsSingleton);
            Assert.IsTrue(od1.IsAbstract);
            Assert.IsFalse(od1.IsLazyInit);

            AbstractObjectDefinition od2 = (AbstractObjectDefinition)of.GetObjectDefinition("test2");

            Assert.IsTrue(od2.IsSingleton);
            Assert.IsFalse(od2.IsAbstract);
            Assert.IsTrue(od2.IsLazyInit);
            Assert.AreEqual(AutoWiringMode.No, od2.AutowireMode);
            Assert.AreEqual("init", od2.InitMethodName);
            Assert.AreEqual("destroy", od2.DestroyMethodName);
            Assert.AreEqual(1, od2.DependsOn.Count);
            Assert.AreEqual("test1", od2.DependsOn[0]);
            Assert.AreEqual(DependencyCheckingMode.Simple, od2.DependencyCheck);
        }
        public void Setup()
        {
            objectFactory = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(objectFactory);

            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("objectNameGeneration.xml", GetType()));
        }
Ejemplo n.º 9
0
        public void LoadObjectDefinitionsWithNullResource()
        {
            XmlObjectDefinitionReader reader
                = new XmlObjectDefinitionReader(
                      new DefaultListableObjectFactory());

            reader.LoadObjectDefinitions((string)null);
        }
Ejemplo n.º 10
0
        public void LoadObjectDefinitionsWithNonExistentResource()
        {
            XmlObjectDefinitionReader reader
                = new XmlObjectDefinitionReader(
                      new DefaultListableObjectFactory());

            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("/dev/null"));
        }
Ejemplo n.º 11
0
 public virtual void InstanceEventWiring()
 {
     DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("event-wiring.xml", GetType()));
     ITestObject source = factory["source"] as ITestObject;
     TestEventHandler instanceHandler = factory["instanceEventListener"] as TestEventHandler;
     // raise the event... handlers should be notified at this point (obviously)
     source.OnClick();
     Assert.IsTrue(instanceHandler.EventWasHandled,
                   "The instance handler did not get notified when the instance event was raised (and was probably not wired up in the first place).");
 }
Ejemplo n.º 12
0
        public virtual void StaticEventWiring()
        {
            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader    reader  = new XmlObjectDefinitionReader(factory);

            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("event-wiring.xml", GetType()));
            TestEventHandler staticHandler = factory["staticEventListener"] as TestEventHandler;

            // raise the event... handlers should be notified at this point (obviously)
            TestObject.OnStaticClick();
            Assert.IsTrue(staticHandler.EventWasHandled,
                          "The instance handler did not get notified when the static event was raised (and was probably not wired up in the first place).");
        }
Ejemplo n.º 13
0
        public virtual void EventWiringInstanceSinkToPrototypeSource()
        {
            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader    reader  = new XmlObjectDefinitionReader(factory);

            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("event-wiring-prototypes.xml", GetType()));
            TestEventHandler instanceHandler = factory["instanceSink"] as TestEventHandler;
            ITestObject      source          = factory["source"] as ITestObject;

            // raise the event... handlers should be notified at this point (obviously)
            source.OnClick();
            Assert.IsTrue(instanceHandler.EventWasHandled,
                          "The instance handler did not get notified when the instance event was raised (and was probably not wired up in the first place).");
        }
Ejemplo n.º 14
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='Spring.Objects.TestObject, 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);
        }
        public void ThrowsOnUnknownNamespaceUri()
        {
            NamespaceParserRegistry.Reset();

            DefaultListableObjectFactory of     = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader    reader = new XmlObjectDefinitionReader(of);

            Assert.Throws <ObjectDefinitionStoreException>(() => reader.LoadObjectDefinitions(new StringResource(
                                                                                                  @"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net' 
     xmlns:x='http://www.springframework.net/XXXX'>  
  <x:group id='tripValidator' />
</objects>
")));
        }
Ejemplo n.º 16
0
        public void ThrowsObjectDefinitionStoreExceptionOnInvalidXml()
        {
            try
            {
                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'>
                                                </objects>
                                                "));
                Assert.Fail();
            }
            catch (ObjectDefinitionStoreException ex)
            {
                Assert.IsTrue(ex.Message.IndexOf("Line 4 in XML document from  is not well formed.") > -1);
            }
        }
Ejemplo n.º 17
0
 public void StaticFieldRetrievingFactoryMethod()
 {
     DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("field-props-factory.xml", GetType()));
     MyTestObject obj = factory.GetObject("withTypesField", typeof(MyTestObject)) as MyTestObject;
     Assert.IsNotNull(obj.Types);
     Assert.AreEqual(Type.EmptyTypes.Length, obj.Types.Length);
 }
Ejemplo n.º 18
0
 public void InstanceFieldRetrievingFactoryMethod()
 {
     DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("field-props-factory.xml", GetType()));
     MyTestObject obj = factory.GetObject("instanceCultureAware", typeof(MyTestObject)) as MyTestObject;
     Assert.IsNotNull(obj.Culture);
     Assert.AreEqual(new MyTestObject().Default, obj.Culture);
 }
Ejemplo n.º 19
0
        public void CanSpecifyFactoryMethodArgumentsOnFactoryMethodPrototype()
        {
            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("factory-methods.xml", GetType()));
            TestObject toArg = new TestObject();
            toArg.Name = "arg1";
            TestObject toArg2 = new TestObject();
            toArg2.Name = "arg2";
            FactoryMethods fm1 = (FactoryMethods) factory.GetObject("testObjectOnlyPrototype", new object[] {toArg});
            FactoryMethods fm2 = (FactoryMethods) factory.GetObject("testObjectOnlyPrototype", new object[] {toArg2});

            Assert.AreEqual(0, fm1.Number);
            Assert.AreEqual("default", fm1.Name);
            // This comes from the test object
            Assert.AreEqual("arg1", fm1.Object.Name);
            Assert.AreEqual("arg2", fm2.Object.Name);
            Assert.AreEqual(fm1.Number, fm2.Number);
            Assert.AreEqual(fm2.Value, "testObjectOnlyPrototypeDISetterString");
            Assert.AreEqual(fm2.Value, fm2.Value);
            // The TestObject reference is resolved to a prototype in the factory
            Assert.AreSame(fm2.Object, fm2.Object);
            Assert.IsFalse(ReferenceEquals(fm1, fm2));
        }
Ejemplo n.º 20
0
 public void StaticPropertyRetrievingFactoryMethod()
 {
     DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("field-props-factory.xml", GetType()));
     MyTestObject obj = factory.GetObject("cultureAware", typeof(MyTestObject)) as MyTestObject;
     Assert.IsNotNull(obj.Culture);
     Assert.AreEqual(CultureInfo.CurrentUICulture, obj.Culture);
 }
        public void LoadObjectDefinitionsWithNonExistentResource()
        {
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(new DefaultListableObjectFactory());

            Assert.Throws <ObjectDefinitionStoreException>(() => reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("/dev/null")));
        }
Ejemplo n.º 22
0
 public void FactoryMethodNoMatchingStaticMethod()
 {
     DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("factory-methods.xml", GetType()));
     factory.GetObject("noMatchPrototype");
 }
Ejemplo n.º 23
0
        public void InnerObjects()
        {
            DefaultListableObjectFactory xof = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(xof);
            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("reftypes.xml", GetType()));

            // Let's create the outer bean named "innerObject",
            // to check whether it doesn't create any conflicts
            // with the actual inner object named "innerObject".
            xof.GetObject("innerObject");

            TestObject hasInnerObjects = (TestObject) xof.GetObject("hasInnerObjects");
            Assert.AreEqual(5, hasInnerObjects.Age);           
            TestObject inner1 = (TestObject) hasInnerObjects.Spouse;
            Assert.IsNotNull(inner1);            
            Assert.AreEqual("Spring.Objects.TestObject#", inner1.ObjectName.Substring(0, inner1.ObjectName.IndexOf("#")+1));            
            Assert.AreEqual("inner1", inner1.Name);
            Assert.AreEqual(6, inner1.Age);
            
            
            Assert.IsNotNull(hasInnerObjects.Friends);
            IList friends = (IList) hasInnerObjects.Friends;
            Assert.AreEqual(2, friends.Count);
            DerivedTestObject inner2 = (DerivedTestObject) friends[0];
            Assert.AreEqual("inner2", inner2.Name);
            Assert.AreEqual(7, inner2.Age);
            Assert.AreEqual("Spring.Objects.DerivedTestObject#", inner2.ObjectName.Substring(0, inner2.ObjectName.IndexOf("#") + 1));      
            TestObject innerFactory = (TestObject) friends[1];
            Assert.AreEqual(DummyFactory.SINGLETON_NAME, innerFactory.Name);

            
            Assert.IsNotNull(hasInnerObjects.SomeMap);
            Assert.IsFalse((hasInnerObjects.SomeMap.Count == 0));
            TestObject inner3 = (TestObject) hasInnerObjects.SomeMap["someKey"];
            Assert.AreEqual("Jenny", inner3.Name);
            Assert.AreEqual(30, inner3.Age);
            xof.Dispose();
            Assert.IsTrue(inner2.WasDestroyed());
            Assert.IsTrue(innerFactory.Name == null);
        }
Ejemplo n.º 24
0
 public void RefToSeparatePrototypeInstances()
 {
     DefaultListableObjectFactory xof = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(xof);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("reftypes.xml", GetType()));
     Assert.IsTrue(xof.ObjectDefinitionCount == 9, "9 objects in reftypes, not " + xof.ObjectDefinitionCount);
     TestObject emma = (TestObject) xof.GetObject("emma");
     TestObject georgia = (TestObject) xof.GetObject("georgia");
     ITestObject emmasJenks = emma.Spouse;
     ITestObject georgiasJenks = georgia.Spouse;
     Assert.IsTrue(emmasJenks != georgiasJenks, "Emma and georgia think they have a different boyfriend.");
     Assert.IsTrue(emmasJenks.Name.Equals("Andrew"), "Emmas jenks has right name");
     Assert.IsTrue(emmasJenks != xof.GetObject("jenks"), "Emmas doesn't equal new ref.");
     Assert.IsTrue(emmasJenks.Name.Equals("Andrew"), "Georgias jenks has right name.");
     Assert.IsTrue(emmasJenks.Equals(georgiasJenks), "They are object equal.");
     Assert.IsTrue(emmasJenks.Equals(xof.GetObject("jenks")), "They object equal direct ref.");
 }
        public void LoadObjectDefinitionsWithNullResource()
        {
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(new DefaultListableObjectFactory());

            Assert.Throws <ObjectDefinitionStoreException>(() => reader.LoadObjectDefinitions((string)null));
        }
 /// <summary>
 /// Initialize the object definition reader used for loading the object
 /// definitions of this context.
 /// </summary>
 /// <remarks>
 /// <p>
 /// The default implementation of this method is a no-op; i.e. it does
 /// nothing. Can be overridden in subclasses to provide custom
 /// initialization of the supplied
 /// <paramref name="objectDefinitionReader"/>; for example, a derived
 /// class may want to turn off XML validation.
 /// </p>
 /// </remarks>
 /// <param name="objectDefinitionReader">
 /// The object definition reader used by this context.
 /// </param>
 protected virtual void InitObjectDefinitionReader(
     XmlObjectDefinitionReader objectDefinitionReader)
 { }
 protected override void  LoadObjectDefinitions(XmlObjectDefinitionReader objectDefinitionReader)
 {
      base.LoadObjectDefinitions(objectDefinitionReader);
     objectDefinitionReader.LoadObjectDefinitions(configurationResources);
 }
        public void SimpleReader()
        {
            GenericApplicationContext applicationContext = new GenericApplicationContext();


            IObjectDefinitionReader objectDefinitionReader = new XmlObjectDefinitionReader(applicationContext);

            objectDefinitionReader.LoadObjectDefinitions("assembly://foo");

            applicationContext.Refresh();
            

        }
        public void GenericApplicationContextWithXmlObjectDefinitions()
        {
            GenericApplicationContext ctx = new GenericApplicationContext();
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(ctx);
            reader.LoadObjectDefinitions("assembly://Spring.Core.Tests/Spring.Context.Support/contextB.xml");
            reader.LoadObjectDefinitions("assembly://Spring.Core.Tests/Spring.Context.Support/contextC.xml");
            reader.LoadObjectDefinitions("assembly://Spring.Core.Tests/Spring.Context.Support/contextA.xml");
            ctx.Refresh();

            Assert.IsTrue(ctx.ContainsObject("service"));
            Assert.IsTrue(ctx.ContainsObject("logicOne"));
            Assert.IsTrue(ctx.ContainsObject("logicTwo"));
            ctx.Dispose();

        }
Ejemplo n.º 30
0
 public void BailsOnRubbishPropertyRetrievingFactoryMethod()
 {
     DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("field-props-factory.xml", GetType()));
     factory.GetObject("rubbishProperty", typeof(MyTestObject));
 }
Ejemplo n.º 31
0
 public void CircularReferences()
 {
     DefaultListableObjectFactory xof = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(xof);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("reftypes.xml", GetType()));
     TestObject jenny = (TestObject) xof.GetObject("jenny");
     TestObject david = (TestObject) xof.GetObject("david");
     TestObject ego = (TestObject) xof.GetObject("ego");
     Assert.IsTrue(jenny.Spouse == david, "Correct circular reference");
     Assert.IsTrue(david.Spouse == jenny, "Correct circular reference");
     Assert.IsTrue(ego.Spouse == ego, "Correct circular reference");
 }
Ejemplo n.º 32
0
 public void DescriptionButNoProperties()
 {
     DefaultListableObjectFactory xof = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(xof);
     reader.LoadObjectDefinitions(
         new ReadOnlyXmlTestResource("collections.xml", GetType()));
     TestObject validEmpty
         = (TestObject) xof.GetObject("validEmptyWithDescription");
     Assert.AreEqual(0, validEmpty.Age);
 }
Ejemplo n.º 33
0
 public void Instantiation()
 {
     XmlObjectDefinitionReader reader
         = new XmlObjectDefinitionReader(
               new DefaultListableObjectFactory());
 }
Ejemplo n.º 34
0
 public void RefToSingleton()
 {
     DefaultListableObjectFactory xof = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(xof);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("reftypes.xml", GetType()));
     Assert.IsTrue(xof.ObjectDefinitionCount == 9, "9 objects in reftypes, not " + xof.ObjectDefinitionCount);
     TestObject jen = (TestObject) xof.GetObject("jenny");
     TestObject dave = (TestObject) xof.GetObject("david");
     TestObject jenks = (TestObject) xof.GetObject("jenks");
     ITestObject davesJen = dave.Spouse;
     ITestObject jenksJen = jenks.Spouse;
     Assert.IsTrue(davesJen == jenksJen, "1 jen instance");
     Assert.IsTrue(davesJen == jen, "1 jen instance");
 }
 public void Instantiation()
 {
     XmlObjectDefinitionReader reader
         = new XmlObjectDefinitionReader(
             new DefaultListableObjectFactory());
 }
Ejemplo n.º 36
0
        public void InnerObjectsInPrototype()
        {
            DefaultListableObjectFactory xof = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(xof);
            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("reftypes.xml", GetType()));
            TestObject hasInnerObjects = (TestObject) xof.GetObject("prototypeHasInnerObjects");
            Assert.AreEqual(5, hasInnerObjects.Age);

            Assert.IsNotNull(hasInnerObjects.Spouse);
            Assert.AreEqual("inner1", hasInnerObjects.Spouse.Name);
            Assert.AreEqual(6, hasInnerObjects.Spouse.Age);
            Assert.IsNotNull(hasInnerObjects.Friends);
            IList friends = (IList) hasInnerObjects.Friends;
            Assert.AreEqual(2, friends.Count);
            DerivedTestObject inner2 = (DerivedTestObject) friends[0];
            Assert.AreEqual("inner2", inner2.Name);
            Assert.AreEqual(7, inner2.Age);


            IList friendsOfInner = (IList) inner2.Friends;
            Assert.AreEqual(1, friendsOfInner.Count);
            DerivedTestObject innerFriendOfAFriend = (DerivedTestObject) friendsOfInner[0];
            Assert.AreEqual("innerFriendOfAFriend", innerFriendOfAFriend.Name);
            Assert.AreEqual(7, innerFriendOfAFriend.Age);
            TestObject innerFactory = (TestObject) friends[1];
            Assert.AreEqual(DummyFactory.SINGLETON_NAME, innerFactory.Name);
            Assert.IsNotNull(hasInnerObjects.SomeMap);
            Assert.IsFalse((hasInnerObjects.SomeMap.Count == 0));

            TestObject inner3 = (TestObject) hasInnerObjects.SomeMap["someKey"];
            Assert.AreEqual("inner3", inner3.Name);
            Assert.AreEqual(8, inner3.Age);
            xof.Dispose();
            
            Assert.IsFalse(inner2.WasDestroyed());
            Assert.IsFalse(innerFactory.Name == null);
            Assert.IsFalse(innerFriendOfAFriend.WasDestroyed());
            
        }
Ejemplo n.º 37
0
 public void InstanceFactoryMethodWithoutArgs()
 {
     DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("factory-methods.xml", GetType()));
     FactoryMethods fm = (FactoryMethods) factory["instanceFactoryMethodWithoutArgs"];
     Assert.AreEqual("instanceFactory", fm.Object.Name);
 }
Ejemplo n.º 38
0
 public void EnumProperty()
 {
     DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
     reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("enums.xml", GetType()));
     TestObject obj = factory.GetObject("rod", typeof(TestObject)) as TestObject;
     Assert.IsNotNull(obj.FileMode);
     Assert.AreEqual(FileMode.Create, obj.FileMode);
 }
Ejemplo n.º 39
0
        public void InstanceFactoryMethodWithOverloadedArgs()
        {
            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("factory-methods.xml", GetType()));
            DataTable table = MakeNamesTable();
            DataRow row = table.NewRow();
            //FactoryMethods fm = (FactoryMethods) factory.GetObject("instanceFactoryMethodOverloads", new object[] {row});
            // Assert.AreEqual("DataRowCtor", fm.Name);

            IDataRecord dataRecord = (IDataRecord) mocks.DynamicMock(typeof(IDataRecord));
            FactoryMethods fm = (FactoryMethods)factory.GetObject("instanceFactoryMethodOverloads", new object[] { dataRecord });
            Assert.AreEqual("DataRecordCtor", fm.Name);


        }
Ejemplo n.º 40
0
        private void ConfigureObjectFactory(IObjectDefinitionRegistry factory)
        {
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
            reader.LoadObjectDefinitions(new StringResource(@"<?xml version='1.0' encoding='UTF-8' ?>
                <objects xmlns='http://www.springframework.net'
                          xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
                          xsi:schemaLocation='http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd'>

                      <object id='Parent' type='Spring.Objects.Factory.SPRNET_1334+ParentClass, Spring.Core.Tests'>
                        <property name='Name' value='Foo!'/>
			                <property name='InnerObject'>
				                <object type='Spring.Objects.Factory.SPRNET_1334+DisposableClass, Spring.Core.Tests'/>
			                </property>
                      </object>
<!--
                      <object id='Parent' type='Spring.Objects.Factory.SPRNET_1334+ParentClass, Spring.Core.Tests'>
                        <property name='Name' value='Foo!'/>
			                <property name='InnerObject' ref='Inner'/>
                      </object>

                      <object id='Inner' type='Spring.Objects.Factory.SPRNET_1334+DisposableClass, Spring.Core.Tests'/>
-->			          
                      
                </objects>
            "));
        }
Ejemplo n.º 41
0
 public void DefaultXmlResolverIsUsedIfNullSuppliedOrSet()
 {
     DefaultListableObjectFactory xof = new DefaultListableObjectFactory();
     XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(xof, null);
     try
     {
         reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("invalid.xml", GetType()));  
         Assert.Fail("Should have thrown XmlObjectDefinitionStoreException");
     }
     catch (ObjectDefinitionStoreException e)
     {
         Assert.AreEqual(0, e.Message.IndexOf("Line 21 in XML document"));
     }
 }
Ejemplo n.º 42
0
        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);
                }
            }
        }
Ejemplo n.º 43
0
        public void FactoryMethodsSingletonOnTargetClass()
        {
            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("factory-methods.xml", GetType()));

            FactoryMethods fm = (FactoryMethods) factory["default"];
            Assert.AreEqual(0, fm.Number);
            Assert.AreEqual("default", fm.Name);
            Assert.AreEqual("defaultInstance", fm.Object.Name);
            Assert.AreEqual("setterString", fm.Value);

            fm = (FactoryMethods) factory["testObjectOnly"];
            Assert.AreEqual(0, fm.Number);
            Assert.AreEqual("default", fm.Name);
            // This comes from the test object
            Assert.AreEqual("Juergen", fm.Object.Name);

            fm = (FactoryMethods) factory["full"];
            Assert.AreEqual(27, fm.Number);
            Assert.AreEqual("gotcha", fm.Name);
            Assert.AreEqual("Juergen", fm.Object.Name);

            FactoryMethods fm2 = (FactoryMethods) factory["full"];
            Assert.AreSame(fm, fm2);
        }
Ejemplo n.º 44
0
        public void FactoryMethodsPrototypeOnTargetClass()
        {
            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("factory-methods.xml", GetType()));
            FactoryMethods fm = (FactoryMethods) factory["defaultPrototype"];
            FactoryMethods fm2 = (FactoryMethods) factory["defaultPrototype"];
            Assert.AreEqual(0, fm.Number);
            Assert.AreEqual("default", fm.Name);
            Assert.AreEqual("defaultInstance", fm.Object.Name);
            Assert.AreEqual("setterString", fm.Value);
            Assert.AreEqual(fm.Number, fm2.Number);
            Assert.AreEqual(fm.Value, fm2.Value);
            // The TestObject is created separately for each object
            Assert.IsFalse(ReferenceEquals(fm.Object, fm2.Object));
            Assert.IsFalse(ReferenceEquals(fm, fm2));

            fm = (FactoryMethods) factory["testObjectOnlyPrototype"];
            fm2 = (FactoryMethods) factory["testObjectOnlyPrototype"];
            Assert.AreEqual(0, fm.Number);
            Assert.AreEqual("default", fm.Name);
            // This comes from the test object
            Assert.AreEqual("Juergen", fm.Object.Name);
            Assert.AreEqual(fm.Number, fm2.Number);
            Assert.AreEqual(fm.Value, fm2.Value);
            // The TestObject reference is resolved to a prototype in the factory
            Assert.AreSame(fm.Object, fm2.Object);
            Assert.IsFalse(ReferenceEquals(fm, fm2));

            fm = (FactoryMethods) factory["fullPrototype"];
            fm2 = (FactoryMethods) factory["fullPrototype"];
            Assert.AreEqual(27, fm.Number);
            Assert.AreEqual("gotcha", fm.Name);
            Assert.AreEqual("Juergen", fm.Object.Name);
            Assert.AreEqual(fm.Number, fm2.Number);
            Assert.AreEqual(fm.Value, fm2.Value);
            // The TestObject reference is resolved to a prototype in the factory
            Assert.AreSame(fm.Object, fm2.Object);
            Assert.IsFalse(ReferenceEquals(fm, fm2));
        }
Ejemplo n.º 45
0
        public void FactoryMethodsOnExternalClass()
        {
            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
            XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory);
            reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("factory-methods.xml", GetType()));
            TestObject to = (TestObject) factory["externalFactoryMethodWithoutArgs"];
            Assert.AreEqual(2, to.Age);
            Assert.AreEqual("Tristan", to.Name);

            to = (TestObject) factory["externalFactoryMethodWithArgs"];
            Assert.AreEqual(33, to.Age);
            Assert.AreEqual("Rod", to.Name);
        }
        /// <summary>
        /// Load the object definitions with the given 
        /// <see cref="Spring.Objects.Factory.Xml.XmlObjectDefinitionReader"/>.
        /// </summary>
        /// <remarks>
        /// <p>
        /// The lifecycle of the object factory is handled by
        /// <see cref="Spring.Context.Support.AbstractXmlApplicationContext.RefreshObjectFactory"/>;
        /// therefore this method is just supposed to load and / or register
        /// object definitions.
        /// </p>
        /// </remarks>
        /// <param name="objectDefinitionReader">
        /// The reader containing object definitions.</param>
        /// <exception cref="ObjectsException">
        /// In case of object registration errors.
        /// </exception>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// In the case of errors encountered reading any of the resources
        /// yielded by either the <see cref="ConfigurationLocations"/> or
        /// the <see cref="ConfigurationResources"/> methods.
        /// </exception>
        protected virtual void LoadObjectDefinitions(XmlObjectDefinitionReader objectDefinitionReader)
        {
            string[] locations = ConfigurationLocations;
            if (locations != null)
            {
                objectDefinitionReader.LoadObjectDefinitions(locations);
            }

            IResource[] resources = ConfigurationResources;
            if (resources != null)
            {
                objectDefinitionReader.LoadObjectDefinitions(resources);
            }

        }
 public ValidationEventHandlerWrapper(XmlObjectDefinitionReader owner)
 {
     _owner = owner;
 }