示例#1
0
        /// <summary>
        /// Gets the replacement token with a snapshot-specific suffix appended.
        /// </summary>
        /// <returns>The configured token replacement with a snapshot database suffix appended.</returns>
        /// <exception cref="FormatException">Occurs if the snapshot identifier in context
        /// is not an alphanumeric value.</exception>
        public string GetDatabaseNameReplacementToken()
        {
            var snapshotContext = _snapshotContextProvider.GetSnapshotContext();

            if (snapshotContext == null || string.IsNullOrEmpty(snapshotContext.SnapshotIdentifier))
            {
                return(_next.GetDatabaseNameReplacementToken());
            }

            // To prevent possible tampering, snapshot identifier must only contain letters or numbers.
            if (snapshotContext.SnapshotIdentifier.Any(c => !(char.IsLetter(c) || char.IsDigit(c))))
            {
                throw new FormatException("Invalid value for snapshot identifier.");
            }

            return(_next.GetDatabaseNameReplacementToken() + $"_SS{snapshotContext.SnapshotIdentifier}");
        }
        protected override void Arrange()
        {
            // Set up mocked dependencies and supplied values
            _databaseReplacementTokenProvider = A.Fake <IDatabaseReplacementTokenProvider>();

            A.CallTo(() => _databaseReplacementTokenProvider.GetDatabaseNameReplacementToken()).Returns("OneDatabase").Once();

            A.CallTo(() => _databaseReplacementTokenProvider.GetDatabaseNameReplacementToken()).Returns("AnotherDatabase").Once();

            A.CallTo(() => _databaseReplacementTokenProvider.GetDatabaseNameReplacementToken()).Returns("OneDatabase").Once();

            _configConnectionStringsProvider = A.Fake <IConfigConnectionStringsProvider>();

            A.CallTo(() => _configConnectionStringsProvider.Count).Returns(1);

            A.CallTo(() => _configConnectionStringsProvider.GetConnectionString("SomeConnectionStringName"))
            .Returns("Server=SomeServer; Database=EdFi_{0}; UID=SomeUser; Password=SomePassword");

            _dbConnectionStringBuilderAdapterFactory = A.Fake <IDbConnectionStringBuilderAdapterFactory>();

            A.CallTo(() => _dbConnectionStringBuilderAdapterFactory.Get()).Returns(new SqlConnectionStringBuilderAdapter());
        }
示例#3
0
        /// <summary>
        /// Gets the connection string with the tokens already replaced using a configured named connection string.
        /// </summary>
        /// <returns>The connection string.</returns>
        public string GetConnectionString()
        {
            var connectionStringBuilder = _dbConnectionStringBuilderAdapterFactory.Get();

            string protoTypeConnectionString = PrototypeConnectionString();

            if (string.IsNullOrEmpty(protoTypeConnectionString))
            {
                throw new ArgumentException(
                          $"No connection string named '{_prototypeConnectionStringName}' was found in the 'connectionStrings' section of the application configuration file.");
            }

            connectionStringBuilder.ConnectionString = protoTypeConnectionString;

            // Override the Database Name, format if string coming in has a format replacement token,
            // otherwise use database name set in the Initial Catalog.
            connectionStringBuilder.DatabaseName = connectionStringBuilder.DatabaseName.IsFormatString()
                ? string.Format(
                connectionStringBuilder.DatabaseName,
                _databaseReplacementTokenProvider.GetDatabaseNameReplacementToken())
                : connectionStringBuilder.DatabaseName;

            // Override the Server Name, format if string coming in has a format replacement token,
            // otherwise use server name set in the Data Source.
            connectionStringBuilder.ServerName = connectionStringBuilder.ServerName.IsFormatString()
                ? string.Format(
                connectionStringBuilder.ServerName,
                _databaseReplacementTokenProvider.GetServerNameReplacementToken())
                : connectionStringBuilder.ServerName;

            return(connectionStringBuilder.ConnectionString);

            string PrototypeConnectionString()
            {
                if (_configConnectionStringsProvider.Count == 0)
                {
                    throw new ArgumentException("No connection strings were found in the configuration file.");
                }

                if (string.IsNullOrWhiteSpace(_prototypeConnectionStringName))
                {
                    throw new ArgumentNullException("prototypeConnectionStringName");
                }

                return(_configConnectionStringsProvider.GetConnectionString(_prototypeConnectionStringName));
            }
        }
示例#4
0
 protected override void Act()
 {
     _databaseReplacementTokenProvider.GetDatabaseNameReplacementToken();
 }
示例#5
0
 protected override void Act()
 {
     _actualDatabaseNameReplacementToken = _databaseReplacementTokenProvider.GetDatabaseNameReplacementToken();
     _actualServerNameReplacementToken   = _databaseReplacementTokenProvider.GetServerNameReplacementToken();
 }