public void LoadFromAppSettingsJsonSection2() { var assembliesPath = "bin/Debug/netcore3.0"; var csvSearchPatterns = "Shared.*"; var mefSettingsText = $@"""mef"": {{ ""assembliesPath"": ""{assembliesPath}"", ""csvSearchPatterns"": ""{csvSearchPatterns}"" }}"; var appSettingsJsonNewText = //create a new appsettings.json file that has mef settings as a section inside, to use for deserialization $@"{{ ""key1"": ""val1"", ""key2"": 200, ""key3"": true, {mefSettingsText}, ""key4"": {{ ""k41"": ""v41"", ""k42"": ""v42"" }} }}"; File.WriteAllText(settingsFile, appSettingsJsonNewText); //overwrite if exists File.Delete(Mef.MEF_ConfigFileName); //delete mef-dedicated json config file //Act new Mef(); //only for testing; not the usual way of using this; this is to force Init() and reading configuration settings from wherever provided try { Mef.Resolve <IComparable>(); } catch { } //Assert ValidateMefConfiguration(assembliesPath, csvSearchPatterns); }
public void InitCacheWithMefAndJsonConfigAndTestExpirationOverrideInPutCall() { //Arrange var expiration = new TimeSpan(0, 0, 10); //2 seconds for cache to hold a value SetupAppSettingsJsonConfigFile(expiration); //delete any existing dedicated JSON config file (it would try to read from there first) if (File.Exists(CacheManager.CACHE_ConfigFileName)) { File.Delete(CacheManager.CACHE_ConfigFileName); } var valueToCache = 23; var key = "MyIntVal"; var expirationOverride = new TimeSpan(0, 0, 2); //Act: init cache and validate setting var cacheMgr = Mef.Resolve <ICache>(); Assert.IsNotNull(cacheMgr); ValidateNonPublicStaticFieldValue(typeof(CacheManager), "Expiration", expiration); //Act: store value cacheMgr.Put(valueToCache, key, expirationOverride); //Assert: value is immediately available, but not after 2 seconds var cachedValue = cacheMgr.Get <int>(key); Assert.AreEqual(valueToCache, cachedValue); //wait 2 seconds, value should be gone from cache Thread.Sleep(expirationOverride.Seconds * 1000); cachedValue = cacheMgr.Get <int>(key); Assert.AreEqual(0, cachedValue); //0 is default for int }
public void InitCacheWithMefAndJsonConfigFile() { //Arrange var expiration = new TimeSpan(0, 0, 5); //setup dedicated JSON configuration SetupJsonConfigFile(expiration); //Act: resolve ICache with configuration loading from dedicated JSON config file var cacheMgr = Mef.Resolve <ICache>(); //Assert Assert.IsNotNull(cacheMgr); ValidateNonPublicStaticFieldValue(typeof(CacheManager), "Expiration", expiration); }
public void InitMefWithInstanceCtorWithParametersAfterStaticCtorInitFromConfigFile() { //Arrange var assembliesPath = "/abc4"; var csvSearchPatterns = "pat4.*"; //Act - try resolve some contract, but only to invoke static CTOR to read from config file values other than what is above try { Mef.Resolve <IComparable>(); //will fail } catch { } new Mef(assembliesPath, csvSearchPatterns); //init with params that override what the static ctor set above //Assert ValidateMefConfiguration(assembliesPath, csvSearchPatterns); }
public void TestLogLevels3() { //Arrange (use the XML AppSettings to init the logging framework var header = "TestLogLevels3"; var logFilePath = $"{LogFolder}/Some.LogFile3.log"; var log4netConfigFile = "xyzLog4net.config"; var runOnSeparateThread = false; //to avoid delay in logging File.WriteAllText(log4netConfigFile, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath)); //will be restored in test cleanup SetupJsonConfigFile(header, log4netConfigFile, runOnSeparateThread); if (File.Exists(settingsFile)) //delete the JSON appsettings file as well, to force fallback on XML app.config { File.Delete(settingsFile); } string pattern(string txt) => $".*{txt.Replace(" ", "\\s")}"; //Act - 1 var logMgr = Mef.Resolve <ILogManager>(); //Assert - 1 ExecuteAssertions(header, runOnSeparateThread, log4netConfigFile, logFilePath); //as specified in app.config of this project //Act - 2 var logger = logMgr.GetLogger <MefConfigTests>(); logger.Info($"log file: {logFilePath}"); //Assert = 2 ValidateLogEntry(logFilePath, pattern(logFilePath), new[] { LogLevel.Info }); //Act - 3 logger = logMgr.GetLogger(typeof(GeneralConfigTests)); logger.Warn("abc"); //Assert - 3 ValidateLogEntry(logFilePath, pattern("abc"), new[] { LogLevel.Warn }); if (File.Exists(log4netConfigFile)) { File.Delete(log4netConfigFile); } }
public void InitCacheWitMefAndhAppSettingsJsonConfigFile() { //Arrange var expiration = new TimeSpan(0, 12, 0); SetupAppSettingsJsonConfigFile(expiration); //delete any existing dedicated JSON config file (it would try to read from there first) if (File.Exists(CacheManager.CACHE_ConfigFileName)) { File.Delete(CacheManager.CACHE_ConfigFileName); } //Act var cacheMgr = Mef.Resolve <ICache>(); //Assert Assert.IsNotNull(cacheMgr); ValidateNonPublicStaticFieldValue(typeof(CacheManager), "Expiration", expiration); }
public void InitCacheWithMefAndXmlAppSettingsConfigFile() { //Arrange: delete all JSON config files if (File.Exists(CacheManager.CACHE_ConfigFileName)) { File.Delete(CacheManager.CACHE_ConfigFileName); } if (File.Exists(settingsFile)) { File.Delete(settingsFile); //will be restored back in the Test Cleanup (base class) } //in app.config we pre-set the expiration to be 1 day, 3 hours, 0 minutes and 0 seconds (1:3:0:0) //Act var cacheMgr = Mef.Resolve <ICache>(); //Assert Assert.IsNotNull(cacheMgr); ValidateNonPublicStaticFieldValue(typeof(CacheManager), "Expiration", new TimeSpan(1, 3, 0, 0)); //this is what is set in app.config }
public void InitLoggingUsingMefAndJsonConfig() { //Arrange var header = "ABC"; var log4netConfigPath = "log4netTmp.config"; //we will create this file as a copy of the existing log4net.config file, which we delete and retore at the end var logFilePath = $"{LogFolder}/Tmp.log"; var runOnSeparateThread = false; File.WriteAllText(log4netConfigPath, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath)); SetupJsonConfigFile(header, log4netConfigPath, runOnSeparateThread); //Act var logger = Mef.Resolve <ILogManager>(); //Assert ExecuteAssertions(logger, header, runOnSeparateThread, log4netConfigPath, logFilePath); //Cleanup for this test only DeleteLog4netConfig(log4netConfigPath); }
public void TestLogLevels() { //Arrange - use custom JSON config file for logging var header = "TestLogLevels"; var logFilePath = $"{LogFolder}/Some.LogFile.log"; var log4netConfigFile = "myOtherLog4net.config"; var runOnSeparateThread = false; //to avoid delay in logging File.WriteAllText(log4netConfigFile, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath)); //will be restored in test cleanup SetupJsonConfigFile(header, log4netConfigFile, runOnSeparateThread); if (File.Exists(settingsFile)) //delete the JSON appsettings file as well, to force fallback on XML app.config { File.Delete(settingsFile); } var ex = new Exception("Test"); var msg = "Some-123 Message"; var pat = $".*{msg.Replace(" ", "\\s")}"; //Act var logMgr = Mef.Resolve <ILogManager>(); Console.WriteLine($"LoggingManager hash {logMgr.GetHashCode()}"); ExecuteAssertions(logMgr, header, runOnSeparateThread, log4netConfigFile, logFilePath); //as specified in app.config of this project var logger = logMgr.GetLogger <LoggingTests>(); foreach (var lev in LogLevels) { logger.Log(msg, ex, lev); } //Assert ValidateLogEntry(logFilePath, pat); if (File.Exists(log4netConfigFile)) { File.Delete(log4netConfigFile); } }
public void InitLoggingUsingMefAndXmlAppSettingsConfig() { //Arrange var logFilePath = $"{LogFolder}/Some.LogFile.log"; File.WriteAllText(Log4netConfig, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath)); //will be restored in test cleanup if (File.Exists(LoggingManager.Logging_ConfigFileName)) //delete dedicated json config file, if any { File.Delete(LoggingManager.Logging_ConfigFileName); } if (File.Exists(settingsFile)) //delete the JSON appsettings file as well, to force fallback on XML app.config { File.Delete(settingsFile); } //Act var logger = Mef.Resolve <ILogManager>(); //Assert ExecuteAssertions(logger, "*** START Unit tests (Samples) ***", true, Log4netConfig, logFilePath); //as specified in app.config of this project }
public void InitMefFromXMLAppSettings() { //Arrange: delete all custom JSON config files if (File.Exists(Mef.MEF_ConfigFileName)) { File.Delete(Mef.MEF_ConfigFileName); } if (File.Exists(settingsFile)) { File.Delete(settingsFile); } new Mef();//reset configuration (from previous use) - force read from app.config (xml) //Act try { Mef.Resolve <IComparable>(); //will fail } catch { } //Assert ValidateMefConfiguration(".", "Shared.*"); }
public void InitLoggingUsingMefAndAppSettingsJsonConfig() { //Arrange var header = "*****START*****"; var log4netConfigPath = "myLog4net.config"; //we will create this file as a copy of the existing log4net.config file, which we delete and retore at the end var logFilePath = $"{LogFolder}/MyLog.log"; var runOnSeparateThread = false; File.WriteAllText(log4netConfigPath, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath)); SetupAppSettingsJsonConfigFile(header, log4netConfigPath, runOnSeparateThread); if (File.Exists(LoggingManager.Logging_ConfigFileName)) //delete dedicated json config file, if any { File.Delete(LoggingManager.Logging_ConfigFileName); } //Act var logger = Mef.Resolve <ILogManager>(); //Assert ExecuteAssertions(logger, header, runOnSeparateThread, log4netConfigPath, logFilePath); //Cleanup for this test only DeleteLog4netConfig(log4netConfigPath); }
public void TestExportNotFoundExpectingException() { Mef.Resolve <IComparable>(); }