Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.TestSupport / WebServer.cs
blob796650f9bf84b82d579fa7254cf70275eb078701
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.TestSupport
17 using System;
18 using System.Configuration;
19 using System.IO;
20 using Cassini;
22 /// <summary>
23 /// Manages a <see cref="Cassini.Server"/> instance. This is useful
24 /// to start/stop a lightweight webserver to run acceptance tests.
25 /// </summary>
26 public static class WebServer
28 private const string AppPathWeb = "web.physical.dir";
30 private static string virtualDir = "/";
31 private static int port = 88;
32 private static bool started;
33 private static Cassini.Server server;
35 /// <summary>
36 /// Gets or sets the port to run the server. Defaults to 88.
37 /// </summary>
38 /// <value>The port.</value>
39 public static int Port
41 get { return port; }
42 set { port = value; }
45 /// <summary>
46 /// Gets or sets the virtual dir to be used by the server. Defaults to <c>/</c>
47 /// </summary>
48 /// <value>The virtual dir.</value>
49 public static string VirtualDir
51 get { return virtualDir; }
52 set { virtualDir = value; }
55 /// <summary>
56 /// Gets a value indicating whether this <see cref="WebServer"/> is started.
57 /// </summary>
58 /// <value><c>true</c> if started; otherwise, <c>false</c>.</value>
59 public static bool Started
61 get { return started; }
64 /// <summary>
65 /// Starts the web server. The web project folder is going to be
66 /// extracted from the appSettings.webapp entry (from the configuration file)
67 /// <para>
68 /// If the path is relative, it is going to be converted to an absolute path.
69 /// </para>
70 /// </summary>
71 public static void StartWebServer()
73 string webAppFromConfig = ConfigurationManager.AppSettings[AppPathWeb];
74 string webAppAbsPath = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, webAppFromConfig)).FullName;
76 StartWebServer(webAppAbsPath);
79 /// <summary>
80 /// Starts the web server using the specified web project path. Note
81 /// that the path must be absolute.
82 /// </summary>
83 /// <param name="webApplicationAbsolutePath">The web application absolute path.</param>
84 public static void StartWebServer(string webApplicationAbsolutePath)
86 if (!Directory.Exists(webApplicationAbsolutePath))
88 throw new ApplicationException("Cannot start web server as the path could not be found. " +
89 "Check if the following folder exists: " + webApplicationAbsolutePath);
92 server = new Server(88, VirtualDir, webApplicationAbsolutePath);
93 server.Start();
95 started = true;
98 /// <summary>
99 /// Stops the web server.
100 /// </summary>
101 public static void StopWebServer()
103 if (started)
105 server.Stop();