5 // Marek Habersack <grendel@twistedcode.net>
7 // Copyright (c) 2010, Marek Habersack
9 // All rights reserved.
11 // Redistribution and use in source and binary forms, with or without modification, are permitted
12 // provided that the following conditions are met:
14 // * Redistributions of source code must retain the above copyright notice, this list of
15 // conditions and the following disclaimer.
16 // * Redistributions in binary form must reproduce the above copyright notice, this list of
17 // conditions and the following disclaimer in the documentation and/or other materials
18 // provided with the distribution.
19 // * Neither the name of Marek Habersack nor the names of its contributors may be used to
20 // endorse or promote products derived from this software without specific prior written
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 using System.Collections;
37 using System.Collections.Generic;
38 using System.ComponentModel;
41 using System.Web.Caching;
43 using System.Xml.XPath;
45 using MonoTests.System.Web.Caching;
51 public static T GetOptionalAttribute <T> (this XPathNavigator nav, string name, out bool found)
53 string value = nav.GetAttribute (name, String.Empty);
54 if (String.IsNullOrEmpty (value)) {
59 return ConvertAttribute <T> (value);
62 public static T GetRequiredAttribute <T> (this XPathNavigator nav, string name)
64 string value = nav.GetAttribute (name, String.Empty);
65 if (String.IsNullOrEmpty (value))
66 throw new InvalidOperationException (String.Format ("Required attribute '{0}' missing.", name));
67 return ConvertAttribute <T> (value);
70 static T ConvertAttribute <T> (string value)
72 if (typeof (T) == typeof (string))
73 return (T)((object)value);
75 // Special cases because we use ticks
76 if (typeof (T) == typeof (DateTime))
77 return (T)((object) new DateTime (Int64.Parse (value)));
78 else if (typeof (T) == typeof (TimeSpan))
79 return (T)((object) new TimeSpan (Int64.Parse (value)));
81 TypeConverter cvt = TypeDescriptor.GetConverter (typeof (T));
83 throw new InvalidOperationException (String.Format ("Type converter for type '{0}' cannot be found.", typeof (T)));
85 if (!cvt.CanConvertFrom (typeof (string)))
86 throw new InvalidOperationException (String.Format ("Conversion from string to type '{0}' is not supported.", typeof (T)));
88 return (T) cvt.ConvertFrom (value);
91 public static void SequenceMethodStart (this StringBuilder sb, string indent, string fileName, int seqNum)
93 sb.Append ("\n" + indent);
94 sb.AppendFormat ("[Test (Description=\"Generated from sequence file {0}\")]\n", Path.GetFileName (fileName));
96 sb.AppendFormat ("public void Sequence_{0:0000} ()\n", seqNum);
101 public static void SequenceMethodEnd (this StringBuilder sb, string indent)
107 public static void FormatQueueSize (this StreamWriter sw, PriorityQueueState qs)
109 var ti = new CacheItemPriorityQueueTestItem () {
110 Operation = QueueOperation.QueueSize,
111 QueueCount = qs.Queue.Count
113 sw.WriteLine (ti.Serialize ());
116 static int FindQueueIndex (PriorityQueueState qs, CacheItem item)
120 for (int i = 0; i < qs.Queue.Count; i++) {
121 ci = ((IList)qs.Queue) [i] as CacheItem;
125 if (ci.Guid == item.Guid)
129 throw new ApplicationException (String.Format ("Failed to find CacheItem with UUID {0} in the queue.", item.Guid));
132 public static void FormatUpdate (this StreamWriter sw, PriorityQueueState qs, List <CacheItem> list, CacheItem updatedItem, int index)
134 CacheItem item = list [index];
135 item.ExpiresAt = updatedItem.ExpiresAt;
136 int qidx = FindQueueIndex (qs, item);
139 var ti = new CacheItemPriorityQueueTestItem () {
140 Operation = QueueOperation.Update,
141 QueueCount = qs.Queue.Count,
142 OperationCount = qs.UpdateCount,
144 ExpiresAt = updatedItem.ExpiresAt,
145 PriorityQueueIndex = FindQueueIndex (qs, item),
146 Guid = updatedItem.Guid != null ? updatedItem.Guid.ToString () : null
148 sw.WriteLine (ti.Serialize ());
152 public static void FormatDisableItem (this StreamWriter sw, PriorityQueueState qs, List <CacheItem> list, int index)
154 CacheItem item = list [index];
155 var ti = new CacheItemPriorityQueueTestItem () {
156 Operation = QueueOperation.Disable,
157 QueueCount = qs.Queue.Count,
159 PriorityQueueIndex = item.PriorityQueueIndex,
160 OperationCount = qs.DisableCount
165 ti.Guid = item.Guid.ToString ();
166 ti.IsDisabled = item.Disabled;
169 sw.WriteLine (ti.Serialize ());
170 item.Disabled = true;
174 public static void FormatDequeue (this StreamWriter sw, PriorityQueueState qs)
176 CacheItem item = qs.Dequeue ();
177 var ti = new CacheItemPriorityQueueTestItem () {
178 Operation = QueueOperation.Dequeue,
179 QueueCount = qs.Queue.Count,
180 OperationCount = qs.DequeueCount,
181 PriorityQueueIndex = item.PriorityQueueIndex
184 ti.Guid = item.Guid.ToString ();
185 ti.IsDisabled = item.Disabled;
189 sw.WriteLine (ti.Serialize ());
193 public static void FormatPeek (this StreamWriter sw, PriorityQueueState qs)
195 CacheItem item = qs.Peek ();
196 var ti = new CacheItemPriorityQueueTestItem () {
197 Operation = QueueOperation.Peek,
198 QueueCount = qs.Queue.Count,
199 OperationCount = qs.PeekCount,
200 PriorityQueueIndex = item.PriorityQueueIndex
203 ti.Guid = item.Guid.ToString ();
204 ti.IsDisabled = item.Disabled;
208 sw.WriteLine (ti.Serialize ());
212 public static void FormatEnqueue (this StreamWriter sw, PriorityQueueState qs, List <CacheItem> list, int index)
214 CacheItem item = list [index];
217 var ti = new CacheItemPriorityQueueTestItem () {
218 Operation = QueueOperation.Enqueue,
219 QueueCount = qs.Queue.Count,
221 Guid = qs.Peek ().Guid.ToString (),
222 OperationCount = qs.EnqueueCount,
223 PriorityQueueIndex = item.PriorityQueueIndex
226 sw.WriteLine (ti.Serialize ());
230 public static void FormatList (this StreamWriter sw, List <CacheItem> list)
232 if (list == null || list.Count == 0) {
233 sw.WriteLine ("# No CacheItems found!");
237 sw.WriteLine ("# Each row contains TestCacheItem fields, one item per line, in the following order:");
238 sw.WriteLine ("# Key, AbsoluteExpiration, SlidingExpiration, Priority, LastChange, ExpiresAt, Disabled, Guid, PriorityQueueIndex");
240 foreach (CacheItem ci in list)
241 CreateNewCacheItemInstanceCode (sw, ci);
244 static void CreateNewCacheItemInstanceCode (StreamWriter sw, CacheItem item)
246 sw.Write ("{0},", item.Key.Replace ("\n", "\\n").Replace ("\r", "\\r").Replace (",", ","));
247 sw.Write ("{0},", item.AbsoluteExpiration.Ticks);
248 sw.Write ("{0},", item.SlidingExpiration.Ticks);
249 sw.Write ("{0},", (int)item.Priority);
250 sw.Write ("{0},", item.LastChange.Ticks);
251 sw.Write ("{0},", item.ExpiresAt);
252 sw.Write ("{0},", item.Disabled.ToString ().ToLowerInvariant ());
253 sw.Write ("{0},", item.Guid.ToString ());
254 sw.Write ("{0}", item.PriorityQueueIndex);