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"); } }
/// <summary> /// Iterates over configured list of <see cref="ISharedStateFactory"/>s until /// the first provider is found that<br/> /// a) true == provider.CanProvideState( instance, name )<br/> /// b) null != provider.GetSharedState( instance, name )<br/> /// </summary> public object PostProcessBeforeInitialization(object instance, string name) { if (SharedStateFactories.Length == 0) { return(instance); } ISharedStateAware ssa = instance as ISharedStateAware; if (ssa != null && ssa.SharedState == null) { // probe for first factory willing to serve shared state foreach (ISharedStateFactory ssf in _sharedStateFactories) { if (ssf.CanProvideState(ssa, name)) { IDictionary sharedState = ssf.GetSharedStateFor(ssa, name); if (sharedState != null) { ssa.SharedState = sharedState; break; } } } } return(instance); }
public void ProbesSharedStateFactories() { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); ISharedStateFactory ssf1 = A.Fake <ISharedStateFactory>(); ISharedStateFactory ssf2 = A.Fake <ISharedStateFactory>(); ISharedStateFactory ssf3 = A.Fake <ISharedStateFactory>(); ISharedStateFactory ssf4 = A.Fake <ISharedStateFactory>(); IDictionary ssf3ProvidedState = new Hashtable(); SharedStateAwareProcessor ssap = new SharedStateAwareProcessor(); ssap.SharedStateFactories = new ISharedStateFactory[] { ssf1, ssf2, ssf3, ssf4 }; of.RegisterSingleton("ssap", ssap); ISharedStateAware ssa = A.Fake <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 ) A.CallTo(() => ssa.SharedState).Returns(null).Once(); A.CallTo(() => ssf1.CanProvideState(ssa, "pageName")).Returns(false).Once(); A.CallTo(() => ssf2.CanProvideState(ssa, "pageName")).Returns(true).Once(); A.CallTo(() => ssf2.GetSharedStateFor(ssa, "pageName")).Returns(null); A.CallTo(() => ssf3.CanProvideState(ssa, "pageName")).Returns(true).Once(); A.CallTo(() => ssf3.GetSharedStateFor(ssa, "pageName")).Returns(ssf3ProvidedState).Once(); ssap.PostProcessBeforeInitialization(ssa, "pageName"); Assert.That(Equals(ssa.SharedState, ssf3ProvidedState)); }
public void IgnoresAlreadyPopulatedState() { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); ISharedStateFactory ssf1 = A.Fake <ISharedStateFactory>(); ISharedStateAware ssa = A.Fake <ISharedStateAware>(); SharedStateAwareProcessor ssap = new SharedStateAwareProcessor(); ssap.SharedStateFactories = new ISharedStateFactory[] { ssf1 }; of.RegisterSingleton("ssap", ssap); // preset SharedState - ssap must ignore it A.CallTo(() => ssa.SharedState).Returns(new Hashtable()); ssap.PostProcessBeforeInitialization(ssa, "myPage"); A.CallTo(ssa).Where(x => x.Method.Name == "set_SharedState").MustNotHaveHappened(); }
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"); } }
/// <summary> /// Creates a new cache instance and attaches it to the given <see cref="sharedStateHolder" /> /// </summary> /// <param name="sharedStateHolder">the holder of the shared state dictionary to be used for caching.</param> public SharedStateResourceCache(ISharedStateAware sharedStateHolder) { AssertUtils.ArgumentNotNull(sharedStateHolder, "sharedStateHolder"); this.sharedStateHolder = sharedStateHolder; }