Revert "Merge pull request #5330 from alexanderkyte/dedup_mkbundle"
[mono.git] / mcs / class / System.Web / Test / System.Web.Caching / CacheItemPriorityQueueTest.cs
1 // Authors:
2 //      Marek Habersack <mhabersack@novell.com>
3 //
4 // Copyright (C) 2010 Novell Inc. http://novell.com
5 //
6
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 using System;
28 using System.Collections.Generic;
29 using System.ComponentModel;
30 using System.IO;
31 using System.Reflection;
32 using System.Text;
33 using System.Web.Caching;
34
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Web.Caching
38 {
39         [TestFixture]
40         public partial class CacheItemPriorityQueueTest
41         {
42                 const string DATA_DIR = "CacheItemPriorityQueueTestData";
43                 static readonly string dataDir;
44                 
45                 sealed class TestCacheItem : CacheItem
46                 {
47                         public Guid Guid;
48
49                         public TestCacheItem (string dataLine)
50                         {
51                                 string[] data = dataLine.Split (',');
52                                 int index = 0;
53                                 
54                                 Key = LoadField <string> (index++, data);
55                                 AbsoluteExpiration = new DateTime (LoadField <long> (index++, data));
56                                 SlidingExpiration = new TimeSpan (LoadField <long> (index++, data));
57                                 Priority = LoadField <CacheItemPriority> (index++, data);
58                                 LastChange = new DateTime (LoadField <long> (index++, data));
59                                 ExpiresAt = LoadField <long> (index++, data);
60                                 Disabled = LoadField <bool> (index++, data);
61                                 Guid = new Guid (LoadField <string> (index++, data));
62                                 if (data.Length > index)
63                                         PriorityQueueIndex = LoadField <int> (index++, data);
64                                 else
65                                         PriorityQueueIndex = -1;
66                         }
67
68                         public override string ToString ()
69                         {
70                                 return String.Format ("CacheItem [{0}]\n[{1}][{2}][{3}]", this.Guid, Key, Disabled, ExpiresAt > 0 ? new DateTime (ExpiresAt).ToString () : "0");
71                         }
72
73                         T LoadField <T> (int index, string[] data)
74                         {
75                                 if (data == null || data.Length <= index)
76                                         throw new ArgumentOutOfRangeException ("index");
77                 
78                                 string s = data [index];
79                                 if (String.IsNullOrEmpty (s))
80                                         return default (T);
81                 
82                                 TypeConverter cvt = TypeDescriptor.GetConverter (typeof (T));
83                                 if (cvt == null)
84                                         throw new InvalidOperationException (String.Format ("Cannot find converter for type {0}, field {1}", typeof (T), index));
85                                 if (!cvt.CanConvertFrom (typeof (string)))
86                                         throw new InvalidOperationException (String.Format ("Converter for type {0} cannot convert from string, field {1}", typeof (T), index));
87                                 
88                                 return (T)cvt.ConvertFromString (s);
89                         }
90                 }
91
92                 static CacheItemPriorityQueueTest ()
93                 {
94                         dataDir =
95                                 Path.Combine (
96                                         Path.Combine (
97                                                 Path.Combine (Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location), "Test"),
98                                                 "System.Web.Caching"),
99                                         DATA_DIR);
100                 }
101                 
102                 void RunTest (string testsFileName, string listFileName)
103                 {
104                         var queue = new CacheItemPriorityQueue ();
105                         var cacheItems = new List <TestCacheItem> ();
106                         string listPath = Path.Combine (dataDir, listFileName);
107                         string testsPath = Path.Combine (dataDir, testsFileName);
108                         string line;
109                         
110                         using (var sr = new StreamReader (listPath, Encoding.UTF8)) {
111                                 while ((line = sr.ReadLine ()) != null) {
112                                         if (line [0] == '#')
113                                                 continue;
114                                         cacheItems.Add (new TestCacheItem (line));
115                                 }
116                         }
117
118                         using (var sr = new StreamReader (testsPath, Encoding.UTF8)) {
119                                 int i = 0;
120                                 while ((line = sr.ReadLine ()) != null) {
121                                         if (line [0] == '#')
122                                                 continue;
123                                         RunItem (new CacheItemPriorityQueueTestItem (line), queue, cacheItems, i++);
124                                 }
125                         }
126                 }
127
128                 void RunItem (CacheItemPriorityQueueTestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list, int testNum)
129                 {
130                         TestCacheItem ci;
131                         string messagePrefix = String.Format ("{0}-{1:00000}-{2:00000}-", item.Operation, item.OperationCount, testNum);
132                         
133                         switch (item.Operation) {
134                                 case QueueOperation.Enqueue:
135                                         queue.Enqueue (list [item.ListIndex]);
136                                         Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "1");
137                                         Assert.AreEqual (item.Guid, ((TestCacheItem)queue.Peek ()).Guid.ToString (), messagePrefix + "2");
138                                         break;
139                                         
140                                 case QueueOperation.Dequeue:
141                                         ci = (TestCacheItem)queue.Dequeue ();
142                                         if (item.IsNull)
143                                                 Assert.IsNull (ci, messagePrefix + "1");
144                                         else {
145                                                 Assert.IsNotNull (ci, messagePrefix + "2");
146                                                 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
147                                                 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
148                                         }
149                                         Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
150                                         break;
151                                         
152                                 case QueueOperation.Disable:
153                                         ci = list [item.ListIndex];
154                                         if (item.IsNull)
155                                                 Assert.IsNull (ci, messagePrefix + "1");
156                                         else {
157                                                 Assert.IsNotNull (ci, messagePrefix + "2");
158                                                 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
159                                                 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
160                                                 ci.Disabled = item.Disable;
161                                         }
162                                         break;
163
164                                 case QueueOperation.Peek:
165                                         ci = (TestCacheItem)queue.Peek ();
166                                         if (item.IsNull)
167                                                 Assert.IsNull (ci, messagePrefix + "1");
168                                         else {
169                                                 Assert.IsNotNull (ci, messagePrefix + "2");
170                                                 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
171                                                 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
172                                         }
173                                         Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
174                                         break;
175
176                                 case QueueOperation.QueueSize:
177                                         Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
178                                         break;
179
180                                 case QueueOperation.Update:
181                                         ci = list [item.ListIndex];
182                                         queue.Update (ci);
183                                         if (item.IsNull)
184                                                 Assert.IsNull (ci, messagePrefix + "1");
185                                         else {
186                                                 Assert.IsNotNull (ci, messagePrefix + "2");
187                                                 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
188                                                 Assert.AreEqual (item.ExpiresAt, ci.ExpiresAt, messagePrefix + "4");
189                                                 Assert.AreEqual (item.PriorityQueueIndex, ci.PriorityQueueIndex, messagePrefix + "5");
190                                         }
191                                         break;
192                                         
193                                 default:
194                                         Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);
195                                         break;
196                         }
197                 }
198         }
199 }