public void DoesNotAcceptInfrastructureAdvisorsDuringScanning() { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); GenericObjectDefinition infrastructureAdvisorDefinition = new GenericObjectDefinition(); infrastructureAdvisorDefinition.ObjectType = typeof (TestAdvisor); infrastructureAdvisorDefinition.PropertyValues.Add("Name", "InfrastructureAdvisor"); infrastructureAdvisorDefinition.Role = ObjectRole.ROLE_INFRASTRUCTURE; of.RegisterObjectDefinition("infrastructure", infrastructureAdvisorDefinition); GenericObjectDefinition regularAdvisorDefinition = new GenericObjectDefinition(); regularAdvisorDefinition.ObjectType = typeof (TestAdvisor); regularAdvisorDefinition.PropertyValues.Add("Name", "RegularAdvisor"); // regularAdvisorDefinition.Role = ObjectRole.ROLE_APPLICATION; of.RegisterObjectDefinition("regular", regularAdvisorDefinition); TestAdvisorAutoProxyCreator apc = new TestAdvisorAutoProxyCreator(); apc.ObjectFactory = of; object[] advisors = apc.GetAdvicesAndAdvisorsForObject(typeof (object), "dummyTarget"); Assert.AreEqual(1, advisors.Length); Assert.AreEqual( "RegularAdvisor", ((TestAdvisor)advisors[0]).Name ); Assert.AreEqual(1, apc.CheckedAdvisors.Count); Assert.AreEqual("regular", apc.CheckedAdvisors[0]); }
public void SetUp() { _singletonDefinition = new RootObjectDefinition(typeof (TestObject), AutoWiringMode.No); _singletonDefinitionWithFactory = new RootObjectDefinition(_singletonDefinition); _singletonDefinitionWithFactory.FactoryMethodName = "GetObject"; _singletonDefinitionWithFactory.FactoryObjectName = "TestObjectFactoryDefinition"; _testObjectFactory = new RootObjectDefinition(typeof (TestObjectFactory), AutoWiringMode.No); DefaultListableObjectFactory myFactory = new DefaultListableObjectFactory(); myFactory.RegisterObjectDefinition("SingletonObjectDefinition", SingletonDefinition); myFactory.RegisterObjectDefinition("SingletonDefinitionWithFactory", SingletonDefinitionWithFactory); myFactory.RegisterObjectDefinition("TestObjectFactoryDefinition", TestObjectFactoryDefinition); _factory = myFactory; }
public void SetUp() { _singletonDefinition = new RootObjectDefinition(typeof(TestObject), AutoWiringMode.No); _singletonDefinitionWithFactory = new RootObjectDefinition(_singletonDefinition); _singletonDefinitionWithFactory.FactoryMethodName = "GetObject"; _singletonDefinitionWithFactory.FactoryObjectName = "TestObjectFactoryDefinition"; _testObjectFactory = new RootObjectDefinition(typeof(TestObjectFactory), AutoWiringMode.No); DefaultListableObjectFactory myFactory = new DefaultListableObjectFactory(); myFactory.RegisterObjectDefinition("SingletonObjectDefinition", SingletonDefinition); myFactory.RegisterObjectDefinition("SingletonDefinitionWithFactory", SingletonDefinitionWithFactory); myFactory.RegisterObjectDefinition("TestObjectFactoryDefinition", TestObjectFactoryDefinition); _factory = myFactory; }
public void ClearWithDynamicProxies() { CompositionProxyTypeBuilder typeBuilder = new CompositionProxyTypeBuilder(); typeBuilder.TargetType = typeof(TestObject); Type proxyType = typeBuilder.BuildProxyType(); DefaultListableObjectFactory of = new DefaultListableObjectFactory(); RootObjectDefinition od1 = new RootObjectDefinition(proxyType, false); od1.PropertyValues.Add("Name", "Bruno"); of.RegisterObjectDefinition("testObject", od1); GenericApplicationContext ctx1 = new GenericApplicationContext(of); ContextRegistry.RegisterContext(ctx1); ITestObject to1 = ContextRegistry.GetContext().GetObject("testObject") as ITestObject; Assert.IsNotNull(to1); Assert.AreEqual("Bruno", to1.Name); DefaultListableObjectFactory of2 = new DefaultListableObjectFactory(); RootObjectDefinition od2 = new RootObjectDefinition(proxyType, false); od2.PropertyValues.Add("Name", "Baia"); of2.RegisterObjectDefinition("testObject", od2); GenericApplicationContext ctx2 = new GenericApplicationContext(of2); ContextRegistry.Clear(); ITestObject to2 = ctx2.GetObject("testObject") as ITestObject; Assert.IsNotNull(to2); Assert.AreEqual("Baia", to2.Name); }
private static NamedObjectDefinition Find( string url, string objectName ) { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); RootObjectDefinition rod = new RootObjectDefinition( typeof( Type1 ) ); of.RegisterObjectDefinition( objectName, rod ); return FindWebObjectDefinition( url, of ); }
public void SunnyDayReplaceMethod() { RootObjectDefinition replacerDef = new RootObjectDefinition(typeof (NewsFeedFactory)); RootObjectDefinition managerDef = new RootObjectDefinition(typeof (ReturnsNullNewsFeedManager)); managerDef.MethodOverrides.Add(new ReplacedMethodOverride("CreateNewsFeed", "replacer")); DefaultListableObjectFactory factory = new DefaultListableObjectFactory(); factory.RegisterObjectDefinition("manager", managerDef); factory.RegisterObjectDefinition("replacer", replacerDef); INewsFeedManager manager = (INewsFeedManager) factory["manager"]; NewsFeed feed1 = manager.CreateNewsFeed(); Assert.IsNotNull(feed1, "The CreateNewsFeed() method is not being replaced."); Assert.AreEqual(NewsFeedFactory.DefaultName, feed1.Name); NewsFeed feed2 = manager.CreateNewsFeed(); // NewsFeedFactory always yields a new NewsFeed (see class definition below)... Assert.IsFalse(ReferenceEquals(feed1, feed2)); }
public void _TestSetUp() { DefaultListableObjectFactory parentFactory = new DefaultListableObjectFactory(); _factory = new DefaultListableObjectFactory(parentFactory); RootObjectDefinition rodA = new RootObjectDefinition(_expectedtype); RootObjectDefinition rodB = new RootObjectDefinition(_expectedtype); RootObjectDefinition rodC = new RootObjectDefinition(_expectedtype); RootObjectDefinition rod2A = new RootObjectDefinition(_expectedtype); RootObjectDefinition rod2C = new RootObjectDefinition(_expectedtype); _factory.RegisterObjectDefinition("objA", rodA); _factory.RegisterObjectDefinition("objB", rodB); _factory.RegisterObjectDefinition("objC", rodC); parentFactory.RegisterObjectDefinition("obj2A", rod2A); parentFactory.RegisterObjectDefinition("objB", rodB); parentFactory.RegisterObjectDefinition("obj2C", rod2C); }
public void CanCreateHostTwice() { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); string svcRegisteredName = System.Guid.NewGuid().ToString(); of.RegisterObjectDefinition(svcRegisteredName, new RootObjectDefinition(new RootObjectDefinition(typeof(Service)))); SpringServiceHost ssh = new SpringServiceHost(svcRegisteredName, of, true); SpringServiceHost ssh1 = new SpringServiceHost(svcRegisteredName, of, true); }
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 ExportSingleCall() { using (DefaultListableObjectFactory of = new DefaultListableObjectFactory()) { of.RegisterObjectDefinition("simpleCounter", new RootObjectDefinition(typeof(SimpleCounter), false)); SaoExporter saoExporter = new SaoExporter(); saoExporter.ObjectFactory = of; saoExporter.TargetName = "simpleCounter"; saoExporter.ServiceName = "RemotedSaoSingleCallCounter"; saoExporter.AfterPropertiesSet(); of.RegisterSingleton("simpleCounterExporter", saoExporter); // also tests SaoExporter.Dispose()! AssertExportedService(saoExporter.ServiceName, 0); } }
public void ShouldAllowConfigurationClassInheritance() { var factory = new DefaultListableObjectFactory(); factory.RegisterObjectDefinition("DerivedConfiguration", new GenericObjectDefinition { ObjectType = typeof(DerivedConfiguration) }); var processor = new ConfigurationClassPostProcessor(); processor.PostProcessObjectFactory(factory); // we should get singleton instances only TestObject testObject = (TestObject) factory.GetObject("DerivedDefinition"); string singletonParent = (string) factory.GetObject("BaseDefinition"); Assert.That(testObject.Value, Is.SameAs(singletonParent)); }
/// <summary> /// Create a special TargetSource for the given object, if any. /// </summary> /// <param name="objectType">the type of the object to create a TargetSource for</param> /// <param name="name">the name of the object</param> /// <param name="factory">the containing factory</param> /// <returns> /// a special TargetSource or null if this TargetSourceCreator isn't /// interested in the particular object /// </returns> public ITargetSource GetTargetSource(Type objectType, string name, IObjectFactory factory) { AbstractPrototypeTargetSource prototypeTargetSource = CreatePrototypeTargetSource(objectType, name, factory); if (prototypeTargetSource == null) { return null; } else { if (!(factory is IObjectDefinitionRegistry)) { if (logger.IsWarnEnabled) logger.Warn("Cannot do autopooling with a IObjectFactory that doesn't implement IObjectDefinitionRegistry"); return null; } IObjectDefinitionRegistry definitionRegistry = (IObjectDefinitionRegistry) factory; RootObjectDefinition definition = (RootObjectDefinition) definitionRegistry.GetObjectDefinition(name); if (logger.IsInfoEnabled) logger.Info("Configuring AbstractPrototypeBasedTargetSource..."); // Infinite cycle will result if we don't use a different factory, // because a GetObject() call with this objectName will go through the autoproxy // infrastructure again. // We to override just this object definition, as it may reference other objects // and we're happy to take the parent's definition for those. DefaultListableObjectFactory objectFactory = new DefaultListableObjectFactory(factory); // Override the prototype object objectFactory.RegisterObjectDefinition(name, definition); // Complete configuring the PrototypeTargetSource prototypeTargetSource.TargetObjectName = name; prototypeTargetSource.ObjectFactory = objectFactory; return prototypeTargetSource; } }
public void AutowireWithParent() { XmlObjectFactory xof = new XmlObjectFactory(new ReadOnlyXmlTestResource("autowire.xml", GetType())); DefaultListableObjectFactory lbf = new DefaultListableObjectFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.Add("Name", "kerry"); lbf.RegisterObjectDefinition("Spouse", new RootObjectDefinition(typeof(TestObject), pvs)); xof.ParentObjectFactory = lbf; DoTestAutowire(xof); }
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 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 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 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 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 ObjectNamesForTypeIncludingAncestorsExcludesObjectsFromParentWhenLocalObjectDefined() { DefaultListableObjectFactory root = new DefaultListableObjectFactory(); root.RegisterObjectDefinition("excludeLocalObject", new RootObjectDefinition(typeof(ArrayList))); DefaultListableObjectFactory child = new DefaultListableObjectFactory(root); child.RegisterObjectDefinition("excludeLocalObject", new RootObjectDefinition(typeof(Hashtable))); IList<string> names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(child, typeof(ArrayList)); // "excludeLocalObject" matches on the parent, but not the local object definition Assert.AreEqual(0, names.Count); names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(child, typeof(ArrayList), true, true); // "excludeLocalObject" matches on the parent, but not the local object definition Assert.AreEqual(0, names.Count); }
public void AutowireWithUnsatisfiedConstructorDependency() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.Add(new PropertyValue("name", "Rod")); RootObjectDefinition rod = new RootObjectDefinition(typeof(TestObject), pvs); lof.RegisterObjectDefinition("rod", rod); Assert.AreEqual(1, lof.ObjectDefinitionCount); lof.Autowire(typeof(UnsatisfiedConstructorDependency), AutoWiringMode.AutoDetect, true); Assert.Fail("Should have unsatisfied constructor dependency on SideEffectObject"); }
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 AutowireWithNoDependencies() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); RootObjectDefinition rod = new RootObjectDefinition(typeof(TestObject)); lof.RegisterObjectDefinition("rod", rod); Assert.AreEqual(1, lof.ObjectDefinitionCount); object registered = lof.Autowire(typeof(NoDependencies), AutoWiringMode.AutoDetect, false); Assert.AreEqual(1, lof.ObjectDefinitionCount); Assert.IsTrue(registered is NoDependencies); }
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 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 AutowireExistingObjectByNameWithDependencyCheck() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); RootObjectDefinition rod = new RootObjectDefinition(typeof(TestObject)); lof.RegisterObjectDefinition("Spous", rod); DependenciesObject existingObj = new DependenciesObject(); lof.AutowireObjectProperties(existingObj, AutoWiringMode.ByName, true); Assert.Fail("Should have thrown UnsatisfiedDependencyException"); }
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 PullingObjectWithFactoryMethodAlsoInjectsDependencies() { string expectedName = "Terese Raquin"; MutablePropertyValues props = new MutablePropertyValues(); props.Add(new PropertyValue("Name", expectedName)); RootObjectDefinition def = new RootObjectDefinition(typeof(MySingleton), props); def.FactoryMethodName = "GetInstance"; DefaultListableObjectFactory fac = new DefaultListableObjectFactory(); fac.RegisterObjectDefinition("foo", def); object foo = fac["foo"]; Assert.IsNotNull(foo, "Couldn't pull manually registered instance out of the factory using factory method instantiation."); MySingleton sing = (MySingleton)foo; Assert.AreEqual(expectedName, sing.Name, "Dependency was not resolved pulling manually registered instance out of the factory using factory method instantiation."); }
public void AutowireExistingObjectByNameWithNoDependencyCheck() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); RootObjectDefinition rod = new RootObjectDefinition(typeof(TestObject)); lof.RegisterObjectDefinition("Spous", rod); DependenciesObject existingObj = new DependenciesObject(); lof.AutowireObjectProperties(existingObj, AutoWiringMode.ByName, false); Assert.IsNull(existingObj.Spouse); }
public void AutowireObjectByNameWithDependencyCheck() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); RootObjectDefinition rod = new RootObjectDefinition(typeof(TestObject)); lof.RegisterObjectDefinition("Spous", rod); lof.Autowire(typeof(DependenciesObject), AutoWiringMode.ByName, true); }