Пример #1
0
        public void ExportSingleton()
        {
            using (DefaultListableObjectFactory of = new DefaultListableObjectFactory())
            {
                of.RegisterSingleton("simpleCounter", new SimpleCounter());
                SaoExporter saoExporter = new SaoExporter();
                saoExporter.ObjectFactory = of;
                saoExporter.TargetName = "simpleCounter";
                saoExporter.ServiceName = "RemotedSaoSingletonCounter";
                saoExporter.AfterPropertiesSet();
                of.RegisterSingleton("simpleCounterExporter", saoExporter); // also tests SaoExporter.Dispose()!

                AssertExportedService(saoExporter.ServiceName, 2);
            }
        }
        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"));

            }
        }
Пример #3
0
        public void CanExportFromFactoryObjectIfObjectTypeIsInterface()
        {
            using (DefaultListableObjectFactory of = new DefaultListableObjectFactory())
            {
                MockRepository mocks = new MockRepository();
                IFactoryObject simpleCounterFactory = (IFactoryObject) mocks.DynamicMock(typeof (IFactoryObject));
                Expect.Call(simpleCounterFactory.ObjectType).Return(typeof (ISimpleCounter));
                Expect.Call(simpleCounterFactory.IsSingleton).Return(true);
                Expect.Call(simpleCounterFactory.GetObject()).Return(new SimpleCounter());

                mocks.ReplayAll();

                of.RegisterSingleton("simpleCounter", simpleCounterFactory);
                SaoExporter saoExporter = new SaoExporter();
                saoExporter.ObjectFactory = of;
                saoExporter.TargetName = "simpleCounter";
                saoExporter.ServiceName = "RemotedSaoCallCounter";
                saoExporter.AfterPropertiesSet();
                of.RegisterSingleton("simpleCounterExporter", saoExporter); // also tests SaoExporter.Dispose()!

                AssertExportedService(saoExporter.ServiceName, 2);

                mocks.VerifyAll();
            }
        }
 public void RegisterExistingSingletonWithAutowire()
 {
     DefaultListableObjectFactory lof = new DefaultListableObjectFactory();
     MutablePropertyValues pvs = new MutablePropertyValues();
     pvs.Add("name", "Tony");
     pvs.Add("age", "48");
     RootObjectDefinition rod = new RootObjectDefinition(typeof(DependenciesObject), pvs, true);
     rod.DependencyCheck = DependencyCheckingMode.Objects;
     rod.AutowireMode = AutoWiringMode.ByType;
     lof.RegisterObjectDefinition("test", rod);
     object singletonObject = new TestObject();
     lof.RegisterSingleton("singletonObject", singletonObject);
     Assert.IsTrue(lof.ContainsObject("singletonObject"));
     Assert.IsTrue(lof.IsSingleton("singletonObject"));
     Assert.AreEqual(0, lof.GetAliases("singletonObject").Count);
     DependenciesObject test = (DependenciesObject)lof.GetObject("test");
     Assert.AreEqual(singletonObject, lof.GetObject("singletonObject"));
     Assert.AreEqual(singletonObject, test.Spouse);
 }
 public void RegisterExistingSingletonWithAlreadyBound()
 {
     DefaultListableObjectFactory lof = new DefaultListableObjectFactory();
     object singletonObject = new TestObject();
     lof.RegisterSingleton("singletonObject", singletonObject);
     lof.RegisterSingleton("singletonObject", singletonObject);
 }
        public void RegisterExistingSingletonWithReference()
        {
            DefaultListableObjectFactory lof = new DefaultListableObjectFactory();

            RootObjectDefinition def = new RootObjectDefinition();
            def.ObjectType = typeof(TestObject);
            def.PropertyValues.Add("Name", "Rick");
            def.PropertyValues.Add("Age", 30);
            def.PropertyValues.Add("Spouse", new RuntimeObjectReference("singletonObject"));
            lof.RegisterObjectDefinition("test", def);

            object singletonObject = new TestObject();
            lof.RegisterSingleton("singletonObject", singletonObject);
            Assert.IsTrue(lof.IsSingleton("singletonObject"));
            TestObject test = (TestObject)lof.GetObject("test");
            Assert.AreEqual(singletonObject, lof.GetObject("singletonObject"));
            Assert.AreEqual(singletonObject, test.Spouse);
            IDictionary<string, object> objectsOfType = lof.GetObjectsOfType(typeof(TestObject), false, true);
            Assert.AreEqual(2, objectsOfType.Count);
            Assert.IsTrue(objectsOfType.Values.Contains(test));
            Assert.IsTrue(objectsOfType.Values.Contains(singletonObject));
        }
 public void HierarchicalObjectFactoryChildParentResolution()
 {
     DefaultListableObjectFactory parent = new DefaultListableObjectFactory();
     DefaultListableObjectFactory child = new DefaultListableObjectFactory(parent);
     parent.RegisterSingleton("parent", new Parent());
     child.RegisterObjectDefinition("child", new RootObjectDefinition(typeof (Child), AutoWiringMode.AutoDetect));
     Child c = (Child) child.GetObject("child");
     Assert.IsNotNull(c);
 }
        public void ProbesSharedStateFactories()
        {
            DefaultListableObjectFactory of = new DefaultListableObjectFactory();

            MockRepository mocks = new MockRepository();
            ISharedStateFactory ssf1 = (ISharedStateFactory)mocks.CreateMock( typeof( ISharedStateFactory ) );
            ISharedStateFactory ssf2 = (ISharedStateFactory)mocks.CreateMock( typeof( ISharedStateFactory ) );
            ISharedStateFactory ssf3 = (ISharedStateFactory)mocks.CreateMock( typeof( ISharedStateFactory ) );
            ISharedStateFactory ssf4 = (ISharedStateFactory)mocks.CreateMock( typeof( ISharedStateFactory ) );
            IDictionary ssf3ProvidedState = new Hashtable();

            SharedStateAwareProcessor ssap = new SharedStateAwareProcessor();
            ssap.SharedStateFactories = new ISharedStateFactory[] { ssf1, ssf2, ssf3, ssf4 };
            of.RegisterSingleton( "ssap", ssap );

            ISharedStateAware ssa = (ISharedStateAware)mocks.DynamicMock( typeof( ISharedStateAware ) );

            // Ensure we iterate over configured SharedStateFactories until 
            // the first provider is found that
            // a) true == provider.CanProvideState( instance, name )
            // b) null != provider.GetSharedState( instance, name )

            using (Record( mocks ))
            {
                Expect.Call( ssa.SharedState ).Return( null );
                Expect.Call( ssf1.CanProvideState( ssa, "pageName" ) ).Return( false );
                Expect.Call( ssf2.CanProvideState( ssa, "pageName" ) ).Return( true );
                Expect.Call( ssf2.GetSharedStateFor( ssa, "pageName" ) ).Return( null );
                Expect.Call( ssf3.CanProvideState( ssa, "pageName" ) ).Return( true );
                Expect.Call( ssf3.GetSharedStateFor( ssa, "pageName" ) ).Return( ssf3ProvidedState );
                Expect.Call( ssa.SharedState = ssf3ProvidedState );
            }

            using (Playback( mocks ))
            {
                ssap.PostProcessBeforeInitialization( ssa, "pageName" );
            }
        }
        public void IgnoresAlreadyPopulatedState()
        {
            DefaultListableObjectFactory of = new DefaultListableObjectFactory();

            MockRepository mocks = new MockRepository();
            ISharedStateFactory ssf1 = (ISharedStateFactory)mocks.CreateMock( typeof( ISharedStateFactory ) );
            ISharedStateAware ssa = (ISharedStateAware)mocks.DynamicMock( typeof( ISharedStateAware ) );

            SharedStateAwareProcessor ssap = new SharedStateAwareProcessor();
            ssap.SharedStateFactories = new ISharedStateFactory[] { ssf1 };
            of.RegisterSingleton( "ssap", ssap );

            using (Record( mocks ))
            {
                // preset SharedState - ssap must ignore it
                Expect.Call( ssa.SharedState ).Return( new Hashtable() );
                // expect nothing else!
            }

            using (Playback( mocks ))
            {
                ssap.PostProcessBeforeInitialization( ssa, "myPage" );
            }
        }
 public void RegisterExistingSingletonWithAlreadyBound()
 {
     DefaultListableObjectFactory lof = new DefaultListableObjectFactory();
     object singletonObject = new TestObject();
     lof.RegisterSingleton("singletonObject", singletonObject);
     Assert.Throws<ObjectDefinitionStoreException>(() => lof.RegisterSingleton("singletonObject", singletonObject));
 }