private ArgumentValidationResult ValidateMinimalRequiredOptions(ApplicationOptions options)
        {
            ArgumentValidationResult result = new ArgumentValidationResult();

            result.Status = ArgumentValidationStatus.Valid;

            if (string.IsNullOrEmpty(options.DatabaseScriptsPath))
            {
                result.Status = ArgumentValidationStatus.Failed;
                result.ValidationMessages.Add("Database script path was not given as an argument.");
            }

            if (string.IsNullOrEmpty(options.FromVersion))
            {
                result.Status = ArgumentValidationStatus.Failed;
                result.ValidationMessages.Add("FromVersion script path was not given as an argument.");
            }

            if (string.IsNullOrEmpty(options.ToVersion))
            {
                result.Status = ArgumentValidationStatus.Failed;
                result.ValidationMessages.Add("ToVersion script path was not given as an argument.");
            }

            return(result);
        }
        public void ShouldReturnValidationStatusFailedWhenEmpyArgumentsAreGiven()
        {
            // arrange
            string[] argumentList = new string[1];
            IMandatoryArgumentValidator validator = new MandatoryArgumentValidator(_applicationOptionsParser);

            // act
            ArgumentValidationResult argumentValidationResult = validator.Validate(argumentList);

            // assert
            Assert.That(argumentValidationResult.Status, Is.EqualTo(ArgumentValidationStatus.Failed));
        }
        public void ShouldReturnValidationFailReasonListWhenValidationFailsWithOneInvalidArgument()
        {
            // arrange
            string[] argumentList = CreateCompleteArgumentTestList();
            argumentList[3] = "invalidArgument: testvalue";
            IMandatoryArgumentValidator validator = new MandatoryArgumentValidator(_applicationOptionsParser);

            // act
            ArgumentValidationResult argumentValidationResult = validator.Validate(argumentList);

            // assert
            Assert.That(argumentValidationResult.ValidationMessages[0], Is.EqualTo("Invalid argument(s) passed."));
        }
        public void ShouldReturnValidationFailReasonListWhenValidationFails()
        {
            // arrange
            string[] argumentList = new string[1];
            argumentList[0] = "invalidArgument: testValue";
            IMandatoryArgumentValidator validator = new MandatoryArgumentValidator(_applicationOptionsParser);

            // act
            ArgumentValidationResult argumentValidationResult = validator.Validate(argumentList);

            // assert
            Assert.That(argumentValidationResult.ValidationMessages[0], Is.EqualTo("Database script path was not given as an argument."));
        }
        public void ShouldReturnStatusValidWhenValidOptionsForConfigFileAreGiven()
        {
            // Arrange
            string[] argumentList = new string[4];
            argumentList[0] = CommandLineArgumentOptions.DatabasePathArgumentOption + "d:\\mypath\\directory";
            argumentList[1] = CommandLineArgumentOptions.FromDatabaseVersion + "1.0.0.0";
            argumentList[2] = CommandLineArgumentOptions.ToDatabaseVersion + "1.1.0.1";
            argumentList[3] = CommandLineArgumentOptions.ConfigFileArgumentOption + "db.config";

            IMandatoryArgumentValidator validator = new MandatoryArgumentValidator(_applicationOptionsParser);

            // Act
            ArgumentValidationResult argumentValidationResult = validator.Validate(argumentList);

            // Assert
            Assert.That(argumentValidationResult.Status, Is.EqualTo(ArgumentValidationStatus.Valid));
        }
        public ArgumentValidationResult Validate(string[] argumentList)
        {
            ApplicationOptions options = applicationOptionsParser.Parse(argumentList);

            ArgumentValidationResult result = ValidateMinimalRequiredOptions(options);

            if (result.Status == ArgumentValidationStatus.Failed)
            {
                return(result);
            }

            if (!string.IsNullOrEmpty(options.ConfigurationFilename))
            {
                return(result);
            }

            if (argumentList.Length > 0 && !RequiredCommandlineOptionsModeAreSet(options))
            {
                result.Status = ArgumentValidationStatus.Failed;
                result.ValidationMessages.Add("Invalid argument(s) passed.");
            }

            return(result);
        }
Esempio n. 7
0
        private static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                PrintUsage();
                return(-1);
            }

            // Initialize default values
            ApplicationOptions options = optionsParser.Parse(args);

            if (options.InformationSwitch == InformationSwitch.On)
            {
                // Print extra info
                PrintUsage();
                PrintDatabaseDirectoryStructureTemplate();
                return(-2);
            }

            ArgumentValidationResult validationResult = argumentValidator.Validate(args);

            if (validationResult.Status == ArgumentValidationStatus.Failed)
            {
                outputLogger.WriteErrorMessage("\nArgument error:\n");
                validationResult.ValidationMessages.ForEach(
                    item => outputLogger.WriteErrorMessage(string.Format(CultureInfo.InvariantCulture, "\t{0}\n", item)));
                PrintUsage();
                return(-3);
            }

            DatabaseReleaseTool databaseReleaseTool;

            try
            {
                databaseReleaseTool = new DatabaseReleaseTool(
                    options.DatabaseScriptsPath,
                    new CreateDatabasePolicyComposite(),
                    new FileStructurePolicyComposite(),
                    outputLogger,
                    new ConnectionStringFactory(),
                    new DatabaseConnectionFactory());
            }
            catch (Exception ex)
            {
                outputLogger.WriteErrorMessage(string.Format("Error while creating databasereleasetool. {0}.", ex.Message));
                outputLogger.WriteDebugMessage(ex.StackTrace);
                return(-5);
            }

            DatabaseConnectionParameters connectionParameters = new DatabaseConnectionParameters();

            // Check if a configuration file parameter is passed.
            // If so then the database configuration will be read from the config file instead of the commandline.
            if (!string.IsNullOrEmpty(options.ConfigurationFilename))
            {
                // Check with configfile
                outputLogger.WriteInfoMessage("Starting DatabaseRelease Tool in ConfigFile Modus.");

                outputLogger.WriteDebugMessage(
                    string.Format(CultureInfo.InvariantCulture, "ConfigFile: {0}.", options.ConfigurationFilename));
                outputLogger.WriteDebugMessage(
                    string.Format(CultureInfo.InvariantCulture, "Database Path: {0}.", options.DatabaseScriptsPath));

                // Create databasesettings with configfile.
                connectionParameters.CreationType               = ConnectionStringCreationType.FromConfigurationFile;
                connectionParameters.ConfigurationFileName      = options.ConfigurationFilename;
                connectionParameters.BeforeExecuteScriptsAction =
                    GetBeforeExecuteScriptsActionFromConfigurationFile(options.ConfigurationFilename);
            }
            else
            {
                // check with databaseparams
                connectionParameters.CreationType = ConnectionStringCreationType.FromArguments;
                connectionParameters.BeforeExecuteScriptsAction = options.BeforeExecuteScriptsAction;
                connectionParameters.Arguments.Hostname         = options.Servername;
                connectionParameters.Arguments.Database         = options.DatabaseName;
                connectionParameters.Arguments.Username         = options.Username;
                connectionParameters.Arguments.Password         = options.Password;
            }

            // DatabaseType is given
            if (options.DatabaseType != DatabaseType.None)
            {
                DatabaseType type = options.DatabaseType;

                // database type is mssql by default.
                connectionParameters.DatabaseType = type;
                outputLogger.WriteInfoMessage(string.Format(CultureInfo.InvariantCulture, "DatabaseType is: {0}.", type));
            }
            else
            {
                connectionParameters.DatabaseType = DatabaseType.MsSql; // default;

                // Create databasesettings with parameters.
                outputLogger.WriteInfoMessage("No DatabaseType was given using default MsSql.");
            }

            ExcecutionResult result;

            try
            {
                result = databaseReleaseTool.Execute(
                    options.FromVersion,
                    options.ToVersion,
                    connectionParameters,
                    options.EncodingForReadingSqlScripts);
            }
            catch (Exception ex)
            {
                outputLogger.WriteErrorMessage(
                    string.Format(CultureInfo.InvariantCulture, "Unexpected error while Executing scripts. {0}.", ex.Message));
                outputLogger.WriteDebugMessage(ex.StackTrace);
                return(-6);
            }

            if (!result.Success)
            {
                outputLogger.WriteErrorMessage("Excecution failed with the following errors: ");
                outputLogger.WriteErrorMessage(string.Join("\n", result.Errors.ToArray()));
                return(-10);
            }

            outputLogger.WriteSuccessMessage("Execution succeeded.");

            return(0);
        }