3e1c6d2e5d4ee69f228291c9a52fc90fd40b3618
[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                         // Assumes this is compiled into mcs/class/lib/<profile>
95                         string class_dir = Directory.GetParent (Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location)).Parent.FullName;
96                         string system_web_dir = Path.Combine (class_dir, "System.Web", "Test", "System.Web.Caching");
97                         dataDir = Path.Combine (system_web_dir, DATA_DIR);
98                 }
99                 
100                 void RunTest (string testsFileName, string listFileName)
101                 {
102                         var queue = new CacheItemPriorityQueue ();
103                         var cacheItems = new List <TestCacheItem> ();
104                         string listPath = Path.Combine (dataDir, listFileName);
105                         string testsPath = Path.Combine (dataDir, testsFileName);
106                         string line;
107                         
108                         using (var sr = new StreamReader (listPath, Encoding.UTF8)) {
109                                 while ((line = sr.ReadLine ()) != null) {
110                                         if (line [0] == '#')
111                                                 continue;
112                                         cacheItems.Add (new TestCacheItem (line));
113                                 }
114                         }
115
116                         using (var sr = new StreamReader (testsPath, Encoding.UTF8)) {
117                                 int i = 0;
118                                 while ((line = sr.ReadLine ()) != null) {
119                                         if (line [0] == '#')
120                                                 continue;
121                                         RunItem (new CacheItemPriorityQueueTestItem (line), queue, cacheItems, i++);
122                                 }
123                         }
124                 }
125
126                 void RunItem (CacheItemPriorityQueueTestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list, int testNum)
127                 {
128                         TestCacheItem ci;
129                         string messagePrefix = String.Format ("{0}-{1:00000}-{2:00000}-", item.Operation, item.OperationCount, testNum);
130                         
131                         switch (item.Operation) {
132                                 case QueueOperation.Enqueue:
133                                         queue.Enqueue (list [item.ListIndex]);
134                                         Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "1");
135                                         Assert.AreEqual (item.Guid, ((TestCacheItem)queue.Peek ()).Guid.ToString (), messagePrefix + "2");
136                                         break;
137                                         
138                                 case QueueOperation.Dequeue:
139                                         ci = (TestCacheItem)queue.Dequeue ();
140                                         if (item.IsNull)
141                                                 Assert.IsNull (ci, messagePrefix + "1");
142                                         else {
143                                                 Assert.IsNotNull (ci, messagePrefix + "2");
144                                                 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
145                                                 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
146                                         }
147                                         Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
148                                         break;
149                                         
150                                 case QueueOperation.Disable:
151                                         ci = list [item.ListIndex];
152                                         if (item.IsNull)
153                                                 Assert.IsNull (ci, messagePrefix + "1");
154                                         else {
155                                                 Assert.IsNotNull (ci, messagePrefix + "2");
156                                                 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
157                                                 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
158                                                 ci.Disabled = item.Disable;
159                                         }
160                                         break;
161
162                                 case QueueOperation.Peek:
163                                         ci = (TestCacheItem)queue.Peek ();
164                                         if (item.IsNull)
165                                                 Assert.IsNull (ci, messagePrefix + "1");
166                                         else {
167                                                 Assert.IsNotNull (ci, messagePrefix + "2");
168                                                 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
169                                                 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
170                                         }
171                                         Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
172                                         break;
173
174                                 case QueueOperation.QueueSize:
175                                         Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
176                                         break;
177
178                                 case QueueOperation.Update:
179                                         ci = list [item.ListIndex];
180                                         queue.Update (ci);
181                                         if (item.IsNull)
182                                                 Assert.IsNull (ci, messagePrefix + "1");
183                                         else {
184                                                 Assert.IsNotNull (ci, messagePrefix + "2");
185                                                 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
186                                                 Assert.AreEqual (item.ExpiresAt, ci.ExpiresAt, messagePrefix + "4");
187                                                 Assert.AreEqual (item.PriorityQueueIndex, ci.PriorityQueueIndex, messagePrefix + "5");
188                                         }
189                                         break;
190                                         
191                                 default:
192                                         Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);
193                                         break;
194                         }
195                 }
196         }
197 }