コード例 #1
0
        public void SqlLocalDbProvider_As_ISqlLocalDbFactory_CreateInstance_Creates_Instance()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            ISqlLocalDbProvider target = new SqlLocalDbProvider();
            string instanceName        = Guid.NewGuid().ToString();

            // Act
            ISqlLocalDbInstance result = target.CreateInstance(instanceName);

            // Assert
            Assert.IsNotNull(result, "CreateInstance() returned null.");
            Assert.AreEqual(instanceName, result.Name, "SqlLocalDbInstance.Name is incorrect.");

            try
            {
                ISqlLocalDbInstanceInfo info = result.GetInstanceInfo();

                Assert.IsTrue(info.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");
                Assert.IsFalse(info.IsRunning, "ISqlLocalDbInstanceInfo.IsRunning is incorrect.");

                Assert.IsTrue(Guid.TryParse(result.Name, out Guid unused), "SqlLocalDbInstance.Name is not a valid GUID.");
            }
            finally
            {
                SqlLocalDbInstance.Delete(result);
            }
        }
コード例 #2
0
        public void SqlLocalDbProvider_CreateInstance_Creates_Instance()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Act
            SqlLocalDbInstance result = target.CreateInstance();

            // Assert
            Assert.IsNotNull(result, "CreateInstance() returned null.");
            Assert.IsNotNull(result.Name, "SqlLocalDbInstance.Name is null.");

            try
            {
                ISqlLocalDbInstanceInfo info = result.GetInstanceInfo();

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

                Assert.AreEqual(
                    new Version(target.Version).Major,
                    info.LocalDbVersion.Major,
                    "ISqlLocalDbInstanceInfo.LocalDbVersion is incorrect.");

                Assert.IsTrue(Guid.TryParse(result.Name, out Guid unused), "SqlLocalDbInstance.Name is not a valid GUID.");
            }
            finally
            {
                SqlLocalDbInstance.Delete(result);
            }
        }
コード例 #3
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.");
            });
        }
コード例 #4
0
        public void SqlLocalDbProvider_CreateInstance_Creates_Instance_With_Specified_Name()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target       = new SqlLocalDbProvider();
            string             instanceName = Guid.NewGuid().ToString();

            // Act
            SqlLocalDbInstance result = target.CreateInstance(instanceName);

            // Assert
            Assert.IsNotNull(result, "CreateInstance() returned null.");
            Assert.AreEqual(instanceName, result.Name, "SqlLocalDbInstance.Name is incorrect.");

            try
            {
                ISqlLocalDbInstanceInfo info = result.GetInstanceInfo();

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

                Assert.AreEqual(
                    new Version(target.Version).Major,
                    info.LocalDbVersion.Major,
                    "ISqlLocalDbInstanceInfo.LocalDbVersion is incorrect.");
            }
            finally
            {
                SqlLocalDbInstance.Delete(result);
            }
        }
コード例 #5
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.");
        }
コード例 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TemporarySqlLocalDbInstance"/> class.
        /// </summary>
        /// <param name="instanceName">The name of the temporary SQL LocalDB instance.</param>
        /// <param name="provider">The <see cref="ISqlLocalDbProvider"/> to use to create the temporary instance.</param>
        /// <param name="deleteFiles">Whether to delete the file(s) associated with the SQL LocalDB instance when deleted.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="instanceName"/> or <paramref name="provider"/> is <see langword="null"/>.
        /// </exception>
        public TemporarySqlLocalDbInstance(string instanceName, ISqlLocalDbProvider provider, bool deleteFiles)
        {
            if (instanceName == null)
            {
                throw new ArgumentNullException(nameof(instanceName));
            }

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

            _instance    = provider.CreateInstance(instanceName);
            _deleteFiles = deleteFiles;

            try
            {
                _instance.Start();
            }
            catch (Exception)
            {
                SqlLocalDbInstance.Delete(_instance, throwIfNotFound: true, deleteFiles: _deleteFiles);
                throw;
            }
        }
コード例 #7
0
        public void SqlLocalDbInstance_Delete_Throws_If_Instance_Is_Null()
        {
            // Arrange
            ISqlLocalDbInstance instance = null;

            // Act and Assert
            throw ErrorAssert.Throws <ArgumentNullException>(
                      () => SqlLocalDbInstance.Delete(instance),
                      "instance");
        }
コード例 #8
0
        public void SqlLocalDbInstance_Delete_Throws_If_Instance_Is_Invalid()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            const string InstanceName = "\\\\";

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

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

            ISqlLocalDbInstance instance = mock.Object;

            // Act
            SqlLocalDbException error = ErrorAssert.Throws <SqlLocalDbException>(
                () => SqlLocalDbInstance.Delete(instance));

            // Assert
            Assert.AreEqual(SqlLocalDbErrors.InvalidInstanceName, error.ErrorCode, "SqlLocalDbException.ErrorCode is incorrect.");
            Assert.AreEqual(InstanceName, error.InstanceName, "SqlLocalDbException.InstanceName is incorrect.");

            throw error;
        }
コード例 #9
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <param name="disposing">
        /// <see langword="true" /> to release both managed and unmanaged resources;
        /// <see langword="false" /> to release only unmanaged resources.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (_instance != null)
                {
                    try
                    {
                        _instance.Stop();
                    }
                    catch (SqlLocalDbException ex)
                    {
                        // Ignore the exception if we could not stop the instance
                        // because it does not exist, otherwise log the error.
                        if (ex.ErrorCode != SqlLocalDbErrors.UnknownInstance)
                        {
                            Logger.Error(Logger.TraceEvent.StopFailed, SR.TemporarySqlLocalDbInstance_StopFailedFormat, _instance.Name, ex.ErrorCode);
                        }
                    }

                    try
                    {
                        SqlLocalDbInstance.Delete(_instance, throwIfNotFound: false, deleteFiles: _deleteFiles);
                    }
                    catch (SqlLocalDbException ex)
                    {
                        // Ignore the exception if we could not delete the instance because it was in use
                        if (ex.ErrorCode != SqlLocalDbErrors.InstanceBusy)
                        {
                            Logger.Error(Logger.TraceEvent.DeleteFailed, SR.TemporarySqlLocalDbInstance_DeleteFailedFormat, _instance.Name, ex.ErrorCode);
                        }
                    }
                }

                _disposed = true;
            }
        }