コード例 #1
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for PersistentMemoryCache`1 Constructor
        ///</summary>
        public void PersistentMemoryCacheTestHelper <T>(T testValue)
        {
            Random rand    = new Random(DateTime.Now.Millisecond);
            string name    = Path.Combine(Path.GetTempPath(), "MLifterCacheTest_" + rand.Next(100, 10000));
            string testKey = "test";

            if (PersistentDictionaryFile.Exists(Path.Combine(name, "values")) || PersistentDictionaryFile.Exists(Path.Combine(name, "lifetime")))
            {
                Assert.Fail("Cache already exisits");
            }

            Directory.CreateDirectory(name);
            using (PersistentMemoryCache <T> target = new PersistentMemoryCache <T>(name))
            {
                target.Add(testKey, testValue, DateTime.Now.AddDays(1));
                Assert.AreEqual(target[testKey], testValue, "Value not saved!");
                target.Remove(testKey);
                Assert.IsNull(target[testKey], "Value not removed!");

                target.Add(testKey, testValue, DateTime.Now.AddDays(1));
                target.Dispose();
            }

            using (PersistentMemoryCache <T> reload = new PersistentMemoryCache <T>(name))
            {
                Assert.AreEqual(reload[testKey], testValue, "Reloaded value not restored!");
                reload.Remove(testKey);
                Assert.IsNull(reload[testKey], "Reloaded value not removed!");
                reload.Dispose();
            }

            using (PersistentMemoryCache <T> reload = new PersistentMemoryCache <T>(name))
            {
                Assert.IsNull(reload[testKey], "Reloaded value not null!");

                reload.Add(testKey, testValue, DateTime.Now.AddMilliseconds(50));
                Assert.AreEqual(reload[testKey], testValue, "Timeout to low!");
                Thread.Sleep(75);
                Assert.IsNull(reload[testKey], "Expired value not null!");

                reload.Dispose();
            }

            PersistentDictionaryFile.DeleteFiles(Path.Combine(name, "values"));
            PersistentDictionaryFile.DeleteFiles(Path.Combine(name, "lifetime"));
            if (PersistentDictionaryFile.Exists(Path.Combine(name, "values")) || PersistentDictionaryFile.Exists(Path.Combine(name, "lifetime")))
            {
                Assert.Fail("Cache not deleted!");
            }
        }
コード例 #2
0
ファイル: FeedTreeNode.cs プロジェクト: hmehr/OSS
        /// <summary>
        /// Loads the content of the web.
        /// </summary>
        /// <remarks>Documented by Dev05, 2009-06-26</remarks>
        public void LoadWebContent(object path)
        {
            IsListLoaded = false;
            IsLoaded = false;

            string configPath = path as string;

            nodes = new Dictionary<int, FeedCategoryTreeNode>();
            if (TreeView.InvokeRequired)
                TreeView.Invoke((MethodInvoker)delegate { Nodes.Clear(); });
            else
                Nodes.Clear();

            try
            {
                if (TreeView.InvokeRequired)
                    TreeView.Invoke((MethodInvoker)delegate
                    {
                        Text = String.IsNullOrWhiteSpace(Feed.Name) ? Resources.FeedTreeNode_Name : Feed.Name;
                    });
                else
                    Text = String.IsNullOrWhiteSpace(Feed.Name) ? Resources.FeedTreeNode_Name : Feed.Name;

                #region read categories

                XmlReader categoryFeedReader = XmlReader.Create(Feed.CategoriesUri);
                SyndicationFeed categoryFeed = SyndicationFeed.Load(categoryFeedReader);

                List<ModuleCategory> categoriesToAdd = new List<ModuleCategory>();
                foreach (SyndicationItem item in categoryFeed.Items)
                {
                    ModuleCategory category = new ModuleCategory()
                    {
                        Id = Convert.ToInt32(item.Id),
                        Title = item.Title.Text,
                        ParentCategory = Convert.ToInt32(item.Links[0].Title)
                    };
                    categoriesToAdd.Add(category);
                }
                categoriesToAdd.Sort((a, b) => a.Id.CompareTo(b.Id));
                categoriesToAdd.ForEach(c => AddCategoryNode(c));

                #endregion

                #region read modules

                XmlReader moduleFeedReader = XmlReader.Create(Feed.ModulesUri);
                SyndicationFeed moduleFeed = SyndicationFeed.Load(moduleFeedReader);

                List<ModuleInfo> modules = new List<ModuleInfo>();
                XmlSerializer infoSerializer = new XmlSerializer(typeof(ModuleInfo));
                Dictionary<string, SyndicationItem> items = new Dictionary<string, SyndicationItem>();
                foreach (SyndicationItem item in moduleFeed.Items)
                {
                    ModuleInfo info;
                    if (item.Summary != null)
                        info = (ModuleInfo)infoSerializer.Deserialize(XmlReader.Create(new StringReader(WebUtility.HtmlDecode(item.Summary.Text))));
                    else
                        info = new ModuleInfo();
                    info.Id = item.Id;
                    info.Title = item.Title.Text;
                    info.EditDate = item.LastUpdatedTime.ToString();
                    if (item.Contributors.Count > 0)
                    {
                        info.Author = item.Contributors[0].Name;
                        info.AuthorMail = item.Contributors[0].Email;
                        info.AuthorUrl = item.Contributors[0].Uri;
                    }
                    if (item.Content is TextSyndicationContent)
                        info.Description = (item.Content as TextSyndicationContent).Text;
                    info.Categories = new SerializableList<string>();
                    foreach (SyndicationCategory cat in item.Categories)
                        info.Categories.Add(cat.Label);
                    foreach (SyndicationLink link in item.Links)
                    {
                        if (link.RelationshipType == AtomLinkRelationshipType.Module.ToString())
                        {
                            info.Size = link.Length;
                            info.DownloadUrl = link.Uri.AbsoluteUri;
                        }
                    }
                    modules.Add(info);
                    items.Add(info.Id, item);
                }

                categories.ForEach(c => nodes[c.Id].SetModuleList(modules.FindAll(p => p.Categories.Contains(c.Id.ToString()))));

                #endregion

                if (TreeView.InvokeRequired)
                    TreeView.Invoke((MethodInvoker)delegate { Expand(); });
                else
                    Expand();
                IsListLoaded = true;

                OnContentLoaded(EventArgs.Empty);

                #region read images

                using (PersistentMemoryCache<ModuleInfoCacheItem> cache = new PersistentMemoryCache<ModuleInfoCacheItem>("Feed_" + moduleFeed.Id))
                {
                    WebClient webClient = new WebClient();
                    foreach (ModuleInfo basicInfo in modules)
                    {
                        try
                        {
                            ModuleInfo info = basicInfo;
                            SyndicationItem item = items[basicInfo.Id];
                            string cacheKey = String.Format("{0}##{1}##{2}", moduleFeed.Id, item.Id, item.LastUpdatedTime);

                            if (cache.Contains(cacheKey))
                            {
                                ModuleInfoCacheItem cacheItem = (ModuleInfoCacheItem)cache[cacheKey];
                                info.IconSmall = Convert.FromBase64String(cacheItem.IconSmall);
                                info.IconBig = Convert.FromBase64String(cacheItem.IconBig);
                                info.Preview = Convert.FromBase64String(cacheItem.Preview);
                            }
                            else
                            {
                                ModuleInfoCacheItem cacheItem = new ModuleInfoCacheItem(info.Id);
                                foreach (SyndicationLink link in item.Links)
                                {
                                    if (link.RelationshipType == AtomLinkRelationshipType.IconSmall.ToString())
                                        cacheItem.IconSmall = Convert.ToBase64String(info.IconSmall = webClient.DownloadData(link.Uri));
                                    if (link.RelationshipType == AtomLinkRelationshipType.IconBig.ToString())
                                        cacheItem.IconBig = Convert.ToBase64String(info.IconBig = webClient.DownloadData(link.Uri));
                                    if (link.RelationshipType == AtomLinkRelationshipType.Preview.ToString())
                                        cacheItem.Preview = Convert.ToBase64String(info.Preview = webClient.DownloadData(link.Uri));
                                }
                                cache.Set(cacheKey, cacheItem, DateTime.Now.AddYears(1));
                            }

                            if (TreeView.InvokeRequired)
                                TreeView.Invoke((MethodInvoker)delegate { LoadDetails(info); });
                            else
                                LoadDetails(info);
                        }
                        catch (Exception exp) { Trace.WriteLine(exp.ToString()); }
                    }
                    cache.Dispose();
                }

                #endregion

                IsLoaded = true;
            }
            catch (Exception)
            {
                try
                {
                    if (TreeView.InvokeRequired)
                        TreeView.Invoke((MethodInvoker)delegate { Text = Resources.FeedTreeNode_Text_Offline; });
                    else
                        Text = Resources.FeedTreeNode_Text_Offline;
                }
                catch (ObjectDisposedException) { }
            }
        }
コード例 #3
0
        /// <summary>
        /// Loads the content of the web.
        /// </summary>
        /// <remarks>Documented by Dev05, 2009-06-26</remarks>
        public void LoadWebContent(object path)
        {
            IsListLoaded = false;
            IsLoaded     = false;

            string configPath = path as string;

            nodes = new Dictionary <int, FeedCategoryTreeNode>();
            if (TreeView.InvokeRequired)
            {
                TreeView.Invoke((MethodInvoker) delegate { Nodes.Clear(); });
            }
            else
            {
                Nodes.Clear();
            }

            try
            {
                if (TreeView.InvokeRequired)
                {
                    TreeView.Invoke((MethodInvoker) delegate
                    {
                        Text = String.IsNullOrWhiteSpace(Feed.Name) ? Resources.FeedTreeNode_Name : Feed.Name;
                    });
                }
                else
                {
                    Text = String.IsNullOrWhiteSpace(Feed.Name) ? Resources.FeedTreeNode_Name : Feed.Name;
                }

                #region read categories

                XmlReader       categoryFeedReader = XmlReader.Create(Feed.CategoriesUri);
                SyndicationFeed categoryFeed       = SyndicationFeed.Load(categoryFeedReader);

                List <ModuleCategory> categoriesToAdd = new List <ModuleCategory>();
                foreach (SyndicationItem item in categoryFeed.Items)
                {
                    ModuleCategory category = new ModuleCategory()
                    {
                        Id             = Convert.ToInt32(item.Id),
                        Title          = item.Title.Text,
                        ParentCategory = Convert.ToInt32(item.Links[0].Title)
                    };
                    categoriesToAdd.Add(category);
                }
                categoriesToAdd.Sort((a, b) => a.Id.CompareTo(b.Id));
                categoriesToAdd.ForEach(c => AddCategoryNode(c));

                #endregion

                #region read modules

                XmlReader       moduleFeedReader = XmlReader.Create(Feed.ModulesUri);
                SyndicationFeed moduleFeed       = SyndicationFeed.Load(moduleFeedReader);

                List <ModuleInfo> modules                  = new List <ModuleInfo>();
                XmlSerializer     infoSerializer           = new XmlSerializer(typeof(ModuleInfo));
                Dictionary <string, SyndicationItem> items = new Dictionary <string, SyndicationItem>();
                foreach (SyndicationItem item in moduleFeed.Items)
                {
                    ModuleInfo info;
                    if (item.Summary != null)
                    {
                        info = (ModuleInfo)infoSerializer.Deserialize(XmlReader.Create(new StringReader(WebUtility.HtmlDecode(item.Summary.Text))));
                    }
                    else
                    {
                        info = new ModuleInfo();
                    }
                    info.Id       = item.Id;
                    info.Title    = item.Title.Text;
                    info.EditDate = item.LastUpdatedTime.ToString();
                    if (item.Contributors.Count > 0)
                    {
                        info.Author     = item.Contributors[0].Name;
                        info.AuthorMail = item.Contributors[0].Email;
                        info.AuthorUrl  = item.Contributors[0].Uri;
                    }
                    if (item.Content is TextSyndicationContent)
                    {
                        info.Description = (item.Content as TextSyndicationContent).Text;
                    }
                    info.Categories = new SerializableList <string>();
                    foreach (SyndicationCategory cat in item.Categories)
                    {
                        info.Categories.Add(cat.Label);
                    }
                    foreach (SyndicationLink link in item.Links)
                    {
                        if (link.RelationshipType == AtomLinkRelationshipType.Module.ToString())
                        {
                            info.Size        = link.Length;
                            info.DownloadUrl = link.Uri.AbsoluteUri;
                        }
                    }
                    modules.Add(info);
                    items.Add(info.Id, item);
                }

                categories.ForEach(c => nodes[c.Id].SetModuleList(modules.FindAll(p => p.Categories.Contains(c.Id.ToString()))));

                #endregion

                if (TreeView.InvokeRequired)
                {
                    TreeView.Invoke((MethodInvoker) delegate { Expand(); });
                }
                else
                {
                    Expand();
                }
                IsListLoaded = true;

                OnContentLoaded(EventArgs.Empty);

                #region read images

                using (PersistentMemoryCache <ModuleInfoCacheItem> cache = new PersistentMemoryCache <ModuleInfoCacheItem>("Feed_" + moduleFeed.Id))
                {
                    WebClient webClient = new WebClient();
                    foreach (ModuleInfo basicInfo in modules)
                    {
                        try
                        {
                            ModuleInfo      info     = basicInfo;
                            SyndicationItem item     = items[basicInfo.Id];
                            string          cacheKey = String.Format("{0}##{1}##{2}", moduleFeed.Id, item.Id, item.LastUpdatedTime);

                            if (cache.Contains(cacheKey))
                            {
                                ModuleInfoCacheItem cacheItem = (ModuleInfoCacheItem)cache[cacheKey];
                                info.IconSmall = Convert.FromBase64String(cacheItem.IconSmall);
                                info.IconBig   = Convert.FromBase64String(cacheItem.IconBig);
                                info.Preview   = Convert.FromBase64String(cacheItem.Preview);
                            }
                            else
                            {
                                ModuleInfoCacheItem cacheItem = new ModuleInfoCacheItem(info.Id);
                                foreach (SyndicationLink link in item.Links)
                                {
                                    if (link.RelationshipType == AtomLinkRelationshipType.IconSmall.ToString())
                                    {
                                        cacheItem.IconSmall = Convert.ToBase64String(info.IconSmall = webClient.DownloadData(link.Uri));
                                    }
                                    if (link.RelationshipType == AtomLinkRelationshipType.IconBig.ToString())
                                    {
                                        cacheItem.IconBig = Convert.ToBase64String(info.IconBig = webClient.DownloadData(link.Uri));
                                    }
                                    if (link.RelationshipType == AtomLinkRelationshipType.Preview.ToString())
                                    {
                                        cacheItem.Preview = Convert.ToBase64String(info.Preview = webClient.DownloadData(link.Uri));
                                    }
                                }
                                cache.Set(cacheKey, cacheItem, DateTime.Now.AddYears(1));
                            }

                            if (TreeView.InvokeRequired)
                            {
                                TreeView.Invoke((MethodInvoker) delegate { LoadDetails(info); });
                            }
                            else
                            {
                                LoadDetails(info);
                            }
                        }
                        catch (Exception exp) { Trace.WriteLine(exp.ToString()); }
                    }
                    cache.Dispose();
                }

                #endregion

                IsLoaded = true;
            }
            catch (Exception)
            {
                try
                {
                    if (TreeView.InvokeRequired)
                    {
                        TreeView.Invoke((MethodInvoker) delegate { Text = Resources.FeedTreeNode_Text_Offline; });
                    }
                    else
                    {
                        Text = Resources.FeedTreeNode_Text_Offline;
                    }
                }
                catch (ObjectDisposedException) { }
            }
        }