public void Read_SeveralTimes_VerifyReload() { var fileSystem = new FakeFileSystem(DateTimeProvider.Singleton); var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.xml"); IFakeFile settingsFile = fileSystem.SetFileFromUtf8String(path, ExpectedSettingsFile1); DateTime currentTime = new DateTime(2016, 1, 1, 12, 0, 0); var dateTimeProviderMock = new Mock <IDateTimeProvider>(); dateTimeProviderMock .Setup(x => x.Time()) .Returns(() => currentTime); var settingsInternal = new SettingsInternal(fileSystem, dateTimeProviderMock.Object); var value = settingsInternal.GetSetting("key"); // should cause reload currentTime = currentTime.AddSeconds(32); value = settingsInternal.GetSetting("key"); currentTime = currentTime.AddSeconds(32); // should cause reload value = settingsInternal.GetSetting("key"); FileAccessInfo fileAccessInfo = fileSystem.GetFileAccessInfo(path); Assert.That(fileAccessInfo.Reads, Is.EqualTo(2)); Assert.That(fileAccessInfo.Writes, Is.EqualTo(0)); }
public void Write() { var fileSystem = new FakeFileSystem(DateTimeProvider.Singleton); var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.xml"); fileSystem.SetFileFromUtf8String(path, ExpectedSettingsFile1); var settingsInternal = new SettingsInternal(fileSystem, DateTimeProvider.Singleton); settingsInternal.SetSetting("key", "value2"); XmlTestSupport.GetComparableXml(fileSystem.GetFile(path).ContentAsUtf8String) .ShouldBe("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><node key=\"key\" value=\"value2\" /></root>"); }
/// <summary> /// Upgrades the specified connection. /// </summary> /// <param name="dbmsName">Name of the DBMS.</param> /// <param name="connStr">The connection string.</param> /// <returns>The upgraded database configuration.</returns> /// <exception cref="ConnectionUpgradeException">Unable to upgrade database connection.</exception> public IIntegrationDatabaseConfiguration Upgrade(string dbmsName, string connStr) { #pragma warning disable 618 connStr = SettingsInternal.DecryptStringForUpgrade(connStr); #pragma warning restore 618 Dictionary <String, String> providerKeys = new Dictionary <string, string>(); providerKeys.Add("sql server", "SqlServer"); providerKeys.Add("oracle", "Oracle"); bool isSqlServer = dbmsName.ToLower().Equals("sql server"); IDatabaseProvider provider = pluginProvider.GetImplementation(DatabaseProviderKey.Deserialize(providerKeys[dbmsName.ToLower()])); IIntegrationDatabaseConfiguration dbConfig = provider.CreateEmptyIntegrationDatabaseConfiguration(); MetaDatabaseConfiguration meta = new MetaDatabaseConfiguration(dbConfig); if (RuntimePlatformUtils.IsJava()) { string connStringOverride_SID = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(PORT={0})(HOST={1}))(CONNECT_DATA=(SID={2})))"; Regex _datasourceJavaSidRegex = new Regex(@"jdbc:oracle:thin:([^/]*)/([^@]*)@([^:]*):([^:]*):([^:]*)", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); string connStringOverride_ServiceName = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(PORT={0})(HOST={1}))(CONNECT_DATA=(SERVICE_NAME={2})))"; Regex _datasourceJavaServiceNameRegex = new Regex(@"jdbc:oracle:thin:([^/]*)/([^@]*)@([^:]*):([^:]*)/([^:]*)", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); string username, password, tnsDescriptor; Match _sidMatch, _snMatch; _sidMatch = _datasourceJavaSidRegex.Match(connStr); if (_sidMatch.Success) { username = _sidMatch.Groups[1].Value; password = _sidMatch.Groups[2].Value; tnsDescriptor = String.Format(connStringOverride_SID, _sidMatch.Groups[4].Value, _sidMatch.Groups[3].Value, _sidMatch.Groups[5].Value); } else { _snMatch = _datasourceJavaServiceNameRegex.Match(connStr); if (!_snMatch.Success) { throw new ConnectionUpgradeException("Unable to upgrade database connection."); } username = _snMatch.Groups[1].Value; password = _snMatch.Groups[2].Value; tnsDescriptor = String.Format(connStringOverride_ServiceName, _snMatch.Groups[4].Value, _snMatch.Groups[3].Value, _snMatch.Groups[5].Value); } meta.GetParameter("Username").Set(username); meta.GetParameter("Password").Set(password); dbConfig.AdvancedConfiguration.AdvancedConnectionStringField = tnsDescriptor; dbConfig.ConnectionStringOverride = String.Empty; } else { Regex _datasourceRegex = new Regex(@"data source=\s*([^ ;]+)\s*;", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); Regex _portRegex = new Regex(@"port=\s*([^ ;]+)\s*;", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); Regex _catalogRegex = new Regex(@"initial catalog=\s*([^ ;]+)\s*;", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); Regex _userIdRegex = new Regex(@"user id=\s*([^ ;]+)\s*;", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); Regex _passwordRegex = new Regex(@"password=\s*([^ ;]+)\s*;", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); Match userIdMatch, passwordMatch; bool isAdvanced = false; List <string> basicKeys = new List <string> { "data source", "user id", "password" }; if (isSqlServer) { basicKeys.Add("initial catalog"); } else { basicKeys.Add("port"); } List <string> keysInConnection = connStr.Split(';').ToList <string>(); connStr += connStr.EndsWith(";") ? "" : ";"; foreach (string key in keysInConnection) { if (!basicKeys.Contains(key.Substring(0, key.IndexOf("=")).Trim().ToLower())) { isAdvanced = true; break; } } userIdMatch = _userIdRegex.Match(connStr); meta.GetParameter("Username").Set(userIdMatch.Groups[1].Value.Replace("\"", "")); passwordMatch = _passwordRegex.Match(connStr); meta.GetParameter("Password").Set(passwordMatch.Groups[1].Value.Replace("\"", "")); if (isAdvanced) { // In advanced mode the connection string override will hold all keys and values except username and password dbConfig.AdvancedConfiguration.AdvancedConnectionStringField = connStr; if (userIdMatch.Success) { dbConfig.AdvancedConfiguration.AdvancedConnectionStringField = dbConfig.AdvancedConfiguration.AdvancedConnectionStringField.Replace(userIdMatch.Groups[0].Value, ""); } if (passwordMatch.Success) { dbConfig.AdvancedConfiguration.AdvancedConnectionStringField = dbConfig.AdvancedConfiguration.AdvancedConnectionStringField.Replace(passwordMatch.Groups[0].Value, ""); } } else { if (isSqlServer) { meta.GetParameter("Server").Set(_datasourceRegex.Match(connStr).Groups[1].Value.Replace("\"", "")); meta.GetParameter("Catalog").Set(_catalogRegex.Match(connStr).Groups[1].Value.Replace("\"", "")); } else { var dataSrcValues = _datasourceRegex.Match(connStr).Groups[1].Value.Replace("\"", "").Split('/'); var portValue = _portRegex.Match(connStr).Groups[1].Value.Replace("\"", ""); meta.GetParameter("Host").Set(dataSrcValues.IsEmpty() ? "" : dataSrcValues[0]); meta.GetParameter("Port").Set(portValue.IsNullOrEmpty() || portValue.Equals("0") ? "1521" /*default*/ : portValue); meta.GetParameter("ServiceName").Set(dataSrcValues.Length < 1 ? "" : dataSrcValues[1]); } } dbConfig.ConnectionStringOverride = String.Empty; } return(dbConfig); }