コード例 #1
0
        public void ViewLocationCache_DefaultRealCache()
        {
            // Arrange
            Mock <VirtualPathProviderViewEngine> engineMock = new Mock <VirtualPathProviderViewEngine>();
            VirtualPathProviderViewEngine        engine     = engineMock.Object;

            HttpRequest  request      = new HttpRequest("foo.txt", "http://localhost", String.Empty);
            HttpResponse response     = new HttpResponse(TextWriter.Null);
            HttpContext  context      = new HttpContext(request, response);
            HttpContext  savedContext = HttpContext.Current;

            // Act
            IViewLocationCache cache;

            try
            {
                HttpContext.Current = context;
                cache = engine.ViewLocationCache;
            }
            finally
            {
                HttpContext.Current = savedContext;
            }

            // Assert
            Assert.NotNull(cache);
            Assert.IsType <DefaultViewLocationCache>(cache);
        }
コード例 #2
0
        /// <summary>
        /// Gets a view engine that is a clone of the given <paramref name="viewEngine"/> and has enhanced search locations.
        /// </summary>
        /// <param name="viewEngine">The view engine.</param>
        /// <param name="controller">The controller.</param>
        /// <param name="pathTransformations">Transformations that have to be applied to each view engine search path.</param>
        private static IViewEngine GetViewEngine(VirtualPathProviderViewEngine viewEngine, Controller controller, IList <Func <string, string> > pathTransformations)
        {
            var newEngine = (VirtualPathProviderViewEngine)Activator.CreateInstance(viewEngine.GetType());

            newEngine.AreaViewLocationFormats        = ControllerExtensions.AppendControllerVirtualPath(viewEngine.AreaViewLocationFormats, pathTransformations);
            newEngine.AreaMasterLocationFormats      = ControllerExtensions.AppendControllerVirtualPath(viewEngine.AreaPartialViewLocationFormats, pathTransformations);
            newEngine.AreaPartialViewLocationFormats = ControllerExtensions.AppendControllerVirtualPath(viewEngine.AreaPartialViewLocationFormats, pathTransformations);
            newEngine.ViewLocationFormats            = ControllerExtensions.AppendControllerVirtualPath(viewEngine.ViewLocationFormats, pathTransformations);
            newEngine.MasterLocationFormats          = ControllerExtensions.AppendControllerVirtualPath(viewEngine.MasterLocationFormats, pathTransformations);
            newEngine.PartialViewLocationFormats     = ControllerExtensions.AppendControllerVirtualPath(viewEngine.PartialViewLocationFormats, pathTransformations);

            return(newEngine);
        }
コード例 #3
0
        private static IViewEngine GetViewEngine(VirtualPathProviderViewEngine viewEngine, IList <Func <string, string> > pathTransformations)
        {
            VirtualPathProviderViewEngine newEngine;
            var precompiledEngine = viewEngine as CompositePrecompiledMvcEngineWrapper;

            if (precompiledEngine != null)
            {
                if (!precompiledEngine.PackageName.IsNullOrEmpty() &&
                    !string.Equals(precompiledEngine.PackageName, new PackageManager().GetCurrentPackage(), StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }

                newEngine = precompiledEngine.Clone();
            }
            else
            {
                var viewEngineType = viewEngine.GetType();
                var defaultCtor    = viewEngineType.GetConstructor(Type.EmptyTypes);
                if (defaultCtor != null)
                {
                    newEngine = (VirtualPathProviderViewEngine)Activator.CreateInstance(viewEngineType);
                }
                else
                {
                    return(null);
                }
            }

            newEngine.ViewLocationCache = DefaultViewLocationCache.Null;

            newEngine.AreaViewLocationFormats        = ControllerExtensions.AppendControllerVirtualPath(viewEngine.AreaViewLocationFormats, pathTransformations);
            newEngine.AreaMasterLocationFormats      = ControllerExtensions.AppendControllerVirtualPath(viewEngine.AreaPartialViewLocationFormats, pathTransformations);
            newEngine.AreaPartialViewLocationFormats = ControllerExtensions.AppendControllerVirtualPath(viewEngine.AreaPartialViewLocationFormats, pathTransformations);
            newEngine.ViewLocationFormats            = ControllerExtensions.AppendControllerVirtualPath(viewEngine.ViewLocationFormats, pathTransformations);
            newEngine.MasterLocationFormats          = ControllerExtensions.AppendControllerVirtualPath(viewEngine.MasterLocationFormats, pathTransformations);
            newEngine.PartialViewLocationFormats     = ControllerExtensions.AppendControllerVirtualPath(viewEngine.PartialViewLocationFormats, pathTransformations);

            if (precompiledEngine != null)
            {
                newEngine.AreaViewLocationFormats        = newEngine.AreaViewLocationFormats.Select(p => FrontendManager.VirtualPathBuilder.RemoveParams(p)).ToArray();
                newEngine.AreaMasterLocationFormats      = newEngine.AreaMasterLocationFormats.Select(p => FrontendManager.VirtualPathBuilder.RemoveParams(p)).ToArray();
                newEngine.AreaPartialViewLocationFormats = newEngine.AreaPartialViewLocationFormats.Select(p => FrontendManager.VirtualPathBuilder.RemoveParams(p)).ToArray();
                newEngine.ViewLocationFormats            = newEngine.ViewLocationFormats.Select(p => FrontendManager.VirtualPathBuilder.RemoveParams(p)).ToArray();
                newEngine.MasterLocationFormats          = newEngine.MasterLocationFormats.Select(p => FrontendManager.VirtualPathBuilder.RemoveParams(p)).ToArray();
                newEngine.PartialViewLocationFormats     = newEngine.PartialViewLocationFormats.Select(p => FrontendManager.VirtualPathBuilder.RemoveParams(p)).ToArray();
            }

            return(newEngine);
        }
コード例 #4
0
        public void ViewLocationCache_DefaultNullCache()
        {
            // Arrange
            Mock <VirtualPathProviderViewEngine> engineMock = new Mock <VirtualPathProviderViewEngine>();
            VirtualPathProviderViewEngine        engine     = engineMock.Object;

            // Act
            IViewLocationCache cache = engine.ViewLocationCache;

            // Assert
            Assert.NotNull(cache);
            Assert.Equal(DefaultViewLocationCache.Null, cache);
            Assert.IsNotType <DefaultViewLocationCache>(cache);
        }
コード例 #5
0
        public void ValueInCacheBypassesVirtualPathProviderForAllAvailableDisplayModesForContext()
        {
            // Arrange
            string cacheKey       = null;
            string mobileCacheKey = null;

            SetupFileExists(VIEW_VIRTUAL);
            SetupFileExists(MOBILE_VIEW_VIRTUAL);

            _engine.MockCache
            .Setup(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), VIEW_VIRTUAL))
            .Callback <HttpContextBase, string, string>((httpContext, key, virtualPath) =>
            {
                cacheKey = key;
                _engine.MockCache
                .Setup(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), key))
                .Returns(MOBILE_VIEW_VIRTUAL)
                .Verifiable();
            })
            .Verifiable();
            _engine.MockCache
            .Setup(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), MOBILE_VIEW_VIRTUAL))
            .Callback <HttpContextBase, string, string>((httpContext, key, virtualPath) =>
            {
                mobileCacheKey = key;
                _engine.MockCache
                .Setup(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), key))
                .Returns(MOBILE_VIEW_VIRTUAL)
                .Verifiable();
            })
            .Verifiable();

            // Act
            _engine.FindView(_mobileContext, "name", null, false);
            _engine.FindView(_mobileContext, "name", null, true);

            // Assert

            // DefaultDisplayMode with Mobile substitution is cached and hit on the second call to FindView
            _engine.MockPathProvider.Verify(vpp => vpp.FileExists(MOBILE_VIEW_VIRTUAL), Times.AtMostOnce());
            _engine.MockCache.Verify(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), MOBILE_VIEW_VIRTUAL), Times.AtMostOnce());
            _engine.MockCache.Verify(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), VirtualPathProviderViewEngine.AppendDisplayModeToCacheKey(cacheKey, DisplayModeProvider.MobileDisplayModeId)), Times.AtMostOnce());

            _engine.MockPathProvider.Verify(vpp => vpp.FileExists(VIEW_VIRTUAL), Times.AtMostOnce());
            _engine.MockCache.Verify(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), VIEW_VIRTUAL), Times.Exactly(1));

            Assert.NotEqual(cacheKey, mobileCacheKey);

            // Act
            _engine.FindView(_context, "name", null, true);

            // Assert

            // The first call to FindView without a mobile browser results in a cache hit
            _engine.MockPathProvider.Verify(vpp => vpp.FileExists(VIEW_VIRTUAL), Times.AtMostOnce());
            _engine.MockCache.Verify(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), VIEW_VIRTUAL), Times.Exactly(1));
            _engine.MockCache.Verify(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), cacheKey), Times.Exactly(1));
        }
コード例 #6
0
        public void ValueInCacheBypassesVirtualPathProvider()
        {
            // Arrange
            string cacheKey = null;

            SetupFileExists(VIEW_VIRTUAL); // It wasn't found, so they call vpp.FileExists
            SetupFileDoesNotExist(MOBILE_VIEW_VIRTUAL);
            SetupCacheMiss(CreateCacheKey(Cache.View, name: "name", displayMode: "Mobile"));
            _engine.MockCache // Then they set the value into the cache
            .Setup(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), VIEW_VIRTUAL))
            .Callback <HttpContextBase, string, string>((httpContext, key, virtualPath) =>
            {
                cacheKey = key;
                _engine.MockCache     // Second time through, we give them a cache hit
                .Setup(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), key))
                .Returns(VIEW_VIRTUAL)
                .Verifiable();
            })
            .Verifiable();

            // Act
            _engine.FindView(_context, "name", null, false); // Call it once with false to seed the cache
            _engine.FindView(_context, "name", null, true);  // Call it once with true to check the cache

            // Assert

            _engine.MockPathProvider.Verify(vpp => vpp.FileExists(VIEW_VIRTUAL), Times.AtMostOnce());
            _engine.MockCache.Verify(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), VIEW_VIRTUAL), Times.AtMostOnce());
            _engine.MockCache.Verify(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), cacheKey), Times.AtMostOnce());

            // We seed the cache with all possible display modes but since the mobile view does not exist we don't insert it into the cache.
            _engine.MockPathProvider.Verify(vpp => vpp.FileExists(MOBILE_VIEW_VIRTUAL), Times.Exactly(1));
            _engine.MockCache.Verify(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), MOBILE_VIEW_VIRTUAL), Times.Never());
            _engine.MockCache.Verify(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), VirtualPathProviderViewEngine.AppendDisplayModeToCacheKey(cacheKey, DisplayModeProvider.MobileDisplayModeId)), Times.Never());
        }
コード例 #7
0
        protected override string GetFormatedVirtualPath(HttpContextBase context, VirtualPathProviderViewEngine.ViewLocation location, string name, string controllerName, string areaName)
        {
            var clientProvider = IocInstance.Container.Resolve<ISSOClientProvider>();
            var session = clientProvider == null ? null : clientProvider.GetCurrentSession();
            string theme = session == null ? "Default" : session.Theme;
            string skin = session == null ? "Default" : session.Skin;

            return string.Format(location.VirtualPathFormatString, name, controllerName, areaName, theme, skin);
        }