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 ()
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);
100 void RunTest (string testsFileName, string listFileName)
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);
108 using (var sr = new StreamReader (listPath, Encoding.UTF8)) {
109 while ((line = sr.ReadLine ()) != null) {
112 cacheItems.Add (new TestCacheItem (line));
116 using (var sr = new StreamReader (testsPath, Encoding.UTF8)) {
118 while ((line = sr.ReadLine ()) != null) {
121 RunItem (new CacheItemPriorityQueueTestItem (line), queue, cacheItems, i++);
126 void RunItem (CacheItemPriorityQueueTestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list, int testNum)
129 string messagePrefix = String.Format ("{0}-{1:00000}-{2:00000}-", item.Operation, item.OperationCount, testNum);
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");
138 case QueueOperation.Dequeue:
139 ci = (TestCacheItem)queue.Dequeue ();
141 Assert.IsNull (ci, messagePrefix + "1");
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");
147 Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
150 case QueueOperation.Disable:
151 ci = list [item.ListIndex];
153 Assert.IsNull (ci, messagePrefix + "1");
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;
162 case QueueOperation.Peek:
163 ci = (TestCacheItem)queue.Peek ();
165 Assert.IsNull (ci, messagePrefix + "1");
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");
171 Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
174 case QueueOperation.QueueSize:
175 Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
178 case QueueOperation.Update:
179 ci = list [item.ListIndex];
182 Assert.IsNull (ci, messagePrefix + "1");
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");
192 Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);