New test.
[mono.git] / mcs / class / System.Web / Test / mainsoft / MainsoftWebTest / Harness.cs
1 //
2 // Authors:
3 //   Rafael Mizrahi   <rafim@mainsoft.com>
4 //   Erez Lotan       <erezl@mainsoft.com>
5 //   Vladimir Krasnov <vladimirk@mainsoft.com>
6 //   
7 // 
8 // Copyright (c) 2002-2005 Mainsoft Corporation.
9 // 
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.Configuration;
31 using System.Xml;
32 using System.Collections;
33 using System.Net;
34 using System.IO;
35 #if NUNIT
36 using NUnit.Framework;
37 using NUnit.Core;
38 #endif
39
40 namespace MonoTests.stand_alone.WebHarness
41 {
42         public class Harness
43         {
44                 static string _ignoreListFile = "";
45                 static string _catalogFile = "";
46                 static string _outputPath = "";
47                 static string _baseUrlExp = "";
48                 static string _baseUrlTst = "";
49                 static bool _disableAlmost = false;
50                 static bool _runExcluded = false;
51
52                 static void Main(string[] args)
53                 {
54                         ParseCommandLineAgrs(args);
55
56                         if (_baseUrlExp != "")
57                         {
58                                 //baseUrl = "http://localhost:80/System_Web_dll";
59                                 CreateExpectedResults(_baseUrlExp);
60                         }
61
62                         if (_baseUrlTst != "")
63                         {
64                                 //baseUrl = "http://localhost:8080/System_Web_dll";
65                                 RunTests(_baseUrlTst);
66                         }
67                 }
68
69 #if NUNIT
70                 [Suite]
71                 static public TestSuite Suite 
72                 {
73                         get 
74                         {
75                                 ParseAppConfigFile();
76
77                                 TestSuite suite = new TestSuite ("SystemWebTests");
78                                 TestsCatalog tc = new TestsCatalog();
79                                 foreach (TestInfo ti in tc)
80                                 {
81                                         suite.Add(new SingleWebTest(ti, _baseUrlTst));
82                                 }
83                                 
84                                 return suite;
85                         }
86                 }
87 #endif
88
89                 #region "Cmd line"
90
91                 static string GetParameterByName(string name, string[] args)
92                 {
93                         int i = Array.IndexOf(args, name);
94                         if (i >= 0)
95                         {
96                                 if (i < args.Length - 1)
97                                 {
98                                         return args[i+1];
99                                 }
100                         }
101                         return "";
102                 }
103
104                 static bool IsParameterSet(string name, string[] args)
105                 {
106                         int i = Array.IndexOf(args, name);
107                         return (i >= 0);
108                 }
109
110                 static void ParseAppConfigFile()
111                 {
112                         _disableAlmost = Convert.ToBoolean(ConfigurationSettings.AppSettings["DisableAlmost"]);
113                         _runExcluded = Convert.ToBoolean(ConfigurationSettings.AppSettings["RunExcluded"]);
114                         _ignoreListFile = ConfigurationSettings.AppSettings["AlmostList"];
115                         _catalogFile = ConfigurationSettings.AppSettings["TestCatalog"];
116                         _outputPath = ConfigurationSettings.AppSettings["OutputDir"];
117                         if ((!_outputPath.EndsWith("\\")) && (!_outputPath.EndsWith("/")))
118                         {
119                                 _outputPath += Path.DirectorySeparatorChar;
120                         }
121
122                         _baseUrlExp = ConfigurationSettings.AppSettings["ExpResBaseUrl"];
123                         _baseUrlTst = ConfigurationSettings.AppSettings["TestBaseUrl"];
124                 }
125
126                 static void ParseCommandLineAgrs(string [] args)
127                 {
128                         _disableAlmost = IsParameterSet("-na", args);
129                         _runExcluded = IsParameterSet("-x", args);
130                         
131                         // specifies the almost config xml file
132                         // default is almost_config.xml in current folder
133                         _ignoreListFile = GetParameterByName("-i", args);
134                         if (_ignoreListFile == "")
135                         {
136                                 _ignoreListFile = "almost_config.xml";
137                         }
138                         
139                         // specifies tests catalog xml file
140                         // default is test_catalog.xml in current folder
141                         _catalogFile = GetParameterByName("-c", args);
142                         if (_catalogFile == "")
143                         {
144                                 _catalogFile = "test_catalog.xml";
145                         }
146                         
147                         // specifies the folder where expected results will be stored
148                         // by default is current folder
149                         _outputPath = GetParameterByName("-o", args);
150                         if (_outputPath != "")
151                         {
152                                 if ((!_outputPath.EndsWith("\\")) && (!_outputPath.EndsWith("/")))
153                                 {
154                                         _outputPath += Path.DirectorySeparatorChar;
155                                 }
156                         }
157
158                         // specifies the base url for all tests
159                         // no default value
160                         _baseUrlExp = GetParameterByName("-e", args);
161                         _baseUrlTst = GetParameterByName("-t", args);
162                 }
163                 #endregion
164
165                 #region "Tests run routines"
166
167                 static void CreateExpectedResults(string baseUrl)
168                 {
169                         TestsCatalog tc = new TestsCatalog(_catalogFile, _runExcluded);
170                         HtmlDiff wt = new HtmlDiff();
171                         wt.TestsBaseUrl = baseUrl;
172                         wt.IgnoreListFile = _ignoreListFile;
173
174                         if ((_outputPath != "") && (!Directory.Exists(_outputPath)))
175                         {
176                                 Directory.CreateDirectory(_outputPath);
177                         }
178
179                         Console.WriteLine("Running expected results...");
180                         foreach (TestInfo ti in tc)
181                         {
182                                 Console.WriteLine("Running...  " + ti.Url);
183                                 XmlDocument d = wt.GetTestXml( ti );
184                                 d.Save(_outputPath + ti.Url.Replace("/", "_") + ".xml");
185                         }
186                 }
187
188                 static void RunTests(string baseUrl)
189                 {
190                         TestsCatalog tc = new TestsCatalog(_catalogFile, _runExcluded);
191                         foreach (TestInfo ti in tc)
192                         {
193                                 try {
194                                         RunSingleTest(baseUrl, ti);
195                                 }
196                                 catch (Exception e) {
197                                         Console.WriteLine(e.Message);
198                                 }
199                         }
200                 }
201
202                 public static bool RunSingleTest(string baseUrl, TestInfo ti)
203                 {
204                         HtmlDiff wt = new HtmlDiff();
205                         wt.TestsBaseUrl = baseUrl;
206                         wt.IgnoreListFile = _ignoreListFile;
207
208                         XmlDocument d1 = new XmlDocument();
209                         d1.Load(_outputPath + ti.Url.Replace("/", "_") + ".xml");
210                         
211                         XmlDocument d2 = wt.GetTestXml( ti );
212                         bool fp = wt.XmlCompare(d1, d2, _disableAlmost);
213                         if (fp == false) {
214                                 throw new Exception("Url: " + ti.Url + "\nCompare failed:\n" + wt.CompareStatus + "\n");
215                         }
216                         return fp;
217                 }
218
219                 #endregion
220         }
221
222         #region "NUnit"
223
224 #if NUNIT
225         public class SingleWebTest : NUnit.Core.TestCase 
226         {
227                 TestInfo _testInfo = null;
228                 string _baseUrl = "";
229                 public SingleWebTest (TestInfo testInfo, string baseUrl) : base (null, testInfo.Url) 
230                 {
231                         _testInfo = testInfo;
232                         _baseUrl = baseUrl;
233                 }
234
235                 public override void Run (TestCaseResult res) 
236                 {
237                         try
238                         {
239                                 Harness.RunSingleTest(_baseUrl, _testInfo);
240                         }
241                         catch (Exception e)
242                         {
243                                 res.Failure(e.Message, null);
244                                 return;
245                         }
246                         res.Success();
247                 }
248         }
249 #endif
250
251         #endregion
252
253 }