Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Web / Test / tools / CachePQTestGenerator / Utils.cs
1 //
2 //  Utils.cs
3 //
4 //  Author:
5 //    Marek Habersack <grendel@twistedcode.net>
6 //
7 //  Copyright (c) 2010, Marek Habersack
8 //
9 //  All rights reserved.
10 //
11 //  Redistribution and use in source and binary forms, with or without modification, are permitted
12 //  provided that the following conditions are met:
13 //
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
21 //       permission.
22 //
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.
34 //
35 using System;
36 using System.Collections;
37 using System.Collections.Generic;
38 using System.ComponentModel;
39 using System.IO;
40 using System.Text;
41 using System.Web.Caching;
42 using System.Xml;
43 using System.Xml.XPath;
44
45 using MonoTests.System.Web.Caching;
46
47 namespace Tester
48 {
49         static class Utils
50         {
51                 public static T GetOptionalAttribute <T> (this XPathNavigator nav, string name, out bool found)
52                 {
53                         string value = nav.GetAttribute (name, String.Empty);
54                         if (String.IsNullOrEmpty (value)) {
55                                 found = false;
56                                 return default(T);
57                         }
58                         found = true;
59                         return ConvertAttribute <T> (value);
60                 }
61                 
62                 public static T GetRequiredAttribute <T> (this XPathNavigator nav, string name)
63                 {
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);
68                 }
69
70                 static T ConvertAttribute <T> (string value)
71                 {
72                         if (typeof (T) == typeof (string))
73                                 return (T)((object)value);
74                         
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)));
80                         
81                         TypeConverter cvt = TypeDescriptor.GetConverter (typeof (T));
82                         if (cvt == null)
83                                 throw new InvalidOperationException (String.Format ("Type converter for type '{0}' cannot be found.", typeof (T)));
84
85                         if (!cvt.CanConvertFrom (typeof (string)))
86                                 throw new InvalidOperationException (String.Format ("Conversion from string to type '{0}' is not supported.", typeof (T)));
87                         
88                         return (T) cvt.ConvertFrom (value);
89                 }
90                 
91                 public static void SequenceMethodStart (this StringBuilder sb, string indent, string fileName, int seqNum)
92                 {
93                         sb.Append ("\n" + indent);
94                         sb.AppendFormat ("[Test (Description=\"Generated from sequence file {0}\")]\n", Path.GetFileName (fileName));
95                         sb.Append (indent);
96                         sb.AppendFormat ("public void Sequence_{0:0000} ()\n", seqNum);
97                         sb.Append (indent);
98                         sb.Append ("{\n");
99                 }
100
101                 public static void SequenceMethodEnd (this StringBuilder sb, string indent)
102                 {
103                         sb.Append (indent);
104                         sb.Append ("}\n");
105                 }
106                 
107                 public static void FormatQueueSize (this StreamWriter sw, PriorityQueueState qs)
108                 {
109                         var ti = new CacheItemPriorityQueueTestItem () {
110                                 Operation = QueueOperation.QueueSize,
111                                 QueueCount = qs.Queue.Count
112                         };
113                         sw.WriteLine (ti.Serialize ());
114                 }
115
116                 static int FindQueueIndex (PriorityQueueState qs, CacheItem item)
117                 {
118                         CacheItem ci;
119                         
120                         for (int i = 0; i < qs.Queue.Count; i++) {
121                                 ci = ((IList)qs.Queue) [i] as CacheItem;
122                                 if (ci == null)
123                                         continue;
124
125                                 if (ci.Guid == item.Guid)
126                                         return i;
127                         }
128                         
129                         throw new ApplicationException (String.Format ("Failed to find CacheItem with UUID {0} in the queue.", item.Guid));
130                 }
131                 
132                 public static void FormatUpdate (this StreamWriter sw, PriorityQueueState qs, List <CacheItem> list, CacheItem updatedItem, int index)
133                 {
134                         CacheItem item = list [index];
135                         item.ExpiresAt = updatedItem.ExpiresAt;
136                         int qidx = FindQueueIndex (qs, item);
137                         qs.Update (qidx);
138                         
139                         var ti = new CacheItemPriorityQueueTestItem () {
140                                 Operation = QueueOperation.Update,
141                                 QueueCount = qs.Queue.Count,
142                                 OperationCount = qs.UpdateCount,
143                                 ListIndex = index,
144                                 ExpiresAt = updatedItem.ExpiresAt,
145                                 PriorityQueueIndex = FindQueueIndex (qs, item),
146                                 Guid = updatedItem.Guid != null ? updatedItem.Guid.ToString () : null
147                         };
148                         sw.WriteLine (ti.Serialize ());
149                         qs.UpdateCount++;
150                 }
151                 
152                 public static void FormatDisableItem (this StreamWriter sw, PriorityQueueState qs, List <CacheItem> list, int index)
153                 {
154                         CacheItem item = list [index];
155                         var ti = new CacheItemPriorityQueueTestItem () {
156                                 Operation = QueueOperation.Disable,
157                                 QueueCount = qs.Queue.Count,
158                                 ListIndex = index,
159                                 PriorityQueueIndex = item.PriorityQueueIndex,
160                                 OperationCount = qs.DisableCount
161                         };
162                         if (item == null)
163                                 ti.IsNull = true;
164                         else {
165                                 ti.Guid = item.Guid.ToString ();
166                                 ti.IsDisabled = item.Disabled;
167                                 ti.Disable = true;
168                         }
169                         sw.WriteLine (ti.Serialize ());
170                         item.Disabled = true;
171                         qs.DisableCount++;
172                 }
173                 
174                 public static void FormatDequeue (this StreamWriter sw, PriorityQueueState qs)
175                 {
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
182                         };
183                         if (item != null) {
184                                 ti.Guid = item.Guid.ToString ();
185                                 ti.IsDisabled = item.Disabled;
186                         } else
187                                 ti.IsNull = true;
188                         
189                         sw.WriteLine (ti.Serialize ());
190                         qs.DequeueCount++;
191                 }
192
193                 public static void FormatPeek (this StreamWriter sw, PriorityQueueState qs)
194                 {
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
201                         };
202                         if (item != null) {
203                                 ti.Guid = item.Guid.ToString ();
204                                 ti.IsDisabled = item.Disabled;
205                         } else
206                                 ti.IsNull = true;
207                         
208                         sw.WriteLine (ti.Serialize ());
209                         qs.PeekCount++;
210                 }
211                 
212                 public static void FormatEnqueue (this StreamWriter sw, PriorityQueueState qs, List <CacheItem> list, int index)
213                 {
214                         CacheItem item = list [index];
215                         qs.Enqueue (item);
216
217                         var ti = new CacheItemPriorityQueueTestItem () {
218                                 Operation = QueueOperation.Enqueue,
219                                 QueueCount = qs.Queue.Count,
220                                 ListIndex = index,
221                                 Guid = qs.Peek ().Guid.ToString (),
222                                 OperationCount = qs.EnqueueCount,
223                                 PriorityQueueIndex = item.PriorityQueueIndex
224                         };
225                         
226                         sw.WriteLine (ti.Serialize ());
227                         qs.EnqueueCount++;
228                 }
229                 
230                 public static void FormatList (this StreamWriter sw, List <CacheItem> list)
231                 {
232                         if (list == null || list.Count == 0) {
233                                 sw.WriteLine ("# No CacheItems found!");
234                                 return;
235                         }
236
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");
239
240                         foreach (CacheItem ci in list)
241                                 CreateNewCacheItemInstanceCode (sw, ci);
242                 }
243
244                 static void CreateNewCacheItemInstanceCode (StreamWriter sw, CacheItem item)
245                 {
246                         sw.Write ("{0},", item.Key.Replace ("\n", "\\n").Replace ("\r", "\\r").Replace (",", "&comma;"));
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);
255                         sw.WriteLine ();
256                 }
257         }
258 }