2 // Marek Habersack <mhabersack@novell.com>
4 // Copyright (C) 2010 Novell Inc. http://novell.com
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
28 using System.Collections.Generic;
29 using System.ComponentModel;
31 using System.Reflection;
33 using System.Web.Caching;
35 using NUnit.Framework;
37 namespace MonoTests.System.Web.Caching
40 public partial class CacheItemPriorityQueueTest
42 const string DATA_DIR = "CacheItemPriorityQueueTestData";
43 static readonly string dataDir;
45 sealed class TestCacheItem : CacheItem
49 public TestCacheItem (string dataLine)
51 string[] data = dataLine.Split (',');
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);
65 PriorityQueueIndex = -1;
68 public override string ToString ()
70 return String.Format ("CacheItem [{0}]\n[{1}][{2}][{3}]", this.Guid, Key, Disabled, ExpiresAt > 0 ? new DateTime (ExpiresAt).ToString () : "0");
73 T LoadField <T> (int index, string[] data)
75 if (data == null || data.Length <= index)
76 throw new ArgumentOutOfRangeException ("index");
78 string s = data [index];
79 if (String.IsNullOrEmpty (s))
82 TypeConverter cvt = TypeDescriptor.GetConverter (typeof (T));
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));
88 return (T)cvt.ConvertFromString (s);
92 static CacheItemPriorityQueueTest ()
97 Path.Combine (Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location), "Test"),
98 "System.Web.Caching"),
102 void RunTest (string testsFileName, string listFileName)
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);
110 using (var sr = new StreamReader (listPath, Encoding.UTF8)) {
111 while ((line = sr.ReadLine ()) != null) {
114 cacheItems.Add (new TestCacheItem (line));
118 using (var sr = new StreamReader (testsPath, Encoding.UTF8)) {
120 while ((line = sr.ReadLine ()) != null) {
123 RunItem (new CacheItemPriorityQueueTestItem (line), queue, cacheItems, i++);
128 void RunItem (CacheItemPriorityQueueTestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list, int testNum)
131 string messagePrefix = String.Format ("{0}-{1:00000}-{2:00000}-", item.Operation, item.OperationCount, testNum);
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");
140 case QueueOperation.Dequeue:
141 ci = (TestCacheItem)queue.Dequeue ();
143 Assert.IsNull (ci, messagePrefix + "1");
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");
149 Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
152 case QueueOperation.Disable:
153 ci = list [item.ListIndex];
155 Assert.IsNull (ci, messagePrefix + "1");
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;
164 case QueueOperation.Peek:
165 ci = (TestCacheItem)queue.Peek ();
167 Assert.IsNull (ci, messagePrefix + "1");
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");
173 Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
176 case QueueOperation.QueueSize:
177 Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
180 case QueueOperation.Update:
181 ci = list [item.ListIndex];
184 Assert.IsNull (ci, messagePrefix + "1");
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");
194 Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);