123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Net;
- using System.Threading;
- using Salient.SqlServer.Testing;
- namespace CassiniDev.Testing.SqlServer
- {
-
-
-
-
-
-
-
- public partial class CassiniSqlFixture : DatabaseFixture, IDisposable
- {
- private bool _disposed;
- private bool _hostAdded;
- private string _hostname;
- private StreamWriter _input;
- private IPAddress _ipAddress;
- private Thread _outputThread;
- private string _rootUrl;
- private Process _serverProcess;
- public CassiniSqlFixture(string dataSource, string initialCatalog) : base(dataSource, initialCatalog)
- {
- }
- public CassiniSqlFixture(string dataSource, string initialCatalog, string userId, string password) : base(dataSource, initialCatalog, userId, password)
- {
- }
- public CassiniSqlFixture(string connectionString) : base(connectionString)
- {
- }
-
-
-
- public string RootUrl
- {
- get { return _rootUrl; }
- }
-
-
-
-
-
-
- public virtual Uri NormalizeUri(string relativeUrl)
- {
- relativeUrl = relativeUrl.TrimStart(new[] {'/'});
- string rootUrl = _rootUrl;
- if (!rootUrl.EndsWith("/"))
- {
- rootUrl += "/";
- }
- return new Uri(rootUrl + relativeUrl);
- }
-
-
-
-
-
-
-
-
-
- public static ushort GetPort(ushort portRangeStart, ushort portRangeEnd, IPAddress ipAddress)
- {
- ushort port = ServiceFactory.Rules.GetAvailablePort(portRangeStart, portRangeEnd, ipAddress, true);
- if (port == 0)
- {
- throw new InvalidOperationException("Port is in use");
- }
- return port;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public virtual void StartServer(string applicationPath, IPAddress ipAddress, ushort port, string virtualPath,
- string hostname, bool addHostsEntry, int waitForPort, int timeOut)
- {
- _hostAdded = addHostsEntry;
- _hostname = hostname;
- _ipAddress = ipAddress;
-
- if (string.IsNullOrEmpty(virtualPath))
- {
- virtualPath = "/";
- }
- if (!virtualPath.StartsWith("/"))
- {
- virtualPath = "/" + virtualPath;
- }
- if (_serverProcess != null)
- {
- throw new InvalidOperationException("Server is running");
- }
- if (addHostsEntry)
- {
- if (string.IsNullOrEmpty(hostname))
- {
- throw new InvalidOperationException("Hostname is missing");
- }
- ServiceFactory.Rules.AddHostEntry(_ipAddress.ToString(), _hostname);
- }
-
- string commandLine = (new CommandLineArguments
- {
- Port = port,
- Path = string.Format("\"{0}\"", Path.GetFullPath(applicationPath).Trim('\"').TrimEnd('\\')),
- HostName = hostname,
- IPAddress = ipAddress.ToString(),
- VirtualPath = string.Format("\"{0}\"",virtualPath),
- TimeOut = timeOut,
- WaitForPort = waitForPort,
- IPMode=IPMode.Specific,
- PortMode=PortMode.Specific
- }).ToString();
- _serverProcess = new Process();
- _serverProcess.StartInfo = new ProcessStartInfo
- {
- UseShellExecute = false,
- ErrorDialog = false,
- CreateNoWindow = true,
- RedirectStandardOutput = true,
- RedirectStandardInput = true,
- FileName = "CassiniDev-console.exe",
- Arguments = commandLine,
- WorkingDirectory = Environment.CurrentDirectory
- };
-
-
- string line = null;
- _serverProcess.Start();
- _outputThread = new Thread(() =>
- {
- string l = _serverProcess.StandardOutput.ReadLine();
- while (l != null)
- {
- if (l.StartsWith("started:") || l.StartsWith("error:"))
- {
- line = l;
- }
- l = _serverProcess.StandardOutput.ReadLine();
- }
- });
- _outputThread.Start();
-
- _input = _serverProcess.StandardInput;
-
- while (line == null)
- {
- Thread.Sleep(10);
- }
- if (!line.StartsWith("started:"))
- {
- throw new Exception(string.Format("Could not start server: {0}", line));
- }
-
- _rootUrl = line.Substring(line.IndexOf(':') + 1);
- }
-
-
-
- public virtual void StopServer()
- {
- StopServer(100);
- }
-
-
-
- public virtual void StopServer(int delay)
- {
- Thread.Sleep(delay);
- if (_serverProcess != null)
- {
- try
- {
- _input.WriteLine();
- _serverProcess.WaitForExit(10000);
- if (_hostAdded)
- {
- ServiceFactory.Rules.RemoveHostEntry(_ipAddress.ToString(), _hostname);
- }
- Thread.Sleep(10);
- }
- catch
- {
- }
- finally
- {
- _serverProcess.Dispose();
- _serverProcess = null;
- }
- }
- }
- #region IDisposable
- public void Dispose()
- {
- if (!_disposed)
- {
- if (_serverProcess != null)
- {
- StopServer();
- }
- }
- _disposed = true;
- GC.SuppressFinalize(this);
- }
- ~CassiniSqlFixture()
- {
- Dispose();
- }
- #endregion
- }
- }
|