Beispiel #1
0
        public void Create_Path_Id()
        {
            var delegateParameter = new Tuple <HiveId, CSharpCodeBlockType>(HiveId.ConvertIntToGuid(10), CSharpCodeBlockType.NodeFilter);
            var virtualPathId     = delegateParameter.Item1.GetHtmlId() + "_" + delegateParameter.Item2;
            var pathId            = CodeDelegateVirtualPath.GetOrCreateVirtualPathId(delegateParameter, virtualPathId);

            Assert.AreEqual("_DVP_u_guid____00000000000000000000000000000010_NodeFilter", pathId);
        }
Beispiel #2
0
        public void Get_Full_Path()
        {
            var delegateParameter = new Tuple <HiveId, CSharpCodeBlockType>(HiveId.ConvertIntToGuid(10), CSharpCodeBlockType.NodeFilter);
            var virtualPathId     = delegateParameter.Item1.GetHtmlId() + "_" + delegateParameter.Item2;
            var path = CodeDelegateVirtualPath.CreateFullPath(
                CodeDelegateVirtualPath.GetOrCreateVirtualPathId(delegateParameter, virtualPathId), "cs");

            Assert.AreEqual("~/DVP.axd/_DVP_u_guid____00000000000000000000000000000010_NodeFilter.cs", path);
        }
Beispiel #3
0
        public void Get_File()
        {
            const string testSnippet   = @"this is just a test {0}";
            var          virtualPathId = CodeDelegateVirtualPath.GetOrCreateVirtualPathId("MyKey");

            CodeDelegatesCollection.TryAdd(virtualPathId, (dp, vpi) => string.Format(testSnippet, dp));
            var path     = CodeDelegateVirtualPath.CreateFullPath(virtualPathId, "cs").TrimStart("~");
            var provider = new CodeDelegateVirtualPathProvider();
            var file     = provider.GetFile(path);

            Assert.AreEqual(typeof(CodeDelegateVirtualFile), file.GetType());
        }
        private void RefreshCodeBlocks(HiveId attrTypeId, CSharpCodeBlockType cSharpCodeType)
        {
            var delegateParameter = new Tuple <HiveId, CSharpCodeBlockType>(attrTypeId, cSharpCodeType);

            //check if we have this registered (multiple ids may exist for the same delegate, but in thsi case it will never be true)
            var ids = CodeDelegateVirtualPath.GetVirtualPathIdsForDelegate(delegateParameter).ToArray();

            if (ids.Any())
            {
                var codeDelegateId = ids.First();
                CodeDelegateVirtualPath.TryUpdate(codeDelegateId, delegateParameter);
            }
        }
Beispiel #5
0
        public void Is_Delegate_Path()
        {
            //first, don't add, but it is valid
            var path = "/DVP.axd/_DVP_u_guid____00000000000000000000000000000010_NodeFilter.cs";

            Assert.IsFalse(CodeDelegateVirtualPath.IsCodeDelegateVirtualPath(path));

            //now, add it to the collection
            var delegateParameter = new Tuple <HiveId, CSharpCodeBlockType>(HiveId.ConvertIntToGuid(10), CSharpCodeBlockType.NodeFilter);
            var virtualPathId     = delegateParameter.Item1.GetHtmlId() + "_" + delegateParameter.Item2;

            CodeDelegateVirtualPath.GetOrCreateVirtualPathId(delegateParameter, virtualPathId);

            //this should pass now that it is added
            Assert.IsTrue(CodeDelegateVirtualPath.IsCodeDelegateVirtualPath(path));
        }
        /// <summary>
        /// Returns the class instance for the virtual Path id specified
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="virtualPathId"></param>
        /// <returns></returns>
        private static T GetClassInstance <T>(string virtualPathId)
            where T : class
        {
            //get a csharp path for our code delegate
            var virtualPath = CodeDelegateVirtualPath.CreateFullPath(virtualPathId, "cs");
            //compile the code snippet to an assembly
            var compiledAssembly = BuildManager.GetCompiledAssembly(virtualPath);

            if (compiledAssembly != null)
            {
                //now, we need to create instance, as we know we only have one type in the assembly, we'll just use that
                var compiledType = compiledAssembly.GetTypes().First();
                var instance     = (T)Activator.CreateInstance(compiledType);
                return(instance);
            }
            return(null);
        }
Beispiel #7
0
        public void Open_File()
        {
            const string testSnippet   = @"this is just a test {0}";
            var          virtualPathId = CodeDelegateVirtualPath.GetOrCreateVirtualPathId("MyKey");

            CodeDelegatesCollection.TryAdd(virtualPathId, (dp, vpi) => string.Format(testSnippet, dp));

            var def = CodeDelegateVirtualPath.TryGetVirtualPathDefinition(virtualPathId);

            var path = CodeDelegateVirtualPath.CreateFullPath(virtualPathId, "cs");
            var file = new CodeDelegateVirtualFile(virtualPathId, def.Result.Value.Parameter, path);

            using (var sr = new StreamReader(file.Open()))
            {
                Assert.AreEqual(string.Format(testSnippet, "MyKey"), sr.ReadToEnd());
            }
        }
Beispiel #8
0
        public void Created_Path_Passes_Microsoft_VirtualPath_Parser()
        {
            var delegateParameter = new Tuple <HiveId, CSharpCodeBlockType>(new HiveId(Guid.NewGuid()), CSharpCodeBlockType.NodeFilter);
            var virtualPathId     = delegateParameter.Item1.GetHtmlId() + "_" + delegateParameter.Item2;
            var path = CodeDelegateVirtualPath.CreateFullPath(
                CodeDelegateVirtualPath.GetOrCreateVirtualPathId(delegateParameter, virtualPathId), "cs");

            try
            {
                var result = BuildManager.CreateInstanceFromVirtualPath(path, typeof(WebViewPage));
            }
            catch (Exception ex)
            {
                //this is the exception we are trying to avoid
                if (ex is ArgumentException && ex.Message.StartsWith("The relative virtual path") && ex.Message.EndsWith("is not allowed here."))
                {
                    throw;
                }
            }

            //no exception was thrown
            Assert.Pass();
        }
        /// <summary>
        /// Ensures that the code delegate for the filter is in the CodeDelegatesCollection
        /// </summary>
        /// <param name="backOfficeRequestContext"></param>
        /// <param name="dataTypeId"></param>
        /// <param name="blockType"></param>
        /// <param name="codedelegate"> </param>
        /// <returns></returns>
        private static string SetupCodeDelegate(
            IBackOfficeRequestContext backOfficeRequestContext,
            HiveId dataTypeId,
            CSharpCodeBlockType blockType,
            Func <object, string, string> codedelegate)
        {
            var delegateParameter = new Tuple <HiveId, CSharpCodeBlockType>(dataTypeId, blockType);

            //first, check if we have this registered (multiple ids may exist for the same delegate, but in thsi case it will never be true)
            var ids = CodeDelegateVirtualPath.GetVirtualPathIdsForDelegate(delegateParameter).ToArray();

            if (ids.Any())
            {
                return(ids.First());
            }

            //need to lookup the data type to get its alias as we'll use this for the path
            using (var uow = backOfficeRequestContext.Application.Hive.OpenReader <IContentStore>())
            {
                var dt = uow.Repositories.Schemas.Get <AttributeType>(true, dataTypeId).SingleOrDefault();
                if (dt == null)
                {
                    throw new InvalidOperationException("Could not find AttributeType with id " + dataTypeId);
                }
                //create a unique path for our object which will be used in the virtual path creation, if we don't set this
                //then the ToString of the object key will be used which is much harder to debug.
                var path = dt.Alias + "_" + delegateParameter.Item2 + "_"
                           + delegateParameter.GetHashCode().ToString().Replace("-", "0"); //only alphanumeric chars allowed
                var virtualPathId = CodeDelegateVirtualPath.GetOrCreateVirtualPathId(delegateParameter, path);

                //add the delegate to the collection
                CodeDelegatesCollection.TryAdd(virtualPathId, codedelegate);

                return(virtualPathId);
            }
        }
Beispiel #10
0
 public void SetupTest()
 {
     CodeDelegatesCollection.Clear();
     CodeDelegateVirtualPath.ClearPathIds();
 }