/// <summary> /// Get properties /// from cache or by extracting from config file /// </summary> /// <param name="ecConfs"></param> /// <returns></returns> public static Dictionary<string, string> GetProperties(EcGlobalConfigurations ecConfs) { var properties = new Dictionary<string, string>(); if (!CacheManager.Exist(EcConfigResources.CacheKey_Properties)) { var path = string.Format(EcConfigResources.ConfigFileNameFormat, Path.Combine(ecConfs.Path, ecConfs.Filename)); var assemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase); path = Path.Combine(assemblyPath ?? string.Empty, path); XDocument doc; try { doc = XDocument.Load(path); } catch (Exception e) { throw new EcConfigException(string.Format(Errors.PropertiesExtractorConfigFileDoesNotExist, path), e); } if (doc.Root == null) throw new EcConfigException(string.Format(Errors.PropertiesExtractorSystemNotToExtractProperties, path), null); ExtractProperties(doc.Root.Elements(), properties, 1, new List<string>(), ecConfs.IsCaseSensitive); CacheManager.Add(EcConfigResources.CacheKey_Properties, properties); } properties = CacheManager.Get<Dictionary<string, string>>(EcConfigResources.CacheKey_Properties); return properties; }
public void DefaultEcConfigConfigurations() { var configs = new EcGlobalConfigurations(); Assert.AreEqual("default", configs.Filename); Assert.AreEqual("", configs.Path); Assert.IsTrue(configs.IsCaseSensitive); }
public static EcGlobalConfigurations GetEcConfigConfigurations() { var configs = CacheManager.Get<EcGlobalConfigurations>(EcConfigResources.CacheKey_EcConfigs); if (configs == null) { configs = new EcGlobalConfigurations(); //Filename var configCurrentConfigFileName = ConfigurationManager.AppSettings[Configurations.Filename]; if (!string.IsNullOrEmpty(configCurrentConfigFileName)) { if (configCurrentConfigFileName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1) { throw new EcConfigException(string.Format(Errors.ConfigCurrentConfigFileName, Configurations.Filename), null); } configs.Filename = configCurrentConfigFileName; } //Files path var configFilesPath = ConfigurationManager.AppSettings[Configurations.Path]; if (!string.IsNullOrEmpty(configFilesPath)) { if (configFilesPath.IndexOfAny(Path.GetInvalidPathChars()) != -1) { throw new EcConfigException(string.Format(Errors.ConfigConfigFilesPath, Configurations.Path), null); } configs.Path = configFilesPath; } //Case sensitive var configIsCaseSensitive = ConfigurationManager.AppSettings[Configurations.IsCaseSensitive]; if (!string.IsNullOrEmpty(configIsCaseSensitive)) { bool isCaseSensitive; if (!Boolean.TryParse(configIsCaseSensitive, out isCaseSensitive)) { throw new EcConfigException(string.Format(Errors.ConfigIsCaseSensitive, Configurations.IsCaseSensitive), null); } configs.IsCaseSensitive = isCaseSensitive; } CacheManager.Add(EcConfigResources.CacheKey_EcConfigs, configs); } return configs; }
public void GetProperties_ErrorConfigFileNotFound() { EcGlobalConfigurations temp = new EcGlobalConfigurations { Filename = "notexist", Path = ".\\test" }; PropertiesExtractor.GetProperties(temp); }