public void Can_Create_Custom_Scan_Routine() { var scanner = new ScanOverridingAssemblyObjectDefinitionScanner(); var registry = new DefaultListableObjectFactory(); scanner.ScanAndRegisterTypes(registry); Assert.That(registry.ObjectDefinitionCount, Is.EqualTo(1), "found multiple definitions"); Assert.That(registry.GetObject<ComponentScan.ScanComponentsAndAddToContext.ConfigurationImpl>(), Is.Not.Null, "correct single defintion was not registered"); }
public void ShouldAllowConfigurationClassInheritance() { var factory = new DefaultListableObjectFactory(); factory.RegisterObjectDefinition("DerivedConfiguration", new GenericObjectDefinition { ObjectType = typeof(DerivedConfiguration) }); var processor = new ConfigurationClassPostProcessor(); processor.PostProcessObjectFactory(factory); // we should get singleton instances only TestObject testObject = (TestObject) factory.GetObject("DerivedDefinition"); string singletonParent = (string) factory.GetObject("BaseDefinition"); Assert.That(testObject.Value, Is.SameAs(singletonParent)); }
protected override void AddPersistenceExceptionTranslation(ProxyFactory pf, IPersistenceExceptionTranslator pet) { if (AttributeUtils.FindAttribute(pf.TargetType, typeof(RepositoryAttribute)) != null) { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); of.RegisterObjectDefinition("peti", new RootObjectDefinition(typeof(PersistenceExceptionTranslationInterceptor))); of.RegisterSingleton("pet", pet); pf.AddAdvice((PersistenceExceptionTranslationInterceptor) of.GetObject("peti")); } }
public void ProxyObjectWithoutInterface() { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); of.RegisterObjectDefinition("bar", new RootObjectDefinition(typeof(ObjectWithoutInterface))); TestAutoProxyCreator apc = new TestAutoProxyCreator(of); of.AddObjectPostProcessor(apc); ObjectWithoutInterface o = of.GetObject("bar") as ObjectWithoutInterface; Assert.IsTrue(AopUtils.IsAopProxy(o)); o.Foo(); Assert.AreEqual(1, apc.NopInterceptor.Count); }
public void ConvertsWriteConcernCorrectly() { DefaultListableObjectFactory factory = new DefaultListableObjectFactory(); factory.RegisterCustomConverter(typeof(WriteConcern), new WriteConcernTypeConverter()); RootObjectDefinition definition = new RootObjectDefinition(typeof(MongoFactoryObject)); definition.PropertyValues.Add("Url", "mongodb://localhost"); definition.PropertyValues.Add("WriteConcern", "Acknowledged"); factory.RegisterObjectDefinition("factory", definition); MongoFactoryObject obj = factory.GetObject<MongoFactoryObject>("&factory"); Assert.That(ReflectionUtils.GetInstanceFieldValue(obj, "_writeConcern"), Is.EqualTo(WriteConcern.Acknowledged)); }
public void Test() { int numIterations = 10000; start = DateTime.Now; DefaultListableObjectFactory factory = new DefaultListableObjectFactory(); InitFactory(factory); for (int i = 0; i < numIterations; i++) { FFoo foo = (FFoo)factory.GetObject("foo"); } stop = DateTime.Now; double timeElapsed = Elapsed; PrintTest("Creations", numIterations, timeElapsed); }
public void 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"); }
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); }
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 BailsOnRubbishPropertyRetrievingFactoryMethod() { DefaultListableObjectFactory factory = new DefaultListableObjectFactory(); XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory); reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("field-props-factory.xml", GetType())); factory.GetObject("rubbishProperty", typeof(MyTestObject)); }
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); }
public void AutowireWithSatisfiedConstructorDependency() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.Add(new PropertyValue("name", "Rod")); RootObjectDefinition rood = new RootObjectDefinition(typeof(TestObject), pvs); lof.RegisterObjectDefinition("rod", rood); Assert.AreEqual(1, lof.ObjectDefinitionCount); object registered = lof.Autowire(typeof(ConstructorDependency), AutoWiringMode.AutoDetect, false); Assert.AreEqual(1, lof.ObjectDefinitionCount); ConstructorDependency kerry = (ConstructorDependency)registered; TestObject rod = (TestObject)lof.GetObject("rod"); Assert.AreSame(rod, kerry._spouse); }
public void AutowireExistingObjectByType() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); RootObjectDefinition rod = new RootObjectDefinition(typeof(TestObject)); lof.RegisterObjectDefinition("test", rod); DependenciesObject existingObj = new DependenciesObject(); lof.AutowireObjectProperties(existingObj, AutoWiringMode.ByType, true); TestObject test = (TestObject)lof.GetObject("test"); Assert.AreEqual(existingObj.Spouse, test); }
public void AutowireExistingObjectByName() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); RootObjectDefinition rod = new RootObjectDefinition(typeof(TestObject)); lof.RegisterObjectDefinition("Spouse", rod); DependenciesObject existingObj = new DependenciesObject(); lof.AutowireObjectProperties(existingObj, AutoWiringMode.ByName, true); TestObject spouse = (TestObject)lof.GetObject("Spouse"); Assert.AreEqual(existingObj.Spouse, spouse); Assert.IsTrue(ObjectFactoryUtils.ObjectOfType(lof, typeof(TestObject)) == spouse); }
public void AutowireObjectByNameIsNotCaseInsensitive() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); RootObjectDefinition rodDefinition = new RootObjectDefinition(typeof(TestObject)); rodDefinition.PropertyValues.Add("name", "Rod"); rodDefinition.AutowireMode = AutoWiringMode.ByName; RootObjectDefinition kerryDefinition = new RootObjectDefinition(typeof(TestObject)); kerryDefinition.PropertyValues.Add("name", "Kerry"); lof.RegisterObjectDefinition("rod", rodDefinition); lof.RegisterObjectDefinition("spouse", kerryDefinition); // property name is Spouse (capital S) TestObject objRod = (TestObject)lof.GetObject("rod"); Assert.IsNull(objRod.Spouse, "Mmm, Spouse property appears to have been autowired by name, even though there is no object in the factory with a name 'Spouse'."); }
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)); }
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 ExtensiveCircularReference() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); for (int i = 0; i < 1000; i++) { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.Add(new PropertyValue("Spouse", new RuntimeObjectReference("object" + (i < 99 ? i + 1 : 0)))); RootObjectDefinition rod = new RootObjectDefinition(typeof(TestObject), pvs); lof.RegisterObjectDefinition("object" + i, rod); } lof.PreInstantiateSingletons(); for (int i = 0; i < 1000; i++) { TestObject obj = (TestObject)lof.GetObject("object" + i); TestObject otherObj = (TestObject)lof.GetObject("object" + (i < 99 ? i + 1 : 0)); Assert.IsTrue(obj.Spouse == otherObj); } }
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); }
public void GetObjectWithArgsOnFactoryObject() { using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory()) { // DummyFactory produces a TestObject RootObjectDefinition factoryObjectDef = new RootObjectDefinition(typeof(DummyFactory)); factoryObjectDef.IsSingleton = true; lof.RegisterObjectDefinition("factoryObject", factoryObjectDef); // verify preconditions TestObject to = lof.GetObject("factoryObject", null, null) as TestObject; Assert.IsNotNull(to); Assert.AreEqual(25, to.Age); Assert.AreEqual(DummyFactory.SINGLETON_NAME, to.Name); try { to = lof.GetObject("factoryObject", new object[] {}) as TestObject; Assert.Fail("should throw ObjectDefinitionStoreException"); } catch (ObjectDefinitionStoreException ex) { Assert.IsTrue( ex.Message.IndexOf("Cannot specify arguments in the GetObject () method when referring to a factory object definition") > -1); } try { to = lof.GetObject("factoryObject", new object[] { "Mark", "35" }) as TestObject; Assert.Fail("should throw ObjectDefinitionStoreException"); } catch (ObjectDefinitionStoreException ex) { Assert.IsTrue( ex.Message.IndexOf("Cannot specify arguments in the GetObject () method when referring to a factory object definition") > -1); } } }
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); }
public void GetObjectWithCtorArgsAndCtorAutowiring() { using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory()) { RootObjectDefinition prototype = new RootObjectDefinition(typeof(TestObject)); prototype.IsSingleton = false; lof.RegisterObjectDefinition("prototype", prototype); TestObject to = lof.GetObject("prototype", new object[] { "Bruno", 26, new NestedTestObject("Home") }) as TestObject; Assert.IsNotNull(to); Assert.AreEqual(26, to.Age); Assert.AreEqual("Bruno", to.Name); Assert.AreEqual("Home", to.Doctor.Company); } }
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 GetObjectWithCtorArgsOnPrototype() { using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory()) { RootObjectDefinition prototype = new RootObjectDefinition(typeof(TestObject)); prototype.IsSingleton = false; lof.RegisterObjectDefinition("prototype", prototype); TestObject to = lof.GetObject("prototype", new object[] { "Mark", 35 }) as TestObject; Assert.IsNotNull(to); Assert.AreEqual(35, to.Age); Assert.AreEqual("Mark", to.Name); } }
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()); }
public void GetObjectWithCtorArgsOnPrototypeOutOfOrderArgs() { using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory()) { RootObjectDefinition prototype = new RootObjectDefinition(typeof(TestObject)); prototype.IsSingleton = false; lof.RegisterObjectDefinition("prototype", prototype); try { TestObject to2 = lof.GetObject("prototype", new object[] { 35, "Mark" }) as TestObject; Assert.IsNotNull(to2); Assert.AreEqual(35, to2.Age); Assert.AreEqual("Mark", to2.Name); } catch (ObjectCreationException ex) { Assert.IsTrue(ex.Message.IndexOf("'Object of type 'System.Int32' cannot be converted to type 'System.String'") >= 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); }
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); }
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); } } }
public void FactoryMethodNoMatchingStaticMethod() { DefaultListableObjectFactory factory = new DefaultListableObjectFactory(); XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(factory); reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("factory-methods.xml", GetType())); factory.GetObject("noMatchPrototype"); }