public void Match_Document_By_Url_With_Template(string urlAsString)
		{
            var template = CreateTemplate("test");
            var altTemplate = CreateTemplate("blah");
			var routingContext = GetRoutingContext(urlAsString, template);
			var url = routingContext.UmbracoContext.CleanedUmbracoUrl; //very important to use the cleaned up umbraco url
			var docRequest = new PublishedContentRequest(url, routingContext);
			var lookup = new ContentFinderByNiceUrlAndTemplate();

		    SettingsForTests.HideTopLevelNodeFromPath = false;

			var result = lookup.TryFindContent(docRequest);

			Assert.IsTrue(result);
			Assert.IsNotNull(docRequest.PublishedContent);
			Assert.IsNotNull(docRequest.TemplateAlias);
			Assert.AreEqual("blah".ToUpperInvariant(), docRequest.TemplateAlias.ToUpperInvariant());
		}
        public static IContentFinder SubsituteFinder(INotFoundHandler handler)
        {
            IContentFinder finder = null;

            if (handler is global::umbraco.SearchForAlias)
            {
                finder = new ContentFinderByUrlAlias();
            }
            else if (handler is global::umbraco.SearchForProfile)
            {
                finder = new ContentFinderByProfile();
            }
            else if (handler is global::umbraco.SearchForTemplate)
            {
                finder = new ContentFinderByNiceUrlAndTemplate();
            }
            else if (handler is global::umbraco.handle404)
            {
                finder = new ContentFinderByLegacy404();
            }

            return(finder);
        }
        void HandlePageNotFound(PublishedContentRequest docRequest)
        {
            var url = NotFoundHandlerHelper.GetLegacyUrlForNotFoundHandlers();

            LogHelper.Debug <ContentFinderByNotFoundHandlers>("Running for legacy url='{0}'.", () => url);

            foreach (var handler in GetNotFoundHandlers())
            {
                IContentFinder finder      = null;
                var            handlerName = handler.GetType().FullName;

                LogHelper.Debug <ContentFinderByNotFoundHandlers>("Handler '{0}'.", () => handlerName);

                // replace with our own implementation
                if (handler is global::umbraco.SearchForAlias)
                {
                    finder = new ContentFinderByUrlAlias();
                }
                else if (handler is global::umbraco.SearchForProfile)
                {
                    finder = new ContentFinderByProfile();
                }
                else if (handler is global::umbraco.SearchForTemplate)
                {
                    finder = new ContentFinderByNiceUrlAndTemplate();
                }
                else if (handler is global::umbraco.handle404)
                {
                    finder = new ContentFinderByLegacy404();
                }

                if (finder != null)
                {
                    var finderName = finder.GetType().FullName;
                    LogHelper.Debug <ContentFinderByNotFoundHandlers>("Replace handler '{0}' by new finder '{1}'.", () => handlerName, () => finderName);
                    if (finder.TryFindContent(docRequest))
                    {
                        // do NOT set docRequest.PublishedContent again here as
                        // it would clear any template that the finder might have set
                        LogHelper.Debug <ContentFinderByNotFoundHandlers>("Finder '{0}' found node with id={1}.", () => finderName, () => docRequest.PublishedContent.Id);
                        if (docRequest.Is404)
                        {
                            LogHelper.Debug <ContentFinderByNotFoundHandlers>("Finder '{0}' set status to 404.", () => finderName);
                        }

                        // if we found a document, break, don't look at more handler -- we're done
                        break;
                    }

                    // if we did not find a document, continue, look at other handlers
                    continue;
                }

                // else it's a legacy handler, run

                if (handler.Execute(url) && handler.redirectID > 0)
                {
                    var redirectId = handler.redirectID;
                    docRequest.PublishedContent = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById(redirectId);

                    if (!docRequest.HasPublishedContent)
                    {
                        LogHelper.Debug <ContentFinderByNotFoundHandlers>("Handler '{0}' found node with id={1} which is not valid.", () => handlerName, () => redirectId);
                        break;
                    }

                    LogHelper.Debug <ContentFinderByNotFoundHandlers>("Handler '{0}' found valid node with id={1}.", () => handlerName, () => redirectId);

                    if (docRequest.RoutingContext.UmbracoContext.HttpContext.Response.StatusCode == 404)
                    {
                        LogHelper.Debug <ContentFinderByNotFoundHandlers>("Handler '{0}' set status code to 404.", () => handlerName);
                        docRequest.Is404 = true;
                    }

                    //// check for caching
                    //if (handler.CacheUrl)
                    //{
                    //    if (url.StartsWith("/"))
                    //        url = "/" + url;

                    //    var cacheKey = (currentDomain == null ? "" : currentDomain.Name) + url;
                    //    var culture = currentDomain == null ? null : currentDomain.Language.CultureAlias;
                    //    SetCache(cacheKey, new CacheEntry(handler.redirectID.ToString(), culture));

                    //    HttpContext.Current.Trace.Write("NotFoundHandler",
                    //        string.Format("Added to cache '{0}', {1}.", url, handler.redirectID));
                    //}

                    // if we found a document, break, don't look at more handler -- we're done
                    break;
                }

                // if we did not find a document, continue, look at other handlers
            }
        }
		void HandlePageNotFound(PublishedContentRequest docRequest)
        {			
			var url = NotFoundHandlerHelper.GetLegacyUrlForNotFoundHandlers();
            LogHelper.Debug<ContentFinderByNotFoundHandlers>("Running for legacy url='{0}'.", () => url);

            foreach (var handler in GetNotFoundHandlers())
            {
                IContentFinder finder = null;
                var handlerName = handler.GetType().FullName;

                LogHelper.Debug<ContentFinderByNotFoundHandlers>("Handler '{0}'.", () => handlerName);

                // replace with our own implementation
                if (handler is global::umbraco.SearchForAlias)
                    finder = new ContentFinderByUrlAlias();
                else if (handler is global::umbraco.SearchForProfile)
                    finder = new ContentFinderByProfile();
                else if (handler is global::umbraco.SearchForTemplate)
                    finder = new ContentFinderByNiceUrlAndTemplate();
                else if (handler is global::umbraco.handle404)
                    finder = new ContentFinderByLegacy404();

                if (finder != null)
                {
                    var finderName = finder.GetType().FullName;
                    LogHelper.Debug<ContentFinderByNotFoundHandlers>("Replace handler '{0}' by new finder '{1}'.", () => handlerName, () => finderName);
                    if (finder.TryFindContent(docRequest))
                    {
                        // do NOT set docRequest.PublishedContent again here as 
                        // it would clear any template that the finder might have set
                        LogHelper.Debug<ContentFinderByNotFoundHandlers>("Finder '{0}' found node with id={1}.", () => finderName, () => docRequest.PublishedContent.Id);
                        if (docRequest.Is404)
                            LogHelper.Debug<ContentFinderByNotFoundHandlers>("Finder '{0}' set status to 404.", () => finderName);

                        // if we found a document, break, don't look at more handler -- we're done
                        break;
                    }

                    // if we did not find a document, continue, look at other handlers
                    continue;
                }

                // else it's a legacy handler, run

				if (handler.Execute(url) && handler.redirectID > 0)
				{
				    var redirectId = handler.redirectID;
                    docRequest.PublishedContent = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById(redirectId);

                    if (!docRequest.HasPublishedContent)
                    {
                        LogHelper.Debug<ContentFinderByNotFoundHandlers>("Handler '{0}' found node with id={1} which is not valid.", () => handlerName, () => redirectId);
                        break;
                    }

                    LogHelper.Debug<ContentFinderByNotFoundHandlers>("Handler '{0}' found valid node with id={1}.", () => handlerName, () => redirectId);

                    if (docRequest.RoutingContext.UmbracoContext.HttpContext.Response.StatusCode == 404)
                    {
                        LogHelper.Debug<ContentFinderByNotFoundHandlers>("Handler '{0}' set status code to 404.", () => handlerName);
                        docRequest.Is404 = true;
                    }

                    //// check for caching
                    //if (handler.CacheUrl)
                    //{
                    //    if (url.StartsWith("/"))
                    //        url = "/" + url;

                    //    var cacheKey = (currentDomain == null ? "" : currentDomain.Name) + url;
                    //    var culture = currentDomain == null ? null : currentDomain.Language.CultureAlias;
                    //    SetCache(cacheKey, new CacheEntry(handler.redirectID.ToString(), culture));

                    //    HttpContext.Current.Trace.Write("NotFoundHandler",
                    //        string.Format("Added to cache '{0}', {1}.", url, handler.redirectID));
                    //}

                    // if we found a document, break, don't look at more handler -- we're done
                    break;
                }

                // if we did not find a document, continue, look at other handlers
            }
        }