2010-02-03 Marek Habersack <mhabersack@novell.com>
[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.Generic;
37 using System.ComponentModel;
38 using System.IO;
39 using System.Text;
40 using System.Web.Caching;
41 using System.Xml;
42 using System.Xml.XPath;
43
44 namespace Tester
45 {
46         static class Utils
47         {
48                 public static T GetRequiredAttribute <T> (this XPathNavigator nav, string name)
49                 {
50                         string value = nav.GetAttribute (name, String.Empty);
51                         if (String.IsNullOrEmpty (value))
52                                 throw new InvalidOperationException (String.Format ("Required attribute '{0}' missing.", name));
53
54                         if (typeof (T) == typeof (string))
55                                 return (T)((object)value);
56                         
57                         // Special cases because we use ticks
58                         if (typeof (T) == typeof (DateTime))
59                                 return (T)((object) new DateTime (Int64.Parse (value)));
60                         else if (typeof (T) == typeof (TimeSpan))
61                                 return (T)((object) new TimeSpan (Int64.Parse (value)));
62                         
63                         TypeConverter cvt = TypeDescriptor.GetConverter (typeof (T));
64                         if (cvt == null)
65                                 throw new InvalidOperationException (String.Format ("Type converter for type '{0}' cannot be found.", typeof (T)));
66
67                         if (!cvt.CanConvertFrom (typeof (string)))
68                                 throw new InvalidOperationException (String.Format ("Conversion from string to type '{0}' is not supported.", typeof (T)));
69                         
70                         return (T) cvt.ConvertFrom (value);
71                 }
72                 
73                 public static void SequenceMethodStart (this StringBuilder sb, string indent, string fileName, int seqNum)
74                 {
75                         sb.Append ("\n" + indent);
76                         sb.AppendFormat ("[Test (Description=\"Generated from sequence file {0}\")]\n", Path.GetFileName (fileName));
77                         sb.Append (indent);
78                         sb.AppendFormat ("public void Sequence_{0:0000} ()\n", seqNum);
79                         sb.Append (indent);
80                         sb.Append ("{\n");
81                 }
82
83                 public static void SequenceMethodEnd (this StringBuilder sb, string indent)
84                 {
85                         sb.Append (indent);
86                         sb.Append ("}\n");
87                 }
88                 
89                 public static void FormatQueueSize (this StringBuilder sb, string indent, PriorityQueueState qs)
90                 {
91                         sb.Append (indent);
92                         sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Count, \"Queue size after sequence\");\n\n",
93                                          qs.Queue.Count, qs.QueueName);
94                 }
95                 
96                 public static void FormatDisableItem (this StringBuilder sb, string indent, PriorityQueueState qs, List <CacheItem> list, int index)
97                 {
98                         CacheItem item = list [index];
99                         sb.Append (indent);
100                         sb.AppendFormat ("{0} = {1} [{2}];\n", qs.ItemName, qs.ListName, index);
101                         sb.Append (indent);
102                         
103                         if (item == null) {
104                                 sb.AppendFormat ("Assert.IsNull ({0}, \"Disable-{1:0000}-1\");\n",
105                                                  qs.ItemName, qs.DisableCount);
106                                 return;
107                         }
108                         
109                         sb.AppendFormat ("Assert.IsNotNull ({0}, \"Disable-{1:0000}-1\");\n",
110                                          qs.ItemName, qs.DisableCount);
111
112                         sb.Append (indent);
113                         sb.AppendFormat ("Assert.AreEqual (\"{0}\", {1}.Guid.ToString(), \"Disable-{2:0000}-3\");\n",
114                                          item.Guid.ToString (), qs.ItemName, qs.DisableCount);
115                         
116                         sb.Append (indent);
117                         sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Disabled, \"Disable-{2:0000}-3\");\n",
118                                          item.Disabled.ToString ().ToLowerInvariant (),
119                                          qs.ItemName, qs.DisableCount);
120                         sb.Append (indent);
121                         sb.AppendFormat ("{0}.Disabled = true;\n\n", qs.ItemName);
122
123                         item.Disabled = true;
124                         
125                         qs.DisableCount++;
126                 }
127                 
128                 public static void FormatDequeue (this StringBuilder sb, string indent, PriorityQueueState qs)
129                 {
130                         CacheItem item = qs.Dequeue ();
131
132                         sb.Append (indent);
133                         sb.AppendFormat ("{0} = {1}.Dequeue ();\n", qs.ItemName, qs.QueueName);
134                         sb.Append (indent);
135                         if (item != null)
136                                 sb.AppendFormat ("Assert.IsNotNull ({0}, \"Dequeue-{1:0000}-1\");\n", qs.ItemName, qs.DequeueCount);
137                         else
138                                 sb.AppendFormat ("Assert.IsNull ({0}, \"Dequeue-{1:0000}-1\");\n", qs.ItemName, qs.DequeueCount);
139                         
140                         sb.Append (indent);
141                         sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Count, \"Dequeue-{2:0000}-2\");\n",
142                                          qs.Queue.Count, qs.QueueName, qs.DequeueCount);
143
144                         if (item != null) {
145                                 sb.Append (indent);
146                                 sb.AppendFormat ("Assert.AreEqual (\"{0}\", {1}.Guid.ToString (), \"Dequeue-{2:0000}-3\");\n",
147                                                  item.Guid.ToString (), qs.ItemName, qs.DequeueCount);
148                                 sb.Append (indent);
149                                 sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Disabled, \"Dequeue-{2:0000}-4\");\n\n",
150                                                  item.Disabled.ToString ().ToLowerInvariant (), qs.ItemName, qs.DequeueCount);
151                         }
152                         
153                         qs.DequeueCount++;
154                 }
155
156                 public static void FormatPeek (this StringBuilder sb, string indent, PriorityQueueState qs)
157                 {
158                         CacheItem item = qs.Peek ();
159
160                         sb.Append (indent);
161                         sb.AppendFormat ("{0} = {1}.Peek ();\n", qs.ItemName, qs.QueueName);
162                         sb.Append (indent);
163                         if (item != null)
164                                 sb.AppendFormat ("Assert.IsNotNull ({0}, \"Peek-{1:0000}-1\");\n", qs.ItemName, qs.PeekCount);
165                         else
166                                 sb.AppendFormat ("Assert.IsNull ({0}, \"Peek-{1:0000}-1\");\n", qs.ItemName, qs.PeekCount);
167
168                         sb.Append (indent);
169                         sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Count, \"Peek-{2:0000}-2\");\n", qs.Queue.Count, qs.QueueName, qs.PeekCount);
170
171                         if (item != null) {
172                                 sb.Append (indent);
173                                 sb.AppendFormat ("Assert.AreEqual (\"{0}\", {1}.Guid.ToString (), \"Peek-{2:0000}-3\");\n",
174                                                  item.Guid.ToString (), qs.ItemName, qs.PeekCount);
175                                 sb.Append (indent);
176                                 sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Disabled, \"Peek-{2:0000}-4\");\n\n",
177                                                  item.Disabled.ToString ().ToLowerInvariant (), qs.ItemName, qs.PeekCount);
178                         }
179
180                         qs.PeekCount++;
181                 }
182                 
183                 public static void FormatEnqueue (this StringBuilder sb, string indent, PriorityQueueState qs, List <CacheItem> list, int index)
184                 {
185                         qs.Enqueue (list [index]);
186                         sb.Append (indent);
187                         sb.AppendFormat ("{0}.Enqueue ({1} [{2}]);\n", qs.QueueName, qs.ListName, index);
188                         sb.Append (indent);
189                         sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Count, \"Enqueue-{2:0000}-1\");\n",
190                                          qs.Queue.Count, qs.QueueName, qs.EnqueueCount);
191                         sb.Append (indent);
192                         sb.AppendFormat ("Assert.AreEqual (\"{0}\", {1}.Peek ().Guid.ToString(), \"Enqueue-{2:0000}-2\");\n\n",
193                                          qs.Peek ().Guid.ToString (), qs.QueueName, qs.EnqueueCount);
194
195                         qs.EnqueueCount++;
196                 }
197
198                 public static void FormatList (this StringBuilder sb, string indent, string listName, List <CacheItem> list)
199                 {
200                         if (list == null || list.Count == 0) {
201                                 sb.AppendFormat (indent + "var {0} = new List <CacheItem> ();\n", listName);
202                                 return;
203                         }
204
205                         sb.AppendFormat (indent + "var {0} = new List <CacheItem> {{\n", listName);
206
207                         foreach (CacheItem ci in list)
208                                 CreateNewCacheItemInstanceCode (indent + "\t", sb, ci);
209                         sb.Append (indent + "};\n");
210                 }
211
212                 static void CreateNewCacheItemInstanceCode (string indent, StringBuilder sb, CacheItem item)
213                 {
214                         sb.Append (indent + "new CacheItem {");
215                         sb.AppendFormat ("Key = \"{0}\", ", item.Key.Replace ("\n", "\\n").Replace ("\r", "\\r"));
216                         sb.AppendFormat ("AbsoluteExpiration = DateTime.Parse (\"{0}\"), ", item.AbsoluteExpiration.ToString ());
217                         sb.AppendFormat ("SlidingExpiration = TimeSpan.Parse (\"{0}\"), ", item.SlidingExpiration.ToString ());
218                         sb.AppendFormat ("Priority = CacheItemPriority.{0}, ", item.Priority);
219                         sb.AppendFormat ("LastChange = DateTime.Parse (\"{0}\"), ", item.LastChange.ToString ());
220                         sb.AppendFormat ("ExpiresAt = {0}, ", item.ExpiresAt);
221                         sb.AppendFormat ("Disabled = {0}, ", item.Disabled.ToString ().ToLowerInvariant ());
222                         sb.AppendFormat ("Guid = new Guid (\"{0}\")}}, \n", item.Guid.ToString ());
223                 }
224         }
225 }