Revert "Merge pull request #5330 from alexanderkyte/dedup_mkbundle"
[mono.git] / mcs / class / System.Web / Test / System.Web.Caching / CacheItemPriorityQueueTest.cs
index d794bea4c66caba2e1109763fa8b9bf2e59fd635..b9754e7e84ae71fc7f1fe99212851ee93e7d3546 100644 (file)
 //
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
+using System.IO;
+using System.Reflection;
+using System.Text;
 using System.Web.Caching;
 
 using NUnit.Framework;
@@ -35,54 +39,96 @@ namespace MonoTests.System.Web.Caching
        [TestFixture]
        public partial class CacheItemPriorityQueueTest
        {
-               enum QueueOperation
-               {
-                       Enqueue,
-                       Dequeue,
-                       Disable,
-                       Peek,
-                       QueueSize
-               }
+               const string DATA_DIR = "CacheItemPriorityQueueTestData";
+               static readonly string dataDir;
                
-               sealed class TestItem
-               {
-                       public int ListIndex;
-                       public int QueueCount;
-                       public QueueOperation Operation;
-                       public bool IsDisabled;
-                       public bool IsNull;
-                       public bool Disable;
-                       public int OperationCount;
-                       public string Guid;
-               }
-
                sealed class TestCacheItem : CacheItem
                {
                        public Guid Guid;
 
-                       public TestCacheItem ()
+                       public TestCacheItem (string dataLine)
                        {
-                               Guid = Guid.NewGuid ();
+                               string[] data = dataLine.Split (',');
+                               int index = 0;
+                               
+                               Key = LoadField <string> (index++, data);
+                               AbsoluteExpiration = new DateTime (LoadField <long> (index++, data));
+                               SlidingExpiration = new TimeSpan (LoadField <long> (index++, data));
+                               Priority = LoadField <CacheItemPriority> (index++, data);
+                               LastChange = new DateTime (LoadField <long> (index++, data));
+                               ExpiresAt = LoadField <long> (index++, data);
+                               Disabled = LoadField <bool> (index++, data);
+                               Guid = new Guid (LoadField <string> (index++, data));
+                               if (data.Length > index)
+                                       PriorityQueueIndex = LoadField <int> (index++, data);
+                               else
+                                       PriorityQueueIndex = -1;
                        }
 
                        public override string ToString ()
                        {
                                return String.Format ("CacheItem [{0}]\n[{1}][{2}][{3}]", this.Guid, Key, Disabled, ExpiresAt > 0 ? new DateTime (ExpiresAt).ToString () : "0");
                        }
+
+                       T LoadField <T> (int index, string[] data)
+                       {
+                               if (data == null || data.Length <= index)
+                                       throw new ArgumentOutOfRangeException ("index");
+                
+                               string s = data [index];
+                               if (String.IsNullOrEmpty (s))
+                                       return default (T);
+                
+                               TypeConverter cvt = TypeDescriptor.GetConverter (typeof (T));
+                               if (cvt == null)
+                                       throw new InvalidOperationException (String.Format ("Cannot find converter for type {0}, field {1}", typeof (T), index));
+                               if (!cvt.CanConvertFrom (typeof (string)))
+                                       throw new InvalidOperationException (String.Format ("Converter for type {0} cannot convert from string, field {1}", typeof (T), index));
+                               
+                               return (T)cvt.ConvertFromString (s);
+                       }
+               }
+
+               static CacheItemPriorityQueueTest ()
+               {
+                       dataDir =
+                               Path.Combine (
+                                       Path.Combine (
+                                               Path.Combine (Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location), "Test"),
+                                               "System.Web.Caching"),
+                                       DATA_DIR);
                }
                
-               void RunTest (List <TestItem> tests, List <TestCacheItem> list)
+               void RunTest (string testsFileName, string listFileName)
                {
                        var queue = new CacheItemPriorityQueue ();
+                       var cacheItems = new List <TestCacheItem> ();
+                       string listPath = Path.Combine (dataDir, listFileName);
+                       string testsPath = Path.Combine (dataDir, testsFileName);
+                       string line;
+                       
+                       using (var sr = new StreamReader (listPath, Encoding.UTF8)) {
+                               while ((line = sr.ReadLine ()) != null) {
+                                       if (line [0] == '#')
+                                               continue;
+                                       cacheItems.Add (new TestCacheItem (line));
+                               }
+                       }
 
-                       foreach (TestItem item in tests)
-                               RunItem (item, queue, list);
+                       using (var sr = new StreamReader (testsPath, Encoding.UTF8)) {
+                               int i = 0;
+                               while ((line = sr.ReadLine ()) != null) {
+                                       if (line [0] == '#')
+                                               continue;
+                                       RunItem (new CacheItemPriorityQueueTestItem (line), queue, cacheItems, i++);
+                               }
+                       }
                }
 
-               void RunItem (TestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list)
+               void RunItem (CacheItemPriorityQueueTestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list, int testNum)
                {
                        TestCacheItem ci;
-                       string messagePrefix = String.Format ("{0}-{1:00000}-", item.Operation, item.OperationCount);
+                       string messagePrefix = String.Format ("{0}-{1:00000}-{2:00000}-", item.Operation, item.OperationCount, testNum);
                        
                        switch (item.Operation) {
                                case QueueOperation.Enqueue:
@@ -130,6 +176,19 @@ namespace MonoTests.System.Web.Caching
                                case QueueOperation.QueueSize:
                                        Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
                                        break;
+
+                               case QueueOperation.Update:
+                                       ci = list [item.ListIndex];
+                                       queue.Update (ci);
+                                       if (item.IsNull)
+                                               Assert.IsNull (ci, messagePrefix + "1");
+                                       else {
+                                               Assert.IsNotNull (ci, messagePrefix + "2");
+                                               Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
+                                               Assert.AreEqual (item.ExpiresAt, ci.ExpiresAt, messagePrefix + "4");
+                                               Assert.AreEqual (item.PriorityQueueIndex, ci.PriorityQueueIndex, messagePrefix + "5");
+                                       }
+                                       break;
                                        
                                default:
                                        Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);