public void MultiplePatterns()
		{
			IObjectFactory iof = new XmlObjectFactory(new ReadOnlyXmlTestResource("RegularExpressionSetterTests.xml", GetType()));
			IPerson advised = (IPerson) iof.GetObject("SettersAndReturnsThisAdvised");
			
		    // Interceptor behind regexp advisor
			NopInterceptor nop = (NopInterceptor) iof.GetObject("NopInterceptor");
			Assert.AreEqual(0, nop.Count);
		
			int newAge = 12;
			// Not advised
			advised.Exceptional(null);
			Assert.AreEqual(0, nop.Count);
			
			// This is proxied
			advised.ReturnsThis();
			Assert.AreEqual(1, nop.Count);
			
			// Only setter is advised
		    advised.SetAge(newAge);
			Assert.AreEqual(2, nop.Count);

		    Assert.AreEqual(newAge, advised.GetAge());
			Assert.AreEqual(2, nop.Count);
		}
Example #2
0
        public void RefSubelementsBuildCollectionWithPrototypes()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);

            TestObject jen     = (TestObject)xof.GetObject("pJenny");
            TestObject dave    = (TestObject)xof.GetObject("pDavid");
            TestObject rod     = (TestObject)xof.GetObject("pRod");
            IList      friends = (IList)rod.Friends;

            Assert.IsTrue(friends.Count == 2);
            Assert.IsTrue(friends[0].ToString().Equals(jen.ToString()), "First friend must be jen, not " + friends[0]);
            Assert.IsTrue(friends[0] != jen, "Jen not same instance");
            Assert.IsTrue(friends[1].ToString().Equals(dave.ToString()));
            Assert.IsTrue(friends[1] != dave, "Dave not same instance");

            TestObject rod2     = (TestObject)xof.GetObject("pRod");
            IList      friends2 = (IList)rod2.Friends;

            Assert.IsTrue(friends2.Count == 2);
            Assert.IsTrue(friends2[0].ToString().Equals(jen.ToString()), "First friend must be jen, not " + friends2[0]);
            Assert.IsTrue(friends2[0] != friends[0], "Jen not same instance");
            Assert.IsTrue(friends2[1].ToString().Equals(dave.ToString()));
            Assert.IsTrue(friends2[1] != friends[1], "Dave not same instance");
        }
Example #3
0
        public void CustomListCollection()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            TestObject       objectWithTypedFriends = (TestObject)xof.GetObject("objectWithTypedFriends");
            TestObjectList   tol = objectWithTypedFriends.TypedFriends;

            Assert.AreEqual(1, tol.Count);
            Assert.IsTrue(tol[0].Equals(xof.GetObject("david")));
        }
Example #4
0
        public void ObjectArray()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("objectArray");

            Assert.IsTrue(hasMap.ObjectArray.Length == 2);
            Assert.IsTrue(hasMap.ObjectArray[0].Equals("one"));
            Assert.IsTrue(hasMap.ObjectArray[1].Equals(xof.GetObject("jenny")));
        }
Example #5
0
        public void PopulatedSet()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("set");

            Assert.IsTrue(hasMap.Set.Count == 3);
            Assert.IsTrue(hasMap.Set.Contains("bar"));
            TestObject jenny = (TestObject)xof.GetObject("jenny");

            Assert.IsTrue(hasMap.Set.Contains(jenny));
            Assert.IsTrue(hasMap.Set.Contains(null));
        }
		public void SingletonProxyWithPrototypeTarget() 
		{
			TestObjectImpl.constructionCount = 0;
			IObjectFactory iof = new XmlObjectFactory(new ReadOnlyXmlTestResource("prototypeTarget.xml", GetType()));
			for (int i = 0; i < 10; i++) 
			{
				TestObject to = (TestObject) iof.GetObject("testObjectSingleton");
				to.DoSomething();
			}
			TestInterceptor interceptor = (TestInterceptor) iof.GetObject("testInterceptor");
			Assert.AreEqual(1, TestObjectImpl.constructionCount);
			Assert.AreEqual(10, interceptor.invocationCount);
		}
Example #7
0
        public void MapWithLiteralsAndReferences()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("mixedMap");

            Assert.IsTrue(hasMap.Map.Count == 3);
            Assert.IsTrue(hasMap.Map["foo"].Equals("bar"));
            TestObject jenny = (TestObject)xof.GetObject("jenny");

            Assert.IsTrue(hasMap.Map["jenny"] == jenny);
            Assert.IsTrue(hasMap.Map["david"].Equals("david"));
        }
Example #8
0
        public void AutoAliasing()
        {
            IResource        resource    = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof         = new XmlObjectFactory(resource);
            IList <string>   objectNames = xof.GetObjectDefinitionNames();
            TestObject       tb1         = (TestObject)xof.GetObject("aliased");
            TestObject       alias1      = (TestObject)xof.GetObject("myalias");

            Assert.IsTrue(tb1 == alias1);

            IList <string> tb1Aliases = xof.GetAliases("aliased");

            Assert.AreEqual(1, tb1Aliases.Count);
            Assert.IsTrue(tb1Aliases.Contains("myalias"));
            Assert.IsTrue(objectNames.Contains("aliased"));
            Assert.IsFalse(objectNames.Contains("myalias"));

            TestObject tb2    = (TestObject)xof.GetObject("multiAliased");
            TestObject alias2 = (TestObject)xof.GetObject("alias1");
            TestObject alias3 = (TestObject)xof.GetObject("alias2");

            Assert.IsTrue(tb2 == alias2);
            Assert.IsTrue(tb2 == alias3);
            IList <string> tb2Aliases = xof.GetAliases("multiAliased");

            Assert.AreEqual(2, tb2Aliases.Count);
            Assert.IsTrue(tb2Aliases.Contains("alias1"));
            Assert.IsTrue(tb2Aliases.Contains("alias2"));
            Assert.IsTrue(objectNames.Contains("multiAliased"));
            Assert.IsFalse(objectNames.Contains("alias1"));
            Assert.IsFalse(objectNames.Contains("alias2"));

            TestObject tb3    = (TestObject)xof.GetObject("aliasWithoutId1");
            TestObject alias4 = (TestObject)xof.GetObject("aliasWithoutId2");
            TestObject alias5 = (TestObject)xof.GetObject("aliasWithoutId3");

            Assert.IsTrue(tb3 == alias4);
            Assert.IsTrue(tb3 == alias5);

            IList <string> tb3Aliases = xof.GetAliases("aliasWithoutId1");

            Assert.AreEqual(2, tb2Aliases.Count);
            Assert.IsTrue(tb3Aliases.Contains("aliasWithoutId2"));
            Assert.IsTrue(tb3Aliases.Contains("aliasWithoutId3"));
            Assert.IsTrue(objectNames.Contains("aliasWithoutId1"));
            Assert.IsFalse(objectNames.Contains("aliasWithoutId2"));
            Assert.IsFalse(objectNames.Contains("aliasWithoutId3"));

            string className  = typeof(TestObject).FullName;
            string targetName = className + ObjectDefinitionReaderUtils.GENERATED_OBJECT_NAME_SEPARATOR + "0";

            TestObject tb4 = (TestObject)xof.GetObject(targetName);

            Assert.AreEqual(null, tb4.Name);
        }
Example #9
0
        public void SimpleCtor()
        {
            XmlObjectFactory xof = new XmlObjectFactory(new ReadOnlyXmlTestResource("simple-constructor-arg.xml", GetType()));
            ConstructorDependenciesObject obj = (ConstructorDependenciesObject)xof.GetObject("rod4");

            Assert.AreEqual("Kerry2", obj.Spouse1.Name);
        }
Example #10
0
        /// <summary>
        /// Test that we can add elements to an ISet that is exposed as a
        /// read-only property
        /// </summary>
        public void AddElementsToReadOnlySet()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);

            TestObject to        = (TestObject)xof.GetObject("readOnlySet");
            ISet       computers = to.Computers;

            Assert.IsTrue(computers.Count == 2);
            bool foundDell = false;
            bool foundIbm  = false;

            foreach (string name in computers)
            {
                if (name.Equals("Dell"))
                {
                    foundDell = true;
                }
                if (name.Equals("IBM T41"))
                {
                    foundIbm = true;
                }
            }
            Assert.IsTrue(foundDell, "Dell string not found in ISet");
            Assert.IsTrue(foundIbm, "IBM T41 not found in ISet");
        }
Example #11
0
        public void EmptyValue()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("emptyValue");

            Assert.AreEqual("", hasMap.Props.Get("foo"), "Expected empty string");
        }
Example #12
0
        public void EmptyProps()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("emptyProps");

            Assert.IsTrue(hasMap.Props.Count == 0);
        }
Example #13
0
        public void BuildCollectionFromMixtureOfReferencesAndValues()
        {
            MixedCollectionObject.ResetStaticState();
            IResource             resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory      xof      = new XmlObjectFactory(resource);
            MixedCollectionObject jumble   = (MixedCollectionObject)xof.GetObject("jumble");

            Assert.AreEqual(1, MixedCollectionObject.nrOfInstances);
            xof.GetObject("david", typeof(TestObject));
            Assert.IsTrue(jumble.Jumble.Count == 4, "Expected 3 elements, not " + jumble.Jumble.Count);
            IList l = (IList)jumble.Jumble;

            Assert.IsTrue(l[0].Equals(xof.GetObject("david")));
            Assert.IsTrue(l[1].Equals("literal"));
            Assert.IsTrue(l[2].Equals(xof.GetObject("jenny")));
            Assert.IsTrue(l[3].Equals("rod"));
        }
Example #14
0
        public void ObjectWithIndexerProperty()
        {
            IResource        resource   = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof        = new XmlObjectFactory(resource);
            TestObject       testObject = (TestObject)xof.GetObject("objectWithIndexer");

            Assert.IsNotNull(testObject);
            Assert.AreEqual("my string value", testObject[0]);
        }
Example #15
0
        public void MapWithLiteralsReferencesAndList()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("mixedMapWithList");

            Assert.IsTrue(hasMap.Map.Count == 4);
            Assert.IsTrue(hasMap.Map["foo"].Equals("bar"));
            TestObject jenny = (TestObject)xof.GetObject("jenny");

            Assert.IsTrue(hasMap.Map["jenny"].Equals(jenny));

            // Check list
            IList l = (IList)hasMap.Map["list"];

            Assert.IsNotNull(l);
            Assert.IsTrue(l.Count == 4);
            Assert.IsTrue(l[0].Equals("zero"));
            Assert.IsTrue(l[3] == null);

            // Check nested map in list
            IDictionary m = (IDictionary)l[1];

            Assert.IsNotNull(m);
            Assert.IsTrue(m.Count == 2);
            Assert.IsTrue(m["fo"].Equals("bar"));
            Assert.IsTrue(m["jen"].Equals(jenny), "Map element 'jenny' should be equal to jenny object, not " + m["jen"]);

            // Check nested list in list
            l = (IList)l[2];
            Assert.IsNotNull(l);
            Assert.IsTrue(l.Count == 2);
            Assert.IsTrue(l[0].Equals(jenny));
            Assert.IsTrue(l[1].Equals("ba"));

            // Check nested map
            m = (IDictionary)hasMap.Map["map"];
            Assert.IsNotNull(m);
            Assert.IsTrue(m.Count == 2);
            Assert.IsTrue(m["foo"].Equals("bar"));
            Assert.IsTrue(m["jenny"].Equals(jenny),
                          "Map element 'jenny' should be equal to jenny object, not " + m["jenny"]);
        }
        public void PrototypeAndSingletonBehaveDifferently()
        {
            int initialCount = 10;
            IObjectFactory of = new XmlObjectFactory(new ReadOnlyXmlTestResource("prototypeTargetSourceTests.xml", GetType()));
            ISideEffectObject singleton = (ISideEffectObject)of.GetObject("singleton");
            Assert.AreEqual(initialCount, singleton.Count);
            singleton.doWork();
            Assert.AreEqual(initialCount + 1, singleton.Count);

            ISideEffectObject prototype = (ISideEffectObject)of.GetObject("prototype");
            Assert.AreEqual(initialCount, prototype.Count);
            singleton.doWork();
            Assert.AreEqual(initialCount, prototype.Count);

            ISideEffectObject prototypeByName = (ISideEffectObject)of.GetObject("prototypeByName");
            Assert.AreEqual(initialCount, prototypeByName.Count);
            singleton.doWork();
            Assert.AreEqual(initialCount, prototypeByName.Count);
        }
Example #17
0
        public void RefSubelementsBuildCollection()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            //Assert.IsTrue ("5 objects in reftypes, not " + xof.GetObjectDefinitionCount(), xof.GetObjectDefinitionCount() == 5);
            TestObject jen  = (TestObject)xof.GetObject("jenny");
            TestObject dave = (TestObject)xof.GetObject("david");
            TestObject rod  = (TestObject)xof.GetObject("rod");

            // Must be a list to support ordering
            // Our object doesn't modify the collection:
            // of course it could be a different copy in a real object
            IList friends = (IList)rod.Friends;

            Assert.IsTrue(friends.Count == 2);
            Assert.IsTrue(friends[0] == jen, "First friend must be jen, not " + friends[0]);
            Assert.IsTrue(friends[1] == dave);
            // Should be ordered
        }
Example #18
0
        public void PrototypeDictionaryFactory()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            IDictionary      map      = (IDictionary)xof.GetObject("pMapFactory");

            Assert.IsTrue(map.Count == 2);
            Assert.AreEqual("bar", map["foo"]);
            Assert.AreEqual("jenny", map["jen"]);
        }
Example #19
0
        public void TypedGenericDictionary()
        {
            XmlObjectFactory        xof = new XmlObjectFactory(new ReadOnlyXmlTestResource("collections.xml", GetType()));
            GenericExpressionHolder obj = (GenericExpressionHolder)xof.GetObject("genericExpressionHolder");

            Assert.AreEqual(2, obj["0"].GetValue());
            Assert.AreEqual(8, obj["1"].GetValue());
            Assert.AreEqual("ALEKSANDAR SEOVIC", obj["2"].GetValue());
            Assert.IsTrue((bool)obj["3"].GetValue());
        }
Example #20
0
        public void PopulatedProps()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("props");

            Assert.AreEqual(2, hasMap.Props.Count);
            Assert.AreEqual("bar", hasMap.Props["foo"]);
            Assert.AreEqual("TWO", hasMap.Props["2"]);
        }
Example #21
0
        public void TypedNonGenericList()
        {
            XmlObjectFactory           xof = new XmlObjectFactory(new ReadOnlyXmlTestResource("collections.xml", GetType()));
            NonGenericExpressionHolder obj = (NonGenericExpressionHolder)xof.GetObject("nonGenericExpressionHolder");

            Assert.AreEqual(2, obj[0].GetValue());
            Assert.AreEqual(8, obj[1].GetValue());
            Assert.AreEqual("ALEKSANDAR SEOVIC", obj[2].GetValue());
            Assert.IsTrue((bool)obj[3].GetValue());
        }
Example #22
0
        public void ClassArray()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("classArray");

            Assert.IsTrue(hasMap.ClassArray.Length == 2);
            Assert.IsTrue(hasMap.ClassArray[0].Equals(typeof(String)));
            Assert.IsTrue(hasMap.ClassArray[1].Equals(typeof(Exception)));
        }
Example #23
0
        public void MapWithLiteralsAndPrototypeReferences()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);

            TestObject jenny  = (TestObject)xof.GetObject("pJenny");
            HasMap     hasMap = (HasMap)xof.GetObject("pMixedMap");

            Assert.IsTrue(hasMap.Map.Count == 2);
            Assert.IsTrue(hasMap.Map["foo"].Equals("bar"));
            Assert.IsTrue(hasMap.Map["jenny"].ToString().Equals(jenny.ToString()));
            Assert.IsTrue(hasMap.Map["jenny"] != jenny, "Not same instance");

            HasMap hasMap2 = (HasMap)xof.GetObject("pMixedMap");

            Assert.IsTrue(hasMap2.Map.Count == 2);
            Assert.IsTrue(hasMap2.Map["foo"].Equals("bar"));
            Assert.IsTrue(hasMap2.Map["jenny"].ToString().Equals(jenny.ToString()));
            Assert.IsTrue(hasMap2.Map["jenny"] != hasMap.Map["jenny"], "Not same instance");
        }
Example #24
0
        public void MapWithLiteralsOnly()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("literalMap");

            Assert.IsTrue(hasMap.Map.Count == 3);
            Assert.IsTrue(hasMap.Map["foo"].Equals("bar"));
            Assert.IsTrue(hasMap.Map["fi"].Equals("fum"));
            Assert.IsTrue(hasMap.Map["fa"] == null);
        }
Example #25
0
        public void RefSubelementsBuildCollectionFromSingleElement()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            TestObject       loner    = (TestObject)xof.GetObject("loner");
            TestObject       dave     = (TestObject)xof.GetObject("david");

            Assert.IsTrue(loner.Friends.Count == 1);
            bool contains = false;

            foreach (object friend in loner.Friends)
            {
                if (friend == dave)
                {
                    contains = true;
                    break;
                }
            }
            Assert.IsTrue(contains);
        }
Example #26
0
        public void DelimitedProps()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("delimitedProps");

            Assert.AreEqual(2, hasMap.Props.Count);
            Assert.AreEqual(3, hasMap.Props.GetValues("foo").Length);
            Assert.AreEqual(7, hasMap.Props.GetValues("days").Length);
            Assert.AreEqual("wednesday", hasMap.Props.GetValues("days")[2]);
        }
Example #27
0
        public void PrototypeSetFactory()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            Set mySet = (Set)xof.GetObject("pSetFactory");

            Assert.IsTrue(mySet is HybridSet);
            Assert.IsTrue(mySet.Count == 2);
            Assert.IsTrue(mySet.Contains("bar"));
            Assert.IsTrue(mySet.Contains("jenny"));
        }
Example #28
0
        public void PrototypeListFactory()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            IList            list     = (IList)xof.GetObject("pListFactory");

            Assert.IsTrue(list is LinkedList);
            Assert.IsTrue(list.Count == 2);
            Assert.AreEqual("bar", list[0]);
            Assert.AreEqual("jenny", list[1]);
        }
Example #29
0
        public void AddElementsToReadOnlyList()
        {
            IResource resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            //Stream stream = new FileSystemResource("file:///w:/collections.xml").InputStream;
            XmlObjectFactory xof = new XmlObjectFactory(resource);

            TestObject aleks = (TestObject)xof.GetObject("aleks");
            IList      pets  = (IList)aleks.Pets;

            Assert.IsTrue(pets.Count == 1);
            Assert.IsTrue(pets[0].Equals("Nadja"));
        }
        public void CanApplyConstructorArgsToAbstractType()
        {
            IResource resource = new ReadOnlyXmlTestResource("ctor-args.xml", GetType());
            XmlObjectFactory xof = new XmlObjectFactory(resource);
            TestObject rod = (TestObject)xof.GetObject("rod");
            Assert.AreEqual(1, rod.Age);

            RootObjectDefinition def = (RootObjectDefinition) xof.GetObjectDefinition("rod");
            ConstructorResolver resolver = new ConstructorResolver(xof, xof, new SimpleInstantiationStrategy(), 
                                                    new ObjectDefinitionValueResolver(xof));
            
            ConstructorInstantiationInfo ci = resolver.GetConstructorInstantiationInfo("rod", def, null, null);

            AbstractObjectDefinition objDef = (AbstractObjectDefinition)xof.GetObjectDefinition("foo");
            objDef.IsAbstract = false;

            TestObject foo = (TestObject) xof.GetObject("foo");


            Assert.AreEqual(2, foo.Age);
        }
Example #31
0
        public void PopulatedPropsWithSameKey()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            HasMap           hasMap   = (HasMap)xof.GetObject("propsWithSameKey");

            Assert.AreEqual(2, hasMap.Props.Count);
            Assert.AreEqual("OnE,tWo", hasMap.Props["foo"]);
            Assert.AreEqual(2, hasMap.Props.GetValues("bar").Length);
            Assert.AreEqual("OnE", hasMap.Props.GetValues("bar")[0]);
            Assert.AreEqual("tWo", hasMap.Props.GetValues("bar")[1]);
        }
Example #32
0
        /// <summary>
        /// Test that we can add elements to an IDictionary that is exposed as a
        /// read-only property.
        /// </summary>
        public void AddElementsToReadOnlyDictionary()
        {
            IResource        resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);

            TestObject  to    = (TestObject)xof.GetObject("readOnlyDictionary");
            IDictionary table = to.PeriodicTable;

            Assert.IsTrue(table.Count == 2);
            Assert.AreEqual("1", table["hydrogen"], "Dictionary Value incorrect");
            Assert.AreEqual("12", table["carbon"], "Dictionary value incorrect");
        }
        public void CreatesDbFactoryBean()
        {
            string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
                           <objects xmlns='http://www.springframework.net' xmlns:mongo='http://www.springframework.net/mongo'>
                             <mongo:mongo url='mongodb://localhost' />
                             <mongo:db-factory id='first' mongo-ref='Mongo' write-concern='WMajority' />
                           </objects>";
            var factory = new XmlObjectFactory(new StringResource(xml, Encoding.UTF8));

            var mongoDatabase = factory.GetObject("first");

            Assert.That(mongoDatabase, Is.Not.Null);
        }
        public void ReadsReplicasWriteConcernCorrectly()
        {
            string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
                           <objects xmlns='http://www.springframework.net' xmlns:mongo='http://www.springframework.net/mongo'>
                             <mongo:db-factory id='second' write-concern='W2' />
                           </objects>";
            var factory = new XmlObjectFactory(new StringResource(xml, Encoding.UTF8));

            var dbFactory = factory.GetObject<IMongoDatabaseFactory>("second");
            var db = dbFactory.GetDatabase();

            Assert.That(db.Settings.WriteConcern, Is.EqualTo(WriteConcern.W2));
        }
Example #35
0
        public void GetDictionaryThatUsesNonStringKeys()
        {
            XmlObjectFactory xof = new XmlObjectFactory(
                new ReadOnlyXmlTestResource("collections.xml", GetType()));
            IDictionary map = (IDictionary)xof.GetObject("mapWithNonStringKeys");

            Assert.AreEqual(2, map.Count);
            string v1 = (string)map[new TestObject("Rick Evans", 30)];

            Assert.AreEqual("Rick Evans", v1);
            string v2 = (string)map[new TestObject("Uncle Elvis", 47)];

            Assert.AreEqual("Elvis Orten", v2);
        }
Example #36
0
        public void CanApplyConstructorArgsToAbstractType()
        {
            IResource        resource = new ReadOnlyXmlTestResource("ctor-args.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            TestObject       rod      = (TestObject)xof.GetObject("rod");

            Assert.AreEqual(1, rod.Age);

            RootObjectDefinition def      = (RootObjectDefinition)xof.GetObjectDefinition("rod");
            ConstructorResolver  resolver = new ConstructorResolver(xof, xof, new SimpleInstantiationStrategy(),
                                                                    new ObjectDefinitionValueResolver(xof));

            ConstructorInstantiationInfo ci = resolver.GetConstructorInstantiationInfo("rod", def, null, null);

            AbstractObjectDefinition objDef = (AbstractObjectDefinition)xof.GetObjectDefinition("foo");

            objDef.IsAbstract = false;

            TestObject foo = (TestObject)xof.GetObject("foo");


            Assert.AreEqual(2, foo.Age);
        }
        public void TestDifferentCollectionTypes()
        {
            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
            <objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
          <v:group id='validatePerson' when='T(Spring.Objects.TestObject) == #this.GetType()'>
            <v:required id ='req' when='true' test='Name'/>
            <v:regex id ='reg' test='Name'>
              <v:property name='Expression' value='[a-z]*\s[a-z]*'/>
              <v:property name='Options' value='IgnoreCase'/>
              <v:message id='reg1' providers='regularni' when='true'>
                 <v:param value='#this.ToString()'/> 
              </v:message>
            </v:regex>
          </v:group>  
           <v:collection id='collectionValidator' validate-all='true'>
                <v:ref name='validatePerson'/>
           </v:collection>
           </objects>";

            MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
            IResource resource = new InputStreamResource(stream, "collectionValidator");

            XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
            CollectionValidator validator = (CollectionValidator) objectFactory.GetObject("collectionValidator");
            
            IList listPersons = new ArrayList();
            IDictionary dictPersons = new Hashtable();
            ISet setPersons = new ListSet();  

            listPersons.Add(new TestObject("DAMJAN Tomic", 24));
            listPersons.Add(new TestObject("Goran Milosavljevic", 24));
            listPersons.Add(new TestObject("Ivan CIKIC", 28));

            dictPersons.Add(1, listPersons[0]);
            dictPersons.Add(2, listPersons[1]);
            dictPersons.Add(3, listPersons[2]);

            setPersons.AddAll(listPersons);
            IValidationErrors ve = new ValidationErrors();

            Assert.IsTrue(validator.Validate(listPersons, ve));                        
            Assert.IsTrue(ve.IsEmpty);                                    
            Assert.IsTrue(validator.Validate(dictPersons, ve));
            Assert.IsTrue(ve.IsEmpty);
            Assert.IsTrue(validator.Validate(setPersons, ve));
            Assert.IsTrue(ve.IsEmpty);
        }
        public void ISBNValidatorTests()
        {
            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
            <objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
                <v:validator id='urlValidator' test='#this' type='Spring.Validation.Validators.UrlValidator, Spring.Core'/>
            </objects>";
            
            MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
            IResource resource = new InputStreamResource(stream, "urlValidator");

            XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
            object obj = objectFactory.GetObject("urlValidator");
            
            Assert.IsTrue(obj is IValidator);
            IValidator validator = obj as IValidator;
            Assert.IsTrue(validator.Validate("http://www.springframework.net", new ValidationErrors()));
        }
        public void DoesntCallContextRegistryForLocalObjectFactoryReferences()
        {
            string xml = string.Format(
                @"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>
	<object id='foo' type='{0}'>
        <property name='MyField' expression='@(theObject)' />
    </object>
</objects>"
                            , typeof(MyTestObject).AssemblyQualifiedName
                            );

            XmlObjectFactory of = new XmlObjectFactory(new StringResource(xml, Encoding.UTF8));
            object theObject = new object();
            of.RegisterSingleton("theObject", theObject);

            MyTestObject to = (MyTestObject) of.GetObject("foo");
            Assert.AreSame( theObject, to.MyField );
        }
        public void ISBNValidatorTests()
        {
            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
            <objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
                <v:validator id='isbnValidator' test='#this' type='Spring.Validation.Validators.ISBNValidator, Spring.Core'>
                    <v:message id='error.airportCode.dummy' providers='summary' when='false'/>
                </v:validator>
            </objects>";
            
            MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
            IResource resource = new InputStreamResource(stream, "isbnValidator");

            XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
            object obj = objectFactory.GetObject("isbnValidator");
            
            Assert.IsTrue(obj is IValidator);
            IValidator validator = obj as IValidator;
            Assert.IsTrue(validator.Validate("978-1-905158-79-9", new ValidationErrors()));
        }        
        public void WithNullCardType()
        {
            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
            <objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
                <v:validator id='ccValidator' test='#this' type='Spring.Validation.Validators.CreditCardValidator, Spring.Core'>
                  <v:message id='error.airportCode.dummy' providers='summary' when='false'/>
                </v:validator>
            </objects>";

            MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
            IResource resource = new InputStreamResource(stream, "ccValidator");

            XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
            object obj = objectFactory.GetObject("ccValidator");

            Assert.IsTrue(obj is CreditCardValidator);
            CreditCardValidator validator = obj as CreditCardValidator;
            Assert.IsNull(validator.CardType);
            Assert.IsTrue(validator.Validate("378282246310005", new ValidationErrors()));
        }
Example #42
0
        public void LocaleTest()
        {
            CultureInfo oldCulture = Thread.CurrentThread.CurrentCulture;
            try
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

                IResource resource = new ReadOnlyXmlTestResource("locale.xml", GetType());
                XmlObjectFactory xof = new XmlObjectFactory(resource);
                TestObject to = xof.GetObject("jenny") as TestObject;
                Assert.IsNotNull(to);
                DateTime d = new DateTime(2007, 10, 30);
                Assert.AreEqual(d, to.Date);
                Assert.AreEqual(30, to.Size.Height);
                Assert.AreEqual(30, to.Size.Width);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = oldCulture;
            }
        }
        public void AbstractParentObjects()
        {
            XmlObjectFactory parent = new XmlObjectFactory(new ReadOnlyXmlTestResource("parent.xml", GetType()));
            parent.PreInstantiateSingletons();
            Assert.IsTrue(parent.IsSingleton("inheritedTestObjectWithoutClass"));

            // abstract objects should not match
            //TODO add overloaded GetObjectOfType with 1 arg
            IDictionary tbs = parent.GetObjectsOfType(typeof(TestObject), true, true);
            Assert.AreEqual(2, tbs.Count);
            Assert.IsTrue(tbs.Contains("inheritedTestObjectPrototype"));
            Assert.IsTrue(tbs.Contains("inheritedTestObjectSingleton"));

            // non-abstract object should work, even if it serves as parent
            object o1 = parent.GetObject("inheritedTestObjectPrototype");
            Assert.IsTrue(o1 is TestObject);

            // abstract object should return IObjectInstance itself
            object o2 = parent.GetObject("inheritedTestObjectWithoutClass");
            Assert.IsTrue(o2 is IObjectDefinition);
        }
 public void DefaultDestroyMethodDisabled()
 {
     IResource resource = new ReadOnlyXmlTestResource("default-destroy-methods.xml", GetType());
     XmlObjectFactory xof = new XmlObjectFactory(resource);
     xof.PreInstantiateSingletons();
     DefaultDestroyer dd = (DefaultDestroyer)xof.GetObject("destroy-method2");
     Assert.IsTrue(!dd.customDestroyed);
     xof.Dispose();
     Assert.IsTrue(!dd.customDestroyed);
 }
 public void FactoryReferenceCircle()
 {
     IResource resource = new ReadOnlyXmlTestResource("factoryCircle.xml", GetType());
     XmlObjectFactory xof = new XmlObjectFactory(resource);
     TestObject tb = (TestObject) xof.GetObject("singletonFactory");
     DummyFactory db = (DummyFactory) xof.GetObject("&singletonFactory");
     Assert.IsTrue(tb == db.OtherTestObject);
 }
 public void NoSuchInitMethod()
 {
     IResource resource = new ReadOnlyXmlTestResource("initializers.xml", GetType());
     XmlObjectFactory xof = new XmlObjectFactory(resource);
     xof.GetObject("init-method3");
     Assert.Fail();
 }
 public void InitializingObjectAndInitMethod()
 {
     InitAndIB.constructed = false;
     IResource resource = new ReadOnlyXmlTestResource("initializers.xml", GetType());
     XmlObjectFactory xof = new XmlObjectFactory(resource);
     Assert.IsFalse(InitAndIB.constructed);
     xof.PreInstantiateSingletons();
     Assert.IsFalse(InitAndIB.constructed);
     InitAndIB iib = (InitAndIB) xof.GetObject("init-and-ib");
     Assert.IsTrue(InitAndIB.constructed);
     Assert.IsTrue(iib.afterPropertiesSetInvoked && iib.initMethodInvoked);
     Assert.IsTrue(!iib.destroyed && !iib.customDestroyed);
     xof.Dispose();
     Assert.IsTrue(iib.destroyed && iib.customDestroyed);
     xof.Dispose();
     Assert.IsTrue(iib.destroyed && iib.customDestroyed);
 }
 public void DefaultInitMethodDisabled()
 {
     IResource resource = new ReadOnlyXmlTestResource("default-initializers.xml", GetType());
     XmlObjectFactory xof = new XmlObjectFactory(resource);
     DoubleInitializer in_Renamed = (DoubleInitializer)xof.GetObject("init-method2");
     // Initializer should have doubled value
     Assert.AreEqual(7, in_Renamed.Num);
 }
 public void InitMethodThrowsException()
 {
     IResource resource = new ReadOnlyXmlTestResource("initializers.xml", GetType());
     XmlObjectFactory xof = new XmlObjectFactory(resource);
     try
     {
         xof.GetObject("init-method2");
         Assert.Fail();
     }
     catch (ObjectCreationException ex)
     {
         Assert.IsTrue(ex.InnerException is FormatException);
     }
 }
        public void BadParentReference()
        {
            IResource resource = new ReadOnlyXmlTestResource("wellformed-but-bad.xml", GetType());
            XmlObjectFactory xof = new XmlObjectFactory(resource);

            xof.GetObject("no.parent.factory");
        }
 public void PropertyWithIdRefObjectAttrSubelement()
 {
     IResource resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
     XmlObjectFactory xof = new XmlObjectFactory(resource);
     TestObject verbose = (TestObject) xof.GetObject("verbose3");
     Assert.IsTrue(verbose.Name.Equals("verbose"));
 }
 public void SingletonInheritsFromParentFactoryPrototype()
 {
     XmlObjectFactory parent = new XmlObjectFactory(new ReadOnlyXmlTestResource("parent.xml", GetType()));
     XmlObjectFactory child = new XmlObjectFactory(new ReadOnlyXmlTestResource("child.xml", GetType()), parent);
     TestObject inherits = (TestObject) child.GetObject("singletonInheritsFromParentFactoryPrototype");
     // Name property value is overriden
     Assert.IsTrue(inherits.Name.Equals("prototype-override"));
     // Age property is inherited from object in parent factory
     Assert.IsTrue(inherits.Age == 2);
     TestObject inherits2 = (TestObject) child.GetObject("singletonInheritsFromParentFactoryPrototype");
     Assert.IsTrue(inherits2 == inherits);
 }
        //SPRNET-1262
        public void ProxiesDeclarativeAttributeWithConstructorArguments()
        {
            XmlObjectFactory objectFactory = null;

            const string xml =
                @"<?xml version='1.0' encoding='UTF-8' ?>
                <objects xmlns='http://www.springframework.net'>
                <object id='theService' type='Spring.ServiceModel.ServiceExporterTests+Service, Spring.Services.Tests'/>	
                <object id='service' type='Spring.ServiceModel.ServiceExporter, Spring.Services'>
	                    <property name='TargetName' value='theService'/>
	                    <property name='TypeAttributes'>
                            <list>
                                <object type='System.ServiceModel.ServiceKnownTypeAttribute, System.ServiceModel' abstract='true'>
                                    <constructor-arg name='type' value='System.Collections.Generic.List&lt;int>' />
                                </object>
                            </list>
                        </property>
                    </object>
                </objects>";

            using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                objectFactory = new XmlObjectFactory(new InputStreamResource(stream, string.Empty));
            }

            Type proxyType = (Type)objectFactory.GetObject("service");
            object[] attrs = proxyType.GetCustomAttributes(false);

            ServiceKnownTypeAttribute attrib = null;

            //loop thru all retreived attributes, attempting to find the one that is of the expected type
            for (int i = 0; i < attrs.Length; i++)
            {
                attrib = attrs[i] as ServiceKnownTypeAttribute;

                //break out of the loop once we find the one we want
                if (attrib != null)
                    break;
            }

            Assert.NotNull(attrib, String.Format("No attributes of the expecte type {0} were found.", typeof(ServiceKnownTypeAttribute)));
            Assert.AreEqual(typeof(List<int>), attrib.Type, "Property 'Type' on Target Attribute not set!");
        }
 public void BogusParentageFromParentFactory()
 {
     XmlObjectFactory parent = new XmlObjectFactory(new ReadOnlyXmlTestResource("parent.xml", GetType()));
     XmlObjectFactory child = new XmlObjectFactory(new ReadOnlyXmlTestResource("child.xml", GetType()), parent);
     child.GetObject("bogusParent");
 }
 public void ChildOverridesParentObject()
 {
     XmlObjectFactory parent = new XmlObjectFactory(new ReadOnlyXmlTestResource("parent.xml", GetType()));
     XmlObjectFactory child = new XmlObjectFactory(new ReadOnlyXmlTestResource("child.xml", GetType()), parent);
     TestObject inherits = (TestObject) child.GetObject("inheritedTestObject");
     // Name property value is overridden
     Assert.IsTrue(inherits.Name.Equals("overrideParentObject"));
     // Age property is inherited from object in parent factory
     Assert.IsTrue(inherits.Age == 1);
     TestObject inherits2 = (TestObject) child.GetObject("inheritedTestObject");
     Assert.IsTrue(inherits2 == inherits);
 }
 public void DependenciesMaterializeThis()
 {
     IResource resource = new ReadOnlyXmlTestResource("dependenciesMaterializeThis.xml", GetType());
     XmlObjectFactory bf = new XmlObjectFactory(resource);
     DummyBo bos = (DummyBo) bf.GetObject("boSingleton");
     DummyBo bop = (DummyBo) bf.GetObject("boPrototype");
     Assert.IsFalse(bos == bop);
     Assert.AreEqual(bos.dao, bop.dao);
 }
 public void PrototypeInheritanceFromParentFactorySingleton()
 {
     XmlObjectFactory parent = new XmlObjectFactory(new ReadOnlyXmlTestResource("parent.xml", GetType()));
     XmlObjectFactory child = new XmlObjectFactory(new ReadOnlyXmlTestResource("child.xml", GetType()), parent);
     TestObject inherits = (TestObject) child.GetObject("protoypeInheritsFromParentFactorySingleton");
     // Name property value is overridden
     Assert.IsTrue(inherits.Name.Equals("prototypeOverridesInheritedSingleton"));
     // Age property is inherited from object in parent factory
     Assert.IsTrue(inherits.Age == 1);
     TestObject inherits2 = (TestObject) child.GetObject("protoypeInheritsFromParentFactorySingleton");
     Assert.AreNotSame(inherits2,inherits);
     inherits2.Age = 13;
     Assert.IsTrue(inherits2.Age == 13);
     // Shouldn't have changed first instance
     Assert.IsTrue(inherits.Age == 1);
 }
        private void _TypedStringValueIsPickedUp(out Stream stream)
        {
            const string xml =
                @"<?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='golyadkin' type='Spring.Objects.TestObject, Spring.Core.Tests'>
		<property name='Hats'><value type='string[]'>trilby,fedora</value></property>
	</object>
</objects>";
            stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
            XmlObjectFactory fac = new XmlObjectFactory(new InputStreamResource(stream, string.Empty));
            IObjectDefinition def = fac.GetObjectDefinition("golyadkin");
            PropertyValue value = def.PropertyValues.GetPropertyValue("Hats");
            Assert.AreEqual(typeof(TypedStringValue), value.Value.GetType());
            TestObject obj = (TestObject) fac.GetObject("golyadkin");
            Assert.IsTrue(ArrayUtils.AreEqual(new string[] {"trilby", "fedora"}, obj.Hats));
        }
 public void SingletonInheritanceFromParentFactorySingletonUsingCtor()
 {
     XmlObjectFactory parent = new XmlObjectFactory(new ReadOnlyXmlTestResource("parent.xml", GetType()));
     XmlObjectFactory child = new XmlObjectFactory(new ReadOnlyXmlTestResource("child.xml", GetType()), parent);                                                                           
     TestObject inherits = (TestObject)child.GetObject("inheritsFromParentFactoryUsingCtor");
     // Name property value is overriden
     Assert.IsTrue(inherits.Name.Equals("child-name"));
     // Age property is inherited from object in parent factory
     Assert.IsTrue(inherits.Age == 1);
     TestObject inherits2 = (TestObject)child.GetObject("inheritsFromParentFactoryUsingCtor");
     Assert.AreSame(inherits2,inherits);
 }
 public void RefSubelement()
 {
     IResource resource = new ReadOnlyXmlTestResource("collections.xml", GetType());
     XmlObjectFactory xof = new XmlObjectFactory(resource);
     //Assert.IsTrue ("5 objects in reftypes, not " + xof.GetObjectDefinitionCount(), xof.GetObjectDefinitionCount() == 5);
     TestObject jen = (TestObject) xof.GetObject("jenny");
     TestObject dave = (TestObject) xof.GetObject("david");
     Assert.IsTrue(jen.Spouse == dave);
 }