Ejemplo n.º 1
0
        public void SqlLocalDbInstance_Delete_If_SqlLocalDbApi_AutomaticallyDeleteInstanceFiles_Is_True()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            Helpers.InvokeInNewAppDomain(
                () =>
            {
                SqlLocalDbApi.AutomaticallyDeleteInstanceFiles = true;

                string instanceName = Guid.NewGuid().ToString();

                SqlLocalDbApi.CreateInstance(instanceName);

                Mock <ISqlLocalDbInstance> mock = new Mock <ISqlLocalDbInstance>();
                mock.Setup((p) => p.Name).Returns(instanceName);

                ISqlLocalDbInstanceInfo info = SqlLocalDbApi.GetInstanceInfo(instanceName);
                Assert.IsNotNull(info, "SqlLocalDbApi.GetInstanceInfo() returned null.");
                Assert.IsTrue(info.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");

                ISqlLocalDbInstance instance = mock.Object;

                // Act
                SqlLocalDbInstance.Delete(instance);

                // Assert
                info = SqlLocalDbApi.GetInstanceInfo(instanceName);
                Assert.IsNotNull(info, "SqlLocalDbApi.GetInstanceInfo() returned null.");
                Assert.IsFalse(info.Exists, "The SQL LocalDB instance was not deleted.");

                string path = Path.Combine(SqlLocalDbApi.GetInstancesFolderPath(), instanceName);
                Assert.IsFalse(Directory.Exists(path), "The instance folder was not deleted.");
            });
        }
        /// <summary>
        /// Assets that the specified SQL LocalDB instance name is in the specified state of existence.
        /// </summary>
        /// <param name="instanceName">The instance name to assert for.</param>
        /// <param name="exists">Whether the specified instance name is expected to exist.</param>
        private static void AssertExistence(string instanceName, bool exists)
        {
            ISqlLocalDbInstanceInfo info = SqlLocalDbApi.GetInstanceInfo(instanceName);

            Assert.IsNotNull(info, "SqlLocalDbApi.GetInstanceInfo() returned null.");
            Assert.AreEqual(exists, info.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");
        }
Ejemplo n.º 3
0
 public void Throws_PlatformNotSupportedException()
 {
     // Arrange
     using (var actual = new SqlLocalDbApi(_loggerFactory))
     {
         // Act and Assert
         Assert.Throws <PlatformNotSupportedException>(() => actual.CreateInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.DeleteInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.DeleteUserInstances());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetDefaultInstance());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetInstanceInfo("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetInstanceNames());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetInstances());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetOrCreateInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetVersionInfo("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.InstanceExists("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.LatestVersion);
         Assert.Throws <PlatformNotSupportedException>(() => actual.ShareInstance("name", "sharedName"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.ShareInstance("sid", "name", "sharedName"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.StartInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.StartTracing());
         Assert.Throws <PlatformNotSupportedException>(() => actual.StopInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.StopTracing());
         Assert.Throws <PlatformNotSupportedException>(() => actual.UnshareInstance("name"));
     }
 }
Ejemplo n.º 4
0
        public void SqlLocalDbInstance_Delete_Deletes_Instance()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            string instanceName = Guid.NewGuid().ToString();

            SqlLocalDbApi.CreateInstance(instanceName);

            Mock <ISqlLocalDbInstance> mock = new Mock <ISqlLocalDbInstance>();

            mock.Setup((p) => p.Name).Returns(instanceName);

            ISqlLocalDbInstanceInfo info = SqlLocalDbApi.GetInstanceInfo(instanceName);

            Assert.IsNotNull(info, "SqlLocalDbApi.GetInstanceInfo() returned null.");
            Assert.IsTrue(info.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");

            ISqlLocalDbInstance instance = mock.Object;

            // Act
            SqlLocalDbInstance.Delete(instance);

            // Assert
            info = SqlLocalDbApi.GetInstanceInfo(instanceName);
            Assert.IsNotNull(info, "SqlLocalDbApi.GetInstanceInfo() returned null.");
            Assert.IsFalse(info.Exists, "The SQL LocalDB instance was not deleted.");

            string path = Path.Combine(SqlLocalDbApi.GetInstancesFolderPath(), instanceName);

            Assert.IsTrue(Directory.Exists(path), "The instance folder was deleted.");
            Assert.AreNotEqual(0, Directory.GetFiles(path).Length, "The instance files were deleted.");
        }
Ejemplo n.º 5
0
        public void Can_Get_Instances_From_Names()
        {
            // Arrange
            using (var api = new SqlLocalDbApi(_loggerFactory))
            {
                IReadOnlyList <string> names = api.GetInstanceNames();

                foreach (string name in names)
                {
                    // Act
                    ISqlLocalDbInstanceInfo info = api.GetInstanceInfo(name);

                    // Assert
                    info.ShouldNotBeNull();
                }

                // Arrange
                string instanceName = Guid.NewGuid().ToString();

                // Act
                bool actual = api.InstanceExists(instanceName);

                // Assert
                actual.ShouldBeFalse();
            }
        }
Ejemplo n.º 6
0
        public void Throws_InvalidOperationException_If_SQL_LocalDB_Not_Installed()
        {
            // Arrange
            var options  = new SqlLocalDbOptions();
            var registry = Mock.Of <Interop.IRegistry>();

            using (var actual = new SqlLocalDbApi(options, registry, _loggerFactory))
            {
                // Act and Assert
                Assert.Throws <InvalidOperationException>(() => actual.CreateInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.DeleteInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.DeleteUserInstances());
                Assert.Throws <InvalidOperationException>(() => actual.GetDefaultInstance());
                Assert.Throws <InvalidOperationException>(() => actual.GetInstanceInfo("name"));
                Assert.Throws <InvalidOperationException>(() => actual.GetInstanceNames());
                Assert.Throws <InvalidOperationException>(() => actual.GetInstances());
                Assert.Throws <InvalidOperationException>(() => actual.GetOrCreateInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.GetVersionInfo("name"));
                Assert.Throws <InvalidOperationException>(() => actual.InstanceExists("name"));
                Assert.Throws <InvalidOperationException>(() => actual.LatestVersion);
                Assert.Throws <InvalidOperationException>(() => actual.ShareInstance("name", "sharedName"));
                Assert.Throws <InvalidOperationException>(() => actual.StartInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.StartTracing());
                Assert.Throws <InvalidOperationException>(() => actual.StopInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.StopTracing());
                Assert.Throws <InvalidOperationException>(() => actual.UnshareInstance("name"));
            }
        }
Ejemplo n.º 7
0
        private static void Execute()
        {
            var localInfo = SqlLocalDbApi.GetInstanceInfo(AppConfig.Database);

            if (localInfo.Exists)
            {
                SqlLocalDbApi.StopInstance(localInfo.Name);
                SqlLocalDbApi.DeleteInstance(localInfo.Name, true);
            }
            SqlLocalDbApi.CreateInstance(localInfo.Name);


            using (var memoryStream = new MemoryStream())
            {
                var remote = new DacServices(AppConfig.RemoteConnectionString);
                remote.ProgressChanged += (sender, args) => Console.WriteLine($"remote {args.Status} {args.Message}");
                remote.ExportBacpac(memoryStream, localInfo.Name, DacSchemaModelStorageType.Memory);

                using (var bacPackage = BacPackage.Load(memoryStream, DacSchemaModelStorageType.Memory))
                {
                    var local = new DacServices(AppConfig.LocalConnectionString);
                    local.ProgressChanged += (sender, args) => Console.WriteLine($"local {args.Status} {args.Message}");
                    local.ImportBacpac(bacPackage, localInfo.Name);
                }
            }
        }
Ejemplo n.º 8
0
        private void Initialize(string instanceName, string databaseName)
        {
            this.databaseName = databaseName;
            this.instanceName = instanceName;

            var existing = SqlLocalDbApi.GetInstanceInfo(instanceName);

            if (existing.Exists)
            {
                if (existing.IsRunning)
                {
                    SqlLocalDbApi.StopInstance(instanceName);
                }
                SqlLocalDbApi.DeleteInstance(instanceName);
            }


            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            this.instance = provider.GetOrCreateInstance(instanceName);

            instance.Start();


            var connectionStringBuilder = instance.CreateConnectionStringBuilder();

            using (var conn = new SqlConnection(connectionStringBuilder.ConnectionString))
            {
                var serverConnection = new ServerConnection(conn);

                // By default, LocalDB stores database files in the user's home folder. Messy for temporary test databases.
                // Store them in the user's temp folder instead.
                var testDatabasesDirectory = Path.Combine(Path.GetTempPath(), "TestDatabases");

                if (!Directory.Exists(testDatabasesDirectory))
                {
                    Directory.CreateDirectory(testDatabasesDirectory);
                }

                // Generate a unique name for our mdf file to avoid clashing with other ongoing test runs.
                var databaseFileNameRoot = string.Format("{0}_{1}_{2}", databaseName, DateTime.Now.ToString("yyyyMMdd_HHmmss"), Guid.NewGuid().ToString("N"));

                var mdfFileName = databaseFileNameRoot + ".mdf";
                var ldfFileName = databaseFileNameRoot + "_log.ldf";

                this.mdfFilePath = Path.Combine(testDatabasesDirectory, mdfFileName);
                this.ldfFilePath = Path.Combine(testDatabasesDirectory, ldfFileName);

                var sql = string.Format("CREATE DATABASE {0} ON (NAME = N'{0}', FILENAME = '{1}')", databaseName, this.mdfFilePath);

                Console.WriteLine(string.Format("Creating database {0} at {1}", databaseName, this.mdfFilePath));
                serverConnection.ExecuteNonQuery(sql);
            }

            connectionStringBuilder.InitialCatalog = this.databaseName;
            this.connectionString = connectionStringBuilder.ConnectionString;
        }
Ejemplo n.º 9
0
        private void CreateLocalDb()
        {
            string connection    = System.Configuration.ConfigurationManager.ConnectionStrings["BVCareManager.Properties.Settings.BVCareManagerConnectionString"].ConnectionString;
            string localDbstring = @"(LocalDB)\";

            int pFrom = connection.IndexOf(localDbstring) + localDbstring.Length;
            int pTo   = connection.LastIndexOf(";AttachDbFilename");

            string resultLocalDb = connection.Substring(pFrom, pTo - pFrom);

            #region SqlLocalDb Handle
            var localDb = new SqlLocalDbApi();

            ISqlLocalDbInstanceInfo localDbInstance = localDb.GetInstanceInfo(resultLocalDb);
            string localDbVersion = localDbInstance.LocalDbVersion.ToString().Substring(0, 2);

            if (localDbVersion == "0.")
            {
                try
                {
                    localDb.CreateInstance(resultLocalDb, "12.0");
                }
                catch
                {
                    MessageBox.Show("Phiên bản SQL Server LocalDB hiện tại không phù hợp. Hãy cài đặt phiên bản 12.0 (SQL Server 2014 Express LocalDb)");
                    Environment.Exit(1);
                }
            }
            else if (localDbVersion != "0." && localDbVersion != "12")
            {
                localDb.StopInstance(resultLocalDb);
                localDb.DeleteInstance(resultLocalDb);

                try
                {
                    localDb.CreateInstance(resultLocalDb, "12.0");
                }
                catch
                {
                    MessageBox.Show("Phiên bản SQL Server LocalDB hiện tại không phù hợp. Hãy cài đặt phiên bản 12.0 (SQL Server 2014 Express LocalDb)");
                    Environment.Exit(1);
                }
            }


            localDb.StartInstance(resultLocalDb);
            #endregion
        }
Ejemplo n.º 10
0
        public void SqlLocalDbInstance_Unshare_If_Instance_Is_Started()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();
            Helpers.EnsureUserIsAdmin();

            string instanceName = Guid.NewGuid().ToString();

            SqlLocalDbApi.CreateInstance(instanceName);

            try
            {
                SqlLocalDbInstance target = new SqlLocalDbInstance(instanceName);
                target.Start();

                try
                {
                    string sharedName = Guid.NewGuid().ToString();
                    target.Share(sharedName);

                    ISqlLocalDbInstanceInfo info = SqlLocalDbApi.GetInstanceInfo(instanceName);

                    Assert.IsNotNull(info, "SqlLocalDbApi.GetInstanceInfo() returned null.");
                    Assert.IsTrue(info.IsShared, "ISqlLocalDbInstanceInfo.IsShared is incorrect.");
                    Assert.AreEqual(sharedName, info.SharedName, "ISqlLocalDbInstanceInfo.SharedName is incorrect.");

                    // Act
                    target.Unshare();

                    // Assert
                    info = SqlLocalDbApi.GetInstanceInfo(instanceName);

                    Assert.IsNotNull(info, "SqlLocalDbApi.GetInstanceInfo() returned null.");
                    Assert.IsFalse(info.IsShared, "ISqlLocalDbInstanceInfo.IsShared is incorrect.");
                    Assert.AreEqual(string.Empty, info.SharedName, "ISqlLocalDbInstanceInfo.SharedName is incorrect.");
                }
                finally
                {
                    SqlLocalDbApi.StopInstance(instanceName);
                }
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }
        }
        public void Manager_Shares_And_Unshares_Instance()
        {
            // Act
            string instanceName = Guid.NewGuid().ToString();
            string sharedName   = Guid.NewGuid().ToString();

            using (var api = new SqlLocalDbApi(_loggerFactory))
            {
                api.CreateInstance(instanceName);

                try
                {
                    api.StartInstance(instanceName);

                    try
                    {
                        var instance = api.GetInstanceInfo(instanceName);

                        var manager = new SqlLocalDbInstanceManager(instance, api);

                        // Act
                        manager.Share(sharedName);

                        // Assert
                        instance.IsShared.ShouldBeTrue();

                        // Act
                        manager.Unshare();

                        // Assert
                        instance.IsShared.ShouldBeFalse();
                    }
                    catch (Exception)
                    {
                        api.StopInstance(instanceName);
                        throw;
                    }
                }
                catch (Exception)
                {
                    api.DeleteInstance(instanceName, deleteFiles: true);
                    throw;
                }
            }
        }
Ejemplo n.º 12
0
        public void CreateTemporaryInstance_Creates_Starts_And_Deletes_An_Instance(bool deleteFiles)
        {
            // Arrange
            if (!SqlLocalDbApi.IsWindows)
            {
                // HACK Theories dont seem to work correctly with subclasses now
                // so cannot make a derived class for a "Windows-only" theory.
                return;
            }

            using (var api = new SqlLocalDbApi(_loggerFactory))
            {
                ISqlLocalDbInstanceInfo info;
                string name;

                // Act
                using (TemporarySqlLocalDbInstance target = api.CreateTemporaryInstance(deleteFiles))
                {
                    // Assert
                    target.ShouldNotBeNull();
                    target.Name.ShouldNotBeNull();
                    target.Name.ShouldNotBeEmpty();

                    Guid.TryParse(target.Name, out Guid nameAsGuid).ShouldBeTrue();
                    nameAsGuid.ShouldNotBe(Guid.Empty);

                    // Act
                    info = target.GetInstanceInfo();

                    // Assert
                    info.ShouldNotBeNull();
                    info.Exists.ShouldBeTrue();
                    info.IsRunning.ShouldBeTrue();

                    name = target.Name;
                }

                // Act
                info = api.GetInstanceInfo(name);

                // Assert
                info.ShouldNotBeNull();
                info.Exists.ShouldBeFalse();
            }
        }
Ejemplo n.º 13
0
        public void SqlLocalDbInstance_Stop_Stops_Instance()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            string instanceName = Guid.NewGuid().ToString();

            SqlLocalDbApi.CreateInstance(instanceName);

            try
            {
                SqlLocalDbInstance target = new SqlLocalDbInstance(instanceName);
                target.Start();

                ISqlLocalDbInstanceInfo info = SqlLocalDbApi.GetInstanceInfo(instanceName);
                Assert.IsNotNull(info, "SqlLocalDbApi.GetInstanceInfo() returned null.");
                Assert.IsTrue(info.IsRunning, "The SQL LocalDB instance was not started.");

                Assert.IsTrue(target.IsRunning, "SqlLocalDbInstance.IsRunning is incorrect.");
                Assert.AreNotEqual(string.Empty, target.NamedPipe, "SqlLocalDbInstance.NamedPipe is incorrect.");

                // Act
                target.Stop();

                try
                {
                    // Assert
                    info = SqlLocalDbApi.GetInstanceInfo(instanceName);
                    Assert.IsNotNull(info, "SqlLocalDbApi.GetInstanceInfo() returned null.");
                    Assert.IsFalse(info.IsRunning, "ISqlLocalDbInstanceInfo.IsRunning is incorrect.");

                    Assert.IsFalse(target.IsRunning, "SqlLocalDbInstance.IsRunning is incorrect.");
                    Assert.AreEqual(string.Empty, target.NamedPipe, "SqlLocalDbInstance.NamedPipe is incorrect.");
                }
                finally
                {
                    SqlLocalDbApi.StopInstance(instanceName);
                }
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Gets an SQL Local DB instance with the specified name if it exists, otherwise a new instance with the specified name is created.
        /// </summary>
        /// <param name="value">The <see cref="ISqlLocalDbProvider"/> to use to get or create the instance.</param>
        /// <param name="instanceName">The name of the SQL Server LocalDB instance to get or create.</param>
        /// <returns>
        /// An SQL Local DB instance with the name specified by <paramref name="instanceName"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="value"/> or <paramref name="instanceName"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// <paramref name="value"/> returns <see langword="null"/> when queried for instances.
        /// </exception>
        public static ISqlLocalDbInstance GetOrCreateInstance(this ISqlLocalDbProvider value, string instanceName)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (instanceName == null)
            {
                throw new ArgumentNullException(nameof(instanceName));
            }

            bool instanceExists = false;

            if (SqlLocalDbApi.IsDefaultInstanceName(instanceName))
            {
                // The default instance is always listed, even if it does not exist,
                // so need to query that separately to verify whether to get or create.
                instanceExists = SqlLocalDbApi.GetInstanceInfo(instanceName).Exists;
            }
            else
            {
                // This approach is used otherwise for testability
                IList <ISqlLocalDbInstanceInfo> instances = value.GetInstances();

                if (instances != null)
                {
                    // Instance names in SQL Local DB are case-insensitive
                    instanceExists = instances
                                     .Where((p) => p != null)
                                     .Where((p) => string.Equals(p.Name, instanceName, StringComparison.OrdinalIgnoreCase))
                                     .Any();
                }
            }

            if (instanceExists)
            {
                return(value.GetInstance(instanceName));
            }
            else
            {
                return(value.CreateInstance(instanceName));
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Initializes the database.
        /// </summary>
        /// <param name="connection"></param>
        public static INDbUnitTest Initialize(SqlConnection connection)
        {
            if (SqlLocalDbApi.GetInstanceInfo(InstanceName).Exists)
            {
                SqlLocalDbApi.StopInstance(InstanceName, TimeSpan.FromSeconds(10));
                SqlLocalDbApi.DeleteInstance(InstanceName, true);
            }

            SqlLocalDbApi.CreateInstance(InstanceName);

            var database = new SqlDbUnitTest(connection);

            database.Scripts.AddSingle($"{TestContext.CurrentContext.TestDirectory}\\Scripts\\InitSchema.sql");
            database.Scripts.AddWithWildcard($"{TestContext.CurrentContext.TestDirectory}\\Scripts", "InitTable_*.sql");
            database.ExecuteScripts();
            database.ReadXmlSchema(Resource.AsStream("Wikibus.xsd"));

            return(database);
        }
        public void TemporarySqlLocalDbInstance_Constructor_Attempts_To_Delete_Instance_If_Instance_Cannot_Be_Started()
        {
            // Arrange
            string instanceName = "MyTempInstance" + Guid.NewGuid().ToString();

            var mock = new Mock <SqlLocalDbProvider>()
            {
                CallBase = true,
            };

            // Set up the CreateInstance() method to create an SQL LocalDB
            // instance but that then throws an exception when started.
            mock.Setup((p) => p.CreateInstance(instanceName))
            .Returns(
                () =>
            {
                SqlLocalDbApi.CreateInstance(instanceName);
                return(new SqlLocalDbInstanceThatCannotBeStarted(instanceName));
            })
            .Verifiable();

            ISqlLocalDbProvider provider = mock.Object;
            bool deleteFiles             = false;

            // Act
            InvalidOperationException error = ErrorAssert.Throws <InvalidOperationException>(
                () =>
            {
                using (TemporarySqlLocalDbInstance target = new TemporarySqlLocalDbInstance(instanceName, provider, deleteFiles))
                {
                }
            });

            mock.Verify();

            ISqlLocalDbInstanceInfo instanceInfo = SqlLocalDbApi.GetInstanceInfo(instanceName);

            Assert.IsFalse(instanceInfo.Exists, "The temporary instance was not deleted.");

            throw error;
        }
        private void CreateTemporaryInstance_Creates_Starts_And_Deletes_An_Instance(bool deleteFiles)
        {
            // Arrange
            using (var api = new SqlLocalDbApi(_loggerFactory))
            {
                ISqlLocalDbInstanceInfo info;
                string name;

                // Act
                using (TemporarySqlLocalDbInstance target = api.CreateTemporaryInstance(deleteFiles))
                {
                    // Assert
                    target.ShouldNotBeNull();
                    target.Name.ShouldNotBeNull();
                    target.Name.ShouldNotBeEmpty();

                    Guid.TryParse(target.Name, out Guid nameAsGuid).ShouldBeTrue();
                    nameAsGuid.ShouldNotBe(Guid.Empty);

                    // Act
                    info = target.GetInstanceInfo();

                    // Assert
                    info.ShouldNotBeNull();
                    info.Exists.ShouldBeTrue();
                    info.IsRunning.ShouldBeTrue();

                    name = target.Name;
                }

                // Act
                info = api.GetInstanceInfo(name);

                // Assert
                info.ShouldNotBeNull();
                info.Exists.ShouldBeFalse();
            }
        }
Ejemplo n.º 18
0
        public void Methods_Validate_Parameters()
        {
            // Arrange
            TimeSpan timeout = TimeSpan.Zero.Add(TimeSpan.FromTicks(-1));

            using (var actual = new SqlLocalDbApi(_loggerFactory))
            {
                // Act and Assert
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.CreateInstance(null, "version"));
                Assert.Throws <ArgumentNullException>("version", () => actual.CreateInstance("instanceName", null));
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.DeleteInstance(null));
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.GetInstanceInfo(null));
                Assert.Throws <ArgumentNullException>("version", () => actual.GetVersionInfo(null));
                Assert.Throws <ArgumentNullException>("ownerSid", () => actual.ShareInstance(null, "instanceName", "sharedInstanceName"));
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.ShareInstance("ownerSid", null, "sharedInstanceName"));
                Assert.Throws <ArgumentNullException>("sharedInstanceName", () => actual.ShareInstance("ownerSid", "instanceName", null));
                Assert.Throws <ArgumentException>("instanceName", () => actual.ShareInstance("sid", string.Empty, "sharedInstanceName"));
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.StartInstance(null));
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.StopInstance(null, TimeSpan.Zero));
                Assert.Throws <ArgumentOutOfRangeException>("timeout", () => actual.StopInstance("instanceName", timeout)).ActualValue.ShouldBe(timeout);
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.UnshareInstance(null));
            }
        }
Ejemplo n.º 19
0
        public void SqlLocalDbInstance_GetInstanceInfo_Returns_Information_For_The_Specified_Instance()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            string instanceName = Guid.NewGuid().ToString();

            SqlLocalDbApi.CreateInstance(instanceName);

            ISqlLocalDbInstanceInfo expected = SqlLocalDbApi.GetInstanceInfo(instanceName);

            try
            {
                SqlLocalDbInstance target = new SqlLocalDbInstance(instanceName);

                // Act
                ISqlLocalDbInstanceInfo result = target.GetInstanceInfo();

                // Assert
                Assert.IsNotNull(result, "GetInstanceInfo() returned null.");
                Assert.AreEqual(expected.ConfigurationCorrupt, result.ConfigurationCorrupt, "ISqlLocalDbInstanceInfo. is incorrect.");
                Assert.AreEqual(expected.Exists, result.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");
                Assert.AreEqual(expected.IsAutomatic, result.IsAutomatic, "ISqlLocalDbInstanceInfo.IsAutomatic is incorrect.");
                Assert.AreEqual(expected.IsRunning, result.IsRunning, "ISqlLocalDbInstanceInfo.IsRunning is incorrect.");
                Assert.AreEqual(expected.IsShared, result.IsShared, "ISqlLocalDbInstanceInfo.IsShared is incorrect.");
                Assert.AreEqual(expected.LastStartTimeUtc, result.LastStartTimeUtc, "ISqlLocalDbInstanceInfo.LastStartTimeUtc is incorrect.");
                Assert.AreEqual(expected.LocalDbVersion, result.LocalDbVersion, "ISqlLocalDbInstanceInfo.LocalDbVersion is incorrect.");
                Assert.AreEqual(expected.Name, result.Name, "ISqlLocalDbInstanceInfo.Name is incorrect.");
                Assert.AreEqual(expected.NamedPipe, result.NamedPipe, "ISqlLocalDbInstanceInfo.NamedPipe is incorrect.");
                Assert.AreEqual(expected.OwnerSid, result.OwnerSid, "ISqlLocalDbInstanceInfo.OwnerSid is incorrect.");
                Assert.AreEqual(expected.SharedName, result.SharedName, "ISqlLocalDbInstanceInfo.SharedName is incorrect.");
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }
        }
Ejemplo n.º 20
0
        public void Can_Create_SqlLocalDB_Instances_With_Different_Versions()
        {
            // Arrange
            using (var actual = new SqlLocalDbApi(_loggerFactory))
            {
                foreach (string version in actual.Versions)
                {
                    // Act
                    ISqlLocalDbVersionInfo versionInfo = actual.GetVersionInfo(version);

                    // Assert
                    versionInfo.ShouldNotBeNull();
                    versionInfo.Name.ShouldStartWith(version.Split('.').First());
                    versionInfo.Exists.ShouldBeTrue();
                    versionInfo.Version.ShouldNotBeNull();
                    versionInfo.Version.ShouldNotBe(new Version());

                    string instanceName = Guid.NewGuid().ToString();

                    // Act
                    actual.CreateInstance(instanceName, version);

                    // Assert
                    ISqlLocalDbInstanceInfo instanceInfo = actual.GetInstanceInfo(instanceName);
                    instanceInfo.ShouldNotBeNull();
                    instanceInfo.Name.ShouldBe(instanceName);
                    instanceInfo.Exists.ShouldBeTrue();
                    instanceInfo.IsRunning.ShouldBeFalse();
                    instanceInfo.LocalDbVersion.ShouldBe(versionInfo.Version);

                    // Act (no Assert)
                    actual.DeleteInstance(instanceName);
                    actual.DeleteInstanceFiles(instanceName);
                }
            }
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Returns information about the specified LocalDB instance.
 /// </summary>
 /// <param name="instanceName">The name of the LocalDB instance to get the information for.</param>
 /// <returns>
 /// An instance of <see cref="ISqlLocalDbInstanceInfo"/> containing information
 /// about the LocalDB instance specified by <paramref name="instanceName"/>.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="instanceName"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="InvalidOperationException">
 /// SQL Server LocalDB is not installed on the local machine.
 /// </exception>
 /// <exception cref="SqlLocalDbException">
 /// The information for the SQL Server LocalDB instance specified by
 /// <paramref name="instanceName"/> could not obtained.
 /// </exception>
 public virtual ISqlLocalDbInstanceInfo GetInstanceInfo(string instanceName) => SqlLocalDbApi.GetInstanceInfo(instanceName);
Ejemplo n.º 22
0
        public async Task Can_Manage_SqlLocalDB_Instances()
        {
            // Arrange
            using (var actual = new SqlLocalDbApi(_loggerFactory))
            {
                string instanceName = Guid.NewGuid().ToString();

                // Act
                ISqlLocalDbInstanceInfo instance = actual.GetInstanceInfo(instanceName);

                // Assert
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeFalse();
                instance.IsRunning.ShouldBeFalse();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeFalse();

                // Act
                instance = actual.CreateInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeFalse();

                // Act
                string namedPipe = actual.StartInstance(instanceName);

                // Assert
                namedPipe.ShouldNotBeNullOrWhiteSpace();

                var builder = new SqlConnectionStringBuilder()
                {
                    DataSource = namedPipe
                };

                using (var connection = new SqlConnection(builder.ConnectionString))
                {
                    await connection.OpenAsync();
                }

                // Act
                instance = actual.GetInstanceInfo(instanceName);

                // Assert
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeTrue();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeTrue();

                // Act
                actual.StopInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeFalse();

                // Act
                actual.DeleteInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeFalse();
                instance.IsRunning.ShouldBeFalse();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeFalse();

                // Act (no Assert)
                actual.DeleteInstanceFiles(instanceName);
            }
        }
Ejemplo n.º 23
0
        public void Can_Manage_SqlLocalDB_Instances()
        {
            // Arrange
            using (var actual = new SqlLocalDbApi(_loggerFactory))
            {
                string instanceName = Guid.NewGuid().ToString();

                // Act
                ISqlLocalDbInstanceInfo instance = actual.GetInstanceInfo(instanceName);

                // Assert
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeFalse();
                instance.IsRunning.ShouldBeFalse();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeFalse();

                // Act
                actual.CreateInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeFalse();

                // Act
                actual.StartInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeTrue();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeTrue();

                // Act
                actual.StopInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeFalse();

                // Act
                actual.DeleteInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeFalse();
                instance.IsRunning.ShouldBeFalse();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeFalse();

                // Act (no Assert)
                actual.DeleteInstanceFiles(instanceName);
            }
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Returns information about the LocalDB instance.
 /// </summary>
 /// <returns>
 /// An instance of <see cref="ISqlLocalDbInstanceInfo"/> containing information about the LocalDB instance.
 /// </returns>
 public virtual ISqlLocalDbInstanceInfo GetInstanceInfo() => SqlLocalDbApi.GetInstanceInfo(_instanceName);