public static void AddSqlLocalDB_Registers_Services_Using_Options()
        {
            // Arrange
            var options = new SqlLocalDbOptions()
            {
                StopOptions = (StopInstanceOptions)int.MinValue,
            };

            IServiceCollection services = CreateServiceCollection();

            // Act
            services.AddSqlLocalDB(options);

            // Assert
            IServiceProvider provider = services.BuildServiceProvider();

            var actualOptions = provider.GetRequiredService <SqlLocalDbOptions>();

            options.ShouldBeSameAs(actualOptions);

            var localDB = provider.GetRequiredService <ISqlLocalDbApi>();

            localDB.ShouldNotBeNull();
            localDB.ShouldBeOfType <SqlLocalDbApi>();
        }
コード例 #2
0
        public void Constructor_Initializes_Instance()
        {
            // Arrange
            var options = new SqlLocalDbOptions()
            {
                AutomaticallyDeleteInstanceFiles = true,
                Language = CultureInfo.GetCultureInfo("de-DE"),
                NativeApiOverrideVersion = "11.0",
                StopOptions = StopInstanceOptions.NoWait,
                StopTimeout = TimeSpan.FromSeconds(30),
            };

            using (var actual = new SqlLocalDbApi(options, _loggerFactory))
            {
                actual.AutomaticallyDeleteInstanceFiles.ShouldBe(options.AutomaticallyDeleteInstanceFiles);
                actual.DefaultInstanceName.ShouldNotBeNull();
                actual.LanguageId.ShouldBeGreaterThan(0);
                actual.LatestVersion.ShouldNotBeNull();
                actual.LoggerFactory.ShouldNotBeNull();
                actual.StopOptions.ShouldBe(options.StopOptions);
                actual.StopTimeout.ShouldBe(options.StopTimeout);
                actual.Versions.ShouldNotBeNull();
                actual.Versions.ShouldNotBeEmpty();
                actual.Versions.ShouldBeUnique();
            }
        }
コード例 #3
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"));
            }
        }
        public static void AddSqlLocalDB_Does_Not_Overwrite_Existing_Services()
        {
            // Arrange
            IServiceCollection services = CreateServiceCollection();

            var existingOptions = new SqlLocalDbOptions();
            var existingLocalDB = Mock.Of <ISqlLocalDbApi>();

            services.AddTransient((_) => existingOptions);
            services.AddTransient((_) => existingLocalDB);

            // Act
            services.AddSqlLocalDB();

            // Assert
            IServiceProvider provider = services.BuildServiceProvider();

            var actualOptions = provider.GetRequiredService <SqlLocalDbOptions>();

            actualOptions.ShouldBeSameAs(existingOptions);

            var actualLocalDB = provider.GetRequiredService <ISqlLocalDbApi>();

            actualLocalDB.ShouldBeSameAs(existingLocalDB);
        }
コード例 #5
0
        public void Constructor_Validates_Parameters()
        {
            // Arrange
            var options = new SqlLocalDbOptions();

            Assert.Throws <ArgumentNullException>("options", () => new SqlLocalDbApi(null, _loggerFactory));
            Assert.Throws <ArgumentNullException>("loggerFactory", () => new SqlLocalDbApi(null));
            Assert.Throws <ArgumentNullException>("loggerFactory", () => new SqlLocalDbApi(options, null));
            Assert.Throws <ArgumentNullException>("registry", () => new SqlLocalDbApi(options, null, _loggerFactory));
        }
コード例 #6
0
        public static void SqlLocalDbOptions_LanguageId_Returns_The_LCID()
        {
            // Act
            var actual = new SqlLocalDbOptions()
            {
                Language = CultureInfo.GetCultureInfo("de-DE")
            };

            // Assert
            actual.LanguageId.ShouldBeGreaterThan(0);
        }
コード例 #7
0
        public static void SqlLocalDbOptions_Defaults_Are_Correct()
        {
            // Act
            var actual = new SqlLocalDbOptions();

            // Assert
            actual.AutomaticallyDeleteInstanceFiles.ShouldBeFalse();
            actual.Language.ShouldBeNull();
            actual.NativeApiOverrideVersion.ShouldBe(string.Empty);
            actual.StopOptions.ShouldBe(StopInstanceOptions.None);
            actual.StopTimeout.ShouldBe(TimeSpan.FromMinutes(1));
        }
コード例 #8
0
        public static void AddSqlLocalDB_Validates_Parameters()
        {
            // Arrange
            var services = Mock.Of <IServiceCollection>();

            SqlLocalDbOptions          options         = null;
            Action <SqlLocalDbOptions> configureAction = null;
            Func <IServiceProvider, SqlLocalDbOptions> configureFunc = null;

            // Act and Assert
            Assert.Throws <ArgumentNullException>("options", () => services.AddSqlLocalDB(options));
            Assert.Throws <ArgumentNullException>("configure", () => services.AddSqlLocalDB(configureAction));
            Assert.Throws <ArgumentNullException>("configure", () => services.AddSqlLocalDB(configureFunc));

            services = null;

            // Act and Assert
            Assert.Throws <ArgumentNullException>("services", () => services.AddSqlLocalDB());
            Assert.Throws <ArgumentNullException>("services", () => services.AddSqlLocalDB(options));
            Assert.Throws <ArgumentNullException>("services", () => services.AddSqlLocalDB(configureAction));
            Assert.Throws <ArgumentNullException>("services", () => services.AddSqlLocalDB(configureFunc));
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: thieuhoang/sqllocaldb
        /// <summary>
        /// The main entry point to the application.
        /// </summary>
        /// <param name="args">The command-line arguments passed to the application.</param>
        internal static void Main(string[] args)
        {
            PrintBanner();

            var options = new SqlLocalDbOptions()
            {
                AutomaticallyDeleteInstanceFiles = true,
                StopOptions = StopInstanceOptions.NoWait,
            };

            var services      = new ServiceCollection().AddLogging((p) => p.AddConsole().SetMinimumLevel(LogLevel.Debug));
            var loggerFactory = services.BuildServiceProvider().GetRequiredService <ILoggerFactory>();

            var localDB = new SqlLocalDbApi(options, loggerFactory);

            if (!localDB.IsLocalDBInstalled())
            {
                Console.WriteLine(SR.SqlLocalDbApi_NotInstalledFormat, Environment.MachineName);
                return;
            }

            if (args?.Length == 1 &&
                (string.Equals(args[0], "/deleteuserinstances", StringComparison.OrdinalIgnoreCase) ||
                 string.Equals(args[0], "--delete-user-instances", StringComparison.OrdinalIgnoreCase)))
            {
                localDB.DeleteUserInstances(deleteFiles: true);
            }

            IReadOnlyList <ISqlLocalDbVersionInfo> versions = localDB.GetVersions();

            Console.WriteLine(Strings.Program_VersionsListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbVersionInfo version in versions)
            {
                Console.WriteLine(version.Name);
            }

            Console.WriteLine();

            IReadOnlyList <ISqlLocalDbInstanceInfo> instances = localDB.GetInstances();

            Console.WriteLine(Strings.Program_InstancesListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbInstanceInfo instanceInfo in instances)
            {
                Console.WriteLine(instanceInfo.Name);
            }

            Console.WriteLine();

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

            ISqlLocalDbInstanceInfo instance = localDB.CreateInstance(instanceName);

            var manager = new SqlLocalDbInstanceManager(instance, localDB);

            manager.Start();

            try
            {
                if (IsCurrentUserAdmin())
                {
                    manager.Share(Guid.NewGuid().ToString());
                }

                try
                {
                    using (SqlConnection connection = manager.CreateConnection())
                    {
                        connection.Open();

                        try
                        {
                            using (SqlCommand command = new SqlCommand("create database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }

                            using (SqlCommand command = new SqlCommand("drop database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }
                        }
                        finally
                        {
                            connection.Close();
                        }
                    }
                }
                finally
                {
                    if (IsCurrentUserAdmin())
                    {
                        manager.Unshare();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                manager.Stop();
                localDB.DeleteInstance(instance.Name);
            }

            Console.WriteLine();
            Console.Write(Strings.Program_ExitPrompt);
            Console.ReadKey();
        }