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); }
public void SetUp() { IObjectFactory grandparent = new XmlObjectFactory(new ReadOnlyXmlTestResource("root.xml", GetType())); IObjectFactory parent = new XmlObjectFactory(new ReadOnlyXmlTestResource("middle.xml", GetType()), grandparent); IConfigurableListableObjectFactory child = new XmlObjectFactory(new ReadOnlyXmlTestResource("leaf.xml", GetType()), parent); _factory = child; }
public void Setup() { NamespaceParserRegistry.RegisterParser(typeof(RabbitNamespaceHandler)); var resourceName = @"assembly://Spring.Messaging.Amqp.Rabbit.Tests/Spring.Messaging.Amqp.Rabbit.Tests.Config/" + typeof(ConnectionFactoryParserTests).Name + "-context.xml"; var resource = new AssemblyResource(resourceName); this.objectFactory = new XmlObjectFactory(resource); }
private void ConfigureIoC() { IResource input = new FileSystemResource(Server.MapPath("objects.xml")); IObjectFactory factory = new XmlObjectFactory(input); DependencyResolver.InitializeWith(new SpringDependencyResolver(factory)); ControllerBuilder.Current.SetControllerFactory(typeof(IoCControllerFactory)); }
public void Setup() { NamespaceParserRegistry.RegisterParser(typeof(RabbitNamespaceHandler)); var resourceName = @"assembly://Spring.Messaging.Amqp.Rabbit.Tests/Spring.Messaging.Amqp.Rabbit.Tests.Config/" + typeof(ListenerContainerParserTests).Name + "-context.xml"; var resource = new AssemblyResource(resourceName); this.objectFactory = new XmlObjectFactory(resource); // ((IConfigurableObjectFactory)objectFactory).setObjectExpressionResolver(new StandardObjectExpressionResolver()); }
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)); }
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); }
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 RejectsConnectStringPlusDetailedConfiguration() { 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 url='mongodb://localhost/database' username='******' password='******'/> </objects>"; Assert.That( delegate { var factory = new XmlObjectFactory(new StringResource(xml, Encoding.UTF8)); }, Throws.TypeOf<ObjectDefinitionStoreException>()); }
public void SetUp() { const string xml = @"<?xml version='1.0' encoding='UTF-8' ?> <objects xmlns='http://www.springframework.net'> <object id='noDecoratedService' type='Spring.Web.Services.WebServiceExporterTests+NoDecoratedService, Spring.Web.Tests'/> <object id='decoratedService' type='Spring.Web.Services.WebServiceExporterTests+DecoratedService, Spring.Web.Tests'/> </objects>"; Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)); IObjectFactory objectFactory = new XmlObjectFactory(new InputStreamResource(stream, string.Empty)); wse = new WebServiceExporter(); wse.ObjectFactory = objectFactory; }
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 SetUp() { const string xml = @"<?xml version='1.0' encoding='UTF-8' ?> <objects xmlns='http://www.springframework.net'> <object id='service' type='Spring.ServiceModel.ServiceExporterTests+Service, Spring.Services.Tests'/> <object id='serviceWithMultipleInterfaces' type='Spring.ServiceModel.ServiceExporterTests+ServiceWithMultipleInterfaces, Spring.Services.Tests'/> <object id='decoratedService' type='Spring.ServiceModel.ServiceExporterTests+DecoratedService, Spring.Services.Tests'/> <object id='anotherService' type='Spring.ServiceModel.ServiceExporterTests+AnotherService, Spring.Services.Tests'/> </objects>"; using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { IObjectFactory objectFactory = new XmlObjectFactory(new InputStreamResource(stream, string.Empty)); se = new ServiceExporter(); se.ObjectFactory = objectFactory; } }
public override void Setup() { string objectXml = "<?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=\"SimpleDependency\" singleton=\"false\" type=\"MvcContrib.UnitTests.IoC.SimpleDependency\"/> " + " <object id=\"NestedDependency\" singleton=\"false\" type=\"MvcContrib.UnitTests.IoC.NestedDependency\" > " + " <constructor-arg> " + " <object type=\"MvcContrib.UnitTests.IoC.SimpleDependency\" /> " + " </constructor-arg> " + " </object> " + " </objects>"; Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(objectXml)); IResource resource = new InputStreamResource(stream, "In memory xml"); IObjectFactory factory = new XmlObjectFactory(resource); _dependencyResolver = new SpringDependencyResolver(factory); }
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 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 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 }
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 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); }
public void ReadsMongoAttributesCorrectly() { 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' write-concern='WMajority' /> </objects>"; var factory = new XmlObjectFactory(new StringResource(xml, Encoding.UTF8)); Assert.That(factory.GetObjectDefinitionNames(), Contains.Item("Mongo")); IObjectDefinition definition = factory.GetObjectDefinition("Mongo"); Assert.That(definition, Is.Not.Null); IList<PropertyValue> values = definition.PropertyValues.PropertyValues; Assert.That(values, Contains.Item(new PropertyValue("WriteConcern", "WMajority"))); Assert.That(values, Contains.Item(new PropertyValue("Url", "mongodb://localhost"))); }
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"); }
public void Setup() { string objectXml = "<?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=\"SimpleController\" singleton=\"false\" type=\"" + typeof(SpringSimpleController).FullName + "\"/> " + " <object id=\"DisposableController\" singleton=\"false\" type=\"" + typeof(SpringDisposableController).FullName + "\"/> " + " <object id=\"TestAreaSimpleController\" singleton=\"false\" type=\"" + typeof(TestArea.SpringSimpleController).FullName + "\"/>" + " </objects>"; var stream = new MemoryStream(Encoding.Default.GetBytes(objectXml)); var factory = new XmlObjectFactory(new InputStreamResource(stream, "In memory xml")); SpringControllerFactory.Configure(factory); _factory = new SpringControllerFactory(); _context = new RequestContext(MvcMockHelpers.DynamicHttpContextBase(), new RouteData()); }
public void Setup() { //still investigating configuring objects without using xml for unit test string objectXml = "<?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=\"SimpleController\" singleton=\"true\" type=\"MvcContrib.UnitTests.ControllerFactories.SpringControllerFactoryTester+WhenAValidControllerTypeIsPassed+SpringSimpleController\"/> " + " <object id=\"DependencyController\" singleton=\"true\" type=\"MvcContrib.UnitTests.ControllerFactories.SpringControllerFactoryTester+WhenAValidControllerTypeIsPassed+SpringDependencyController\" > " + " <constructor-arg> " + " <object type=\"MvcContrib.UnitTests.ControllerFactories.SpringControllerFactoryTester+WhenAValidControllerTypeIsPassed+StubDependency\" /> " + " </constructor-arg> " + " </object> " + " </objects>"; Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(objectXml)); IResource resource = new InputStreamResource(stream, "In memory xml"); IObjectFactory factory = new XmlObjectFactory(resource); SpringControllerFactory.Configure(factory); }
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); }
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())); }
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 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 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 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 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 GetDictionaryWithValueRefAttributeShortcuts() { XmlObjectFactory xof = new XmlObjectFactory( new ReadOnlyXmlTestResource("collections.xml", GetType())); IDictionary map = (IDictionary)xof.GetObject("mapWithValueRefAttributeShortcuts"); Assert.AreEqual(2, map.Count); foreach (string key in map.Keys) { object obj = map[key]; Assert.IsNotNull(obj); Assert.AreEqual(typeof(TestObject), obj.GetType(), "Wrong value assigned to value of dictionary."); TestObject tob = (TestObject)obj; if (key == "rick") { Assert.AreEqual("Rick Evans", tob.Name, "Wrong object value assigned to the 'rick' key."); } else if (key == "uncleelvis") { Assert.AreEqual("Uncle Elvis", tob.Name, "Wrong object value assigned to the 'uncleelvis' key."); } } }
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); }
//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<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 MultiThreadedLazyInit() { IResource resource = new ReadOnlyXmlTestResource("lazy-init-multithreaded.xml", GetType()); XmlObjectFactory xof = new XmlObjectFactory(resource); LazyWorker lw1 = new LazyWorker(xof); LazyWorker lw2 = new LazyWorker(xof); Thread thread1 = new Thread(new ThreadStart(lw1.DoWork)); Thread thread2 = new Thread(new ThreadStart(lw2.DoWork)); thread1.Start(); Thread.Sleep(1000); thread2.Start(); thread1.Join(); thread2.Join(); Assert.AreEqual(typeof(LazyTestObject), lw1.ObjectFromContext.GetType()); Assert.AreEqual(typeof(LazyTestObject), lw2.ObjectFromContext.GetType()); Assert.AreEqual(1, LazyTestObject.Count); }
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 NoSuchInitMethod() { IResource resource = new ReadOnlyXmlTestResource("initializers.xml", GetType()); XmlObjectFactory xof = new XmlObjectFactory(resource); xof.GetObject("init-method3"); Assert.Fail(); }
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); } }